0036-bcm2708-vchiq-driver.patch 408 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198
  1. From 24d3397930cc8faafd020bae31a2c9f1e4682f02 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] 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. @@ -545,6 +545,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. @@ -52,6 +52,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,585 @@
  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) ? FOLL_WRITE : 0 /*Flags */ ,
  2229. + pages,
  2230. + NULL /*vmas */);
  2231. + up_read(&task->mm->mmap_sem);
  2232. +
  2233. + if (actual_pages != num_pages) {
  2234. + vchiq_log_info(vchiq_arm_log_level,
  2235. + "create_pagelist - only %d/%d pages locked",
  2236. + actual_pages,
  2237. + num_pages);
  2238. +
  2239. + /* This is probably due to the process being killed */
  2240. + while (actual_pages > 0)
  2241. + {
  2242. + actual_pages--;
  2243. + page_cache_release(pages[actual_pages]);
  2244. + }
  2245. + kfree(pagelist);
  2246. + if (actual_pages == 0)
  2247. + actual_pages = -ENOMEM;
  2248. + return actual_pages;
  2249. + }
  2250. + *need_release = 1; /* release user pages */
  2251. + }
  2252. +
  2253. + pagelist->length = count;
  2254. + pagelist->type = type;
  2255. + pagelist->offset = offset;
  2256. +
  2257. + /* Group the pages into runs of contiguous pages */
  2258. +
  2259. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  2260. + next_addr = base_addr + PAGE_SIZE;
  2261. + addridx = 0;
  2262. + run = 0;
  2263. +
  2264. + for (i = 1; i < num_pages; i++) {
  2265. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  2266. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  2267. + next_addr += PAGE_SIZE;
  2268. + run++;
  2269. + } else {
  2270. + addrs[addridx] = (unsigned long)base_addr + run;
  2271. + addridx++;
  2272. + base_addr = addr;
  2273. + next_addr = addr + PAGE_SIZE;
  2274. + run = 0;
  2275. + }
  2276. + }
  2277. +
  2278. + addrs[addridx] = (unsigned long)base_addr + run;
  2279. + addridx++;
  2280. +
  2281. + /* Partial cache lines (fragments) require special measures */
  2282. + if ((type == PAGELIST_READ) &&
  2283. + ((pagelist->offset & (g_cache_line_size - 1)) ||
  2284. + ((pagelist->offset + pagelist->length) &
  2285. + (g_cache_line_size - 1)))) {
  2286. + char *fragments;
  2287. +
  2288. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  2289. + kfree(pagelist);
  2290. + return -EINTR;
  2291. + }
  2292. +
  2293. + WARN_ON(g_free_fragments == NULL);
  2294. +
  2295. + down(&g_free_fragments_mutex);
  2296. + fragments = g_free_fragments;
  2297. + WARN_ON(fragments == NULL);
  2298. + g_free_fragments = *(char **) g_free_fragments;
  2299. + up(&g_free_fragments_mutex);
  2300. + pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
  2301. + (fragments - g_fragments_base) / g_fragments_size;
  2302. + }
  2303. +
  2304. + dmac_flush_range(pagelist, addrs + num_pages);
  2305. +
  2306. + *ppagelist = pagelist;
  2307. +
  2308. + return 0;
  2309. +}
  2310. +
  2311. +static void
  2312. +free_pagelist(PAGELIST_T *pagelist, int actual)
  2313. +{
  2314. + unsigned long *need_release;
  2315. + struct page **pages;
  2316. + unsigned int num_pages, i;
  2317. +
  2318. + vchiq_log_trace(vchiq_arm_log_level,
  2319. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  2320. +
  2321. + num_pages =
  2322. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  2323. + PAGE_SIZE;
  2324. +
  2325. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  2326. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  2327. +
  2328. + /* Deal with any partial cache lines (fragments) */
  2329. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  2330. + char *fragments = g_fragments_base +
  2331. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
  2332. + g_fragments_size;
  2333. + int head_bytes, tail_bytes;
  2334. + head_bytes = (g_cache_line_size - pagelist->offset) &
  2335. + (g_cache_line_size - 1);
  2336. + tail_bytes = (pagelist->offset + actual) &
  2337. + (g_cache_line_size - 1);
  2338. +
  2339. + if ((actual >= 0) && (head_bytes != 0)) {
  2340. + if (head_bytes > actual)
  2341. + head_bytes = actual;
  2342. +
  2343. + memcpy((char *)page_address(pages[0]) +
  2344. + pagelist->offset,
  2345. + fragments,
  2346. + head_bytes);
  2347. + }
  2348. + if ((actual >= 0) && (head_bytes < actual) &&
  2349. + (tail_bytes != 0)) {
  2350. + memcpy((char *)page_address(pages[num_pages - 1]) +
  2351. + ((pagelist->offset + actual) &
  2352. + (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
  2353. + fragments + g_cache_line_size,
  2354. + tail_bytes);
  2355. + }
  2356. +
  2357. + down(&g_free_fragments_mutex);
  2358. + *(char **)fragments = g_free_fragments;
  2359. + g_free_fragments = fragments;
  2360. + up(&g_free_fragments_mutex);
  2361. + up(&g_free_fragments_sema);
  2362. + }
  2363. +
  2364. + if (*need_release) {
  2365. + unsigned int length = pagelist->length;
  2366. + unsigned int offset = pagelist->offset;
  2367. +
  2368. + for (i = 0; i < num_pages; i++) {
  2369. + struct page *pg = pages[i];
  2370. +
  2371. + if (pagelist->type != PAGELIST_WRITE) {
  2372. + unsigned int bytes = PAGE_SIZE - offset;
  2373. +
  2374. + if (bytes > length)
  2375. + bytes = length;
  2376. + dmac_unmap_area(page_address(pg) + offset,
  2377. + bytes, DMA_FROM_DEVICE);
  2378. + length -= bytes;
  2379. + offset = 0;
  2380. + set_page_dirty(pg);
  2381. + }
  2382. + page_cache_release(pg);
  2383. + }
  2384. + }
  2385. +
  2386. + kfree(pagelist);
  2387. +}
  2388. --- /dev/null
  2389. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  2390. @@ -0,0 +1,2902 @@
  2391. +/**
  2392. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  2393. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  2394. + *
  2395. + * Redistribution and use in source and binary forms, with or without
  2396. + * modification, are permitted provided that the following conditions
  2397. + * are met:
  2398. + * 1. Redistributions of source code must retain the above copyright
  2399. + * notice, this list of conditions, and the following disclaimer,
  2400. + * without modification.
  2401. + * 2. Redistributions in binary form must reproduce the above copyright
  2402. + * notice, this list of conditions and the following disclaimer in the
  2403. + * documentation and/or other materials provided with the distribution.
  2404. + * 3. The names of the above-listed copyright holders may not be used
  2405. + * to endorse or promote products derived from this software without
  2406. + * specific prior written permission.
  2407. + *
  2408. + * ALTERNATIVELY, this software may be distributed under the terms of the
  2409. + * GNU General Public License ("GPL") version 2, as published by the Free
  2410. + * Software Foundation.
  2411. + *
  2412. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  2413. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  2414. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  2415. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  2416. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  2417. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  2418. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  2419. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2420. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2421. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2422. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2423. + */
  2424. +
  2425. +#include <linux/kernel.h>
  2426. +#include <linux/module.h>
  2427. +#include <linux/types.h>
  2428. +#include <linux/errno.h>
  2429. +#include <linux/cdev.h>
  2430. +#include <linux/fs.h>
  2431. +#include <linux/device.h>
  2432. +#include <linux/mm.h>
  2433. +#include <linux/highmem.h>
  2434. +#include <linux/pagemap.h>
  2435. +#include <linux/bug.h>
  2436. +#include <linux/semaphore.h>
  2437. +#include <linux/list.h>
  2438. +#include <linux/of.h>
  2439. +#include <linux/platform_device.h>
  2440. +#include <soc/bcm2835/raspberrypi-firmware.h>
  2441. +
  2442. +#include "vchiq_core.h"
  2443. +#include "vchiq_ioctl.h"
  2444. +#include "vchiq_arm.h"
  2445. +#include "vchiq_debugfs.h"
  2446. +#include "vchiq_killable.h"
  2447. +
  2448. +#define DEVICE_NAME "vchiq"
  2449. +
  2450. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  2451. +#undef MODULE_PARAM_PREFIX
  2452. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  2453. +
  2454. +#define VCHIQ_MINOR 0
  2455. +
  2456. +/* Some per-instance constants */
  2457. +#define MAX_COMPLETIONS 16
  2458. +#define MAX_SERVICES 64
  2459. +#define MAX_ELEMENTS 8
  2460. +#define MSG_QUEUE_SIZE 64
  2461. +
  2462. +#define KEEPALIVE_VER 1
  2463. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  2464. +
  2465. +/* Run time control of log level, based on KERN_XXX level. */
  2466. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  2467. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  2468. +
  2469. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  2470. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  2471. +
  2472. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  2473. +static const char *const suspend_state_names[] = {
  2474. + "VC_SUSPEND_FORCE_CANCELED",
  2475. + "VC_SUSPEND_REJECTED",
  2476. + "VC_SUSPEND_FAILED",
  2477. + "VC_SUSPEND_IDLE",
  2478. + "VC_SUSPEND_REQUESTED",
  2479. + "VC_SUSPEND_IN_PROGRESS",
  2480. + "VC_SUSPEND_SUSPENDED"
  2481. +};
  2482. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  2483. +static const char *const resume_state_names[] = {
  2484. + "VC_RESUME_FAILED",
  2485. + "VC_RESUME_IDLE",
  2486. + "VC_RESUME_REQUESTED",
  2487. + "VC_RESUME_IN_PROGRESS",
  2488. + "VC_RESUME_RESUMED"
  2489. +};
  2490. +/* The number of times we allow force suspend to timeout before actually
  2491. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  2492. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  2493. +*/
  2494. +#define FORCE_SUSPEND_FAIL_MAX 8
  2495. +
  2496. +/* The time in ms allowed for videocore to go idle when force suspend has been
  2497. + * requested */
  2498. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  2499. +
  2500. +
  2501. +static void suspend_timer_callback(unsigned long context);
  2502. +
  2503. +
  2504. +typedef struct user_service_struct {
  2505. + VCHIQ_SERVICE_T *service;
  2506. + void *userdata;
  2507. + VCHIQ_INSTANCE_T instance;
  2508. + char is_vchi;
  2509. + char dequeue_pending;
  2510. + char close_pending;
  2511. + int message_available_pos;
  2512. + int msg_insert;
  2513. + int msg_remove;
  2514. + struct semaphore insert_event;
  2515. + struct semaphore remove_event;
  2516. + struct semaphore close_event;
  2517. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  2518. +} USER_SERVICE_T;
  2519. +
  2520. +struct bulk_waiter_node {
  2521. + struct bulk_waiter bulk_waiter;
  2522. + int pid;
  2523. + struct list_head list;
  2524. +};
  2525. +
  2526. +struct vchiq_instance_struct {
  2527. + VCHIQ_STATE_T *state;
  2528. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  2529. + int completion_insert;
  2530. + int completion_remove;
  2531. + struct semaphore insert_event;
  2532. + struct semaphore remove_event;
  2533. + struct mutex completion_mutex;
  2534. +
  2535. + int connected;
  2536. + int closing;
  2537. + int pid;
  2538. + int mark;
  2539. + int use_close_delivered;
  2540. + int trace;
  2541. +
  2542. + struct list_head bulk_waiter_list;
  2543. + struct mutex bulk_waiter_list_mutex;
  2544. +
  2545. + VCHIQ_DEBUGFS_NODE_T debugfs_node;
  2546. +};
  2547. +
  2548. +typedef struct dump_context_struct {
  2549. + char __user *buf;
  2550. + size_t actual;
  2551. + size_t space;
  2552. + loff_t offset;
  2553. +} DUMP_CONTEXT_T;
  2554. +
  2555. +static struct cdev vchiq_cdev;
  2556. +static dev_t vchiq_devid;
  2557. +static VCHIQ_STATE_T g_state;
  2558. +static struct class *vchiq_class;
  2559. +static struct device *vchiq_dev;
  2560. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  2561. +
  2562. +static const char *const ioctl_names[] = {
  2563. + "CONNECT",
  2564. + "SHUTDOWN",
  2565. + "CREATE_SERVICE",
  2566. + "REMOVE_SERVICE",
  2567. + "QUEUE_MESSAGE",
  2568. + "QUEUE_BULK_TRANSMIT",
  2569. + "QUEUE_BULK_RECEIVE",
  2570. + "AWAIT_COMPLETION",
  2571. + "DEQUEUE_MESSAGE",
  2572. + "GET_CLIENT_ID",
  2573. + "GET_CONFIG",
  2574. + "CLOSE_SERVICE",
  2575. + "USE_SERVICE",
  2576. + "RELEASE_SERVICE",
  2577. + "SET_SERVICE_OPTION",
  2578. + "DUMP_PHYS_MEM",
  2579. + "LIB_VERSION",
  2580. + "CLOSE_DELIVERED"
  2581. +};
  2582. +
  2583. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  2584. + (VCHIQ_IOC_MAX + 1));
  2585. +
  2586. +static void
  2587. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  2588. +
  2589. +/****************************************************************************
  2590. +*
  2591. +* add_completion
  2592. +*
  2593. +***************************************************************************/
  2594. +
  2595. +static VCHIQ_STATUS_T
  2596. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  2597. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  2598. + void *bulk_userdata)
  2599. +{
  2600. + VCHIQ_COMPLETION_DATA_T *completion;
  2601. + DEBUG_INITIALISE(g_state.local)
  2602. +
  2603. + while (instance->completion_insert ==
  2604. + (instance->completion_remove + MAX_COMPLETIONS)) {
  2605. + /* Out of space - wait for the client */
  2606. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2607. + vchiq_log_trace(vchiq_arm_log_level,
  2608. + "add_completion - completion queue full");
  2609. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  2610. + if (down_interruptible(&instance->remove_event) != 0) {
  2611. + vchiq_log_info(vchiq_arm_log_level,
  2612. + "service_callback interrupted");
  2613. + return VCHIQ_RETRY;
  2614. + } else if (instance->closing) {
  2615. + vchiq_log_info(vchiq_arm_log_level,
  2616. + "service_callback closing");
  2617. + return VCHIQ_ERROR;
  2618. + }
  2619. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2620. + }
  2621. +
  2622. + completion =
  2623. + &instance->completions[instance->completion_insert &
  2624. + (MAX_COMPLETIONS - 1)];
  2625. +
  2626. + completion->header = header;
  2627. + completion->reason = reason;
  2628. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  2629. + completion->service_userdata = user_service->service;
  2630. + completion->bulk_userdata = bulk_userdata;
  2631. +
  2632. + if (reason == VCHIQ_SERVICE_CLOSED) {
  2633. + /* Take an extra reference, to be held until
  2634. + this CLOSED notification is delivered. */
  2635. + lock_service(user_service->service);
  2636. + if (instance->use_close_delivered)
  2637. + user_service->close_pending = 1;
  2638. + }
  2639. +
  2640. + /* A write barrier is needed here to ensure that the entire completion
  2641. + record is written out before the insert point. */
  2642. + wmb();
  2643. +
  2644. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  2645. + user_service->message_available_pos =
  2646. + instance->completion_insert;
  2647. + instance->completion_insert++;
  2648. +
  2649. + up(&instance->insert_event);
  2650. +
  2651. + return VCHIQ_SUCCESS;
  2652. +}
  2653. +
  2654. +/****************************************************************************
  2655. +*
  2656. +* service_callback
  2657. +*
  2658. +***************************************************************************/
  2659. +
  2660. +static VCHIQ_STATUS_T
  2661. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  2662. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  2663. +{
  2664. + /* How do we ensure the callback goes to the right client?
  2665. + ** The service_user data points to a USER_SERVICE_T record containing
  2666. + ** the original callback and the user state structure, which contains a
  2667. + ** circular buffer for completion records.
  2668. + */
  2669. + USER_SERVICE_T *user_service;
  2670. + VCHIQ_SERVICE_T *service;
  2671. + VCHIQ_INSTANCE_T instance;
  2672. + DEBUG_INITIALISE(g_state.local)
  2673. +
  2674. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2675. +
  2676. + service = handle_to_service(handle);
  2677. + BUG_ON(!service);
  2678. + user_service = (USER_SERVICE_T *)service->base.userdata;
  2679. + instance = user_service->instance;
  2680. +
  2681. + if (!instance || instance->closing)
  2682. + return VCHIQ_SUCCESS;
  2683. +
  2684. + vchiq_log_trace(vchiq_arm_log_level,
  2685. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  2686. + "instance %lx, bulk_userdata %lx",
  2687. + (unsigned long)user_service,
  2688. + service->localport, user_service->userdata,
  2689. + reason, (unsigned long)header,
  2690. + (unsigned long)instance, (unsigned long)bulk_userdata);
  2691. +
  2692. + if (header && user_service->is_vchi) {
  2693. + spin_lock(&msg_queue_spinlock);
  2694. + while (user_service->msg_insert ==
  2695. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  2696. + spin_unlock(&msg_queue_spinlock);
  2697. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2698. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  2699. + vchiq_log_trace(vchiq_arm_log_level,
  2700. + "service_callback - msg queue full");
  2701. + /* If there is no MESSAGE_AVAILABLE in the completion
  2702. + ** queue, add one
  2703. + */
  2704. + if ((user_service->message_available_pos -
  2705. + instance->completion_remove) < 0) {
  2706. + VCHIQ_STATUS_T status;
  2707. + vchiq_log_info(vchiq_arm_log_level,
  2708. + "Inserting extra MESSAGE_AVAILABLE");
  2709. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2710. + status = add_completion(instance, reason,
  2711. + NULL, user_service, bulk_userdata);
  2712. + if (status != VCHIQ_SUCCESS) {
  2713. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2714. + return status;
  2715. + }
  2716. + }
  2717. +
  2718. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2719. + if (down_interruptible(&user_service->remove_event)
  2720. + != 0) {
  2721. + vchiq_log_info(vchiq_arm_log_level,
  2722. + "service_callback interrupted");
  2723. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2724. + return VCHIQ_RETRY;
  2725. + } else if (instance->closing) {
  2726. + vchiq_log_info(vchiq_arm_log_level,
  2727. + "service_callback closing");
  2728. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2729. + return VCHIQ_ERROR;
  2730. + }
  2731. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2732. + spin_lock(&msg_queue_spinlock);
  2733. + }
  2734. +
  2735. + user_service->msg_queue[user_service->msg_insert &
  2736. + (MSG_QUEUE_SIZE - 1)] = header;
  2737. + user_service->msg_insert++;
  2738. + spin_unlock(&msg_queue_spinlock);
  2739. +
  2740. + up(&user_service->insert_event);
  2741. +
  2742. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  2743. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  2744. + ** bypass the completion queue.
  2745. + */
  2746. + if (((user_service->message_available_pos -
  2747. + instance->completion_remove) >= 0) ||
  2748. + user_service->dequeue_pending) {
  2749. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2750. + user_service->dequeue_pending = 0;
  2751. + return VCHIQ_SUCCESS;
  2752. + }
  2753. +
  2754. + header = NULL;
  2755. + }
  2756. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2757. +
  2758. + return add_completion(instance, reason, header, user_service,
  2759. + bulk_userdata);
  2760. +}
  2761. +
  2762. +/****************************************************************************
  2763. +*
  2764. +* user_service_free
  2765. +*
  2766. +***************************************************************************/
  2767. +static void
  2768. +user_service_free(void *userdata)
  2769. +{
  2770. + kfree(userdata);
  2771. +}
  2772. +
  2773. +/****************************************************************************
  2774. +*
  2775. +* close_delivered
  2776. +*
  2777. +***************************************************************************/
  2778. +static void close_delivered(USER_SERVICE_T *user_service)
  2779. +{
  2780. + vchiq_log_info(vchiq_arm_log_level,
  2781. + "close_delivered(handle=%x)",
  2782. + user_service->service->handle);
  2783. +
  2784. + if (user_service->close_pending) {
  2785. + /* Allow the underlying service to be culled */
  2786. + unlock_service(user_service->service);
  2787. +
  2788. + /* Wake the user-thread blocked in close_ or remove_service */
  2789. + up(&user_service->close_event);
  2790. +
  2791. + user_service->close_pending = 0;
  2792. + }
  2793. +}
  2794. +
  2795. +/****************************************************************************
  2796. +*
  2797. +* vchiq_ioctl
  2798. +*
  2799. +***************************************************************************/
  2800. +static long
  2801. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2802. +{
  2803. + VCHIQ_INSTANCE_T instance = file->private_data;
  2804. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  2805. + VCHIQ_SERVICE_T *service = NULL;
  2806. + long ret = 0;
  2807. + int i, rc;
  2808. + DEBUG_INITIALISE(g_state.local)
  2809. +
  2810. + vchiq_log_trace(vchiq_arm_log_level,
  2811. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  2812. + (unsigned int)instance,
  2813. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  2814. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  2815. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  2816. +
  2817. + switch (cmd) {
  2818. + case VCHIQ_IOC_SHUTDOWN:
  2819. + if (!instance->connected)
  2820. + break;
  2821. +
  2822. + /* Remove all services */
  2823. + i = 0;
  2824. + while ((service = next_service_by_instance(instance->state,
  2825. + instance, &i)) != NULL) {
  2826. + status = vchiq_remove_service(service->handle);
  2827. + unlock_service(service);
  2828. + if (status != VCHIQ_SUCCESS)
  2829. + break;
  2830. + }
  2831. + service = NULL;
  2832. +
  2833. + if (status == VCHIQ_SUCCESS) {
  2834. + /* Wake the completion thread and ask it to exit */
  2835. + instance->closing = 1;
  2836. + up(&instance->insert_event);
  2837. + }
  2838. +
  2839. + break;
  2840. +
  2841. + case VCHIQ_IOC_CONNECT:
  2842. + if (instance->connected) {
  2843. + ret = -EINVAL;
  2844. + break;
  2845. + }
  2846. + rc = mutex_lock_interruptible(&instance->state->mutex);
  2847. + if (rc != 0) {
  2848. + vchiq_log_error(vchiq_arm_log_level,
  2849. + "vchiq: connect: could not lock mutex for "
  2850. + "state %d: %d",
  2851. + instance->state->id, rc);
  2852. + ret = -EINTR;
  2853. + break;
  2854. + }
  2855. + status = vchiq_connect_internal(instance->state, instance);
  2856. + mutex_unlock(&instance->state->mutex);
  2857. +
  2858. + if (status == VCHIQ_SUCCESS)
  2859. + instance->connected = 1;
  2860. + else
  2861. + vchiq_log_error(vchiq_arm_log_level,
  2862. + "vchiq: could not connect: %d", status);
  2863. + break;
  2864. +
  2865. + case VCHIQ_IOC_CREATE_SERVICE: {
  2866. + VCHIQ_CREATE_SERVICE_T args;
  2867. + USER_SERVICE_T *user_service = NULL;
  2868. + void *userdata;
  2869. + int srvstate;
  2870. +
  2871. + if (copy_from_user
  2872. + (&args, (const void __user *)arg,
  2873. + sizeof(args)) != 0) {
  2874. + ret = -EFAULT;
  2875. + break;
  2876. + }
  2877. +
  2878. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  2879. + if (!user_service) {
  2880. + ret = -ENOMEM;
  2881. + break;
  2882. + }
  2883. +
  2884. + if (args.is_open) {
  2885. + if (!instance->connected) {
  2886. + ret = -ENOTCONN;
  2887. + kfree(user_service);
  2888. + break;
  2889. + }
  2890. + srvstate = VCHIQ_SRVSTATE_OPENING;
  2891. + } else {
  2892. + srvstate =
  2893. + instance->connected ?
  2894. + VCHIQ_SRVSTATE_LISTENING :
  2895. + VCHIQ_SRVSTATE_HIDDEN;
  2896. + }
  2897. +
  2898. + userdata = args.params.userdata;
  2899. + args.params.callback = service_callback;
  2900. + args.params.userdata = user_service;
  2901. + service = vchiq_add_service_internal(
  2902. + instance->state,
  2903. + &args.params, srvstate,
  2904. + instance, user_service_free);
  2905. +
  2906. + if (service != NULL) {
  2907. + user_service->service = service;
  2908. + user_service->userdata = userdata;
  2909. + user_service->instance = instance;
  2910. + user_service->is_vchi = (args.is_vchi != 0);
  2911. + user_service->dequeue_pending = 0;
  2912. + user_service->close_pending = 0;
  2913. + user_service->message_available_pos =
  2914. + instance->completion_remove - 1;
  2915. + user_service->msg_insert = 0;
  2916. + user_service->msg_remove = 0;
  2917. + sema_init(&user_service->insert_event, 0);
  2918. + sema_init(&user_service->remove_event, 0);
  2919. + sema_init(&user_service->close_event, 0);
  2920. +
  2921. + if (args.is_open) {
  2922. + status = vchiq_open_service_internal
  2923. + (service, instance->pid);
  2924. + if (status != VCHIQ_SUCCESS) {
  2925. + vchiq_remove_service(service->handle);
  2926. + service = NULL;
  2927. + ret = (status == VCHIQ_RETRY) ?
  2928. + -EINTR : -EIO;
  2929. + break;
  2930. + }
  2931. + }
  2932. +
  2933. + if (copy_to_user((void __user *)
  2934. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  2935. + arg)->handle),
  2936. + (const void *)&service->handle,
  2937. + sizeof(service->handle)) != 0) {
  2938. + ret = -EFAULT;
  2939. + vchiq_remove_service(service->handle);
  2940. + }
  2941. +
  2942. + service = NULL;
  2943. + } else {
  2944. + ret = -EEXIST;
  2945. + kfree(user_service);
  2946. + }
  2947. + } break;
  2948. +
  2949. + case VCHIQ_IOC_CLOSE_SERVICE: {
  2950. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2951. +
  2952. + service = find_service_for_instance(instance, handle);
  2953. + if (service != NULL) {
  2954. + USER_SERVICE_T *user_service =
  2955. + (USER_SERVICE_T *)service->base.userdata;
  2956. + /* close_pending is false on first entry, and when the
  2957. + wait in vchiq_close_service has been interrupted. */
  2958. + if (!user_service->close_pending) {
  2959. + status = vchiq_close_service(service->handle);
  2960. + if (status != VCHIQ_SUCCESS)
  2961. + break;
  2962. + }
  2963. +
  2964. + /* close_pending is true once the underlying service
  2965. + has been closed until the client library calls the
  2966. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2967. + if (user_service->close_pending &&
  2968. + down_interruptible(&user_service->close_event))
  2969. + status = VCHIQ_RETRY;
  2970. + }
  2971. + else
  2972. + ret = -EINVAL;
  2973. + } break;
  2974. +
  2975. + case VCHIQ_IOC_REMOVE_SERVICE: {
  2976. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2977. +
  2978. + service = find_service_for_instance(instance, handle);
  2979. + if (service != NULL) {
  2980. + USER_SERVICE_T *user_service =
  2981. + (USER_SERVICE_T *)service->base.userdata;
  2982. + /* close_pending is false on first entry, and when the
  2983. + wait in vchiq_close_service has been interrupted. */
  2984. + if (!user_service->close_pending) {
  2985. + status = vchiq_remove_service(service->handle);
  2986. + if (status != VCHIQ_SUCCESS)
  2987. + break;
  2988. + }
  2989. +
  2990. + /* close_pending is true once the underlying service
  2991. + has been closed until the client library calls the
  2992. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2993. + if (user_service->close_pending &&
  2994. + down_interruptible(&user_service->close_event))
  2995. + status = VCHIQ_RETRY;
  2996. + }
  2997. + else
  2998. + ret = -EINVAL;
  2999. + } break;
  3000. +
  3001. + case VCHIQ_IOC_USE_SERVICE:
  3002. + case VCHIQ_IOC_RELEASE_SERVICE: {
  3003. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3004. +
  3005. + service = find_service_for_instance(instance, handle);
  3006. + if (service != NULL) {
  3007. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  3008. + vchiq_use_service_internal(service) :
  3009. + vchiq_release_service_internal(service);
  3010. + if (status != VCHIQ_SUCCESS) {
  3011. + vchiq_log_error(vchiq_susp_log_level,
  3012. + "%s: cmd %s returned error %d for "
  3013. + "service %c%c%c%c:%03d",
  3014. + __func__,
  3015. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  3016. + "VCHIQ_IOC_USE_SERVICE" :
  3017. + "VCHIQ_IOC_RELEASE_SERVICE",
  3018. + status,
  3019. + VCHIQ_FOURCC_AS_4CHARS(
  3020. + service->base.fourcc),
  3021. + service->client_id);
  3022. + ret = -EINVAL;
  3023. + }
  3024. + } else
  3025. + ret = -EINVAL;
  3026. + } break;
  3027. +
  3028. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  3029. + VCHIQ_QUEUE_MESSAGE_T args;
  3030. + if (copy_from_user
  3031. + (&args, (const void __user *)arg,
  3032. + sizeof(args)) != 0) {
  3033. + ret = -EFAULT;
  3034. + break;
  3035. + }
  3036. +
  3037. + service = find_service_for_instance(instance, args.handle);
  3038. +
  3039. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  3040. + /* Copy elements into kernel space */
  3041. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  3042. + if (copy_from_user(elements, args.elements,
  3043. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  3044. + status = vchiq_queue_message
  3045. + (args.handle,
  3046. + elements, args.count);
  3047. + else
  3048. + ret = -EFAULT;
  3049. + } else {
  3050. + ret = -EINVAL;
  3051. + }
  3052. + } break;
  3053. +
  3054. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  3055. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  3056. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  3057. + struct bulk_waiter_node *waiter = NULL;
  3058. + VCHIQ_BULK_DIR_T dir =
  3059. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  3060. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  3061. +
  3062. + if (copy_from_user
  3063. + (&args, (const void __user *)arg,
  3064. + sizeof(args)) != 0) {
  3065. + ret = -EFAULT;
  3066. + break;
  3067. + }
  3068. +
  3069. + service = find_service_for_instance(instance, args.handle);
  3070. + if (!service) {
  3071. + ret = -EINVAL;
  3072. + break;
  3073. + }
  3074. +
  3075. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  3076. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  3077. + GFP_KERNEL);
  3078. + if (!waiter) {
  3079. + ret = -ENOMEM;
  3080. + break;
  3081. + }
  3082. + args.userdata = &waiter->bulk_waiter;
  3083. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  3084. + struct list_head *pos;
  3085. + mutex_lock(&instance->bulk_waiter_list_mutex);
  3086. + list_for_each(pos, &instance->bulk_waiter_list) {
  3087. + if (list_entry(pos, struct bulk_waiter_node,
  3088. + list)->pid == current->pid) {
  3089. + waiter = list_entry(pos,
  3090. + struct bulk_waiter_node,
  3091. + list);
  3092. + list_del(pos);
  3093. + break;
  3094. + }
  3095. +
  3096. + }
  3097. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  3098. + if (!waiter) {
  3099. + vchiq_log_error(vchiq_arm_log_level,
  3100. + "no bulk_waiter found for pid %d",
  3101. + current->pid);
  3102. + ret = -ESRCH;
  3103. + break;
  3104. + }
  3105. + vchiq_log_info(vchiq_arm_log_level,
  3106. + "found bulk_waiter %x for pid %d",
  3107. + (unsigned int)waiter, current->pid);
  3108. + args.userdata = &waiter->bulk_waiter;
  3109. + }
  3110. + status = vchiq_bulk_transfer
  3111. + (args.handle,
  3112. + VCHI_MEM_HANDLE_INVALID,
  3113. + args.data, args.size,
  3114. + args.userdata, args.mode,
  3115. + dir);
  3116. + if (!waiter)
  3117. + break;
  3118. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  3119. + !waiter->bulk_waiter.bulk) {
  3120. + if (waiter->bulk_waiter.bulk) {
  3121. + /* Cancel the signal when the transfer
  3122. + ** completes. */
  3123. + spin_lock(&bulk_waiter_spinlock);
  3124. + waiter->bulk_waiter.bulk->userdata = NULL;
  3125. + spin_unlock(&bulk_waiter_spinlock);
  3126. + }
  3127. + kfree(waiter);
  3128. + } else {
  3129. + const VCHIQ_BULK_MODE_T mode_waiting =
  3130. + VCHIQ_BULK_MODE_WAITING;
  3131. + waiter->pid = current->pid;
  3132. + mutex_lock(&instance->bulk_waiter_list_mutex);
  3133. + list_add(&waiter->list, &instance->bulk_waiter_list);
  3134. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  3135. + vchiq_log_info(vchiq_arm_log_level,
  3136. + "saved bulk_waiter %x for pid %d",
  3137. + (unsigned int)waiter, current->pid);
  3138. +
  3139. + if (copy_to_user((void __user *)
  3140. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  3141. + arg)->mode),
  3142. + (const void *)&mode_waiting,
  3143. + sizeof(mode_waiting)) != 0)
  3144. + ret = -EFAULT;
  3145. + }
  3146. + } break;
  3147. +
  3148. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  3149. + VCHIQ_AWAIT_COMPLETION_T args;
  3150. +
  3151. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3152. + if (!instance->connected) {
  3153. + ret = -ENOTCONN;
  3154. + break;
  3155. + }
  3156. +
  3157. + if (copy_from_user(&args, (const void __user *)arg,
  3158. + sizeof(args)) != 0) {
  3159. + ret = -EFAULT;
  3160. + break;
  3161. + }
  3162. +
  3163. + mutex_lock(&instance->completion_mutex);
  3164. +
  3165. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3166. + while ((instance->completion_remove ==
  3167. + instance->completion_insert)
  3168. + && !instance->closing) {
  3169. + int rc;
  3170. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3171. + mutex_unlock(&instance->completion_mutex);
  3172. + rc = down_interruptible(&instance->insert_event);
  3173. + mutex_lock(&instance->completion_mutex);
  3174. + if (rc != 0) {
  3175. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3176. + vchiq_log_info(vchiq_arm_log_level,
  3177. + "AWAIT_COMPLETION interrupted");
  3178. + ret = -EINTR;
  3179. + break;
  3180. + }
  3181. + }
  3182. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3183. +
  3184. + /* A read memory barrier is needed to stop prefetch of a stale
  3185. + ** completion record
  3186. + */
  3187. + rmb();
  3188. +
  3189. + if (ret == 0) {
  3190. + int msgbufcount = args.msgbufcount;
  3191. + for (ret = 0; ret < args.count; ret++) {
  3192. + VCHIQ_COMPLETION_DATA_T *completion;
  3193. + VCHIQ_SERVICE_T *service;
  3194. + USER_SERVICE_T *user_service;
  3195. + VCHIQ_HEADER_T *header;
  3196. + if (instance->completion_remove ==
  3197. + instance->completion_insert)
  3198. + break;
  3199. + completion = &instance->completions[
  3200. + instance->completion_remove &
  3201. + (MAX_COMPLETIONS - 1)];
  3202. +
  3203. + service = completion->service_userdata;
  3204. + user_service = service->base.userdata;
  3205. + completion->service_userdata =
  3206. + user_service->userdata;
  3207. +
  3208. + header = completion->header;
  3209. + if (header) {
  3210. + void __user *msgbuf;
  3211. + int msglen;
  3212. +
  3213. + msglen = header->size +
  3214. + sizeof(VCHIQ_HEADER_T);
  3215. + /* This must be a VCHIQ-style service */
  3216. + if (args.msgbufsize < msglen) {
  3217. + vchiq_log_error(
  3218. + vchiq_arm_log_level,
  3219. + "header %x: msgbufsize"
  3220. + " %x < msglen %x",
  3221. + (unsigned int)header,
  3222. + args.msgbufsize,
  3223. + msglen);
  3224. + WARN(1, "invalid message "
  3225. + "size\n");
  3226. + if (ret == 0)
  3227. + ret = -EMSGSIZE;
  3228. + break;
  3229. + }
  3230. + if (msgbufcount <= 0)
  3231. + /* Stall here for lack of a
  3232. + ** buffer for the message. */
  3233. + break;
  3234. + /* Get the pointer from user space */
  3235. + msgbufcount--;
  3236. + if (copy_from_user(&msgbuf,
  3237. + (const void __user *)
  3238. + &args.msgbufs[msgbufcount],
  3239. + sizeof(msgbuf)) != 0) {
  3240. + if (ret == 0)
  3241. + ret = -EFAULT;
  3242. + break;
  3243. + }
  3244. +
  3245. + /* Copy the message to user space */
  3246. + if (copy_to_user(msgbuf, header,
  3247. + msglen) != 0) {
  3248. + if (ret == 0)
  3249. + ret = -EFAULT;
  3250. + break;
  3251. + }
  3252. +
  3253. + /* Now it has been copied, the message
  3254. + ** can be released. */
  3255. + vchiq_release_message(service->handle,
  3256. + header);
  3257. +
  3258. + /* The completion must point to the
  3259. + ** msgbuf. */
  3260. + completion->header = msgbuf;
  3261. + }
  3262. +
  3263. + if ((completion->reason ==
  3264. + VCHIQ_SERVICE_CLOSED) &&
  3265. + !instance->use_close_delivered)
  3266. + unlock_service(service);
  3267. +
  3268. + if (copy_to_user((void __user *)(
  3269. + (size_t)args.buf +
  3270. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  3271. + completion,
  3272. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  3273. + if (ret == 0)
  3274. + ret = -EFAULT;
  3275. + break;
  3276. + }
  3277. +
  3278. + instance->completion_remove++;
  3279. + }
  3280. +
  3281. + if (msgbufcount != args.msgbufcount) {
  3282. + if (copy_to_user((void __user *)
  3283. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  3284. + msgbufcount,
  3285. + &msgbufcount,
  3286. + sizeof(msgbufcount)) != 0) {
  3287. + ret = -EFAULT;
  3288. + }
  3289. + }
  3290. + }
  3291. +
  3292. + if (ret != 0)
  3293. + up(&instance->remove_event);
  3294. + mutex_unlock(&instance->completion_mutex);
  3295. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3296. + } break;
  3297. +
  3298. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  3299. + VCHIQ_DEQUEUE_MESSAGE_T args;
  3300. + USER_SERVICE_T *user_service;
  3301. + VCHIQ_HEADER_T *header;
  3302. +
  3303. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3304. + if (copy_from_user
  3305. + (&args, (const void __user *)arg,
  3306. + sizeof(args)) != 0) {
  3307. + ret = -EFAULT;
  3308. + break;
  3309. + }
  3310. + service = find_service_for_instance(instance, args.handle);
  3311. + if (!service) {
  3312. + ret = -EINVAL;
  3313. + break;
  3314. + }
  3315. + user_service = (USER_SERVICE_T *)service->base.userdata;
  3316. + if (user_service->is_vchi == 0) {
  3317. + ret = -EINVAL;
  3318. + break;
  3319. + }
  3320. +
  3321. + spin_lock(&msg_queue_spinlock);
  3322. + if (user_service->msg_remove == user_service->msg_insert) {
  3323. + if (!args.blocking) {
  3324. + spin_unlock(&msg_queue_spinlock);
  3325. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3326. + ret = -EWOULDBLOCK;
  3327. + break;
  3328. + }
  3329. + user_service->dequeue_pending = 1;
  3330. + do {
  3331. + spin_unlock(&msg_queue_spinlock);
  3332. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3333. + if (down_interruptible(
  3334. + &user_service->insert_event) != 0) {
  3335. + vchiq_log_info(vchiq_arm_log_level,
  3336. + "DEQUEUE_MESSAGE interrupted");
  3337. + ret = -EINTR;
  3338. + break;
  3339. + }
  3340. + spin_lock(&msg_queue_spinlock);
  3341. + } while (user_service->msg_remove ==
  3342. + user_service->msg_insert);
  3343. +
  3344. + if (ret)
  3345. + break;
  3346. + }
  3347. +
  3348. + BUG_ON((int)(user_service->msg_insert -
  3349. + user_service->msg_remove) < 0);
  3350. +
  3351. + header = user_service->msg_queue[user_service->msg_remove &
  3352. + (MSG_QUEUE_SIZE - 1)];
  3353. + user_service->msg_remove++;
  3354. + spin_unlock(&msg_queue_spinlock);
  3355. +
  3356. + up(&user_service->remove_event);
  3357. + if (header == NULL)
  3358. + ret = -ENOTCONN;
  3359. + else if (header->size <= args.bufsize) {
  3360. + /* Copy to user space if msgbuf is not NULL */
  3361. + if ((args.buf == NULL) ||
  3362. + (copy_to_user((void __user *)args.buf,
  3363. + header->data,
  3364. + header->size) == 0)) {
  3365. + ret = header->size;
  3366. + vchiq_release_message(
  3367. + service->handle,
  3368. + header);
  3369. + } else
  3370. + ret = -EFAULT;
  3371. + } else {
  3372. + vchiq_log_error(vchiq_arm_log_level,
  3373. + "header %x: bufsize %x < size %x",
  3374. + (unsigned int)header, args.bufsize,
  3375. + header->size);
  3376. + WARN(1, "invalid size\n");
  3377. + ret = -EMSGSIZE;
  3378. + }
  3379. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3380. + } break;
  3381. +
  3382. + case VCHIQ_IOC_GET_CLIENT_ID: {
  3383. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3384. +
  3385. + ret = vchiq_get_client_id(handle);
  3386. + } break;
  3387. +
  3388. + case VCHIQ_IOC_GET_CONFIG: {
  3389. + VCHIQ_GET_CONFIG_T args;
  3390. + VCHIQ_CONFIG_T config;
  3391. +
  3392. + if (copy_from_user(&args, (const void __user *)arg,
  3393. + sizeof(args)) != 0) {
  3394. + ret = -EFAULT;
  3395. + break;
  3396. + }
  3397. + if (args.config_size > sizeof(config)) {
  3398. + ret = -EINVAL;
  3399. + break;
  3400. + }
  3401. + status = vchiq_get_config(instance, args.config_size, &config);
  3402. + if (status == VCHIQ_SUCCESS) {
  3403. + if (copy_to_user((void __user *)args.pconfig,
  3404. + &config, args.config_size) != 0) {
  3405. + ret = -EFAULT;
  3406. + break;
  3407. + }
  3408. + }
  3409. + } break;
  3410. +
  3411. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  3412. + VCHIQ_SET_SERVICE_OPTION_T args;
  3413. +
  3414. + if (copy_from_user(
  3415. + &args, (const void __user *)arg,
  3416. + sizeof(args)) != 0) {
  3417. + ret = -EFAULT;
  3418. + break;
  3419. + }
  3420. +
  3421. + service = find_service_for_instance(instance, args.handle);
  3422. + if (!service) {
  3423. + ret = -EINVAL;
  3424. + break;
  3425. + }
  3426. +
  3427. + status = vchiq_set_service_option(
  3428. + args.handle, args.option, args.value);
  3429. + } break;
  3430. +
  3431. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  3432. + VCHIQ_DUMP_MEM_T args;
  3433. +
  3434. + if (copy_from_user
  3435. + (&args, (const void __user *)arg,
  3436. + sizeof(args)) != 0) {
  3437. + ret = -EFAULT;
  3438. + break;
  3439. + }
  3440. + dump_phys_mem(args.virt_addr, args.num_bytes);
  3441. + } break;
  3442. +
  3443. + case VCHIQ_IOC_LIB_VERSION: {
  3444. + unsigned int lib_version = (unsigned int)arg;
  3445. +
  3446. + if (lib_version < VCHIQ_VERSION_MIN)
  3447. + ret = -EINVAL;
  3448. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  3449. + instance->use_close_delivered = 1;
  3450. + } break;
  3451. +
  3452. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  3453. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3454. +
  3455. + service = find_closed_service_for_instance(instance, handle);
  3456. + if (service != NULL) {
  3457. + USER_SERVICE_T *user_service =
  3458. + (USER_SERVICE_T *)service->base.userdata;
  3459. + close_delivered(user_service);
  3460. + }
  3461. + else
  3462. + ret = -EINVAL;
  3463. + } break;
  3464. +
  3465. + default:
  3466. + ret = -ENOTTY;
  3467. + break;
  3468. + }
  3469. +
  3470. + if (service)
  3471. + unlock_service(service);
  3472. +
  3473. + if (ret == 0) {
  3474. + if (status == VCHIQ_ERROR)
  3475. + ret = -EIO;
  3476. + else if (status == VCHIQ_RETRY)
  3477. + ret = -EINTR;
  3478. + }
  3479. +
  3480. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  3481. + (ret != -EWOULDBLOCK))
  3482. + vchiq_log_info(vchiq_arm_log_level,
  3483. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3484. + (unsigned long)instance,
  3485. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3486. + ioctl_names[_IOC_NR(cmd)] :
  3487. + "<invalid>",
  3488. + status, ret);
  3489. + else
  3490. + vchiq_log_trace(vchiq_arm_log_level,
  3491. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3492. + (unsigned long)instance,
  3493. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3494. + ioctl_names[_IOC_NR(cmd)] :
  3495. + "<invalid>",
  3496. + status, ret);
  3497. +
  3498. + return ret;
  3499. +}
  3500. +
  3501. +/****************************************************************************
  3502. +*
  3503. +* vchiq_open
  3504. +*
  3505. +***************************************************************************/
  3506. +
  3507. +static int
  3508. +vchiq_open(struct inode *inode, struct file *file)
  3509. +{
  3510. + int dev = iminor(inode) & 0x0f;
  3511. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  3512. + switch (dev) {
  3513. + case VCHIQ_MINOR: {
  3514. + int ret;
  3515. + VCHIQ_STATE_T *state = vchiq_get_state();
  3516. + VCHIQ_INSTANCE_T instance;
  3517. +
  3518. + if (!state) {
  3519. + vchiq_log_error(vchiq_arm_log_level,
  3520. + "vchiq has no connection to VideoCore");
  3521. + return -ENOTCONN;
  3522. + }
  3523. +
  3524. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  3525. + if (!instance)
  3526. + return -ENOMEM;
  3527. +
  3528. + instance->state = state;
  3529. + instance->pid = current->tgid;
  3530. +
  3531. + ret = vchiq_debugfs_add_instance(instance);
  3532. + if (ret != 0) {
  3533. + kfree(instance);
  3534. + return ret;
  3535. + }
  3536. +
  3537. + sema_init(&instance->insert_event, 0);
  3538. + sema_init(&instance->remove_event, 0);
  3539. + mutex_init(&instance->completion_mutex);
  3540. + mutex_init(&instance->bulk_waiter_list_mutex);
  3541. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  3542. +
  3543. + file->private_data = instance;
  3544. + } break;
  3545. +
  3546. + default:
  3547. + vchiq_log_error(vchiq_arm_log_level,
  3548. + "Unknown minor device: %d", dev);
  3549. + return -ENXIO;
  3550. + }
  3551. +
  3552. + return 0;
  3553. +}
  3554. +
  3555. +/****************************************************************************
  3556. +*
  3557. +* vchiq_release
  3558. +*
  3559. +***************************************************************************/
  3560. +
  3561. +static int
  3562. +vchiq_release(struct inode *inode, struct file *file)
  3563. +{
  3564. + int dev = iminor(inode) & 0x0f;
  3565. + int ret = 0;
  3566. + switch (dev) {
  3567. + case VCHIQ_MINOR: {
  3568. + VCHIQ_INSTANCE_T instance = file->private_data;
  3569. + VCHIQ_STATE_T *state = vchiq_get_state();
  3570. + VCHIQ_SERVICE_T *service;
  3571. + int i;
  3572. +
  3573. + vchiq_log_info(vchiq_arm_log_level,
  3574. + "vchiq_release: instance=%lx",
  3575. + (unsigned long)instance);
  3576. +
  3577. + if (!state) {
  3578. + ret = -EPERM;
  3579. + goto out;
  3580. + }
  3581. +
  3582. + /* Ensure videocore is awake to allow termination. */
  3583. + vchiq_use_internal(instance->state, NULL,
  3584. + USE_TYPE_VCHIQ);
  3585. +
  3586. + mutex_lock(&instance->completion_mutex);
  3587. +
  3588. + /* Wake the completion thread and ask it to exit */
  3589. + instance->closing = 1;
  3590. + up(&instance->insert_event);
  3591. +
  3592. + mutex_unlock(&instance->completion_mutex);
  3593. +
  3594. + /* Wake the slot handler if the completion queue is full. */
  3595. + up(&instance->remove_event);
  3596. +
  3597. + /* Mark all services for termination... */
  3598. + i = 0;
  3599. + while ((service = next_service_by_instance(state, instance,
  3600. + &i)) != NULL) {
  3601. + USER_SERVICE_T *user_service = service->base.userdata;
  3602. +
  3603. + /* Wake the slot handler if the msg queue is full. */
  3604. + up(&user_service->remove_event);
  3605. +
  3606. + vchiq_terminate_service_internal(service);
  3607. + unlock_service(service);
  3608. + }
  3609. +
  3610. + /* ...and wait for them to die */
  3611. + i = 0;
  3612. + while ((service = next_service_by_instance(state, instance, &i))
  3613. + != NULL) {
  3614. + USER_SERVICE_T *user_service = service->base.userdata;
  3615. +
  3616. + down(&service->remove_event);
  3617. +
  3618. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  3619. +
  3620. + spin_lock(&msg_queue_spinlock);
  3621. +
  3622. + while (user_service->msg_remove !=
  3623. + user_service->msg_insert) {
  3624. + VCHIQ_HEADER_T *header = user_service->
  3625. + msg_queue[user_service->msg_remove &
  3626. + (MSG_QUEUE_SIZE - 1)];
  3627. + user_service->msg_remove++;
  3628. + spin_unlock(&msg_queue_spinlock);
  3629. +
  3630. + if (header)
  3631. + vchiq_release_message(
  3632. + service->handle,
  3633. + header);
  3634. + spin_lock(&msg_queue_spinlock);
  3635. + }
  3636. +
  3637. + spin_unlock(&msg_queue_spinlock);
  3638. +
  3639. + unlock_service(service);
  3640. + }
  3641. +
  3642. + /* Release any closed services */
  3643. + while (instance->completion_remove !=
  3644. + instance->completion_insert) {
  3645. + VCHIQ_COMPLETION_DATA_T *completion;
  3646. + VCHIQ_SERVICE_T *service;
  3647. + completion = &instance->completions[
  3648. + instance->completion_remove &
  3649. + (MAX_COMPLETIONS - 1)];
  3650. + service = completion->service_userdata;
  3651. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  3652. + {
  3653. + USER_SERVICE_T *user_service =
  3654. + service->base.userdata;
  3655. +
  3656. + /* Wake any blocked user-thread */
  3657. + if (instance->use_close_delivered)
  3658. + up(&user_service->close_event);
  3659. + unlock_service(service);
  3660. + }
  3661. + instance->completion_remove++;
  3662. + }
  3663. +
  3664. + /* Release the PEER service count. */
  3665. + vchiq_release_internal(instance->state, NULL);
  3666. +
  3667. + {
  3668. + struct list_head *pos, *next;
  3669. + list_for_each_safe(pos, next,
  3670. + &instance->bulk_waiter_list) {
  3671. + struct bulk_waiter_node *waiter;
  3672. + waiter = list_entry(pos,
  3673. + struct bulk_waiter_node,
  3674. + list);
  3675. + list_del(pos);
  3676. + vchiq_log_info(vchiq_arm_log_level,
  3677. + "bulk_waiter - cleaned up %x "
  3678. + "for pid %d",
  3679. + (unsigned int)waiter, waiter->pid);
  3680. + kfree(waiter);
  3681. + }
  3682. + }
  3683. +
  3684. + vchiq_debugfs_remove_instance(instance);
  3685. +
  3686. + kfree(instance);
  3687. + file->private_data = NULL;
  3688. + } break;
  3689. +
  3690. + default:
  3691. + vchiq_log_error(vchiq_arm_log_level,
  3692. + "Unknown minor device: %d", dev);
  3693. + ret = -ENXIO;
  3694. + }
  3695. +
  3696. +out:
  3697. + return ret;
  3698. +}
  3699. +
  3700. +/****************************************************************************
  3701. +*
  3702. +* vchiq_dump
  3703. +*
  3704. +***************************************************************************/
  3705. +
  3706. +void
  3707. +vchiq_dump(void *dump_context, const char *str, int len)
  3708. +{
  3709. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  3710. +
  3711. + if (context->actual < context->space) {
  3712. + int copy_bytes;
  3713. + if (context->offset > 0) {
  3714. + int skip_bytes = min(len, (int)context->offset);
  3715. + str += skip_bytes;
  3716. + len -= skip_bytes;
  3717. + context->offset -= skip_bytes;
  3718. + if (context->offset > 0)
  3719. + return;
  3720. + }
  3721. + copy_bytes = min(len, (int)(context->space - context->actual));
  3722. + if (copy_bytes == 0)
  3723. + return;
  3724. + if (copy_to_user(context->buf + context->actual, str,
  3725. + copy_bytes))
  3726. + context->actual = -EFAULT;
  3727. + context->actual += copy_bytes;
  3728. + len -= copy_bytes;
  3729. +
  3730. + /* If tne terminating NUL is included in the length, then it
  3731. + ** marks the end of a line and should be replaced with a
  3732. + ** carriage return. */
  3733. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  3734. + char cr = '\n';
  3735. + if (copy_to_user(context->buf + context->actual - 1,
  3736. + &cr, 1))
  3737. + context->actual = -EFAULT;
  3738. + }
  3739. + }
  3740. +}
  3741. +
  3742. +/****************************************************************************
  3743. +*
  3744. +* vchiq_dump_platform_instance_state
  3745. +*
  3746. +***************************************************************************/
  3747. +
  3748. +void
  3749. +vchiq_dump_platform_instances(void *dump_context)
  3750. +{
  3751. + VCHIQ_STATE_T *state = vchiq_get_state();
  3752. + char buf[80];
  3753. + int len;
  3754. + int i;
  3755. +
  3756. + /* There is no list of instances, so instead scan all services,
  3757. + marking those that have been dumped. */
  3758. +
  3759. + for (i = 0; i < state->unused_service; i++) {
  3760. + VCHIQ_SERVICE_T *service = state->services[i];
  3761. + VCHIQ_INSTANCE_T instance;
  3762. +
  3763. + if (service && (service->base.callback == service_callback)) {
  3764. + instance = service->instance;
  3765. + if (instance)
  3766. + instance->mark = 0;
  3767. + }
  3768. + }
  3769. +
  3770. + for (i = 0; i < state->unused_service; i++) {
  3771. + VCHIQ_SERVICE_T *service = state->services[i];
  3772. + VCHIQ_INSTANCE_T instance;
  3773. +
  3774. + if (service && (service->base.callback == service_callback)) {
  3775. + instance = service->instance;
  3776. + if (instance && !instance->mark) {
  3777. + len = snprintf(buf, sizeof(buf),
  3778. + "Instance %x: pid %d,%s completions "
  3779. + "%d/%d",
  3780. + (unsigned int)instance, instance->pid,
  3781. + instance->connected ? " connected, " :
  3782. + "",
  3783. + instance->completion_insert -
  3784. + instance->completion_remove,
  3785. + MAX_COMPLETIONS);
  3786. +
  3787. + vchiq_dump(dump_context, buf, len + 1);
  3788. +
  3789. + instance->mark = 1;
  3790. + }
  3791. + }
  3792. + }
  3793. +}
  3794. +
  3795. +/****************************************************************************
  3796. +*
  3797. +* vchiq_dump_platform_service_state
  3798. +*
  3799. +***************************************************************************/
  3800. +
  3801. +void
  3802. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  3803. +{
  3804. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  3805. + char buf[80];
  3806. + int len;
  3807. +
  3808. + len = snprintf(buf, sizeof(buf), " instance %x",
  3809. + (unsigned int)service->instance);
  3810. +
  3811. + if ((service->base.callback == service_callback) &&
  3812. + user_service->is_vchi) {
  3813. + len += snprintf(buf + len, sizeof(buf) - len,
  3814. + ", %d/%d messages",
  3815. + user_service->msg_insert - user_service->msg_remove,
  3816. + MSG_QUEUE_SIZE);
  3817. +
  3818. + if (user_service->dequeue_pending)
  3819. + len += snprintf(buf + len, sizeof(buf) - len,
  3820. + " (dequeue pending)");
  3821. + }
  3822. +
  3823. + vchiq_dump(dump_context, buf, len + 1);
  3824. +}
  3825. +
  3826. +/****************************************************************************
  3827. +*
  3828. +* dump_user_mem
  3829. +*
  3830. +***************************************************************************/
  3831. +
  3832. +static void
  3833. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  3834. +{
  3835. + int rc;
  3836. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  3837. + int num_pages;
  3838. + int offset;
  3839. + int end_offset;
  3840. + int page_idx;
  3841. + int prev_idx;
  3842. + struct page *page;
  3843. + struct page **pages;
  3844. + uint8_t *kmapped_virt_ptr;
  3845. +
  3846. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  3847. +
  3848. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  3849. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  3850. + ~0x0fuL);
  3851. +
  3852. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  3853. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  3854. +
  3855. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  3856. +
  3857. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  3858. + if (pages == NULL) {
  3859. + vchiq_log_error(vchiq_arm_log_level,
  3860. + "Unable to allocation memory for %d pages\n",
  3861. + num_pages);
  3862. + return;
  3863. + }
  3864. +
  3865. + down_read(&current->mm->mmap_sem);
  3866. + rc = get_user_pages(current, /* task */
  3867. + current->mm, /* mm */
  3868. + (unsigned long)virt_addr, /* start */
  3869. + num_pages, /* len */
  3870. + 0, /* flags */
  3871. + pages, /* pages (array of page pointers) */
  3872. + NULL); /* vmas */
  3873. + up_read(&current->mm->mmap_sem);
  3874. +
  3875. + prev_idx = -1;
  3876. + page = NULL;
  3877. +
  3878. + while (offset < end_offset) {
  3879. +
  3880. + int page_offset = offset % PAGE_SIZE;
  3881. + page_idx = offset / PAGE_SIZE;
  3882. +
  3883. + if (page_idx != prev_idx) {
  3884. +
  3885. + if (page != NULL)
  3886. + kunmap(page);
  3887. + page = pages[page_idx];
  3888. + kmapped_virt_ptr = kmap(page);
  3889. +
  3890. + prev_idx = page_idx;
  3891. + }
  3892. +
  3893. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  3894. + vchiq_log_dump_mem("ph",
  3895. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  3896. + page_offset],
  3897. + &kmapped_virt_ptr[page_offset], 16);
  3898. +
  3899. + offset += 16;
  3900. + }
  3901. + if (page != NULL)
  3902. + kunmap(page);
  3903. +
  3904. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  3905. + page_cache_release(pages[page_idx]);
  3906. +
  3907. + kfree(pages);
  3908. +}
  3909. +
  3910. +/****************************************************************************
  3911. +*
  3912. +* vchiq_read
  3913. +*
  3914. +***************************************************************************/
  3915. +
  3916. +static ssize_t
  3917. +vchiq_read(struct file *file, char __user *buf,
  3918. + size_t count, loff_t *ppos)
  3919. +{
  3920. + DUMP_CONTEXT_T context;
  3921. + context.buf = buf;
  3922. + context.actual = 0;
  3923. + context.space = count;
  3924. + context.offset = *ppos;
  3925. +
  3926. + vchiq_dump_state(&context, &g_state);
  3927. +
  3928. + *ppos += context.actual;
  3929. +
  3930. + return context.actual;
  3931. +}
  3932. +
  3933. +VCHIQ_STATE_T *
  3934. +vchiq_get_state(void)
  3935. +{
  3936. +
  3937. + if (g_state.remote == NULL)
  3938. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  3939. + else if (g_state.remote->initialised != 1)
  3940. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  3941. + __func__, g_state.remote->initialised);
  3942. +
  3943. + return ((g_state.remote != NULL) &&
  3944. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  3945. +}
  3946. +
  3947. +static const struct file_operations
  3948. +vchiq_fops = {
  3949. + .owner = THIS_MODULE,
  3950. + .unlocked_ioctl = vchiq_ioctl,
  3951. + .open = vchiq_open,
  3952. + .release = vchiq_release,
  3953. + .read = vchiq_read
  3954. +};
  3955. +
  3956. +/*
  3957. + * Autosuspend related functionality
  3958. + */
  3959. +
  3960. +int
  3961. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  3962. +{
  3963. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3964. + if (!arm_state)
  3965. + /* autosuspend not supported - always return wanted */
  3966. + return 1;
  3967. + else if (arm_state->blocked_count)
  3968. + return 1;
  3969. + else if (!arm_state->videocore_use_count)
  3970. + /* usage count zero - check for override unless we're forcing */
  3971. + if (arm_state->resume_blocked)
  3972. + return 0;
  3973. + else
  3974. + return vchiq_platform_videocore_wanted(state);
  3975. + else
  3976. + /* non-zero usage count - videocore still required */
  3977. + return 1;
  3978. +}
  3979. +
  3980. +static VCHIQ_STATUS_T
  3981. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  3982. + VCHIQ_HEADER_T *header,
  3983. + VCHIQ_SERVICE_HANDLE_T service_user,
  3984. + void *bulk_user)
  3985. +{
  3986. + vchiq_log_error(vchiq_susp_log_level,
  3987. + "%s callback reason %d", __func__, reason);
  3988. + return 0;
  3989. +}
  3990. +
  3991. +static int
  3992. +vchiq_keepalive_thread_func(void *v)
  3993. +{
  3994. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  3995. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3996. +
  3997. + VCHIQ_STATUS_T status;
  3998. + VCHIQ_INSTANCE_T instance;
  3999. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  4000. +
  4001. + VCHIQ_SERVICE_PARAMS_T params = {
  4002. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  4003. + .callback = vchiq_keepalive_vchiq_callback,
  4004. + .version = KEEPALIVE_VER,
  4005. + .version_min = KEEPALIVE_VER_MIN
  4006. + };
  4007. +
  4008. + status = vchiq_initialise(&instance);
  4009. + if (status != VCHIQ_SUCCESS) {
  4010. + vchiq_log_error(vchiq_susp_log_level,
  4011. + "%s vchiq_initialise failed %d", __func__, status);
  4012. + goto exit;
  4013. + }
  4014. +
  4015. + status = vchiq_connect(instance);
  4016. + if (status != VCHIQ_SUCCESS) {
  4017. + vchiq_log_error(vchiq_susp_log_level,
  4018. + "%s vchiq_connect failed %d", __func__, status);
  4019. + goto shutdown;
  4020. + }
  4021. +
  4022. + status = vchiq_add_service(instance, &params, &ka_handle);
  4023. + if (status != VCHIQ_SUCCESS) {
  4024. + vchiq_log_error(vchiq_susp_log_level,
  4025. + "%s vchiq_open_service failed %d", __func__, status);
  4026. + goto shutdown;
  4027. + }
  4028. +
  4029. + while (1) {
  4030. + long rc = 0, uc = 0;
  4031. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  4032. + != 0) {
  4033. + vchiq_log_error(vchiq_susp_log_level,
  4034. + "%s interrupted", __func__);
  4035. + flush_signals(current);
  4036. + continue;
  4037. + }
  4038. +
  4039. + /* read and clear counters. Do release_count then use_count to
  4040. + * prevent getting more releases than uses */
  4041. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  4042. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  4043. +
  4044. + /* Call use/release service the requisite number of times.
  4045. + * Process use before release so use counts don't go negative */
  4046. + while (uc--) {
  4047. + atomic_inc(&arm_state->ka_use_ack_count);
  4048. + status = vchiq_use_service(ka_handle);
  4049. + if (status != VCHIQ_SUCCESS) {
  4050. + vchiq_log_error(vchiq_susp_log_level,
  4051. + "%s vchiq_use_service error %d",
  4052. + __func__, status);
  4053. + }
  4054. + }
  4055. + while (rc--) {
  4056. + status = vchiq_release_service(ka_handle);
  4057. + if (status != VCHIQ_SUCCESS) {
  4058. + vchiq_log_error(vchiq_susp_log_level,
  4059. + "%s vchiq_release_service error %d",
  4060. + __func__, status);
  4061. + }
  4062. + }
  4063. + }
  4064. +
  4065. +shutdown:
  4066. + vchiq_shutdown(instance);
  4067. +exit:
  4068. + return 0;
  4069. +}
  4070. +
  4071. +
  4072. +
  4073. +VCHIQ_STATUS_T
  4074. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  4075. +{
  4076. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  4077. +
  4078. + if (arm_state) {
  4079. + rwlock_init(&arm_state->susp_res_lock);
  4080. +
  4081. + init_completion(&arm_state->ka_evt);
  4082. + atomic_set(&arm_state->ka_use_count, 0);
  4083. + atomic_set(&arm_state->ka_use_ack_count, 0);
  4084. + atomic_set(&arm_state->ka_release_count, 0);
  4085. +
  4086. + init_completion(&arm_state->vc_suspend_complete);
  4087. +
  4088. + init_completion(&arm_state->vc_resume_complete);
  4089. + /* Initialise to 'done' state. We only want to block on resume
  4090. + * completion while videocore is suspended. */
  4091. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  4092. +
  4093. + init_completion(&arm_state->resume_blocker);
  4094. + /* Initialise to 'done' state. We only want to block on this
  4095. + * completion while resume is blocked */
  4096. + complete_all(&arm_state->resume_blocker);
  4097. +
  4098. + init_completion(&arm_state->blocked_blocker);
  4099. + /* Initialise to 'done' state. We only want to block on this
  4100. + * completion while things are waiting on the resume blocker */
  4101. + complete_all(&arm_state->blocked_blocker);
  4102. +
  4103. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  4104. + arm_state->suspend_timer_running = 0;
  4105. + init_timer(&arm_state->suspend_timer);
  4106. + arm_state->suspend_timer.data = (unsigned long)(state);
  4107. + arm_state->suspend_timer.function = suspend_timer_callback;
  4108. +
  4109. + arm_state->first_connect = 0;
  4110. +
  4111. + }
  4112. + return status;
  4113. +}
  4114. +
  4115. +/*
  4116. +** Functions to modify the state variables;
  4117. +** set_suspend_state
  4118. +** set_resume_state
  4119. +**
  4120. +** There are more state variables than we might like, so ensure they remain in
  4121. +** step. Suspend and resume state are maintained separately, since most of
  4122. +** these state machines can operate independently. However, there are a few
  4123. +** states where state transitions in one state machine cause a reset to the
  4124. +** other state machine. In addition, there are some completion events which
  4125. +** need to occur on state machine reset and end-state(s), so these are also
  4126. +** dealt with in these functions.
  4127. +**
  4128. +** In all states we set the state variable according to the input, but in some
  4129. +** cases we perform additional steps outlined below;
  4130. +**
  4131. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  4132. +** The suspend completion is completed after any suspend
  4133. +** attempt. When we reset the state machine we also reset
  4134. +** the completion. This reset occurs when videocore is
  4135. +** resumed, and also if we initiate suspend after a suspend
  4136. +** failure.
  4137. +**
  4138. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  4139. +** suspend - ie from this point on we must try to suspend
  4140. +** before resuming can occur. We therefore also reset the
  4141. +** resume state machine to VC_RESUME_IDLE in this state.
  4142. +**
  4143. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  4144. +** complete_all on the suspend completion to notify
  4145. +** anything waiting for suspend to happen.
  4146. +**
  4147. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  4148. +** initiate resume, so no need to alter resume state.
  4149. +** We call complete_all on the suspend completion to notify
  4150. +** of suspend rejection.
  4151. +**
  4152. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  4153. +** suspend completion and reset the resume state machine.
  4154. +**
  4155. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  4156. +** resume completion is in it's 'done' state whenever
  4157. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  4158. +** implies that videocore is suspended.
  4159. +** Hence, any thread which needs to wait until videocore is
  4160. +** running can wait on this completion - it will only block
  4161. +** if videocore is suspended.
  4162. +**
  4163. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  4164. +** Call complete_all on the resume completion to unblock
  4165. +** any threads waiting for resume. Also reset the suspend
  4166. +** state machine to it's idle state.
  4167. +**
  4168. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  4169. +*/
  4170. +
  4171. +void
  4172. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  4173. + enum vc_suspend_status new_state)
  4174. +{
  4175. + /* set the state in all cases */
  4176. + arm_state->vc_suspend_state = new_state;
  4177. +
  4178. + /* state specific additional actions */
  4179. + switch (new_state) {
  4180. + case VC_SUSPEND_FORCE_CANCELED:
  4181. + complete_all(&arm_state->vc_suspend_complete);
  4182. + break;
  4183. + case VC_SUSPEND_REJECTED:
  4184. + complete_all(&arm_state->vc_suspend_complete);
  4185. + break;
  4186. + case VC_SUSPEND_FAILED:
  4187. + complete_all(&arm_state->vc_suspend_complete);
  4188. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  4189. + complete_all(&arm_state->vc_resume_complete);
  4190. + break;
  4191. + case VC_SUSPEND_IDLE:
  4192. + reinit_completion(&arm_state->vc_suspend_complete);
  4193. + break;
  4194. + case VC_SUSPEND_REQUESTED:
  4195. + break;
  4196. + case VC_SUSPEND_IN_PROGRESS:
  4197. + set_resume_state(arm_state, VC_RESUME_IDLE);
  4198. + break;
  4199. + case VC_SUSPEND_SUSPENDED:
  4200. + complete_all(&arm_state->vc_suspend_complete);
  4201. + break;
  4202. + default:
  4203. + BUG();
  4204. + break;
  4205. + }
  4206. +}
  4207. +
  4208. +void
  4209. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  4210. + enum vc_resume_status new_state)
  4211. +{
  4212. + /* set the state in all cases */
  4213. + arm_state->vc_resume_state = new_state;
  4214. +
  4215. + /* state specific additional actions */
  4216. + switch (new_state) {
  4217. + case VC_RESUME_FAILED:
  4218. + break;
  4219. + case VC_RESUME_IDLE:
  4220. + reinit_completion(&arm_state->vc_resume_complete);
  4221. + break;
  4222. + case VC_RESUME_REQUESTED:
  4223. + break;
  4224. + case VC_RESUME_IN_PROGRESS:
  4225. + break;
  4226. + case VC_RESUME_RESUMED:
  4227. + complete_all(&arm_state->vc_resume_complete);
  4228. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4229. + break;
  4230. + default:
  4231. + BUG();
  4232. + break;
  4233. + }
  4234. +}
  4235. +
  4236. +
  4237. +/* should be called with the write lock held */
  4238. +inline void
  4239. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4240. +{
  4241. + del_timer(&arm_state->suspend_timer);
  4242. + arm_state->suspend_timer.expires = jiffies +
  4243. + msecs_to_jiffies(arm_state->
  4244. + suspend_timer_timeout);
  4245. + add_timer(&arm_state->suspend_timer);
  4246. + arm_state->suspend_timer_running = 1;
  4247. +}
  4248. +
  4249. +/* should be called with the write lock held */
  4250. +static inline void
  4251. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4252. +{
  4253. + if (arm_state->suspend_timer_running) {
  4254. + del_timer(&arm_state->suspend_timer);
  4255. + arm_state->suspend_timer_running = 0;
  4256. + }
  4257. +}
  4258. +
  4259. +static inline int
  4260. +need_resume(VCHIQ_STATE_T *state)
  4261. +{
  4262. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4263. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  4264. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  4265. + vchiq_videocore_wanted(state);
  4266. +}
  4267. +
  4268. +static int
  4269. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  4270. +{
  4271. + int status = VCHIQ_SUCCESS;
  4272. + const unsigned long timeout_val =
  4273. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  4274. + int resume_count = 0;
  4275. +
  4276. + /* Allow any threads which were blocked by the last force suspend to
  4277. + * complete if they haven't already. Only give this one shot; if
  4278. + * blocked_count is incremented after blocked_blocker is completed
  4279. + * (which only happens when blocked_count hits 0) then those threads
  4280. + * will have to wait until next time around */
  4281. + if (arm_state->blocked_count) {
  4282. + reinit_completion(&arm_state->blocked_blocker);
  4283. + write_unlock_bh(&arm_state->susp_res_lock);
  4284. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  4285. + "blocked clients", __func__);
  4286. + if (wait_for_completion_interruptible_timeout(
  4287. + &arm_state->blocked_blocker, timeout_val)
  4288. + <= 0) {
  4289. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4290. + "previously blocked clients failed" , __func__);
  4291. + status = VCHIQ_ERROR;
  4292. + write_lock_bh(&arm_state->susp_res_lock);
  4293. + goto out;
  4294. + }
  4295. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  4296. + "clients resumed", __func__);
  4297. + write_lock_bh(&arm_state->susp_res_lock);
  4298. + }
  4299. +
  4300. + /* We need to wait for resume to complete if it's in process */
  4301. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  4302. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  4303. + if (resume_count > 1) {
  4304. + status = VCHIQ_ERROR;
  4305. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  4306. + "many times for resume" , __func__);
  4307. + goto out;
  4308. + }
  4309. + write_unlock_bh(&arm_state->susp_res_lock);
  4310. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  4311. + __func__);
  4312. + if (wait_for_completion_interruptible_timeout(
  4313. + &arm_state->vc_resume_complete, timeout_val)
  4314. + <= 0) {
  4315. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4316. + "resume failed (%s)", __func__,
  4317. + resume_state_names[arm_state->vc_resume_state +
  4318. + VC_RESUME_NUM_OFFSET]);
  4319. + status = VCHIQ_ERROR;
  4320. + write_lock_bh(&arm_state->susp_res_lock);
  4321. + goto out;
  4322. + }
  4323. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  4324. + write_lock_bh(&arm_state->susp_res_lock);
  4325. + resume_count++;
  4326. + }
  4327. + reinit_completion(&arm_state->resume_blocker);
  4328. + arm_state->resume_blocked = 1;
  4329. +
  4330. +out:
  4331. + return status;
  4332. +}
  4333. +
  4334. +static inline void
  4335. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  4336. +{
  4337. + complete_all(&arm_state->resume_blocker);
  4338. + arm_state->resume_blocked = 0;
  4339. +}
  4340. +
  4341. +/* Initiate suspend via slot handler. Should be called with the write lock
  4342. + * held */
  4343. +VCHIQ_STATUS_T
  4344. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  4345. +{
  4346. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4347. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4348. +
  4349. + if (!arm_state)
  4350. + goto out;
  4351. +
  4352. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4353. + status = VCHIQ_SUCCESS;
  4354. +
  4355. +
  4356. + switch (arm_state->vc_suspend_state) {
  4357. + case VC_SUSPEND_REQUESTED:
  4358. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  4359. + "requested", __func__);
  4360. + break;
  4361. + case VC_SUSPEND_IN_PROGRESS:
  4362. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  4363. + "progress", __func__);
  4364. + break;
  4365. +
  4366. + default:
  4367. + /* We don't expect to be in other states, so log but continue
  4368. + * anyway */
  4369. + vchiq_log_error(vchiq_susp_log_level,
  4370. + "%s unexpected suspend state %s", __func__,
  4371. + suspend_state_names[arm_state->vc_suspend_state +
  4372. + VC_SUSPEND_NUM_OFFSET]);
  4373. + /* fall through */
  4374. + case VC_SUSPEND_REJECTED:
  4375. + case VC_SUSPEND_FAILED:
  4376. + /* Ensure any idle state actions have been run */
  4377. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4378. + /* fall through */
  4379. + case VC_SUSPEND_IDLE:
  4380. + vchiq_log_info(vchiq_susp_log_level,
  4381. + "%s: suspending", __func__);
  4382. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  4383. + /* kick the slot handler thread to initiate suspend */
  4384. + request_poll(state, NULL, 0);
  4385. + break;
  4386. + }
  4387. +
  4388. +out:
  4389. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4390. + return status;
  4391. +}
  4392. +
  4393. +void
  4394. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  4395. +{
  4396. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4397. + int susp = 0;
  4398. +
  4399. + if (!arm_state)
  4400. + goto out;
  4401. +
  4402. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4403. +
  4404. + write_lock_bh(&arm_state->susp_res_lock);
  4405. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  4406. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  4407. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  4408. + susp = 1;
  4409. + }
  4410. + write_unlock_bh(&arm_state->susp_res_lock);
  4411. +
  4412. + if (susp)
  4413. + vchiq_platform_suspend(state);
  4414. +
  4415. +out:
  4416. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4417. + return;
  4418. +}
  4419. +
  4420. +
  4421. +static void
  4422. +output_timeout_error(VCHIQ_STATE_T *state)
  4423. +{
  4424. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4425. + char service_err[50] = "";
  4426. + int vc_use_count = arm_state->videocore_use_count;
  4427. + int active_services = state->unused_service;
  4428. + int i;
  4429. +
  4430. + if (!arm_state->videocore_use_count) {
  4431. + snprintf(service_err, 50, " Videocore usecount is 0");
  4432. + goto output_msg;
  4433. + }
  4434. + for (i = 0; i < active_services; i++) {
  4435. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  4436. + if (service_ptr && service_ptr->service_use_count &&
  4437. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  4438. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  4439. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  4440. + service_ptr->base.fourcc),
  4441. + service_ptr->client_id,
  4442. + service_ptr->service_use_count,
  4443. + service_ptr->service_use_count ==
  4444. + vc_use_count ? "" : " (+ more)");
  4445. + break;
  4446. + }
  4447. + }
  4448. +
  4449. +output_msg:
  4450. + vchiq_log_error(vchiq_susp_log_level,
  4451. + "timed out waiting for vc suspend (%d).%s",
  4452. + arm_state->autosuspend_override, service_err);
  4453. +
  4454. +}
  4455. +
  4456. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  4457. +** We don't actually force suspend, since videocore may get into a bad state
  4458. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  4459. +** determine a good point to suspend. If this doesn't happen within 100ms we
  4460. +** report failure.
  4461. +**
  4462. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  4463. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  4464. +*/
  4465. +VCHIQ_STATUS_T
  4466. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  4467. +{
  4468. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4469. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4470. + long rc = 0;
  4471. + int repeat = -1;
  4472. +
  4473. + if (!arm_state)
  4474. + goto out;
  4475. +
  4476. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4477. +
  4478. + write_lock_bh(&arm_state->susp_res_lock);
  4479. +
  4480. + status = block_resume(arm_state);
  4481. + if (status != VCHIQ_SUCCESS)
  4482. + goto unlock;
  4483. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4484. + /* Already suspended - just block resume and exit */
  4485. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  4486. + __func__);
  4487. + status = VCHIQ_SUCCESS;
  4488. + goto unlock;
  4489. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  4490. + /* initiate suspend immediately in the case that we're waiting
  4491. + * for the timeout */
  4492. + stop_suspend_timer(arm_state);
  4493. + if (!vchiq_videocore_wanted(state)) {
  4494. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  4495. + "idle, initiating suspend", __func__);
  4496. + status = vchiq_arm_vcsuspend(state);
  4497. + } else if (arm_state->autosuspend_override <
  4498. + FORCE_SUSPEND_FAIL_MAX) {
  4499. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  4500. + "videocore go idle", __func__);
  4501. + status = VCHIQ_SUCCESS;
  4502. + } else {
  4503. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  4504. + "many times - attempting suspend", __func__);
  4505. + status = vchiq_arm_vcsuspend(state);
  4506. + }
  4507. + } else {
  4508. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  4509. + "in progress - wait for completion", __func__);
  4510. + status = VCHIQ_SUCCESS;
  4511. + }
  4512. +
  4513. + /* Wait for suspend to happen due to system idle (not forced..) */
  4514. + if (status != VCHIQ_SUCCESS)
  4515. + goto unblock_resume;
  4516. +
  4517. + do {
  4518. + write_unlock_bh(&arm_state->susp_res_lock);
  4519. +
  4520. + rc = wait_for_completion_interruptible_timeout(
  4521. + &arm_state->vc_suspend_complete,
  4522. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  4523. +
  4524. + write_lock_bh(&arm_state->susp_res_lock);
  4525. + if (rc < 0) {
  4526. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  4527. + "interrupted waiting for suspend", __func__);
  4528. + status = VCHIQ_ERROR;
  4529. + goto unblock_resume;
  4530. + } else if (rc == 0) {
  4531. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  4532. + /* Repeat timeout once if in progress */
  4533. + if (repeat < 0) {
  4534. + repeat = 1;
  4535. + continue;
  4536. + }
  4537. + }
  4538. + arm_state->autosuspend_override++;
  4539. + output_timeout_error(state);
  4540. +
  4541. + status = VCHIQ_RETRY;
  4542. + goto unblock_resume;
  4543. + }
  4544. + } while (0 < (repeat--));
  4545. +
  4546. + /* Check and report state in case we need to abort ARM suspend */
  4547. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  4548. + status = VCHIQ_RETRY;
  4549. + vchiq_log_error(vchiq_susp_log_level,
  4550. + "%s videocore suspend failed (state %s)", __func__,
  4551. + suspend_state_names[arm_state->vc_suspend_state +
  4552. + VC_SUSPEND_NUM_OFFSET]);
  4553. + /* Reset the state only if it's still in an error state.
  4554. + * Something could have already initiated another suspend. */
  4555. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  4556. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4557. +
  4558. + goto unblock_resume;
  4559. + }
  4560. +
  4561. + /* successfully suspended - unlock and exit */
  4562. + goto unlock;
  4563. +
  4564. +unblock_resume:
  4565. + /* all error states need to unblock resume before exit */
  4566. + unblock_resume(arm_state);
  4567. +
  4568. +unlock:
  4569. + write_unlock_bh(&arm_state->susp_res_lock);
  4570. +
  4571. +out:
  4572. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4573. + return status;
  4574. +}
  4575. +
  4576. +void
  4577. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  4578. +{
  4579. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4580. +
  4581. + if (!arm_state)
  4582. + goto out;
  4583. +
  4584. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4585. +
  4586. + write_lock_bh(&arm_state->susp_res_lock);
  4587. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  4588. + arm_state->first_connect &&
  4589. + !vchiq_videocore_wanted(state)) {
  4590. + vchiq_arm_vcsuspend(state);
  4591. + }
  4592. + write_unlock_bh(&arm_state->susp_res_lock);
  4593. +
  4594. +out:
  4595. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4596. + return;
  4597. +}
  4598. +
  4599. +
  4600. +int
  4601. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  4602. +{
  4603. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4604. + int resume = 0;
  4605. + int ret = -1;
  4606. +
  4607. + if (!arm_state)
  4608. + goto out;
  4609. +
  4610. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4611. +
  4612. + write_lock_bh(&arm_state->susp_res_lock);
  4613. + unblock_resume(arm_state);
  4614. + resume = vchiq_check_resume(state);
  4615. + write_unlock_bh(&arm_state->susp_res_lock);
  4616. +
  4617. + if (resume) {
  4618. + if (wait_for_completion_interruptible(
  4619. + &arm_state->vc_resume_complete) < 0) {
  4620. + vchiq_log_error(vchiq_susp_log_level,
  4621. + "%s interrupted", __func__);
  4622. + /* failed, cannot accurately derive suspend
  4623. + * state, so exit early. */
  4624. + goto out;
  4625. + }
  4626. + }
  4627. +
  4628. + read_lock_bh(&arm_state->susp_res_lock);
  4629. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4630. + vchiq_log_info(vchiq_susp_log_level,
  4631. + "%s: Videocore remains suspended", __func__);
  4632. + } else {
  4633. + vchiq_log_info(vchiq_susp_log_level,
  4634. + "%s: Videocore resumed", __func__);
  4635. + ret = 0;
  4636. + }
  4637. + read_unlock_bh(&arm_state->susp_res_lock);
  4638. +out:
  4639. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4640. + return ret;
  4641. +}
  4642. +
  4643. +/* This function should be called with the write lock held */
  4644. +int
  4645. +vchiq_check_resume(VCHIQ_STATE_T *state)
  4646. +{
  4647. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4648. + int resume = 0;
  4649. +
  4650. + if (!arm_state)
  4651. + goto out;
  4652. +
  4653. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4654. +
  4655. + if (need_resume(state)) {
  4656. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4657. + request_poll(state, NULL, 0);
  4658. + resume = 1;
  4659. + }
  4660. +
  4661. +out:
  4662. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4663. + return resume;
  4664. +}
  4665. +
  4666. +void
  4667. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  4668. +{
  4669. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4670. + int res = 0;
  4671. +
  4672. + if (!arm_state)
  4673. + goto out;
  4674. +
  4675. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4676. +
  4677. + write_lock_bh(&arm_state->susp_res_lock);
  4678. + if (arm_state->wake_address == 0) {
  4679. + vchiq_log_info(vchiq_susp_log_level,
  4680. + "%s: already awake", __func__);
  4681. + goto unlock;
  4682. + }
  4683. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  4684. + vchiq_log_info(vchiq_susp_log_level,
  4685. + "%s: already resuming", __func__);
  4686. + goto unlock;
  4687. + }
  4688. +
  4689. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  4690. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  4691. + res = 1;
  4692. + } else
  4693. + vchiq_log_trace(vchiq_susp_log_level,
  4694. + "%s: not resuming (resume state %s)", __func__,
  4695. + resume_state_names[arm_state->vc_resume_state +
  4696. + VC_RESUME_NUM_OFFSET]);
  4697. +
  4698. +unlock:
  4699. + write_unlock_bh(&arm_state->susp_res_lock);
  4700. +
  4701. + if (res)
  4702. + vchiq_platform_resume(state);
  4703. +
  4704. +out:
  4705. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4706. + return;
  4707. +
  4708. +}
  4709. +
  4710. +
  4711. +
  4712. +VCHIQ_STATUS_T
  4713. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  4714. + enum USE_TYPE_E use_type)
  4715. +{
  4716. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4717. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4718. + char entity[16];
  4719. + int *entity_uc;
  4720. + int local_uc, local_entity_uc;
  4721. +
  4722. + if (!arm_state)
  4723. + goto out;
  4724. +
  4725. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4726. +
  4727. + if (use_type == USE_TYPE_VCHIQ) {
  4728. + sprintf(entity, "VCHIQ: ");
  4729. + entity_uc = &arm_state->peer_use_count;
  4730. + } else if (service) {
  4731. + sprintf(entity, "%c%c%c%c:%03d",
  4732. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4733. + service->client_id);
  4734. + entity_uc = &service->service_use_count;
  4735. + } else {
  4736. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  4737. + "ptr", __func__);
  4738. + ret = VCHIQ_ERROR;
  4739. + goto out;
  4740. + }
  4741. +
  4742. + write_lock_bh(&arm_state->susp_res_lock);
  4743. + while (arm_state->resume_blocked) {
  4744. + /* If we call 'use' while force suspend is waiting for suspend,
  4745. + * then we're about to block the thread which the force is
  4746. + * waiting to complete, so we're bound to just time out. In this
  4747. + * case, set the suspend state such that the wait will be
  4748. + * canceled, so we can complete as quickly as possible. */
  4749. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  4750. + VC_SUSPEND_IDLE) {
  4751. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  4752. + break;
  4753. + }
  4754. + /* If suspend is already in progress then we need to block */
  4755. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  4756. + /* Indicate that there are threads waiting on the resume
  4757. + * blocker. These need to be allowed to complete before
  4758. + * a _second_ call to force suspend can complete,
  4759. + * otherwise low priority threads might never actually
  4760. + * continue */
  4761. + arm_state->blocked_count++;
  4762. + write_unlock_bh(&arm_state->susp_res_lock);
  4763. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4764. + "blocked - waiting...", __func__, entity);
  4765. + if (wait_for_completion_killable(
  4766. + &arm_state->resume_blocker) != 0) {
  4767. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  4768. + "wait for resume blocker interrupted",
  4769. + __func__, entity);
  4770. + ret = VCHIQ_ERROR;
  4771. + write_lock_bh(&arm_state->susp_res_lock);
  4772. + arm_state->blocked_count--;
  4773. + write_unlock_bh(&arm_state->susp_res_lock);
  4774. + goto out;
  4775. + }
  4776. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4777. + "unblocked", __func__, entity);
  4778. + write_lock_bh(&arm_state->susp_res_lock);
  4779. + if (--arm_state->blocked_count == 0)
  4780. + complete_all(&arm_state->blocked_blocker);
  4781. + }
  4782. + }
  4783. +
  4784. + stop_suspend_timer(arm_state);
  4785. +
  4786. + local_uc = ++arm_state->videocore_use_count;
  4787. + local_entity_uc = ++(*entity_uc);
  4788. +
  4789. + /* If there's a pending request which hasn't yet been serviced then
  4790. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  4791. + * vc_resume_complete will block until we either resume or fail to
  4792. + * suspend */
  4793. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  4794. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4795. +
  4796. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  4797. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4798. + vchiq_log_info(vchiq_susp_log_level,
  4799. + "%s %s count %d, state count %d",
  4800. + __func__, entity, local_entity_uc, local_uc);
  4801. + request_poll(state, NULL, 0);
  4802. + } else
  4803. + vchiq_log_trace(vchiq_susp_log_level,
  4804. + "%s %s count %d, state count %d",
  4805. + __func__, entity, *entity_uc, local_uc);
  4806. +
  4807. +
  4808. + write_unlock_bh(&arm_state->susp_res_lock);
  4809. +
  4810. + /* Completion is in a done state when we're not suspended, so this won't
  4811. + * block for the non-suspended case. */
  4812. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  4813. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  4814. + __func__, entity);
  4815. + if (wait_for_completion_killable(
  4816. + &arm_state->vc_resume_complete) != 0) {
  4817. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  4818. + "resume interrupted", __func__, entity);
  4819. + ret = VCHIQ_ERROR;
  4820. + goto out;
  4821. + }
  4822. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  4823. + entity);
  4824. + }
  4825. +
  4826. + if (ret == VCHIQ_SUCCESS) {
  4827. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  4828. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  4829. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  4830. + /* Send the use notify to videocore */
  4831. + status = vchiq_send_remote_use_active(state);
  4832. + if (status == VCHIQ_SUCCESS)
  4833. + ack_cnt--;
  4834. + else
  4835. + atomic_add(ack_cnt,
  4836. + &arm_state->ka_use_ack_count);
  4837. + }
  4838. + }
  4839. +
  4840. +out:
  4841. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4842. + return ret;
  4843. +}
  4844. +
  4845. +VCHIQ_STATUS_T
  4846. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  4847. +{
  4848. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4849. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4850. + char entity[16];
  4851. + int *entity_uc;
  4852. + int local_uc, local_entity_uc;
  4853. +
  4854. + if (!arm_state)
  4855. + goto out;
  4856. +
  4857. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4858. +
  4859. + if (service) {
  4860. + sprintf(entity, "%c%c%c%c:%03d",
  4861. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4862. + service->client_id);
  4863. + entity_uc = &service->service_use_count;
  4864. + } else {
  4865. + sprintf(entity, "PEER: ");
  4866. + entity_uc = &arm_state->peer_use_count;
  4867. + }
  4868. +
  4869. + write_lock_bh(&arm_state->susp_res_lock);
  4870. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  4871. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  4872. + WARN_ON(!arm_state->videocore_use_count);
  4873. + WARN_ON(!(*entity_uc));
  4874. + ret = VCHIQ_ERROR;
  4875. + goto unlock;
  4876. + }
  4877. + local_uc = --arm_state->videocore_use_count;
  4878. + local_entity_uc = --(*entity_uc);
  4879. +
  4880. + if (!vchiq_videocore_wanted(state)) {
  4881. + if (vchiq_platform_use_suspend_timer() &&
  4882. + !arm_state->resume_blocked) {
  4883. + /* Only use the timer if we're not trying to force
  4884. + * suspend (=> resume_blocked) */
  4885. + start_suspend_timer(arm_state);
  4886. + } else {
  4887. + vchiq_log_info(vchiq_susp_log_level,
  4888. + "%s %s count %d, state count %d - suspending",
  4889. + __func__, entity, *entity_uc,
  4890. + arm_state->videocore_use_count);
  4891. + vchiq_arm_vcsuspend(state);
  4892. + }
  4893. + } else
  4894. + vchiq_log_trace(vchiq_susp_log_level,
  4895. + "%s %s count %d, state count %d",
  4896. + __func__, entity, *entity_uc,
  4897. + arm_state->videocore_use_count);
  4898. +
  4899. +unlock:
  4900. + write_unlock_bh(&arm_state->susp_res_lock);
  4901. +
  4902. +out:
  4903. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4904. + return ret;
  4905. +}
  4906. +
  4907. +void
  4908. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  4909. +{
  4910. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4911. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4912. + atomic_inc(&arm_state->ka_use_count);
  4913. + complete(&arm_state->ka_evt);
  4914. +}
  4915. +
  4916. +void
  4917. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  4918. +{
  4919. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4920. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4921. + atomic_inc(&arm_state->ka_release_count);
  4922. + complete(&arm_state->ka_evt);
  4923. +}
  4924. +
  4925. +VCHIQ_STATUS_T
  4926. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  4927. +{
  4928. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  4929. +}
  4930. +
  4931. +VCHIQ_STATUS_T
  4932. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  4933. +{
  4934. + return vchiq_release_internal(service->state, service);
  4935. +}
  4936. +
  4937. +VCHIQ_DEBUGFS_NODE_T *
  4938. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
  4939. +{
  4940. + return &instance->debugfs_node;
  4941. +}
  4942. +
  4943. +int
  4944. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  4945. +{
  4946. + VCHIQ_SERVICE_T *service;
  4947. + int use_count = 0, i;
  4948. + i = 0;
  4949. + while ((service = next_service_by_instance(instance->state,
  4950. + instance, &i)) != NULL) {
  4951. + use_count += service->service_use_count;
  4952. + unlock_service(service);
  4953. + }
  4954. + return use_count;
  4955. +}
  4956. +
  4957. +int
  4958. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
  4959. +{
  4960. + return instance->pid;
  4961. +}
  4962. +
  4963. +int
  4964. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
  4965. +{
  4966. + return instance->trace;
  4967. +}
  4968. +
  4969. +void
  4970. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
  4971. +{
  4972. + VCHIQ_SERVICE_T *service;
  4973. + int i;
  4974. + i = 0;
  4975. + while ((service = next_service_by_instance(instance->state,
  4976. + instance, &i)) != NULL) {
  4977. + service->trace = trace;
  4978. + unlock_service(service);
  4979. + }
  4980. + instance->trace = (trace != 0);
  4981. +}
  4982. +
  4983. +static void suspend_timer_callback(unsigned long context)
  4984. +{
  4985. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  4986. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4987. + if (!arm_state)
  4988. + goto out;
  4989. + vchiq_log_info(vchiq_susp_log_level,
  4990. + "%s - suspend timer expired - check suspend", __func__);
  4991. + vchiq_check_suspend(state);
  4992. +out:
  4993. + return;
  4994. +}
  4995. +
  4996. +VCHIQ_STATUS_T
  4997. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  4998. +{
  4999. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5000. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5001. + if (service) {
  5002. + ret = vchiq_use_internal(service->state, service,
  5003. + USE_TYPE_SERVICE_NO_RESUME);
  5004. + unlock_service(service);
  5005. + }
  5006. + return ret;
  5007. +}
  5008. +
  5009. +VCHIQ_STATUS_T
  5010. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  5011. +{
  5012. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5013. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5014. + if (service) {
  5015. + ret = vchiq_use_internal(service->state, service,
  5016. + USE_TYPE_SERVICE);
  5017. + unlock_service(service);
  5018. + }
  5019. + return ret;
  5020. +}
  5021. +
  5022. +VCHIQ_STATUS_T
  5023. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  5024. +{
  5025. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5026. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5027. + if (service) {
  5028. + ret = vchiq_release_internal(service->state, service);
  5029. + unlock_service(service);
  5030. + }
  5031. + return ret;
  5032. +}
  5033. +
  5034. +void
  5035. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  5036. +{
  5037. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  5038. + int i, j = 0;
  5039. + /* Only dump 64 services */
  5040. + static const int local_max_services = 64;
  5041. + /* If there's more than 64 services, only dump ones with
  5042. + * non-zero counts */
  5043. + int only_nonzero = 0;
  5044. + static const char *nz = "<-- preventing suspend";
  5045. +
  5046. + enum vc_suspend_status vc_suspend_state;
  5047. + enum vc_resume_status vc_resume_state;
  5048. + int peer_count;
  5049. + int vc_use_count;
  5050. + int active_services;
  5051. + struct service_data_struct {
  5052. + int fourcc;
  5053. + int clientid;
  5054. + int use_count;
  5055. + } service_data[local_max_services];
  5056. +
  5057. + if (!arm_state)
  5058. + return;
  5059. +
  5060. + read_lock_bh(&arm_state->susp_res_lock);
  5061. + vc_suspend_state = arm_state->vc_suspend_state;
  5062. + vc_resume_state = arm_state->vc_resume_state;
  5063. + peer_count = arm_state->peer_use_count;
  5064. + vc_use_count = arm_state->videocore_use_count;
  5065. + active_services = state->unused_service;
  5066. + if (active_services > local_max_services)
  5067. + only_nonzero = 1;
  5068. +
  5069. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  5070. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  5071. + if (!service_ptr)
  5072. + continue;
  5073. +
  5074. + if (only_nonzero && !service_ptr->service_use_count)
  5075. + continue;
  5076. +
  5077. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  5078. + service_data[j].fourcc = service_ptr->base.fourcc;
  5079. + service_data[j].clientid = service_ptr->client_id;
  5080. + service_data[j++].use_count = service_ptr->
  5081. + service_use_count;
  5082. + }
  5083. + }
  5084. +
  5085. + read_unlock_bh(&arm_state->susp_res_lock);
  5086. +
  5087. + vchiq_log_warning(vchiq_susp_log_level,
  5088. + "-- Videcore suspend state: %s --",
  5089. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  5090. + vchiq_log_warning(vchiq_susp_log_level,
  5091. + "-- Videcore resume state: %s --",
  5092. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  5093. +
  5094. + if (only_nonzero)
  5095. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  5096. + "services (%d). Only dumping up to first %d services "
  5097. + "with non-zero use-count", active_services,
  5098. + local_max_services);
  5099. +
  5100. + for (i = 0; i < j; i++) {
  5101. + vchiq_log_warning(vchiq_susp_log_level,
  5102. + "----- %c%c%c%c:%d service count %d %s",
  5103. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  5104. + service_data[i].clientid,
  5105. + service_data[i].use_count,
  5106. + service_data[i].use_count ? nz : "");
  5107. + }
  5108. + vchiq_log_warning(vchiq_susp_log_level,
  5109. + "----- VCHIQ use count count %d", peer_count);
  5110. + vchiq_log_warning(vchiq_susp_log_level,
  5111. + "--- Overall vchiq instance use count %d", vc_use_count);
  5112. +
  5113. + vchiq_dump_platform_use_state(state);
  5114. +}
  5115. +
  5116. +VCHIQ_STATUS_T
  5117. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  5118. +{
  5119. + VCHIQ_ARM_STATE_T *arm_state;
  5120. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  5121. +
  5122. + if (!service || !service->state)
  5123. + goto out;
  5124. +
  5125. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  5126. +
  5127. + arm_state = vchiq_platform_get_arm_state(service->state);
  5128. +
  5129. + read_lock_bh(&arm_state->susp_res_lock);
  5130. + if (service->service_use_count)
  5131. + ret = VCHIQ_SUCCESS;
  5132. + read_unlock_bh(&arm_state->susp_res_lock);
  5133. +
  5134. + if (ret == VCHIQ_ERROR) {
  5135. + vchiq_log_error(vchiq_susp_log_level,
  5136. + "%s ERROR - %c%c%c%c:%d service count %d, "
  5137. + "state count %d, videocore suspend state %s", __func__,
  5138. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  5139. + service->client_id, service->service_use_count,
  5140. + arm_state->videocore_use_count,
  5141. + suspend_state_names[arm_state->vc_suspend_state +
  5142. + VC_SUSPEND_NUM_OFFSET]);
  5143. + vchiq_dump_service_use_state(service->state);
  5144. + }
  5145. +out:
  5146. + return ret;
  5147. +}
  5148. +
  5149. +/* stub functions */
  5150. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  5151. +{
  5152. + (void)state;
  5153. +}
  5154. +
  5155. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  5156. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  5157. +{
  5158. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  5159. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  5160. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  5161. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  5162. + write_lock_bh(&arm_state->susp_res_lock);
  5163. + if (!arm_state->first_connect) {
  5164. + char threadname[10];
  5165. + arm_state->first_connect = 1;
  5166. + write_unlock_bh(&arm_state->susp_res_lock);
  5167. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  5168. + state->id);
  5169. + arm_state->ka_thread = kthread_create(
  5170. + &vchiq_keepalive_thread_func,
  5171. + (void *)state,
  5172. + threadname);
  5173. + if (arm_state->ka_thread == NULL) {
  5174. + vchiq_log_error(vchiq_susp_log_level,
  5175. + "vchiq: FATAL: couldn't create thread %s",
  5176. + threadname);
  5177. + } else {
  5178. + wake_up_process(arm_state->ka_thread);
  5179. + }
  5180. + } else
  5181. + write_unlock_bh(&arm_state->susp_res_lock);
  5182. + }
  5183. +}
  5184. +
  5185. +static int vchiq_probe(struct platform_device *pdev)
  5186. +{
  5187. + struct device_node *fw_node;
  5188. + struct rpi_firmware *fw;
  5189. + int err;
  5190. + void *ptr_err;
  5191. +
  5192. + fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
  5193. +/* Remove comment when booting without Device Tree is no longer supported
  5194. + if (!fw_node) {
  5195. + dev_err(&pdev->dev, "Missing firmware node\n");
  5196. + return -ENOENT;
  5197. + }
  5198. +*/
  5199. + fw = rpi_firmware_get(fw_node);
  5200. + if (!fw)
  5201. + return -EPROBE_DEFER;
  5202. +
  5203. + platform_set_drvdata(pdev, fw);
  5204. +
  5205. + /* create debugfs entries */
  5206. + err = vchiq_debugfs_init();
  5207. + if (err != 0)
  5208. + goto failed_debugfs_init;
  5209. +
  5210. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  5211. + if (err != 0) {
  5212. + vchiq_log_error(vchiq_arm_log_level,
  5213. + "Unable to allocate device number");
  5214. + goto failed_alloc_chrdev;
  5215. + }
  5216. + cdev_init(&vchiq_cdev, &vchiq_fops);
  5217. + vchiq_cdev.owner = THIS_MODULE;
  5218. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  5219. + if (err != 0) {
  5220. + vchiq_log_error(vchiq_arm_log_level,
  5221. + "Unable to register device");
  5222. + goto failed_cdev_add;
  5223. + }
  5224. +
  5225. + /* create sysfs entries */
  5226. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  5227. + ptr_err = vchiq_class;
  5228. + if (IS_ERR(ptr_err))
  5229. + goto failed_class_create;
  5230. +
  5231. + vchiq_dev = device_create(vchiq_class, NULL,
  5232. + vchiq_devid, NULL, "vchiq");
  5233. + ptr_err = vchiq_dev;
  5234. + if (IS_ERR(ptr_err))
  5235. + goto failed_device_create;
  5236. +
  5237. + err = vchiq_platform_init(pdev, &g_state);
  5238. + if (err != 0)
  5239. + goto failed_platform_init;
  5240. +
  5241. + vchiq_log_info(vchiq_arm_log_level,
  5242. + "vchiq: initialised - version %d (min %d), device %d.%d",
  5243. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  5244. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  5245. +
  5246. + return 0;
  5247. +
  5248. +failed_platform_init:
  5249. + device_destroy(vchiq_class, vchiq_devid);
  5250. +failed_device_create:
  5251. + class_destroy(vchiq_class);
  5252. +failed_class_create:
  5253. + cdev_del(&vchiq_cdev);
  5254. + err = PTR_ERR(ptr_err);
  5255. +failed_cdev_add:
  5256. + unregister_chrdev_region(vchiq_devid, 1);
  5257. +failed_alloc_chrdev:
  5258. + vchiq_debugfs_deinit();
  5259. +failed_debugfs_init:
  5260. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  5261. + return err;
  5262. +}
  5263. +
  5264. +static int vchiq_remove(struct platform_device *pdev)
  5265. +{
  5266. + device_destroy(vchiq_class, vchiq_devid);
  5267. + class_destroy(vchiq_class);
  5268. + cdev_del(&vchiq_cdev);
  5269. + unregister_chrdev_region(vchiq_devid, 1);
  5270. +
  5271. + return 0;
  5272. +}
  5273. +
  5274. +static const struct of_device_id vchiq_of_match[] = {
  5275. + { .compatible = "brcm,bcm2835-vchiq", },
  5276. + {},
  5277. +};
  5278. +MODULE_DEVICE_TABLE(of, vchiq_of_match);
  5279. +
  5280. +static struct platform_driver vchiq_driver = {
  5281. + .driver = {
  5282. + .name = "bcm2835_vchiq",
  5283. + .owner = THIS_MODULE,
  5284. + .of_match_table = vchiq_of_match,
  5285. + },
  5286. + .probe = vchiq_probe,
  5287. + .remove = vchiq_remove,
  5288. +};
  5289. +module_platform_driver(vchiq_driver);
  5290. +
  5291. +MODULE_LICENSE("GPL");
  5292. +MODULE_AUTHOR("Broadcom Corporation");
  5293. --- /dev/null
  5294. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  5295. @@ -0,0 +1,220 @@
  5296. +/**
  5297. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  5298. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5299. + *
  5300. + * Redistribution and use in source and binary forms, with or without
  5301. + * modification, are permitted provided that the following conditions
  5302. + * are met:
  5303. + * 1. Redistributions of source code must retain the above copyright
  5304. + * notice, this list of conditions, and the following disclaimer,
  5305. + * without modification.
  5306. + * 2. Redistributions in binary form must reproduce the above copyright
  5307. + * notice, this list of conditions and the following disclaimer in the
  5308. + * documentation and/or other materials provided with the distribution.
  5309. + * 3. The names of the above-listed copyright holders may not be used
  5310. + * to endorse or promote products derived from this software without
  5311. + * specific prior written permission.
  5312. + *
  5313. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5314. + * GNU General Public License ("GPL") version 2, as published by the Free
  5315. + * Software Foundation.
  5316. + *
  5317. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5318. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5319. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5320. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5321. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5322. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5323. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5324. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5325. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5326. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5327. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5328. + */
  5329. +
  5330. +#ifndef VCHIQ_ARM_H
  5331. +#define VCHIQ_ARM_H
  5332. +
  5333. +#include <linux/mutex.h>
  5334. +#include <linux/platform_device.h>
  5335. +#include <linux/semaphore.h>
  5336. +#include <linux/atomic.h>
  5337. +#include "vchiq_core.h"
  5338. +#include "vchiq_debugfs.h"
  5339. +
  5340. +
  5341. +enum vc_suspend_status {
  5342. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  5343. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  5344. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  5345. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  5346. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  5347. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  5348. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  5349. +};
  5350. +
  5351. +enum vc_resume_status {
  5352. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  5353. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  5354. + VC_RESUME_REQUESTED, /* User has requested resume */
  5355. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  5356. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  5357. +};
  5358. +
  5359. +
  5360. +enum USE_TYPE_E {
  5361. + USE_TYPE_SERVICE,
  5362. + USE_TYPE_SERVICE_NO_RESUME,
  5363. + USE_TYPE_VCHIQ
  5364. +};
  5365. +
  5366. +
  5367. +
  5368. +typedef struct vchiq_arm_state_struct {
  5369. + /* Keepalive-related data */
  5370. + struct task_struct *ka_thread;
  5371. + struct completion ka_evt;
  5372. + atomic_t ka_use_count;
  5373. + atomic_t ka_use_ack_count;
  5374. + atomic_t ka_release_count;
  5375. +
  5376. + struct completion vc_suspend_complete;
  5377. + struct completion vc_resume_complete;
  5378. +
  5379. + rwlock_t susp_res_lock;
  5380. + enum vc_suspend_status vc_suspend_state;
  5381. + enum vc_resume_status vc_resume_state;
  5382. +
  5383. + unsigned int wake_address;
  5384. +
  5385. + struct timer_list suspend_timer;
  5386. + int suspend_timer_timeout;
  5387. + int suspend_timer_running;
  5388. +
  5389. + /* Global use count for videocore.
  5390. + ** This is equal to the sum of the use counts for all services. When
  5391. + ** this hits zero the videocore suspend procedure will be initiated.
  5392. + */
  5393. + int videocore_use_count;
  5394. +
  5395. + /* Use count to track requests from videocore peer.
  5396. + ** This use count is not associated with a service, so needs to be
  5397. + ** tracked separately with the state.
  5398. + */
  5399. + int peer_use_count;
  5400. +
  5401. + /* Flag to indicate whether resume is blocked. This happens when the
  5402. + ** ARM is suspending
  5403. + */
  5404. + struct completion resume_blocker;
  5405. + int resume_blocked;
  5406. + struct completion blocked_blocker;
  5407. + int blocked_count;
  5408. +
  5409. + int autosuspend_override;
  5410. +
  5411. + /* Flag to indicate that the first vchiq connect has made it through.
  5412. + ** This means that both sides should be fully ready, and we should
  5413. + ** be able to suspend after this point.
  5414. + */
  5415. + int first_connect;
  5416. +
  5417. + unsigned long long suspend_start_time;
  5418. + unsigned long long sleep_start_time;
  5419. + unsigned long long resume_start_time;
  5420. + unsigned long long last_wake_time;
  5421. +
  5422. +} VCHIQ_ARM_STATE_T;
  5423. +
  5424. +extern int vchiq_arm_log_level;
  5425. +extern int vchiq_susp_log_level;
  5426. +
  5427. +int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state);
  5428. +
  5429. +extern VCHIQ_STATE_T *
  5430. +vchiq_get_state(void);
  5431. +
  5432. +extern VCHIQ_STATUS_T
  5433. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  5434. +
  5435. +extern VCHIQ_STATUS_T
  5436. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  5437. +
  5438. +extern int
  5439. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  5440. +
  5441. +extern VCHIQ_STATUS_T
  5442. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  5443. +
  5444. +extern VCHIQ_STATUS_T
  5445. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  5446. +
  5447. +extern int
  5448. +vchiq_check_resume(VCHIQ_STATE_T *state);
  5449. +
  5450. +extern void
  5451. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  5452. + VCHIQ_STATUS_T
  5453. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  5454. +
  5455. +extern VCHIQ_STATUS_T
  5456. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  5457. +
  5458. +extern VCHIQ_STATUS_T
  5459. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  5460. +
  5461. +extern VCHIQ_STATUS_T
  5462. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  5463. +
  5464. +extern int
  5465. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  5466. +
  5467. +extern int
  5468. +vchiq_platform_use_suspend_timer(void);
  5469. +
  5470. +extern void
  5471. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  5472. +
  5473. +extern void
  5474. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  5475. +
  5476. +extern VCHIQ_ARM_STATE_T*
  5477. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  5478. +
  5479. +extern int
  5480. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  5481. +
  5482. +extern VCHIQ_STATUS_T
  5483. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  5484. + enum USE_TYPE_E use_type);
  5485. +extern VCHIQ_STATUS_T
  5486. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  5487. +
  5488. +extern VCHIQ_DEBUGFS_NODE_T *
  5489. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
  5490. +
  5491. +extern int
  5492. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
  5493. +
  5494. +extern int
  5495. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
  5496. +
  5497. +extern int
  5498. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
  5499. +
  5500. +extern void
  5501. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
  5502. +
  5503. +extern void
  5504. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  5505. + enum vc_suspend_status new_state);
  5506. +
  5507. +extern void
  5508. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  5509. + enum vc_resume_status new_state);
  5510. +
  5511. +extern void
  5512. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  5513. +
  5514. +
  5515. +#endif /* VCHIQ_ARM_H */
  5516. --- /dev/null
  5517. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  5518. @@ -0,0 +1,37 @@
  5519. +/**
  5520. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5521. + *
  5522. + * Redistribution and use in source and binary forms, with or without
  5523. + * modification, are permitted provided that the following conditions
  5524. + * are met:
  5525. + * 1. Redistributions of source code must retain the above copyright
  5526. + * notice, this list of conditions, and the following disclaimer,
  5527. + * without modification.
  5528. + * 2. Redistributions in binary form must reproduce the above copyright
  5529. + * notice, this list of conditions and the following disclaimer in the
  5530. + * documentation and/or other materials provided with the distribution.
  5531. + * 3. The names of the above-listed copyright holders may not be used
  5532. + * to endorse or promote products derived from this software without
  5533. + * specific prior written permission.
  5534. + *
  5535. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5536. + * GNU General Public License ("GPL") version 2, as published by the Free
  5537. + * Software Foundation.
  5538. + *
  5539. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5540. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5541. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5542. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5543. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5544. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5545. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5546. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5547. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5548. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5549. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5550. + */
  5551. +
  5552. +const char *vchiq_get_build_hostname(void);
  5553. +const char *vchiq_get_build_version(void);
  5554. +const char *vchiq_get_build_time(void);
  5555. +const char *vchiq_get_build_date(void);
  5556. --- /dev/null
  5557. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  5558. @@ -0,0 +1,69 @@
  5559. +/**
  5560. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  5561. + *
  5562. + * Redistribution and use in source and binary forms, with or without
  5563. + * modification, are permitted provided that the following conditions
  5564. + * are met:
  5565. + * 1. Redistributions of source code must retain the above copyright
  5566. + * notice, this list of conditions, and the following disclaimer,
  5567. + * without modification.
  5568. + * 2. Redistributions in binary form must reproduce the above copyright
  5569. + * notice, this list of conditions and the following disclaimer in the
  5570. + * documentation and/or other materials provided with the distribution.
  5571. + * 3. The names of the above-listed copyright holders may not be used
  5572. + * to endorse or promote products derived from this software without
  5573. + * specific prior written permission.
  5574. + *
  5575. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5576. + * GNU General Public License ("GPL") version 2, as published by the Free
  5577. + * Software Foundation.
  5578. + *
  5579. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5580. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5581. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5582. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5583. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5584. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5585. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5586. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5587. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5588. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5589. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5590. + */
  5591. +
  5592. +#ifndef VCHIQ_CFG_H
  5593. +#define VCHIQ_CFG_H
  5594. +
  5595. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  5596. +/* The version of VCHIQ - change with any non-trivial change */
  5597. +#define VCHIQ_VERSION 8
  5598. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  5599. +** incompatible change */
  5600. +#define VCHIQ_VERSION_MIN 3
  5601. +
  5602. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  5603. +#define VCHIQ_VERSION_LIB_VERSION 7
  5604. +
  5605. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  5606. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  5607. +
  5608. +/* The version that made it safe to use SYNCHRONOUS mode */
  5609. +#define VCHIQ_VERSION_SYNCHRONOUS_MODE 8
  5610. +
  5611. +#define VCHIQ_MAX_STATES 1
  5612. +#define VCHIQ_MAX_SERVICES 4096
  5613. +#define VCHIQ_MAX_SLOTS 128
  5614. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  5615. +
  5616. +#define VCHIQ_NUM_CURRENT_BULKS 32
  5617. +#define VCHIQ_NUM_SERVICE_BULKS 4
  5618. +
  5619. +#ifndef VCHIQ_ENABLE_DEBUG
  5620. +#define VCHIQ_ENABLE_DEBUG 1
  5621. +#endif
  5622. +
  5623. +#ifndef VCHIQ_ENABLE_STATS
  5624. +#define VCHIQ_ENABLE_STATS 1
  5625. +#endif
  5626. +
  5627. +#endif /* VCHIQ_CFG_H */
  5628. --- /dev/null
  5629. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  5630. @@ -0,0 +1,120 @@
  5631. +/**
  5632. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5633. + *
  5634. + * Redistribution and use in source and binary forms, with or without
  5635. + * modification, are permitted provided that the following conditions
  5636. + * are met:
  5637. + * 1. Redistributions of source code must retain the above copyright
  5638. + * notice, this list of conditions, and the following disclaimer,
  5639. + * without modification.
  5640. + * 2. Redistributions in binary form must reproduce the above copyright
  5641. + * notice, this list of conditions and the following disclaimer in the
  5642. + * documentation and/or other materials provided with the distribution.
  5643. + * 3. The names of the above-listed copyright holders may not be used
  5644. + * to endorse or promote products derived from this software without
  5645. + * specific prior written permission.
  5646. + *
  5647. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5648. + * GNU General Public License ("GPL") version 2, as published by the Free
  5649. + * Software Foundation.
  5650. + *
  5651. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5652. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5653. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5654. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5655. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5656. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5657. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5658. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5659. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5660. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5661. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5662. + */
  5663. +
  5664. +#include "vchiq_connected.h"
  5665. +#include "vchiq_core.h"
  5666. +#include "vchiq_killable.h"
  5667. +#include <linux/module.h>
  5668. +#include <linux/mutex.h>
  5669. +
  5670. +#define MAX_CALLBACKS 10
  5671. +
  5672. +static int g_connected;
  5673. +static int g_num_deferred_callbacks;
  5674. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  5675. +static int g_once_init;
  5676. +static struct mutex g_connected_mutex;
  5677. +
  5678. +/****************************************************************************
  5679. +*
  5680. +* Function to initialize our lock.
  5681. +*
  5682. +***************************************************************************/
  5683. +
  5684. +static void connected_init(void)
  5685. +{
  5686. + if (!g_once_init) {
  5687. + mutex_init(&g_connected_mutex);
  5688. + g_once_init = 1;
  5689. + }
  5690. +}
  5691. +
  5692. +/****************************************************************************
  5693. +*
  5694. +* This function is used to defer initialization until the vchiq stack is
  5695. +* initialized. If the stack is already initialized, then the callback will
  5696. +* be made immediately, otherwise it will be deferred until
  5697. +* vchiq_call_connected_callbacks is called.
  5698. +*
  5699. +***************************************************************************/
  5700. +
  5701. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  5702. +{
  5703. + connected_init();
  5704. +
  5705. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5706. + return;
  5707. +
  5708. + if (g_connected)
  5709. + /* We're already connected. Call the callback immediately. */
  5710. +
  5711. + callback();
  5712. + else {
  5713. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  5714. + vchiq_log_error(vchiq_core_log_level,
  5715. + "There already %d callback registered - "
  5716. + "please increase MAX_CALLBACKS",
  5717. + g_num_deferred_callbacks);
  5718. + else {
  5719. + g_deferred_callback[g_num_deferred_callbacks] =
  5720. + callback;
  5721. + g_num_deferred_callbacks++;
  5722. + }
  5723. + }
  5724. + mutex_unlock(&g_connected_mutex);
  5725. +}
  5726. +
  5727. +/****************************************************************************
  5728. +*
  5729. +* This function is called by the vchiq stack once it has been connected to
  5730. +* the videocore and clients can start to use the stack.
  5731. +*
  5732. +***************************************************************************/
  5733. +
  5734. +void vchiq_call_connected_callbacks(void)
  5735. +{
  5736. + int i;
  5737. +
  5738. + connected_init();
  5739. +
  5740. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5741. + return;
  5742. +
  5743. + for (i = 0; i < g_num_deferred_callbacks; i++)
  5744. + g_deferred_callback[i]();
  5745. +
  5746. + g_num_deferred_callbacks = 0;
  5747. + g_connected = 1;
  5748. + mutex_unlock(&g_connected_mutex);
  5749. +}
  5750. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  5751. --- /dev/null
  5752. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  5753. @@ -0,0 +1,50 @@
  5754. +/**
  5755. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5756. + *
  5757. + * Redistribution and use in source and binary forms, with or without
  5758. + * modification, are permitted provided that the following conditions
  5759. + * are met:
  5760. + * 1. Redistributions of source code must retain the above copyright
  5761. + * notice, this list of conditions, and the following disclaimer,
  5762. + * without modification.
  5763. + * 2. Redistributions in binary form must reproduce the above copyright
  5764. + * notice, this list of conditions and the following disclaimer in the
  5765. + * documentation and/or other materials provided with the distribution.
  5766. + * 3. The names of the above-listed copyright holders may not be used
  5767. + * to endorse or promote products derived from this software without
  5768. + * specific prior written permission.
  5769. + *
  5770. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5771. + * GNU General Public License ("GPL") version 2, as published by the Free
  5772. + * Software Foundation.
  5773. + *
  5774. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5775. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5776. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5777. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5778. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5779. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5780. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5781. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5782. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5783. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5784. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5785. + */
  5786. +
  5787. +#ifndef VCHIQ_CONNECTED_H
  5788. +#define VCHIQ_CONNECTED_H
  5789. +
  5790. +/* ---- Include Files ----------------------------------------------------- */
  5791. +
  5792. +/* ---- Constants and Types ---------------------------------------------- */
  5793. +
  5794. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  5795. +
  5796. +/* ---- Variable Externs ------------------------------------------------- */
  5797. +
  5798. +/* ---- Function Prototypes ---------------------------------------------- */
  5799. +
  5800. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  5801. +void vchiq_call_connected_callbacks(void);
  5802. +
  5803. +#endif /* VCHIQ_CONNECTED_H */
  5804. --- /dev/null
  5805. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  5806. @@ -0,0 +1,3934 @@
  5807. +/**
  5808. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5809. + *
  5810. + * Redistribution and use in source and binary forms, with or without
  5811. + * modification, are permitted provided that the following conditions
  5812. + * are met:
  5813. + * 1. Redistributions of source code must retain the above copyright
  5814. + * notice, this list of conditions, and the following disclaimer,
  5815. + * without modification.
  5816. + * 2. Redistributions in binary form must reproduce the above copyright
  5817. + * notice, this list of conditions and the following disclaimer in the
  5818. + * documentation and/or other materials provided with the distribution.
  5819. + * 3. The names of the above-listed copyright holders may not be used
  5820. + * to endorse or promote products derived from this software without
  5821. + * specific prior written permission.
  5822. + *
  5823. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5824. + * GNU General Public License ("GPL") version 2, as published by the Free
  5825. + * Software Foundation.
  5826. + *
  5827. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5828. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5829. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5830. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5831. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5832. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5833. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5834. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5835. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5836. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5837. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5838. + */
  5839. +
  5840. +#include "vchiq_core.h"
  5841. +#include "vchiq_killable.h"
  5842. +
  5843. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  5844. +
  5845. +#define HANDLE_STATE_SHIFT 12
  5846. +
  5847. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  5848. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  5849. +#define SLOT_INDEX_FROM_DATA(state, data) \
  5850. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  5851. + VCHIQ_SLOT_SIZE)
  5852. +#define SLOT_INDEX_FROM_INFO(state, info) \
  5853. + ((unsigned int)(info - state->slot_info))
  5854. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  5855. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  5856. +
  5857. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  5858. +
  5859. +#define SRVTRACE_LEVEL(srv) \
  5860. + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
  5861. +#define SRVTRACE_ENABLED(srv, lev) \
  5862. + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
  5863. +
  5864. +struct vchiq_open_payload {
  5865. + int fourcc;
  5866. + int client_id;
  5867. + short version;
  5868. + short version_min;
  5869. +};
  5870. +
  5871. +struct vchiq_openack_payload {
  5872. + short version;
  5873. +};
  5874. +
  5875. +enum
  5876. +{
  5877. + QMFLAGS_IS_BLOCKING = (1 << 0),
  5878. + QMFLAGS_NO_MUTEX_LOCK = (1 << 1),
  5879. + QMFLAGS_NO_MUTEX_UNLOCK = (1 << 2)
  5880. +};
  5881. +
  5882. +/* we require this for consistency between endpoints */
  5883. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  5884. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  5885. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  5886. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  5887. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  5888. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  5889. +
  5890. +/* Run time control of log level, based on KERN_XXX level. */
  5891. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  5892. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  5893. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  5894. +
  5895. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  5896. +
  5897. +static DEFINE_SPINLOCK(service_spinlock);
  5898. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  5899. +DEFINE_SPINLOCK(quota_spinlock);
  5900. +
  5901. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  5902. +static unsigned int handle_seq;
  5903. +
  5904. +static const char *const srvstate_names[] = {
  5905. + "FREE",
  5906. + "HIDDEN",
  5907. + "LISTENING",
  5908. + "OPENING",
  5909. + "OPEN",
  5910. + "OPENSYNC",
  5911. + "CLOSESENT",
  5912. + "CLOSERECVD",
  5913. + "CLOSEWAIT",
  5914. + "CLOSED"
  5915. +};
  5916. +
  5917. +static const char *const reason_names[] = {
  5918. + "SERVICE_OPENED",
  5919. + "SERVICE_CLOSED",
  5920. + "MESSAGE_AVAILABLE",
  5921. + "BULK_TRANSMIT_DONE",
  5922. + "BULK_RECEIVE_DONE",
  5923. + "BULK_TRANSMIT_ABORTED",
  5924. + "BULK_RECEIVE_ABORTED"
  5925. +};
  5926. +
  5927. +static const char *const conn_state_names[] = {
  5928. + "DISCONNECTED",
  5929. + "CONNECTING",
  5930. + "CONNECTED",
  5931. + "PAUSING",
  5932. + "PAUSE_SENT",
  5933. + "PAUSED",
  5934. + "RESUMING",
  5935. + "PAUSE_TIMEOUT",
  5936. + "RESUME_TIMEOUT"
  5937. +};
  5938. +
  5939. +
  5940. +static void
  5941. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  5942. +
  5943. +static const char *msg_type_str(unsigned int msg_type)
  5944. +{
  5945. + switch (msg_type) {
  5946. + case VCHIQ_MSG_PADDING: return "PADDING";
  5947. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  5948. + case VCHIQ_MSG_OPEN: return "OPEN";
  5949. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  5950. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  5951. + case VCHIQ_MSG_DATA: return "DATA";
  5952. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  5953. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  5954. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  5955. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  5956. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  5957. + case VCHIQ_MSG_RESUME: return "RESUME";
  5958. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  5959. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  5960. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  5961. + }
  5962. + return "???";
  5963. +}
  5964. +
  5965. +static inline void
  5966. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  5967. +{
  5968. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  5969. + service->state->id, service->localport,
  5970. + srvstate_names[service->srvstate],
  5971. + srvstate_names[newstate]);
  5972. + service->srvstate = newstate;
  5973. +}
  5974. +
  5975. +VCHIQ_SERVICE_T *
  5976. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  5977. +{
  5978. + VCHIQ_SERVICE_T *service;
  5979. +
  5980. + spin_lock(&service_spinlock);
  5981. + service = handle_to_service(handle);
  5982. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  5983. + (service->handle == handle)) {
  5984. + BUG_ON(service->ref_count == 0);
  5985. + service->ref_count++;
  5986. + } else
  5987. + service = NULL;
  5988. + spin_unlock(&service_spinlock);
  5989. +
  5990. + if (!service)
  5991. + vchiq_log_info(vchiq_core_log_level,
  5992. + "Invalid service handle 0x%x", handle);
  5993. +
  5994. + return service;
  5995. +}
  5996. +
  5997. +VCHIQ_SERVICE_T *
  5998. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  5999. +{
  6000. + VCHIQ_SERVICE_T *service = NULL;
  6001. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  6002. + spin_lock(&service_spinlock);
  6003. + service = state->services[localport];
  6004. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  6005. + BUG_ON(service->ref_count == 0);
  6006. + service->ref_count++;
  6007. + } else
  6008. + service = NULL;
  6009. + spin_unlock(&service_spinlock);
  6010. + }
  6011. +
  6012. + if (!service)
  6013. + vchiq_log_info(vchiq_core_log_level,
  6014. + "Invalid port %d", localport);
  6015. +
  6016. + return service;
  6017. +}
  6018. +
  6019. +VCHIQ_SERVICE_T *
  6020. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  6021. + VCHIQ_SERVICE_HANDLE_T handle) {
  6022. + VCHIQ_SERVICE_T *service;
  6023. +
  6024. + spin_lock(&service_spinlock);
  6025. + service = handle_to_service(handle);
  6026. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  6027. + (service->handle == handle) &&
  6028. + (service->instance == instance)) {
  6029. + BUG_ON(service->ref_count == 0);
  6030. + service->ref_count++;
  6031. + } else
  6032. + service = NULL;
  6033. + spin_unlock(&service_spinlock);
  6034. +
  6035. + if (!service)
  6036. + vchiq_log_info(vchiq_core_log_level,
  6037. + "Invalid service handle 0x%x", handle);
  6038. +
  6039. + return service;
  6040. +}
  6041. +
  6042. +VCHIQ_SERVICE_T *
  6043. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  6044. + VCHIQ_SERVICE_HANDLE_T handle) {
  6045. + VCHIQ_SERVICE_T *service;
  6046. +
  6047. + spin_lock(&service_spinlock);
  6048. + service = handle_to_service(handle);
  6049. + if (service &&
  6050. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  6051. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  6052. + (service->handle == handle) &&
  6053. + (service->instance == instance)) {
  6054. + BUG_ON(service->ref_count == 0);
  6055. + service->ref_count++;
  6056. + } else
  6057. + service = NULL;
  6058. + spin_unlock(&service_spinlock);
  6059. +
  6060. + if (!service)
  6061. + vchiq_log_info(vchiq_core_log_level,
  6062. + "Invalid service handle 0x%x", handle);
  6063. +
  6064. + return service;
  6065. +}
  6066. +
  6067. +VCHIQ_SERVICE_T *
  6068. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  6069. + int *pidx)
  6070. +{
  6071. + VCHIQ_SERVICE_T *service = NULL;
  6072. + int idx = *pidx;
  6073. +
  6074. + spin_lock(&service_spinlock);
  6075. + while (idx < state->unused_service) {
  6076. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  6077. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  6078. + (srv->instance == instance)) {
  6079. + service = srv;
  6080. + BUG_ON(service->ref_count == 0);
  6081. + service->ref_count++;
  6082. + break;
  6083. + }
  6084. + }
  6085. + spin_unlock(&service_spinlock);
  6086. +
  6087. + *pidx = idx;
  6088. +
  6089. + return service;
  6090. +}
  6091. +
  6092. +void
  6093. +lock_service(VCHIQ_SERVICE_T *service)
  6094. +{
  6095. + spin_lock(&service_spinlock);
  6096. + BUG_ON(!service || (service->ref_count == 0));
  6097. + if (service)
  6098. + service->ref_count++;
  6099. + spin_unlock(&service_spinlock);
  6100. +}
  6101. +
  6102. +void
  6103. +unlock_service(VCHIQ_SERVICE_T *service)
  6104. +{
  6105. + VCHIQ_STATE_T *state = service->state;
  6106. + spin_lock(&service_spinlock);
  6107. + BUG_ON(!service || (service->ref_count == 0));
  6108. + if (service && service->ref_count) {
  6109. + service->ref_count--;
  6110. + if (!service->ref_count) {
  6111. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  6112. + state->services[service->localport] = NULL;
  6113. + } else
  6114. + service = NULL;
  6115. + }
  6116. + spin_unlock(&service_spinlock);
  6117. +
  6118. + if (service && service->userdata_term)
  6119. + service->userdata_term(service->base.userdata);
  6120. +
  6121. + kfree(service);
  6122. +}
  6123. +
  6124. +int
  6125. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  6126. +{
  6127. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  6128. + int id;
  6129. +
  6130. + id = service ? service->client_id : 0;
  6131. + if (service)
  6132. + unlock_service(service);
  6133. +
  6134. + return id;
  6135. +}
  6136. +
  6137. +void *
  6138. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  6139. +{
  6140. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  6141. +
  6142. + return service ? service->base.userdata : NULL;
  6143. +}
  6144. +
  6145. +int
  6146. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  6147. +{
  6148. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  6149. +
  6150. + return service ? service->base.fourcc : 0;
  6151. +}
  6152. +
  6153. +static void
  6154. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  6155. +{
  6156. + VCHIQ_STATE_T *state = service->state;
  6157. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  6158. +
  6159. + service->closing = 1;
  6160. +
  6161. + /* Synchronise with other threads. */
  6162. + mutex_lock(&state->recycle_mutex);
  6163. + mutex_unlock(&state->recycle_mutex);
  6164. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  6165. + /* If we're pausing then the slot_mutex is held until resume
  6166. + * by the slot handler. Therefore don't try to acquire this
  6167. + * mutex if we're the slot handler and in the pause sent state.
  6168. + * We don't need to in this case anyway. */
  6169. + mutex_lock(&state->slot_mutex);
  6170. + mutex_unlock(&state->slot_mutex);
  6171. + }
  6172. +
  6173. + /* Unblock any sending thread. */
  6174. + service_quota = &state->service_quotas[service->localport];
  6175. + up(&service_quota->quota_event);
  6176. +}
  6177. +
  6178. +static void
  6179. +mark_service_closing(VCHIQ_SERVICE_T *service)
  6180. +{
  6181. + mark_service_closing_internal(service, 0);
  6182. +}
  6183. +
  6184. +static inline VCHIQ_STATUS_T
  6185. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  6186. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  6187. +{
  6188. + VCHIQ_STATUS_T status;
  6189. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  6190. + service->state->id, service->localport, reason_names[reason],
  6191. + (unsigned int)header, (unsigned int)bulk_userdata);
  6192. + status = service->base.callback(reason, header, service->handle,
  6193. + bulk_userdata);
  6194. + if (status == VCHIQ_ERROR) {
  6195. + vchiq_log_warning(vchiq_core_log_level,
  6196. + "%d: ignoring ERROR from callback to service %x",
  6197. + service->state->id, service->handle);
  6198. + status = VCHIQ_SUCCESS;
  6199. + }
  6200. + return status;
  6201. +}
  6202. +
  6203. +inline void
  6204. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  6205. +{
  6206. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  6207. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  6208. + conn_state_names[oldstate],
  6209. + conn_state_names[newstate]);
  6210. + state->conn_state = newstate;
  6211. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  6212. +}
  6213. +
  6214. +static inline void
  6215. +remote_event_create(REMOTE_EVENT_T *event)
  6216. +{
  6217. + event->armed = 0;
  6218. + /* Don't clear the 'fired' flag because it may already have been set
  6219. + ** by the other side. */
  6220. + sema_init(event->event, 0);
  6221. +}
  6222. +
  6223. +static inline void
  6224. +remote_event_destroy(REMOTE_EVENT_T *event)
  6225. +{
  6226. + (void)event;
  6227. +}
  6228. +
  6229. +static inline int
  6230. +remote_event_wait(REMOTE_EVENT_T *event)
  6231. +{
  6232. + if (!event->fired) {
  6233. + event->armed = 1;
  6234. + dsb();
  6235. + if (!event->fired) {
  6236. + if (down_interruptible(event->event) != 0) {
  6237. + event->armed = 0;
  6238. + return 0;
  6239. + }
  6240. + }
  6241. + event->armed = 0;
  6242. + wmb();
  6243. + }
  6244. +
  6245. + event->fired = 0;
  6246. + return 1;
  6247. +}
  6248. +
  6249. +static inline void
  6250. +remote_event_signal_local(REMOTE_EVENT_T *event)
  6251. +{
  6252. + event->armed = 0;
  6253. + up(event->event);
  6254. +}
  6255. +
  6256. +static inline void
  6257. +remote_event_poll(REMOTE_EVENT_T *event)
  6258. +{
  6259. + if (event->fired && event->armed)
  6260. + remote_event_signal_local(event);
  6261. +}
  6262. +
  6263. +void
  6264. +remote_event_pollall(VCHIQ_STATE_T *state)
  6265. +{
  6266. + remote_event_poll(&state->local->sync_trigger);
  6267. + remote_event_poll(&state->local->sync_release);
  6268. + remote_event_poll(&state->local->trigger);
  6269. + remote_event_poll(&state->local->recycle);
  6270. +}
  6271. +
  6272. +/* Round up message sizes so that any space at the end of a slot is always big
  6273. +** enough for a header. This relies on header size being a power of two, which
  6274. +** has been verified earlier by a static assertion. */
  6275. +
  6276. +static inline unsigned int
  6277. +calc_stride(unsigned int size)
  6278. +{
  6279. + /* Allow room for the header */
  6280. + size += sizeof(VCHIQ_HEADER_T);
  6281. +
  6282. + /* Round up */
  6283. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  6284. + - 1);
  6285. +}
  6286. +
  6287. +/* Called by the slot handler thread */
  6288. +static VCHIQ_SERVICE_T *
  6289. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  6290. +{
  6291. + int i;
  6292. +
  6293. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  6294. +
  6295. + for (i = 0; i < state->unused_service; i++) {
  6296. + VCHIQ_SERVICE_T *service = state->services[i];
  6297. + if (service &&
  6298. + (service->public_fourcc == fourcc) &&
  6299. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  6300. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  6301. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  6302. + lock_service(service);
  6303. + return service;
  6304. + }
  6305. + }
  6306. +
  6307. + return NULL;
  6308. +}
  6309. +
  6310. +/* Called by the slot handler thread */
  6311. +static VCHIQ_SERVICE_T *
  6312. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  6313. +{
  6314. + int i;
  6315. + for (i = 0; i < state->unused_service; i++) {
  6316. + VCHIQ_SERVICE_T *service = state->services[i];
  6317. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  6318. + && (service->remoteport == port)) {
  6319. + lock_service(service);
  6320. + return service;
  6321. + }
  6322. + }
  6323. + return NULL;
  6324. +}
  6325. +
  6326. +inline void
  6327. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  6328. +{
  6329. + uint32_t value;
  6330. +
  6331. + if (service) {
  6332. + do {
  6333. + value = atomic_read(&service->poll_flags);
  6334. + } while (atomic_cmpxchg(&service->poll_flags, value,
  6335. + value | (1 << poll_type)) != value);
  6336. +
  6337. + do {
  6338. + value = atomic_read(&state->poll_services[
  6339. + service->localport>>5]);
  6340. + } while (atomic_cmpxchg(
  6341. + &state->poll_services[service->localport>>5],
  6342. + value, value | (1 << (service->localport & 0x1f)))
  6343. + != value);
  6344. + }
  6345. +
  6346. + state->poll_needed = 1;
  6347. + wmb();
  6348. +
  6349. + /* ... and ensure the slot handler runs. */
  6350. + remote_event_signal_local(&state->local->trigger);
  6351. +}
  6352. +
  6353. +/* Called from queue_message, by the slot handler and application threads,
  6354. +** with slot_mutex held */
  6355. +static VCHIQ_HEADER_T *
  6356. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  6357. +{
  6358. + VCHIQ_SHARED_STATE_T *local = state->local;
  6359. + int tx_pos = state->local_tx_pos;
  6360. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  6361. +
  6362. + if (space > slot_space) {
  6363. + VCHIQ_HEADER_T *header;
  6364. + /* Fill the remaining space with padding */
  6365. + WARN_ON(state->tx_data == NULL);
  6366. + header = (VCHIQ_HEADER_T *)
  6367. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6368. + header->msgid = VCHIQ_MSGID_PADDING;
  6369. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  6370. +
  6371. + tx_pos += slot_space;
  6372. + }
  6373. +
  6374. + /* If necessary, get the next slot. */
  6375. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  6376. + int slot_index;
  6377. +
  6378. + /* If there is no free slot... */
  6379. +
  6380. + if (down_trylock(&state->slot_available_event) != 0) {
  6381. + /* ...wait for one. */
  6382. +
  6383. + VCHIQ_STATS_INC(state, slot_stalls);
  6384. +
  6385. + /* But first, flush through the last slot. */
  6386. + state->local_tx_pos = tx_pos;
  6387. + local->tx_pos = tx_pos;
  6388. + remote_event_signal(&state->remote->trigger);
  6389. +
  6390. + if (!is_blocking ||
  6391. + (down_interruptible(
  6392. + &state->slot_available_event) != 0))
  6393. + return NULL; /* No space available */
  6394. + }
  6395. +
  6396. + BUG_ON(tx_pos ==
  6397. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  6398. +
  6399. + slot_index = local->slot_queue[
  6400. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  6401. + VCHIQ_SLOT_QUEUE_MASK];
  6402. + state->tx_data =
  6403. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6404. + }
  6405. +
  6406. + state->local_tx_pos = tx_pos + space;
  6407. +
  6408. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6409. +}
  6410. +
  6411. +/* Called by the recycle thread. */
  6412. +static void
  6413. +process_free_queue(VCHIQ_STATE_T *state)
  6414. +{
  6415. + VCHIQ_SHARED_STATE_T *local = state->local;
  6416. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  6417. + int slot_queue_available;
  6418. +
  6419. + /* Use a read memory barrier to ensure that any state that may have
  6420. + ** been modified by another thread is not masked by stale prefetched
  6421. + ** values. */
  6422. + rmb();
  6423. +
  6424. + /* Find slots which have been freed by the other side, and return them
  6425. + ** to the available queue. */
  6426. + slot_queue_available = state->slot_queue_available;
  6427. +
  6428. + while (slot_queue_available != local->slot_queue_recycle) {
  6429. + unsigned int pos;
  6430. + int slot_index = local->slot_queue[slot_queue_available++ &
  6431. + VCHIQ_SLOT_QUEUE_MASK];
  6432. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6433. + int data_found = 0;
  6434. +
  6435. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  6436. + state->id, slot_index, (unsigned int)data,
  6437. + local->slot_queue_recycle, slot_queue_available);
  6438. +
  6439. + /* Initialise the bitmask for services which have used this
  6440. + ** slot */
  6441. + BITSET_ZERO(service_found);
  6442. +
  6443. + pos = 0;
  6444. +
  6445. + while (pos < VCHIQ_SLOT_SIZE) {
  6446. + VCHIQ_HEADER_T *header =
  6447. + (VCHIQ_HEADER_T *)(data + pos);
  6448. + int msgid = header->msgid;
  6449. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  6450. + int port = VCHIQ_MSG_SRCPORT(msgid);
  6451. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  6452. + &state->service_quotas[port];
  6453. + int count;
  6454. + spin_lock(&quota_spinlock);
  6455. + count = service_quota->message_use_count;
  6456. + if (count > 0)
  6457. + service_quota->message_use_count =
  6458. + count - 1;
  6459. + spin_unlock(&quota_spinlock);
  6460. +
  6461. + if (count == service_quota->message_quota)
  6462. + /* Signal the service that it
  6463. + ** has dropped below its quota
  6464. + */
  6465. + up(&service_quota->quota_event);
  6466. + else if (count == 0) {
  6467. + vchiq_log_error(vchiq_core_log_level,
  6468. + "service %d "
  6469. + "message_use_count=%d "
  6470. + "(header %x, msgid %x, "
  6471. + "header->msgid %x, "
  6472. + "header->size %x)",
  6473. + port,
  6474. + service_quota->
  6475. + message_use_count,
  6476. + (unsigned int)header, msgid,
  6477. + header->msgid,
  6478. + header->size);
  6479. + WARN(1, "invalid message use count\n");
  6480. + }
  6481. + if (!BITSET_IS_SET(service_found, port)) {
  6482. + /* Set the found bit for this service */
  6483. + BITSET_SET(service_found, port);
  6484. +
  6485. + spin_lock(&quota_spinlock);
  6486. + count = service_quota->slot_use_count;
  6487. + if (count > 0)
  6488. + service_quota->slot_use_count =
  6489. + count - 1;
  6490. + spin_unlock(&quota_spinlock);
  6491. +
  6492. + if (count > 0) {
  6493. + /* Signal the service in case
  6494. + ** it has dropped below its
  6495. + ** quota */
  6496. + up(&service_quota->quota_event);
  6497. + vchiq_log_trace(
  6498. + vchiq_core_log_level,
  6499. + "%d: pfq:%d %x@%x - "
  6500. + "slot_use->%d",
  6501. + state->id, port,
  6502. + header->size,
  6503. + (unsigned int)header,
  6504. + count - 1);
  6505. + } else {
  6506. + vchiq_log_error(
  6507. + vchiq_core_log_level,
  6508. + "service %d "
  6509. + "slot_use_count"
  6510. + "=%d (header %x"
  6511. + ", msgid %x, "
  6512. + "header->msgid"
  6513. + " %x, header->"
  6514. + "size %x)",
  6515. + port, count,
  6516. + (unsigned int)header,
  6517. + msgid,
  6518. + header->msgid,
  6519. + header->size);
  6520. + WARN(1, "bad slot use count\n");
  6521. + }
  6522. + }
  6523. +
  6524. + data_found = 1;
  6525. + }
  6526. +
  6527. + pos += calc_stride(header->size);
  6528. + if (pos > VCHIQ_SLOT_SIZE) {
  6529. + vchiq_log_error(vchiq_core_log_level,
  6530. + "pfq - pos %x: header %x, msgid %x, "
  6531. + "header->msgid %x, header->size %x",
  6532. + pos, (unsigned int)header, msgid,
  6533. + header->msgid, header->size);
  6534. + WARN(1, "invalid slot position\n");
  6535. + }
  6536. + }
  6537. +
  6538. + if (data_found) {
  6539. + int count;
  6540. + spin_lock(&quota_spinlock);
  6541. + count = state->data_use_count;
  6542. + if (count > 0)
  6543. + state->data_use_count =
  6544. + count - 1;
  6545. + spin_unlock(&quota_spinlock);
  6546. + if (count == state->data_quota)
  6547. + up(&state->data_quota_event);
  6548. + }
  6549. +
  6550. + state->slot_queue_available = slot_queue_available;
  6551. + up(&state->slot_available_event);
  6552. + }
  6553. +}
  6554. +
  6555. +/* Called by the slot handler and application threads */
  6556. +static VCHIQ_STATUS_T
  6557. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6558. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6559. + int count, int size, int flags)
  6560. +{
  6561. + VCHIQ_SHARED_STATE_T *local;
  6562. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  6563. + VCHIQ_HEADER_T *header;
  6564. + int type = VCHIQ_MSG_TYPE(msgid);
  6565. +
  6566. + unsigned int stride;
  6567. +
  6568. + local = state->local;
  6569. +
  6570. + stride = calc_stride(size);
  6571. +
  6572. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  6573. +
  6574. + if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
  6575. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  6576. + return VCHIQ_RETRY;
  6577. +
  6578. + if (type == VCHIQ_MSG_DATA) {
  6579. + int tx_end_index;
  6580. +
  6581. + BUG_ON(!service);
  6582. + BUG_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
  6583. + QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
  6584. +
  6585. + if (service->closing) {
  6586. + /* The service has been closed */
  6587. + mutex_unlock(&state->slot_mutex);
  6588. + return VCHIQ_ERROR;
  6589. + }
  6590. +
  6591. + service_quota = &state->service_quotas[service->localport];
  6592. +
  6593. + spin_lock(&quota_spinlock);
  6594. +
  6595. + /* Ensure this service doesn't use more than its quota of
  6596. + ** messages or slots */
  6597. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6598. + state->local_tx_pos + stride - 1);
  6599. +
  6600. + /* Ensure data messages don't use more than their quota of
  6601. + ** slots */
  6602. + while ((tx_end_index != state->previous_data_index) &&
  6603. + (state->data_use_count == state->data_quota)) {
  6604. + VCHIQ_STATS_INC(state, data_stalls);
  6605. + spin_unlock(&quota_spinlock);
  6606. + mutex_unlock(&state->slot_mutex);
  6607. +
  6608. + if (down_interruptible(&state->data_quota_event)
  6609. + != 0)
  6610. + return VCHIQ_RETRY;
  6611. +
  6612. + mutex_lock(&state->slot_mutex);
  6613. + spin_lock(&quota_spinlock);
  6614. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6615. + state->local_tx_pos + stride - 1);
  6616. + if ((tx_end_index == state->previous_data_index) ||
  6617. + (state->data_use_count < state->data_quota)) {
  6618. + /* Pass the signal on to other waiters */
  6619. + up(&state->data_quota_event);
  6620. + break;
  6621. + }
  6622. + }
  6623. +
  6624. + while ((service_quota->message_use_count ==
  6625. + service_quota->message_quota) ||
  6626. + ((tx_end_index != service_quota->previous_tx_index) &&
  6627. + (service_quota->slot_use_count ==
  6628. + service_quota->slot_quota))) {
  6629. + spin_unlock(&quota_spinlock);
  6630. + vchiq_log_trace(vchiq_core_log_level,
  6631. + "%d: qm:%d %s,%x - quota stall "
  6632. + "(msg %d, slot %d)",
  6633. + state->id, service->localport,
  6634. + msg_type_str(type), size,
  6635. + service_quota->message_use_count,
  6636. + service_quota->slot_use_count);
  6637. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  6638. + mutex_unlock(&state->slot_mutex);
  6639. + if (down_interruptible(&service_quota->quota_event)
  6640. + != 0)
  6641. + return VCHIQ_RETRY;
  6642. + if (service->closing)
  6643. + return VCHIQ_ERROR;
  6644. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  6645. + return VCHIQ_RETRY;
  6646. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  6647. + /* The service has been closed */
  6648. + mutex_unlock(&state->slot_mutex);
  6649. + return VCHIQ_ERROR;
  6650. + }
  6651. + spin_lock(&quota_spinlock);
  6652. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6653. + state->local_tx_pos + stride - 1);
  6654. + }
  6655. +
  6656. + spin_unlock(&quota_spinlock);
  6657. + }
  6658. +
  6659. + header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
  6660. +
  6661. + if (!header) {
  6662. + if (service)
  6663. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  6664. + /* In the event of a failure, return the mutex to the
  6665. + state it was in */
  6666. + if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
  6667. + mutex_unlock(&state->slot_mutex);
  6668. + return VCHIQ_RETRY;
  6669. + }
  6670. +
  6671. + if (type == VCHIQ_MSG_DATA) {
  6672. + int i, pos;
  6673. + int tx_end_index;
  6674. + int slot_use_count;
  6675. +
  6676. + vchiq_log_info(vchiq_core_log_level,
  6677. + "%d: qm %s@%x,%x (%d->%d)",
  6678. + state->id,
  6679. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6680. + (unsigned int)header, size,
  6681. + VCHIQ_MSG_SRCPORT(msgid),
  6682. + VCHIQ_MSG_DSTPORT(msgid));
  6683. +
  6684. + BUG_ON(!service);
  6685. + BUG_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
  6686. + QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
  6687. +
  6688. + for (i = 0, pos = 0; i < (unsigned int)count;
  6689. + pos += elements[i++].size)
  6690. + if (elements[i].size) {
  6691. + if (vchiq_copy_from_user
  6692. + (header->data + pos, elements[i].data,
  6693. + (size_t) elements[i].size) !=
  6694. + VCHIQ_SUCCESS) {
  6695. + mutex_unlock(&state->slot_mutex);
  6696. + VCHIQ_SERVICE_STATS_INC(service,
  6697. + error_count);
  6698. + return VCHIQ_ERROR;
  6699. + }
  6700. + if (i == 0) {
  6701. + if (SRVTRACE_ENABLED(service,
  6702. + VCHIQ_LOG_INFO))
  6703. + vchiq_log_dump_mem("Sent", 0,
  6704. + header->data + pos,
  6705. + min(64u,
  6706. + elements[0].size));
  6707. + }
  6708. + }
  6709. +
  6710. + spin_lock(&quota_spinlock);
  6711. + service_quota->message_use_count++;
  6712. +
  6713. + tx_end_index =
  6714. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  6715. +
  6716. + /* If this transmission can't fit in the last slot used by any
  6717. + ** service, the data_use_count must be increased. */
  6718. + if (tx_end_index != state->previous_data_index) {
  6719. + state->previous_data_index = tx_end_index;
  6720. + state->data_use_count++;
  6721. + }
  6722. +
  6723. + /* If this isn't the same slot last used by this service,
  6724. + ** the service's slot_use_count must be increased. */
  6725. + if (tx_end_index != service_quota->previous_tx_index) {
  6726. + service_quota->previous_tx_index = tx_end_index;
  6727. + slot_use_count = ++service_quota->slot_use_count;
  6728. + } else {
  6729. + slot_use_count = 0;
  6730. + }
  6731. +
  6732. + spin_unlock(&quota_spinlock);
  6733. +
  6734. + if (slot_use_count)
  6735. + vchiq_log_trace(vchiq_core_log_level,
  6736. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  6737. + state->id, service->localport,
  6738. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  6739. + slot_use_count, header);
  6740. +
  6741. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6742. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6743. + } else {
  6744. + vchiq_log_info(vchiq_core_log_level,
  6745. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  6746. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6747. + (unsigned int)header, size,
  6748. + VCHIQ_MSG_SRCPORT(msgid),
  6749. + VCHIQ_MSG_DSTPORT(msgid));
  6750. + if (size != 0) {
  6751. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6752. + memcpy(header->data, elements[0].data,
  6753. + elements[0].size);
  6754. + }
  6755. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6756. + }
  6757. +
  6758. + header->msgid = msgid;
  6759. + header->size = size;
  6760. +
  6761. + {
  6762. + int svc_fourcc;
  6763. +
  6764. + svc_fourcc = service
  6765. + ? service->base.fourcc
  6766. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6767. +
  6768. + vchiq_log_info(SRVTRACE_LEVEL(service),
  6769. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6770. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6771. + VCHIQ_MSG_TYPE(msgid),
  6772. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6773. + VCHIQ_MSG_SRCPORT(msgid),
  6774. + VCHIQ_MSG_DSTPORT(msgid),
  6775. + size);
  6776. + }
  6777. +
  6778. + /* Make sure the new header is visible to the peer. */
  6779. + wmb();
  6780. +
  6781. + /* Make the new tx_pos visible to the peer. */
  6782. + local->tx_pos = state->local_tx_pos;
  6783. + wmb();
  6784. +
  6785. + if (service && (type == VCHIQ_MSG_CLOSE))
  6786. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  6787. +
  6788. + if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
  6789. + mutex_unlock(&state->slot_mutex);
  6790. +
  6791. + remote_event_signal(&state->remote->trigger);
  6792. +
  6793. + return VCHIQ_SUCCESS;
  6794. +}
  6795. +
  6796. +/* Called by the slot handler and application threads */
  6797. +static VCHIQ_STATUS_T
  6798. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6799. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6800. + int count, int size, int is_blocking)
  6801. +{
  6802. + VCHIQ_SHARED_STATE_T *local;
  6803. + VCHIQ_HEADER_T *header;
  6804. +
  6805. + local = state->local;
  6806. +
  6807. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  6808. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  6809. + return VCHIQ_RETRY;
  6810. +
  6811. + remote_event_wait(&local->sync_release);
  6812. +
  6813. + rmb();
  6814. +
  6815. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  6816. + local->slot_sync);
  6817. +
  6818. + {
  6819. + int oldmsgid = header->msgid;
  6820. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  6821. + vchiq_log_error(vchiq_core_log_level,
  6822. + "%d: qms - msgid %x, not PADDING",
  6823. + state->id, oldmsgid);
  6824. + }
  6825. +
  6826. + if (service) {
  6827. + int i, pos;
  6828. +
  6829. + vchiq_log_info(vchiq_sync_log_level,
  6830. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6831. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6832. + (unsigned int)header, size,
  6833. + VCHIQ_MSG_SRCPORT(msgid),
  6834. + VCHIQ_MSG_DSTPORT(msgid));
  6835. +
  6836. + for (i = 0, pos = 0; i < (unsigned int)count;
  6837. + pos += elements[i++].size)
  6838. + if (elements[i].size) {
  6839. + if (vchiq_copy_from_user
  6840. + (header->data + pos, elements[i].data,
  6841. + (size_t) elements[i].size) !=
  6842. + VCHIQ_SUCCESS) {
  6843. + mutex_unlock(&state->sync_mutex);
  6844. + VCHIQ_SERVICE_STATS_INC(service,
  6845. + error_count);
  6846. + return VCHIQ_ERROR;
  6847. + }
  6848. + if (i == 0) {
  6849. + if (vchiq_sync_log_level >=
  6850. + VCHIQ_LOG_TRACE)
  6851. + vchiq_log_dump_mem("Sent Sync",
  6852. + 0, header->data + pos,
  6853. + min(64u,
  6854. + elements[0].size));
  6855. + }
  6856. + }
  6857. +
  6858. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6859. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6860. + } else {
  6861. + vchiq_log_info(vchiq_sync_log_level,
  6862. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6863. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6864. + (unsigned int)header, size,
  6865. + VCHIQ_MSG_SRCPORT(msgid),
  6866. + VCHIQ_MSG_DSTPORT(msgid));
  6867. + if (size != 0) {
  6868. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6869. + memcpy(header->data, elements[0].data,
  6870. + elements[0].size);
  6871. + }
  6872. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6873. + }
  6874. +
  6875. + header->size = size;
  6876. + header->msgid = msgid;
  6877. +
  6878. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  6879. + int svc_fourcc;
  6880. +
  6881. + svc_fourcc = service
  6882. + ? service->base.fourcc
  6883. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6884. +
  6885. + vchiq_log_trace(vchiq_sync_log_level,
  6886. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6887. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6888. + VCHIQ_MSG_TYPE(msgid),
  6889. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6890. + VCHIQ_MSG_SRCPORT(msgid),
  6891. + VCHIQ_MSG_DSTPORT(msgid),
  6892. + size);
  6893. + }
  6894. +
  6895. + /* Make sure the new header is visible to the peer. */
  6896. + wmb();
  6897. +
  6898. + remote_event_signal(&state->remote->sync_trigger);
  6899. +
  6900. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  6901. + mutex_unlock(&state->sync_mutex);
  6902. +
  6903. + return VCHIQ_SUCCESS;
  6904. +}
  6905. +
  6906. +static inline void
  6907. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  6908. +{
  6909. + slot->use_count++;
  6910. +}
  6911. +
  6912. +static void
  6913. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  6914. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  6915. +{
  6916. + int release_count;
  6917. +
  6918. + mutex_lock(&state->recycle_mutex);
  6919. +
  6920. + if (header) {
  6921. + int msgid = header->msgid;
  6922. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  6923. + (service && service->closing)) {
  6924. + mutex_unlock(&state->recycle_mutex);
  6925. + return;
  6926. + }
  6927. +
  6928. + /* Rewrite the message header to prevent a double
  6929. + ** release */
  6930. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  6931. + }
  6932. +
  6933. + release_count = slot_info->release_count;
  6934. + slot_info->release_count = ++release_count;
  6935. +
  6936. + if (release_count == slot_info->use_count) {
  6937. + int slot_queue_recycle;
  6938. + /* Add to the freed queue */
  6939. +
  6940. + /* A read barrier is necessary here to prevent speculative
  6941. + ** fetches of remote->slot_queue_recycle from overtaking the
  6942. + ** mutex. */
  6943. + rmb();
  6944. +
  6945. + slot_queue_recycle = state->remote->slot_queue_recycle;
  6946. + state->remote->slot_queue[slot_queue_recycle &
  6947. + VCHIQ_SLOT_QUEUE_MASK] =
  6948. + SLOT_INDEX_FROM_INFO(state, slot_info);
  6949. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  6950. + vchiq_log_info(vchiq_core_log_level,
  6951. + "%d: release_slot %d - recycle->%x",
  6952. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  6953. + state->remote->slot_queue_recycle);
  6954. +
  6955. + /* A write barrier is necessary, but remote_event_signal
  6956. + ** contains one. */
  6957. + remote_event_signal(&state->remote->recycle);
  6958. + }
  6959. +
  6960. + mutex_unlock(&state->recycle_mutex);
  6961. +}
  6962. +
  6963. +/* Called by the slot handler - don't hold the bulk mutex */
  6964. +static VCHIQ_STATUS_T
  6965. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  6966. + int retry_poll)
  6967. +{
  6968. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  6969. +
  6970. + vchiq_log_trace(vchiq_core_log_level,
  6971. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  6972. + service->state->id, service->localport,
  6973. + (queue == &service->bulk_tx) ? 't' : 'r',
  6974. + queue->process, queue->remote_notify, queue->remove);
  6975. +
  6976. + if (service->state->is_master) {
  6977. + while (queue->remote_notify != queue->process) {
  6978. + VCHIQ_BULK_T *bulk =
  6979. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  6980. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  6981. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  6982. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  6983. + service->remoteport);
  6984. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  6985. + /* Only reply to non-dummy bulk requests */
  6986. + if (bulk->remote_data) {
  6987. + status = queue_message(service->state, NULL,
  6988. + msgid, &element, 1, 4, 0);
  6989. + if (status != VCHIQ_SUCCESS)
  6990. + break;
  6991. + }
  6992. + queue->remote_notify++;
  6993. + }
  6994. + } else {
  6995. + queue->remote_notify = queue->process;
  6996. + }
  6997. +
  6998. + if (status == VCHIQ_SUCCESS) {
  6999. + while (queue->remove != queue->remote_notify) {
  7000. + VCHIQ_BULK_T *bulk =
  7001. + &queue->bulks[BULK_INDEX(queue->remove)];
  7002. +
  7003. + /* Only generate callbacks for non-dummy bulk
  7004. + ** requests, and non-terminated services */
  7005. + if (bulk->data && service->instance) {
  7006. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  7007. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  7008. + VCHIQ_SERVICE_STATS_INC(service,
  7009. + bulk_tx_count);
  7010. + VCHIQ_SERVICE_STATS_ADD(service,
  7011. + bulk_tx_bytes,
  7012. + bulk->actual);
  7013. + } else {
  7014. + VCHIQ_SERVICE_STATS_INC(service,
  7015. + bulk_rx_count);
  7016. + VCHIQ_SERVICE_STATS_ADD(service,
  7017. + bulk_rx_bytes,
  7018. + bulk->actual);
  7019. + }
  7020. + } else {
  7021. + VCHIQ_SERVICE_STATS_INC(service,
  7022. + bulk_aborted_count);
  7023. + }
  7024. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  7025. + struct bulk_waiter *waiter;
  7026. + spin_lock(&bulk_waiter_spinlock);
  7027. + waiter = bulk->userdata;
  7028. + if (waiter) {
  7029. + waiter->actual = bulk->actual;
  7030. + up(&waiter->event);
  7031. + }
  7032. + spin_unlock(&bulk_waiter_spinlock);
  7033. + } else if (bulk->mode ==
  7034. + VCHIQ_BULK_MODE_CALLBACK) {
  7035. + VCHIQ_REASON_T reason = (bulk->dir ==
  7036. + VCHIQ_BULK_TRANSMIT) ?
  7037. + ((bulk->actual ==
  7038. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  7039. + VCHIQ_BULK_TRANSMIT_ABORTED :
  7040. + VCHIQ_BULK_TRANSMIT_DONE) :
  7041. + ((bulk->actual ==
  7042. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  7043. + VCHIQ_BULK_RECEIVE_ABORTED :
  7044. + VCHIQ_BULK_RECEIVE_DONE);
  7045. + status = make_service_callback(service,
  7046. + reason, NULL, bulk->userdata);
  7047. + if (status == VCHIQ_RETRY)
  7048. + break;
  7049. + }
  7050. + }
  7051. +
  7052. + queue->remove++;
  7053. + up(&service->bulk_remove_event);
  7054. + }
  7055. + if (!retry_poll)
  7056. + status = VCHIQ_SUCCESS;
  7057. + }
  7058. +
  7059. + if (status == VCHIQ_RETRY)
  7060. + request_poll(service->state, service,
  7061. + (queue == &service->bulk_tx) ?
  7062. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  7063. +
  7064. + return status;
  7065. +}
  7066. +
  7067. +/* Called by the slot handler thread */
  7068. +static void
  7069. +poll_services(VCHIQ_STATE_T *state)
  7070. +{
  7071. + int group, i;
  7072. +
  7073. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  7074. + uint32_t flags;
  7075. + flags = atomic_xchg(&state->poll_services[group], 0);
  7076. + for (i = 0; flags; i++) {
  7077. + if (flags & (1 << i)) {
  7078. + VCHIQ_SERVICE_T *service =
  7079. + find_service_by_port(state,
  7080. + (group<<5) + i);
  7081. + uint32_t service_flags;
  7082. + flags &= ~(1 << i);
  7083. + if (!service)
  7084. + continue;
  7085. + service_flags =
  7086. + atomic_xchg(&service->poll_flags, 0);
  7087. + if (service_flags &
  7088. + (1 << VCHIQ_POLL_REMOVE)) {
  7089. + vchiq_log_info(vchiq_core_log_level,
  7090. + "%d: ps - remove %d<->%d",
  7091. + state->id, service->localport,
  7092. + service->remoteport);
  7093. +
  7094. + /* Make it look like a client, because
  7095. + it must be removed and not left in
  7096. + the LISTENING state. */
  7097. + service->public_fourcc =
  7098. + VCHIQ_FOURCC_INVALID;
  7099. +
  7100. + if (vchiq_close_service_internal(
  7101. + service, 0/*!close_recvd*/) !=
  7102. + VCHIQ_SUCCESS)
  7103. + request_poll(state, service,
  7104. + VCHIQ_POLL_REMOVE);
  7105. + } else if (service_flags &
  7106. + (1 << VCHIQ_POLL_TERMINATE)) {
  7107. + vchiq_log_info(vchiq_core_log_level,
  7108. + "%d: ps - terminate %d<->%d",
  7109. + state->id, service->localport,
  7110. + service->remoteport);
  7111. + if (vchiq_close_service_internal(
  7112. + service, 0/*!close_recvd*/) !=
  7113. + VCHIQ_SUCCESS)
  7114. + request_poll(state, service,
  7115. + VCHIQ_POLL_TERMINATE);
  7116. + }
  7117. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  7118. + notify_bulks(service,
  7119. + &service->bulk_tx,
  7120. + 1/*retry_poll*/);
  7121. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  7122. + notify_bulks(service,
  7123. + &service->bulk_rx,
  7124. + 1/*retry_poll*/);
  7125. + unlock_service(service);
  7126. + }
  7127. + }
  7128. + }
  7129. +}
  7130. +
  7131. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  7132. +static int
  7133. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  7134. +{
  7135. + VCHIQ_STATE_T *state = service->state;
  7136. + int resolved = 0;
  7137. + int rc;
  7138. +
  7139. + while ((queue->process != queue->local_insert) &&
  7140. + (queue->process != queue->remote_insert)) {
  7141. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  7142. +
  7143. + vchiq_log_trace(vchiq_core_log_level,
  7144. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  7145. + state->id, service->localport,
  7146. + (queue == &service->bulk_tx) ? 't' : 'r',
  7147. + queue->local_insert, queue->remote_insert,
  7148. + queue->process);
  7149. +
  7150. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  7151. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  7152. +
  7153. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  7154. + if (rc != 0)
  7155. + break;
  7156. +
  7157. + vchiq_transfer_bulk(bulk);
  7158. + mutex_unlock(&state->bulk_transfer_mutex);
  7159. +
  7160. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  7161. + const char *header = (queue == &service->bulk_tx) ?
  7162. + "Send Bulk to" : "Recv Bulk from";
  7163. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  7164. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7165. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  7166. + header,
  7167. + VCHIQ_FOURCC_AS_4CHARS(
  7168. + service->base.fourcc),
  7169. + service->remoteport,
  7170. + bulk->size,
  7171. + (unsigned int)bulk->data,
  7172. + (unsigned int)bulk->remote_data);
  7173. + else
  7174. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7175. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  7176. + " rx len:%d %x<->%x",
  7177. + header,
  7178. + VCHIQ_FOURCC_AS_4CHARS(
  7179. + service->base.fourcc),
  7180. + service->remoteport,
  7181. + bulk->size,
  7182. + bulk->remote_size,
  7183. + (unsigned int)bulk->data,
  7184. + (unsigned int)bulk->remote_data);
  7185. + }
  7186. +
  7187. + vchiq_complete_bulk(bulk);
  7188. + queue->process++;
  7189. + resolved++;
  7190. + }
  7191. + return resolved;
  7192. +}
  7193. +
  7194. +/* Called with the bulk_mutex held */
  7195. +static void
  7196. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  7197. +{
  7198. + int is_tx = (queue == &service->bulk_tx);
  7199. + vchiq_log_trace(vchiq_core_log_level,
  7200. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  7201. + service->state->id, service->localport, is_tx ? 't' : 'r',
  7202. + queue->local_insert, queue->remote_insert, queue->process);
  7203. +
  7204. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  7205. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  7206. +
  7207. + while ((queue->process != queue->local_insert) ||
  7208. + (queue->process != queue->remote_insert)) {
  7209. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  7210. +
  7211. + if (queue->process == queue->remote_insert) {
  7212. + /* fabricate a matching dummy bulk */
  7213. + bulk->remote_data = NULL;
  7214. + bulk->remote_size = 0;
  7215. + queue->remote_insert++;
  7216. + }
  7217. +
  7218. + if (queue->process != queue->local_insert) {
  7219. + vchiq_complete_bulk(bulk);
  7220. +
  7221. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7222. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  7223. + "rx len:%d",
  7224. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  7225. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7226. + service->remoteport,
  7227. + bulk->size,
  7228. + bulk->remote_size);
  7229. + } else {
  7230. + /* fabricate a matching dummy bulk */
  7231. + bulk->data = NULL;
  7232. + bulk->size = 0;
  7233. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  7234. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  7235. + VCHIQ_BULK_RECEIVE;
  7236. + queue->local_insert++;
  7237. + }
  7238. +
  7239. + queue->process++;
  7240. + }
  7241. +}
  7242. +
  7243. +/* Called from the slot handler thread */
  7244. +static void
  7245. +pause_bulks(VCHIQ_STATE_T *state)
  7246. +{
  7247. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  7248. + WARN_ON_ONCE(1);
  7249. + atomic_set(&pause_bulks_count, 1);
  7250. + return;
  7251. + }
  7252. +
  7253. + /* Block bulk transfers from all services */
  7254. + mutex_lock(&state->bulk_transfer_mutex);
  7255. +}
  7256. +
  7257. +/* Called from the slot handler thread */
  7258. +static void
  7259. +resume_bulks(VCHIQ_STATE_T *state)
  7260. +{
  7261. + int i;
  7262. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  7263. + WARN_ON_ONCE(1);
  7264. + atomic_set(&pause_bulks_count, 0);
  7265. + return;
  7266. + }
  7267. +
  7268. + /* Allow bulk transfers from all services */
  7269. + mutex_unlock(&state->bulk_transfer_mutex);
  7270. +
  7271. + if (state->deferred_bulks == 0)
  7272. + return;
  7273. +
  7274. + /* Deal with any bulks which had to be deferred due to being in
  7275. + * paused state. Don't try to match up to number of deferred bulks
  7276. + * in case we've had something come and close the service in the
  7277. + * interim - just process all bulk queues for all services */
  7278. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  7279. + __func__, state->deferred_bulks);
  7280. +
  7281. + for (i = 0; i < state->unused_service; i++) {
  7282. + VCHIQ_SERVICE_T *service = state->services[i];
  7283. + int resolved_rx = 0;
  7284. + int resolved_tx = 0;
  7285. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  7286. + continue;
  7287. +
  7288. + mutex_lock(&service->bulk_mutex);
  7289. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  7290. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  7291. + mutex_unlock(&service->bulk_mutex);
  7292. + if (resolved_rx)
  7293. + notify_bulks(service, &service->bulk_rx, 1);
  7294. + if (resolved_tx)
  7295. + notify_bulks(service, &service->bulk_tx, 1);
  7296. + }
  7297. + state->deferred_bulks = 0;
  7298. +}
  7299. +
  7300. +static int
  7301. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  7302. +{
  7303. + VCHIQ_SERVICE_T *service = NULL;
  7304. + int msgid, size;
  7305. + int type;
  7306. + unsigned int localport, remoteport;
  7307. +
  7308. + msgid = header->msgid;
  7309. + size = header->size;
  7310. + type = VCHIQ_MSG_TYPE(msgid);
  7311. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7312. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7313. + if (size >= sizeof(struct vchiq_open_payload)) {
  7314. + const struct vchiq_open_payload *payload =
  7315. + (struct vchiq_open_payload *)header->data;
  7316. + unsigned int fourcc;
  7317. +
  7318. + fourcc = payload->fourcc;
  7319. + vchiq_log_info(vchiq_core_log_level,
  7320. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  7321. + state->id, (unsigned int)header,
  7322. + localport,
  7323. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  7324. +
  7325. + service = get_listening_service(state, fourcc);
  7326. +
  7327. + if (service) {
  7328. + /* A matching service exists */
  7329. + short version = payload->version;
  7330. + short version_min = payload->version_min;
  7331. + if ((service->version < version_min) ||
  7332. + (version < service->version_min)) {
  7333. + /* Version mismatch */
  7334. + vchiq_loud_error_header();
  7335. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  7336. + "version mismatch - local (%d, min %d)"
  7337. + " vs. remote (%d, min %d)",
  7338. + state->id, service->localport,
  7339. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  7340. + service->version, service->version_min,
  7341. + version, version_min);
  7342. + vchiq_loud_error_footer();
  7343. + unlock_service(service);
  7344. + service = NULL;
  7345. + goto fail_open;
  7346. + }
  7347. + service->peer_version = version;
  7348. +
  7349. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  7350. + struct vchiq_openack_payload ack_payload = {
  7351. + service->version
  7352. + };
  7353. + VCHIQ_ELEMENT_T body = {
  7354. + &ack_payload,
  7355. + sizeof(ack_payload)
  7356. + };
  7357. +
  7358. + if (state->version_common <
  7359. + VCHIQ_VERSION_SYNCHRONOUS_MODE)
  7360. + service->sync = 0;
  7361. +
  7362. + /* Acknowledge the OPEN */
  7363. + if (service->sync &&
  7364. + (state->version_common >=
  7365. + VCHIQ_VERSION_SYNCHRONOUS_MODE)) {
  7366. + if (queue_message_sync(state, NULL,
  7367. + VCHIQ_MAKE_MSG(
  7368. + VCHIQ_MSG_OPENACK,
  7369. + service->localport,
  7370. + remoteport),
  7371. + &body, 1, sizeof(ack_payload),
  7372. + 0) == VCHIQ_RETRY)
  7373. + goto bail_not_ready;
  7374. + } else {
  7375. + if (queue_message(state, NULL,
  7376. + VCHIQ_MAKE_MSG(
  7377. + VCHIQ_MSG_OPENACK,
  7378. + service->localport,
  7379. + remoteport),
  7380. + &body, 1, sizeof(ack_payload),
  7381. + 0) == VCHIQ_RETRY)
  7382. + goto bail_not_ready;
  7383. + }
  7384. +
  7385. + /* The service is now open */
  7386. + vchiq_set_service_state(service,
  7387. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  7388. + : VCHIQ_SRVSTATE_OPEN);
  7389. + }
  7390. +
  7391. + service->remoteport = remoteport;
  7392. + service->client_id = ((int *)header->data)[1];
  7393. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  7394. + NULL, NULL) == VCHIQ_RETRY) {
  7395. + /* Bail out if not ready */
  7396. + service->remoteport = VCHIQ_PORT_FREE;
  7397. + goto bail_not_ready;
  7398. + }
  7399. +
  7400. + /* Success - the message has been dealt with */
  7401. + unlock_service(service);
  7402. + return 1;
  7403. + }
  7404. + }
  7405. +
  7406. +fail_open:
  7407. + /* No available service, or an invalid request - send a CLOSE */
  7408. + if (queue_message(state, NULL,
  7409. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  7410. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  7411. + goto bail_not_ready;
  7412. +
  7413. + return 1;
  7414. +
  7415. +bail_not_ready:
  7416. + if (service)
  7417. + unlock_service(service);
  7418. +
  7419. + return 0;
  7420. +}
  7421. +
  7422. +/* Called by the slot handler thread */
  7423. +static void
  7424. +parse_rx_slots(VCHIQ_STATE_T *state)
  7425. +{
  7426. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  7427. + VCHIQ_SERVICE_T *service = NULL;
  7428. + int tx_pos;
  7429. + DEBUG_INITIALISE(state->local)
  7430. +
  7431. + tx_pos = remote->tx_pos;
  7432. +
  7433. + while (state->rx_pos != tx_pos) {
  7434. + VCHIQ_HEADER_T *header;
  7435. + int msgid, size;
  7436. + int type;
  7437. + unsigned int localport, remoteport;
  7438. +
  7439. + DEBUG_TRACE(PARSE_LINE);
  7440. + if (!state->rx_data) {
  7441. + int rx_index;
  7442. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  7443. + rx_index = remote->slot_queue[
  7444. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  7445. + VCHIQ_SLOT_QUEUE_MASK];
  7446. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  7447. + rx_index);
  7448. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  7449. +
  7450. + /* Initialise use_count to one, and increment
  7451. + ** release_count at the end of the slot to avoid
  7452. + ** releasing the slot prematurely. */
  7453. + state->rx_info->use_count = 1;
  7454. + state->rx_info->release_count = 0;
  7455. + }
  7456. +
  7457. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  7458. + (state->rx_pos & VCHIQ_SLOT_MASK));
  7459. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  7460. + msgid = header->msgid;
  7461. + DEBUG_VALUE(PARSE_MSGID, msgid);
  7462. + size = header->size;
  7463. + type = VCHIQ_MSG_TYPE(msgid);
  7464. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7465. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7466. +
  7467. + if (type != VCHIQ_MSG_DATA)
  7468. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  7469. +
  7470. + switch (type) {
  7471. + case VCHIQ_MSG_OPENACK:
  7472. + case VCHIQ_MSG_CLOSE:
  7473. + case VCHIQ_MSG_DATA:
  7474. + case VCHIQ_MSG_BULK_RX:
  7475. + case VCHIQ_MSG_BULK_TX:
  7476. + case VCHIQ_MSG_BULK_RX_DONE:
  7477. + case VCHIQ_MSG_BULK_TX_DONE:
  7478. + service = find_service_by_port(state, localport);
  7479. + if ((!service ||
  7480. + ((service->remoteport != remoteport) &&
  7481. + (service->remoteport != VCHIQ_PORT_FREE))) &&
  7482. + (localport == 0) &&
  7483. + (type == VCHIQ_MSG_CLOSE)) {
  7484. + /* This could be a CLOSE from a client which
  7485. + hadn't yet received the OPENACK - look for
  7486. + the connected service */
  7487. + if (service)
  7488. + unlock_service(service);
  7489. + service = get_connected_service(state,
  7490. + remoteport);
  7491. + if (service)
  7492. + vchiq_log_warning(vchiq_core_log_level,
  7493. + "%d: prs %s@%x (%d->%d) - "
  7494. + "found connected service %d",
  7495. + state->id, msg_type_str(type),
  7496. + (unsigned int)header,
  7497. + remoteport, localport,
  7498. + service->localport);
  7499. + }
  7500. +
  7501. + if (!service) {
  7502. + vchiq_log_error(vchiq_core_log_level,
  7503. + "%d: prs %s@%x (%d->%d) - "
  7504. + "invalid/closed service %d",
  7505. + state->id, msg_type_str(type),
  7506. + (unsigned int)header,
  7507. + remoteport, localport, localport);
  7508. + goto skip_message;
  7509. + }
  7510. + break;
  7511. + default:
  7512. + break;
  7513. + }
  7514. +
  7515. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  7516. + int svc_fourcc;
  7517. +
  7518. + svc_fourcc = service
  7519. + ? service->base.fourcc
  7520. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7521. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7522. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  7523. + "len:%d",
  7524. + msg_type_str(type), type,
  7525. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7526. + remoteport, localport, size);
  7527. + if (size > 0)
  7528. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7529. + min(64, size));
  7530. + }
  7531. +
  7532. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  7533. + > VCHIQ_SLOT_SIZE) {
  7534. + vchiq_log_error(vchiq_core_log_level,
  7535. + "header %x (msgid %x) - size %x too big for "
  7536. + "slot",
  7537. + (unsigned int)header, (unsigned int)msgid,
  7538. + (unsigned int)size);
  7539. + WARN(1, "oversized for slot\n");
  7540. + }
  7541. +
  7542. + switch (type) {
  7543. + case VCHIQ_MSG_OPEN:
  7544. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  7545. + if (!parse_open(state, header))
  7546. + goto bail_not_ready;
  7547. + break;
  7548. + case VCHIQ_MSG_OPENACK:
  7549. + if (size >= sizeof(struct vchiq_openack_payload)) {
  7550. + const struct vchiq_openack_payload *payload =
  7551. + (struct vchiq_openack_payload *)
  7552. + header->data;
  7553. + service->peer_version = payload->version;
  7554. + }
  7555. + vchiq_log_info(vchiq_core_log_level,
  7556. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  7557. + state->id, (unsigned int)header, size,
  7558. + remoteport, localport, service->peer_version);
  7559. + if (service->srvstate ==
  7560. + VCHIQ_SRVSTATE_OPENING) {
  7561. + service->remoteport = remoteport;
  7562. + vchiq_set_service_state(service,
  7563. + VCHIQ_SRVSTATE_OPEN);
  7564. + up(&service->remove_event);
  7565. + } else
  7566. + vchiq_log_error(vchiq_core_log_level,
  7567. + "OPENACK received in state %s",
  7568. + srvstate_names[service->srvstate]);
  7569. + break;
  7570. + case VCHIQ_MSG_CLOSE:
  7571. + WARN_ON(size != 0); /* There should be no data */
  7572. +
  7573. + vchiq_log_info(vchiq_core_log_level,
  7574. + "%d: prs CLOSE@%x (%d->%d)",
  7575. + state->id, (unsigned int)header,
  7576. + remoteport, localport);
  7577. +
  7578. + mark_service_closing_internal(service, 1);
  7579. +
  7580. + if (vchiq_close_service_internal(service,
  7581. + 1/*close_recvd*/) == VCHIQ_RETRY)
  7582. + goto bail_not_ready;
  7583. +
  7584. + vchiq_log_info(vchiq_core_log_level,
  7585. + "Close Service %c%c%c%c s:%u d:%d",
  7586. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7587. + service->localport,
  7588. + service->remoteport);
  7589. + break;
  7590. + case VCHIQ_MSG_DATA:
  7591. + vchiq_log_info(vchiq_core_log_level,
  7592. + "%d: prs DATA@%x,%x (%d->%d)",
  7593. + state->id, (unsigned int)header, size,
  7594. + remoteport, localport);
  7595. +
  7596. + if ((service->remoteport == remoteport)
  7597. + && (service->srvstate ==
  7598. + VCHIQ_SRVSTATE_OPEN)) {
  7599. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  7600. + claim_slot(state->rx_info);
  7601. + DEBUG_TRACE(PARSE_LINE);
  7602. + if (make_service_callback(service,
  7603. + VCHIQ_MESSAGE_AVAILABLE, header,
  7604. + NULL) == VCHIQ_RETRY) {
  7605. + DEBUG_TRACE(PARSE_LINE);
  7606. + goto bail_not_ready;
  7607. + }
  7608. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  7609. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  7610. + size);
  7611. + } else {
  7612. + VCHIQ_STATS_INC(state, error_count);
  7613. + }
  7614. + break;
  7615. + case VCHIQ_MSG_CONNECT:
  7616. + vchiq_log_info(vchiq_core_log_level,
  7617. + "%d: prs CONNECT@%x",
  7618. + state->id, (unsigned int)header);
  7619. + state->version_common = ((VCHIQ_SLOT_ZERO_T *)
  7620. + state->slot_data)->version;
  7621. + up(&state->connect);
  7622. + break;
  7623. + case VCHIQ_MSG_BULK_RX:
  7624. + case VCHIQ_MSG_BULK_TX: {
  7625. + VCHIQ_BULK_QUEUE_T *queue;
  7626. + WARN_ON(!state->is_master);
  7627. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  7628. + &service->bulk_tx : &service->bulk_rx;
  7629. + if ((service->remoteport == remoteport)
  7630. + && (service->srvstate ==
  7631. + VCHIQ_SRVSTATE_OPEN)) {
  7632. + VCHIQ_BULK_T *bulk;
  7633. + int resolved = 0;
  7634. +
  7635. + DEBUG_TRACE(PARSE_LINE);
  7636. + if (mutex_lock_interruptible(
  7637. + &service->bulk_mutex) != 0) {
  7638. + DEBUG_TRACE(PARSE_LINE);
  7639. + goto bail_not_ready;
  7640. + }
  7641. +
  7642. + WARN_ON(!(queue->remote_insert < queue->remove +
  7643. + VCHIQ_NUM_SERVICE_BULKS));
  7644. + bulk = &queue->bulks[
  7645. + BULK_INDEX(queue->remote_insert)];
  7646. + bulk->remote_data =
  7647. + (void *)((int *)header->data)[0];
  7648. + bulk->remote_size = ((int *)header->data)[1];
  7649. + wmb();
  7650. +
  7651. + vchiq_log_info(vchiq_core_log_level,
  7652. + "%d: prs %s@%x (%d->%d) %x@%x",
  7653. + state->id, msg_type_str(type),
  7654. + (unsigned int)header,
  7655. + remoteport, localport,
  7656. + bulk->remote_size,
  7657. + (unsigned int)bulk->remote_data);
  7658. +
  7659. + queue->remote_insert++;
  7660. +
  7661. + if (atomic_read(&pause_bulks_count)) {
  7662. + state->deferred_bulks++;
  7663. + vchiq_log_info(vchiq_core_log_level,
  7664. + "%s: deferring bulk (%d)",
  7665. + __func__,
  7666. + state->deferred_bulks);
  7667. + if (state->conn_state !=
  7668. + VCHIQ_CONNSTATE_PAUSE_SENT)
  7669. + vchiq_log_error(
  7670. + vchiq_core_log_level,
  7671. + "%s: bulks paused in "
  7672. + "unexpected state %s",
  7673. + __func__,
  7674. + conn_state_names[
  7675. + state->conn_state]);
  7676. + } else if (state->conn_state ==
  7677. + VCHIQ_CONNSTATE_CONNECTED) {
  7678. + DEBUG_TRACE(PARSE_LINE);
  7679. + resolved = resolve_bulks(service,
  7680. + queue);
  7681. + }
  7682. +
  7683. + mutex_unlock(&service->bulk_mutex);
  7684. + if (resolved)
  7685. + notify_bulks(service, queue,
  7686. + 1/*retry_poll*/);
  7687. + }
  7688. + } break;
  7689. + case VCHIQ_MSG_BULK_RX_DONE:
  7690. + case VCHIQ_MSG_BULK_TX_DONE:
  7691. + WARN_ON(state->is_master);
  7692. + if ((service->remoteport == remoteport)
  7693. + && (service->srvstate !=
  7694. + VCHIQ_SRVSTATE_FREE)) {
  7695. + VCHIQ_BULK_QUEUE_T *queue;
  7696. + VCHIQ_BULK_T *bulk;
  7697. +
  7698. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7699. + &service->bulk_rx : &service->bulk_tx;
  7700. +
  7701. + DEBUG_TRACE(PARSE_LINE);
  7702. + if (mutex_lock_interruptible(
  7703. + &service->bulk_mutex) != 0) {
  7704. + DEBUG_TRACE(PARSE_LINE);
  7705. + goto bail_not_ready;
  7706. + }
  7707. + if ((int)(queue->remote_insert -
  7708. + queue->local_insert) >= 0) {
  7709. + vchiq_log_error(vchiq_core_log_level,
  7710. + "%d: prs %s@%x (%d->%d) "
  7711. + "unexpected (ri=%d,li=%d)",
  7712. + state->id, msg_type_str(type),
  7713. + (unsigned int)header,
  7714. + remoteport, localport,
  7715. + queue->remote_insert,
  7716. + queue->local_insert);
  7717. + mutex_unlock(&service->bulk_mutex);
  7718. + break;
  7719. + }
  7720. +
  7721. + BUG_ON(queue->process == queue->local_insert);
  7722. + BUG_ON(queue->process != queue->remote_insert);
  7723. +
  7724. + bulk = &queue->bulks[
  7725. + BULK_INDEX(queue->remote_insert)];
  7726. + bulk->actual = *(int *)header->data;
  7727. + queue->remote_insert++;
  7728. +
  7729. + vchiq_log_info(vchiq_core_log_level,
  7730. + "%d: prs %s@%x (%d->%d) %x@%x",
  7731. + state->id, msg_type_str(type),
  7732. + (unsigned int)header,
  7733. + remoteport, localport,
  7734. + bulk->actual, (unsigned int)bulk->data);
  7735. +
  7736. + vchiq_log_trace(vchiq_core_log_level,
  7737. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  7738. + state->id, localport,
  7739. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7740. + 'r' : 't',
  7741. + queue->local_insert,
  7742. + queue->remote_insert, queue->process);
  7743. +
  7744. + DEBUG_TRACE(PARSE_LINE);
  7745. + WARN_ON(queue->process == queue->local_insert);
  7746. + vchiq_complete_bulk(bulk);
  7747. + queue->process++;
  7748. + mutex_unlock(&service->bulk_mutex);
  7749. + DEBUG_TRACE(PARSE_LINE);
  7750. + notify_bulks(service, queue, 1/*retry_poll*/);
  7751. + DEBUG_TRACE(PARSE_LINE);
  7752. + }
  7753. + break;
  7754. + case VCHIQ_MSG_PADDING:
  7755. + vchiq_log_trace(vchiq_core_log_level,
  7756. + "%d: prs PADDING@%x,%x",
  7757. + state->id, (unsigned int)header, size);
  7758. + break;
  7759. + case VCHIQ_MSG_PAUSE:
  7760. + /* If initiated, signal the application thread */
  7761. + vchiq_log_trace(vchiq_core_log_level,
  7762. + "%d: prs PAUSE@%x,%x",
  7763. + state->id, (unsigned int)header, size);
  7764. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  7765. + vchiq_log_error(vchiq_core_log_level,
  7766. + "%d: PAUSE received in state PAUSED",
  7767. + state->id);
  7768. + break;
  7769. + }
  7770. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  7771. + /* Send a PAUSE in response */
  7772. + if (queue_message(state, NULL,
  7773. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7774. + NULL, 0, 0, QMFLAGS_NO_MUTEX_UNLOCK)
  7775. + == VCHIQ_RETRY)
  7776. + goto bail_not_ready;
  7777. + if (state->is_master)
  7778. + pause_bulks(state);
  7779. + }
  7780. + /* At this point slot_mutex is held */
  7781. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  7782. + vchiq_platform_paused(state);
  7783. + break;
  7784. + case VCHIQ_MSG_RESUME:
  7785. + vchiq_log_trace(vchiq_core_log_level,
  7786. + "%d: prs RESUME@%x,%x",
  7787. + state->id, (unsigned int)header, size);
  7788. + /* Release the slot mutex */
  7789. + mutex_unlock(&state->slot_mutex);
  7790. + if (state->is_master)
  7791. + resume_bulks(state);
  7792. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  7793. + vchiq_platform_resumed(state);
  7794. + break;
  7795. +
  7796. + case VCHIQ_MSG_REMOTE_USE:
  7797. + vchiq_on_remote_use(state);
  7798. + break;
  7799. + case VCHIQ_MSG_REMOTE_RELEASE:
  7800. + vchiq_on_remote_release(state);
  7801. + break;
  7802. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  7803. + vchiq_on_remote_use_active(state);
  7804. + break;
  7805. +
  7806. + default:
  7807. + vchiq_log_error(vchiq_core_log_level,
  7808. + "%d: prs invalid msgid %x@%x,%x",
  7809. + state->id, msgid, (unsigned int)header, size);
  7810. + WARN(1, "invalid message\n");
  7811. + break;
  7812. + }
  7813. +
  7814. +skip_message:
  7815. + if (service) {
  7816. + unlock_service(service);
  7817. + service = NULL;
  7818. + }
  7819. +
  7820. + state->rx_pos += calc_stride(size);
  7821. +
  7822. + DEBUG_TRACE(PARSE_LINE);
  7823. + /* Perform some housekeeping when the end of the slot is
  7824. + ** reached. */
  7825. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  7826. + /* Remove the extra reference count. */
  7827. + release_slot(state, state->rx_info, NULL, NULL);
  7828. + state->rx_data = NULL;
  7829. + }
  7830. + }
  7831. +
  7832. +bail_not_ready:
  7833. + if (service)
  7834. + unlock_service(service);
  7835. +}
  7836. +
  7837. +/* Called by the slot handler thread */
  7838. +static int
  7839. +slot_handler_func(void *v)
  7840. +{
  7841. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7842. + VCHIQ_SHARED_STATE_T *local = state->local;
  7843. + DEBUG_INITIALISE(local)
  7844. +
  7845. + while (1) {
  7846. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  7847. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7848. + remote_event_wait(&local->trigger);
  7849. +
  7850. + rmb();
  7851. +
  7852. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7853. + if (state->poll_needed) {
  7854. + /* Check if we need to suspend - may change our
  7855. + * conn_state */
  7856. + vchiq_platform_check_suspend(state);
  7857. +
  7858. + state->poll_needed = 0;
  7859. +
  7860. + /* Handle service polling and other rare conditions here
  7861. + ** out of the mainline code */
  7862. + switch (state->conn_state) {
  7863. + case VCHIQ_CONNSTATE_CONNECTED:
  7864. + /* Poll the services as requested */
  7865. + poll_services(state);
  7866. + break;
  7867. +
  7868. + case VCHIQ_CONNSTATE_PAUSING:
  7869. + if (state->is_master)
  7870. + pause_bulks(state);
  7871. + if (queue_message(state, NULL,
  7872. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7873. + NULL, 0, 0,
  7874. + QMFLAGS_NO_MUTEX_UNLOCK)
  7875. + != VCHIQ_RETRY) {
  7876. + vchiq_set_conn_state(state,
  7877. + VCHIQ_CONNSTATE_PAUSE_SENT);
  7878. + } else {
  7879. + if (state->is_master)
  7880. + resume_bulks(state);
  7881. + /* Retry later */
  7882. + state->poll_needed = 1;
  7883. + }
  7884. + break;
  7885. +
  7886. + case VCHIQ_CONNSTATE_PAUSED:
  7887. + vchiq_platform_resume(state);
  7888. + break;
  7889. +
  7890. + case VCHIQ_CONNSTATE_RESUMING:
  7891. + if (queue_message(state, NULL,
  7892. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  7893. + NULL, 0, 0, QMFLAGS_NO_MUTEX_LOCK)
  7894. + != VCHIQ_RETRY) {
  7895. + if (state->is_master)
  7896. + resume_bulks(state);
  7897. + vchiq_set_conn_state(state,
  7898. + VCHIQ_CONNSTATE_CONNECTED);
  7899. + vchiq_platform_resumed(state);
  7900. + } else {
  7901. + /* This should really be impossible,
  7902. + ** since the PAUSE should have flushed
  7903. + ** through outstanding messages. */
  7904. + vchiq_log_error(vchiq_core_log_level,
  7905. + "Failed to send RESUME "
  7906. + "message");
  7907. + BUG();
  7908. + }
  7909. + break;
  7910. +
  7911. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  7912. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  7913. + vchiq_platform_handle_timeout(state);
  7914. + break;
  7915. + default:
  7916. + break;
  7917. + }
  7918. +
  7919. +
  7920. + }
  7921. +
  7922. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7923. + parse_rx_slots(state);
  7924. + }
  7925. + return 0;
  7926. +}
  7927. +
  7928. +
  7929. +/* Called by the recycle thread */
  7930. +static int
  7931. +recycle_func(void *v)
  7932. +{
  7933. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7934. + VCHIQ_SHARED_STATE_T *local = state->local;
  7935. +
  7936. + while (1) {
  7937. + remote_event_wait(&local->recycle);
  7938. +
  7939. + process_free_queue(state);
  7940. + }
  7941. + return 0;
  7942. +}
  7943. +
  7944. +
  7945. +/* Called by the sync thread */
  7946. +static int
  7947. +sync_func(void *v)
  7948. +{
  7949. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7950. + VCHIQ_SHARED_STATE_T *local = state->local;
  7951. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  7952. + state->remote->slot_sync);
  7953. +
  7954. + while (1) {
  7955. + VCHIQ_SERVICE_T *service;
  7956. + int msgid, size;
  7957. + int type;
  7958. + unsigned int localport, remoteport;
  7959. +
  7960. + remote_event_wait(&local->sync_trigger);
  7961. +
  7962. + rmb();
  7963. +
  7964. + msgid = header->msgid;
  7965. + size = header->size;
  7966. + type = VCHIQ_MSG_TYPE(msgid);
  7967. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7968. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7969. +
  7970. + service = find_service_by_port(state, localport);
  7971. +
  7972. + if (!service) {
  7973. + vchiq_log_error(vchiq_sync_log_level,
  7974. + "%d: sf %s@%x (%d->%d) - "
  7975. + "invalid/closed service %d",
  7976. + state->id, msg_type_str(type),
  7977. + (unsigned int)header,
  7978. + remoteport, localport, localport);
  7979. + release_message_sync(state, header);
  7980. + continue;
  7981. + }
  7982. +
  7983. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  7984. + int svc_fourcc;
  7985. +
  7986. + svc_fourcc = service
  7987. + ? service->base.fourcc
  7988. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7989. + vchiq_log_trace(vchiq_sync_log_level,
  7990. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  7991. + msg_type_str(type),
  7992. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7993. + remoteport, localport, size);
  7994. + if (size > 0)
  7995. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7996. + min(64, size));
  7997. + }
  7998. +
  7999. + switch (type) {
  8000. + case VCHIQ_MSG_OPENACK:
  8001. + if (size >= sizeof(struct vchiq_openack_payload)) {
  8002. + const struct vchiq_openack_payload *payload =
  8003. + (struct vchiq_openack_payload *)
  8004. + header->data;
  8005. + service->peer_version = payload->version;
  8006. + }
  8007. + vchiq_log_info(vchiq_sync_log_level,
  8008. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  8009. + state->id, (unsigned int)header, size,
  8010. + remoteport, localport, service->peer_version);
  8011. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  8012. + service->remoteport = remoteport;
  8013. + vchiq_set_service_state(service,
  8014. + VCHIQ_SRVSTATE_OPENSYNC);
  8015. + service->sync = 1;
  8016. + up(&service->remove_event);
  8017. + }
  8018. + release_message_sync(state, header);
  8019. + break;
  8020. +
  8021. + case VCHIQ_MSG_DATA:
  8022. + vchiq_log_trace(vchiq_sync_log_level,
  8023. + "%d: sf DATA@%x,%x (%d->%d)",
  8024. + state->id, (unsigned int)header, size,
  8025. + remoteport, localport);
  8026. +
  8027. + if ((service->remoteport == remoteport) &&
  8028. + (service->srvstate ==
  8029. + VCHIQ_SRVSTATE_OPENSYNC)) {
  8030. + if (make_service_callback(service,
  8031. + VCHIQ_MESSAGE_AVAILABLE, header,
  8032. + NULL) == VCHIQ_RETRY)
  8033. + vchiq_log_error(vchiq_sync_log_level,
  8034. + "synchronous callback to "
  8035. + "service %d returns "
  8036. + "VCHIQ_RETRY",
  8037. + localport);
  8038. + }
  8039. + break;
  8040. +
  8041. + default:
  8042. + vchiq_log_error(vchiq_sync_log_level,
  8043. + "%d: sf unexpected msgid %x@%x,%x",
  8044. + state->id, msgid, (unsigned int)header, size);
  8045. + release_message_sync(state, header);
  8046. + break;
  8047. + }
  8048. +
  8049. + unlock_service(service);
  8050. + }
  8051. +
  8052. + return 0;
  8053. +}
  8054. +
  8055. +
  8056. +static void
  8057. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  8058. +{
  8059. + queue->local_insert = 0;
  8060. + queue->remote_insert = 0;
  8061. + queue->process = 0;
  8062. + queue->remote_notify = 0;
  8063. + queue->remove = 0;
  8064. +}
  8065. +
  8066. +
  8067. +inline const char *
  8068. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  8069. +{
  8070. + return conn_state_names[conn_state];
  8071. +}
  8072. +
  8073. +
  8074. +VCHIQ_SLOT_ZERO_T *
  8075. +vchiq_init_slots(void *mem_base, int mem_size)
  8076. +{
  8077. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  8078. + VCHIQ_SLOT_ZERO_T *slot_zero =
  8079. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  8080. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  8081. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  8082. +
  8083. + /* Ensure there is enough memory to run an absolutely minimum system */
  8084. + num_slots -= first_data_slot;
  8085. +
  8086. + if (num_slots < 4) {
  8087. + vchiq_log_error(vchiq_core_log_level,
  8088. + "vchiq_init_slots - insufficient memory %x bytes",
  8089. + mem_size);
  8090. + return NULL;
  8091. + }
  8092. +
  8093. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  8094. +
  8095. + slot_zero->magic = VCHIQ_MAGIC;
  8096. + slot_zero->version = VCHIQ_VERSION;
  8097. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  8098. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  8099. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  8100. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  8101. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  8102. +
  8103. + slot_zero->master.slot_sync = first_data_slot;
  8104. + slot_zero->master.slot_first = first_data_slot + 1;
  8105. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  8106. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  8107. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  8108. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  8109. +
  8110. + return slot_zero;
  8111. +}
  8112. +
  8113. +VCHIQ_STATUS_T
  8114. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  8115. + int is_master)
  8116. +{
  8117. + VCHIQ_SHARED_STATE_T *local;
  8118. + VCHIQ_SHARED_STATE_T *remote;
  8119. + VCHIQ_STATUS_T status;
  8120. + char threadname[10];
  8121. + static int id;
  8122. + int i;
  8123. +
  8124. + vchiq_log_warning(vchiq_core_log_level,
  8125. + "%s: slot_zero = 0x%08lx, is_master = %d",
  8126. + __func__, (unsigned long)slot_zero, is_master);
  8127. +
  8128. + /* Check the input configuration */
  8129. +
  8130. + if (slot_zero->magic != VCHIQ_MAGIC) {
  8131. + vchiq_loud_error_header();
  8132. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  8133. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  8134. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  8135. + vchiq_loud_error_footer();
  8136. + return VCHIQ_ERROR;
  8137. + }
  8138. +
  8139. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  8140. + vchiq_loud_error_header();
  8141. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  8142. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  8143. + "(minimum %d)",
  8144. + (unsigned int)slot_zero, slot_zero->version,
  8145. + VCHIQ_VERSION_MIN);
  8146. + vchiq_loud_error("Restart with a newer VideoCore image.");
  8147. + vchiq_loud_error_footer();
  8148. + return VCHIQ_ERROR;
  8149. + }
  8150. +
  8151. + if (VCHIQ_VERSION < slot_zero->version_min) {
  8152. + vchiq_loud_error_header();
  8153. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  8154. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  8155. + "minimum %d)",
  8156. + (unsigned int)slot_zero, VCHIQ_VERSION,
  8157. + slot_zero->version_min);
  8158. + vchiq_loud_error("Restart with a newer kernel.");
  8159. + vchiq_loud_error_footer();
  8160. + return VCHIQ_ERROR;
  8161. + }
  8162. +
  8163. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  8164. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  8165. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  8166. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  8167. + vchiq_loud_error_header();
  8168. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  8169. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  8170. + "(expected %x)",
  8171. + (unsigned int)slot_zero,
  8172. + slot_zero->slot_zero_size,
  8173. + sizeof(VCHIQ_SLOT_ZERO_T));
  8174. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  8175. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  8176. + "(expected %d",
  8177. + (unsigned int)slot_zero, slot_zero->slot_size,
  8178. + VCHIQ_SLOT_SIZE);
  8179. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  8180. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  8181. + "(expected %d)",
  8182. + (unsigned int)slot_zero, slot_zero->max_slots,
  8183. + VCHIQ_MAX_SLOTS);
  8184. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  8185. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  8186. + "(expected %d)",
  8187. + (unsigned int)slot_zero,
  8188. + slot_zero->max_slots_per_side,
  8189. + VCHIQ_MAX_SLOTS_PER_SIDE);
  8190. + vchiq_loud_error_footer();
  8191. + return VCHIQ_ERROR;
  8192. + }
  8193. +
  8194. + if (VCHIQ_VERSION < slot_zero->version)
  8195. + slot_zero->version = VCHIQ_VERSION;
  8196. +
  8197. + if (is_master) {
  8198. + local = &slot_zero->master;
  8199. + remote = &slot_zero->slave;
  8200. + } else {
  8201. + local = &slot_zero->slave;
  8202. + remote = &slot_zero->master;
  8203. + }
  8204. +
  8205. + if (local->initialised) {
  8206. + vchiq_loud_error_header();
  8207. + if (remote->initialised)
  8208. + vchiq_loud_error("local state has already been "
  8209. + "initialised");
  8210. + else
  8211. + vchiq_loud_error("master/slave mismatch - two %ss",
  8212. + is_master ? "master" : "slave");
  8213. + vchiq_loud_error_footer();
  8214. + return VCHIQ_ERROR;
  8215. + }
  8216. +
  8217. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  8218. +
  8219. + state->id = id++;
  8220. + state->is_master = is_master;
  8221. +
  8222. + /*
  8223. + initialize shared state pointers
  8224. + */
  8225. +
  8226. + state->local = local;
  8227. + state->remote = remote;
  8228. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  8229. +
  8230. + /*
  8231. + initialize events and mutexes
  8232. + */
  8233. +
  8234. + sema_init(&state->connect, 0);
  8235. + mutex_init(&state->mutex);
  8236. + sema_init(&state->trigger_event, 0);
  8237. + sema_init(&state->recycle_event, 0);
  8238. + sema_init(&state->sync_trigger_event, 0);
  8239. + sema_init(&state->sync_release_event, 0);
  8240. +
  8241. + mutex_init(&state->slot_mutex);
  8242. + mutex_init(&state->recycle_mutex);
  8243. + mutex_init(&state->sync_mutex);
  8244. + mutex_init(&state->bulk_transfer_mutex);
  8245. +
  8246. + sema_init(&state->slot_available_event, 0);
  8247. + sema_init(&state->slot_remove_event, 0);
  8248. + sema_init(&state->data_quota_event, 0);
  8249. +
  8250. + state->slot_queue_available = 0;
  8251. +
  8252. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  8253. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8254. + &state->service_quotas[i];
  8255. + sema_init(&service_quota->quota_event, 0);
  8256. + }
  8257. +
  8258. + for (i = local->slot_first; i <= local->slot_last; i++) {
  8259. + local->slot_queue[state->slot_queue_available++] = i;
  8260. + up(&state->slot_available_event);
  8261. + }
  8262. +
  8263. + state->default_slot_quota = state->slot_queue_available/2;
  8264. + state->default_message_quota =
  8265. + min((unsigned short)(state->default_slot_quota * 256),
  8266. + (unsigned short)~0);
  8267. +
  8268. + state->previous_data_index = -1;
  8269. + state->data_use_count = 0;
  8270. + state->data_quota = state->slot_queue_available - 1;
  8271. +
  8272. + local->trigger.event = &state->trigger_event;
  8273. + remote_event_create(&local->trigger);
  8274. + local->tx_pos = 0;
  8275. +
  8276. + local->recycle.event = &state->recycle_event;
  8277. + remote_event_create(&local->recycle);
  8278. + local->slot_queue_recycle = state->slot_queue_available;
  8279. +
  8280. + local->sync_trigger.event = &state->sync_trigger_event;
  8281. + remote_event_create(&local->sync_trigger);
  8282. +
  8283. + local->sync_release.event = &state->sync_release_event;
  8284. + remote_event_create(&local->sync_release);
  8285. +
  8286. + /* At start-of-day, the slot is empty and available */
  8287. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  8288. + = VCHIQ_MSGID_PADDING;
  8289. + remote_event_signal_local(&local->sync_release);
  8290. +
  8291. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  8292. +
  8293. + status = vchiq_platform_init_state(state);
  8294. +
  8295. + /*
  8296. + bring up slot handler thread
  8297. + */
  8298. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  8299. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  8300. + (void *)state,
  8301. + threadname);
  8302. +
  8303. + if (state->slot_handler_thread == NULL) {
  8304. + vchiq_loud_error_header();
  8305. + vchiq_loud_error("couldn't create thread %s", threadname);
  8306. + vchiq_loud_error_footer();
  8307. + return VCHIQ_ERROR;
  8308. + }
  8309. + set_user_nice(state->slot_handler_thread, -19);
  8310. + wake_up_process(state->slot_handler_thread);
  8311. +
  8312. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  8313. + state->recycle_thread = kthread_create(&recycle_func,
  8314. + (void *)state,
  8315. + threadname);
  8316. + if (state->recycle_thread == NULL) {
  8317. + vchiq_loud_error_header();
  8318. + vchiq_loud_error("couldn't create thread %s", threadname);
  8319. + vchiq_loud_error_footer();
  8320. + return VCHIQ_ERROR;
  8321. + }
  8322. + set_user_nice(state->recycle_thread, -19);
  8323. + wake_up_process(state->recycle_thread);
  8324. +
  8325. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  8326. + state->sync_thread = kthread_create(&sync_func,
  8327. + (void *)state,
  8328. + threadname);
  8329. + if (state->sync_thread == NULL) {
  8330. + vchiq_loud_error_header();
  8331. + vchiq_loud_error("couldn't create thread %s", threadname);
  8332. + vchiq_loud_error_footer();
  8333. + return VCHIQ_ERROR;
  8334. + }
  8335. + set_user_nice(state->sync_thread, -20);
  8336. + wake_up_process(state->sync_thread);
  8337. +
  8338. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  8339. + vchiq_states[state->id] = state;
  8340. +
  8341. + /* Indicate readiness to the other side */
  8342. + local->initialised = 1;
  8343. +
  8344. + return status;
  8345. +}
  8346. +
  8347. +/* Called from application thread when a client or server service is created. */
  8348. +VCHIQ_SERVICE_T *
  8349. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  8350. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  8351. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  8352. +{
  8353. + VCHIQ_SERVICE_T *service;
  8354. +
  8355. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  8356. + if (service) {
  8357. + service->base.fourcc = params->fourcc;
  8358. + service->base.callback = params->callback;
  8359. + service->base.userdata = params->userdata;
  8360. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  8361. + service->ref_count = 1;
  8362. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  8363. + service->userdata_term = userdata_term;
  8364. + service->localport = VCHIQ_PORT_FREE;
  8365. + service->remoteport = VCHIQ_PORT_FREE;
  8366. +
  8367. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  8368. + VCHIQ_FOURCC_INVALID : params->fourcc;
  8369. + service->client_id = 0;
  8370. + service->auto_close = 1;
  8371. + service->sync = 0;
  8372. + service->closing = 0;
  8373. + service->trace = 0;
  8374. + atomic_set(&service->poll_flags, 0);
  8375. + service->version = params->version;
  8376. + service->version_min = params->version_min;
  8377. + service->state = state;
  8378. + service->instance = instance;
  8379. + service->service_use_count = 0;
  8380. + init_bulk_queue(&service->bulk_tx);
  8381. + init_bulk_queue(&service->bulk_rx);
  8382. + sema_init(&service->remove_event, 0);
  8383. + sema_init(&service->bulk_remove_event, 0);
  8384. + mutex_init(&service->bulk_mutex);
  8385. + memset(&service->stats, 0, sizeof(service->stats));
  8386. + } else {
  8387. + vchiq_log_error(vchiq_core_log_level,
  8388. + "Out of memory");
  8389. + }
  8390. +
  8391. + if (service) {
  8392. + VCHIQ_SERVICE_T **pservice = NULL;
  8393. + int i;
  8394. +
  8395. + /* Although it is perfectly possible to use service_spinlock
  8396. + ** to protect the creation of services, it is overkill as it
  8397. + ** disables interrupts while the array is searched.
  8398. + ** The only danger is of another thread trying to create a
  8399. + ** service - service deletion is safe.
  8400. + ** Therefore it is preferable to use state->mutex which,
  8401. + ** although slower to claim, doesn't block interrupts while
  8402. + ** it is held.
  8403. + */
  8404. +
  8405. + mutex_lock(&state->mutex);
  8406. +
  8407. + /* Prepare to use a previously unused service */
  8408. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  8409. + pservice = &state->services[state->unused_service];
  8410. +
  8411. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  8412. + for (i = 0; i < state->unused_service; i++) {
  8413. + VCHIQ_SERVICE_T *srv = state->services[i];
  8414. + if (!srv) {
  8415. + pservice = &state->services[i];
  8416. + break;
  8417. + }
  8418. + }
  8419. + } else {
  8420. + for (i = (state->unused_service - 1); i >= 0; i--) {
  8421. + VCHIQ_SERVICE_T *srv = state->services[i];
  8422. + if (!srv)
  8423. + pservice = &state->services[i];
  8424. + else if ((srv->public_fourcc == params->fourcc)
  8425. + && ((srv->instance != instance) ||
  8426. + (srv->base.callback !=
  8427. + params->callback))) {
  8428. + /* There is another server using this
  8429. + ** fourcc which doesn't match. */
  8430. + pservice = NULL;
  8431. + break;
  8432. + }
  8433. + }
  8434. + }
  8435. +
  8436. + if (pservice) {
  8437. + service->localport = (pservice - state->services);
  8438. + if (!handle_seq)
  8439. + handle_seq = VCHIQ_MAX_STATES *
  8440. + VCHIQ_MAX_SERVICES;
  8441. + service->handle = handle_seq |
  8442. + (state->id * VCHIQ_MAX_SERVICES) |
  8443. + service->localport;
  8444. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  8445. + *pservice = service;
  8446. + if (pservice == &state->services[state->unused_service])
  8447. + state->unused_service++;
  8448. + }
  8449. +
  8450. + mutex_unlock(&state->mutex);
  8451. +
  8452. + if (!pservice) {
  8453. + kfree(service);
  8454. + service = NULL;
  8455. + }
  8456. + }
  8457. +
  8458. + if (service) {
  8459. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8460. + &state->service_quotas[service->localport];
  8461. + service_quota->slot_quota = state->default_slot_quota;
  8462. + service_quota->message_quota = state->default_message_quota;
  8463. + if (service_quota->slot_use_count == 0)
  8464. + service_quota->previous_tx_index =
  8465. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  8466. + - 1;
  8467. +
  8468. + /* Bring this service online */
  8469. + vchiq_set_service_state(service, srvstate);
  8470. +
  8471. + vchiq_log_info(vchiq_core_msg_log_level,
  8472. + "%s Service %c%c%c%c SrcPort:%d",
  8473. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  8474. + ? "Open" : "Add",
  8475. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  8476. + service->localport);
  8477. + }
  8478. +
  8479. + /* Don't unlock the service - leave it with a ref_count of 1. */
  8480. +
  8481. + return service;
  8482. +}
  8483. +
  8484. +VCHIQ_STATUS_T
  8485. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  8486. +{
  8487. + struct vchiq_open_payload payload = {
  8488. + service->base.fourcc,
  8489. + client_id,
  8490. + service->version,
  8491. + service->version_min
  8492. + };
  8493. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  8494. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8495. +
  8496. + service->client_id = client_id;
  8497. + vchiq_use_service_internal(service);
  8498. + status = queue_message(service->state, NULL,
  8499. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  8500. + &body, 1, sizeof(payload), QMFLAGS_IS_BLOCKING);
  8501. + if (status == VCHIQ_SUCCESS) {
  8502. + /* Wait for the ACK/NAK */
  8503. + if (down_interruptible(&service->remove_event) != 0) {
  8504. + status = VCHIQ_RETRY;
  8505. + vchiq_release_service_internal(service);
  8506. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  8507. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  8508. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  8509. + vchiq_log_error(vchiq_core_log_level,
  8510. + "%d: osi - srvstate = %s (ref %d)",
  8511. + service->state->id,
  8512. + srvstate_names[service->srvstate],
  8513. + service->ref_count);
  8514. + status = VCHIQ_ERROR;
  8515. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  8516. + vchiq_release_service_internal(service);
  8517. + }
  8518. + }
  8519. + return status;
  8520. +}
  8521. +
  8522. +static void
  8523. +release_service_messages(VCHIQ_SERVICE_T *service)
  8524. +{
  8525. + VCHIQ_STATE_T *state = service->state;
  8526. + int slot_last = state->remote->slot_last;
  8527. + int i;
  8528. +
  8529. + /* Release any claimed messages aimed at this service */
  8530. +
  8531. + if (service->sync) {
  8532. + VCHIQ_HEADER_T *header =
  8533. + (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  8534. + state->remote->slot_sync);
  8535. + if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
  8536. + release_message_sync(state, header);
  8537. +
  8538. + return;
  8539. + }
  8540. +
  8541. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  8542. + VCHIQ_SLOT_INFO_T *slot_info =
  8543. + SLOT_INFO_FROM_INDEX(state, i);
  8544. + if (slot_info->release_count != slot_info->use_count) {
  8545. + char *data =
  8546. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  8547. + unsigned int pos, end;
  8548. +
  8549. + end = VCHIQ_SLOT_SIZE;
  8550. + if (data == state->rx_data)
  8551. + /* This buffer is still being read from - stop
  8552. + ** at the current read position */
  8553. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  8554. +
  8555. + pos = 0;
  8556. +
  8557. + while (pos < end) {
  8558. + VCHIQ_HEADER_T *header =
  8559. + (VCHIQ_HEADER_T *)(data + pos);
  8560. + int msgid = header->msgid;
  8561. + int port = VCHIQ_MSG_DSTPORT(msgid);
  8562. + if ((port == service->localport) &&
  8563. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  8564. + vchiq_log_info(vchiq_core_log_level,
  8565. + " fsi - hdr %x",
  8566. + (unsigned int)header);
  8567. + release_slot(state, slot_info, header,
  8568. + NULL);
  8569. + }
  8570. + pos += calc_stride(header->size);
  8571. + if (pos > VCHIQ_SLOT_SIZE) {
  8572. + vchiq_log_error(vchiq_core_log_level,
  8573. + "fsi - pos %x: header %x, "
  8574. + "msgid %x, header->msgid %x, "
  8575. + "header->size %x",
  8576. + pos, (unsigned int)header,
  8577. + msgid, header->msgid,
  8578. + header->size);
  8579. + WARN(1, "invalid slot position\n");
  8580. + }
  8581. + }
  8582. + }
  8583. + }
  8584. +}
  8585. +
  8586. +static int
  8587. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  8588. +{
  8589. + VCHIQ_STATUS_T status;
  8590. +
  8591. + /* Abort any outstanding bulk transfers */
  8592. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  8593. + return 0;
  8594. + abort_outstanding_bulks(service, &service->bulk_tx);
  8595. + abort_outstanding_bulks(service, &service->bulk_rx);
  8596. + mutex_unlock(&service->bulk_mutex);
  8597. +
  8598. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  8599. + if (status == VCHIQ_SUCCESS)
  8600. + status = notify_bulks(service, &service->bulk_rx,
  8601. + 0/*!retry_poll*/);
  8602. + return (status == VCHIQ_SUCCESS);
  8603. +}
  8604. +
  8605. +static VCHIQ_STATUS_T
  8606. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  8607. +{
  8608. + VCHIQ_STATUS_T status;
  8609. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8610. + int newstate;
  8611. +
  8612. + switch (service->srvstate) {
  8613. + case VCHIQ_SRVSTATE_OPEN:
  8614. + case VCHIQ_SRVSTATE_CLOSESENT:
  8615. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8616. + if (is_server) {
  8617. + if (service->auto_close) {
  8618. + service->client_id = 0;
  8619. + service->remoteport = VCHIQ_PORT_FREE;
  8620. + newstate = VCHIQ_SRVSTATE_LISTENING;
  8621. + } else
  8622. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  8623. + } else
  8624. + newstate = VCHIQ_SRVSTATE_CLOSED;
  8625. + vchiq_set_service_state(service, newstate);
  8626. + break;
  8627. + case VCHIQ_SRVSTATE_LISTENING:
  8628. + break;
  8629. + default:
  8630. + vchiq_log_error(vchiq_core_log_level,
  8631. + "close_service_complete(%x) called in state %s",
  8632. + service->handle, srvstate_names[service->srvstate]);
  8633. + WARN(1, "close_service_complete in unexpected state\n");
  8634. + return VCHIQ_ERROR;
  8635. + }
  8636. +
  8637. + status = make_service_callback(service,
  8638. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  8639. +
  8640. + if (status != VCHIQ_RETRY) {
  8641. + int uc = service->service_use_count;
  8642. + int i;
  8643. + /* Complete the close process */
  8644. + for (i = 0; i < uc; i++)
  8645. + /* cater for cases where close is forced and the
  8646. + ** client may not close all it's handles */
  8647. + vchiq_release_service_internal(service);
  8648. +
  8649. + service->client_id = 0;
  8650. + service->remoteport = VCHIQ_PORT_FREE;
  8651. +
  8652. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  8653. + vchiq_free_service_internal(service);
  8654. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  8655. + if (is_server)
  8656. + service->closing = 0;
  8657. +
  8658. + up(&service->remove_event);
  8659. + }
  8660. + } else
  8661. + vchiq_set_service_state(service, failstate);
  8662. +
  8663. + return status;
  8664. +}
  8665. +
  8666. +/* Called by the slot handler */
  8667. +VCHIQ_STATUS_T
  8668. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  8669. +{
  8670. + VCHIQ_STATE_T *state = service->state;
  8671. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8672. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8673. +
  8674. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  8675. + service->state->id, service->localport, close_recvd,
  8676. + srvstate_names[service->srvstate]);
  8677. +
  8678. + switch (service->srvstate) {
  8679. + case VCHIQ_SRVSTATE_CLOSED:
  8680. + case VCHIQ_SRVSTATE_HIDDEN:
  8681. + case VCHIQ_SRVSTATE_LISTENING:
  8682. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8683. + if (close_recvd)
  8684. + vchiq_log_error(vchiq_core_log_level,
  8685. + "vchiq_close_service_internal(1) called "
  8686. + "in state %s",
  8687. + srvstate_names[service->srvstate]);
  8688. + else if (is_server) {
  8689. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  8690. + status = VCHIQ_ERROR;
  8691. + } else {
  8692. + service->client_id = 0;
  8693. + service->remoteport = VCHIQ_PORT_FREE;
  8694. + if (service->srvstate ==
  8695. + VCHIQ_SRVSTATE_CLOSEWAIT)
  8696. + vchiq_set_service_state(service,
  8697. + VCHIQ_SRVSTATE_LISTENING);
  8698. + }
  8699. + up(&service->remove_event);
  8700. + } else
  8701. + vchiq_free_service_internal(service);
  8702. + break;
  8703. + case VCHIQ_SRVSTATE_OPENING:
  8704. + if (close_recvd) {
  8705. + /* The open was rejected - tell the user */
  8706. + vchiq_set_service_state(service,
  8707. + VCHIQ_SRVSTATE_CLOSEWAIT);
  8708. + up(&service->remove_event);
  8709. + } else {
  8710. + /* Shutdown mid-open - let the other side know */
  8711. + status = queue_message(state, service,
  8712. + VCHIQ_MAKE_MSG
  8713. + (VCHIQ_MSG_CLOSE,
  8714. + service->localport,
  8715. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8716. + NULL, 0, 0, 0);
  8717. + }
  8718. + break;
  8719. +
  8720. + case VCHIQ_SRVSTATE_OPENSYNC:
  8721. + mutex_lock(&state->sync_mutex);
  8722. + /* Drop through */
  8723. +
  8724. + case VCHIQ_SRVSTATE_OPEN:
  8725. + if (state->is_master || close_recvd) {
  8726. + if (!do_abort_bulks(service))
  8727. + status = VCHIQ_RETRY;
  8728. + }
  8729. +
  8730. + release_service_messages(service);
  8731. +
  8732. + if (status == VCHIQ_SUCCESS)
  8733. + status = queue_message(state, service,
  8734. + VCHIQ_MAKE_MSG
  8735. + (VCHIQ_MSG_CLOSE,
  8736. + service->localport,
  8737. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8738. + NULL, 0, 0, QMFLAGS_NO_MUTEX_UNLOCK);
  8739. +
  8740. + if (status == VCHIQ_SUCCESS) {
  8741. + if (!close_recvd) {
  8742. + /* Change the state while the mutex is
  8743. + still held */
  8744. + vchiq_set_service_state(service,
  8745. + VCHIQ_SRVSTATE_CLOSESENT);
  8746. + mutex_unlock(&state->slot_mutex);
  8747. + if (service->sync)
  8748. + mutex_unlock(&state->sync_mutex);
  8749. + break;
  8750. + }
  8751. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  8752. + mutex_unlock(&state->sync_mutex);
  8753. + break;
  8754. + } else
  8755. + break;
  8756. +
  8757. + /* Change the state while the mutex is still held */
  8758. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
  8759. + mutex_unlock(&state->slot_mutex);
  8760. + if (service->sync)
  8761. + mutex_unlock(&state->sync_mutex);
  8762. +
  8763. + status = close_service_complete(service,
  8764. + VCHIQ_SRVSTATE_CLOSERECVD);
  8765. + break;
  8766. +
  8767. + case VCHIQ_SRVSTATE_CLOSESENT:
  8768. + if (!close_recvd)
  8769. + /* This happens when a process is killed mid-close */
  8770. + break;
  8771. +
  8772. + if (!state->is_master) {
  8773. + if (!do_abort_bulks(service)) {
  8774. + status = VCHIQ_RETRY;
  8775. + break;
  8776. + }
  8777. + }
  8778. +
  8779. + if (status == VCHIQ_SUCCESS)
  8780. + status = close_service_complete(service,
  8781. + VCHIQ_SRVSTATE_CLOSERECVD);
  8782. + break;
  8783. +
  8784. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8785. + if (!close_recvd && is_server)
  8786. + /* Force into LISTENING mode */
  8787. + vchiq_set_service_state(service,
  8788. + VCHIQ_SRVSTATE_LISTENING);
  8789. + status = close_service_complete(service,
  8790. + VCHIQ_SRVSTATE_CLOSERECVD);
  8791. + break;
  8792. +
  8793. + default:
  8794. + vchiq_log_error(vchiq_core_log_level,
  8795. + "vchiq_close_service_internal(%d) called in state %s",
  8796. + close_recvd, srvstate_names[service->srvstate]);
  8797. + break;
  8798. + }
  8799. +
  8800. + return status;
  8801. +}
  8802. +
  8803. +/* Called from the application process upon process death */
  8804. +void
  8805. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  8806. +{
  8807. + VCHIQ_STATE_T *state = service->state;
  8808. +
  8809. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  8810. + state->id, service->localport, service->remoteport);
  8811. +
  8812. + mark_service_closing(service);
  8813. +
  8814. + /* Mark the service for removal by the slot handler */
  8815. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  8816. +}
  8817. +
  8818. +/* Called from the slot handler */
  8819. +void
  8820. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  8821. +{
  8822. + VCHIQ_STATE_T *state = service->state;
  8823. +
  8824. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  8825. + state->id, service->localport);
  8826. +
  8827. + switch (service->srvstate) {
  8828. + case VCHIQ_SRVSTATE_OPENING:
  8829. + case VCHIQ_SRVSTATE_CLOSED:
  8830. + case VCHIQ_SRVSTATE_HIDDEN:
  8831. + case VCHIQ_SRVSTATE_LISTENING:
  8832. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8833. + break;
  8834. + default:
  8835. + vchiq_log_error(vchiq_core_log_level,
  8836. + "%d: fsi - (%d) in state %s",
  8837. + state->id, service->localport,
  8838. + srvstate_names[service->srvstate]);
  8839. + return;
  8840. + }
  8841. +
  8842. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  8843. +
  8844. + up(&service->remove_event);
  8845. +
  8846. + /* Release the initial lock */
  8847. + unlock_service(service);
  8848. +}
  8849. +
  8850. +VCHIQ_STATUS_T
  8851. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8852. +{
  8853. + VCHIQ_SERVICE_T *service;
  8854. + int i;
  8855. +
  8856. + /* Find all services registered to this client and enable them. */
  8857. + i = 0;
  8858. + while ((service = next_service_by_instance(state, instance,
  8859. + &i)) != NULL) {
  8860. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  8861. + vchiq_set_service_state(service,
  8862. + VCHIQ_SRVSTATE_LISTENING);
  8863. + unlock_service(service);
  8864. + }
  8865. +
  8866. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  8867. + if (queue_message(state, NULL,
  8868. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  8869. + 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
  8870. + return VCHIQ_RETRY;
  8871. +
  8872. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  8873. + }
  8874. +
  8875. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  8876. + if (down_interruptible(&state->connect) != 0)
  8877. + return VCHIQ_RETRY;
  8878. +
  8879. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  8880. + up(&state->connect);
  8881. + }
  8882. +
  8883. + return VCHIQ_SUCCESS;
  8884. +}
  8885. +
  8886. +VCHIQ_STATUS_T
  8887. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8888. +{
  8889. + VCHIQ_SERVICE_T *service;
  8890. + int i;
  8891. +
  8892. + /* Find all services registered to this client and enable them. */
  8893. + i = 0;
  8894. + while ((service = next_service_by_instance(state, instance,
  8895. + &i)) != NULL) {
  8896. + (void)vchiq_remove_service(service->handle);
  8897. + unlock_service(service);
  8898. + }
  8899. +
  8900. + return VCHIQ_SUCCESS;
  8901. +}
  8902. +
  8903. +VCHIQ_STATUS_T
  8904. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  8905. +{
  8906. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8907. +
  8908. + switch (state->conn_state) {
  8909. + case VCHIQ_CONNSTATE_CONNECTED:
  8910. + /* Request a pause */
  8911. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  8912. + request_poll(state, NULL, 0);
  8913. + break;
  8914. + default:
  8915. + vchiq_log_error(vchiq_core_log_level,
  8916. + "vchiq_pause_internal in state %s\n",
  8917. + conn_state_names[state->conn_state]);
  8918. + status = VCHIQ_ERROR;
  8919. + VCHIQ_STATS_INC(state, error_count);
  8920. + break;
  8921. + }
  8922. +
  8923. + return status;
  8924. +}
  8925. +
  8926. +VCHIQ_STATUS_T
  8927. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  8928. +{
  8929. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8930. +
  8931. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  8932. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  8933. + request_poll(state, NULL, 0);
  8934. + } else {
  8935. + status = VCHIQ_ERROR;
  8936. + VCHIQ_STATS_INC(state, error_count);
  8937. + }
  8938. +
  8939. + return status;
  8940. +}
  8941. +
  8942. +VCHIQ_STATUS_T
  8943. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  8944. +{
  8945. + /* Unregister the service */
  8946. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  8947. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8948. +
  8949. + if (!service)
  8950. + return VCHIQ_ERROR;
  8951. +
  8952. + vchiq_log_info(vchiq_core_log_level,
  8953. + "%d: close_service:%d",
  8954. + service->state->id, service->localport);
  8955. +
  8956. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8957. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8958. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  8959. + unlock_service(service);
  8960. + return VCHIQ_ERROR;
  8961. + }
  8962. +
  8963. + mark_service_closing(service);
  8964. +
  8965. + if (current == service->state->slot_handler_thread) {
  8966. + status = vchiq_close_service_internal(service,
  8967. + 0/*!close_recvd*/);
  8968. + BUG_ON(status == VCHIQ_RETRY);
  8969. + } else {
  8970. + /* Mark the service for termination by the slot handler */
  8971. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  8972. + }
  8973. +
  8974. + while (1) {
  8975. + if (down_interruptible(&service->remove_event) != 0) {
  8976. + status = VCHIQ_RETRY;
  8977. + break;
  8978. + }
  8979. +
  8980. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8981. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8982. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  8983. + break;
  8984. +
  8985. + vchiq_log_warning(vchiq_core_log_level,
  8986. + "%d: close_service:%d - waiting in state %s",
  8987. + service->state->id, service->localport,
  8988. + srvstate_names[service->srvstate]);
  8989. + }
  8990. +
  8991. + if ((status == VCHIQ_SUCCESS) &&
  8992. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  8993. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  8994. + status = VCHIQ_ERROR;
  8995. +
  8996. + unlock_service(service);
  8997. +
  8998. + return status;
  8999. +}
  9000. +
  9001. +VCHIQ_STATUS_T
  9002. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  9003. +{
  9004. + /* Unregister the service */
  9005. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9006. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  9007. +
  9008. + if (!service)
  9009. + return VCHIQ_ERROR;
  9010. +
  9011. + vchiq_log_info(vchiq_core_log_level,
  9012. + "%d: remove_service:%d",
  9013. + service->state->id, service->localport);
  9014. +
  9015. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  9016. + unlock_service(service);
  9017. + return VCHIQ_ERROR;
  9018. + }
  9019. +
  9020. + mark_service_closing(service);
  9021. +
  9022. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  9023. + (current == service->state->slot_handler_thread)) {
  9024. + /* Make it look like a client, because it must be removed and
  9025. + not left in the LISTENING state. */
  9026. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  9027. +
  9028. + status = vchiq_close_service_internal(service,
  9029. + 0/*!close_recvd*/);
  9030. + BUG_ON(status == VCHIQ_RETRY);
  9031. + } else {
  9032. + /* Mark the service for removal by the slot handler */
  9033. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  9034. + }
  9035. + while (1) {
  9036. + if (down_interruptible(&service->remove_event) != 0) {
  9037. + status = VCHIQ_RETRY;
  9038. + break;
  9039. + }
  9040. +
  9041. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  9042. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  9043. + break;
  9044. +
  9045. + vchiq_log_warning(vchiq_core_log_level,
  9046. + "%d: remove_service:%d - waiting in state %s",
  9047. + service->state->id, service->localport,
  9048. + srvstate_names[service->srvstate]);
  9049. + }
  9050. +
  9051. + if ((status == VCHIQ_SUCCESS) &&
  9052. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  9053. + status = VCHIQ_ERROR;
  9054. +
  9055. + unlock_service(service);
  9056. +
  9057. + return status;
  9058. +}
  9059. +
  9060. +
  9061. +/* This function may be called by kernel threads or user threads.
  9062. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  9063. + * received and the call should be retried after being returned to user
  9064. + * context.
  9065. + * When called in blocking mode, the userdata field points to a bulk_waiter
  9066. + * structure.
  9067. + */
  9068. +VCHIQ_STATUS_T
  9069. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  9070. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  9071. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  9072. +{
  9073. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9074. + VCHIQ_BULK_QUEUE_T *queue;
  9075. + VCHIQ_BULK_T *bulk;
  9076. + VCHIQ_STATE_T *state;
  9077. + struct bulk_waiter *bulk_waiter = NULL;
  9078. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  9079. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  9080. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  9081. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9082. +
  9083. + if (!service ||
  9084. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  9085. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  9086. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  9087. + goto error_exit;
  9088. +
  9089. + switch (mode) {
  9090. + case VCHIQ_BULK_MODE_NOCALLBACK:
  9091. + case VCHIQ_BULK_MODE_CALLBACK:
  9092. + break;
  9093. + case VCHIQ_BULK_MODE_BLOCKING:
  9094. + bulk_waiter = (struct bulk_waiter *)userdata;
  9095. + sema_init(&bulk_waiter->event, 0);
  9096. + bulk_waiter->actual = 0;
  9097. + bulk_waiter->bulk = NULL;
  9098. + break;
  9099. + case VCHIQ_BULK_MODE_WAITING:
  9100. + bulk_waiter = (struct bulk_waiter *)userdata;
  9101. + bulk = bulk_waiter->bulk;
  9102. + goto waiting;
  9103. + default:
  9104. + goto error_exit;
  9105. + }
  9106. +
  9107. + state = service->state;
  9108. +
  9109. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  9110. + &service->bulk_tx : &service->bulk_rx;
  9111. +
  9112. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  9113. + status = VCHIQ_RETRY;
  9114. + goto error_exit;
  9115. + }
  9116. +
  9117. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  9118. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  9119. + do {
  9120. + mutex_unlock(&service->bulk_mutex);
  9121. + if (down_interruptible(&service->bulk_remove_event)
  9122. + != 0) {
  9123. + status = VCHIQ_RETRY;
  9124. + goto error_exit;
  9125. + }
  9126. + if (mutex_lock_interruptible(&service->bulk_mutex)
  9127. + != 0) {
  9128. + status = VCHIQ_RETRY;
  9129. + goto error_exit;
  9130. + }
  9131. + } while (queue->local_insert == queue->remove +
  9132. + VCHIQ_NUM_SERVICE_BULKS);
  9133. + }
  9134. +
  9135. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  9136. +
  9137. + bulk->mode = mode;
  9138. + bulk->dir = dir;
  9139. + bulk->userdata = userdata;
  9140. + bulk->size = size;
  9141. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  9142. +
  9143. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  9144. + VCHIQ_SUCCESS)
  9145. + goto unlock_error_exit;
  9146. +
  9147. + wmb();
  9148. +
  9149. + vchiq_log_info(vchiq_core_log_level,
  9150. + "%d: bt (%d->%d) %cx %x@%x %x",
  9151. + state->id,
  9152. + service->localport, service->remoteport, dir_char,
  9153. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  9154. +
  9155. + /* The slot mutex must be held when the service is being closed, so
  9156. + claim it here to ensure that isn't happening */
  9157. + if (mutex_lock_interruptible(&state->slot_mutex) != 0) {
  9158. + status = VCHIQ_RETRY;
  9159. + goto cancel_bulk_error_exit;
  9160. + }
  9161. +
  9162. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
  9163. + goto unlock_both_error_exit;
  9164. +
  9165. + if (state->is_master) {
  9166. + queue->local_insert++;
  9167. + if (resolve_bulks(service, queue))
  9168. + request_poll(state, service,
  9169. + (dir == VCHIQ_BULK_TRANSMIT) ?
  9170. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  9171. + } else {
  9172. + int payload[2] = { (int)bulk->data, bulk->size };
  9173. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  9174. +
  9175. + status = queue_message(state, NULL,
  9176. + VCHIQ_MAKE_MSG(dir_msgtype,
  9177. + service->localport, service->remoteport),
  9178. + &element, 1, sizeof(payload),
  9179. + QMFLAGS_IS_BLOCKING |
  9180. + QMFLAGS_NO_MUTEX_LOCK |
  9181. + QMFLAGS_NO_MUTEX_UNLOCK);
  9182. + if (status != VCHIQ_SUCCESS) {
  9183. + goto unlock_both_error_exit;
  9184. + }
  9185. + queue->local_insert++;
  9186. + }
  9187. +
  9188. + mutex_unlock(&state->slot_mutex);
  9189. + mutex_unlock(&service->bulk_mutex);
  9190. +
  9191. + vchiq_log_trace(vchiq_core_log_level,
  9192. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  9193. + state->id,
  9194. + service->localport, dir_char,
  9195. + queue->local_insert, queue->remote_insert, queue->process);
  9196. +
  9197. +waiting:
  9198. + unlock_service(service);
  9199. +
  9200. + status = VCHIQ_SUCCESS;
  9201. +
  9202. + if (bulk_waiter) {
  9203. + bulk_waiter->bulk = bulk;
  9204. + if (down_interruptible(&bulk_waiter->event) != 0)
  9205. + status = VCHIQ_RETRY;
  9206. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  9207. + status = VCHIQ_ERROR;
  9208. + }
  9209. +
  9210. + return status;
  9211. +
  9212. +unlock_both_error_exit:
  9213. + mutex_unlock(&state->slot_mutex);
  9214. +cancel_bulk_error_exit:
  9215. + vchiq_complete_bulk(bulk);
  9216. +unlock_error_exit:
  9217. + mutex_unlock(&service->bulk_mutex);
  9218. +
  9219. +error_exit:
  9220. + if (service)
  9221. + unlock_service(service);
  9222. + return status;
  9223. +}
  9224. +
  9225. +VCHIQ_STATUS_T
  9226. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  9227. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  9228. +{
  9229. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9230. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9231. +
  9232. + unsigned int size = 0;
  9233. + unsigned int i;
  9234. +
  9235. + if (!service ||
  9236. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  9237. + goto error_exit;
  9238. +
  9239. + for (i = 0; i < (unsigned int)count; i++) {
  9240. + if (elements[i].size) {
  9241. + if (elements[i].data == NULL) {
  9242. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9243. + goto error_exit;
  9244. + }
  9245. + size += elements[i].size;
  9246. + }
  9247. + }
  9248. +
  9249. + if (size > VCHIQ_MAX_MSG_SIZE) {
  9250. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9251. + goto error_exit;
  9252. + }
  9253. +
  9254. + switch (service->srvstate) {
  9255. + case VCHIQ_SRVSTATE_OPEN:
  9256. + status = queue_message(service->state, service,
  9257. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9258. + service->localport,
  9259. + service->remoteport),
  9260. + elements, count, size, 1);
  9261. + break;
  9262. + case VCHIQ_SRVSTATE_OPENSYNC:
  9263. + status = queue_message_sync(service->state, service,
  9264. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9265. + service->localport,
  9266. + service->remoteport),
  9267. + elements, count, size, 1);
  9268. + break;
  9269. + default:
  9270. + status = VCHIQ_ERROR;
  9271. + break;
  9272. + }
  9273. +
  9274. +error_exit:
  9275. + if (service)
  9276. + unlock_service(service);
  9277. +
  9278. + return status;
  9279. +}
  9280. +
  9281. +void
  9282. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  9283. +{
  9284. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9285. + VCHIQ_SHARED_STATE_T *remote;
  9286. + VCHIQ_STATE_T *state;
  9287. + int slot_index;
  9288. +
  9289. + if (!service)
  9290. + return;
  9291. +
  9292. + state = service->state;
  9293. + remote = state->remote;
  9294. +
  9295. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  9296. +
  9297. + if ((slot_index >= remote->slot_first) &&
  9298. + (slot_index <= remote->slot_last)) {
  9299. + int msgid = header->msgid;
  9300. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  9301. + VCHIQ_SLOT_INFO_T *slot_info =
  9302. + SLOT_INFO_FROM_INDEX(state, slot_index);
  9303. +
  9304. + release_slot(state, slot_info, header, service);
  9305. + }
  9306. + } else if (slot_index == remote->slot_sync)
  9307. + release_message_sync(state, header);
  9308. +
  9309. + unlock_service(service);
  9310. +}
  9311. +
  9312. +static void
  9313. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  9314. +{
  9315. + header->msgid = VCHIQ_MSGID_PADDING;
  9316. + wmb();
  9317. + remote_event_signal(&state->remote->sync_release);
  9318. +}
  9319. +
  9320. +VCHIQ_STATUS_T
  9321. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  9322. +{
  9323. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9324. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9325. +
  9326. + if (!service ||
  9327. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  9328. + !peer_version)
  9329. + goto exit;
  9330. + *peer_version = service->peer_version;
  9331. + status = VCHIQ_SUCCESS;
  9332. +
  9333. +exit:
  9334. + if (service)
  9335. + unlock_service(service);
  9336. + return status;
  9337. +}
  9338. +
  9339. +VCHIQ_STATUS_T
  9340. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  9341. + int config_size, VCHIQ_CONFIG_T *pconfig)
  9342. +{
  9343. + VCHIQ_CONFIG_T config;
  9344. +
  9345. + (void)instance;
  9346. +
  9347. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  9348. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  9349. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  9350. + config.max_services = VCHIQ_MAX_SERVICES;
  9351. + config.version = VCHIQ_VERSION;
  9352. + config.version_min = VCHIQ_VERSION_MIN;
  9353. +
  9354. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  9355. + return VCHIQ_ERROR;
  9356. +
  9357. + memcpy(pconfig, &config,
  9358. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  9359. +
  9360. + return VCHIQ_SUCCESS;
  9361. +}
  9362. +
  9363. +VCHIQ_STATUS_T
  9364. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  9365. + VCHIQ_SERVICE_OPTION_T option, int value)
  9366. +{
  9367. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9368. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9369. +
  9370. + if (service) {
  9371. + switch (option) {
  9372. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  9373. + service->auto_close = value;
  9374. + status = VCHIQ_SUCCESS;
  9375. + break;
  9376. +
  9377. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  9378. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9379. + &service->state->service_quotas[
  9380. + service->localport];
  9381. + if (value == 0)
  9382. + value = service->state->default_slot_quota;
  9383. + if ((value >= service_quota->slot_use_count) &&
  9384. + (value < (unsigned short)~0)) {
  9385. + service_quota->slot_quota = value;
  9386. + if ((value >= service_quota->slot_use_count) &&
  9387. + (service_quota->message_quota >=
  9388. + service_quota->message_use_count)) {
  9389. + /* Signal the service that it may have
  9390. + ** dropped below its quota */
  9391. + up(&service_quota->quota_event);
  9392. + }
  9393. + status = VCHIQ_SUCCESS;
  9394. + }
  9395. + } break;
  9396. +
  9397. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  9398. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9399. + &service->state->service_quotas[
  9400. + service->localport];
  9401. + if (value == 0)
  9402. + value = service->state->default_message_quota;
  9403. + if ((value >= service_quota->message_use_count) &&
  9404. + (value < (unsigned short)~0)) {
  9405. + service_quota->message_quota = value;
  9406. + if ((value >=
  9407. + service_quota->message_use_count) &&
  9408. + (service_quota->slot_quota >=
  9409. + service_quota->slot_use_count))
  9410. + /* Signal the service that it may have
  9411. + ** dropped below its quota */
  9412. + up(&service_quota->quota_event);
  9413. + status = VCHIQ_SUCCESS;
  9414. + }
  9415. + } break;
  9416. +
  9417. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  9418. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  9419. + (service->srvstate ==
  9420. + VCHIQ_SRVSTATE_LISTENING)) {
  9421. + service->sync = value;
  9422. + status = VCHIQ_SUCCESS;
  9423. + }
  9424. + break;
  9425. +
  9426. + case VCHIQ_SERVICE_OPTION_TRACE:
  9427. + service->trace = value;
  9428. + status = VCHIQ_SUCCESS;
  9429. + break;
  9430. +
  9431. + default:
  9432. + break;
  9433. + }
  9434. + unlock_service(service);
  9435. + }
  9436. +
  9437. + return status;
  9438. +}
  9439. +
  9440. +void
  9441. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  9442. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  9443. +{
  9444. + static const char *const debug_names[] = {
  9445. + "<entries>",
  9446. + "SLOT_HANDLER_COUNT",
  9447. + "SLOT_HANDLER_LINE",
  9448. + "PARSE_LINE",
  9449. + "PARSE_HEADER",
  9450. + "PARSE_MSGID",
  9451. + "AWAIT_COMPLETION_LINE",
  9452. + "DEQUEUE_MESSAGE_LINE",
  9453. + "SERVICE_CALLBACK_LINE",
  9454. + "MSG_QUEUE_FULL_COUNT",
  9455. + "COMPLETION_QUEUE_FULL_COUNT"
  9456. + };
  9457. + int i;
  9458. +
  9459. + char buf[80];
  9460. + int len;
  9461. + len = snprintf(buf, sizeof(buf),
  9462. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  9463. + label, shared->slot_first, shared->slot_last,
  9464. + shared->tx_pos, shared->slot_queue_recycle);
  9465. + vchiq_dump(dump_context, buf, len + 1);
  9466. +
  9467. + len = snprintf(buf, sizeof(buf),
  9468. + " Slots claimed:");
  9469. + vchiq_dump(dump_context, buf, len + 1);
  9470. +
  9471. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  9472. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  9473. + if (slot_info.use_count != slot_info.release_count) {
  9474. + len = snprintf(buf, sizeof(buf),
  9475. + " %d: %d/%d", i, slot_info.use_count,
  9476. + slot_info.release_count);
  9477. + vchiq_dump(dump_context, buf, len + 1);
  9478. + }
  9479. + }
  9480. +
  9481. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  9482. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  9483. + debug_names[i], shared->debug[i], shared->debug[i]);
  9484. + vchiq_dump(dump_context, buf, len + 1);
  9485. + }
  9486. +}
  9487. +
  9488. +void
  9489. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  9490. +{
  9491. + char buf[80];
  9492. + int len;
  9493. + int i;
  9494. +
  9495. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  9496. + conn_state_names[state->conn_state]);
  9497. + vchiq_dump(dump_context, buf, len + 1);
  9498. +
  9499. + len = snprintf(buf, sizeof(buf),
  9500. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  9501. + state->local->tx_pos,
  9502. + (uint32_t)state->tx_data +
  9503. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  9504. + state->rx_pos,
  9505. + (uint32_t)state->rx_data +
  9506. + (state->rx_pos & VCHIQ_SLOT_MASK));
  9507. + vchiq_dump(dump_context, buf, len + 1);
  9508. +
  9509. + len = snprintf(buf, sizeof(buf),
  9510. + " Version: %d (min %d)",
  9511. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  9512. + vchiq_dump(dump_context, buf, len + 1);
  9513. +
  9514. + if (VCHIQ_ENABLE_STATS) {
  9515. + len = snprintf(buf, sizeof(buf),
  9516. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  9517. + "error_count=%d",
  9518. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  9519. + state->stats.error_count);
  9520. + vchiq_dump(dump_context, buf, len + 1);
  9521. + }
  9522. +
  9523. + len = snprintf(buf, sizeof(buf),
  9524. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  9525. + "(%d data)",
  9526. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  9527. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  9528. + state->data_quota - state->data_use_count,
  9529. + state->local->slot_queue_recycle - state->slot_queue_available,
  9530. + state->stats.slot_stalls, state->stats.data_stalls);
  9531. + vchiq_dump(dump_context, buf, len + 1);
  9532. +
  9533. + vchiq_dump_platform_state(dump_context);
  9534. +
  9535. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  9536. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  9537. +
  9538. + vchiq_dump_platform_instances(dump_context);
  9539. +
  9540. + for (i = 0; i < state->unused_service; i++) {
  9541. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  9542. +
  9543. + if (service) {
  9544. + vchiq_dump_service_state(dump_context, service);
  9545. + unlock_service(service);
  9546. + }
  9547. + }
  9548. +}
  9549. +
  9550. +void
  9551. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  9552. +{
  9553. + char buf[80];
  9554. + int len;
  9555. +
  9556. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  9557. + service->localport, srvstate_names[service->srvstate],
  9558. + service->ref_count - 1); /*Don't include the lock just taken*/
  9559. +
  9560. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  9561. + char remoteport[30];
  9562. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9563. + &service->state->service_quotas[service->localport];
  9564. + int fourcc = service->base.fourcc;
  9565. + int tx_pending, rx_pending;
  9566. + if (service->remoteport != VCHIQ_PORT_FREE) {
  9567. + int len2 = snprintf(remoteport, sizeof(remoteport),
  9568. + "%d", service->remoteport);
  9569. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  9570. + snprintf(remoteport + len2,
  9571. + sizeof(remoteport) - len2,
  9572. + " (client %x)", service->client_id);
  9573. + } else
  9574. + strcpy(remoteport, "n/a");
  9575. +
  9576. + len += snprintf(buf + len, sizeof(buf) - len,
  9577. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  9578. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  9579. + remoteport,
  9580. + service_quota->message_use_count,
  9581. + service_quota->message_quota,
  9582. + service_quota->slot_use_count,
  9583. + service_quota->slot_quota);
  9584. +
  9585. + vchiq_dump(dump_context, buf, len + 1);
  9586. +
  9587. + tx_pending = service->bulk_tx.local_insert -
  9588. + service->bulk_tx.remote_insert;
  9589. +
  9590. + rx_pending = service->bulk_rx.local_insert -
  9591. + service->bulk_rx.remote_insert;
  9592. +
  9593. + len = snprintf(buf, sizeof(buf),
  9594. + " Bulk: tx_pending=%d (size %d),"
  9595. + " rx_pending=%d (size %d)",
  9596. + tx_pending,
  9597. + tx_pending ? service->bulk_tx.bulks[
  9598. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  9599. + rx_pending,
  9600. + rx_pending ? service->bulk_rx.bulks[
  9601. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  9602. +
  9603. + if (VCHIQ_ENABLE_STATS) {
  9604. + vchiq_dump(dump_context, buf, len + 1);
  9605. +
  9606. + len = snprintf(buf, sizeof(buf),
  9607. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  9608. + "rx_count=%d, rx_bytes=%llu",
  9609. + service->stats.ctrl_tx_count,
  9610. + service->stats.ctrl_tx_bytes,
  9611. + service->stats.ctrl_rx_count,
  9612. + service->stats.ctrl_rx_bytes);
  9613. + vchiq_dump(dump_context, buf, len + 1);
  9614. +
  9615. + len = snprintf(buf, sizeof(buf),
  9616. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  9617. + "rx_count=%d, rx_bytes=%llu",
  9618. + service->stats.bulk_tx_count,
  9619. + service->stats.bulk_tx_bytes,
  9620. + service->stats.bulk_rx_count,
  9621. + service->stats.bulk_rx_bytes);
  9622. + vchiq_dump(dump_context, buf, len + 1);
  9623. +
  9624. + len = snprintf(buf, sizeof(buf),
  9625. + " %d quota stalls, %d slot stalls, "
  9626. + "%d bulk stalls, %d aborted, %d errors",
  9627. + service->stats.quota_stalls,
  9628. + service->stats.slot_stalls,
  9629. + service->stats.bulk_stalls,
  9630. + service->stats.bulk_aborted_count,
  9631. + service->stats.error_count);
  9632. + }
  9633. + }
  9634. +
  9635. + vchiq_dump(dump_context, buf, len + 1);
  9636. +
  9637. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  9638. + vchiq_dump_platform_service_state(dump_context, service);
  9639. +}
  9640. +
  9641. +
  9642. +void
  9643. +vchiq_loud_error_header(void)
  9644. +{
  9645. + vchiq_log_error(vchiq_core_log_level,
  9646. + "============================================================"
  9647. + "================");
  9648. + vchiq_log_error(vchiq_core_log_level,
  9649. + "============================================================"
  9650. + "================");
  9651. + vchiq_log_error(vchiq_core_log_level, "=====");
  9652. +}
  9653. +
  9654. +void
  9655. +vchiq_loud_error_footer(void)
  9656. +{
  9657. + vchiq_log_error(vchiq_core_log_level, "=====");
  9658. + vchiq_log_error(vchiq_core_log_level,
  9659. + "============================================================"
  9660. + "================");
  9661. + vchiq_log_error(vchiq_core_log_level,
  9662. + "============================================================"
  9663. + "================");
  9664. +}
  9665. +
  9666. +
  9667. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  9668. +{
  9669. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9670. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9671. + status = queue_message(state, NULL,
  9672. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  9673. + NULL, 0, 0, 0);
  9674. + return status;
  9675. +}
  9676. +
  9677. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  9678. +{
  9679. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9680. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9681. + status = queue_message(state, NULL,
  9682. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  9683. + NULL, 0, 0, 0);
  9684. + return status;
  9685. +}
  9686. +
  9687. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  9688. +{
  9689. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9690. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9691. + status = queue_message(state, NULL,
  9692. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  9693. + NULL, 0, 0, 0);
  9694. + return status;
  9695. +}
  9696. +
  9697. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  9698. + size_t numBytes)
  9699. +{
  9700. + const uint8_t *mem = (const uint8_t *)voidMem;
  9701. + size_t offset;
  9702. + char lineBuf[100];
  9703. + char *s;
  9704. +
  9705. + while (numBytes > 0) {
  9706. + s = lineBuf;
  9707. +
  9708. + for (offset = 0; offset < 16; offset++) {
  9709. + if (offset < numBytes)
  9710. + s += snprintf(s, 4, "%02x ", mem[offset]);
  9711. + else
  9712. + s += snprintf(s, 4, " ");
  9713. + }
  9714. +
  9715. + for (offset = 0; offset < 16; offset++) {
  9716. + if (offset < numBytes) {
  9717. + uint8_t ch = mem[offset];
  9718. +
  9719. + if ((ch < ' ') || (ch > '~'))
  9720. + ch = '.';
  9721. + *s++ = (char)ch;
  9722. + }
  9723. + }
  9724. + *s++ = '\0';
  9725. +
  9726. + if ((label != NULL) && (*label != '\0'))
  9727. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9728. + "%s: %08x: %s", label, addr, lineBuf);
  9729. + else
  9730. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9731. + "%08x: %s", addr, lineBuf);
  9732. +
  9733. + addr += 16;
  9734. + mem += 16;
  9735. + if (numBytes > 16)
  9736. + numBytes -= 16;
  9737. + else
  9738. + numBytes = 0;
  9739. + }
  9740. +}
  9741. --- /dev/null
  9742. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  9743. @@ -0,0 +1,712 @@
  9744. +/**
  9745. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9746. + *
  9747. + * Redistribution and use in source and binary forms, with or without
  9748. + * modification, are permitted provided that the following conditions
  9749. + * are met:
  9750. + * 1. Redistributions of source code must retain the above copyright
  9751. + * notice, this list of conditions, and the following disclaimer,
  9752. + * without modification.
  9753. + * 2. Redistributions in binary form must reproduce the above copyright
  9754. + * notice, this list of conditions and the following disclaimer in the
  9755. + * documentation and/or other materials provided with the distribution.
  9756. + * 3. The names of the above-listed copyright holders may not be used
  9757. + * to endorse or promote products derived from this software without
  9758. + * specific prior written permission.
  9759. + *
  9760. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9761. + * GNU General Public License ("GPL") version 2, as published by the Free
  9762. + * Software Foundation.
  9763. + *
  9764. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9765. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9766. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9767. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9768. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9769. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9770. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9771. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9772. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9773. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9774. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9775. + */
  9776. +
  9777. +#ifndef VCHIQ_CORE_H
  9778. +#define VCHIQ_CORE_H
  9779. +
  9780. +#include <linux/mutex.h>
  9781. +#include <linux/semaphore.h>
  9782. +#include <linux/kthread.h>
  9783. +
  9784. +#include "vchiq_cfg.h"
  9785. +
  9786. +#include "vchiq.h"
  9787. +
  9788. +/* Run time control of log level, based on KERN_XXX level. */
  9789. +#define VCHIQ_LOG_DEFAULT 4
  9790. +#define VCHIQ_LOG_ERROR 3
  9791. +#define VCHIQ_LOG_WARNING 4
  9792. +#define VCHIQ_LOG_INFO 6
  9793. +#define VCHIQ_LOG_TRACE 7
  9794. +
  9795. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  9796. +
  9797. +#ifndef vchiq_log_error
  9798. +#define vchiq_log_error(cat, fmt, ...) \
  9799. + do { if (cat >= VCHIQ_LOG_ERROR) \
  9800. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9801. +#endif
  9802. +#ifndef vchiq_log_warning
  9803. +#define vchiq_log_warning(cat, fmt, ...) \
  9804. + do { if (cat >= VCHIQ_LOG_WARNING) \
  9805. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9806. +#endif
  9807. +#ifndef vchiq_log_info
  9808. +#define vchiq_log_info(cat, fmt, ...) \
  9809. + do { if (cat >= VCHIQ_LOG_INFO) \
  9810. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9811. +#endif
  9812. +#ifndef vchiq_log_trace
  9813. +#define vchiq_log_trace(cat, fmt, ...) \
  9814. + do { if (cat >= VCHIQ_LOG_TRACE) \
  9815. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9816. +#endif
  9817. +
  9818. +#define vchiq_loud_error(...) \
  9819. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  9820. +
  9821. +#ifndef vchiq_static_assert
  9822. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  9823. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  9824. +#endif
  9825. +
  9826. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  9827. +
  9828. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  9829. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  9830. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  9831. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  9832. +
  9833. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  9834. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  9835. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  9836. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  9837. +
  9838. +#define VCHIQ_MSG_PADDING 0 /* - */
  9839. +#define VCHIQ_MSG_CONNECT 1 /* - */
  9840. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  9841. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  9842. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  9843. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  9844. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  9845. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  9846. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  9847. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  9848. +#define VCHIQ_MSG_PAUSE 10 /* - */
  9849. +#define VCHIQ_MSG_RESUME 11 /* - */
  9850. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  9851. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  9852. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  9853. +
  9854. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  9855. +#define VCHIQ_PORT_FREE 0x1000
  9856. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  9857. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  9858. + ((type<<24) | (srcport<<12) | (dstport<<0))
  9859. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  9860. +#define VCHIQ_MSG_SRCPORT(msgid) \
  9861. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  9862. +#define VCHIQ_MSG_DSTPORT(msgid) \
  9863. + ((unsigned short)msgid & 0xfff)
  9864. +
  9865. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  9866. + ((fourcc) >> 24) & 0xff, \
  9867. + ((fourcc) >> 16) & 0xff, \
  9868. + ((fourcc) >> 8) & 0xff, \
  9869. + (fourcc) & 0xff
  9870. +
  9871. +/* Ensure the fields are wide enough */
  9872. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  9873. + == 0);
  9874. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  9875. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  9876. + (unsigned int)VCHIQ_PORT_FREE);
  9877. +
  9878. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  9879. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  9880. +
  9881. +#define VCHIQ_FOURCC_INVALID 0x00000000
  9882. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  9883. +
  9884. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  9885. +
  9886. +typedef uint32_t BITSET_T;
  9887. +
  9888. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  9889. +
  9890. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  9891. +#define BITSET_WORD(b) (b >> 5)
  9892. +#define BITSET_BIT(b) (1 << (b & 31))
  9893. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  9894. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  9895. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  9896. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  9897. +
  9898. +#if VCHIQ_ENABLE_STATS
  9899. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  9900. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  9901. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  9902. + (service->stats. stat += addend)
  9903. +#else
  9904. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  9905. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  9906. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  9907. +#endif
  9908. +
  9909. +enum {
  9910. + DEBUG_ENTRIES,
  9911. +#if VCHIQ_ENABLE_DEBUG
  9912. + DEBUG_SLOT_HANDLER_COUNT,
  9913. + DEBUG_SLOT_HANDLER_LINE,
  9914. + DEBUG_PARSE_LINE,
  9915. + DEBUG_PARSE_HEADER,
  9916. + DEBUG_PARSE_MSGID,
  9917. + DEBUG_AWAIT_COMPLETION_LINE,
  9918. + DEBUG_DEQUEUE_MESSAGE_LINE,
  9919. + DEBUG_SERVICE_CALLBACK_LINE,
  9920. + DEBUG_MSG_QUEUE_FULL_COUNT,
  9921. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  9922. +#endif
  9923. + DEBUG_MAX
  9924. +};
  9925. +
  9926. +#if VCHIQ_ENABLE_DEBUG
  9927. +
  9928. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  9929. +#define DEBUG_TRACE(d) \
  9930. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  9931. +#define DEBUG_VALUE(d, v) \
  9932. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  9933. +#define DEBUG_COUNT(d) \
  9934. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  9935. +
  9936. +#else /* VCHIQ_ENABLE_DEBUG */
  9937. +
  9938. +#define DEBUG_INITIALISE(local)
  9939. +#define DEBUG_TRACE(d)
  9940. +#define DEBUG_VALUE(d, v)
  9941. +#define DEBUG_COUNT(d)
  9942. +
  9943. +#endif /* VCHIQ_ENABLE_DEBUG */
  9944. +
  9945. +typedef enum {
  9946. + VCHIQ_CONNSTATE_DISCONNECTED,
  9947. + VCHIQ_CONNSTATE_CONNECTING,
  9948. + VCHIQ_CONNSTATE_CONNECTED,
  9949. + VCHIQ_CONNSTATE_PAUSING,
  9950. + VCHIQ_CONNSTATE_PAUSE_SENT,
  9951. + VCHIQ_CONNSTATE_PAUSED,
  9952. + VCHIQ_CONNSTATE_RESUMING,
  9953. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  9954. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  9955. +} VCHIQ_CONNSTATE_T;
  9956. +
  9957. +enum {
  9958. + VCHIQ_SRVSTATE_FREE,
  9959. + VCHIQ_SRVSTATE_HIDDEN,
  9960. + VCHIQ_SRVSTATE_LISTENING,
  9961. + VCHIQ_SRVSTATE_OPENING,
  9962. + VCHIQ_SRVSTATE_OPEN,
  9963. + VCHIQ_SRVSTATE_OPENSYNC,
  9964. + VCHIQ_SRVSTATE_CLOSESENT,
  9965. + VCHIQ_SRVSTATE_CLOSERECVD,
  9966. + VCHIQ_SRVSTATE_CLOSEWAIT,
  9967. + VCHIQ_SRVSTATE_CLOSED
  9968. +};
  9969. +
  9970. +enum {
  9971. + VCHIQ_POLL_TERMINATE,
  9972. + VCHIQ_POLL_REMOVE,
  9973. + VCHIQ_POLL_TXNOTIFY,
  9974. + VCHIQ_POLL_RXNOTIFY,
  9975. + VCHIQ_POLL_COUNT
  9976. +};
  9977. +
  9978. +typedef enum {
  9979. + VCHIQ_BULK_TRANSMIT,
  9980. + VCHIQ_BULK_RECEIVE
  9981. +} VCHIQ_BULK_DIR_T;
  9982. +
  9983. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  9984. +
  9985. +typedef struct vchiq_bulk_struct {
  9986. + short mode;
  9987. + short dir;
  9988. + void *userdata;
  9989. + VCHI_MEM_HANDLE_T handle;
  9990. + void *data;
  9991. + int size;
  9992. + void *remote_data;
  9993. + int remote_size;
  9994. + int actual;
  9995. +} VCHIQ_BULK_T;
  9996. +
  9997. +typedef struct vchiq_bulk_queue_struct {
  9998. + int local_insert; /* Where to insert the next local bulk */
  9999. + int remote_insert; /* Where to insert the next remote bulk (master) */
  10000. + int process; /* Bulk to transfer next */
  10001. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  10002. + int remove; /* Bulk to notify the local client of, and remove,
  10003. + ** next */
  10004. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  10005. +} VCHIQ_BULK_QUEUE_T;
  10006. +
  10007. +typedef struct remote_event_struct {
  10008. + int armed;
  10009. + int fired;
  10010. + struct semaphore *event;
  10011. +} REMOTE_EVENT_T;
  10012. +
  10013. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  10014. +
  10015. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  10016. +
  10017. +typedef struct vchiq_slot_struct {
  10018. + char data[VCHIQ_SLOT_SIZE];
  10019. +} VCHIQ_SLOT_T;
  10020. +
  10021. +typedef struct vchiq_slot_info_struct {
  10022. + /* Use two counters rather than one to avoid the need for a mutex. */
  10023. + short use_count;
  10024. + short release_count;
  10025. +} VCHIQ_SLOT_INFO_T;
  10026. +
  10027. +typedef struct vchiq_service_struct {
  10028. + VCHIQ_SERVICE_BASE_T base;
  10029. + VCHIQ_SERVICE_HANDLE_T handle;
  10030. + unsigned int ref_count;
  10031. + int srvstate;
  10032. + VCHIQ_USERDATA_TERM_T userdata_term;
  10033. + unsigned int localport;
  10034. + unsigned int remoteport;
  10035. + int public_fourcc;
  10036. + int client_id;
  10037. + char auto_close;
  10038. + char sync;
  10039. + char closing;
  10040. + char trace;
  10041. + atomic_t poll_flags;
  10042. + short version;
  10043. + short version_min;
  10044. + short peer_version;
  10045. +
  10046. + VCHIQ_STATE_T *state;
  10047. + VCHIQ_INSTANCE_T instance;
  10048. +
  10049. + int service_use_count;
  10050. +
  10051. + VCHIQ_BULK_QUEUE_T bulk_tx;
  10052. + VCHIQ_BULK_QUEUE_T bulk_rx;
  10053. +
  10054. + struct semaphore remove_event;
  10055. + struct semaphore bulk_remove_event;
  10056. + struct mutex bulk_mutex;
  10057. +
  10058. + struct service_stats_struct {
  10059. + int quota_stalls;
  10060. + int slot_stalls;
  10061. + int bulk_stalls;
  10062. + int error_count;
  10063. + int ctrl_tx_count;
  10064. + int ctrl_rx_count;
  10065. + int bulk_tx_count;
  10066. + int bulk_rx_count;
  10067. + int bulk_aborted_count;
  10068. + uint64_t ctrl_tx_bytes;
  10069. + uint64_t ctrl_rx_bytes;
  10070. + uint64_t bulk_tx_bytes;
  10071. + uint64_t bulk_rx_bytes;
  10072. + } stats;
  10073. +} VCHIQ_SERVICE_T;
  10074. +
  10075. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  10076. + statically allocated, since for accounting reasons a service's slot
  10077. + usage is carried over between users of the same port number.
  10078. + */
  10079. +typedef struct vchiq_service_quota_struct {
  10080. + unsigned short slot_quota;
  10081. + unsigned short slot_use_count;
  10082. + unsigned short message_quota;
  10083. + unsigned short message_use_count;
  10084. + struct semaphore quota_event;
  10085. + int previous_tx_index;
  10086. +} VCHIQ_SERVICE_QUOTA_T;
  10087. +
  10088. +typedef struct vchiq_shared_state_struct {
  10089. +
  10090. + /* A non-zero value here indicates that the content is valid. */
  10091. + int initialised;
  10092. +
  10093. + /* The first and last (inclusive) slots allocated to the owner. */
  10094. + int slot_first;
  10095. + int slot_last;
  10096. +
  10097. + /* The slot allocated to synchronous messages from the owner. */
  10098. + int slot_sync;
  10099. +
  10100. + /* Signalling this event indicates that owner's slot handler thread
  10101. + ** should run. */
  10102. + REMOTE_EVENT_T trigger;
  10103. +
  10104. + /* Indicates the byte position within the stream where the next message
  10105. + ** will be written. The least significant bits are an index into the
  10106. + ** slot. The next bits are the index of the slot in slot_queue. */
  10107. + int tx_pos;
  10108. +
  10109. + /* This event should be signalled when a slot is recycled. */
  10110. + REMOTE_EVENT_T recycle;
  10111. +
  10112. + /* The slot_queue index where the next recycled slot will be written. */
  10113. + int slot_queue_recycle;
  10114. +
  10115. + /* This event should be signalled when a synchronous message is sent. */
  10116. + REMOTE_EVENT_T sync_trigger;
  10117. +
  10118. + /* This event should be signalled when a synchronous message has been
  10119. + ** released. */
  10120. + REMOTE_EVENT_T sync_release;
  10121. +
  10122. + /* A circular buffer of slot indexes. */
  10123. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  10124. +
  10125. + /* Debugging state */
  10126. + int debug[DEBUG_MAX];
  10127. +} VCHIQ_SHARED_STATE_T;
  10128. +
  10129. +typedef struct vchiq_slot_zero_struct {
  10130. + int magic;
  10131. + short version;
  10132. + short version_min;
  10133. + int slot_zero_size;
  10134. + int slot_size;
  10135. + int max_slots;
  10136. + int max_slots_per_side;
  10137. + int platform_data[2];
  10138. + VCHIQ_SHARED_STATE_T master;
  10139. + VCHIQ_SHARED_STATE_T slave;
  10140. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  10141. +} VCHIQ_SLOT_ZERO_T;
  10142. +
  10143. +struct vchiq_state_struct {
  10144. + int id;
  10145. + int initialised;
  10146. + VCHIQ_CONNSTATE_T conn_state;
  10147. + int is_master;
  10148. + short version_common;
  10149. +
  10150. + VCHIQ_SHARED_STATE_T *local;
  10151. + VCHIQ_SHARED_STATE_T *remote;
  10152. + VCHIQ_SLOT_T *slot_data;
  10153. +
  10154. + unsigned short default_slot_quota;
  10155. + unsigned short default_message_quota;
  10156. +
  10157. + /* Event indicating connect message received */
  10158. + struct semaphore connect;
  10159. +
  10160. + /* Mutex protecting services */
  10161. + struct mutex mutex;
  10162. + VCHIQ_INSTANCE_T *instance;
  10163. +
  10164. + /* Processes incoming messages */
  10165. + struct task_struct *slot_handler_thread;
  10166. +
  10167. + /* Processes recycled slots */
  10168. + struct task_struct *recycle_thread;
  10169. +
  10170. + /* Processes synchronous messages */
  10171. + struct task_struct *sync_thread;
  10172. +
  10173. + /* Local implementation of the trigger remote event */
  10174. + struct semaphore trigger_event;
  10175. +
  10176. + /* Local implementation of the recycle remote event */
  10177. + struct semaphore recycle_event;
  10178. +
  10179. + /* Local implementation of the sync trigger remote event */
  10180. + struct semaphore sync_trigger_event;
  10181. +
  10182. + /* Local implementation of the sync release remote event */
  10183. + struct semaphore sync_release_event;
  10184. +
  10185. + char *tx_data;
  10186. + char *rx_data;
  10187. + VCHIQ_SLOT_INFO_T *rx_info;
  10188. +
  10189. + struct mutex slot_mutex;
  10190. +
  10191. + struct mutex recycle_mutex;
  10192. +
  10193. + struct mutex sync_mutex;
  10194. +
  10195. + struct mutex bulk_transfer_mutex;
  10196. +
  10197. + /* Indicates the byte position within the stream from where the next
  10198. + ** message will be read. The least significant bits are an index into
  10199. + ** the slot.The next bits are the index of the slot in
  10200. + ** remote->slot_queue. */
  10201. + int rx_pos;
  10202. +
  10203. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  10204. + from remote->tx_pos. */
  10205. + int local_tx_pos;
  10206. +
  10207. + /* The slot_queue index of the slot to become available next. */
  10208. + int slot_queue_available;
  10209. +
  10210. + /* A flag to indicate if any poll has been requested */
  10211. + int poll_needed;
  10212. +
  10213. + /* Ths index of the previous slot used for data messages. */
  10214. + int previous_data_index;
  10215. +
  10216. + /* The number of slots occupied by data messages. */
  10217. + unsigned short data_use_count;
  10218. +
  10219. + /* The maximum number of slots to be occupied by data messages. */
  10220. + unsigned short data_quota;
  10221. +
  10222. + /* An array of bit sets indicating which services must be polled. */
  10223. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  10224. +
  10225. + /* The number of the first unused service */
  10226. + int unused_service;
  10227. +
  10228. + /* Signalled when a free slot becomes available. */
  10229. + struct semaphore slot_available_event;
  10230. +
  10231. + struct semaphore slot_remove_event;
  10232. +
  10233. + /* Signalled when a free data slot becomes available. */
  10234. + struct semaphore data_quota_event;
  10235. +
  10236. + /* Incremented when there are bulk transfers which cannot be processed
  10237. + * whilst paused and must be processed on resume */
  10238. + int deferred_bulks;
  10239. +
  10240. + struct state_stats_struct {
  10241. + int slot_stalls;
  10242. + int data_stalls;
  10243. + int ctrl_tx_count;
  10244. + int ctrl_rx_count;
  10245. + int error_count;
  10246. + } stats;
  10247. +
  10248. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  10249. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  10250. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  10251. +
  10252. + VCHIQ_PLATFORM_STATE_T platform_state;
  10253. +};
  10254. +
  10255. +struct bulk_waiter {
  10256. + VCHIQ_BULK_T *bulk;
  10257. + struct semaphore event;
  10258. + int actual;
  10259. +};
  10260. +
  10261. +extern spinlock_t bulk_waiter_spinlock;
  10262. +
  10263. +extern int vchiq_core_log_level;
  10264. +extern int vchiq_core_msg_log_level;
  10265. +extern int vchiq_sync_log_level;
  10266. +
  10267. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  10268. +
  10269. +extern const char *
  10270. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  10271. +
  10272. +extern VCHIQ_SLOT_ZERO_T *
  10273. +vchiq_init_slots(void *mem_base, int mem_size);
  10274. +
  10275. +extern VCHIQ_STATUS_T
  10276. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  10277. + int is_master);
  10278. +
  10279. +extern VCHIQ_STATUS_T
  10280. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10281. +
  10282. +extern VCHIQ_SERVICE_T *
  10283. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  10284. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  10285. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  10286. +
  10287. +extern VCHIQ_STATUS_T
  10288. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  10289. +
  10290. +extern VCHIQ_STATUS_T
  10291. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  10292. +
  10293. +extern void
  10294. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  10295. +
  10296. +extern void
  10297. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  10298. +
  10299. +extern VCHIQ_STATUS_T
  10300. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10301. +
  10302. +extern VCHIQ_STATUS_T
  10303. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  10304. +
  10305. +extern VCHIQ_STATUS_T
  10306. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  10307. +
  10308. +extern void
  10309. +remote_event_pollall(VCHIQ_STATE_T *state);
  10310. +
  10311. +extern VCHIQ_STATUS_T
  10312. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  10313. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  10314. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  10315. +
  10316. +extern void
  10317. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  10318. +
  10319. +extern void
  10320. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  10321. +
  10322. +extern void
  10323. +vchiq_loud_error_header(void);
  10324. +
  10325. +extern void
  10326. +vchiq_loud_error_footer(void);
  10327. +
  10328. +extern void
  10329. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  10330. +
  10331. +static inline VCHIQ_SERVICE_T *
  10332. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  10333. +{
  10334. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  10335. + (VCHIQ_MAX_STATES - 1)];
  10336. + if (!state)
  10337. + return NULL;
  10338. +
  10339. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  10340. +}
  10341. +
  10342. +extern VCHIQ_SERVICE_T *
  10343. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  10344. +
  10345. +extern VCHIQ_SERVICE_T *
  10346. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  10347. +
  10348. +extern VCHIQ_SERVICE_T *
  10349. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  10350. + VCHIQ_SERVICE_HANDLE_T handle);
  10351. +
  10352. +extern VCHIQ_SERVICE_T *
  10353. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  10354. + VCHIQ_SERVICE_HANDLE_T handle);
  10355. +
  10356. +extern VCHIQ_SERVICE_T *
  10357. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  10358. + int *pidx);
  10359. +
  10360. +extern void
  10361. +lock_service(VCHIQ_SERVICE_T *service);
  10362. +
  10363. +extern void
  10364. +unlock_service(VCHIQ_SERVICE_T *service);
  10365. +
  10366. +/* The following functions are called from vchiq_core, and external
  10367. +** implementations must be provided. */
  10368. +
  10369. +extern VCHIQ_STATUS_T
  10370. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  10371. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  10372. +
  10373. +extern void
  10374. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  10375. +
  10376. +extern void
  10377. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  10378. +
  10379. +extern VCHIQ_STATUS_T
  10380. +vchiq_copy_from_user(void *dst, const void *src, int size);
  10381. +
  10382. +extern void
  10383. +remote_event_signal(REMOTE_EVENT_T *event);
  10384. +
  10385. +void
  10386. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  10387. +
  10388. +extern void
  10389. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  10390. +
  10391. +extern VCHIQ_STATUS_T
  10392. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  10393. +
  10394. +extern void
  10395. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  10396. +
  10397. +extern void
  10398. +vchiq_dump(void *dump_context, const char *str, int len);
  10399. +
  10400. +extern void
  10401. +vchiq_dump_platform_state(void *dump_context);
  10402. +
  10403. +extern void
  10404. +vchiq_dump_platform_instances(void *dump_context);
  10405. +
  10406. +extern void
  10407. +vchiq_dump_platform_service_state(void *dump_context,
  10408. + VCHIQ_SERVICE_T *service);
  10409. +
  10410. +extern VCHIQ_STATUS_T
  10411. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  10412. +
  10413. +extern VCHIQ_STATUS_T
  10414. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  10415. +
  10416. +extern void
  10417. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  10418. +
  10419. +extern void
  10420. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  10421. +
  10422. +extern VCHIQ_STATUS_T
  10423. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  10424. +
  10425. +extern VCHIQ_STATUS_T
  10426. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  10427. +
  10428. +extern void
  10429. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  10430. +
  10431. +extern VCHIQ_STATUS_T
  10432. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  10433. +
  10434. +extern VCHIQ_STATUS_T
  10435. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  10436. +
  10437. +extern VCHIQ_STATUS_T
  10438. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  10439. +
  10440. +extern void
  10441. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  10442. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  10443. +
  10444. +extern void
  10445. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  10446. +
  10447. +extern void
  10448. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  10449. +
  10450. +
  10451. +extern void
  10452. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  10453. + size_t numBytes);
  10454. +
  10455. +#endif
  10456. --- /dev/null
  10457. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  10458. @@ -0,0 +1,383 @@
  10459. +/**
  10460. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10461. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10462. + *
  10463. + * Redistribution and use in source and binary forms, with or without
  10464. + * modification, are permitted provided that the following conditions
  10465. + * are met:
  10466. + * 1. Redistributions of source code must retain the above copyright
  10467. + * notice, this list of conditions, and the following disclaimer,
  10468. + * without modification.
  10469. + * 2. Redistributions in binary form must reproduce the above copyright
  10470. + * notice, this list of conditions and the following disclaimer in the
  10471. + * documentation and/or other materials provided with the distribution.
  10472. + * 3. The names of the above-listed copyright holders may not be used
  10473. + * to endorse or promote products derived from this software without
  10474. + * specific prior written permission.
  10475. + *
  10476. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10477. + * GNU General Public License ("GPL") version 2, as published by the Free
  10478. + * Software Foundation.
  10479. + *
  10480. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10481. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10482. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10483. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10484. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10485. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10486. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10487. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10488. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10489. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10490. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10491. + */
  10492. +
  10493. +
  10494. +#include <linux/debugfs.h>
  10495. +#include "vchiq_core.h"
  10496. +#include "vchiq_arm.h"
  10497. +#include "vchiq_debugfs.h"
  10498. +
  10499. +#ifdef CONFIG_DEBUG_FS
  10500. +
  10501. +/****************************************************************************
  10502. +*
  10503. +* log category entries
  10504. +*
  10505. +***************************************************************************/
  10506. +#define DEBUGFS_WRITE_BUF_SIZE 256
  10507. +
  10508. +#define VCHIQ_LOG_ERROR_STR "error"
  10509. +#define VCHIQ_LOG_WARNING_STR "warning"
  10510. +#define VCHIQ_LOG_INFO_STR "info"
  10511. +#define VCHIQ_LOG_TRACE_STR "trace"
  10512. +
  10513. +
  10514. +/* Top-level debug info */
  10515. +struct vchiq_debugfs_info {
  10516. + /* Global 'vchiq' debugfs entry used by all instances */
  10517. + struct dentry *vchiq_cfg_dir;
  10518. +
  10519. + /* one entry per client process */
  10520. + struct dentry *clients;
  10521. +
  10522. + /* log categories */
  10523. + struct dentry *log_categories;
  10524. +};
  10525. +
  10526. +static struct vchiq_debugfs_info debugfs_info;
  10527. +
  10528. +/* Log category debugfs entries */
  10529. +struct vchiq_debugfs_log_entry {
  10530. + const char *name;
  10531. + int *plevel;
  10532. + struct dentry *dir;
  10533. +};
  10534. +
  10535. +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
  10536. + { "core", &vchiq_core_log_level },
  10537. + { "msg", &vchiq_core_msg_log_level },
  10538. + { "sync", &vchiq_sync_log_level },
  10539. + { "susp", &vchiq_susp_log_level },
  10540. + { "arm", &vchiq_arm_log_level },
  10541. +};
  10542. +static int n_log_entries =
  10543. + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
  10544. +
  10545. +
  10546. +static struct dentry *vchiq_clients_top(void);
  10547. +static struct dentry *vchiq_debugfs_top(void);
  10548. +
  10549. +static int debugfs_log_show(struct seq_file *f, void *offset)
  10550. +{
  10551. + int *levp = f->private;
  10552. + char *log_value = NULL;
  10553. +
  10554. + switch (*levp) {
  10555. + case VCHIQ_LOG_ERROR:
  10556. + log_value = VCHIQ_LOG_ERROR_STR;
  10557. + break;
  10558. + case VCHIQ_LOG_WARNING:
  10559. + log_value = VCHIQ_LOG_WARNING_STR;
  10560. + break;
  10561. + case VCHIQ_LOG_INFO:
  10562. + log_value = VCHIQ_LOG_INFO_STR;
  10563. + break;
  10564. + case VCHIQ_LOG_TRACE:
  10565. + log_value = VCHIQ_LOG_TRACE_STR;
  10566. + break;
  10567. + default:
  10568. + break;
  10569. + }
  10570. +
  10571. + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
  10572. +
  10573. + return 0;
  10574. +}
  10575. +
  10576. +static int debugfs_log_open(struct inode *inode, struct file *file)
  10577. +{
  10578. + return single_open(file, debugfs_log_show, inode->i_private);
  10579. +}
  10580. +
  10581. +static int debugfs_log_write(struct file *file,
  10582. + const char __user *buffer,
  10583. + size_t count, loff_t *ppos)
  10584. +{
  10585. + struct seq_file *f = (struct seq_file *)file->private_data;
  10586. + int *levp = f->private;
  10587. + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
  10588. +
  10589. + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
  10590. + if (count >= DEBUGFS_WRITE_BUF_SIZE)
  10591. + count = DEBUGFS_WRITE_BUF_SIZE;
  10592. +
  10593. + if (copy_from_user(kbuf, buffer, count) != 0)
  10594. + return -EFAULT;
  10595. + kbuf[count - 1] = 0;
  10596. +
  10597. + if (strncmp("error", kbuf, strlen("error")) == 0)
  10598. + *levp = VCHIQ_LOG_ERROR;
  10599. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  10600. + *levp = VCHIQ_LOG_WARNING;
  10601. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  10602. + *levp = VCHIQ_LOG_INFO;
  10603. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  10604. + *levp = VCHIQ_LOG_TRACE;
  10605. + else
  10606. + *levp = VCHIQ_LOG_DEFAULT;
  10607. +
  10608. + *ppos += count;
  10609. +
  10610. + return count;
  10611. +}
  10612. +
  10613. +static const struct file_operations debugfs_log_fops = {
  10614. + .owner = THIS_MODULE,
  10615. + .open = debugfs_log_open,
  10616. + .write = debugfs_log_write,
  10617. + .read = seq_read,
  10618. + .llseek = seq_lseek,
  10619. + .release = single_release,
  10620. +};
  10621. +
  10622. +/* create an entry under <debugfs>/vchiq/log for each log category */
  10623. +static int vchiq_debugfs_create_log_entries(struct dentry *top)
  10624. +{
  10625. + struct dentry *dir;
  10626. + size_t i;
  10627. + int ret = 0;
  10628. + dir = debugfs_create_dir("log", vchiq_debugfs_top());
  10629. + if (!dir)
  10630. + return -ENOMEM;
  10631. + debugfs_info.log_categories = dir;
  10632. +
  10633. + for (i = 0; i < n_log_entries; i++) {
  10634. + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
  10635. + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
  10636. + 0644,
  10637. + debugfs_info.log_categories,
  10638. + levp,
  10639. + &debugfs_log_fops);
  10640. + if (!dir) {
  10641. + ret = -ENOMEM;
  10642. + break;
  10643. + }
  10644. +
  10645. + vchiq_debugfs_log_entries[i].dir = dir;
  10646. + }
  10647. + return ret;
  10648. +}
  10649. +
  10650. +static int debugfs_usecount_show(struct seq_file *f, void *offset)
  10651. +{
  10652. + VCHIQ_INSTANCE_T instance = f->private;
  10653. + int use_count;
  10654. +
  10655. + use_count = vchiq_instance_get_use_count(instance);
  10656. + seq_printf(f, "%d\n", use_count);
  10657. +
  10658. + return 0;
  10659. +}
  10660. +
  10661. +static int debugfs_usecount_open(struct inode *inode, struct file *file)
  10662. +{
  10663. + return single_open(file, debugfs_usecount_show, inode->i_private);
  10664. +}
  10665. +
  10666. +static const struct file_operations debugfs_usecount_fops = {
  10667. + .owner = THIS_MODULE,
  10668. + .open = debugfs_usecount_open,
  10669. + .read = seq_read,
  10670. + .llseek = seq_lseek,
  10671. + .release = single_release,
  10672. +};
  10673. +
  10674. +static int debugfs_trace_show(struct seq_file *f, void *offset)
  10675. +{
  10676. + VCHIQ_INSTANCE_T instance = f->private;
  10677. + int trace;
  10678. +
  10679. + trace = vchiq_instance_get_trace(instance);
  10680. + seq_printf(f, "%s\n", trace ? "Y" : "N");
  10681. +
  10682. + return 0;
  10683. +}
  10684. +
  10685. +static int debugfs_trace_open(struct inode *inode, struct file *file)
  10686. +{
  10687. + return single_open(file, debugfs_trace_show, inode->i_private);
  10688. +}
  10689. +
  10690. +static int debugfs_trace_write(struct file *file,
  10691. + const char __user *buffer,
  10692. + size_t count, loff_t *ppos)
  10693. +{
  10694. + struct seq_file *f = (struct seq_file *)file->private_data;
  10695. + VCHIQ_INSTANCE_T instance = f->private;
  10696. + char firstchar;
  10697. +
  10698. + if (copy_from_user(&firstchar, buffer, 1) != 0)
  10699. + return -EFAULT;
  10700. +
  10701. + switch (firstchar) {
  10702. + case 'Y':
  10703. + case 'y':
  10704. + case '1':
  10705. + vchiq_instance_set_trace(instance, 1);
  10706. + break;
  10707. + case 'N':
  10708. + case 'n':
  10709. + case '0':
  10710. + vchiq_instance_set_trace(instance, 0);
  10711. + break;
  10712. + default:
  10713. + break;
  10714. + }
  10715. +
  10716. + *ppos += count;
  10717. +
  10718. + return count;
  10719. +}
  10720. +
  10721. +static const struct file_operations debugfs_trace_fops = {
  10722. + .owner = THIS_MODULE,
  10723. + .open = debugfs_trace_open,
  10724. + .write = debugfs_trace_write,
  10725. + .read = seq_read,
  10726. + .llseek = seq_lseek,
  10727. + .release = single_release,
  10728. +};
  10729. +
  10730. +/* add an instance (process) to the debugfs entries */
  10731. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10732. +{
  10733. + char pidstr[16];
  10734. + struct dentry *top, *use_count, *trace;
  10735. + struct dentry *clients = vchiq_clients_top();
  10736. +
  10737. + snprintf(pidstr, sizeof(pidstr), "%d",
  10738. + vchiq_instance_get_pid(instance));
  10739. +
  10740. + top = debugfs_create_dir(pidstr, clients);
  10741. + if (!top)
  10742. + goto fail_top;
  10743. +
  10744. + use_count = debugfs_create_file("use_count",
  10745. + 0444, top,
  10746. + instance,
  10747. + &debugfs_usecount_fops);
  10748. + if (!use_count)
  10749. + goto fail_use_count;
  10750. +
  10751. + trace = debugfs_create_file("trace",
  10752. + 0644, top,
  10753. + instance,
  10754. + &debugfs_trace_fops);
  10755. + if (!trace)
  10756. + goto fail_trace;
  10757. +
  10758. + vchiq_instance_get_debugfs_node(instance)->dentry = top;
  10759. +
  10760. + return 0;
  10761. +
  10762. +fail_trace:
  10763. + debugfs_remove(use_count);
  10764. +fail_use_count:
  10765. + debugfs_remove(top);
  10766. +fail_top:
  10767. + return -ENOMEM;
  10768. +}
  10769. +
  10770. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10771. +{
  10772. + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
  10773. + debugfs_remove_recursive(node->dentry);
  10774. +}
  10775. +
  10776. +
  10777. +int vchiq_debugfs_init(void)
  10778. +{
  10779. + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
  10780. +
  10781. + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
  10782. + if (debugfs_info.vchiq_cfg_dir == NULL)
  10783. + goto fail;
  10784. +
  10785. + debugfs_info.clients = debugfs_create_dir("clients",
  10786. + vchiq_debugfs_top());
  10787. + if (!debugfs_info.clients)
  10788. + goto fail;
  10789. +
  10790. + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
  10791. + goto fail;
  10792. +
  10793. + return 0;
  10794. +
  10795. +fail:
  10796. + vchiq_debugfs_deinit();
  10797. + vchiq_log_error(vchiq_arm_log_level,
  10798. + "%s: failed to create debugfs directory",
  10799. + __func__);
  10800. +
  10801. + return -ENOMEM;
  10802. +}
  10803. +
  10804. +/* remove all the debugfs entries */
  10805. +void vchiq_debugfs_deinit(void)
  10806. +{
  10807. + debugfs_remove_recursive(vchiq_debugfs_top());
  10808. +}
  10809. +
  10810. +static struct dentry *vchiq_clients_top(void)
  10811. +{
  10812. + return debugfs_info.clients;
  10813. +}
  10814. +
  10815. +static struct dentry *vchiq_debugfs_top(void)
  10816. +{
  10817. + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
  10818. + return debugfs_info.vchiq_cfg_dir;
  10819. +}
  10820. +
  10821. +#else /* CONFIG_DEBUG_FS */
  10822. +
  10823. +int vchiq_debugfs_init(void)
  10824. +{
  10825. + return 0;
  10826. +}
  10827. +
  10828. +void vchiq_debugfs_deinit(void)
  10829. +{
  10830. +}
  10831. +
  10832. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10833. +{
  10834. + return 0;
  10835. +}
  10836. +
  10837. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10838. +{
  10839. +}
  10840. +
  10841. +#endif /* CONFIG_DEBUG_FS */
  10842. --- /dev/null
  10843. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  10844. @@ -0,0 +1,52 @@
  10845. +/**
  10846. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10847. + *
  10848. + * Redistribution and use in source and binary forms, with or without
  10849. + * modification, are permitted provided that the following conditions
  10850. + * are met:
  10851. + * 1. Redistributions of source code must retain the above copyright
  10852. + * notice, this list of conditions, and the following disclaimer,
  10853. + * without modification.
  10854. + * 2. Redistributions in binary form must reproduce the above copyright
  10855. + * notice, this list of conditions and the following disclaimer in the
  10856. + * documentation and/or other materials provided with the distribution.
  10857. + * 3. The names of the above-listed copyright holders may not be used
  10858. + * to endorse or promote products derived from this software without
  10859. + * specific prior written permission.
  10860. + *
  10861. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10862. + * GNU General Public License ("GPL") version 2, as published by the Free
  10863. + * Software Foundation.
  10864. + *
  10865. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10866. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10867. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10868. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10869. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10870. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10871. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10872. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10873. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10874. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10875. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10876. + */
  10877. +
  10878. +#ifndef VCHIQ_DEBUGFS_H
  10879. +#define VCHIQ_DEBUGFS_H
  10880. +
  10881. +#include "vchiq_core.h"
  10882. +
  10883. +typedef struct vchiq_debugfs_node_struct
  10884. +{
  10885. + struct dentry *dentry;
  10886. +} VCHIQ_DEBUGFS_NODE_T;
  10887. +
  10888. +int vchiq_debugfs_init(void);
  10889. +
  10890. +void vchiq_debugfs_deinit(void);
  10891. +
  10892. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
  10893. +
  10894. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
  10895. +
  10896. +#endif /* VCHIQ_DEBUGFS_H */
  10897. --- /dev/null
  10898. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  10899. @@ -0,0 +1,87 @@
  10900. +#!/usr/bin/perl -w
  10901. +
  10902. +use strict;
  10903. +
  10904. +#
  10905. +# Generate a version from available information
  10906. +#
  10907. +
  10908. +my $prefix = shift @ARGV;
  10909. +my $root = shift @ARGV;
  10910. +
  10911. +
  10912. +if ( not defined $root ) {
  10913. + die "usage: $0 prefix root-dir\n";
  10914. +}
  10915. +
  10916. +if ( ! -d $root ) {
  10917. + die "root directory $root not found\n";
  10918. +}
  10919. +
  10920. +my $version = "unknown";
  10921. +my $tainted = "";
  10922. +
  10923. +if ( -d "$root/.git" ) {
  10924. + # attempt to work out git version. only do so
  10925. + # on a linux build host, as cygwin builds are
  10926. + # already slow enough
  10927. +
  10928. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  10929. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  10930. + $version = "no git version";
  10931. + }
  10932. + else {
  10933. + $version = <F>;
  10934. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10935. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10936. + }
  10937. +
  10938. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  10939. + $tainted = <G>;
  10940. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10941. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10942. + if (length $tainted) {
  10943. + $version = join ' ', $version, "(tainted)";
  10944. + }
  10945. + else {
  10946. + $version = join ' ', $version, "(clean)";
  10947. + }
  10948. + }
  10949. + }
  10950. +}
  10951. +
  10952. +my $hostname = `hostname`;
  10953. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10954. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10955. +
  10956. +
  10957. +print STDERR "Version $version\n";
  10958. +print <<EOF;
  10959. +#include "${prefix}_build_info.h"
  10960. +#include <linux/broadcom/vc_debug_sym.h>
  10961. +
  10962. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  10963. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  10964. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  10965. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  10966. +
  10967. +const char *vchiq_get_build_hostname( void )
  10968. +{
  10969. + return vchiq_build_hostname;
  10970. +}
  10971. +
  10972. +const char *vchiq_get_build_version( void )
  10973. +{
  10974. + return vchiq_build_version;
  10975. +}
  10976. +
  10977. +const char *vchiq_get_build_date( void )
  10978. +{
  10979. + return vchiq_build_date;
  10980. +}
  10981. +
  10982. +const char *vchiq_get_build_time( void )
  10983. +{
  10984. + return vchiq_build_time;
  10985. +}
  10986. +EOF
  10987. --- /dev/null
  10988. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  10989. @@ -0,0 +1,189 @@
  10990. +/**
  10991. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10992. + *
  10993. + * Redistribution and use in source and binary forms, with or without
  10994. + * modification, are permitted provided that the following conditions
  10995. + * are met:
  10996. + * 1. Redistributions of source code must retain the above copyright
  10997. + * notice, this list of conditions, and the following disclaimer,
  10998. + * without modification.
  10999. + * 2. Redistributions in binary form must reproduce the above copyright
  11000. + * notice, this list of conditions and the following disclaimer in the
  11001. + * documentation and/or other materials provided with the distribution.
  11002. + * 3. The names of the above-listed copyright holders may not be used
  11003. + * to endorse or promote products derived from this software without
  11004. + * specific prior written permission.
  11005. + *
  11006. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11007. + * GNU General Public License ("GPL") version 2, as published by the Free
  11008. + * Software Foundation.
  11009. + *
  11010. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11011. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11012. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11013. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11014. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11015. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11016. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11017. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11018. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11019. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11020. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11021. + */
  11022. +
  11023. +#ifndef VCHIQ_IF_H
  11024. +#define VCHIQ_IF_H
  11025. +
  11026. +#include "interface/vchi/vchi_mh.h"
  11027. +
  11028. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  11029. +
  11030. +#define VCHIQ_SLOT_SIZE 4096
  11031. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  11032. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  11033. +
  11034. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  11035. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  11036. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  11037. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  11038. +
  11039. +typedef enum {
  11040. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  11041. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  11042. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  11043. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  11044. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  11045. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  11046. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  11047. +} VCHIQ_REASON_T;
  11048. +
  11049. +typedef enum {
  11050. + VCHIQ_ERROR = -1,
  11051. + VCHIQ_SUCCESS = 0,
  11052. + VCHIQ_RETRY = 1
  11053. +} VCHIQ_STATUS_T;
  11054. +
  11055. +typedef enum {
  11056. + VCHIQ_BULK_MODE_CALLBACK,
  11057. + VCHIQ_BULK_MODE_BLOCKING,
  11058. + VCHIQ_BULK_MODE_NOCALLBACK,
  11059. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  11060. +} VCHIQ_BULK_MODE_T;
  11061. +
  11062. +typedef enum {
  11063. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  11064. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  11065. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  11066. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
  11067. + VCHIQ_SERVICE_OPTION_TRACE
  11068. +} VCHIQ_SERVICE_OPTION_T;
  11069. +
  11070. +typedef struct vchiq_header_struct {
  11071. + /* The message identifier - opaque to applications. */
  11072. + int msgid;
  11073. +
  11074. + /* Size of message data. */
  11075. + unsigned int size;
  11076. +
  11077. + char data[0]; /* message */
  11078. +} VCHIQ_HEADER_T;
  11079. +
  11080. +typedef struct {
  11081. + const void *data;
  11082. + unsigned int size;
  11083. +} VCHIQ_ELEMENT_T;
  11084. +
  11085. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  11086. +
  11087. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  11088. + VCHIQ_SERVICE_HANDLE_T, void *);
  11089. +
  11090. +typedef struct vchiq_service_base_struct {
  11091. + int fourcc;
  11092. + VCHIQ_CALLBACK_T callback;
  11093. + void *userdata;
  11094. +} VCHIQ_SERVICE_BASE_T;
  11095. +
  11096. +typedef struct vchiq_service_params_struct {
  11097. + int fourcc;
  11098. + VCHIQ_CALLBACK_T callback;
  11099. + void *userdata;
  11100. + short version; /* Increment for non-trivial changes */
  11101. + short version_min; /* Update for incompatible changes */
  11102. +} VCHIQ_SERVICE_PARAMS_T;
  11103. +
  11104. +typedef struct vchiq_config_struct {
  11105. + unsigned int max_msg_size;
  11106. + unsigned int bulk_threshold; /* The message size above which it
  11107. + is better to use a bulk transfer
  11108. + (<= max_msg_size) */
  11109. + unsigned int max_outstanding_bulks;
  11110. + unsigned int max_services;
  11111. + short version; /* The version of VCHIQ */
  11112. + short version_min; /* The minimum compatible version of VCHIQ */
  11113. +} VCHIQ_CONFIG_T;
  11114. +
  11115. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  11116. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  11117. +
  11118. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  11119. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  11120. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  11121. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  11122. + const VCHIQ_SERVICE_PARAMS_T *params,
  11123. + VCHIQ_SERVICE_HANDLE_T *pservice);
  11124. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  11125. + const VCHIQ_SERVICE_PARAMS_T *params,
  11126. + VCHIQ_SERVICE_HANDLE_T *pservice);
  11127. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  11128. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  11129. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  11130. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  11131. + VCHIQ_SERVICE_HANDLE_T service);
  11132. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  11133. +
  11134. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  11135. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  11136. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  11137. + VCHIQ_HEADER_T *header);
  11138. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  11139. + const void *data, unsigned int size, void *userdata);
  11140. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  11141. + void *data, unsigned int size, void *userdata);
  11142. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  11143. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  11144. + const void *offset, unsigned int size, void *userdata);
  11145. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  11146. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  11147. + void *offset, unsigned int size, void *userdata);
  11148. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  11149. + const void *data, unsigned int size, void *userdata,
  11150. + VCHIQ_BULK_MODE_T mode);
  11151. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  11152. + void *data, unsigned int size, void *userdata,
  11153. + VCHIQ_BULK_MODE_T mode);
  11154. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  11155. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  11156. + void *userdata, VCHIQ_BULK_MODE_T mode);
  11157. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  11158. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  11159. + void *userdata, VCHIQ_BULK_MODE_T mode);
  11160. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  11161. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  11162. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  11163. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  11164. + int config_size, VCHIQ_CONFIG_T *pconfig);
  11165. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  11166. + VCHIQ_SERVICE_OPTION_T option, int value);
  11167. +
  11168. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  11169. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  11170. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  11171. +
  11172. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  11173. + void *ptr, size_t num_bytes);
  11174. +
  11175. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  11176. + short *peer_version);
  11177. +
  11178. +#endif /* VCHIQ_IF_H */
  11179. --- /dev/null
  11180. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  11181. @@ -0,0 +1,131 @@
  11182. +/**
  11183. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11184. + *
  11185. + * Redistribution and use in source and binary forms, with or without
  11186. + * modification, are permitted provided that the following conditions
  11187. + * are met:
  11188. + * 1. Redistributions of source code must retain the above copyright
  11189. + * notice, this list of conditions, and the following disclaimer,
  11190. + * without modification.
  11191. + * 2. Redistributions in binary form must reproduce the above copyright
  11192. + * notice, this list of conditions and the following disclaimer in the
  11193. + * documentation and/or other materials provided with the distribution.
  11194. + * 3. The names of the above-listed copyright holders may not be used
  11195. + * to endorse or promote products derived from this software without
  11196. + * specific prior written permission.
  11197. + *
  11198. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11199. + * GNU General Public License ("GPL") version 2, as published by the Free
  11200. + * Software Foundation.
  11201. + *
  11202. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11203. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11204. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11205. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11206. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11207. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11208. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11209. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11210. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11211. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11212. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11213. + */
  11214. +
  11215. +#ifndef VCHIQ_IOCTLS_H
  11216. +#define VCHIQ_IOCTLS_H
  11217. +
  11218. +#include <linux/ioctl.h>
  11219. +#include "vchiq_if.h"
  11220. +
  11221. +#define VCHIQ_IOC_MAGIC 0xc4
  11222. +#define VCHIQ_INVALID_HANDLE (~0)
  11223. +
  11224. +typedef struct {
  11225. + VCHIQ_SERVICE_PARAMS_T params;
  11226. + int is_open;
  11227. + int is_vchi;
  11228. + unsigned int handle; /* OUT */
  11229. +} VCHIQ_CREATE_SERVICE_T;
  11230. +
  11231. +typedef struct {
  11232. + unsigned int handle;
  11233. + unsigned int count;
  11234. + const VCHIQ_ELEMENT_T *elements;
  11235. +} VCHIQ_QUEUE_MESSAGE_T;
  11236. +
  11237. +typedef struct {
  11238. + unsigned int handle;
  11239. + void *data;
  11240. + unsigned int size;
  11241. + void *userdata;
  11242. + VCHIQ_BULK_MODE_T mode;
  11243. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  11244. +
  11245. +typedef struct {
  11246. + VCHIQ_REASON_T reason;
  11247. + VCHIQ_HEADER_T *header;
  11248. + void *service_userdata;
  11249. + void *bulk_userdata;
  11250. +} VCHIQ_COMPLETION_DATA_T;
  11251. +
  11252. +typedef struct {
  11253. + unsigned int count;
  11254. + VCHIQ_COMPLETION_DATA_T *buf;
  11255. + unsigned int msgbufsize;
  11256. + unsigned int msgbufcount; /* IN/OUT */
  11257. + void **msgbufs;
  11258. +} VCHIQ_AWAIT_COMPLETION_T;
  11259. +
  11260. +typedef struct {
  11261. + unsigned int handle;
  11262. + int blocking;
  11263. + unsigned int bufsize;
  11264. + void *buf;
  11265. +} VCHIQ_DEQUEUE_MESSAGE_T;
  11266. +
  11267. +typedef struct {
  11268. + unsigned int config_size;
  11269. + VCHIQ_CONFIG_T *pconfig;
  11270. +} VCHIQ_GET_CONFIG_T;
  11271. +
  11272. +typedef struct {
  11273. + unsigned int handle;
  11274. + VCHIQ_SERVICE_OPTION_T option;
  11275. + int value;
  11276. +} VCHIQ_SET_SERVICE_OPTION_T;
  11277. +
  11278. +typedef struct {
  11279. + void *virt_addr;
  11280. + size_t num_bytes;
  11281. +} VCHIQ_DUMP_MEM_T;
  11282. +
  11283. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  11284. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  11285. +#define VCHIQ_IOC_CREATE_SERVICE \
  11286. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  11287. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  11288. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  11289. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  11290. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  11291. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11292. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  11293. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11294. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  11295. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  11296. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  11297. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  11298. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  11299. +#define VCHIQ_IOC_GET_CONFIG \
  11300. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  11301. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  11302. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  11303. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  11304. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  11305. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  11306. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  11307. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  11308. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  11309. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  11310. +#define VCHIQ_IOC_MAX 17
  11311. +
  11312. +#endif
  11313. --- /dev/null
  11314. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  11315. @@ -0,0 +1,458 @@
  11316. +/**
  11317. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11318. + *
  11319. + * Redistribution and use in source and binary forms, with or without
  11320. + * modification, are permitted provided that the following conditions
  11321. + * are met:
  11322. + * 1. Redistributions of source code must retain the above copyright
  11323. + * notice, this list of conditions, and the following disclaimer,
  11324. + * without modification.
  11325. + * 2. Redistributions in binary form must reproduce the above copyright
  11326. + * notice, this list of conditions and the following disclaimer in the
  11327. + * documentation and/or other materials provided with the distribution.
  11328. + * 3. The names of the above-listed copyright holders may not be used
  11329. + * to endorse or promote products derived from this software without
  11330. + * specific prior written permission.
  11331. + *
  11332. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11333. + * GNU General Public License ("GPL") version 2, as published by the Free
  11334. + * Software Foundation.
  11335. + *
  11336. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11337. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11338. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11339. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11340. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11341. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11342. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11343. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11344. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11345. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11346. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11347. + */
  11348. +
  11349. +/* ---- Include Files ---------------------------------------------------- */
  11350. +
  11351. +#include <linux/kernel.h>
  11352. +#include <linux/module.h>
  11353. +#include <linux/mutex.h>
  11354. +
  11355. +#include "vchiq_core.h"
  11356. +#include "vchiq_arm.h"
  11357. +#include "vchiq_killable.h"
  11358. +
  11359. +/* ---- Public Variables ------------------------------------------------- */
  11360. +
  11361. +/* ---- Private Constants and Types -------------------------------------- */
  11362. +
  11363. +struct bulk_waiter_node {
  11364. + struct bulk_waiter bulk_waiter;
  11365. + int pid;
  11366. + struct list_head list;
  11367. +};
  11368. +
  11369. +struct vchiq_instance_struct {
  11370. + VCHIQ_STATE_T *state;
  11371. +
  11372. + int connected;
  11373. +
  11374. + struct list_head bulk_waiter_list;
  11375. + struct mutex bulk_waiter_list_mutex;
  11376. +};
  11377. +
  11378. +static VCHIQ_STATUS_T
  11379. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11380. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  11381. +
  11382. +/****************************************************************************
  11383. +*
  11384. +* vchiq_initialise
  11385. +*
  11386. +***************************************************************************/
  11387. +#define VCHIQ_INIT_RETRIES 10
  11388. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  11389. +{
  11390. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11391. + VCHIQ_STATE_T *state;
  11392. + VCHIQ_INSTANCE_T instance = NULL;
  11393. + int i;
  11394. +
  11395. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  11396. +
  11397. + /* VideoCore may not be ready due to boot up timing.
  11398. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  11399. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  11400. + state = vchiq_get_state();
  11401. + if (state)
  11402. + break;
  11403. + udelay(500);
  11404. + }
  11405. + if (i==VCHIQ_INIT_RETRIES) {
  11406. + vchiq_log_error(vchiq_core_log_level,
  11407. + "%s: videocore not initialized\n", __func__);
  11408. + goto failed;
  11409. + } else if (i>0) {
  11410. + vchiq_log_warning(vchiq_core_log_level,
  11411. + "%s: videocore initialized after %d retries\n", __func__, i);
  11412. + }
  11413. +
  11414. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  11415. + if (!instance) {
  11416. + vchiq_log_error(vchiq_core_log_level,
  11417. + "%s: error allocating vchiq instance\n", __func__);
  11418. + goto failed;
  11419. + }
  11420. +
  11421. + instance->connected = 0;
  11422. + instance->state = state;
  11423. + mutex_init(&instance->bulk_waiter_list_mutex);
  11424. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  11425. +
  11426. + *instanceOut = instance;
  11427. +
  11428. + status = VCHIQ_SUCCESS;
  11429. +
  11430. +failed:
  11431. + vchiq_log_trace(vchiq_core_log_level,
  11432. + "%s(%p): returning %d", __func__, instance, status);
  11433. +
  11434. + return status;
  11435. +}
  11436. +EXPORT_SYMBOL(vchiq_initialise);
  11437. +
  11438. +/****************************************************************************
  11439. +*
  11440. +* vchiq_shutdown
  11441. +*
  11442. +***************************************************************************/
  11443. +
  11444. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  11445. +{
  11446. + VCHIQ_STATUS_T status;
  11447. + VCHIQ_STATE_T *state = instance->state;
  11448. +
  11449. + vchiq_log_trace(vchiq_core_log_level,
  11450. + "%s(%p) called", __func__, instance);
  11451. +
  11452. + if (mutex_lock_interruptible(&state->mutex) != 0)
  11453. + return VCHIQ_RETRY;
  11454. +
  11455. + /* Remove all services */
  11456. + status = vchiq_shutdown_internal(state, instance);
  11457. +
  11458. + mutex_unlock(&state->mutex);
  11459. +
  11460. + vchiq_log_trace(vchiq_core_log_level,
  11461. + "%s(%p): returning %d", __func__, instance, status);
  11462. +
  11463. + if (status == VCHIQ_SUCCESS) {
  11464. + struct list_head *pos, *next;
  11465. + list_for_each_safe(pos, next,
  11466. + &instance->bulk_waiter_list) {
  11467. + struct bulk_waiter_node *waiter;
  11468. + waiter = list_entry(pos,
  11469. + struct bulk_waiter_node,
  11470. + list);
  11471. + list_del(pos);
  11472. + vchiq_log_info(vchiq_arm_log_level,
  11473. + "bulk_waiter - cleaned up %x "
  11474. + "for pid %d",
  11475. + (unsigned int)waiter, waiter->pid);
  11476. + kfree(waiter);
  11477. + }
  11478. + kfree(instance);
  11479. + }
  11480. +
  11481. + return status;
  11482. +}
  11483. +EXPORT_SYMBOL(vchiq_shutdown);
  11484. +
  11485. +/****************************************************************************
  11486. +*
  11487. +* vchiq_is_connected
  11488. +*
  11489. +***************************************************************************/
  11490. +
  11491. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  11492. +{
  11493. + return instance->connected;
  11494. +}
  11495. +
  11496. +/****************************************************************************
  11497. +*
  11498. +* vchiq_connect
  11499. +*
  11500. +***************************************************************************/
  11501. +
  11502. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  11503. +{
  11504. + VCHIQ_STATUS_T status;
  11505. + VCHIQ_STATE_T *state = instance->state;
  11506. +
  11507. + vchiq_log_trace(vchiq_core_log_level,
  11508. + "%s(%p) called", __func__, instance);
  11509. +
  11510. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  11511. + vchiq_log_trace(vchiq_core_log_level,
  11512. + "%s: call to mutex_lock failed", __func__);
  11513. + status = VCHIQ_RETRY;
  11514. + goto failed;
  11515. + }
  11516. + status = vchiq_connect_internal(state, instance);
  11517. +
  11518. + if (status == VCHIQ_SUCCESS)
  11519. + instance->connected = 1;
  11520. +
  11521. + mutex_unlock(&state->mutex);
  11522. +
  11523. +failed:
  11524. + vchiq_log_trace(vchiq_core_log_level,
  11525. + "%s(%p): returning %d", __func__, instance, status);
  11526. +
  11527. + return status;
  11528. +}
  11529. +EXPORT_SYMBOL(vchiq_connect);
  11530. +
  11531. +/****************************************************************************
  11532. +*
  11533. +* vchiq_add_service
  11534. +*
  11535. +***************************************************************************/
  11536. +
  11537. +VCHIQ_STATUS_T vchiq_add_service(
  11538. + VCHIQ_INSTANCE_T instance,
  11539. + const VCHIQ_SERVICE_PARAMS_T *params,
  11540. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11541. +{
  11542. + VCHIQ_STATUS_T status;
  11543. + VCHIQ_STATE_T *state = instance->state;
  11544. + VCHIQ_SERVICE_T *service = NULL;
  11545. + int srvstate;
  11546. +
  11547. + vchiq_log_trace(vchiq_core_log_level,
  11548. + "%s(%p) called", __func__, instance);
  11549. +
  11550. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11551. +
  11552. + srvstate = vchiq_is_connected(instance)
  11553. + ? VCHIQ_SRVSTATE_LISTENING
  11554. + : VCHIQ_SRVSTATE_HIDDEN;
  11555. +
  11556. + service = vchiq_add_service_internal(
  11557. + state,
  11558. + params,
  11559. + srvstate,
  11560. + instance,
  11561. + NULL);
  11562. +
  11563. + if (service) {
  11564. + *phandle = service->handle;
  11565. + status = VCHIQ_SUCCESS;
  11566. + } else
  11567. + status = VCHIQ_ERROR;
  11568. +
  11569. + vchiq_log_trace(vchiq_core_log_level,
  11570. + "%s(%p): returning %d", __func__, instance, status);
  11571. +
  11572. + return status;
  11573. +}
  11574. +EXPORT_SYMBOL(vchiq_add_service);
  11575. +
  11576. +/****************************************************************************
  11577. +*
  11578. +* vchiq_open_service
  11579. +*
  11580. +***************************************************************************/
  11581. +
  11582. +VCHIQ_STATUS_T vchiq_open_service(
  11583. + VCHIQ_INSTANCE_T instance,
  11584. + const VCHIQ_SERVICE_PARAMS_T *params,
  11585. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11586. +{
  11587. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11588. + VCHIQ_STATE_T *state = instance->state;
  11589. + VCHIQ_SERVICE_T *service = NULL;
  11590. +
  11591. + vchiq_log_trace(vchiq_core_log_level,
  11592. + "%s(%p) called", __func__, instance);
  11593. +
  11594. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11595. +
  11596. + if (!vchiq_is_connected(instance))
  11597. + goto failed;
  11598. +
  11599. + service = vchiq_add_service_internal(state,
  11600. + params,
  11601. + VCHIQ_SRVSTATE_OPENING,
  11602. + instance,
  11603. + NULL);
  11604. +
  11605. + if (service) {
  11606. + *phandle = service->handle;
  11607. + status = vchiq_open_service_internal(service, current->pid);
  11608. + if (status != VCHIQ_SUCCESS) {
  11609. + vchiq_remove_service(service->handle);
  11610. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11611. + }
  11612. + }
  11613. +
  11614. +failed:
  11615. + vchiq_log_trace(vchiq_core_log_level,
  11616. + "%s(%p): returning %d", __func__, instance, status);
  11617. +
  11618. + return status;
  11619. +}
  11620. +EXPORT_SYMBOL(vchiq_open_service);
  11621. +
  11622. +VCHIQ_STATUS_T
  11623. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  11624. + const void *data, unsigned int size, void *userdata)
  11625. +{
  11626. + return vchiq_bulk_transfer(handle,
  11627. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11628. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  11629. +}
  11630. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  11631. +
  11632. +VCHIQ_STATUS_T
  11633. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11634. + unsigned int size, void *userdata)
  11635. +{
  11636. + return vchiq_bulk_transfer(handle,
  11637. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11638. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  11639. +}
  11640. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  11641. +
  11642. +VCHIQ_STATUS_T
  11643. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  11644. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11645. +{
  11646. + VCHIQ_STATUS_T status;
  11647. +
  11648. + switch (mode) {
  11649. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11650. + case VCHIQ_BULK_MODE_CALLBACK:
  11651. + status = vchiq_bulk_transfer(handle,
  11652. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11653. + mode, VCHIQ_BULK_TRANSMIT);
  11654. + break;
  11655. + case VCHIQ_BULK_MODE_BLOCKING:
  11656. + status = vchiq_blocking_bulk_transfer(handle,
  11657. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  11658. + break;
  11659. + default:
  11660. + return VCHIQ_ERROR;
  11661. + }
  11662. +
  11663. + return status;
  11664. +}
  11665. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  11666. +
  11667. +VCHIQ_STATUS_T
  11668. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11669. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11670. +{
  11671. + VCHIQ_STATUS_T status;
  11672. +
  11673. + switch (mode) {
  11674. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11675. + case VCHIQ_BULK_MODE_CALLBACK:
  11676. + status = vchiq_bulk_transfer(handle,
  11677. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11678. + mode, VCHIQ_BULK_RECEIVE);
  11679. + break;
  11680. + case VCHIQ_BULK_MODE_BLOCKING:
  11681. + status = vchiq_blocking_bulk_transfer(handle,
  11682. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  11683. + break;
  11684. + default:
  11685. + return VCHIQ_ERROR;
  11686. + }
  11687. +
  11688. + return status;
  11689. +}
  11690. +EXPORT_SYMBOL(vchiq_bulk_receive);
  11691. +
  11692. +static VCHIQ_STATUS_T
  11693. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11694. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  11695. +{
  11696. + VCHIQ_INSTANCE_T instance;
  11697. + VCHIQ_SERVICE_T *service;
  11698. + VCHIQ_STATUS_T status;
  11699. + struct bulk_waiter_node *waiter = NULL;
  11700. + struct list_head *pos;
  11701. +
  11702. + service = find_service_by_handle(handle);
  11703. + if (!service)
  11704. + return VCHIQ_ERROR;
  11705. +
  11706. + instance = service->instance;
  11707. +
  11708. + unlock_service(service);
  11709. +
  11710. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11711. + list_for_each(pos, &instance->bulk_waiter_list) {
  11712. + if (list_entry(pos, struct bulk_waiter_node,
  11713. + list)->pid == current->pid) {
  11714. + waiter = list_entry(pos,
  11715. + struct bulk_waiter_node,
  11716. + list);
  11717. + list_del(pos);
  11718. + break;
  11719. + }
  11720. + }
  11721. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11722. +
  11723. + if (waiter) {
  11724. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11725. + if (bulk) {
  11726. + /* This thread has an outstanding bulk transfer. */
  11727. + if ((bulk->data != data) ||
  11728. + (bulk->size != size)) {
  11729. + /* This is not a retry of the previous one.
  11730. + ** Cancel the signal when the transfer
  11731. + ** completes. */
  11732. + spin_lock(&bulk_waiter_spinlock);
  11733. + bulk->userdata = NULL;
  11734. + spin_unlock(&bulk_waiter_spinlock);
  11735. + }
  11736. + }
  11737. + }
  11738. +
  11739. + if (!waiter) {
  11740. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  11741. + if (!waiter) {
  11742. + vchiq_log_error(vchiq_core_log_level,
  11743. + "%s - out of memory", __func__);
  11744. + return VCHIQ_ERROR;
  11745. + }
  11746. + }
  11747. +
  11748. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  11749. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  11750. + dir);
  11751. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  11752. + !waiter->bulk_waiter.bulk) {
  11753. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11754. + if (bulk) {
  11755. + /* Cancel the signal when the transfer
  11756. + ** completes. */
  11757. + spin_lock(&bulk_waiter_spinlock);
  11758. + bulk->userdata = NULL;
  11759. + spin_unlock(&bulk_waiter_spinlock);
  11760. + }
  11761. + kfree(waiter);
  11762. + } else {
  11763. + waiter->pid = current->pid;
  11764. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11765. + list_add(&waiter->list, &instance->bulk_waiter_list);
  11766. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11767. + vchiq_log_info(vchiq_arm_log_level,
  11768. + "saved bulk_waiter %x for pid %d",
  11769. + (unsigned int)waiter, current->pid);
  11770. + }
  11771. +
  11772. + return status;
  11773. +}
  11774. --- /dev/null
  11775. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  11776. @@ -0,0 +1,69 @@
  11777. +/**
  11778. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11779. + *
  11780. + * Redistribution and use in source and binary forms, with or without
  11781. + * modification, are permitted provided that the following conditions
  11782. + * are met:
  11783. + * 1. Redistributions of source code must retain the above copyright
  11784. + * notice, this list of conditions, and the following disclaimer,
  11785. + * without modification.
  11786. + * 2. Redistributions in binary form must reproduce the above copyright
  11787. + * notice, this list of conditions and the following disclaimer in the
  11788. + * documentation and/or other materials provided with the distribution.
  11789. + * 3. The names of the above-listed copyright holders may not be used
  11790. + * to endorse or promote products derived from this software without
  11791. + * specific prior written permission.
  11792. + *
  11793. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11794. + * GNU General Public License ("GPL") version 2, as published by the Free
  11795. + * Software Foundation.
  11796. + *
  11797. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11798. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11799. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11800. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11801. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11802. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11803. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11804. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11805. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11806. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11807. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11808. + */
  11809. +
  11810. +#ifndef VCHIQ_KILLABLE_H
  11811. +#define VCHIQ_KILLABLE_H
  11812. +
  11813. +#include <linux/mutex.h>
  11814. +#include <linux/semaphore.h>
  11815. +
  11816. +#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTRAP) | sigmask(SIGSTOP) | sigmask(SIGCONT))
  11817. +
  11818. +static inline int __must_check down_interruptible_killable(struct semaphore *sem)
  11819. +{
  11820. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  11821. + int ret;
  11822. + sigset_t blocked, oldset;
  11823. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  11824. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  11825. + ret = down_interruptible(sem);
  11826. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  11827. + return ret;
  11828. +}
  11829. +#define down_interruptible down_interruptible_killable
  11830. +
  11831. +
  11832. +static inline int __must_check mutex_lock_interruptible_killable(struct mutex *lock)
  11833. +{
  11834. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  11835. + int ret;
  11836. + sigset_t blocked, oldset;
  11837. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  11838. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  11839. + ret = mutex_lock_interruptible(lock);
  11840. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  11841. + return ret;
  11842. +}
  11843. +#define mutex_lock_interruptible mutex_lock_interruptible_killable
  11844. +
  11845. +#endif
  11846. --- /dev/null
  11847. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  11848. @@ -0,0 +1,71 @@
  11849. +/**
  11850. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11851. + *
  11852. + * Redistribution and use in source and binary forms, with or without
  11853. + * modification, are permitted provided that the following conditions
  11854. + * are met:
  11855. + * 1. Redistributions of source code must retain the above copyright
  11856. + * notice, this list of conditions, and the following disclaimer,
  11857. + * without modification.
  11858. + * 2. Redistributions in binary form must reproduce the above copyright
  11859. + * notice, this list of conditions and the following disclaimer in the
  11860. + * documentation and/or other materials provided with the distribution.
  11861. + * 3. The names of the above-listed copyright holders may not be used
  11862. + * to endorse or promote products derived from this software without
  11863. + * specific prior written permission.
  11864. + *
  11865. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11866. + * GNU General Public License ("GPL") version 2, as published by the Free
  11867. + * Software Foundation.
  11868. + *
  11869. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11870. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11871. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11872. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11873. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11874. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11875. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11876. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11877. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11878. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11879. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11880. + */
  11881. +
  11882. +#ifndef VCHIQ_MEMDRV_H
  11883. +#define VCHIQ_MEMDRV_H
  11884. +
  11885. +/* ---- Include Files ----------------------------------------------------- */
  11886. +
  11887. +#include <linux/kernel.h>
  11888. +#include "vchiq_if.h"
  11889. +
  11890. +/* ---- Constants and Types ---------------------------------------------- */
  11891. +
  11892. +typedef struct {
  11893. + void *armSharedMemVirt;
  11894. + dma_addr_t armSharedMemPhys;
  11895. + size_t armSharedMemSize;
  11896. +
  11897. + void *vcSharedMemVirt;
  11898. + dma_addr_t vcSharedMemPhys;
  11899. + size_t vcSharedMemSize;
  11900. +} VCHIQ_SHARED_MEM_INFO_T;
  11901. +
  11902. +/* ---- Variable Externs ------------------------------------------------- */
  11903. +
  11904. +/* ---- Function Prototypes ---------------------------------------------- */
  11905. +
  11906. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  11907. +
  11908. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  11909. +
  11910. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  11911. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11912. +
  11913. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  11914. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11915. +
  11916. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  11917. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11918. +
  11919. +#endif
  11920. --- /dev/null
  11921. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  11922. @@ -0,0 +1,58 @@
  11923. +/**
  11924. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11925. + *
  11926. + * Redistribution and use in source and binary forms, with or without
  11927. + * modification, are permitted provided that the following conditions
  11928. + * are met:
  11929. + * 1. Redistributions of source code must retain the above copyright
  11930. + * notice, this list of conditions, and the following disclaimer,
  11931. + * without modification.
  11932. + * 2. Redistributions in binary form must reproduce the above copyright
  11933. + * notice, this list of conditions and the following disclaimer in the
  11934. + * documentation and/or other materials provided with the distribution.
  11935. + * 3. The names of the above-listed copyright holders may not be used
  11936. + * to endorse or promote products derived from this software without
  11937. + * specific prior written permission.
  11938. + *
  11939. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11940. + * GNU General Public License ("GPL") version 2, as published by the Free
  11941. + * Software Foundation.
  11942. + *
  11943. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11944. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11945. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11946. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11947. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11948. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11949. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11950. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11951. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11952. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11953. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11954. + */
  11955. +
  11956. +#ifndef VCHIQ_PAGELIST_H
  11957. +#define VCHIQ_PAGELIST_H
  11958. +
  11959. +#ifndef PAGE_SIZE
  11960. +#define PAGE_SIZE 4096
  11961. +#endif
  11962. +#define CACHE_LINE_SIZE 32
  11963. +#define PAGELIST_WRITE 0
  11964. +#define PAGELIST_READ 1
  11965. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  11966. +
  11967. +typedef struct pagelist_struct {
  11968. + unsigned long length;
  11969. + unsigned short type;
  11970. + unsigned short offset;
  11971. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  11972. + pages at consecutive addresses. */
  11973. +} PAGELIST_T;
  11974. +
  11975. +typedef struct fragments_struct {
  11976. + char headbuf[CACHE_LINE_SIZE];
  11977. + char tailbuf[CACHE_LINE_SIZE];
  11978. +} FRAGMENTS_T;
  11979. +
  11980. +#endif /* VCHIQ_PAGELIST_H */
  11981. --- /dev/null
  11982. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  11983. @@ -0,0 +1,860 @@
  11984. +/**
  11985. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11986. + *
  11987. + * Redistribution and use in source and binary forms, with or without
  11988. + * modification, are permitted provided that the following conditions
  11989. + * are met:
  11990. + * 1. Redistributions of source code must retain the above copyright
  11991. + * notice, this list of conditions, and the following disclaimer,
  11992. + * without modification.
  11993. + * 2. Redistributions in binary form must reproduce the above copyright
  11994. + * notice, this list of conditions and the following disclaimer in the
  11995. + * documentation and/or other materials provided with the distribution.
  11996. + * 3. The names of the above-listed copyright holders may not be used
  11997. + * to endorse or promote products derived from this software without
  11998. + * specific prior written permission.
  11999. + *
  12000. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12001. + * GNU General Public License ("GPL") version 2, as published by the Free
  12002. + * Software Foundation.
  12003. + *
  12004. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12005. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12006. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12007. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12008. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12009. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12010. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12011. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12012. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12013. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12014. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12015. + */
  12016. +#include <linux/module.h>
  12017. +#include <linux/types.h>
  12018. +
  12019. +#include "interface/vchi/vchi.h"
  12020. +#include "vchiq.h"
  12021. +#include "vchiq_core.h"
  12022. +
  12023. +#include "vchiq_util.h"
  12024. +
  12025. +#include <stddef.h>
  12026. +
  12027. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  12028. +
  12029. +typedef struct {
  12030. + VCHIQ_SERVICE_HANDLE_T handle;
  12031. +
  12032. + VCHIU_QUEUE_T queue;
  12033. +
  12034. + VCHI_CALLBACK_T callback;
  12035. + void *callback_param;
  12036. +} SHIM_SERVICE_T;
  12037. +
  12038. +/* ----------------------------------------------------------------------
  12039. + * return pointer to the mphi message driver function table
  12040. + * -------------------------------------------------------------------- */
  12041. +const VCHI_MESSAGE_DRIVER_T *
  12042. +vchi_mphi_message_driver_func_table(void)
  12043. +{
  12044. + return NULL;
  12045. +}
  12046. +
  12047. +/* ----------------------------------------------------------------------
  12048. + * return a pointer to the 'single' connection driver fops
  12049. + * -------------------------------------------------------------------- */
  12050. +const VCHI_CONNECTION_API_T *
  12051. +single_get_func_table(void)
  12052. +{
  12053. + return NULL;
  12054. +}
  12055. +
  12056. +VCHI_CONNECTION_T *vchi_create_connection(
  12057. + const VCHI_CONNECTION_API_T *function_table,
  12058. + const VCHI_MESSAGE_DRIVER_T *low_level)
  12059. +{
  12060. + (void)function_table;
  12061. + (void)low_level;
  12062. + return NULL;
  12063. +}
  12064. +
  12065. +/***********************************************************
  12066. + * Name: vchi_msg_peek
  12067. + *
  12068. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  12069. + * void **data,
  12070. + * uint32_t *msg_size,
  12071. +
  12072. +
  12073. + * VCHI_FLAGS_T flags
  12074. + *
  12075. + * Description: Routine to return a pointer to the current message (to allow in
  12076. + * place processing). The message can be removed using
  12077. + * vchi_msg_remove when you're finished
  12078. + *
  12079. + * Returns: int32_t - success == 0
  12080. + *
  12081. + ***********************************************************/
  12082. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  12083. + void **data,
  12084. + uint32_t *msg_size,
  12085. + VCHI_FLAGS_T flags)
  12086. +{
  12087. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12088. + VCHIQ_HEADER_T *header;
  12089. +
  12090. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12091. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12092. +
  12093. + if (flags == VCHI_FLAGS_NONE)
  12094. + if (vchiu_queue_is_empty(&service->queue))
  12095. + return -1;
  12096. +
  12097. + header = vchiu_queue_peek(&service->queue);
  12098. +
  12099. + *data = header->data;
  12100. + *msg_size = header->size;
  12101. +
  12102. + return 0;
  12103. +}
  12104. +EXPORT_SYMBOL(vchi_msg_peek);
  12105. +
  12106. +/***********************************************************
  12107. + * Name: vchi_msg_remove
  12108. + *
  12109. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  12110. + *
  12111. + * Description: Routine to remove a message (after it has been read with
  12112. + * vchi_msg_peek)
  12113. + *
  12114. + * Returns: int32_t - success == 0
  12115. + *
  12116. + ***********************************************************/
  12117. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  12118. +{
  12119. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12120. + VCHIQ_HEADER_T *header;
  12121. +
  12122. + header = vchiu_queue_pop(&service->queue);
  12123. +
  12124. + vchiq_release_message(service->handle, header);
  12125. +
  12126. + return 0;
  12127. +}
  12128. +EXPORT_SYMBOL(vchi_msg_remove);
  12129. +
  12130. +/***********************************************************
  12131. + * Name: vchi_msg_queue
  12132. + *
  12133. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12134. + * const void *data,
  12135. + * uint32_t data_size,
  12136. + * VCHI_FLAGS_T flags,
  12137. + * void *msg_handle,
  12138. + *
  12139. + * Description: Thin wrapper to queue a message onto a connection
  12140. + *
  12141. + * Returns: int32_t - success == 0
  12142. + *
  12143. + ***********************************************************/
  12144. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  12145. + const void *data,
  12146. + uint32_t data_size,
  12147. + VCHI_FLAGS_T flags,
  12148. + void *msg_handle)
  12149. +{
  12150. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12151. + VCHIQ_ELEMENT_T element = {data, data_size};
  12152. + VCHIQ_STATUS_T status;
  12153. +
  12154. + (void)msg_handle;
  12155. +
  12156. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  12157. +
  12158. + status = vchiq_queue_message(service->handle, &element, 1);
  12159. +
  12160. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  12161. + ** implement a retry mechanism since this function is supposed
  12162. + ** to block until queued
  12163. + */
  12164. + while (status == VCHIQ_RETRY) {
  12165. + msleep(1);
  12166. + status = vchiq_queue_message(service->handle, &element, 1);
  12167. + }
  12168. +
  12169. + return vchiq_status_to_vchi(status);
  12170. +}
  12171. +EXPORT_SYMBOL(vchi_msg_queue);
  12172. +
  12173. +/***********************************************************
  12174. + * Name: vchi_bulk_queue_receive
  12175. + *
  12176. + * Arguments: VCHI_BULK_HANDLE_T handle,
  12177. + * void *data_dst,
  12178. + * const uint32_t data_size,
  12179. + * VCHI_FLAGS_T flags
  12180. + * void *bulk_handle
  12181. + *
  12182. + * Description: Routine to setup a rcv buffer
  12183. + *
  12184. + * Returns: int32_t - success == 0
  12185. + *
  12186. + ***********************************************************/
  12187. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  12188. + void *data_dst,
  12189. + uint32_t data_size,
  12190. + VCHI_FLAGS_T flags,
  12191. + void *bulk_handle)
  12192. +{
  12193. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12194. + VCHIQ_BULK_MODE_T mode;
  12195. + VCHIQ_STATUS_T status;
  12196. +
  12197. + switch ((int)flags) {
  12198. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  12199. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12200. + WARN_ON(!service->callback);
  12201. + mode = VCHIQ_BULK_MODE_CALLBACK;
  12202. + break;
  12203. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  12204. + mode = VCHIQ_BULK_MODE_BLOCKING;
  12205. + break;
  12206. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12207. + case VCHI_FLAGS_NONE:
  12208. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  12209. + break;
  12210. + default:
  12211. + WARN(1, "unsupported message\n");
  12212. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  12213. + }
  12214. +
  12215. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  12216. + bulk_handle, mode);
  12217. +
  12218. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  12219. + ** implement a retry mechanism since this function is supposed
  12220. + ** to block until queued
  12221. + */
  12222. + while (status == VCHIQ_RETRY) {
  12223. + msleep(1);
  12224. + status = vchiq_bulk_receive(service->handle, data_dst,
  12225. + data_size, bulk_handle, mode);
  12226. + }
  12227. +
  12228. + return vchiq_status_to_vchi(status);
  12229. +}
  12230. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  12231. +
  12232. +/***********************************************************
  12233. + * Name: vchi_bulk_queue_transmit
  12234. + *
  12235. + * Arguments: VCHI_BULK_HANDLE_T handle,
  12236. + * const void *data_src,
  12237. + * uint32_t data_size,
  12238. + * VCHI_FLAGS_T flags,
  12239. + * void *bulk_handle
  12240. + *
  12241. + * Description: Routine to transmit some data
  12242. + *
  12243. + * Returns: int32_t - success == 0
  12244. + *
  12245. + ***********************************************************/
  12246. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  12247. + const void *data_src,
  12248. + uint32_t data_size,
  12249. + VCHI_FLAGS_T flags,
  12250. + void *bulk_handle)
  12251. +{
  12252. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12253. + VCHIQ_BULK_MODE_T mode;
  12254. + VCHIQ_STATUS_T status;
  12255. +
  12256. + switch ((int)flags) {
  12257. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  12258. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12259. + WARN_ON(!service->callback);
  12260. + mode = VCHIQ_BULK_MODE_CALLBACK;
  12261. + break;
  12262. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  12263. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  12264. + mode = VCHIQ_BULK_MODE_BLOCKING;
  12265. + break;
  12266. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  12267. + case VCHI_FLAGS_NONE:
  12268. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  12269. + break;
  12270. + default:
  12271. + WARN(1, "unsupported message\n");
  12272. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  12273. + }
  12274. +
  12275. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  12276. + bulk_handle, mode);
  12277. +
  12278. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  12279. + ** implement a retry mechanism since this function is supposed
  12280. + ** to block until queued
  12281. + */
  12282. + while (status == VCHIQ_RETRY) {
  12283. + msleep(1);
  12284. + status = vchiq_bulk_transmit(service->handle, data_src,
  12285. + data_size, bulk_handle, mode);
  12286. + }
  12287. +
  12288. + return vchiq_status_to_vchi(status);
  12289. +}
  12290. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  12291. +
  12292. +/***********************************************************
  12293. + * Name: vchi_msg_dequeue
  12294. + *
  12295. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12296. + * void *data,
  12297. + * uint32_t max_data_size_to_read,
  12298. + * uint32_t *actual_msg_size
  12299. + * VCHI_FLAGS_T flags
  12300. + *
  12301. + * Description: Routine to dequeue a message into the supplied buffer
  12302. + *
  12303. + * Returns: int32_t - success == 0
  12304. + *
  12305. + ***********************************************************/
  12306. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  12307. + void *data,
  12308. + uint32_t max_data_size_to_read,
  12309. + uint32_t *actual_msg_size,
  12310. + VCHI_FLAGS_T flags)
  12311. +{
  12312. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12313. + VCHIQ_HEADER_T *header;
  12314. +
  12315. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12316. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12317. +
  12318. + if (flags == VCHI_FLAGS_NONE)
  12319. + if (vchiu_queue_is_empty(&service->queue))
  12320. + return -1;
  12321. +
  12322. + header = vchiu_queue_pop(&service->queue);
  12323. +
  12324. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  12325. + header->size : max_data_size_to_read);
  12326. +
  12327. + *actual_msg_size = header->size;
  12328. +
  12329. + vchiq_release_message(service->handle, header);
  12330. +
  12331. + return 0;
  12332. +}
  12333. +EXPORT_SYMBOL(vchi_msg_dequeue);
  12334. +
  12335. +/***********************************************************
  12336. + * Name: vchi_msg_queuev
  12337. + *
  12338. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12339. + * VCHI_MSG_VECTOR_T *vector,
  12340. + * uint32_t count,
  12341. + * VCHI_FLAGS_T flags,
  12342. + * void *msg_handle
  12343. + *
  12344. + * Description: Thin wrapper to queue a message onto a connection
  12345. + *
  12346. + * Returns: int32_t - success == 0
  12347. + *
  12348. + ***********************************************************/
  12349. +
  12350. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  12351. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  12352. + offsetof(VCHIQ_ELEMENT_T, data));
  12353. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  12354. + offsetof(VCHIQ_ELEMENT_T, size));
  12355. +
  12356. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  12357. + VCHI_MSG_VECTOR_T *vector,
  12358. + uint32_t count,
  12359. + VCHI_FLAGS_T flags,
  12360. + void *msg_handle)
  12361. +{
  12362. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12363. +
  12364. + (void)msg_handle;
  12365. +
  12366. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  12367. +
  12368. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  12369. + (const VCHIQ_ELEMENT_T *)vector, count));
  12370. +}
  12371. +EXPORT_SYMBOL(vchi_msg_queuev);
  12372. +
  12373. +/***********************************************************
  12374. + * Name: vchi_held_msg_release
  12375. + *
  12376. + * Arguments: VCHI_HELD_MSG_T *message
  12377. + *
  12378. + * Description: Routine to release a held message (after it has been read with
  12379. + * vchi_msg_hold)
  12380. + *
  12381. + * Returns: int32_t - success == 0
  12382. + *
  12383. + ***********************************************************/
  12384. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  12385. +{
  12386. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  12387. + (VCHIQ_HEADER_T *)message->message);
  12388. +
  12389. + return 0;
  12390. +}
  12391. +EXPORT_SYMBOL(vchi_held_msg_release);
  12392. +
  12393. +/***********************************************************
  12394. + * Name: vchi_msg_hold
  12395. + *
  12396. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12397. + * void **data,
  12398. + * uint32_t *msg_size,
  12399. + * VCHI_FLAGS_T flags,
  12400. + * VCHI_HELD_MSG_T *message_handle
  12401. + *
  12402. + * Description: Routine to return a pointer to the current message (to allow
  12403. + * in place processing). The message is dequeued - don't forget
  12404. + * to release the message using vchi_held_msg_release when you're
  12405. + * finished.
  12406. + *
  12407. + * Returns: int32_t - success == 0
  12408. + *
  12409. + ***********************************************************/
  12410. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  12411. + void **data,
  12412. + uint32_t *msg_size,
  12413. + VCHI_FLAGS_T flags,
  12414. + VCHI_HELD_MSG_T *message_handle)
  12415. +{
  12416. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12417. + VCHIQ_HEADER_T *header;
  12418. +
  12419. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12420. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12421. +
  12422. + if (flags == VCHI_FLAGS_NONE)
  12423. + if (vchiu_queue_is_empty(&service->queue))
  12424. + return -1;
  12425. +
  12426. + header = vchiu_queue_pop(&service->queue);
  12427. +
  12428. + *data = header->data;
  12429. + *msg_size = header->size;
  12430. +
  12431. + message_handle->service =
  12432. + (struct opaque_vchi_service_t *)service->handle;
  12433. + message_handle->message = header;
  12434. +
  12435. + return 0;
  12436. +}
  12437. +EXPORT_SYMBOL(vchi_msg_hold);
  12438. +
  12439. +/***********************************************************
  12440. + * Name: vchi_initialise
  12441. + *
  12442. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12443. + *
  12444. + * Description: Initialises the hardware but does not transmit anything
  12445. + * When run as a Host App this will be called twice hence the need
  12446. + * to malloc the state information
  12447. + *
  12448. + * Returns: 0 if successful, failure otherwise
  12449. + *
  12450. + ***********************************************************/
  12451. +
  12452. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  12453. +{
  12454. + VCHIQ_INSTANCE_T instance;
  12455. + VCHIQ_STATUS_T status;
  12456. +
  12457. + status = vchiq_initialise(&instance);
  12458. +
  12459. + *instance_handle = (VCHI_INSTANCE_T)instance;
  12460. +
  12461. + return vchiq_status_to_vchi(status);
  12462. +}
  12463. +EXPORT_SYMBOL(vchi_initialise);
  12464. +
  12465. +/***********************************************************
  12466. + * Name: vchi_connect
  12467. + *
  12468. + * Arguments: VCHI_CONNECTION_T **connections
  12469. + * const uint32_t num_connections
  12470. + * VCHI_INSTANCE_T instance_handle)
  12471. + *
  12472. + * Description: Starts the command service on each connection,
  12473. + * causing INIT messages to be pinged back and forth
  12474. + *
  12475. + * Returns: 0 if successful, failure otherwise
  12476. + *
  12477. + ***********************************************************/
  12478. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  12479. + const uint32_t num_connections,
  12480. + VCHI_INSTANCE_T instance_handle)
  12481. +{
  12482. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12483. +
  12484. + (void)connections;
  12485. + (void)num_connections;
  12486. +
  12487. + return vchiq_connect(instance);
  12488. +}
  12489. +EXPORT_SYMBOL(vchi_connect);
  12490. +
  12491. +
  12492. +/***********************************************************
  12493. + * Name: vchi_disconnect
  12494. + *
  12495. + * Arguments: VCHI_INSTANCE_T instance_handle
  12496. + *
  12497. + * Description: Stops the command service on each connection,
  12498. + * causing DE-INIT messages to be pinged back and forth
  12499. + *
  12500. + * Returns: 0 if successful, failure otherwise
  12501. + *
  12502. + ***********************************************************/
  12503. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  12504. +{
  12505. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12506. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  12507. +}
  12508. +EXPORT_SYMBOL(vchi_disconnect);
  12509. +
  12510. +
  12511. +/***********************************************************
  12512. + * Name: vchi_service_open
  12513. + * Name: vchi_service_create
  12514. + *
  12515. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12516. + * SERVICE_CREATION_T *setup,
  12517. + * VCHI_SERVICE_HANDLE_T *handle
  12518. + *
  12519. + * Description: Routine to open a service
  12520. + *
  12521. + * Returns: int32_t - success == 0
  12522. + *
  12523. + ***********************************************************/
  12524. +
  12525. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  12526. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  12527. +{
  12528. + SHIM_SERVICE_T *service =
  12529. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  12530. +
  12531. + if (!service->callback)
  12532. + goto release;
  12533. +
  12534. + switch (reason) {
  12535. + case VCHIQ_MESSAGE_AVAILABLE:
  12536. + vchiu_queue_push(&service->queue, header);
  12537. +
  12538. + service->callback(service->callback_param,
  12539. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  12540. +
  12541. + goto done;
  12542. + break;
  12543. +
  12544. + case VCHIQ_BULK_TRANSMIT_DONE:
  12545. + service->callback(service->callback_param,
  12546. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  12547. + break;
  12548. +
  12549. + case VCHIQ_BULK_RECEIVE_DONE:
  12550. + service->callback(service->callback_param,
  12551. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  12552. + break;
  12553. +
  12554. + case VCHIQ_SERVICE_CLOSED:
  12555. + service->callback(service->callback_param,
  12556. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  12557. + break;
  12558. +
  12559. + case VCHIQ_SERVICE_OPENED:
  12560. + /* No equivalent VCHI reason */
  12561. + break;
  12562. +
  12563. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  12564. + service->callback(service->callback_param,
  12565. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  12566. + bulk_user);
  12567. + break;
  12568. +
  12569. + case VCHIQ_BULK_RECEIVE_ABORTED:
  12570. + service->callback(service->callback_param,
  12571. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  12572. + bulk_user);
  12573. + break;
  12574. +
  12575. + default:
  12576. + WARN(1, "not supported\n");
  12577. + break;
  12578. + }
  12579. +
  12580. +release:
  12581. + vchiq_release_message(service->handle, header);
  12582. +done:
  12583. + return VCHIQ_SUCCESS;
  12584. +}
  12585. +
  12586. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  12587. + SERVICE_CREATION_T *setup)
  12588. +{
  12589. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  12590. +
  12591. + (void)instance;
  12592. +
  12593. + if (service) {
  12594. + if (vchiu_queue_init(&service->queue, 64)) {
  12595. + service->callback = setup->callback;
  12596. + service->callback_param = setup->callback_param;
  12597. + } else {
  12598. + kfree(service);
  12599. + service = NULL;
  12600. + }
  12601. + }
  12602. +
  12603. + return service;
  12604. +}
  12605. +
  12606. +static void service_free(SHIM_SERVICE_T *service)
  12607. +{
  12608. + if (service) {
  12609. + vchiu_queue_delete(&service->queue);
  12610. + kfree(service);
  12611. + }
  12612. +}
  12613. +
  12614. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  12615. + SERVICE_CREATION_T *setup,
  12616. + VCHI_SERVICE_HANDLE_T *handle)
  12617. +{
  12618. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12619. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12620. +
  12621. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12622. +
  12623. + if (service) {
  12624. + VCHIQ_SERVICE_PARAMS_T params;
  12625. + VCHIQ_STATUS_T status;
  12626. +
  12627. + memset(&params, 0, sizeof(params));
  12628. + params.fourcc = setup->service_id;
  12629. + params.callback = shim_callback;
  12630. + params.userdata = service;
  12631. + params.version = setup->version.version;
  12632. + params.version_min = setup->version.version_min;
  12633. +
  12634. + status = vchiq_open_service(instance, &params,
  12635. + &service->handle);
  12636. + if (status != VCHIQ_SUCCESS) {
  12637. + service_free(service);
  12638. + service = NULL;
  12639. + *handle = NULL;
  12640. + }
  12641. + }
  12642. +
  12643. + return (service != NULL) ? 0 : -1;
  12644. +}
  12645. +EXPORT_SYMBOL(vchi_service_open);
  12646. +
  12647. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  12648. + SERVICE_CREATION_T *setup,
  12649. + VCHI_SERVICE_HANDLE_T *handle)
  12650. +{
  12651. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12652. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12653. +
  12654. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12655. +
  12656. + if (service) {
  12657. + VCHIQ_SERVICE_PARAMS_T params;
  12658. + VCHIQ_STATUS_T status;
  12659. +
  12660. + memset(&params, 0, sizeof(params));
  12661. + params.fourcc = setup->service_id;
  12662. + params.callback = shim_callback;
  12663. + params.userdata = service;
  12664. + params.version = setup->version.version;
  12665. + params.version_min = setup->version.version_min;
  12666. + status = vchiq_add_service(instance, &params, &service->handle);
  12667. +
  12668. + if (status != VCHIQ_SUCCESS) {
  12669. + service_free(service);
  12670. + service = NULL;
  12671. + *handle = NULL;
  12672. + }
  12673. + }
  12674. +
  12675. + return (service != NULL) ? 0 : -1;
  12676. +}
  12677. +EXPORT_SYMBOL(vchi_service_create);
  12678. +
  12679. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  12680. +{
  12681. + int32_t ret = -1;
  12682. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12683. + if (service) {
  12684. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  12685. + if (status == VCHIQ_SUCCESS) {
  12686. + service_free(service);
  12687. + service = NULL;
  12688. + }
  12689. +
  12690. + ret = vchiq_status_to_vchi(status);
  12691. + }
  12692. + return ret;
  12693. +}
  12694. +EXPORT_SYMBOL(vchi_service_close);
  12695. +
  12696. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  12697. +{
  12698. + int32_t ret = -1;
  12699. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12700. + if (service) {
  12701. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  12702. + if (status == VCHIQ_SUCCESS) {
  12703. + service_free(service);
  12704. + service = NULL;
  12705. + }
  12706. +
  12707. + ret = vchiq_status_to_vchi(status);
  12708. + }
  12709. + return ret;
  12710. +}
  12711. +EXPORT_SYMBOL(vchi_service_destroy);
  12712. +
  12713. +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
  12714. + VCHI_SERVICE_OPTION_T option,
  12715. + int value)
  12716. +{
  12717. + int32_t ret = -1;
  12718. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12719. + VCHIQ_SERVICE_OPTION_T vchiq_option;
  12720. + switch (option) {
  12721. + case VCHI_SERVICE_OPTION_TRACE:
  12722. + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
  12723. + break;
  12724. + case VCHI_SERVICE_OPTION_SYNCHRONOUS:
  12725. + vchiq_option = VCHIQ_SERVICE_OPTION_SYNCHRONOUS;
  12726. + break;
  12727. + default:
  12728. + service = NULL;
  12729. + break;
  12730. + }
  12731. + if (service) {
  12732. + VCHIQ_STATUS_T status =
  12733. + vchiq_set_service_option(service->handle,
  12734. + vchiq_option,
  12735. + value);
  12736. +
  12737. + ret = vchiq_status_to_vchi(status);
  12738. + }
  12739. + return ret;
  12740. +}
  12741. +EXPORT_SYMBOL(vchi_service_set_option);
  12742. +
  12743. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  12744. +{
  12745. + int32_t ret = -1;
  12746. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12747. + if(service)
  12748. + {
  12749. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  12750. + ret = vchiq_status_to_vchi( status );
  12751. + }
  12752. + return ret;
  12753. +}
  12754. +EXPORT_SYMBOL(vchi_get_peer_version);
  12755. +
  12756. +/* ----------------------------------------------------------------------
  12757. + * read a uint32_t from buffer.
  12758. + * network format is defined to be little endian
  12759. + * -------------------------------------------------------------------- */
  12760. +uint32_t
  12761. +vchi_readbuf_uint32(const void *_ptr)
  12762. +{
  12763. + const unsigned char *ptr = _ptr;
  12764. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  12765. +}
  12766. +
  12767. +/* ----------------------------------------------------------------------
  12768. + * write a uint32_t to buffer.
  12769. + * network format is defined to be little endian
  12770. + * -------------------------------------------------------------------- */
  12771. +void
  12772. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  12773. +{
  12774. + unsigned char *ptr = _ptr;
  12775. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  12776. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  12777. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  12778. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  12779. +}
  12780. +
  12781. +/* ----------------------------------------------------------------------
  12782. + * read a uint16_t from buffer.
  12783. + * network format is defined to be little endian
  12784. + * -------------------------------------------------------------------- */
  12785. +uint16_t
  12786. +vchi_readbuf_uint16(const void *_ptr)
  12787. +{
  12788. + const unsigned char *ptr = _ptr;
  12789. + return ptr[0] | (ptr[1] << 8);
  12790. +}
  12791. +
  12792. +/* ----------------------------------------------------------------------
  12793. + * write a uint16_t into the buffer.
  12794. + * network format is defined to be little endian
  12795. + * -------------------------------------------------------------------- */
  12796. +void
  12797. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  12798. +{
  12799. + unsigned char *ptr = _ptr;
  12800. + ptr[0] = (value >> 0) & 0xFF;
  12801. + ptr[1] = (value >> 8) & 0xFF;
  12802. +}
  12803. +
  12804. +/***********************************************************
  12805. + * Name: vchi_service_use
  12806. + *
  12807. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12808. + *
  12809. + * Description: Routine to increment refcount on a service
  12810. + *
  12811. + * Returns: void
  12812. + *
  12813. + ***********************************************************/
  12814. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  12815. +{
  12816. + int32_t ret = -1;
  12817. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12818. + if (service)
  12819. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  12820. + return ret;
  12821. +}
  12822. +EXPORT_SYMBOL(vchi_service_use);
  12823. +
  12824. +/***********************************************************
  12825. + * Name: vchi_service_release
  12826. + *
  12827. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12828. + *
  12829. + * Description: Routine to decrement refcount on a service
  12830. + *
  12831. + * Returns: void
  12832. + *
  12833. + ***********************************************************/
  12834. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  12835. +{
  12836. + int32_t ret = -1;
  12837. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12838. + if (service)
  12839. + ret = vchiq_status_to_vchi(
  12840. + vchiq_release_service(service->handle));
  12841. + return ret;
  12842. +}
  12843. +EXPORT_SYMBOL(vchi_service_release);
  12844. --- /dev/null
  12845. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  12846. @@ -0,0 +1,156 @@
  12847. +/**
  12848. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  12849. + *
  12850. + * Redistribution and use in source and binary forms, with or without
  12851. + * modification, are permitted provided that the following conditions
  12852. + * are met:
  12853. + * 1. Redistributions of source code must retain the above copyright
  12854. + * notice, this list of conditions, and the following disclaimer,
  12855. + * without modification.
  12856. + * 2. Redistributions in binary form must reproduce the above copyright
  12857. + * notice, this list of conditions and the following disclaimer in the
  12858. + * documentation and/or other materials provided with the distribution.
  12859. + * 3. The names of the above-listed copyright holders may not be used
  12860. + * to endorse or promote products derived from this software without
  12861. + * specific prior written permission.
  12862. + *
  12863. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12864. + * GNU General Public License ("GPL") version 2, as published by the Free
  12865. + * Software Foundation.
  12866. + *
  12867. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12868. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12869. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12870. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12871. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12872. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12873. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12874. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12875. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12876. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12877. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12878. + */
  12879. +
  12880. +#include "vchiq_util.h"
  12881. +#include "vchiq_killable.h"
  12882. +
  12883. +static inline int is_pow2(int i)
  12884. +{
  12885. + return i && !(i & (i - 1));
  12886. +}
  12887. +
  12888. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  12889. +{
  12890. + WARN_ON(!is_pow2(size));
  12891. +
  12892. + queue->size = size;
  12893. + queue->read = 0;
  12894. + queue->write = 0;
  12895. + queue->initialized = 1;
  12896. +
  12897. + sema_init(&queue->pop, 0);
  12898. + sema_init(&queue->push, 0);
  12899. +
  12900. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  12901. + if (queue->storage == NULL) {
  12902. + vchiu_queue_delete(queue);
  12903. + return 0;
  12904. + }
  12905. + return 1;
  12906. +}
  12907. +
  12908. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  12909. +{
  12910. + if (queue->storage != NULL)
  12911. + kfree(queue->storage);
  12912. +}
  12913. +
  12914. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  12915. +{
  12916. + return queue->read == queue->write;
  12917. +}
  12918. +
  12919. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  12920. +{
  12921. + return queue->write == queue->read + queue->size;
  12922. +}
  12923. +
  12924. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  12925. +{
  12926. + if (!queue->initialized)
  12927. + return;
  12928. +
  12929. + while (queue->write == queue->read + queue->size) {
  12930. + if (down_interruptible(&queue->pop) != 0) {
  12931. + flush_signals(current);
  12932. + }
  12933. + }
  12934. +
  12935. + /*
  12936. + * Write to queue->storage must be visible after read from
  12937. + * queue->read
  12938. + */
  12939. + smp_mb();
  12940. +
  12941. + queue->storage[queue->write & (queue->size - 1)] = header;
  12942. +
  12943. + /*
  12944. + * Write to queue->storage must be visible before write to
  12945. + * queue->write
  12946. + */
  12947. + smp_wmb();
  12948. +
  12949. + queue->write++;
  12950. +
  12951. + up(&queue->push);
  12952. +}
  12953. +
  12954. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  12955. +{
  12956. + while (queue->write == queue->read) {
  12957. + if (down_interruptible(&queue->push) != 0) {
  12958. + flush_signals(current);
  12959. + }
  12960. + }
  12961. +
  12962. + up(&queue->push); // We haven't removed anything from the queue.
  12963. +
  12964. + /*
  12965. + * Read from queue->storage must be visible after read from
  12966. + * queue->write
  12967. + */
  12968. + smp_rmb();
  12969. +
  12970. + return queue->storage[queue->read & (queue->size - 1)];
  12971. +}
  12972. +
  12973. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  12974. +{
  12975. + VCHIQ_HEADER_T *header;
  12976. +
  12977. + while (queue->write == queue->read) {
  12978. + if (down_interruptible(&queue->push) != 0) {
  12979. + flush_signals(current);
  12980. + }
  12981. + }
  12982. +
  12983. + /*
  12984. + * Read from queue->storage must be visible after read from
  12985. + * queue->write
  12986. + */
  12987. + smp_rmb();
  12988. +
  12989. + header = queue->storage[queue->read & (queue->size - 1)];
  12990. +
  12991. + /*
  12992. + * Read from queue->storage must be visible before write to
  12993. + * queue->read
  12994. + */
  12995. + smp_mb();
  12996. +
  12997. + queue->read++;
  12998. +
  12999. + up(&queue->pop);
  13000. +
  13001. + return header;
  13002. +}
  13003. --- /dev/null
  13004. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  13005. @@ -0,0 +1,82 @@
  13006. +/**
  13007. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  13008. + *
  13009. + * Redistribution and use in source and binary forms, with or without
  13010. + * modification, are permitted provided that the following conditions
  13011. + * are met:
  13012. + * 1. Redistributions of source code must retain the above copyright
  13013. + * notice, this list of conditions, and the following disclaimer,
  13014. + * without modification.
  13015. + * 2. Redistributions in binary form must reproduce the above copyright
  13016. + * notice, this list of conditions and the following disclaimer in the
  13017. + * documentation and/or other materials provided with the distribution.
  13018. + * 3. The names of the above-listed copyright holders may not be used
  13019. + * to endorse or promote products derived from this software without
  13020. + * specific prior written permission.
  13021. + *
  13022. + * ALTERNATIVELY, this software may be distributed under the terms of the
  13023. + * GNU General Public License ("GPL") version 2, as published by the Free
  13024. + * Software Foundation.
  13025. + *
  13026. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  13027. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13028. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13029. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  13030. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13031. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13032. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  13033. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  13034. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  13035. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  13036. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13037. + */
  13038. +
  13039. +#ifndef VCHIQ_UTIL_H
  13040. +#define VCHIQ_UTIL_H
  13041. +
  13042. +#include <linux/types.h>
  13043. +#include <linux/semaphore.h>
  13044. +#include <linux/mutex.h>
  13045. +#include <linux/bitops.h>
  13046. +#include <linux/kthread.h>
  13047. +#include <linux/wait.h>
  13048. +#include <linux/vmalloc.h>
  13049. +#include <linux/jiffies.h>
  13050. +#include <linux/delay.h>
  13051. +#include <linux/string.h>
  13052. +#include <linux/types.h>
  13053. +#include <linux/interrupt.h>
  13054. +#include <linux/random.h>
  13055. +#include <linux/sched.h>
  13056. +#include <linux/ctype.h>
  13057. +#include <linux/uaccess.h>
  13058. +#include <linux/time.h> /* for time_t */
  13059. +#include <linux/slab.h>
  13060. +#include <linux/vmalloc.h>
  13061. +
  13062. +#include "vchiq_if.h"
  13063. +
  13064. +typedef struct {
  13065. + int size;
  13066. + int read;
  13067. + int write;
  13068. + int initialized;
  13069. +
  13070. + struct semaphore pop;
  13071. + struct semaphore push;
  13072. +
  13073. + VCHIQ_HEADER_T **storage;
  13074. +} VCHIU_QUEUE_T;
  13075. +
  13076. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  13077. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  13078. +
  13079. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  13080. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  13081. +
  13082. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  13083. +
  13084. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  13085. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  13086. +
  13087. +#endif
  13088. --- /dev/null
  13089. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  13090. @@ -0,0 +1,59 @@
  13091. +/**
  13092. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  13093. + *
  13094. + * Redistribution and use in source and binary forms, with or without
  13095. + * modification, are permitted provided that the following conditions
  13096. + * are met:
  13097. + * 1. Redistributions of source code must retain the above copyright
  13098. + * notice, this list of conditions, and the following disclaimer,
  13099. + * without modification.
  13100. + * 2. Redistributions in binary form must reproduce the above copyright
  13101. + * notice, this list of conditions and the following disclaimer in the
  13102. + * documentation and/or other materials provided with the distribution.
  13103. + * 3. The names of the above-listed copyright holders may not be used
  13104. + * to endorse or promote products derived from this software without
  13105. + * specific prior written permission.
  13106. + *
  13107. + * ALTERNATIVELY, this software may be distributed under the terms of the
  13108. + * GNU General Public License ("GPL") version 2, as published by the Free
  13109. + * Software Foundation.
  13110. + *
  13111. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  13112. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13113. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13114. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  13115. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13116. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13117. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  13118. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  13119. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  13120. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  13121. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13122. + */
  13123. +#include "vchiq_build_info.h"
  13124. +#include <linux/broadcom/vc_debug_sym.h>
  13125. +
  13126. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  13127. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  13128. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  13129. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  13130. +
  13131. +const char *vchiq_get_build_hostname( void )
  13132. +{
  13133. + return vchiq_build_hostname;
  13134. +}
  13135. +
  13136. +const char *vchiq_get_build_version( void )
  13137. +{
  13138. + return vchiq_build_version;
  13139. +}
  13140. +
  13141. +const char *vchiq_get_build_date( void )
  13142. +{
  13143. + return vchiq_build_date;
  13144. +}
  13145. +
  13146. +const char *vchiq_get_build_time( void )
  13147. +{
  13148. + return vchiq_build_time;
  13149. +}