0010-bcm2708-vchiq-driver.patch 394 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851
  1. From adee2a81f0be488e079498ac457bf01c954a029e 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 010/114] bcm2708 vchiq driver
  5. Signed-off-by: popcornmix <popcornmix@gmail.com>
  6. vchiq: create_pagelist copes with vmalloc memory
  7. Signed-off-by: Daniel Stone <daniels@collabora.com>
  8. vchiq: fix the shim message release
  9. Signed-off-by: Daniel Stone <daniels@collabora.com>
  10. vchiq: export additional symbols
  11. Signed-off-by: Daniel Stone <daniels@collabora.com>
  12. VCHIQ: Make service closure fully synchronous (drv)
  13. This is one half of a two-part patch, the other half of which is to
  14. the vchiq_lib user library. With these patches, calls to
  15. vchiq_close_service and vchiq_remove_service won't return until any
  16. associated callbacks have been delivered to the callback thread.
  17. VCHIQ: Add per-service tracing
  18. The new service option VCHIQ_SERVICE_OPTION_TRACE is a boolean that
  19. toggles tracing for the specified service.
  20. This commit also introduces vchi_service_set_option and the associated
  21. option VCHI_SERVICE_OPTION_TRACE.
  22. vchiq: Make the synchronous-CLOSE logic more tolerant
  23. vchiq: Move logging control into debugfs
  24. vchiq: Take care of a corner case tickled by VCSM
  25. Closing a connection that isn't fully open requires care, since one
  26. side does not know the other side's port number. Code was present to
  27. handle the case where a CLOSE is sent immediately after an OPEN, i.e.
  28. before the OPENACK has been received, but this was incorrectly being
  29. used when an OPEN from a client using port 0 was rejected.
  30. (In the observed failure, the host was attempting to use the VCSM
  31. service, which isn't present in the 'cutdown' firmware. The failure
  32. was intermittent because sometimes the keepalive service would
  33. grab port 0.)
  34. This case can be distinguished because the client's remoteport will
  35. still be VCHIQ_PORT_FREE, and the srvstate will be OPENING. Either
  36. condition is sufficient to differentiate it from the special case
  37. described above.
  38. ---
  39. drivers/misc/Kconfig | 1 +
  40. drivers/misc/Makefile | 1 +
  41. drivers/misc/vc04_services/Kconfig | 9 +
  42. drivers/misc/vc04_services/Makefile | 17 +
  43. .../interface/vchi/connections/connection.h | 328 ++
  44. .../interface/vchi/message_drivers/message.h | 204 ++
  45. drivers/misc/vc04_services/interface/vchi/vchi.h | 378 ++
  46. .../misc/vc04_services/interface/vchi/vchi_cfg.h | 224 ++
  47. .../interface/vchi/vchi_cfg_internal.h | 71 +
  48. .../vc04_services/interface/vchi/vchi_common.h | 174 +
  49. .../misc/vc04_services/interface/vchi/vchi_mh.h | 42 +
  50. .../misc/vc04_services/interface/vchiq_arm/vchiq.h | 40 +
  51. .../vc04_services/interface/vchiq_arm/vchiq_2835.h | 42 +
  52. .../interface/vchiq_arm/vchiq_2835_arm.c | 561 +++
  53. .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2883 +++++++++++++++
  54. .../vc04_services/interface/vchiq_arm/vchiq_arm.h | 223 ++
  55. .../interface/vchiq_arm/vchiq_build_info.h | 37 +
  56. .../vc04_services/interface/vchiq_arm/vchiq_cfg.h | 66 +
  57. .../interface/vchiq_arm/vchiq_connected.c | 119 +
  58. .../interface/vchiq_arm/vchiq_connected.h | 50 +
  59. .../vc04_services/interface/vchiq_arm/vchiq_core.c | 3861 ++++++++++++++++++++
  60. .../vc04_services/interface/vchiq_arm/vchiq_core.h | 711 ++++
  61. .../interface/vchiq_arm/vchiq_debugfs.c | 383 ++
  62. .../interface/vchiq_arm/vchiq_debugfs.h | 52 +
  63. .../interface/vchiq_arm/vchiq_genversion | 87 +
  64. .../vc04_services/interface/vchiq_arm/vchiq_if.h | 189 +
  65. .../interface/vchiq_arm/vchiq_ioctl.h | 131 +
  66. .../interface/vchiq_arm/vchiq_kern_lib.c | 456 +++
  67. .../interface/vchiq_arm/vchiq_memdrv.h | 71 +
  68. .../interface/vchiq_arm/vchiq_pagelist.h | 58 +
  69. .../vc04_services/interface/vchiq_arm/vchiq_shim.c | 853 +++++
  70. .../vc04_services/interface/vchiq_arm/vchiq_util.c | 151 +
  71. .../vc04_services/interface/vchiq_arm/vchiq_util.h | 81 +
  72. .../interface/vchiq_arm/vchiq_version.c | 59 +
  73. 34 files changed, 12613 insertions(+)
  74. create mode 100644 drivers/misc/vc04_services/Kconfig
  75. create mode 100644 drivers/misc/vc04_services/Makefile
  76. create mode 100644 drivers/misc/vc04_services/interface/vchi/connections/connection.h
  77. create mode 100644 drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  78. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi.h
  79. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  80. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  81. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_common.h
  82. create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  83. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  84. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  85. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  86. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  87. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  88. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  89. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  90. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  91. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  92. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  93. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  94. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  95. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  96. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  97. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  98. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  99. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  100. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  101. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  102. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  103. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  104. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  105. create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  106. --- a/drivers/misc/Kconfig
  107. +++ b/drivers/misc/Kconfig
  108. @@ -524,6 +524,7 @@ source "drivers/misc/carma/Kconfig"
  109. source "drivers/misc/altera-stapl/Kconfig"
  110. source "drivers/misc/mei/Kconfig"
  111. source "drivers/misc/vmw_vmci/Kconfig"
  112. +source "drivers/misc/vc04_services/Kconfig"
  113. source "drivers/misc/mic/Kconfig"
  114. source "drivers/misc/genwqe/Kconfig"
  115. source "drivers/misc/echo/Kconfig"
  116. --- a/drivers/misc/Makefile
  117. +++ b/drivers/misc/Makefile
  118. @@ -51,6 +51,7 @@ obj-$(CONFIG_INTEL_MEI) += mei/
  119. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  120. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  121. obj-$(CONFIG_SRAM) += sram.o
  122. +obj-y += vc04_services/
  123. obj-y += mic/
  124. obj-$(CONFIG_GENWQE) += genwqe/
  125. obj-$(CONFIG_ECHO) += echo/
  126. --- /dev/null
  127. +++ b/drivers/misc/vc04_services/Kconfig
  128. @@ -0,0 +1,9 @@
  129. +config BCM2708_VCHIQ
  130. + tristate "Videocore VCHIQ"
  131. + depends on MACH_BCM2708
  132. + default y
  133. + help
  134. + Kernel to VideoCore communication interface for the
  135. + BCM2708 family of products.
  136. + Defaults to Y when the Broadcom Videocore services
  137. + are included in the build, N otherwise.
  138. --- /dev/null
  139. +++ b/drivers/misc/vc04_services/Makefile
  140. @@ -0,0 +1,17 @@
  141. +ifeq ($(CONFIG_MACH_BCM2708),y)
  142. +
  143. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  144. +
  145. +vchiq-objs := \
  146. + interface/vchiq_arm/vchiq_core.o \
  147. + interface/vchiq_arm/vchiq_arm.o \
  148. + interface/vchiq_arm/vchiq_kern_lib.o \
  149. + interface/vchiq_arm/vchiq_2835_arm.o \
  150. + interface/vchiq_arm/vchiq_debugfs.o \
  151. + interface/vchiq_arm/vchiq_shim.o \
  152. + interface/vchiq_arm/vchiq_util.o \
  153. + interface/vchiq_arm/vchiq_connected.o \
  154. +
  155. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  156. +
  157. +endif
  158. --- /dev/null
  159. +++ b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  160. @@ -0,0 +1,328 @@
  161. +/**
  162. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  163. + *
  164. + * Redistribution and use in source and binary forms, with or without
  165. + * modification, are permitted provided that the following conditions
  166. + * are met:
  167. + * 1. Redistributions of source code must retain the above copyright
  168. + * notice, this list of conditions, and the following disclaimer,
  169. + * without modification.
  170. + * 2. Redistributions in binary form must reproduce the above copyright
  171. + * notice, this list of conditions and the following disclaimer in the
  172. + * documentation and/or other materials provided with the distribution.
  173. + * 3. The names of the above-listed copyright holders may not be used
  174. + * to endorse or promote products derived from this software without
  175. + * specific prior written permission.
  176. + *
  177. + * ALTERNATIVELY, this software may be distributed under the terms of the
  178. + * GNU General Public License ("GPL") version 2, as published by the Free
  179. + * Software Foundation.
  180. + *
  181. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  182. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  183. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  184. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  185. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  186. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  187. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  188. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  189. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  190. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  191. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  192. + */
  193. +
  194. +#ifndef CONNECTION_H_
  195. +#define CONNECTION_H_
  196. +
  197. +#include <linux/kernel.h>
  198. +#include <linux/types.h>
  199. +#include <linux/semaphore.h>
  200. +
  201. +#include "interface/vchi/vchi_cfg_internal.h"
  202. +#include "interface/vchi/vchi_common.h"
  203. +#include "interface/vchi/message_drivers/message.h"
  204. +
  205. +/******************************************************************************
  206. + Global defs
  207. + *****************************************************************************/
  208. +
  209. +// Opaque handle for a connection / service pair
  210. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  211. +
  212. +// opaque handle to the connection state information
  213. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  214. +
  215. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  216. +
  217. +
  218. +/******************************************************************************
  219. + API
  220. + *****************************************************************************/
  221. +
  222. +// Routine to init a connection with a particular low level driver
  223. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  224. + const VCHI_MESSAGE_DRIVER_T * driver );
  225. +
  226. +// Routine to control CRC enabling at a connection level
  227. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  228. + VCHI_CRC_CONTROL_T control );
  229. +
  230. +// Routine to create a service
  231. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  232. + int32_t service_id,
  233. + uint32_t rx_fifo_size,
  234. + uint32_t tx_fifo_size,
  235. + int server,
  236. + VCHI_CALLBACK_T callback,
  237. + void *callback_param,
  238. + int32_t want_crc,
  239. + int32_t want_unaligned_bulk_rx,
  240. + int32_t want_unaligned_bulk_tx,
  241. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  242. +
  243. +// Routine to close a service
  244. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  245. +
  246. +// Routine to queue a message
  247. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  248. + const void *data,
  249. + uint32_t data_size,
  250. + VCHI_FLAGS_T flags,
  251. + void *msg_handle );
  252. +
  253. +// scatter-gather (vector) message queueing
  254. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  255. + VCHI_MSG_VECTOR_T *vector,
  256. + uint32_t count,
  257. + VCHI_FLAGS_T flags,
  258. + void *msg_handle );
  259. +
  260. +// Routine to dequeue a message
  261. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  262. + void *data,
  263. + uint32_t max_data_size_to_read,
  264. + uint32_t *actual_msg_size,
  265. + VCHI_FLAGS_T flags );
  266. +
  267. +// Routine to peek at a message
  268. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  269. + void **data,
  270. + uint32_t *msg_size,
  271. + VCHI_FLAGS_T flags );
  272. +
  273. +// Routine to hold a message
  274. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  275. + void **data,
  276. + uint32_t *msg_size,
  277. + VCHI_FLAGS_T flags,
  278. + void **message_handle );
  279. +
  280. +// Routine to initialise a received message iterator
  281. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  282. + VCHI_MSG_ITER_T *iter,
  283. + VCHI_FLAGS_T flags );
  284. +
  285. +// Routine to release a held message
  286. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  287. + void *message_handle );
  288. +
  289. +// Routine to get info on a held message
  290. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  291. + void *message_handle,
  292. + void **data,
  293. + int32_t *msg_size,
  294. + uint32_t *tx_timestamp,
  295. + uint32_t *rx_timestamp );
  296. +
  297. +// Routine to check whether the iterator has a next message
  298. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  299. + const VCHI_MSG_ITER_T *iter );
  300. +
  301. +// Routine to advance the iterator
  302. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  303. + VCHI_MSG_ITER_T *iter,
  304. + void **data,
  305. + uint32_t *msg_size );
  306. +
  307. +// Routine to remove the last message returned by the iterator
  308. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  309. + VCHI_MSG_ITER_T *iter );
  310. +
  311. +// Routine to hold the last message returned by the iterator
  312. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  313. + VCHI_MSG_ITER_T *iter,
  314. + void **msg_handle );
  315. +
  316. +// Routine to transmit bulk data
  317. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  318. + const void *data_src,
  319. + uint32_t data_size,
  320. + VCHI_FLAGS_T flags,
  321. + void *bulk_handle );
  322. +
  323. +// Routine to receive data
  324. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  325. + void *data_dst,
  326. + uint32_t data_size,
  327. + VCHI_FLAGS_T flags,
  328. + void *bulk_handle );
  329. +
  330. +// Routine to report if a server is available
  331. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  332. +
  333. +// Routine to report the number of RX slots available
  334. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  335. +
  336. +// Routine to report the RX slot size
  337. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  338. +
  339. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  340. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  341. + int32_t service,
  342. + uint32_t length,
  343. + MESSAGE_TX_CHANNEL_T channel,
  344. + uint32_t channel_params,
  345. + uint32_t data_length,
  346. + uint32_t data_offset);
  347. +
  348. +// Callback to inform a service that a Xon or Xoff message has been received
  349. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  350. +
  351. +// Callback to inform a service that a server available reply message has been received
  352. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  353. +
  354. +// Callback to indicate that bulk auxiliary messages have arrived
  355. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  356. +
  357. +// Callback to indicate that bulk auxiliary messages have arrived
  358. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  359. +
  360. +// Callback with all the connection info you require
  361. +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);
  362. +
  363. +// Callback to inform of a disconnect
  364. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  365. +
  366. +// Callback to inform of a power control request
  367. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  368. +
  369. +// allocate memory suitably aligned for this connection
  370. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  371. +
  372. +// free memory allocated by buffer_allocate
  373. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  374. +
  375. +
  376. +/******************************************************************************
  377. + System driver struct
  378. + *****************************************************************************/
  379. +
  380. +struct opaque_vchi_connection_api_t
  381. +{
  382. + // Routine to init the connection
  383. + VCHI_CONNECTION_INIT_T init;
  384. +
  385. + // Connection-level CRC control
  386. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  387. +
  388. + // Routine to connect to or create service
  389. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  390. +
  391. + // Routine to disconnect from a service
  392. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  393. +
  394. + // Routine to queue a message
  395. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  396. +
  397. + // scatter-gather (vector) message queue
  398. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  399. +
  400. + // Routine to dequeue a message
  401. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  402. +
  403. + // Routine to peek at a message
  404. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  405. +
  406. + // Routine to hold a message
  407. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  408. +
  409. + // Routine to initialise a received message iterator
  410. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  411. +
  412. + // Routine to release a message
  413. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  414. +
  415. + // Routine to get information on a held message
  416. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  417. +
  418. + // Routine to check for next message on iterator
  419. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  420. +
  421. + // Routine to get next message on iterator
  422. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  423. +
  424. + // Routine to remove the last message returned by iterator
  425. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  426. +
  427. + // Routine to hold the last message returned by iterator
  428. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  429. +
  430. + // Routine to transmit bulk data
  431. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  432. +
  433. + // Routine to receive data
  434. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  435. +
  436. + // Routine to report the available servers
  437. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  438. +
  439. + // Routine to report the number of RX slots available
  440. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  441. +
  442. + // Routine to report the RX slot size
  443. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  444. +
  445. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  446. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  447. +
  448. + // Callback to inform a service that a Xon or Xoff message has been received
  449. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  450. +
  451. + // Callback to inform a service that a server available reply message has been received
  452. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  453. +
  454. + // Callback to indicate that bulk auxiliary messages have arrived
  455. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  456. +
  457. + // Callback to indicate that a bulk auxiliary message has been transmitted
  458. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  459. +
  460. + // Callback to provide information about the connection
  461. + VCHI_CONNECTION_INFO connection_info;
  462. +
  463. + // Callback to notify that peer has requested disconnect
  464. + VCHI_CONNECTION_DISCONNECT disconnect;
  465. +
  466. + // Callback to notify that peer has requested power change
  467. + VCHI_CONNECTION_POWER_CONTROL power_control;
  468. +
  469. + // allocate memory suitably aligned for this connection
  470. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  471. +
  472. + // free memory allocated by buffer_allocate
  473. + VCHI_BUFFER_FREE buffer_free;
  474. +
  475. +};
  476. +
  477. +struct vchi_connection_t {
  478. + const VCHI_CONNECTION_API_T *api;
  479. + VCHI_CONNECTION_STATE_T *state;
  480. +#ifdef VCHI_COARSE_LOCKING
  481. + struct semaphore sem;
  482. +#endif
  483. +};
  484. +
  485. +
  486. +#endif /* CONNECTION_H_ */
  487. +
  488. +/****************************** End of file **********************************/
  489. --- /dev/null
  490. +++ b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  491. @@ -0,0 +1,204 @@
  492. +/**
  493. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  494. + *
  495. + * Redistribution and use in source and binary forms, with or without
  496. + * modification, are permitted provided that the following conditions
  497. + * are met:
  498. + * 1. Redistributions of source code must retain the above copyright
  499. + * notice, this list of conditions, and the following disclaimer,
  500. + * without modification.
  501. + * 2. Redistributions in binary form must reproduce the above copyright
  502. + * notice, this list of conditions and the following disclaimer in the
  503. + * documentation and/or other materials provided with the distribution.
  504. + * 3. The names of the above-listed copyright holders may not be used
  505. + * to endorse or promote products derived from this software without
  506. + * specific prior written permission.
  507. + *
  508. + * ALTERNATIVELY, this software may be distributed under the terms of the
  509. + * GNU General Public License ("GPL") version 2, as published by the Free
  510. + * Software Foundation.
  511. + *
  512. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  513. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  514. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  515. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  516. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  517. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  518. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  519. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  520. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  521. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  522. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  523. + */
  524. +
  525. +#ifndef _VCHI_MESSAGE_H_
  526. +#define _VCHI_MESSAGE_H_
  527. +
  528. +#include <linux/kernel.h>
  529. +#include <linux/types.h>
  530. +#include <linux/semaphore.h>
  531. +
  532. +#include "interface/vchi/vchi_cfg_internal.h"
  533. +#include "interface/vchi/vchi_common.h"
  534. +
  535. +
  536. +typedef enum message_event_type {
  537. + MESSAGE_EVENT_NONE,
  538. + MESSAGE_EVENT_NOP,
  539. + MESSAGE_EVENT_MESSAGE,
  540. + MESSAGE_EVENT_SLOT_COMPLETE,
  541. + MESSAGE_EVENT_RX_BULK_PAUSED,
  542. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  543. + MESSAGE_EVENT_TX_COMPLETE,
  544. + MESSAGE_EVENT_MSG_DISCARDED
  545. +} MESSAGE_EVENT_TYPE_T;
  546. +
  547. +typedef enum vchi_msg_flags
  548. +{
  549. + VCHI_MSG_FLAGS_NONE = 0x0,
  550. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  551. +} VCHI_MSG_FLAGS_T;
  552. +
  553. +typedef enum message_tx_channel
  554. +{
  555. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  556. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  557. +} MESSAGE_TX_CHANNEL_T;
  558. +
  559. +// Macros used for cycling through bulk channels
  560. +#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)
  561. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  562. +
  563. +typedef enum message_rx_channel
  564. +{
  565. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  566. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  567. +} MESSAGE_RX_CHANNEL_T;
  568. +
  569. +// Message receive slot information
  570. +typedef struct rx_msg_slot_info {
  571. +
  572. + struct rx_msg_slot_info *next;
  573. + //struct slot_info *prev;
  574. +#if !defined VCHI_COARSE_LOCKING
  575. + struct semaphore sem;
  576. +#endif
  577. +
  578. + uint8_t *addr; // base address of slot
  579. + uint32_t len; // length of slot in bytes
  580. +
  581. + uint32_t write_ptr; // hardware causes this to advance
  582. + uint32_t read_ptr; // this module does the reading
  583. + int active; // is this slot in the hardware dma fifo?
  584. + uint32_t msgs_parsed; // count how many messages are in this slot
  585. + uint32_t msgs_released; // how many messages have been released
  586. + void *state; // connection state information
  587. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  588. +} RX_MSG_SLOTINFO_T;
  589. +
  590. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  591. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  592. +// driver will be tasked with sending the aligned core section.
  593. +typedef struct rx_bulk_slotinfo_t {
  594. + struct rx_bulk_slotinfo_t *next;
  595. +
  596. + struct semaphore *blocking;
  597. +
  598. + // needed by DMA
  599. + void *addr;
  600. + uint32_t len;
  601. +
  602. + // needed for the callback
  603. + void *service;
  604. + void *handle;
  605. + VCHI_FLAGS_T flags;
  606. +} RX_BULK_SLOTINFO_T;
  607. +
  608. +
  609. +/* ----------------------------------------------------------------------
  610. + * each connection driver will have a pool of the following struct.
  611. + *
  612. + * the pool will be managed by vchi_qman_*
  613. + * this means there will be multiple queues (single linked lists)
  614. + * a given struct message_info will be on exactly one of these queues
  615. + * at any one time
  616. + * -------------------------------------------------------------------- */
  617. +typedef struct rx_message_info {
  618. +
  619. + struct message_info *next;
  620. + //struct message_info *prev;
  621. +
  622. + uint8_t *addr;
  623. + uint32_t len;
  624. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  625. + uint32_t tx_timestamp;
  626. + uint32_t rx_timestamp;
  627. +
  628. +} RX_MESSAGE_INFO_T;
  629. +
  630. +typedef struct {
  631. + MESSAGE_EVENT_TYPE_T type;
  632. +
  633. + struct {
  634. + // for messages
  635. + void *addr; // address of message
  636. + uint16_t slot_delta; // whether this message indicated slot delta
  637. + uint32_t len; // length of message
  638. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  639. + int32_t service; // service id this message is destined for
  640. + uint32_t tx_timestamp; // timestamp from the header
  641. + uint32_t rx_timestamp; // timestamp when we parsed it
  642. + } message;
  643. +
  644. + // FIXME: cleanup slot reporting...
  645. + RX_MSG_SLOTINFO_T *rx_msg;
  646. + RX_BULK_SLOTINFO_T *rx_bulk;
  647. + void *tx_handle;
  648. + MESSAGE_TX_CHANNEL_T tx_channel;
  649. +
  650. +} MESSAGE_EVENT_T;
  651. +
  652. +
  653. +// callbacks
  654. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  655. +
  656. +typedef struct {
  657. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  658. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  659. +
  660. +
  661. +// handle to this instance of message driver (as returned by ->open)
  662. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  663. +
  664. +struct opaque_vchi_message_driver_t {
  665. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  666. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  667. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  668. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  669. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  670. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  671. + 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)
  672. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  673. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  674. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  675. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  676. +
  677. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  678. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  679. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  680. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  681. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  682. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  683. +
  684. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  685. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  686. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  687. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  688. + 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 );
  689. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  690. +};
  691. +
  692. +
  693. +#endif // _VCHI_MESSAGE_H_
  694. +
  695. +/****************************** End of file ***********************************/
  696. --- /dev/null
  697. +++ b/drivers/misc/vc04_services/interface/vchi/vchi.h
  698. @@ -0,0 +1,378 @@
  699. +/**
  700. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  701. + *
  702. + * Redistribution and use in source and binary forms, with or without
  703. + * modification, are permitted provided that the following conditions
  704. + * are met:
  705. + * 1. Redistributions of source code must retain the above copyright
  706. + * notice, this list of conditions, and the following disclaimer,
  707. + * without modification.
  708. + * 2. Redistributions in binary form must reproduce the above copyright
  709. + * notice, this list of conditions and the following disclaimer in the
  710. + * documentation and/or other materials provided with the distribution.
  711. + * 3. The names of the above-listed copyright holders may not be used
  712. + * to endorse or promote products derived from this software without
  713. + * specific prior written permission.
  714. + *
  715. + * ALTERNATIVELY, this software may be distributed under the terms of the
  716. + * GNU General Public License ("GPL") version 2, as published by the Free
  717. + * Software Foundation.
  718. + *
  719. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  720. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  721. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  722. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  723. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  724. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  725. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  726. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  727. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  728. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  729. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  730. + */
  731. +
  732. +#ifndef VCHI_H_
  733. +#define VCHI_H_
  734. +
  735. +#include "interface/vchi/vchi_cfg.h"
  736. +#include "interface/vchi/vchi_common.h"
  737. +#include "interface/vchi/connections/connection.h"
  738. +#include "vchi_mh.h"
  739. +
  740. +
  741. +/******************************************************************************
  742. + Global defs
  743. + *****************************************************************************/
  744. +
  745. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  746. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  747. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  748. +
  749. +#ifdef USE_VCHIQ_ARM
  750. +#define VCHI_BULK_ALIGNED(x) 1
  751. +#else
  752. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  753. +#endif
  754. +
  755. +struct vchi_version {
  756. + uint32_t version;
  757. + uint32_t version_min;
  758. +};
  759. +#define VCHI_VERSION(v_) { v_, v_ }
  760. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  761. +
  762. +typedef enum
  763. +{
  764. + VCHI_VEC_POINTER,
  765. + VCHI_VEC_HANDLE,
  766. + VCHI_VEC_LIST
  767. +} VCHI_MSG_VECTOR_TYPE_T;
  768. +
  769. +typedef struct vchi_msg_vector_ex {
  770. +
  771. + VCHI_MSG_VECTOR_TYPE_T type;
  772. + union
  773. + {
  774. + // a memory handle
  775. + struct
  776. + {
  777. + VCHI_MEM_HANDLE_T handle;
  778. + uint32_t offset;
  779. + int32_t vec_len;
  780. + } handle;
  781. +
  782. + // an ordinary data pointer
  783. + struct
  784. + {
  785. + const void *vec_base;
  786. + int32_t vec_len;
  787. + } ptr;
  788. +
  789. + // a nested vector list
  790. + struct
  791. + {
  792. + struct vchi_msg_vector_ex *vec;
  793. + uint32_t vec_len;
  794. + } list;
  795. + } u;
  796. +} VCHI_MSG_VECTOR_EX_T;
  797. +
  798. +
  799. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  800. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  801. +
  802. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  803. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  804. +
  805. +// Macros to manipulate 'FOURCC' values
  806. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  807. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  808. +
  809. +
  810. +// Opaque service information
  811. +struct opaque_vchi_service_t;
  812. +
  813. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  814. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  815. +typedef struct
  816. +{
  817. + struct opaque_vchi_service_t *service;
  818. + void *message;
  819. +} VCHI_HELD_MSG_T;
  820. +
  821. +
  822. +
  823. +// structure used to provide the information needed to open a server or a client
  824. +typedef struct {
  825. + struct vchi_version version;
  826. + int32_t service_id;
  827. + VCHI_CONNECTION_T *connection;
  828. + uint32_t rx_fifo_size;
  829. + uint32_t tx_fifo_size;
  830. + VCHI_CALLBACK_T callback;
  831. + void *callback_param;
  832. + /* client intends to receive bulk transfers of
  833. + odd lengths or into unaligned buffers */
  834. + int32_t want_unaligned_bulk_rx;
  835. + /* client intends to transmit bulk transfers of
  836. + odd lengths or out of unaligned buffers */
  837. + int32_t want_unaligned_bulk_tx;
  838. + /* client wants to check CRCs on (bulk) xfers.
  839. + Only needs to be set at 1 end - will do both directions. */
  840. + int32_t want_crc;
  841. +} SERVICE_CREATION_T;
  842. +
  843. +// Opaque handle for a VCHI instance
  844. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  845. +
  846. +// Opaque handle for a server or client
  847. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  848. +
  849. +// Service registration & startup
  850. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  851. +
  852. +typedef struct service_info_tag {
  853. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  854. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  855. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  856. +} SERVICE_INFO_T;
  857. +
  858. +/******************************************************************************
  859. + Global funcs - implementation is specific to which side you are on (local / remote)
  860. + *****************************************************************************/
  861. +
  862. +#ifdef __cplusplus
  863. +extern "C" {
  864. +#endif
  865. +
  866. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  867. + const VCHI_MESSAGE_DRIVER_T * low_level);
  868. +
  869. +
  870. +// Routine used to initialise the vchi on both local + remote connections
  871. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  872. +
  873. +extern int32_t vchi_exit( void );
  874. +
  875. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  876. + const uint32_t num_connections,
  877. + VCHI_INSTANCE_T instance_handle );
  878. +
  879. +//When this is called, ensure that all services have no data pending.
  880. +//Bulk transfers can remain 'queued'
  881. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  882. +
  883. +// Global control over bulk CRC checking
  884. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  885. + VCHI_CRC_CONTROL_T control );
  886. +
  887. +// helper functions
  888. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  889. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  890. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  891. +
  892. +
  893. +/******************************************************************************
  894. + Global service API
  895. + *****************************************************************************/
  896. +// Routine to create a named service
  897. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  898. + SERVICE_CREATION_T *setup,
  899. + VCHI_SERVICE_HANDLE_T *handle );
  900. +
  901. +// Routine to destory a service
  902. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  903. +
  904. +// Routine to open a named service
  905. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  906. + SERVICE_CREATION_T *setup,
  907. + VCHI_SERVICE_HANDLE_T *handle);
  908. +
  909. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  910. + short *peer_version );
  911. +
  912. +// Routine to close a named service
  913. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  914. +
  915. +// Routine to increment ref count on a named service
  916. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  917. +
  918. +// Routine to decrement ref count on a named service
  919. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  920. +
  921. +// Routine to set a control option for a named service
  922. +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
  923. + VCHI_SERVICE_OPTION_T option,
  924. + int value);
  925. +
  926. +// Routine to send a message across a service
  927. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  928. + const void *data,
  929. + uint32_t data_size,
  930. + VCHI_FLAGS_T flags,
  931. + void *msg_handle );
  932. +
  933. +// scatter-gather (vector) and send message
  934. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  935. + VCHI_MSG_VECTOR_EX_T *vector,
  936. + uint32_t count,
  937. + VCHI_FLAGS_T flags,
  938. + void *msg_handle );
  939. +
  940. +// legacy scatter-gather (vector) and send message, only handles pointers
  941. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  942. + VCHI_MSG_VECTOR_T *vector,
  943. + uint32_t count,
  944. + VCHI_FLAGS_T flags,
  945. + void *msg_handle );
  946. +
  947. +// Routine to receive a msg from a service
  948. +// Dequeue is equivalent to hold, copy into client buffer, release
  949. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  950. + void *data,
  951. + uint32_t max_data_size_to_read,
  952. + uint32_t *actual_msg_size,
  953. + VCHI_FLAGS_T flags );
  954. +
  955. +// Routine to look at a message in place.
  956. +// The message is not dequeued, so a subsequent call to peek or dequeue
  957. +// will return the same message.
  958. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  959. + void **data,
  960. + uint32_t *msg_size,
  961. + VCHI_FLAGS_T flags );
  962. +
  963. +// Routine to remove a message after it has been read in place with peek
  964. +// The first message on the queue is dequeued.
  965. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  966. +
  967. +// Routine to look at a message in place.
  968. +// The message is dequeued, so the caller is left holding it; the descriptor is
  969. +// filled in and must be released when the user has finished with the message.
  970. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  971. + void **data, // } may be NULL, as info can be
  972. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  973. + VCHI_FLAGS_T flags,
  974. + VCHI_HELD_MSG_T *message_descriptor );
  975. +
  976. +// Initialise an iterator to look through messages in place
  977. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  978. + VCHI_MSG_ITER_T *iter,
  979. + VCHI_FLAGS_T flags );
  980. +
  981. +/******************************************************************************
  982. + Global service support API - operations on held messages and message iterators
  983. + *****************************************************************************/
  984. +
  985. +// Routine to get the address of a held message
  986. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  987. +
  988. +// Routine to get the size of a held message
  989. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  990. +
  991. +// Routine to get the transmit timestamp as written into the header by the peer
  992. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  993. +
  994. +// Routine to get the reception timestamp, written as we parsed the header
  995. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  996. +
  997. +// Routine to release a held message after it has been processed
  998. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  999. +
  1000. +// Indicates whether the iterator has a next message.
  1001. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  1002. +
  1003. +// Return the pointer and length for the next message and advance the iterator.
  1004. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  1005. + void **data,
  1006. + uint32_t *msg_size );
  1007. +
  1008. +// Remove the last message returned by vchi_msg_iter_next.
  1009. +// Can only be called once after each call to vchi_msg_iter_next.
  1010. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  1011. +
  1012. +// Hold the last message returned by vchi_msg_iter_next.
  1013. +// Can only be called once after each call to vchi_msg_iter_next.
  1014. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  1015. + VCHI_HELD_MSG_T *message );
  1016. +
  1017. +// Return information for the next message, and hold it, advancing the iterator.
  1018. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  1019. + void **data, // } may be NULL
  1020. + uint32_t *msg_size, // }
  1021. + VCHI_HELD_MSG_T *message );
  1022. +
  1023. +
  1024. +/******************************************************************************
  1025. + Global bulk API
  1026. + *****************************************************************************/
  1027. +
  1028. +// Routine to prepare interface for a transfer from the other side
  1029. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  1030. + void *data_dst,
  1031. + uint32_t data_size,
  1032. + VCHI_FLAGS_T flags,
  1033. + void *transfer_handle );
  1034. +
  1035. +
  1036. +// Prepare interface for a transfer from the other side into relocatable memory.
  1037. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  1038. + VCHI_MEM_HANDLE_T h_dst,
  1039. + uint32_t offset,
  1040. + uint32_t data_size,
  1041. + const VCHI_FLAGS_T flags,
  1042. + void * const bulk_handle );
  1043. +
  1044. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  1045. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  1046. + const void *data_src,
  1047. + uint32_t data_size,
  1048. + VCHI_FLAGS_T flags,
  1049. + void *transfer_handle );
  1050. +
  1051. +
  1052. +/******************************************************************************
  1053. + Configuration plumbing
  1054. + *****************************************************************************/
  1055. +
  1056. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  1057. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  1058. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  1059. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  1060. +
  1061. +// declare all message drivers here
  1062. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  1063. +
  1064. +#ifdef __cplusplus
  1065. +}
  1066. +#endif
  1067. +
  1068. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  1069. + VCHI_MEM_HANDLE_T h_src,
  1070. + uint32_t offset,
  1071. + uint32_t data_size,
  1072. + VCHI_FLAGS_T flags,
  1073. + void *transfer_handle );
  1074. +#endif /* VCHI_H_ */
  1075. +
  1076. +/****************************** End of file **********************************/
  1077. --- /dev/null
  1078. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  1079. @@ -0,0 +1,224 @@
  1080. +/**
  1081. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1082. + *
  1083. + * Redistribution and use in source and binary forms, with or without
  1084. + * modification, are permitted provided that the following conditions
  1085. + * are met:
  1086. + * 1. Redistributions of source code must retain the above copyright
  1087. + * notice, this list of conditions, and the following disclaimer,
  1088. + * without modification.
  1089. + * 2. Redistributions in binary form must reproduce the above copyright
  1090. + * notice, this list of conditions and the following disclaimer in the
  1091. + * documentation and/or other materials provided with the distribution.
  1092. + * 3. The names of the above-listed copyright holders may not be used
  1093. + * to endorse or promote products derived from this software without
  1094. + * specific prior written permission.
  1095. + *
  1096. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1097. + * GNU General Public License ("GPL") version 2, as published by the Free
  1098. + * Software Foundation.
  1099. + *
  1100. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1101. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1102. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1103. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1104. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1105. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1106. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1107. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1108. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1109. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1110. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1111. + */
  1112. +
  1113. +#ifndef VCHI_CFG_H_
  1114. +#define VCHI_CFG_H_
  1115. +
  1116. +/****************************************************************************************
  1117. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  1118. + * services.
  1119. + ***************************************************************************************/
  1120. +
  1121. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  1122. +/* Really determined by the message driver, and should be available from a run-time call. */
  1123. +#ifndef VCHI_BULK_ALIGN
  1124. +# if __VCCOREVER__ >= 0x04000000
  1125. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  1126. +# else
  1127. +# define VCHI_BULK_ALIGN 16
  1128. +# endif
  1129. +#endif
  1130. +
  1131. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  1132. +/* May be less than or greater than VCHI_BULK_ALIGN */
  1133. +/* Really determined by the message driver, and should be available from a run-time call. */
  1134. +#ifndef VCHI_BULK_GRANULARITY
  1135. +# if __VCCOREVER__ >= 0x04000000
  1136. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  1137. +# else
  1138. +# define VCHI_BULK_GRANULARITY 16
  1139. +# endif
  1140. +#endif
  1141. +
  1142. +/* The largest possible message to be queued with vchi_msg_queue. */
  1143. +#ifndef VCHI_MAX_MSG_SIZE
  1144. +# if defined VCHI_LOCAL_HOST_PORT
  1145. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  1146. +# else
  1147. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  1148. +# endif
  1149. +#endif
  1150. +
  1151. +/******************************************************************************************
  1152. + * Defines below are system configuration options, and should not be used by VCHI services.
  1153. + *****************************************************************************************/
  1154. +
  1155. +/* How many connections can we support? A localhost implementation uses 2 connections,
  1156. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  1157. + * driver. */
  1158. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  1159. +# define VCHI_MAX_NUM_CONNECTIONS 3
  1160. +#endif
  1161. +
  1162. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  1163. + * amount of static memory. */
  1164. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  1165. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  1166. +#endif
  1167. +
  1168. +/* Adjust if using a message driver that supports more logical TX channels */
  1169. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  1170. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  1171. +#endif
  1172. +
  1173. +/* Adjust if using a message driver that supports more logical RX channels */
  1174. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  1175. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  1176. +#endif
  1177. +
  1178. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  1179. + * receive queue space, less message headers. */
  1180. +#ifndef VCHI_NUM_READ_SLOTS
  1181. +# if defined(VCHI_LOCAL_HOST_PORT)
  1182. +# define VCHI_NUM_READ_SLOTS 4
  1183. +# else
  1184. +# define VCHI_NUM_READ_SLOTS 48
  1185. +# endif
  1186. +#endif
  1187. +
  1188. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  1189. + * performance. Only define on VideoCore end, talking to host.
  1190. + */
  1191. +//#define VCHI_MSG_RX_OVERRUN
  1192. +
  1193. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  1194. + * underneath VCHI will usually have its own buffering. */
  1195. +#ifndef VCHI_NUM_WRITE_SLOTS
  1196. +# define VCHI_NUM_WRITE_SLOTS 4
  1197. +#endif
  1198. +
  1199. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  1200. + * then it's taking up too much buffer space, and the peer service will be told to stop
  1201. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  1202. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  1203. + * is too high. */
  1204. +#ifndef VCHI_XOFF_THRESHOLD
  1205. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  1206. +#endif
  1207. +
  1208. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  1209. + * service has dequeued/released enough messages that it's now occupying
  1210. + * VCHI_XON_THRESHOLD slots or fewer. */
  1211. +#ifndef VCHI_XON_THRESHOLD
  1212. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  1213. +#endif
  1214. +
  1215. +/* A size below which a bulk transfer omits the handshake completely and always goes
  1216. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  1217. + * can guarantee this by enabling unaligned transmits).
  1218. + * Not API. */
  1219. +#ifndef VCHI_MIN_BULK_SIZE
  1220. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  1221. +#endif
  1222. +
  1223. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  1224. + * speed and latency; the smaller the chunk size the better change of messages and other
  1225. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  1226. + * break transmissions into chunks.
  1227. + */
  1228. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  1229. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  1230. +#endif
  1231. +
  1232. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  1233. + * with multiple-line frames. Only use if the receiver can cope. */
  1234. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  1235. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  1236. +#endif
  1237. +
  1238. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  1239. + * vchi_msg_queue will be blocked. */
  1240. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  1241. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  1242. +#endif
  1243. +
  1244. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  1245. + * will be suspended until older messages are dequeued/released. */
  1246. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  1247. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  1248. +#endif
  1249. +
  1250. +/* Really should be able to cope if we run out of received message descriptors, by
  1251. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  1252. + * under the carpet. */
  1253. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  1254. +# undef VCHI_RX_MSG_QUEUE_SIZE
  1255. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  1256. +#endif
  1257. +
  1258. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  1259. + * will be blocked. */
  1260. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  1261. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  1262. +#endif
  1263. +
  1264. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  1265. + * will be blocked. */
  1266. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  1267. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  1268. +#endif
  1269. +
  1270. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  1271. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  1272. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  1273. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  1274. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  1275. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  1276. +#endif
  1277. +
  1278. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  1279. + * transmitter on and off.
  1280. + */
  1281. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  1282. +
  1283. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  1284. +
  1285. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  1286. + * negative for no IDLE.
  1287. + */
  1288. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  1289. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  1290. +# endif
  1291. +
  1292. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  1293. + * negative for no OFF.
  1294. + */
  1295. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  1296. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  1297. +# endif
  1298. +
  1299. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  1300. +
  1301. +#endif /* VCHI_CFG_H_ */
  1302. +
  1303. +/****************************** End of file **********************************/
  1304. --- /dev/null
  1305. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  1306. @@ -0,0 +1,71 @@
  1307. +/**
  1308. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1309. + *
  1310. + * Redistribution and use in source and binary forms, with or without
  1311. + * modification, are permitted provided that the following conditions
  1312. + * are met:
  1313. + * 1. Redistributions of source code must retain the above copyright
  1314. + * notice, this list of conditions, and the following disclaimer,
  1315. + * without modification.
  1316. + * 2. Redistributions in binary form must reproduce the above copyright
  1317. + * notice, this list of conditions and the following disclaimer in the
  1318. + * documentation and/or other materials provided with the distribution.
  1319. + * 3. The names of the above-listed copyright holders may not be used
  1320. + * to endorse or promote products derived from this software without
  1321. + * specific prior written permission.
  1322. + *
  1323. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1324. + * GNU General Public License ("GPL") version 2, as published by the Free
  1325. + * Software Foundation.
  1326. + *
  1327. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1328. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1329. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1330. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1331. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1332. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1333. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1334. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1335. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1336. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1337. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1338. + */
  1339. +
  1340. +#ifndef VCHI_CFG_INTERNAL_H_
  1341. +#define VCHI_CFG_INTERNAL_H_
  1342. +
  1343. +/****************************************************************************************
  1344. + * Control optimisation attempts.
  1345. + ***************************************************************************************/
  1346. +
  1347. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  1348. +#define VCHI_COARSE_LOCKING
  1349. +
  1350. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  1351. +// (only relevant if VCHI_COARSE_LOCKING)
  1352. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  1353. +
  1354. +// Avoid lock on non-blocking peek
  1355. +// (only relevant if VCHI_COARSE_LOCKING)
  1356. +#define VCHI_AVOID_PEEK_LOCK
  1357. +
  1358. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  1359. +#define VCHI_MULTIPLE_HANDLER_THREADS
  1360. +
  1361. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  1362. +// our way through the pool of descriptors.
  1363. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  1364. +
  1365. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  1366. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  1367. +
  1368. +// Don't use message descriptors for TX messages that don't need them
  1369. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  1370. +
  1371. +// Nano-locks for multiqueue
  1372. +//#define VCHI_MQUEUE_NANOLOCKS
  1373. +
  1374. +// Lock-free(er) dequeuing
  1375. +//#define VCHI_RX_NANOLOCKS
  1376. +
  1377. +#endif /*VCHI_CFG_INTERNAL_H_*/
  1378. --- /dev/null
  1379. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  1380. @@ -0,0 +1,174 @@
  1381. +/**
  1382. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1383. + *
  1384. + * Redistribution and use in source and binary forms, with or without
  1385. + * modification, are permitted provided that the following conditions
  1386. + * are met:
  1387. + * 1. Redistributions of source code must retain the above copyright
  1388. + * notice, this list of conditions, and the following disclaimer,
  1389. + * without modification.
  1390. + * 2. Redistributions in binary form must reproduce the above copyright
  1391. + * notice, this list of conditions and the following disclaimer in the
  1392. + * documentation and/or other materials provided with the distribution.
  1393. + * 3. The names of the above-listed copyright holders may not be used
  1394. + * to endorse or promote products derived from this software without
  1395. + * specific prior written permission.
  1396. + *
  1397. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1398. + * GNU General Public License ("GPL") version 2, as published by the Free
  1399. + * Software Foundation.
  1400. + *
  1401. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1402. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1403. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1404. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1405. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1406. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1407. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1408. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1409. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1410. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1411. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1412. + */
  1413. +
  1414. +#ifndef VCHI_COMMON_H_
  1415. +#define VCHI_COMMON_H_
  1416. +
  1417. +
  1418. +//flags used when sending messages (must be bitmapped)
  1419. +typedef enum
  1420. +{
  1421. + VCHI_FLAGS_NONE = 0x0,
  1422. + 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)
  1423. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  1424. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  1425. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  1426. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  1427. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  1428. +
  1429. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  1430. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  1431. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  1432. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  1433. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  1434. + VCHI_FLAGS_INTERNAL = 0xFF0000
  1435. +} VCHI_FLAGS_T;
  1436. +
  1437. +// constants for vchi_crc_control()
  1438. +typedef enum {
  1439. + VCHI_CRC_NOTHING = -1,
  1440. + VCHI_CRC_PER_SERVICE = 0,
  1441. + VCHI_CRC_EVERYTHING = 1,
  1442. +} VCHI_CRC_CONTROL_T;
  1443. +
  1444. +//callback reasons when an event occurs on a service
  1445. +typedef enum
  1446. +{
  1447. + VCHI_CALLBACK_REASON_MIN,
  1448. +
  1449. + //This indicates that there is data available
  1450. + //handle is the msg id that was transmitted with the data
  1451. + // When a message is received and there was no FULL message available previously, send callback
  1452. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  1453. + VCHI_CALLBACK_MSG_AVAILABLE,
  1454. + VCHI_CALLBACK_MSG_SENT,
  1455. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  1456. +
  1457. + // This indicates that a transfer from the other side has completed
  1458. + VCHI_CALLBACK_BULK_RECEIVED,
  1459. + //This indicates that data queued up to be sent has now gone
  1460. + //handle is the msg id that was used when sending the data
  1461. + VCHI_CALLBACK_BULK_SENT,
  1462. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  1463. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  1464. +
  1465. + VCHI_CALLBACK_SERVICE_CLOSED,
  1466. +
  1467. + // this side has sent XOFF to peer due to lack of data consumption by service
  1468. + // (suggests the service may need to take some recovery action if it has
  1469. + // been deliberately holding off consuming data)
  1470. + VCHI_CALLBACK_SENT_XOFF,
  1471. + VCHI_CALLBACK_SENT_XON,
  1472. +
  1473. + // indicates that a bulk transfer has finished reading the source buffer
  1474. + VCHI_CALLBACK_BULK_DATA_READ,
  1475. +
  1476. + // power notification events (currently host side only)
  1477. + VCHI_CALLBACK_PEER_OFF,
  1478. + VCHI_CALLBACK_PEER_SUSPENDED,
  1479. + VCHI_CALLBACK_PEER_ON,
  1480. + VCHI_CALLBACK_PEER_RESUMED,
  1481. + VCHI_CALLBACK_FORCED_POWER_OFF,
  1482. +
  1483. +#ifdef USE_VCHIQ_ARM
  1484. + // some extra notifications provided by vchiq_arm
  1485. + VCHI_CALLBACK_SERVICE_OPENED,
  1486. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  1487. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  1488. +#endif
  1489. +
  1490. + VCHI_CALLBACK_REASON_MAX
  1491. +} VCHI_CALLBACK_REASON_T;
  1492. +
  1493. +// service control options
  1494. +typedef enum
  1495. +{
  1496. + VCHI_SERVICE_OPTION_MIN,
  1497. +
  1498. + VCHI_SERVICE_OPTION_TRACE,
  1499. +
  1500. + VCHI_SERVICE_OPTION_MAX
  1501. +} VCHI_SERVICE_OPTION_T;
  1502. +
  1503. +
  1504. +//Callback used by all services / bulk transfers
  1505. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  1506. + VCHI_CALLBACK_REASON_T reason,
  1507. + void *handle ); //for transmitting msg's only
  1508. +
  1509. +
  1510. +
  1511. +/*
  1512. + * Define vector struct for scatter-gather (vector) operations
  1513. + * Vectors can be nested - if a vector element has negative length, then
  1514. + * the data pointer is treated as pointing to another vector array, with
  1515. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  1516. + * you can do this:
  1517. + *
  1518. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  1519. + * {
  1520. + * VCHI_MSG_VECTOR_T nv[2];
  1521. + * nv[0].vec_base = my_header;
  1522. + * nv[0].vec_len = sizeof my_header;
  1523. + * nv[1].vec_base = v;
  1524. + * nv[1].vec_len = -n;
  1525. + * ...
  1526. + *
  1527. + */
  1528. +typedef struct vchi_msg_vector {
  1529. + const void *vec_base;
  1530. + int32_t vec_len;
  1531. +} VCHI_MSG_VECTOR_T;
  1532. +
  1533. +// Opaque type for a connection API
  1534. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  1535. +
  1536. +// Opaque type for a message driver
  1537. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  1538. +
  1539. +
  1540. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  1541. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  1542. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  1543. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  1544. +// is used again after messages for that service are removed/dequeued by any
  1545. +// means other than vchi_msg_iter_... calls on the iterator itself.
  1546. +typedef struct {
  1547. + struct opaque_vchi_service_t *service;
  1548. + void *last;
  1549. + void *next;
  1550. + void *remove;
  1551. +} VCHI_MSG_ITER_T;
  1552. +
  1553. +
  1554. +#endif // VCHI_COMMON_H_
  1555. --- /dev/null
  1556. +++ b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  1557. @@ -0,0 +1,42 @@
  1558. +/**
  1559. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1560. + *
  1561. + * Redistribution and use in source and binary forms, with or without
  1562. + * modification, are permitted provided that the following conditions
  1563. + * are met:
  1564. + * 1. Redistributions of source code must retain the above copyright
  1565. + * notice, this list of conditions, and the following disclaimer,
  1566. + * without modification.
  1567. + * 2. Redistributions in binary form must reproduce the above copyright
  1568. + * notice, this list of conditions and the following disclaimer in the
  1569. + * documentation and/or other materials provided with the distribution.
  1570. + * 3. The names of the above-listed copyright holders may not be used
  1571. + * to endorse or promote products derived from this software without
  1572. + * specific prior written permission.
  1573. + *
  1574. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1575. + * GNU General Public License ("GPL") version 2, as published by the Free
  1576. + * Software Foundation.
  1577. + *
  1578. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1579. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1580. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1581. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1582. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1583. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1584. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1585. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1586. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1587. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1588. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1589. + */
  1590. +
  1591. +#ifndef VCHI_MH_H_
  1592. +#define VCHI_MH_H_
  1593. +
  1594. +#include <linux/types.h>
  1595. +
  1596. +typedef int32_t VCHI_MEM_HANDLE_T;
  1597. +#define VCHI_MEM_HANDLE_INVALID 0
  1598. +
  1599. +#endif
  1600. --- /dev/null
  1601. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  1602. @@ -0,0 +1,40 @@
  1603. +/**
  1604. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1605. + *
  1606. + * Redistribution and use in source and binary forms, with or without
  1607. + * modification, are permitted provided that the following conditions
  1608. + * are met:
  1609. + * 1. Redistributions of source code must retain the above copyright
  1610. + * notice, this list of conditions, and the following disclaimer,
  1611. + * without modification.
  1612. + * 2. Redistributions in binary form must reproduce the above copyright
  1613. + * notice, this list of conditions and the following disclaimer in the
  1614. + * documentation and/or other materials provided with the distribution.
  1615. + * 3. The names of the above-listed copyright holders may not be used
  1616. + * to endorse or promote products derived from this software without
  1617. + * specific prior written permission.
  1618. + *
  1619. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1620. + * GNU General Public License ("GPL") version 2, as published by the Free
  1621. + * Software Foundation.
  1622. + *
  1623. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1624. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1625. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1626. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1627. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1628. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1629. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1630. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1631. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1632. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1633. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1634. + */
  1635. +
  1636. +#ifndef VCHIQ_VCHIQ_H
  1637. +#define VCHIQ_VCHIQ_H
  1638. +
  1639. +#include "vchiq_if.h"
  1640. +#include "vchiq_util.h"
  1641. +
  1642. +#endif
  1643. --- /dev/null
  1644. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  1645. @@ -0,0 +1,42 @@
  1646. +/**
  1647. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1648. + *
  1649. + * Redistribution and use in source and binary forms, with or without
  1650. + * modification, are permitted provided that the following conditions
  1651. + * are met:
  1652. + * 1. Redistributions of source code must retain the above copyright
  1653. + * notice, this list of conditions, and the following disclaimer,
  1654. + * without modification.
  1655. + * 2. Redistributions in binary form must reproduce the above copyright
  1656. + * notice, this list of conditions and the following disclaimer in the
  1657. + * documentation and/or other materials provided with the distribution.
  1658. + * 3. The names of the above-listed copyright holders may not be used
  1659. + * to endorse or promote products derived from this software without
  1660. + * specific prior written permission.
  1661. + *
  1662. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1663. + * GNU General Public License ("GPL") version 2, as published by the Free
  1664. + * Software Foundation.
  1665. + *
  1666. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1667. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1668. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1669. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1670. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1671. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1672. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1673. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1674. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1675. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1676. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1677. + */
  1678. +
  1679. +#ifndef VCHIQ_2835_H
  1680. +#define VCHIQ_2835_H
  1681. +
  1682. +#include "vchiq_pagelist.h"
  1683. +
  1684. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  1685. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  1686. +
  1687. +#endif /* VCHIQ_2835_H */
  1688. --- /dev/null
  1689. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  1690. @@ -0,0 +1,561 @@
  1691. +/**
  1692. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  1693. + *
  1694. + * Redistribution and use in source and binary forms, with or without
  1695. + * modification, are permitted provided that the following conditions
  1696. + * are met:
  1697. + * 1. Redistributions of source code must retain the above copyright
  1698. + * notice, this list of conditions, and the following disclaimer,
  1699. + * without modification.
  1700. + * 2. Redistributions in binary form must reproduce the above copyright
  1701. + * notice, this list of conditions and the following disclaimer in the
  1702. + * documentation and/or other materials provided with the distribution.
  1703. + * 3. The names of the above-listed copyright holders may not be used
  1704. + * to endorse or promote products derived from this software without
  1705. + * specific prior written permission.
  1706. + *
  1707. + * ALTERNATIVELY, this software may be distributed under the terms of the
  1708. + * GNU General Public License ("GPL") version 2, as published by the Free
  1709. + * Software Foundation.
  1710. + *
  1711. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  1712. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  1713. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  1714. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  1715. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  1716. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  1717. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  1718. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1719. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1720. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1721. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1722. + */
  1723. +
  1724. +#include <linux/kernel.h>
  1725. +#include <linux/types.h>
  1726. +#include <linux/errno.h>
  1727. +#include <linux/interrupt.h>
  1728. +#include <linux/irq.h>
  1729. +#include <linux/pagemap.h>
  1730. +#include <linux/dma-mapping.h>
  1731. +#include <linux/version.h>
  1732. +#include <linux/io.h>
  1733. +#include <linux/uaccess.h>
  1734. +#include <asm/pgtable.h>
  1735. +
  1736. +#include <mach/irqs.h>
  1737. +
  1738. +#include <mach/platform.h>
  1739. +#include <mach/vcio.h>
  1740. +
  1741. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  1742. +
  1743. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  1744. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  1745. +
  1746. +#include "vchiq_arm.h"
  1747. +#include "vchiq_2835.h"
  1748. +#include "vchiq_connected.h"
  1749. +
  1750. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  1751. +
  1752. +typedef struct vchiq_2835_state_struct {
  1753. + int inited;
  1754. + VCHIQ_ARM_STATE_T arm_state;
  1755. +} VCHIQ_2835_ARM_STATE_T;
  1756. +
  1757. +static char *g_slot_mem;
  1758. +static int g_slot_mem_size;
  1759. +dma_addr_t g_slot_phys;
  1760. +static FRAGMENTS_T *g_fragments_base;
  1761. +static FRAGMENTS_T *g_free_fragments;
  1762. +struct semaphore g_free_fragments_sema;
  1763. +
  1764. +extern int vchiq_arm_log_level;
  1765. +
  1766. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  1767. +
  1768. +static irqreturn_t
  1769. +vchiq_doorbell_irq(int irq, void *dev_id);
  1770. +
  1771. +static int
  1772. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  1773. + struct task_struct *task, PAGELIST_T ** ppagelist);
  1774. +
  1775. +static void
  1776. +free_pagelist(PAGELIST_T *pagelist, int actual);
  1777. +
  1778. +int __init
  1779. +vchiq_platform_init(VCHIQ_STATE_T *state)
  1780. +{
  1781. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  1782. + int frag_mem_size;
  1783. + int err;
  1784. + int i;
  1785. +
  1786. + /* Allocate space for the channels in coherent memory */
  1787. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  1788. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  1789. +
  1790. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  1791. + &g_slot_phys, GFP_ATOMIC);
  1792. +
  1793. + if (!g_slot_mem) {
  1794. + vchiq_log_error(vchiq_arm_log_level,
  1795. + "Unable to allocate channel memory");
  1796. + err = -ENOMEM;
  1797. + goto failed_alloc;
  1798. + }
  1799. +
  1800. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  1801. +
  1802. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  1803. + if (!vchiq_slot_zero) {
  1804. + err = -EINVAL;
  1805. + goto failed_init_slots;
  1806. + }
  1807. +
  1808. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  1809. + (int)g_slot_phys + g_slot_mem_size;
  1810. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  1811. + MAX_FRAGMENTS;
  1812. +
  1813. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  1814. + g_slot_mem_size += frag_mem_size;
  1815. +
  1816. + g_free_fragments = g_fragments_base;
  1817. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  1818. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  1819. + &g_fragments_base[i + 1];
  1820. + }
  1821. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  1822. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  1823. +
  1824. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  1825. + VCHIQ_SUCCESS) {
  1826. + err = -EINVAL;
  1827. + goto failed_vchiq_init;
  1828. + }
  1829. +
  1830. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  1831. + IRQF_IRQPOLL, "VCHIQ doorbell",
  1832. + state);
  1833. + if (err < 0) {
  1834. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  1835. + "irq=%d err=%d", __func__,
  1836. + VCHIQ_DOORBELL_IRQ, err);
  1837. + goto failed_request_irq;
  1838. + }
  1839. +
  1840. + /* Send the base address of the slots to VideoCore */
  1841. +
  1842. + dsb(); /* Ensure all writes have completed */
  1843. +
  1844. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  1845. +
  1846. + vchiq_log_info(vchiq_arm_log_level,
  1847. + "vchiq_init - done (slots %x, phys %x)",
  1848. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  1849. +
  1850. + vchiq_call_connected_callbacks();
  1851. +
  1852. + return 0;
  1853. +
  1854. +failed_request_irq:
  1855. +failed_vchiq_init:
  1856. +failed_init_slots:
  1857. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  1858. +
  1859. +failed_alloc:
  1860. + return err;
  1861. +}
  1862. +
  1863. +void __exit
  1864. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  1865. +{
  1866. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  1867. + dma_free_coherent(NULL, g_slot_mem_size,
  1868. + g_slot_mem, g_slot_phys);
  1869. +}
  1870. +
  1871. +
  1872. +VCHIQ_STATUS_T
  1873. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  1874. +{
  1875. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  1876. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  1877. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  1878. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  1879. + if(status != VCHIQ_SUCCESS)
  1880. + {
  1881. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  1882. + }
  1883. + return status;
  1884. +}
  1885. +
  1886. +VCHIQ_ARM_STATE_T*
  1887. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  1888. +{
  1889. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  1890. + {
  1891. + BUG();
  1892. + }
  1893. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  1894. +}
  1895. +
  1896. +void
  1897. +remote_event_signal(REMOTE_EVENT_T *event)
  1898. +{
  1899. + wmb();
  1900. +
  1901. + event->fired = 1;
  1902. +
  1903. + dsb(); /* data barrier operation */
  1904. +
  1905. + if (event->armed) {
  1906. + /* trigger vc interrupt */
  1907. +
  1908. + writel(0, __io_address(ARM_0_BELL2));
  1909. + }
  1910. +}
  1911. +
  1912. +int
  1913. +vchiq_copy_from_user(void *dst, const void *src, int size)
  1914. +{
  1915. + if ((uint32_t)src < TASK_SIZE) {
  1916. + return copy_from_user(dst, src, size);
  1917. + } else {
  1918. + memcpy(dst, src, size);
  1919. + return 0;
  1920. + }
  1921. +}
  1922. +
  1923. +VCHIQ_STATUS_T
  1924. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  1925. + void *offset, int size, int dir)
  1926. +{
  1927. + PAGELIST_T *pagelist;
  1928. + int ret;
  1929. +
  1930. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  1931. +
  1932. + ret = create_pagelist((char __user *)offset, size,
  1933. + (dir == VCHIQ_BULK_RECEIVE)
  1934. + ? PAGELIST_READ
  1935. + : PAGELIST_WRITE,
  1936. + current,
  1937. + &pagelist);
  1938. + if (ret != 0)
  1939. + return VCHIQ_ERROR;
  1940. +
  1941. + bulk->handle = memhandle;
  1942. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  1943. +
  1944. + /* Store the pagelist address in remote_data, which isn't used by the
  1945. + slave. */
  1946. + bulk->remote_data = pagelist;
  1947. +
  1948. + return VCHIQ_SUCCESS;
  1949. +}
  1950. +
  1951. +void
  1952. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  1953. +{
  1954. + if (bulk && bulk->remote_data && bulk->actual)
  1955. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  1956. +}
  1957. +
  1958. +void
  1959. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  1960. +{
  1961. + /*
  1962. + * This should only be called on the master (VideoCore) side, but
  1963. + * provide an implementation to avoid the need for ifdefery.
  1964. + */
  1965. + BUG();
  1966. +}
  1967. +
  1968. +void
  1969. +vchiq_dump_platform_state(void *dump_context)
  1970. +{
  1971. + char buf[80];
  1972. + int len;
  1973. + len = snprintf(buf, sizeof(buf),
  1974. + " Platform: 2835 (VC master)");
  1975. + vchiq_dump(dump_context, buf, len + 1);
  1976. +}
  1977. +
  1978. +VCHIQ_STATUS_T
  1979. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  1980. +{
  1981. + return VCHIQ_ERROR;
  1982. +}
  1983. +
  1984. +VCHIQ_STATUS_T
  1985. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  1986. +{
  1987. + return VCHIQ_SUCCESS;
  1988. +}
  1989. +
  1990. +void
  1991. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  1992. +{
  1993. +}
  1994. +
  1995. +void
  1996. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  1997. +{
  1998. +}
  1999. +
  2000. +int
  2001. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  2002. +{
  2003. + return 1; // autosuspend not supported - videocore always wanted
  2004. +}
  2005. +
  2006. +int
  2007. +vchiq_platform_use_suspend_timer(void)
  2008. +{
  2009. + return 0;
  2010. +}
  2011. +void
  2012. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  2013. +{
  2014. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  2015. +}
  2016. +void
  2017. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  2018. +{
  2019. + (void)state;
  2020. +}
  2021. +/*
  2022. + * Local functions
  2023. + */
  2024. +
  2025. +static irqreturn_t
  2026. +vchiq_doorbell_irq(int irq, void *dev_id)
  2027. +{
  2028. + VCHIQ_STATE_T *state = dev_id;
  2029. + irqreturn_t ret = IRQ_NONE;
  2030. + unsigned int status;
  2031. +
  2032. + /* Read (and clear) the doorbell */
  2033. + status = readl(__io_address(ARM_0_BELL0));
  2034. +
  2035. + if (status & 0x4) { /* Was the doorbell rung? */
  2036. + remote_event_pollall(state);
  2037. + ret = IRQ_HANDLED;
  2038. + }
  2039. +
  2040. + return ret;
  2041. +}
  2042. +
  2043. +/* There is a potential problem with partial cache lines (pages?)
  2044. +** at the ends of the block when reading. If the CPU accessed anything in
  2045. +** the same line (page?) then it may have pulled old data into the cache,
  2046. +** obscuring the new data underneath. We can solve this by transferring the
  2047. +** partial cache lines separately, and allowing the ARM to copy into the
  2048. +** cached area.
  2049. +
  2050. +** N.B. This implementation plays slightly fast and loose with the Linux
  2051. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  2052. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  2053. +** from increased speed as a result.
  2054. +*/
  2055. +
  2056. +static int
  2057. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  2058. + struct task_struct *task, PAGELIST_T ** ppagelist)
  2059. +{
  2060. + PAGELIST_T *pagelist;
  2061. + struct page **pages;
  2062. + struct page *page;
  2063. + unsigned long *addrs;
  2064. + unsigned int num_pages, offset, i;
  2065. + char *addr, *base_addr, *next_addr;
  2066. + int run, addridx, actual_pages;
  2067. + unsigned long *need_release;
  2068. +
  2069. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  2070. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  2071. +
  2072. + *ppagelist = NULL;
  2073. +
  2074. + /* Allocate enough storage to hold the page pointers and the page
  2075. + ** list
  2076. + */
  2077. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  2078. + (num_pages * sizeof(unsigned long)) +
  2079. + sizeof(unsigned long) +
  2080. + (num_pages * sizeof(pages[0])),
  2081. + GFP_KERNEL);
  2082. +
  2083. + vchiq_log_trace(vchiq_arm_log_level,
  2084. + "create_pagelist - %x", (unsigned int)pagelist);
  2085. + if (!pagelist)
  2086. + return -ENOMEM;
  2087. +
  2088. + addrs = pagelist->addrs;
  2089. + need_release = (unsigned long *)(addrs + num_pages);
  2090. + pages = (struct page **)(addrs + num_pages + 1);
  2091. +
  2092. + if (is_vmalloc_addr(buf)) {
  2093. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  2094. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  2095. + }
  2096. + *need_release = 0; /* do not try and release vmalloc pages */
  2097. + } else {
  2098. + down_read(&task->mm->mmap_sem);
  2099. + actual_pages = get_user_pages(task, task->mm,
  2100. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  2101. + num_pages,
  2102. + (type == PAGELIST_READ) /*Write */ ,
  2103. + 0 /*Force */ ,
  2104. + pages,
  2105. + NULL /*vmas */);
  2106. + up_read(&task->mm->mmap_sem);
  2107. +
  2108. + if (actual_pages != num_pages) {
  2109. + vchiq_log_info(vchiq_arm_log_level,
  2110. + "create_pagelist - only %d/%d pages locked",
  2111. + actual_pages,
  2112. + num_pages);
  2113. +
  2114. + /* This is probably due to the process being killed */
  2115. + while (actual_pages > 0)
  2116. + {
  2117. + actual_pages--;
  2118. + page_cache_release(pages[actual_pages]);
  2119. + }
  2120. + kfree(pagelist);
  2121. + if (actual_pages == 0)
  2122. + actual_pages = -ENOMEM;
  2123. + return actual_pages;
  2124. + }
  2125. + *need_release = 1; /* release user pages */
  2126. + }
  2127. +
  2128. + pagelist->length = count;
  2129. + pagelist->type = type;
  2130. + pagelist->offset = offset;
  2131. +
  2132. + /* Group the pages into runs of contiguous pages */
  2133. +
  2134. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  2135. + next_addr = base_addr + PAGE_SIZE;
  2136. + addridx = 0;
  2137. + run = 0;
  2138. +
  2139. + for (i = 1; i < num_pages; i++) {
  2140. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  2141. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  2142. + next_addr += PAGE_SIZE;
  2143. + run++;
  2144. + } else {
  2145. + addrs[addridx] = (unsigned long)base_addr + run;
  2146. + addridx++;
  2147. + base_addr = addr;
  2148. + next_addr = addr + PAGE_SIZE;
  2149. + run = 0;
  2150. + }
  2151. + }
  2152. +
  2153. + addrs[addridx] = (unsigned long)base_addr + run;
  2154. + addridx++;
  2155. +
  2156. + /* Partial cache lines (fragments) require special measures */
  2157. + if ((type == PAGELIST_READ) &&
  2158. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  2159. + ((pagelist->offset + pagelist->length) &
  2160. + (CACHE_LINE_SIZE - 1)))) {
  2161. + FRAGMENTS_T *fragments;
  2162. +
  2163. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  2164. + kfree(pagelist);
  2165. + return -EINTR;
  2166. + }
  2167. +
  2168. + WARN_ON(g_free_fragments == NULL);
  2169. +
  2170. + down(&g_free_fragments_mutex);
  2171. + fragments = (FRAGMENTS_T *) g_free_fragments;
  2172. + WARN_ON(fragments == NULL);
  2173. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  2174. + up(&g_free_fragments_mutex);
  2175. + pagelist->type =
  2176. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  2177. + g_fragments_base);
  2178. + }
  2179. +
  2180. + for (page = virt_to_page(pagelist);
  2181. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  2182. + flush_dcache_page(page);
  2183. + }
  2184. +
  2185. + *ppagelist = pagelist;
  2186. +
  2187. + return 0;
  2188. +}
  2189. +
  2190. +static void
  2191. +free_pagelist(PAGELIST_T *pagelist, int actual)
  2192. +{
  2193. + unsigned long *need_release;
  2194. + struct page **pages;
  2195. + unsigned int num_pages, i;
  2196. +
  2197. + vchiq_log_trace(vchiq_arm_log_level,
  2198. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  2199. +
  2200. + num_pages =
  2201. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  2202. + PAGE_SIZE;
  2203. +
  2204. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  2205. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  2206. +
  2207. + /* Deal with any partial cache lines (fragments) */
  2208. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  2209. + FRAGMENTS_T *fragments = g_fragments_base +
  2210. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  2211. + int head_bytes, tail_bytes;
  2212. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  2213. + (CACHE_LINE_SIZE - 1);
  2214. + tail_bytes = (pagelist->offset + actual) &
  2215. + (CACHE_LINE_SIZE - 1);
  2216. +
  2217. + if ((actual >= 0) && (head_bytes != 0)) {
  2218. + if (head_bytes > actual)
  2219. + head_bytes = actual;
  2220. +
  2221. + memcpy((char *)page_address(pages[0]) +
  2222. + pagelist->offset,
  2223. + fragments->headbuf,
  2224. + head_bytes);
  2225. + }
  2226. + if ((actual >= 0) && (head_bytes < actual) &&
  2227. + (tail_bytes != 0)) {
  2228. + memcpy((char *)page_address(pages[num_pages - 1]) +
  2229. + ((pagelist->offset + actual) &
  2230. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  2231. + fragments->tailbuf, tail_bytes);
  2232. + }
  2233. +
  2234. + down(&g_free_fragments_mutex);
  2235. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  2236. + g_free_fragments = fragments;
  2237. + up(&g_free_fragments_mutex);
  2238. + up(&g_free_fragments_sema);
  2239. + }
  2240. +
  2241. + if (*need_release) {
  2242. + for (i = 0; i < num_pages; i++) {
  2243. + if (pagelist->type != PAGELIST_WRITE)
  2244. + set_page_dirty(pages[i]);
  2245. +
  2246. + page_cache_release(pages[i]);
  2247. + }
  2248. + }
  2249. +
  2250. + kfree(pagelist);
  2251. +}
  2252. --- /dev/null
  2253. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  2254. @@ -0,0 +1,2883 @@
  2255. +/**
  2256. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  2257. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  2258. + *
  2259. + * Redistribution and use in source and binary forms, with or without
  2260. + * modification, are permitted provided that the following conditions
  2261. + * are met:
  2262. + * 1. Redistributions of source code must retain the above copyright
  2263. + * notice, this list of conditions, and the following disclaimer,
  2264. + * without modification.
  2265. + * 2. Redistributions in binary form must reproduce the above copyright
  2266. + * notice, this list of conditions and the following disclaimer in the
  2267. + * documentation and/or other materials provided with the distribution.
  2268. + * 3. The names of the above-listed copyright holders may not be used
  2269. + * to endorse or promote products derived from this software without
  2270. + * specific prior written permission.
  2271. + *
  2272. + * ALTERNATIVELY, this software may be distributed under the terms of the
  2273. + * GNU General Public License ("GPL") version 2, as published by the Free
  2274. + * Software Foundation.
  2275. + *
  2276. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  2277. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  2278. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  2279. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  2280. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  2281. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  2282. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  2283. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2284. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2285. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2286. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2287. + */
  2288. +
  2289. +#include <linux/kernel.h>
  2290. +#include <linux/module.h>
  2291. +#include <linux/types.h>
  2292. +#include <linux/errno.h>
  2293. +#include <linux/cdev.h>
  2294. +#include <linux/fs.h>
  2295. +#include <linux/device.h>
  2296. +#include <linux/mm.h>
  2297. +#include <linux/highmem.h>
  2298. +#include <linux/pagemap.h>
  2299. +#include <linux/bug.h>
  2300. +#include <linux/semaphore.h>
  2301. +#include <linux/list.h>
  2302. +
  2303. +#include "vchiq_core.h"
  2304. +#include "vchiq_ioctl.h"
  2305. +#include "vchiq_arm.h"
  2306. +#include "vchiq_debugfs.h"
  2307. +
  2308. +#define DEVICE_NAME "vchiq"
  2309. +
  2310. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  2311. +#undef MODULE_PARAM_PREFIX
  2312. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  2313. +
  2314. +#define VCHIQ_MINOR 0
  2315. +
  2316. +/* Some per-instance constants */
  2317. +#define MAX_COMPLETIONS 16
  2318. +#define MAX_SERVICES 64
  2319. +#define MAX_ELEMENTS 8
  2320. +#define MSG_QUEUE_SIZE 64
  2321. +
  2322. +#define KEEPALIVE_VER 1
  2323. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  2324. +
  2325. +/* Run time control of log level, based on KERN_XXX level. */
  2326. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  2327. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  2328. +
  2329. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  2330. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  2331. +
  2332. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  2333. +static const char *const suspend_state_names[] = {
  2334. + "VC_SUSPEND_FORCE_CANCELED",
  2335. + "VC_SUSPEND_REJECTED",
  2336. + "VC_SUSPEND_FAILED",
  2337. + "VC_SUSPEND_IDLE",
  2338. + "VC_SUSPEND_REQUESTED",
  2339. + "VC_SUSPEND_IN_PROGRESS",
  2340. + "VC_SUSPEND_SUSPENDED"
  2341. +};
  2342. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  2343. +static const char *const resume_state_names[] = {
  2344. + "VC_RESUME_FAILED",
  2345. + "VC_RESUME_IDLE",
  2346. + "VC_RESUME_REQUESTED",
  2347. + "VC_RESUME_IN_PROGRESS",
  2348. + "VC_RESUME_RESUMED"
  2349. +};
  2350. +/* The number of times we allow force suspend to timeout before actually
  2351. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  2352. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  2353. +*/
  2354. +#define FORCE_SUSPEND_FAIL_MAX 8
  2355. +
  2356. +/* The time in ms allowed for videocore to go idle when force suspend has been
  2357. + * requested */
  2358. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  2359. +
  2360. +
  2361. +static void suspend_timer_callback(unsigned long context);
  2362. +
  2363. +
  2364. +typedef struct user_service_struct {
  2365. + VCHIQ_SERVICE_T *service;
  2366. + void *userdata;
  2367. + VCHIQ_INSTANCE_T instance;
  2368. + char is_vchi;
  2369. + char dequeue_pending;
  2370. + char close_pending;
  2371. + int message_available_pos;
  2372. + int msg_insert;
  2373. + int msg_remove;
  2374. + struct semaphore insert_event;
  2375. + struct semaphore remove_event;
  2376. + struct semaphore close_event;
  2377. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  2378. +} USER_SERVICE_T;
  2379. +
  2380. +struct bulk_waiter_node {
  2381. + struct bulk_waiter bulk_waiter;
  2382. + int pid;
  2383. + struct list_head list;
  2384. +};
  2385. +
  2386. +struct vchiq_instance_struct {
  2387. + VCHIQ_STATE_T *state;
  2388. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  2389. + int completion_insert;
  2390. + int completion_remove;
  2391. + struct semaphore insert_event;
  2392. + struct semaphore remove_event;
  2393. + struct mutex completion_mutex;
  2394. +
  2395. + int connected;
  2396. + int closing;
  2397. + int pid;
  2398. + int mark;
  2399. + int use_close_delivered;
  2400. + int trace;
  2401. +
  2402. + struct list_head bulk_waiter_list;
  2403. + struct mutex bulk_waiter_list_mutex;
  2404. +
  2405. + VCHIQ_DEBUGFS_NODE_T debugfs_node;
  2406. +};
  2407. +
  2408. +typedef struct dump_context_struct {
  2409. + char __user *buf;
  2410. + size_t actual;
  2411. + size_t space;
  2412. + loff_t offset;
  2413. +} DUMP_CONTEXT_T;
  2414. +
  2415. +static struct cdev vchiq_cdev;
  2416. +static dev_t vchiq_devid;
  2417. +static VCHIQ_STATE_T g_state;
  2418. +static struct class *vchiq_class;
  2419. +static struct device *vchiq_dev;
  2420. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  2421. +
  2422. +static const char *const ioctl_names[] = {
  2423. + "CONNECT",
  2424. + "SHUTDOWN",
  2425. + "CREATE_SERVICE",
  2426. + "REMOVE_SERVICE",
  2427. + "QUEUE_MESSAGE",
  2428. + "QUEUE_BULK_TRANSMIT",
  2429. + "QUEUE_BULK_RECEIVE",
  2430. + "AWAIT_COMPLETION",
  2431. + "DEQUEUE_MESSAGE",
  2432. + "GET_CLIENT_ID",
  2433. + "GET_CONFIG",
  2434. + "CLOSE_SERVICE",
  2435. + "USE_SERVICE",
  2436. + "RELEASE_SERVICE",
  2437. + "SET_SERVICE_OPTION",
  2438. + "DUMP_PHYS_MEM",
  2439. + "LIB_VERSION",
  2440. + "CLOSE_DELIVERED"
  2441. +};
  2442. +
  2443. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  2444. + (VCHIQ_IOC_MAX + 1));
  2445. +
  2446. +static void
  2447. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  2448. +
  2449. +/****************************************************************************
  2450. +*
  2451. +* add_completion
  2452. +*
  2453. +***************************************************************************/
  2454. +
  2455. +static VCHIQ_STATUS_T
  2456. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  2457. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  2458. + void *bulk_userdata)
  2459. +{
  2460. + VCHIQ_COMPLETION_DATA_T *completion;
  2461. + DEBUG_INITIALISE(g_state.local)
  2462. +
  2463. + while (instance->completion_insert ==
  2464. + (instance->completion_remove + MAX_COMPLETIONS)) {
  2465. + /* Out of space - wait for the client */
  2466. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2467. + vchiq_log_trace(vchiq_arm_log_level,
  2468. + "add_completion - completion queue full");
  2469. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  2470. + if (down_interruptible(&instance->remove_event) != 0) {
  2471. + vchiq_log_info(vchiq_arm_log_level,
  2472. + "service_callback interrupted");
  2473. + return VCHIQ_RETRY;
  2474. + } else if (instance->closing) {
  2475. + vchiq_log_info(vchiq_arm_log_level,
  2476. + "service_callback closing");
  2477. + return VCHIQ_ERROR;
  2478. + }
  2479. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2480. + }
  2481. +
  2482. + completion =
  2483. + &instance->completions[instance->completion_insert &
  2484. + (MAX_COMPLETIONS - 1)];
  2485. +
  2486. + completion->header = header;
  2487. + completion->reason = reason;
  2488. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  2489. + completion->service_userdata = user_service->service;
  2490. + completion->bulk_userdata = bulk_userdata;
  2491. +
  2492. + if (reason == VCHIQ_SERVICE_CLOSED) {
  2493. + /* Take an extra reference, to be held until
  2494. + this CLOSED notification is delivered. */
  2495. + lock_service(user_service->service);
  2496. + if (instance->use_close_delivered)
  2497. + user_service->close_pending = 1;
  2498. + }
  2499. +
  2500. + /* A write barrier is needed here to ensure that the entire completion
  2501. + record is written out before the insert point. */
  2502. + wmb();
  2503. +
  2504. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  2505. + user_service->message_available_pos =
  2506. + instance->completion_insert;
  2507. + instance->completion_insert++;
  2508. +
  2509. + up(&instance->insert_event);
  2510. +
  2511. + return VCHIQ_SUCCESS;
  2512. +}
  2513. +
  2514. +/****************************************************************************
  2515. +*
  2516. +* service_callback
  2517. +*
  2518. +***************************************************************************/
  2519. +
  2520. +static VCHIQ_STATUS_T
  2521. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  2522. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  2523. +{
  2524. + /* How do we ensure the callback goes to the right client?
  2525. + ** The service_user data points to a USER_SERVICE_T record containing
  2526. + ** the original callback and the user state structure, which contains a
  2527. + ** circular buffer for completion records.
  2528. + */
  2529. + USER_SERVICE_T *user_service;
  2530. + VCHIQ_SERVICE_T *service;
  2531. + VCHIQ_INSTANCE_T instance;
  2532. + DEBUG_INITIALISE(g_state.local)
  2533. +
  2534. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2535. +
  2536. + service = handle_to_service(handle);
  2537. + BUG_ON(!service);
  2538. + user_service = (USER_SERVICE_T *)service->base.userdata;
  2539. + instance = user_service->instance;
  2540. +
  2541. + if (!instance || instance->closing)
  2542. + return VCHIQ_SUCCESS;
  2543. +
  2544. + vchiq_log_trace(vchiq_arm_log_level,
  2545. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  2546. + "instance %lx, bulk_userdata %lx",
  2547. + (unsigned long)user_service,
  2548. + service->localport, user_service->userdata,
  2549. + reason, (unsigned long)header,
  2550. + (unsigned long)instance, (unsigned long)bulk_userdata);
  2551. +
  2552. + if (header && user_service->is_vchi) {
  2553. + spin_lock(&msg_queue_spinlock);
  2554. + while (user_service->msg_insert ==
  2555. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  2556. + spin_unlock(&msg_queue_spinlock);
  2557. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2558. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  2559. + vchiq_log_trace(vchiq_arm_log_level,
  2560. + "service_callback - msg queue full");
  2561. + /* If there is no MESSAGE_AVAILABLE in the completion
  2562. + ** queue, add one
  2563. + */
  2564. + if ((user_service->message_available_pos -
  2565. + instance->completion_remove) < 0) {
  2566. + VCHIQ_STATUS_T status;
  2567. + vchiq_log_info(vchiq_arm_log_level,
  2568. + "Inserting extra MESSAGE_AVAILABLE");
  2569. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2570. + status = add_completion(instance, reason,
  2571. + NULL, user_service, bulk_userdata);
  2572. + if (status != VCHIQ_SUCCESS) {
  2573. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2574. + return status;
  2575. + }
  2576. + }
  2577. +
  2578. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2579. + if (down_interruptible(&user_service->remove_event)
  2580. + != 0) {
  2581. + vchiq_log_info(vchiq_arm_log_level,
  2582. + "service_callback interrupted");
  2583. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2584. + return VCHIQ_RETRY;
  2585. + } else if (instance->closing) {
  2586. + vchiq_log_info(vchiq_arm_log_level,
  2587. + "service_callback closing");
  2588. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2589. + return VCHIQ_ERROR;
  2590. + }
  2591. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2592. + spin_lock(&msg_queue_spinlock);
  2593. + }
  2594. +
  2595. + user_service->msg_queue[user_service->msg_insert &
  2596. + (MSG_QUEUE_SIZE - 1)] = header;
  2597. + user_service->msg_insert++;
  2598. + spin_unlock(&msg_queue_spinlock);
  2599. +
  2600. + up(&user_service->insert_event);
  2601. +
  2602. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  2603. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  2604. + ** bypass the completion queue.
  2605. + */
  2606. + if (((user_service->message_available_pos -
  2607. + instance->completion_remove) >= 0) ||
  2608. + user_service->dequeue_pending) {
  2609. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2610. + user_service->dequeue_pending = 0;
  2611. + return VCHIQ_SUCCESS;
  2612. + }
  2613. +
  2614. + header = NULL;
  2615. + }
  2616. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  2617. +
  2618. + return add_completion(instance, reason, header, user_service,
  2619. + bulk_userdata);
  2620. +}
  2621. +
  2622. +/****************************************************************************
  2623. +*
  2624. +* user_service_free
  2625. +*
  2626. +***************************************************************************/
  2627. +static void
  2628. +user_service_free(void *userdata)
  2629. +{
  2630. + kfree(userdata);
  2631. +}
  2632. +
  2633. +/****************************************************************************
  2634. +*
  2635. +* close_delivered
  2636. +*
  2637. +***************************************************************************/
  2638. +static void close_delivered(USER_SERVICE_T *user_service)
  2639. +{
  2640. + vchiq_log_info(vchiq_arm_log_level,
  2641. + "close_delivered(handle=%x)",
  2642. + user_service->service->handle);
  2643. +
  2644. + if (user_service->close_pending) {
  2645. + /* Allow the underlying service to be culled */
  2646. + unlock_service(user_service->service);
  2647. +
  2648. + /* Wake the user-thread blocked in close_ or remove_service */
  2649. + up(&user_service->close_event);
  2650. +
  2651. + user_service->close_pending = 0;
  2652. + }
  2653. +}
  2654. +
  2655. +/****************************************************************************
  2656. +*
  2657. +* vchiq_ioctl
  2658. +*
  2659. +***************************************************************************/
  2660. +static long
  2661. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2662. +{
  2663. + VCHIQ_INSTANCE_T instance = file->private_data;
  2664. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  2665. + VCHIQ_SERVICE_T *service = NULL;
  2666. + long ret = 0;
  2667. + int i, rc;
  2668. + DEBUG_INITIALISE(g_state.local)
  2669. +
  2670. + vchiq_log_trace(vchiq_arm_log_level,
  2671. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  2672. + (unsigned int)instance,
  2673. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  2674. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  2675. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  2676. +
  2677. + switch (cmd) {
  2678. + case VCHIQ_IOC_SHUTDOWN:
  2679. + if (!instance->connected)
  2680. + break;
  2681. +
  2682. + /* Remove all services */
  2683. + i = 0;
  2684. + while ((service = next_service_by_instance(instance->state,
  2685. + instance, &i)) != NULL) {
  2686. + status = vchiq_remove_service(service->handle);
  2687. + unlock_service(service);
  2688. + if (status != VCHIQ_SUCCESS)
  2689. + break;
  2690. + }
  2691. + service = NULL;
  2692. +
  2693. + if (status == VCHIQ_SUCCESS) {
  2694. + /* Wake the completion thread and ask it to exit */
  2695. + instance->closing = 1;
  2696. + up(&instance->insert_event);
  2697. + }
  2698. +
  2699. + break;
  2700. +
  2701. + case VCHIQ_IOC_CONNECT:
  2702. + if (instance->connected) {
  2703. + ret = -EINVAL;
  2704. + break;
  2705. + }
  2706. + rc = mutex_lock_interruptible(&instance->state->mutex);
  2707. + if (rc != 0) {
  2708. + vchiq_log_error(vchiq_arm_log_level,
  2709. + "vchiq: connect: could not lock mutex for "
  2710. + "state %d: %d",
  2711. + instance->state->id, rc);
  2712. + ret = -EINTR;
  2713. + break;
  2714. + }
  2715. + status = vchiq_connect_internal(instance->state, instance);
  2716. + mutex_unlock(&instance->state->mutex);
  2717. +
  2718. + if (status == VCHIQ_SUCCESS)
  2719. + instance->connected = 1;
  2720. + else
  2721. + vchiq_log_error(vchiq_arm_log_level,
  2722. + "vchiq: could not connect: %d", status);
  2723. + break;
  2724. +
  2725. + case VCHIQ_IOC_CREATE_SERVICE: {
  2726. + VCHIQ_CREATE_SERVICE_T args;
  2727. + USER_SERVICE_T *user_service = NULL;
  2728. + void *userdata;
  2729. + int srvstate;
  2730. +
  2731. + if (copy_from_user
  2732. + (&args, (const void __user *)arg,
  2733. + sizeof(args)) != 0) {
  2734. + ret = -EFAULT;
  2735. + break;
  2736. + }
  2737. +
  2738. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  2739. + if (!user_service) {
  2740. + ret = -ENOMEM;
  2741. + break;
  2742. + }
  2743. +
  2744. + if (args.is_open) {
  2745. + if (!instance->connected) {
  2746. + ret = -ENOTCONN;
  2747. + kfree(user_service);
  2748. + break;
  2749. + }
  2750. + srvstate = VCHIQ_SRVSTATE_OPENING;
  2751. + } else {
  2752. + srvstate =
  2753. + instance->connected ?
  2754. + VCHIQ_SRVSTATE_LISTENING :
  2755. + VCHIQ_SRVSTATE_HIDDEN;
  2756. + }
  2757. +
  2758. + userdata = args.params.userdata;
  2759. + args.params.callback = service_callback;
  2760. + args.params.userdata = user_service;
  2761. + service = vchiq_add_service_internal(
  2762. + instance->state,
  2763. + &args.params, srvstate,
  2764. + instance, user_service_free);
  2765. +
  2766. + if (service != NULL) {
  2767. + user_service->service = service;
  2768. + user_service->userdata = userdata;
  2769. + user_service->instance = instance;
  2770. + user_service->is_vchi = (args.is_vchi != 0);
  2771. + user_service->dequeue_pending = 0;
  2772. + user_service->close_pending = 0;
  2773. + user_service->message_available_pos =
  2774. + instance->completion_remove - 1;
  2775. + user_service->msg_insert = 0;
  2776. + user_service->msg_remove = 0;
  2777. + sema_init(&user_service->insert_event, 0);
  2778. + sema_init(&user_service->remove_event, 0);
  2779. + sema_init(&user_service->close_event, 0);
  2780. +
  2781. + if (args.is_open) {
  2782. + status = vchiq_open_service_internal
  2783. + (service, instance->pid);
  2784. + if (status != VCHIQ_SUCCESS) {
  2785. + vchiq_remove_service(service->handle);
  2786. + service = NULL;
  2787. + ret = (status == VCHIQ_RETRY) ?
  2788. + -EINTR : -EIO;
  2789. + break;
  2790. + }
  2791. + }
  2792. +
  2793. + if (copy_to_user((void __user *)
  2794. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  2795. + arg)->handle),
  2796. + (const void *)&service->handle,
  2797. + sizeof(service->handle)) != 0) {
  2798. + ret = -EFAULT;
  2799. + vchiq_remove_service(service->handle);
  2800. + }
  2801. +
  2802. + service = NULL;
  2803. + } else {
  2804. + ret = -EEXIST;
  2805. + kfree(user_service);
  2806. + }
  2807. + } break;
  2808. +
  2809. + case VCHIQ_IOC_CLOSE_SERVICE: {
  2810. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2811. +
  2812. + service = find_service_for_instance(instance, handle);
  2813. + if (service != NULL) {
  2814. + USER_SERVICE_T *user_service =
  2815. + (USER_SERVICE_T *)service->base.userdata;
  2816. + /* close_pending is false on first entry, and when the
  2817. + wait in vchiq_close_service has been interrupted. */
  2818. + if (!user_service->close_pending) {
  2819. + status = vchiq_close_service(service->handle);
  2820. + if (status != VCHIQ_SUCCESS)
  2821. + break;
  2822. + }
  2823. +
  2824. + /* close_pending is true once the underlying service
  2825. + has been closed until the client library calls the
  2826. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2827. + if (user_service->close_pending &&
  2828. + down_interruptible(&user_service->close_event))
  2829. + status = VCHIQ_RETRY;
  2830. + }
  2831. + else
  2832. + ret = -EINVAL;
  2833. + } break;
  2834. +
  2835. + case VCHIQ_IOC_REMOVE_SERVICE: {
  2836. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2837. +
  2838. + service = find_service_for_instance(instance, handle);
  2839. + if (service != NULL) {
  2840. + USER_SERVICE_T *user_service =
  2841. + (USER_SERVICE_T *)service->base.userdata;
  2842. + /* close_pending is false on first entry, and when the
  2843. + wait in vchiq_close_service has been interrupted. */
  2844. + if (!user_service->close_pending) {
  2845. + status = vchiq_remove_service(service->handle);
  2846. + if (status != VCHIQ_SUCCESS)
  2847. + break;
  2848. + }
  2849. +
  2850. + /* close_pending is true once the underlying service
  2851. + has been closed until the client library calls the
  2852. + CLOSE_DELIVERED ioctl, signalling close_event. */
  2853. + if (user_service->close_pending &&
  2854. + down_interruptible(&user_service->close_event))
  2855. + status = VCHIQ_RETRY;
  2856. + }
  2857. + else
  2858. + ret = -EINVAL;
  2859. + } break;
  2860. +
  2861. + case VCHIQ_IOC_USE_SERVICE:
  2862. + case VCHIQ_IOC_RELEASE_SERVICE: {
  2863. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  2864. +
  2865. + service = find_service_for_instance(instance, handle);
  2866. + if (service != NULL) {
  2867. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  2868. + vchiq_use_service_internal(service) :
  2869. + vchiq_release_service_internal(service);
  2870. + if (status != VCHIQ_SUCCESS) {
  2871. + vchiq_log_error(vchiq_susp_log_level,
  2872. + "%s: cmd %s returned error %d for "
  2873. + "service %c%c%c%c:%03d",
  2874. + __func__,
  2875. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  2876. + "VCHIQ_IOC_USE_SERVICE" :
  2877. + "VCHIQ_IOC_RELEASE_SERVICE",
  2878. + status,
  2879. + VCHIQ_FOURCC_AS_4CHARS(
  2880. + service->base.fourcc),
  2881. + service->client_id);
  2882. + ret = -EINVAL;
  2883. + }
  2884. + } else
  2885. + ret = -EINVAL;
  2886. + } break;
  2887. +
  2888. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  2889. + VCHIQ_QUEUE_MESSAGE_T args;
  2890. + if (copy_from_user
  2891. + (&args, (const void __user *)arg,
  2892. + sizeof(args)) != 0) {
  2893. + ret = -EFAULT;
  2894. + break;
  2895. + }
  2896. +
  2897. + service = find_service_for_instance(instance, args.handle);
  2898. +
  2899. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  2900. + /* Copy elements into kernel space */
  2901. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  2902. + if (copy_from_user(elements, args.elements,
  2903. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  2904. + status = vchiq_queue_message
  2905. + (args.handle,
  2906. + elements, args.count);
  2907. + else
  2908. + ret = -EFAULT;
  2909. + } else {
  2910. + ret = -EINVAL;
  2911. + }
  2912. + } break;
  2913. +
  2914. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  2915. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  2916. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  2917. + struct bulk_waiter_node *waiter = NULL;
  2918. + VCHIQ_BULK_DIR_T dir =
  2919. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  2920. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  2921. +
  2922. + if (copy_from_user
  2923. + (&args, (const void __user *)arg,
  2924. + sizeof(args)) != 0) {
  2925. + ret = -EFAULT;
  2926. + break;
  2927. + }
  2928. +
  2929. + service = find_service_for_instance(instance, args.handle);
  2930. + if (!service) {
  2931. + ret = -EINVAL;
  2932. + break;
  2933. + }
  2934. +
  2935. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  2936. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  2937. + GFP_KERNEL);
  2938. + if (!waiter) {
  2939. + ret = -ENOMEM;
  2940. + break;
  2941. + }
  2942. + args.userdata = &waiter->bulk_waiter;
  2943. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  2944. + struct list_head *pos;
  2945. + mutex_lock(&instance->bulk_waiter_list_mutex);
  2946. + list_for_each(pos, &instance->bulk_waiter_list) {
  2947. + if (list_entry(pos, struct bulk_waiter_node,
  2948. + list)->pid == current->pid) {
  2949. + waiter = list_entry(pos,
  2950. + struct bulk_waiter_node,
  2951. + list);
  2952. + list_del(pos);
  2953. + break;
  2954. + }
  2955. +
  2956. + }
  2957. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  2958. + if (!waiter) {
  2959. + vchiq_log_error(vchiq_arm_log_level,
  2960. + "no bulk_waiter found for pid %d",
  2961. + current->pid);
  2962. + ret = -ESRCH;
  2963. + break;
  2964. + }
  2965. + vchiq_log_info(vchiq_arm_log_level,
  2966. + "found bulk_waiter %x for pid %d",
  2967. + (unsigned int)waiter, current->pid);
  2968. + args.userdata = &waiter->bulk_waiter;
  2969. + }
  2970. + status = vchiq_bulk_transfer
  2971. + (args.handle,
  2972. + VCHI_MEM_HANDLE_INVALID,
  2973. + args.data, args.size,
  2974. + args.userdata, args.mode,
  2975. + dir);
  2976. + if (!waiter)
  2977. + break;
  2978. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  2979. + !waiter->bulk_waiter.bulk) {
  2980. + if (waiter->bulk_waiter.bulk) {
  2981. + /* Cancel the signal when the transfer
  2982. + ** completes. */
  2983. + spin_lock(&bulk_waiter_spinlock);
  2984. + waiter->bulk_waiter.bulk->userdata = NULL;
  2985. + spin_unlock(&bulk_waiter_spinlock);
  2986. + }
  2987. + kfree(waiter);
  2988. + } else {
  2989. + const VCHIQ_BULK_MODE_T mode_waiting =
  2990. + VCHIQ_BULK_MODE_WAITING;
  2991. + waiter->pid = current->pid;
  2992. + mutex_lock(&instance->bulk_waiter_list_mutex);
  2993. + list_add(&waiter->list, &instance->bulk_waiter_list);
  2994. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  2995. + vchiq_log_info(vchiq_arm_log_level,
  2996. + "saved bulk_waiter %x for pid %d",
  2997. + (unsigned int)waiter, current->pid);
  2998. +
  2999. + if (copy_to_user((void __user *)
  3000. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  3001. + arg)->mode),
  3002. + (const void *)&mode_waiting,
  3003. + sizeof(mode_waiting)) != 0)
  3004. + ret = -EFAULT;
  3005. + }
  3006. + } break;
  3007. +
  3008. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  3009. + VCHIQ_AWAIT_COMPLETION_T args;
  3010. +
  3011. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3012. + if (!instance->connected) {
  3013. + ret = -ENOTCONN;
  3014. + break;
  3015. + }
  3016. +
  3017. + if (copy_from_user(&args, (const void __user *)arg,
  3018. + sizeof(args)) != 0) {
  3019. + ret = -EFAULT;
  3020. + break;
  3021. + }
  3022. +
  3023. + mutex_lock(&instance->completion_mutex);
  3024. +
  3025. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3026. + while ((instance->completion_remove ==
  3027. + instance->completion_insert)
  3028. + && !instance->closing) {
  3029. + int rc;
  3030. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3031. + mutex_unlock(&instance->completion_mutex);
  3032. + rc = down_interruptible(&instance->insert_event);
  3033. + mutex_lock(&instance->completion_mutex);
  3034. + if (rc != 0) {
  3035. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3036. + vchiq_log_info(vchiq_arm_log_level,
  3037. + "AWAIT_COMPLETION interrupted");
  3038. + ret = -EINTR;
  3039. + break;
  3040. + }
  3041. + }
  3042. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3043. +
  3044. + /* A read memory barrier is needed to stop prefetch of a stale
  3045. + ** completion record
  3046. + */
  3047. + rmb();
  3048. +
  3049. + if (ret == 0) {
  3050. + int msgbufcount = args.msgbufcount;
  3051. + for (ret = 0; ret < args.count; ret++) {
  3052. + VCHIQ_COMPLETION_DATA_T *completion;
  3053. + VCHIQ_SERVICE_T *service;
  3054. + USER_SERVICE_T *user_service;
  3055. + VCHIQ_HEADER_T *header;
  3056. + if (instance->completion_remove ==
  3057. + instance->completion_insert)
  3058. + break;
  3059. + completion = &instance->completions[
  3060. + instance->completion_remove &
  3061. + (MAX_COMPLETIONS - 1)];
  3062. +
  3063. + service = completion->service_userdata;
  3064. + user_service = service->base.userdata;
  3065. + completion->service_userdata =
  3066. + user_service->userdata;
  3067. +
  3068. + header = completion->header;
  3069. + if (header) {
  3070. + void __user *msgbuf;
  3071. + int msglen;
  3072. +
  3073. + msglen = header->size +
  3074. + sizeof(VCHIQ_HEADER_T);
  3075. + /* This must be a VCHIQ-style service */
  3076. + if (args.msgbufsize < msglen) {
  3077. + vchiq_log_error(
  3078. + vchiq_arm_log_level,
  3079. + "header %x: msgbufsize"
  3080. + " %x < msglen %x",
  3081. + (unsigned int)header,
  3082. + args.msgbufsize,
  3083. + msglen);
  3084. + WARN(1, "invalid message "
  3085. + "size\n");
  3086. + if (ret == 0)
  3087. + ret = -EMSGSIZE;
  3088. + break;
  3089. + }
  3090. + if (msgbufcount <= 0)
  3091. + /* Stall here for lack of a
  3092. + ** buffer for the message. */
  3093. + break;
  3094. + /* Get the pointer from user space */
  3095. + msgbufcount--;
  3096. + if (copy_from_user(&msgbuf,
  3097. + (const void __user *)
  3098. + &args.msgbufs[msgbufcount],
  3099. + sizeof(msgbuf)) != 0) {
  3100. + if (ret == 0)
  3101. + ret = -EFAULT;
  3102. + break;
  3103. + }
  3104. +
  3105. + /* Copy the message to user space */
  3106. + if (copy_to_user(msgbuf, header,
  3107. + msglen) != 0) {
  3108. + if (ret == 0)
  3109. + ret = -EFAULT;
  3110. + break;
  3111. + }
  3112. +
  3113. + /* Now it has been copied, the message
  3114. + ** can be released. */
  3115. + vchiq_release_message(service->handle,
  3116. + header);
  3117. +
  3118. + /* The completion must point to the
  3119. + ** msgbuf. */
  3120. + completion->header = msgbuf;
  3121. + }
  3122. +
  3123. + if ((completion->reason ==
  3124. + VCHIQ_SERVICE_CLOSED) &&
  3125. + !instance->use_close_delivered)
  3126. + unlock_service(service);
  3127. +
  3128. + if (copy_to_user((void __user *)(
  3129. + (size_t)args.buf +
  3130. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  3131. + completion,
  3132. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  3133. + if (ret == 0)
  3134. + ret = -EFAULT;
  3135. + break;
  3136. + }
  3137. +
  3138. + instance->completion_remove++;
  3139. + }
  3140. +
  3141. + if (msgbufcount != args.msgbufcount) {
  3142. + if (copy_to_user((void __user *)
  3143. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  3144. + msgbufcount,
  3145. + &msgbufcount,
  3146. + sizeof(msgbufcount)) != 0) {
  3147. + ret = -EFAULT;
  3148. + }
  3149. + }
  3150. + }
  3151. +
  3152. + if (ret != 0)
  3153. + up(&instance->remove_event);
  3154. + mutex_unlock(&instance->completion_mutex);
  3155. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  3156. + } break;
  3157. +
  3158. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  3159. + VCHIQ_DEQUEUE_MESSAGE_T args;
  3160. + USER_SERVICE_T *user_service;
  3161. + VCHIQ_HEADER_T *header;
  3162. +
  3163. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3164. + if (copy_from_user
  3165. + (&args, (const void __user *)arg,
  3166. + sizeof(args)) != 0) {
  3167. + ret = -EFAULT;
  3168. + break;
  3169. + }
  3170. + service = find_service_for_instance(instance, args.handle);
  3171. + if (!service) {
  3172. + ret = -EINVAL;
  3173. + break;
  3174. + }
  3175. + user_service = (USER_SERVICE_T *)service->base.userdata;
  3176. + if (user_service->is_vchi == 0) {
  3177. + ret = -EINVAL;
  3178. + break;
  3179. + }
  3180. +
  3181. + spin_lock(&msg_queue_spinlock);
  3182. + if (user_service->msg_remove == user_service->msg_insert) {
  3183. + if (!args.blocking) {
  3184. + spin_unlock(&msg_queue_spinlock);
  3185. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3186. + ret = -EWOULDBLOCK;
  3187. + break;
  3188. + }
  3189. + user_service->dequeue_pending = 1;
  3190. + do {
  3191. + spin_unlock(&msg_queue_spinlock);
  3192. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3193. + if (down_interruptible(
  3194. + &user_service->insert_event) != 0) {
  3195. + vchiq_log_info(vchiq_arm_log_level,
  3196. + "DEQUEUE_MESSAGE interrupted");
  3197. + ret = -EINTR;
  3198. + break;
  3199. + }
  3200. + spin_lock(&msg_queue_spinlock);
  3201. + } while (user_service->msg_remove ==
  3202. + user_service->msg_insert);
  3203. +
  3204. + if (ret)
  3205. + break;
  3206. + }
  3207. +
  3208. + BUG_ON((int)(user_service->msg_insert -
  3209. + user_service->msg_remove) < 0);
  3210. +
  3211. + header = user_service->msg_queue[user_service->msg_remove &
  3212. + (MSG_QUEUE_SIZE - 1)];
  3213. + user_service->msg_remove++;
  3214. + spin_unlock(&msg_queue_spinlock);
  3215. +
  3216. + up(&user_service->remove_event);
  3217. + if (header == NULL)
  3218. + ret = -ENOTCONN;
  3219. + else if (header->size <= args.bufsize) {
  3220. + /* Copy to user space if msgbuf is not NULL */
  3221. + if ((args.buf == NULL) ||
  3222. + (copy_to_user((void __user *)args.buf,
  3223. + header->data,
  3224. + header->size) == 0)) {
  3225. + ret = header->size;
  3226. + vchiq_release_message(
  3227. + service->handle,
  3228. + header);
  3229. + } else
  3230. + ret = -EFAULT;
  3231. + } else {
  3232. + vchiq_log_error(vchiq_arm_log_level,
  3233. + "header %x: bufsize %x < size %x",
  3234. + (unsigned int)header, args.bufsize,
  3235. + header->size);
  3236. + WARN(1, "invalid size\n");
  3237. + ret = -EMSGSIZE;
  3238. + }
  3239. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  3240. + } break;
  3241. +
  3242. + case VCHIQ_IOC_GET_CLIENT_ID: {
  3243. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3244. +
  3245. + ret = vchiq_get_client_id(handle);
  3246. + } break;
  3247. +
  3248. + case VCHIQ_IOC_GET_CONFIG: {
  3249. + VCHIQ_GET_CONFIG_T args;
  3250. + VCHIQ_CONFIG_T config;
  3251. +
  3252. + if (copy_from_user(&args, (const void __user *)arg,
  3253. + sizeof(args)) != 0) {
  3254. + ret = -EFAULT;
  3255. + break;
  3256. + }
  3257. + if (args.config_size > sizeof(config)) {
  3258. + ret = -EINVAL;
  3259. + break;
  3260. + }
  3261. + status = vchiq_get_config(instance, args.config_size, &config);
  3262. + if (status == VCHIQ_SUCCESS) {
  3263. + if (copy_to_user((void __user *)args.pconfig,
  3264. + &config, args.config_size) != 0) {
  3265. + ret = -EFAULT;
  3266. + break;
  3267. + }
  3268. + }
  3269. + } break;
  3270. +
  3271. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  3272. + VCHIQ_SET_SERVICE_OPTION_T args;
  3273. +
  3274. + if (copy_from_user(
  3275. + &args, (const void __user *)arg,
  3276. + sizeof(args)) != 0) {
  3277. + ret = -EFAULT;
  3278. + break;
  3279. + }
  3280. +
  3281. + service = find_service_for_instance(instance, args.handle);
  3282. + if (!service) {
  3283. + ret = -EINVAL;
  3284. + break;
  3285. + }
  3286. +
  3287. + status = vchiq_set_service_option(
  3288. + args.handle, args.option, args.value);
  3289. + } break;
  3290. +
  3291. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  3292. + VCHIQ_DUMP_MEM_T args;
  3293. +
  3294. + if (copy_from_user
  3295. + (&args, (const void __user *)arg,
  3296. + sizeof(args)) != 0) {
  3297. + ret = -EFAULT;
  3298. + break;
  3299. + }
  3300. + dump_phys_mem(args.virt_addr, args.num_bytes);
  3301. + } break;
  3302. +
  3303. + case VCHIQ_IOC_LIB_VERSION: {
  3304. + unsigned int lib_version = (unsigned int)arg;
  3305. +
  3306. + if (lib_version < VCHIQ_VERSION_MIN)
  3307. + ret = -EINVAL;
  3308. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  3309. + instance->use_close_delivered = 1;
  3310. + } break;
  3311. +
  3312. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  3313. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  3314. +
  3315. + service = find_closed_service_for_instance(instance, handle);
  3316. + if (service != NULL) {
  3317. + USER_SERVICE_T *user_service =
  3318. + (USER_SERVICE_T *)service->base.userdata;
  3319. + close_delivered(user_service);
  3320. + }
  3321. + else
  3322. + ret = -EINVAL;
  3323. + } break;
  3324. +
  3325. + default:
  3326. + ret = -ENOTTY;
  3327. + break;
  3328. + }
  3329. +
  3330. + if (service)
  3331. + unlock_service(service);
  3332. +
  3333. + if (ret == 0) {
  3334. + if (status == VCHIQ_ERROR)
  3335. + ret = -EIO;
  3336. + else if (status == VCHIQ_RETRY)
  3337. + ret = -EINTR;
  3338. + }
  3339. +
  3340. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  3341. + (ret != -EWOULDBLOCK))
  3342. + vchiq_log_info(vchiq_arm_log_level,
  3343. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3344. + (unsigned long)instance,
  3345. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3346. + ioctl_names[_IOC_NR(cmd)] :
  3347. + "<invalid>",
  3348. + status, ret);
  3349. + else
  3350. + vchiq_log_trace(vchiq_arm_log_level,
  3351. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  3352. + (unsigned long)instance,
  3353. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  3354. + ioctl_names[_IOC_NR(cmd)] :
  3355. + "<invalid>",
  3356. + status, ret);
  3357. +
  3358. + return ret;
  3359. +}
  3360. +
  3361. +/****************************************************************************
  3362. +*
  3363. +* vchiq_open
  3364. +*
  3365. +***************************************************************************/
  3366. +
  3367. +static int
  3368. +vchiq_open(struct inode *inode, struct file *file)
  3369. +{
  3370. + int dev = iminor(inode) & 0x0f;
  3371. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  3372. + switch (dev) {
  3373. + case VCHIQ_MINOR: {
  3374. + int ret;
  3375. + VCHIQ_STATE_T *state = vchiq_get_state();
  3376. + VCHIQ_INSTANCE_T instance;
  3377. +
  3378. + if (!state) {
  3379. + vchiq_log_error(vchiq_arm_log_level,
  3380. + "vchiq has no connection to VideoCore");
  3381. + return -ENOTCONN;
  3382. + }
  3383. +
  3384. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  3385. + if (!instance)
  3386. + return -ENOMEM;
  3387. +
  3388. + instance->state = state;
  3389. + instance->pid = current->tgid;
  3390. +
  3391. + ret = vchiq_debugfs_add_instance(instance);
  3392. + if (ret != 0) {
  3393. + kfree(instance);
  3394. + return ret;
  3395. + }
  3396. +
  3397. + sema_init(&instance->insert_event, 0);
  3398. + sema_init(&instance->remove_event, 0);
  3399. + mutex_init(&instance->completion_mutex);
  3400. + mutex_init(&instance->bulk_waiter_list_mutex);
  3401. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  3402. +
  3403. + file->private_data = instance;
  3404. + } break;
  3405. +
  3406. + default:
  3407. + vchiq_log_error(vchiq_arm_log_level,
  3408. + "Unknown minor device: %d", dev);
  3409. + return -ENXIO;
  3410. + }
  3411. +
  3412. + return 0;
  3413. +}
  3414. +
  3415. +/****************************************************************************
  3416. +*
  3417. +* vchiq_release
  3418. +*
  3419. +***************************************************************************/
  3420. +
  3421. +static int
  3422. +vchiq_release(struct inode *inode, struct file *file)
  3423. +{
  3424. + int dev = iminor(inode) & 0x0f;
  3425. + int ret = 0;
  3426. + switch (dev) {
  3427. + case VCHIQ_MINOR: {
  3428. + VCHIQ_INSTANCE_T instance = file->private_data;
  3429. + VCHIQ_STATE_T *state = vchiq_get_state();
  3430. + VCHIQ_SERVICE_T *service;
  3431. + int i;
  3432. +
  3433. + vchiq_log_info(vchiq_arm_log_level,
  3434. + "vchiq_release: instance=%lx",
  3435. + (unsigned long)instance);
  3436. +
  3437. + if (!state) {
  3438. + ret = -EPERM;
  3439. + goto out;
  3440. + }
  3441. +
  3442. + /* Ensure videocore is awake to allow termination. */
  3443. + vchiq_use_internal(instance->state, NULL,
  3444. + USE_TYPE_VCHIQ);
  3445. +
  3446. + mutex_lock(&instance->completion_mutex);
  3447. +
  3448. + /* Wake the completion thread and ask it to exit */
  3449. + instance->closing = 1;
  3450. + up(&instance->insert_event);
  3451. +
  3452. + mutex_unlock(&instance->completion_mutex);
  3453. +
  3454. + /* Wake the slot handler if the completion queue is full. */
  3455. + up(&instance->remove_event);
  3456. +
  3457. + /* Mark all services for termination... */
  3458. + i = 0;
  3459. + while ((service = next_service_by_instance(state, instance,
  3460. + &i)) != NULL) {
  3461. + USER_SERVICE_T *user_service = service->base.userdata;
  3462. +
  3463. + /* Wake the slot handler if the msg queue is full. */
  3464. + up(&user_service->remove_event);
  3465. +
  3466. + vchiq_terminate_service_internal(service);
  3467. + unlock_service(service);
  3468. + }
  3469. +
  3470. + /* ...and wait for them to die */
  3471. + i = 0;
  3472. + while ((service = next_service_by_instance(state, instance, &i))
  3473. + != NULL) {
  3474. + USER_SERVICE_T *user_service = service->base.userdata;
  3475. +
  3476. + down(&service->remove_event);
  3477. +
  3478. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  3479. +
  3480. + spin_lock(&msg_queue_spinlock);
  3481. +
  3482. + while (user_service->msg_remove !=
  3483. + user_service->msg_insert) {
  3484. + VCHIQ_HEADER_T *header = user_service->
  3485. + msg_queue[user_service->msg_remove &
  3486. + (MSG_QUEUE_SIZE - 1)];
  3487. + user_service->msg_remove++;
  3488. + spin_unlock(&msg_queue_spinlock);
  3489. +
  3490. + if (header)
  3491. + vchiq_release_message(
  3492. + service->handle,
  3493. + header);
  3494. + spin_lock(&msg_queue_spinlock);
  3495. + }
  3496. +
  3497. + spin_unlock(&msg_queue_spinlock);
  3498. +
  3499. + unlock_service(service);
  3500. + }
  3501. +
  3502. + /* Release any closed services */
  3503. + while (instance->completion_remove !=
  3504. + instance->completion_insert) {
  3505. + VCHIQ_COMPLETION_DATA_T *completion;
  3506. + VCHIQ_SERVICE_T *service;
  3507. + completion = &instance->completions[
  3508. + instance->completion_remove &
  3509. + (MAX_COMPLETIONS - 1)];
  3510. + service = completion->service_userdata;
  3511. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  3512. + {
  3513. + USER_SERVICE_T *user_service =
  3514. + service->base.userdata;
  3515. +
  3516. + /* Wake any blocked user-thread */
  3517. + if (instance->use_close_delivered)
  3518. + up(&user_service->close_event);
  3519. + unlock_service(service);
  3520. + }
  3521. + instance->completion_remove++;
  3522. + }
  3523. +
  3524. + /* Release the PEER service count. */
  3525. + vchiq_release_internal(instance->state, NULL);
  3526. +
  3527. + {
  3528. + struct list_head *pos, *next;
  3529. + list_for_each_safe(pos, next,
  3530. + &instance->bulk_waiter_list) {
  3531. + struct bulk_waiter_node *waiter;
  3532. + waiter = list_entry(pos,
  3533. + struct bulk_waiter_node,
  3534. + list);
  3535. + list_del(pos);
  3536. + vchiq_log_info(vchiq_arm_log_level,
  3537. + "bulk_waiter - cleaned up %x "
  3538. + "for pid %d",
  3539. + (unsigned int)waiter, waiter->pid);
  3540. + kfree(waiter);
  3541. + }
  3542. + }
  3543. +
  3544. + vchiq_debugfs_remove_instance(instance);
  3545. +
  3546. + kfree(instance);
  3547. + file->private_data = NULL;
  3548. + } break;
  3549. +
  3550. + default:
  3551. + vchiq_log_error(vchiq_arm_log_level,
  3552. + "Unknown minor device: %d", dev);
  3553. + ret = -ENXIO;
  3554. + }
  3555. +
  3556. +out:
  3557. + return ret;
  3558. +}
  3559. +
  3560. +/****************************************************************************
  3561. +*
  3562. +* vchiq_dump
  3563. +*
  3564. +***************************************************************************/
  3565. +
  3566. +void
  3567. +vchiq_dump(void *dump_context, const char *str, int len)
  3568. +{
  3569. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  3570. +
  3571. + if (context->actual < context->space) {
  3572. + int copy_bytes;
  3573. + if (context->offset > 0) {
  3574. + int skip_bytes = min(len, (int)context->offset);
  3575. + str += skip_bytes;
  3576. + len -= skip_bytes;
  3577. + context->offset -= skip_bytes;
  3578. + if (context->offset > 0)
  3579. + return;
  3580. + }
  3581. + copy_bytes = min(len, (int)(context->space - context->actual));
  3582. + if (copy_bytes == 0)
  3583. + return;
  3584. + if (copy_to_user(context->buf + context->actual, str,
  3585. + copy_bytes))
  3586. + context->actual = -EFAULT;
  3587. + context->actual += copy_bytes;
  3588. + len -= copy_bytes;
  3589. +
  3590. + /* If tne terminating NUL is included in the length, then it
  3591. + ** marks the end of a line and should be replaced with a
  3592. + ** carriage return. */
  3593. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  3594. + char cr = '\n';
  3595. + if (copy_to_user(context->buf + context->actual - 1,
  3596. + &cr, 1))
  3597. + context->actual = -EFAULT;
  3598. + }
  3599. + }
  3600. +}
  3601. +
  3602. +/****************************************************************************
  3603. +*
  3604. +* vchiq_dump_platform_instance_state
  3605. +*
  3606. +***************************************************************************/
  3607. +
  3608. +void
  3609. +vchiq_dump_platform_instances(void *dump_context)
  3610. +{
  3611. + VCHIQ_STATE_T *state = vchiq_get_state();
  3612. + char buf[80];
  3613. + int len;
  3614. + int i;
  3615. +
  3616. + /* There is no list of instances, so instead scan all services,
  3617. + marking those that have been dumped. */
  3618. +
  3619. + for (i = 0; i < state->unused_service; i++) {
  3620. + VCHIQ_SERVICE_T *service = state->services[i];
  3621. + VCHIQ_INSTANCE_T instance;
  3622. +
  3623. + if (service && (service->base.callback == service_callback)) {
  3624. + instance = service->instance;
  3625. + if (instance)
  3626. + instance->mark = 0;
  3627. + }
  3628. + }
  3629. +
  3630. + for (i = 0; i < state->unused_service; i++) {
  3631. + VCHIQ_SERVICE_T *service = state->services[i];
  3632. + VCHIQ_INSTANCE_T instance;
  3633. +
  3634. + if (service && (service->base.callback == service_callback)) {
  3635. + instance = service->instance;
  3636. + if (instance && !instance->mark) {
  3637. + len = snprintf(buf, sizeof(buf),
  3638. + "Instance %x: pid %d,%s completions "
  3639. + "%d/%d",
  3640. + (unsigned int)instance, instance->pid,
  3641. + instance->connected ? " connected, " :
  3642. + "",
  3643. + instance->completion_insert -
  3644. + instance->completion_remove,
  3645. + MAX_COMPLETIONS);
  3646. +
  3647. + vchiq_dump(dump_context, buf, len + 1);
  3648. +
  3649. + instance->mark = 1;
  3650. + }
  3651. + }
  3652. + }
  3653. +}
  3654. +
  3655. +/****************************************************************************
  3656. +*
  3657. +* vchiq_dump_platform_service_state
  3658. +*
  3659. +***************************************************************************/
  3660. +
  3661. +void
  3662. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  3663. +{
  3664. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  3665. + char buf[80];
  3666. + int len;
  3667. +
  3668. + len = snprintf(buf, sizeof(buf), " instance %x",
  3669. + (unsigned int)service->instance);
  3670. +
  3671. + if ((service->base.callback == service_callback) &&
  3672. + user_service->is_vchi) {
  3673. + len += snprintf(buf + len, sizeof(buf) - len,
  3674. + ", %d/%d messages",
  3675. + user_service->msg_insert - user_service->msg_remove,
  3676. + MSG_QUEUE_SIZE);
  3677. +
  3678. + if (user_service->dequeue_pending)
  3679. + len += snprintf(buf + len, sizeof(buf) - len,
  3680. + " (dequeue pending)");
  3681. + }
  3682. +
  3683. + vchiq_dump(dump_context, buf, len + 1);
  3684. +}
  3685. +
  3686. +/****************************************************************************
  3687. +*
  3688. +* dump_user_mem
  3689. +*
  3690. +***************************************************************************/
  3691. +
  3692. +static void
  3693. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  3694. +{
  3695. + int rc;
  3696. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  3697. + int num_pages;
  3698. + int offset;
  3699. + int end_offset;
  3700. + int page_idx;
  3701. + int prev_idx;
  3702. + struct page *page;
  3703. + struct page **pages;
  3704. + uint8_t *kmapped_virt_ptr;
  3705. +
  3706. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  3707. +
  3708. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  3709. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  3710. + ~0x0fuL);
  3711. +
  3712. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  3713. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  3714. +
  3715. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  3716. +
  3717. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  3718. + if (pages == NULL) {
  3719. + vchiq_log_error(vchiq_arm_log_level,
  3720. + "Unable to allocation memory for %d pages\n",
  3721. + num_pages);
  3722. + return;
  3723. + }
  3724. +
  3725. + down_read(&current->mm->mmap_sem);
  3726. + rc = get_user_pages(current, /* task */
  3727. + current->mm, /* mm */
  3728. + (unsigned long)virt_addr, /* start */
  3729. + num_pages, /* len */
  3730. + 0, /* write */
  3731. + 0, /* force */
  3732. + pages, /* pages (array of page pointers) */
  3733. + NULL); /* vmas */
  3734. + up_read(&current->mm->mmap_sem);
  3735. +
  3736. + prev_idx = -1;
  3737. + page = NULL;
  3738. +
  3739. + while (offset < end_offset) {
  3740. +
  3741. + int page_offset = offset % PAGE_SIZE;
  3742. + page_idx = offset / PAGE_SIZE;
  3743. +
  3744. + if (page_idx != prev_idx) {
  3745. +
  3746. + if (page != NULL)
  3747. + kunmap(page);
  3748. + page = pages[page_idx];
  3749. + kmapped_virt_ptr = kmap(page);
  3750. +
  3751. + prev_idx = page_idx;
  3752. + }
  3753. +
  3754. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  3755. + vchiq_log_dump_mem("ph",
  3756. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  3757. + page_offset],
  3758. + &kmapped_virt_ptr[page_offset], 16);
  3759. +
  3760. + offset += 16;
  3761. + }
  3762. + if (page != NULL)
  3763. + kunmap(page);
  3764. +
  3765. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  3766. + page_cache_release(pages[page_idx]);
  3767. +
  3768. + kfree(pages);
  3769. +}
  3770. +
  3771. +/****************************************************************************
  3772. +*
  3773. +* vchiq_read
  3774. +*
  3775. +***************************************************************************/
  3776. +
  3777. +static ssize_t
  3778. +vchiq_read(struct file *file, char __user *buf,
  3779. + size_t count, loff_t *ppos)
  3780. +{
  3781. + DUMP_CONTEXT_T context;
  3782. + context.buf = buf;
  3783. + context.actual = 0;
  3784. + context.space = count;
  3785. + context.offset = *ppos;
  3786. +
  3787. + vchiq_dump_state(&context, &g_state);
  3788. +
  3789. + *ppos += context.actual;
  3790. +
  3791. + return context.actual;
  3792. +}
  3793. +
  3794. +VCHIQ_STATE_T *
  3795. +vchiq_get_state(void)
  3796. +{
  3797. +
  3798. + if (g_state.remote == NULL)
  3799. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  3800. + else if (g_state.remote->initialised != 1)
  3801. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  3802. + __func__, g_state.remote->initialised);
  3803. +
  3804. + return ((g_state.remote != NULL) &&
  3805. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  3806. +}
  3807. +
  3808. +static const struct file_operations
  3809. +vchiq_fops = {
  3810. + .owner = THIS_MODULE,
  3811. + .unlocked_ioctl = vchiq_ioctl,
  3812. + .open = vchiq_open,
  3813. + .release = vchiq_release,
  3814. + .read = vchiq_read
  3815. +};
  3816. +
  3817. +/*
  3818. + * Autosuspend related functionality
  3819. + */
  3820. +
  3821. +int
  3822. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  3823. +{
  3824. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3825. + if (!arm_state)
  3826. + /* autosuspend not supported - always return wanted */
  3827. + return 1;
  3828. + else if (arm_state->blocked_count)
  3829. + return 1;
  3830. + else if (!arm_state->videocore_use_count)
  3831. + /* usage count zero - check for override unless we're forcing */
  3832. + if (arm_state->resume_blocked)
  3833. + return 0;
  3834. + else
  3835. + return vchiq_platform_videocore_wanted(state);
  3836. + else
  3837. + /* non-zero usage count - videocore still required */
  3838. + return 1;
  3839. +}
  3840. +
  3841. +static VCHIQ_STATUS_T
  3842. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  3843. + VCHIQ_HEADER_T *header,
  3844. + VCHIQ_SERVICE_HANDLE_T service_user,
  3845. + void *bulk_user)
  3846. +{
  3847. + vchiq_log_error(vchiq_susp_log_level,
  3848. + "%s callback reason %d", __func__, reason);
  3849. + return 0;
  3850. +}
  3851. +
  3852. +static int
  3853. +vchiq_keepalive_thread_func(void *v)
  3854. +{
  3855. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  3856. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  3857. +
  3858. + VCHIQ_STATUS_T status;
  3859. + VCHIQ_INSTANCE_T instance;
  3860. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  3861. +
  3862. + VCHIQ_SERVICE_PARAMS_T params = {
  3863. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  3864. + .callback = vchiq_keepalive_vchiq_callback,
  3865. + .version = KEEPALIVE_VER,
  3866. + .version_min = KEEPALIVE_VER_MIN
  3867. + };
  3868. +
  3869. + status = vchiq_initialise(&instance);
  3870. + if (status != VCHIQ_SUCCESS) {
  3871. + vchiq_log_error(vchiq_susp_log_level,
  3872. + "%s vchiq_initialise failed %d", __func__, status);
  3873. + goto exit;
  3874. + }
  3875. +
  3876. + status = vchiq_connect(instance);
  3877. + if (status != VCHIQ_SUCCESS) {
  3878. + vchiq_log_error(vchiq_susp_log_level,
  3879. + "%s vchiq_connect failed %d", __func__, status);
  3880. + goto shutdown;
  3881. + }
  3882. +
  3883. + status = vchiq_add_service(instance, &params, &ka_handle);
  3884. + if (status != VCHIQ_SUCCESS) {
  3885. + vchiq_log_error(vchiq_susp_log_level,
  3886. + "%s vchiq_open_service failed %d", __func__, status);
  3887. + goto shutdown;
  3888. + }
  3889. +
  3890. + while (1) {
  3891. + long rc = 0, uc = 0;
  3892. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  3893. + != 0) {
  3894. + vchiq_log_error(vchiq_susp_log_level,
  3895. + "%s interrupted", __func__);
  3896. + flush_signals(current);
  3897. + continue;
  3898. + }
  3899. +
  3900. + /* read and clear counters. Do release_count then use_count to
  3901. + * prevent getting more releases than uses */
  3902. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  3903. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  3904. +
  3905. + /* Call use/release service the requisite number of times.
  3906. + * Process use before release so use counts don't go negative */
  3907. + while (uc--) {
  3908. + atomic_inc(&arm_state->ka_use_ack_count);
  3909. + status = vchiq_use_service(ka_handle);
  3910. + if (status != VCHIQ_SUCCESS) {
  3911. + vchiq_log_error(vchiq_susp_log_level,
  3912. + "%s vchiq_use_service error %d",
  3913. + __func__, status);
  3914. + }
  3915. + }
  3916. + while (rc--) {
  3917. + status = vchiq_release_service(ka_handle);
  3918. + if (status != VCHIQ_SUCCESS) {
  3919. + vchiq_log_error(vchiq_susp_log_level,
  3920. + "%s vchiq_release_service error %d",
  3921. + __func__, status);
  3922. + }
  3923. + }
  3924. + }
  3925. +
  3926. +shutdown:
  3927. + vchiq_shutdown(instance);
  3928. +exit:
  3929. + return 0;
  3930. +}
  3931. +
  3932. +
  3933. +
  3934. +VCHIQ_STATUS_T
  3935. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  3936. +{
  3937. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  3938. +
  3939. + if (arm_state) {
  3940. + rwlock_init(&arm_state->susp_res_lock);
  3941. +
  3942. + init_completion(&arm_state->ka_evt);
  3943. + atomic_set(&arm_state->ka_use_count, 0);
  3944. + atomic_set(&arm_state->ka_use_ack_count, 0);
  3945. + atomic_set(&arm_state->ka_release_count, 0);
  3946. +
  3947. + init_completion(&arm_state->vc_suspend_complete);
  3948. +
  3949. + init_completion(&arm_state->vc_resume_complete);
  3950. + /* Initialise to 'done' state. We only want to block on resume
  3951. + * completion while videocore is suspended. */
  3952. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  3953. +
  3954. + init_completion(&arm_state->resume_blocker);
  3955. + /* Initialise to 'done' state. We only want to block on this
  3956. + * completion while resume is blocked */
  3957. + complete_all(&arm_state->resume_blocker);
  3958. +
  3959. + init_completion(&arm_state->blocked_blocker);
  3960. + /* Initialise to 'done' state. We only want to block on this
  3961. + * completion while things are waiting on the resume blocker */
  3962. + complete_all(&arm_state->blocked_blocker);
  3963. +
  3964. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  3965. + arm_state->suspend_timer_running = 0;
  3966. + init_timer(&arm_state->suspend_timer);
  3967. + arm_state->suspend_timer.data = (unsigned long)(state);
  3968. + arm_state->suspend_timer.function = suspend_timer_callback;
  3969. +
  3970. + arm_state->first_connect = 0;
  3971. +
  3972. + }
  3973. + return status;
  3974. +}
  3975. +
  3976. +/*
  3977. +** Functions to modify the state variables;
  3978. +** set_suspend_state
  3979. +** set_resume_state
  3980. +**
  3981. +** There are more state variables than we might like, so ensure they remain in
  3982. +** step. Suspend and resume state are maintained separately, since most of
  3983. +** these state machines can operate independently. However, there are a few
  3984. +** states where state transitions in one state machine cause a reset to the
  3985. +** other state machine. In addition, there are some completion events which
  3986. +** need to occur on state machine reset and end-state(s), so these are also
  3987. +** dealt with in these functions.
  3988. +**
  3989. +** In all states we set the state variable according to the input, but in some
  3990. +** cases we perform additional steps outlined below;
  3991. +**
  3992. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  3993. +** The suspend completion is completed after any suspend
  3994. +** attempt. When we reset the state machine we also reset
  3995. +** the completion. This reset occurs when videocore is
  3996. +** resumed, and also if we initiate suspend after a suspend
  3997. +** failure.
  3998. +**
  3999. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  4000. +** suspend - ie from this point on we must try to suspend
  4001. +** before resuming can occur. We therefore also reset the
  4002. +** resume state machine to VC_RESUME_IDLE in this state.
  4003. +**
  4004. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  4005. +** complete_all on the suspend completion to notify
  4006. +** anything waiting for suspend to happen.
  4007. +**
  4008. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  4009. +** initiate resume, so no need to alter resume state.
  4010. +** We call complete_all on the suspend completion to notify
  4011. +** of suspend rejection.
  4012. +**
  4013. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  4014. +** suspend completion and reset the resume state machine.
  4015. +**
  4016. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  4017. +** resume completion is in it's 'done' state whenever
  4018. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  4019. +** implies that videocore is suspended.
  4020. +** Hence, any thread which needs to wait until videocore is
  4021. +** running can wait on this completion - it will only block
  4022. +** if videocore is suspended.
  4023. +**
  4024. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  4025. +** Call complete_all on the resume completion to unblock
  4026. +** any threads waiting for resume. Also reset the suspend
  4027. +** state machine to it's idle state.
  4028. +**
  4029. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  4030. +*/
  4031. +
  4032. +inline void
  4033. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  4034. + enum vc_suspend_status new_state)
  4035. +{
  4036. + /* set the state in all cases */
  4037. + arm_state->vc_suspend_state = new_state;
  4038. +
  4039. + /* state specific additional actions */
  4040. + switch (new_state) {
  4041. + case VC_SUSPEND_FORCE_CANCELED:
  4042. + complete_all(&arm_state->vc_suspend_complete);
  4043. + break;
  4044. + case VC_SUSPEND_REJECTED:
  4045. + complete_all(&arm_state->vc_suspend_complete);
  4046. + break;
  4047. + case VC_SUSPEND_FAILED:
  4048. + complete_all(&arm_state->vc_suspend_complete);
  4049. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  4050. + complete_all(&arm_state->vc_resume_complete);
  4051. + break;
  4052. + case VC_SUSPEND_IDLE:
  4053. + reinit_completion(&arm_state->vc_suspend_complete);
  4054. + break;
  4055. + case VC_SUSPEND_REQUESTED:
  4056. + break;
  4057. + case VC_SUSPEND_IN_PROGRESS:
  4058. + set_resume_state(arm_state, VC_RESUME_IDLE);
  4059. + break;
  4060. + case VC_SUSPEND_SUSPENDED:
  4061. + complete_all(&arm_state->vc_suspend_complete);
  4062. + break;
  4063. + default:
  4064. + BUG();
  4065. + break;
  4066. + }
  4067. +}
  4068. +
  4069. +inline void
  4070. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  4071. + enum vc_resume_status new_state)
  4072. +{
  4073. + /* set the state in all cases */
  4074. + arm_state->vc_resume_state = new_state;
  4075. +
  4076. + /* state specific additional actions */
  4077. + switch (new_state) {
  4078. + case VC_RESUME_FAILED:
  4079. + break;
  4080. + case VC_RESUME_IDLE:
  4081. + reinit_completion(&arm_state->vc_resume_complete);
  4082. + break;
  4083. + case VC_RESUME_REQUESTED:
  4084. + break;
  4085. + case VC_RESUME_IN_PROGRESS:
  4086. + break;
  4087. + case VC_RESUME_RESUMED:
  4088. + complete_all(&arm_state->vc_resume_complete);
  4089. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4090. + break;
  4091. + default:
  4092. + BUG();
  4093. + break;
  4094. + }
  4095. +}
  4096. +
  4097. +
  4098. +/* should be called with the write lock held */
  4099. +inline void
  4100. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4101. +{
  4102. + del_timer(&arm_state->suspend_timer);
  4103. + arm_state->suspend_timer.expires = jiffies +
  4104. + msecs_to_jiffies(arm_state->
  4105. + suspend_timer_timeout);
  4106. + add_timer(&arm_state->suspend_timer);
  4107. + arm_state->suspend_timer_running = 1;
  4108. +}
  4109. +
  4110. +/* should be called with the write lock held */
  4111. +static inline void
  4112. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  4113. +{
  4114. + if (arm_state->suspend_timer_running) {
  4115. + del_timer(&arm_state->suspend_timer);
  4116. + arm_state->suspend_timer_running = 0;
  4117. + }
  4118. +}
  4119. +
  4120. +static inline int
  4121. +need_resume(VCHIQ_STATE_T *state)
  4122. +{
  4123. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4124. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  4125. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  4126. + vchiq_videocore_wanted(state);
  4127. +}
  4128. +
  4129. +static int
  4130. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  4131. +{
  4132. + int status = VCHIQ_SUCCESS;
  4133. + const unsigned long timeout_val =
  4134. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  4135. + int resume_count = 0;
  4136. +
  4137. + /* Allow any threads which were blocked by the last force suspend to
  4138. + * complete if they haven't already. Only give this one shot; if
  4139. + * blocked_count is incremented after blocked_blocker is completed
  4140. + * (which only happens when blocked_count hits 0) then those threads
  4141. + * will have to wait until next time around */
  4142. + if (arm_state->blocked_count) {
  4143. + reinit_completion(&arm_state->blocked_blocker);
  4144. + write_unlock_bh(&arm_state->susp_res_lock);
  4145. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  4146. + "blocked clients", __func__);
  4147. + if (wait_for_completion_interruptible_timeout(
  4148. + &arm_state->blocked_blocker, timeout_val)
  4149. + <= 0) {
  4150. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4151. + "previously blocked clients failed" , __func__);
  4152. + status = VCHIQ_ERROR;
  4153. + write_lock_bh(&arm_state->susp_res_lock);
  4154. + goto out;
  4155. + }
  4156. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  4157. + "clients resumed", __func__);
  4158. + write_lock_bh(&arm_state->susp_res_lock);
  4159. + }
  4160. +
  4161. + /* We need to wait for resume to complete if it's in process */
  4162. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  4163. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  4164. + if (resume_count > 1) {
  4165. + status = VCHIQ_ERROR;
  4166. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  4167. + "many times for resume" , __func__);
  4168. + goto out;
  4169. + }
  4170. + write_unlock_bh(&arm_state->susp_res_lock);
  4171. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  4172. + __func__);
  4173. + if (wait_for_completion_interruptible_timeout(
  4174. + &arm_state->vc_resume_complete, timeout_val)
  4175. + <= 0) {
  4176. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  4177. + "resume failed (%s)", __func__,
  4178. + resume_state_names[arm_state->vc_resume_state +
  4179. + VC_RESUME_NUM_OFFSET]);
  4180. + status = VCHIQ_ERROR;
  4181. + write_lock_bh(&arm_state->susp_res_lock);
  4182. + goto out;
  4183. + }
  4184. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  4185. + write_lock_bh(&arm_state->susp_res_lock);
  4186. + resume_count++;
  4187. + }
  4188. + reinit_completion(&arm_state->resume_blocker);
  4189. + arm_state->resume_blocked = 1;
  4190. +
  4191. +out:
  4192. + return status;
  4193. +}
  4194. +
  4195. +static inline void
  4196. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  4197. +{
  4198. + complete_all(&arm_state->resume_blocker);
  4199. + arm_state->resume_blocked = 0;
  4200. +}
  4201. +
  4202. +/* Initiate suspend via slot handler. Should be called with the write lock
  4203. + * held */
  4204. +VCHIQ_STATUS_T
  4205. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  4206. +{
  4207. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4208. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4209. +
  4210. + if (!arm_state)
  4211. + goto out;
  4212. +
  4213. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4214. + status = VCHIQ_SUCCESS;
  4215. +
  4216. +
  4217. + switch (arm_state->vc_suspend_state) {
  4218. + case VC_SUSPEND_REQUESTED:
  4219. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  4220. + "requested", __func__);
  4221. + break;
  4222. + case VC_SUSPEND_IN_PROGRESS:
  4223. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  4224. + "progress", __func__);
  4225. + break;
  4226. +
  4227. + default:
  4228. + /* We don't expect to be in other states, so log but continue
  4229. + * anyway */
  4230. + vchiq_log_error(vchiq_susp_log_level,
  4231. + "%s unexpected suspend state %s", __func__,
  4232. + suspend_state_names[arm_state->vc_suspend_state +
  4233. + VC_SUSPEND_NUM_OFFSET]);
  4234. + /* fall through */
  4235. + case VC_SUSPEND_REJECTED:
  4236. + case VC_SUSPEND_FAILED:
  4237. + /* Ensure any idle state actions have been run */
  4238. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4239. + /* fall through */
  4240. + case VC_SUSPEND_IDLE:
  4241. + vchiq_log_info(vchiq_susp_log_level,
  4242. + "%s: suspending", __func__);
  4243. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  4244. + /* kick the slot handler thread to initiate suspend */
  4245. + request_poll(state, NULL, 0);
  4246. + break;
  4247. + }
  4248. +
  4249. +out:
  4250. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4251. + return status;
  4252. +}
  4253. +
  4254. +void
  4255. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  4256. +{
  4257. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4258. + int susp = 0;
  4259. +
  4260. + if (!arm_state)
  4261. + goto out;
  4262. +
  4263. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4264. +
  4265. + write_lock_bh(&arm_state->susp_res_lock);
  4266. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  4267. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  4268. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  4269. + susp = 1;
  4270. + }
  4271. + write_unlock_bh(&arm_state->susp_res_lock);
  4272. +
  4273. + if (susp)
  4274. + vchiq_platform_suspend(state);
  4275. +
  4276. +out:
  4277. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4278. + return;
  4279. +}
  4280. +
  4281. +
  4282. +static void
  4283. +output_timeout_error(VCHIQ_STATE_T *state)
  4284. +{
  4285. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4286. + char service_err[50] = "";
  4287. + int vc_use_count = arm_state->videocore_use_count;
  4288. + int active_services = state->unused_service;
  4289. + int i;
  4290. +
  4291. + if (!arm_state->videocore_use_count) {
  4292. + snprintf(service_err, 50, " Videocore usecount is 0");
  4293. + goto output_msg;
  4294. + }
  4295. + for (i = 0; i < active_services; i++) {
  4296. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  4297. + if (service_ptr && service_ptr->service_use_count &&
  4298. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  4299. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  4300. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  4301. + service_ptr->base.fourcc),
  4302. + service_ptr->client_id,
  4303. + service_ptr->service_use_count,
  4304. + service_ptr->service_use_count ==
  4305. + vc_use_count ? "" : " (+ more)");
  4306. + break;
  4307. + }
  4308. + }
  4309. +
  4310. +output_msg:
  4311. + vchiq_log_error(vchiq_susp_log_level,
  4312. + "timed out waiting for vc suspend (%d).%s",
  4313. + arm_state->autosuspend_override, service_err);
  4314. +
  4315. +}
  4316. +
  4317. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  4318. +** We don't actually force suspend, since videocore may get into a bad state
  4319. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  4320. +** determine a good point to suspend. If this doesn't happen within 100ms we
  4321. +** report failure.
  4322. +**
  4323. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  4324. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  4325. +*/
  4326. +VCHIQ_STATUS_T
  4327. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  4328. +{
  4329. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4330. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  4331. + long rc = 0;
  4332. + int repeat = -1;
  4333. +
  4334. + if (!arm_state)
  4335. + goto out;
  4336. +
  4337. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4338. +
  4339. + write_lock_bh(&arm_state->susp_res_lock);
  4340. +
  4341. + status = block_resume(arm_state);
  4342. + if (status != VCHIQ_SUCCESS)
  4343. + goto unlock;
  4344. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4345. + /* Already suspended - just block resume and exit */
  4346. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  4347. + __func__);
  4348. + status = VCHIQ_SUCCESS;
  4349. + goto unlock;
  4350. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  4351. + /* initiate suspend immediately in the case that we're waiting
  4352. + * for the timeout */
  4353. + stop_suspend_timer(arm_state);
  4354. + if (!vchiq_videocore_wanted(state)) {
  4355. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  4356. + "idle, initiating suspend", __func__);
  4357. + status = vchiq_arm_vcsuspend(state);
  4358. + } else if (arm_state->autosuspend_override <
  4359. + FORCE_SUSPEND_FAIL_MAX) {
  4360. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  4361. + "videocore go idle", __func__);
  4362. + status = VCHIQ_SUCCESS;
  4363. + } else {
  4364. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  4365. + "many times - attempting suspend", __func__);
  4366. + status = vchiq_arm_vcsuspend(state);
  4367. + }
  4368. + } else {
  4369. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  4370. + "in progress - wait for completion", __func__);
  4371. + status = VCHIQ_SUCCESS;
  4372. + }
  4373. +
  4374. + /* Wait for suspend to happen due to system idle (not forced..) */
  4375. + if (status != VCHIQ_SUCCESS)
  4376. + goto unblock_resume;
  4377. +
  4378. + do {
  4379. + write_unlock_bh(&arm_state->susp_res_lock);
  4380. +
  4381. + rc = wait_for_completion_interruptible_timeout(
  4382. + &arm_state->vc_suspend_complete,
  4383. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  4384. +
  4385. + write_lock_bh(&arm_state->susp_res_lock);
  4386. + if (rc < 0) {
  4387. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  4388. + "interrupted waiting for suspend", __func__);
  4389. + status = VCHIQ_ERROR;
  4390. + goto unblock_resume;
  4391. + } else if (rc == 0) {
  4392. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  4393. + /* Repeat timeout once if in progress */
  4394. + if (repeat < 0) {
  4395. + repeat = 1;
  4396. + continue;
  4397. + }
  4398. + }
  4399. + arm_state->autosuspend_override++;
  4400. + output_timeout_error(state);
  4401. +
  4402. + status = VCHIQ_RETRY;
  4403. + goto unblock_resume;
  4404. + }
  4405. + } while (0 < (repeat--));
  4406. +
  4407. + /* Check and report state in case we need to abort ARM suspend */
  4408. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  4409. + status = VCHIQ_RETRY;
  4410. + vchiq_log_error(vchiq_susp_log_level,
  4411. + "%s videocore suspend failed (state %s)", __func__,
  4412. + suspend_state_names[arm_state->vc_suspend_state +
  4413. + VC_SUSPEND_NUM_OFFSET]);
  4414. + /* Reset the state only if it's still in an error state.
  4415. + * Something could have already initiated another suspend. */
  4416. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  4417. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4418. +
  4419. + goto unblock_resume;
  4420. + }
  4421. +
  4422. + /* successfully suspended - unlock and exit */
  4423. + goto unlock;
  4424. +
  4425. +unblock_resume:
  4426. + /* all error states need to unblock resume before exit */
  4427. + unblock_resume(arm_state);
  4428. +
  4429. +unlock:
  4430. + write_unlock_bh(&arm_state->susp_res_lock);
  4431. +
  4432. +out:
  4433. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  4434. + return status;
  4435. +}
  4436. +
  4437. +void
  4438. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  4439. +{
  4440. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4441. +
  4442. + if (!arm_state)
  4443. + goto out;
  4444. +
  4445. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4446. +
  4447. + write_lock_bh(&arm_state->susp_res_lock);
  4448. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  4449. + arm_state->first_connect &&
  4450. + !vchiq_videocore_wanted(state)) {
  4451. + vchiq_arm_vcsuspend(state);
  4452. + }
  4453. + write_unlock_bh(&arm_state->susp_res_lock);
  4454. +
  4455. +out:
  4456. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4457. + return;
  4458. +}
  4459. +
  4460. +
  4461. +int
  4462. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  4463. +{
  4464. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4465. + int resume = 0;
  4466. + int ret = -1;
  4467. +
  4468. + if (!arm_state)
  4469. + goto out;
  4470. +
  4471. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4472. +
  4473. + write_lock_bh(&arm_state->susp_res_lock);
  4474. + unblock_resume(arm_state);
  4475. + resume = vchiq_check_resume(state);
  4476. + write_unlock_bh(&arm_state->susp_res_lock);
  4477. +
  4478. + if (resume) {
  4479. + if (wait_for_completion_interruptible(
  4480. + &arm_state->vc_resume_complete) < 0) {
  4481. + vchiq_log_error(vchiq_susp_log_level,
  4482. + "%s interrupted", __func__);
  4483. + /* failed, cannot accurately derive suspend
  4484. + * state, so exit early. */
  4485. + goto out;
  4486. + }
  4487. + }
  4488. +
  4489. + read_lock_bh(&arm_state->susp_res_lock);
  4490. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  4491. + vchiq_log_info(vchiq_susp_log_level,
  4492. + "%s: Videocore remains suspended", __func__);
  4493. + } else {
  4494. + vchiq_log_info(vchiq_susp_log_level,
  4495. + "%s: Videocore resumed", __func__);
  4496. + ret = 0;
  4497. + }
  4498. + read_unlock_bh(&arm_state->susp_res_lock);
  4499. +out:
  4500. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4501. + return ret;
  4502. +}
  4503. +
  4504. +/* This function should be called with the write lock held */
  4505. +int
  4506. +vchiq_check_resume(VCHIQ_STATE_T *state)
  4507. +{
  4508. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4509. + int resume = 0;
  4510. +
  4511. + if (!arm_state)
  4512. + goto out;
  4513. +
  4514. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4515. +
  4516. + if (need_resume(state)) {
  4517. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4518. + request_poll(state, NULL, 0);
  4519. + resume = 1;
  4520. + }
  4521. +
  4522. +out:
  4523. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4524. + return resume;
  4525. +}
  4526. +
  4527. +void
  4528. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  4529. +{
  4530. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4531. + int res = 0;
  4532. +
  4533. + if (!arm_state)
  4534. + goto out;
  4535. +
  4536. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4537. +
  4538. + write_lock_bh(&arm_state->susp_res_lock);
  4539. + if (arm_state->wake_address == 0) {
  4540. + vchiq_log_info(vchiq_susp_log_level,
  4541. + "%s: already awake", __func__);
  4542. + goto unlock;
  4543. + }
  4544. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  4545. + vchiq_log_info(vchiq_susp_log_level,
  4546. + "%s: already resuming", __func__);
  4547. + goto unlock;
  4548. + }
  4549. +
  4550. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  4551. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  4552. + res = 1;
  4553. + } else
  4554. + vchiq_log_trace(vchiq_susp_log_level,
  4555. + "%s: not resuming (resume state %s)", __func__,
  4556. + resume_state_names[arm_state->vc_resume_state +
  4557. + VC_RESUME_NUM_OFFSET]);
  4558. +
  4559. +unlock:
  4560. + write_unlock_bh(&arm_state->susp_res_lock);
  4561. +
  4562. + if (res)
  4563. + vchiq_platform_resume(state);
  4564. +
  4565. +out:
  4566. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  4567. + return;
  4568. +
  4569. +}
  4570. +
  4571. +
  4572. +
  4573. +VCHIQ_STATUS_T
  4574. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  4575. + enum USE_TYPE_E use_type)
  4576. +{
  4577. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4578. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4579. + char entity[16];
  4580. + int *entity_uc;
  4581. + int local_uc, local_entity_uc;
  4582. +
  4583. + if (!arm_state)
  4584. + goto out;
  4585. +
  4586. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4587. +
  4588. + if (use_type == USE_TYPE_VCHIQ) {
  4589. + sprintf(entity, "VCHIQ: ");
  4590. + entity_uc = &arm_state->peer_use_count;
  4591. + } else if (service) {
  4592. + sprintf(entity, "%c%c%c%c:%03d",
  4593. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4594. + service->client_id);
  4595. + entity_uc = &service->service_use_count;
  4596. + } else {
  4597. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  4598. + "ptr", __func__);
  4599. + ret = VCHIQ_ERROR;
  4600. + goto out;
  4601. + }
  4602. +
  4603. + write_lock_bh(&arm_state->susp_res_lock);
  4604. + while (arm_state->resume_blocked) {
  4605. + /* If we call 'use' while force suspend is waiting for suspend,
  4606. + * then we're about to block the thread which the force is
  4607. + * waiting to complete, so we're bound to just time out. In this
  4608. + * case, set the suspend state such that the wait will be
  4609. + * canceled, so we can complete as quickly as possible. */
  4610. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  4611. + VC_SUSPEND_IDLE) {
  4612. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  4613. + break;
  4614. + }
  4615. + /* If suspend is already in progress then we need to block */
  4616. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  4617. + /* Indicate that there are threads waiting on the resume
  4618. + * blocker. These need to be allowed to complete before
  4619. + * a _second_ call to force suspend can complete,
  4620. + * otherwise low priority threads might never actually
  4621. + * continue */
  4622. + arm_state->blocked_count++;
  4623. + write_unlock_bh(&arm_state->susp_res_lock);
  4624. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4625. + "blocked - waiting...", __func__, entity);
  4626. + if (wait_for_completion_killable(
  4627. + &arm_state->resume_blocker) != 0) {
  4628. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  4629. + "wait for resume blocker interrupted",
  4630. + __func__, entity);
  4631. + ret = VCHIQ_ERROR;
  4632. + write_lock_bh(&arm_state->susp_res_lock);
  4633. + arm_state->blocked_count--;
  4634. + write_unlock_bh(&arm_state->susp_res_lock);
  4635. + goto out;
  4636. + }
  4637. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  4638. + "unblocked", __func__, entity);
  4639. + write_lock_bh(&arm_state->susp_res_lock);
  4640. + if (--arm_state->blocked_count == 0)
  4641. + complete_all(&arm_state->blocked_blocker);
  4642. + }
  4643. + }
  4644. +
  4645. + stop_suspend_timer(arm_state);
  4646. +
  4647. + local_uc = ++arm_state->videocore_use_count;
  4648. + local_entity_uc = ++(*entity_uc);
  4649. +
  4650. + /* If there's a pending request which hasn't yet been serviced then
  4651. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  4652. + * vc_resume_complete will block until we either resume or fail to
  4653. + * suspend */
  4654. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  4655. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  4656. +
  4657. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  4658. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  4659. + vchiq_log_info(vchiq_susp_log_level,
  4660. + "%s %s count %d, state count %d",
  4661. + __func__, entity, local_entity_uc, local_uc);
  4662. + request_poll(state, NULL, 0);
  4663. + } else
  4664. + vchiq_log_trace(vchiq_susp_log_level,
  4665. + "%s %s count %d, state count %d",
  4666. + __func__, entity, *entity_uc, local_uc);
  4667. +
  4668. +
  4669. + write_unlock_bh(&arm_state->susp_res_lock);
  4670. +
  4671. + /* Completion is in a done state when we're not suspended, so this won't
  4672. + * block for the non-suspended case. */
  4673. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  4674. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  4675. + __func__, entity);
  4676. + if (wait_for_completion_killable(
  4677. + &arm_state->vc_resume_complete) != 0) {
  4678. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  4679. + "resume interrupted", __func__, entity);
  4680. + ret = VCHIQ_ERROR;
  4681. + goto out;
  4682. + }
  4683. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  4684. + entity);
  4685. + }
  4686. +
  4687. + if (ret == VCHIQ_SUCCESS) {
  4688. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  4689. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  4690. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  4691. + /* Send the use notify to videocore */
  4692. + status = vchiq_send_remote_use_active(state);
  4693. + if (status == VCHIQ_SUCCESS)
  4694. + ack_cnt--;
  4695. + else
  4696. + atomic_add(ack_cnt,
  4697. + &arm_state->ka_use_ack_count);
  4698. + }
  4699. + }
  4700. +
  4701. +out:
  4702. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4703. + return ret;
  4704. +}
  4705. +
  4706. +VCHIQ_STATUS_T
  4707. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  4708. +{
  4709. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4710. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  4711. + char entity[16];
  4712. + int *entity_uc;
  4713. + int local_uc, local_entity_uc;
  4714. +
  4715. + if (!arm_state)
  4716. + goto out;
  4717. +
  4718. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4719. +
  4720. + if (service) {
  4721. + sprintf(entity, "%c%c%c%c:%03d",
  4722. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  4723. + service->client_id);
  4724. + entity_uc = &service->service_use_count;
  4725. + } else {
  4726. + sprintf(entity, "PEER: ");
  4727. + entity_uc = &arm_state->peer_use_count;
  4728. + }
  4729. +
  4730. + write_lock_bh(&arm_state->susp_res_lock);
  4731. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  4732. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  4733. + WARN_ON(!arm_state->videocore_use_count);
  4734. + WARN_ON(!(*entity_uc));
  4735. + ret = VCHIQ_ERROR;
  4736. + goto unlock;
  4737. + }
  4738. + local_uc = --arm_state->videocore_use_count;
  4739. + local_entity_uc = --(*entity_uc);
  4740. +
  4741. + if (!vchiq_videocore_wanted(state)) {
  4742. + if (vchiq_platform_use_suspend_timer() &&
  4743. + !arm_state->resume_blocked) {
  4744. + /* Only use the timer if we're not trying to force
  4745. + * suspend (=> resume_blocked) */
  4746. + start_suspend_timer(arm_state);
  4747. + } else {
  4748. + vchiq_log_info(vchiq_susp_log_level,
  4749. + "%s %s count %d, state count %d - suspending",
  4750. + __func__, entity, *entity_uc,
  4751. + arm_state->videocore_use_count);
  4752. + vchiq_arm_vcsuspend(state);
  4753. + }
  4754. + } else
  4755. + vchiq_log_trace(vchiq_susp_log_level,
  4756. + "%s %s count %d, state count %d",
  4757. + __func__, entity, *entity_uc,
  4758. + arm_state->videocore_use_count);
  4759. +
  4760. +unlock:
  4761. + write_unlock_bh(&arm_state->susp_res_lock);
  4762. +
  4763. +out:
  4764. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  4765. + return ret;
  4766. +}
  4767. +
  4768. +void
  4769. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  4770. +{
  4771. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4772. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4773. + atomic_inc(&arm_state->ka_use_count);
  4774. + complete(&arm_state->ka_evt);
  4775. +}
  4776. +
  4777. +void
  4778. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  4779. +{
  4780. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4781. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4782. + atomic_inc(&arm_state->ka_release_count);
  4783. + complete(&arm_state->ka_evt);
  4784. +}
  4785. +
  4786. +VCHIQ_STATUS_T
  4787. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  4788. +{
  4789. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  4790. +}
  4791. +
  4792. +VCHIQ_STATUS_T
  4793. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  4794. +{
  4795. + return vchiq_release_internal(service->state, service);
  4796. +}
  4797. +
  4798. +VCHIQ_DEBUGFS_NODE_T *
  4799. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
  4800. +{
  4801. + return &instance->debugfs_node;
  4802. +}
  4803. +
  4804. +int
  4805. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  4806. +{
  4807. + VCHIQ_SERVICE_T *service;
  4808. + int use_count = 0, i;
  4809. + i = 0;
  4810. + while ((service = next_service_by_instance(instance->state,
  4811. + instance, &i)) != NULL) {
  4812. + use_count += service->service_use_count;
  4813. + unlock_service(service);
  4814. + }
  4815. + return use_count;
  4816. +}
  4817. +
  4818. +int
  4819. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
  4820. +{
  4821. + return instance->pid;
  4822. +}
  4823. +
  4824. +int
  4825. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
  4826. +{
  4827. + return instance->trace;
  4828. +}
  4829. +
  4830. +void
  4831. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
  4832. +{
  4833. + VCHIQ_SERVICE_T *service;
  4834. + int i;
  4835. + i = 0;
  4836. + while ((service = next_service_by_instance(instance->state,
  4837. + instance, &i)) != NULL) {
  4838. + service->trace = trace;
  4839. + unlock_service(service);
  4840. + }
  4841. + instance->trace = (trace != 0);
  4842. +}
  4843. +
  4844. +static void suspend_timer_callback(unsigned long context)
  4845. +{
  4846. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  4847. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4848. + if (!arm_state)
  4849. + goto out;
  4850. + vchiq_log_info(vchiq_susp_log_level,
  4851. + "%s - suspend timer expired - check suspend", __func__);
  4852. + vchiq_check_suspend(state);
  4853. +out:
  4854. + return;
  4855. +}
  4856. +
  4857. +VCHIQ_STATUS_T
  4858. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  4859. +{
  4860. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  4861. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  4862. + if (service) {
  4863. + ret = vchiq_use_internal(service->state, service,
  4864. + USE_TYPE_SERVICE_NO_RESUME);
  4865. + unlock_service(service);
  4866. + }
  4867. + return ret;
  4868. +}
  4869. +
  4870. +VCHIQ_STATUS_T
  4871. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  4872. +{
  4873. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  4874. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  4875. + if (service) {
  4876. + ret = vchiq_use_internal(service->state, service,
  4877. + USE_TYPE_SERVICE);
  4878. + unlock_service(service);
  4879. + }
  4880. + return ret;
  4881. +}
  4882. +
  4883. +VCHIQ_STATUS_T
  4884. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  4885. +{
  4886. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  4887. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  4888. + if (service) {
  4889. + ret = vchiq_release_internal(service->state, service);
  4890. + unlock_service(service);
  4891. + }
  4892. + return ret;
  4893. +}
  4894. +
  4895. +void
  4896. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  4897. +{
  4898. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  4899. + int i, j = 0;
  4900. + /* Only dump 64 services */
  4901. + static const int local_max_services = 64;
  4902. + /* If there's more than 64 services, only dump ones with
  4903. + * non-zero counts */
  4904. + int only_nonzero = 0;
  4905. + static const char *nz = "<-- preventing suspend";
  4906. +
  4907. + enum vc_suspend_status vc_suspend_state;
  4908. + enum vc_resume_status vc_resume_state;
  4909. + int peer_count;
  4910. + int vc_use_count;
  4911. + int active_services;
  4912. + struct service_data_struct {
  4913. + int fourcc;
  4914. + int clientid;
  4915. + int use_count;
  4916. + } service_data[local_max_services];
  4917. +
  4918. + if (!arm_state)
  4919. + return;
  4920. +
  4921. + read_lock_bh(&arm_state->susp_res_lock);
  4922. + vc_suspend_state = arm_state->vc_suspend_state;
  4923. + vc_resume_state = arm_state->vc_resume_state;
  4924. + peer_count = arm_state->peer_use_count;
  4925. + vc_use_count = arm_state->videocore_use_count;
  4926. + active_services = state->unused_service;
  4927. + if (active_services > local_max_services)
  4928. + only_nonzero = 1;
  4929. +
  4930. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  4931. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  4932. + if (!service_ptr)
  4933. + continue;
  4934. +
  4935. + if (only_nonzero && !service_ptr->service_use_count)
  4936. + continue;
  4937. +
  4938. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  4939. + service_data[j].fourcc = service_ptr->base.fourcc;
  4940. + service_data[j].clientid = service_ptr->client_id;
  4941. + service_data[j++].use_count = service_ptr->
  4942. + service_use_count;
  4943. + }
  4944. + }
  4945. +
  4946. + read_unlock_bh(&arm_state->susp_res_lock);
  4947. +
  4948. + vchiq_log_warning(vchiq_susp_log_level,
  4949. + "-- Videcore suspend state: %s --",
  4950. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  4951. + vchiq_log_warning(vchiq_susp_log_level,
  4952. + "-- Videcore resume state: %s --",
  4953. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  4954. +
  4955. + if (only_nonzero)
  4956. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  4957. + "services (%d). Only dumping up to first %d services "
  4958. + "with non-zero use-count", active_services,
  4959. + local_max_services);
  4960. +
  4961. + for (i = 0; i < j; i++) {
  4962. + vchiq_log_warning(vchiq_susp_log_level,
  4963. + "----- %c%c%c%c:%d service count %d %s",
  4964. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  4965. + service_data[i].clientid,
  4966. + service_data[i].use_count,
  4967. + service_data[i].use_count ? nz : "");
  4968. + }
  4969. + vchiq_log_warning(vchiq_susp_log_level,
  4970. + "----- VCHIQ use count count %d", peer_count);
  4971. + vchiq_log_warning(vchiq_susp_log_level,
  4972. + "--- Overall vchiq instance use count %d", vc_use_count);
  4973. +
  4974. + vchiq_dump_platform_use_state(state);
  4975. +}
  4976. +
  4977. +VCHIQ_STATUS_T
  4978. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  4979. +{
  4980. + VCHIQ_ARM_STATE_T *arm_state;
  4981. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  4982. +
  4983. + if (!service || !service->state)
  4984. + goto out;
  4985. +
  4986. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  4987. +
  4988. + arm_state = vchiq_platform_get_arm_state(service->state);
  4989. +
  4990. + read_lock_bh(&arm_state->susp_res_lock);
  4991. + if (service->service_use_count)
  4992. + ret = VCHIQ_SUCCESS;
  4993. + read_unlock_bh(&arm_state->susp_res_lock);
  4994. +
  4995. + if (ret == VCHIQ_ERROR) {
  4996. + vchiq_log_error(vchiq_susp_log_level,
  4997. + "%s ERROR - %c%c%c%c:%d service count %d, "
  4998. + "state count %d, videocore suspend state %s", __func__,
  4999. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  5000. + service->client_id, service->service_use_count,
  5001. + arm_state->videocore_use_count,
  5002. + suspend_state_names[arm_state->vc_suspend_state +
  5003. + VC_SUSPEND_NUM_OFFSET]);
  5004. + vchiq_dump_service_use_state(service->state);
  5005. + }
  5006. +out:
  5007. + return ret;
  5008. +}
  5009. +
  5010. +/* stub functions */
  5011. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  5012. +{
  5013. + (void)state;
  5014. +}
  5015. +
  5016. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  5017. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  5018. +{
  5019. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  5020. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  5021. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  5022. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  5023. + write_lock_bh(&arm_state->susp_res_lock);
  5024. + if (!arm_state->first_connect) {
  5025. + char threadname[10];
  5026. + arm_state->first_connect = 1;
  5027. + write_unlock_bh(&arm_state->susp_res_lock);
  5028. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  5029. + state->id);
  5030. + arm_state->ka_thread = kthread_create(
  5031. + &vchiq_keepalive_thread_func,
  5032. + (void *)state,
  5033. + threadname);
  5034. + if (arm_state->ka_thread == NULL) {
  5035. + vchiq_log_error(vchiq_susp_log_level,
  5036. + "vchiq: FATAL: couldn't create thread %s",
  5037. + threadname);
  5038. + } else {
  5039. + wake_up_process(arm_state->ka_thread);
  5040. + }
  5041. + } else
  5042. + write_unlock_bh(&arm_state->susp_res_lock);
  5043. + }
  5044. +}
  5045. +
  5046. +
  5047. +/****************************************************************************
  5048. +*
  5049. +* vchiq_init - called when the module is loaded.
  5050. +*
  5051. +***************************************************************************/
  5052. +
  5053. +static int __init
  5054. +vchiq_init(void)
  5055. +{
  5056. + int err;
  5057. + void *ptr_err;
  5058. +
  5059. + /* create debugfs entries */
  5060. + err = vchiq_debugfs_init();
  5061. + if (err != 0)
  5062. + goto failed_debugfs_init;
  5063. +
  5064. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  5065. + if (err != 0) {
  5066. + vchiq_log_error(vchiq_arm_log_level,
  5067. + "Unable to allocate device number");
  5068. + goto failed_alloc_chrdev;
  5069. + }
  5070. + cdev_init(&vchiq_cdev, &vchiq_fops);
  5071. + vchiq_cdev.owner = THIS_MODULE;
  5072. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  5073. + if (err != 0) {
  5074. + vchiq_log_error(vchiq_arm_log_level,
  5075. + "Unable to register device");
  5076. + goto failed_cdev_add;
  5077. + }
  5078. +
  5079. + /* create sysfs entries */
  5080. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  5081. + ptr_err = vchiq_class;
  5082. + if (IS_ERR(ptr_err))
  5083. + goto failed_class_create;
  5084. +
  5085. + vchiq_dev = device_create(vchiq_class, NULL,
  5086. + vchiq_devid, NULL, "vchiq");
  5087. + ptr_err = vchiq_dev;
  5088. + if (IS_ERR(ptr_err))
  5089. + goto failed_device_create;
  5090. +
  5091. + err = vchiq_platform_init(&g_state);
  5092. + if (err != 0)
  5093. + goto failed_platform_init;
  5094. +
  5095. + vchiq_log_info(vchiq_arm_log_level,
  5096. + "vchiq: initialised - version %d (min %d), device %d.%d",
  5097. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  5098. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  5099. +
  5100. + return 0;
  5101. +
  5102. +failed_platform_init:
  5103. + device_destroy(vchiq_class, vchiq_devid);
  5104. +failed_device_create:
  5105. + class_destroy(vchiq_class);
  5106. +failed_class_create:
  5107. + cdev_del(&vchiq_cdev);
  5108. + err = PTR_ERR(ptr_err);
  5109. +failed_cdev_add:
  5110. + unregister_chrdev_region(vchiq_devid, 1);
  5111. +failed_alloc_chrdev:
  5112. + vchiq_debugfs_deinit();
  5113. +failed_debugfs_init:
  5114. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  5115. + return err;
  5116. +}
  5117. +
  5118. +/****************************************************************************
  5119. +*
  5120. +* vchiq_exit - called when the module is unloaded.
  5121. +*
  5122. +***************************************************************************/
  5123. +
  5124. +static void __exit
  5125. +vchiq_exit(void)
  5126. +{
  5127. + vchiq_platform_exit(&g_state);
  5128. + device_destroy(vchiq_class, vchiq_devid);
  5129. + class_destroy(vchiq_class);
  5130. + cdev_del(&vchiq_cdev);
  5131. + unregister_chrdev_region(vchiq_devid, 1);
  5132. +}
  5133. +
  5134. +module_init(vchiq_init);
  5135. +module_exit(vchiq_exit);
  5136. +MODULE_LICENSE("GPL");
  5137. +MODULE_AUTHOR("Broadcom Corporation");
  5138. --- /dev/null
  5139. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  5140. @@ -0,0 +1,223 @@
  5141. +/**
  5142. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  5143. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5144. + *
  5145. + * Redistribution and use in source and binary forms, with or without
  5146. + * modification, are permitted provided that the following conditions
  5147. + * are met:
  5148. + * 1. Redistributions of source code must retain the above copyright
  5149. + * notice, this list of conditions, and the following disclaimer,
  5150. + * without modification.
  5151. + * 2. Redistributions in binary form must reproduce the above copyright
  5152. + * notice, this list of conditions and the following disclaimer in the
  5153. + * documentation and/or other materials provided with the distribution.
  5154. + * 3. The names of the above-listed copyright holders may not be used
  5155. + * to endorse or promote products derived from this software without
  5156. + * specific prior written permission.
  5157. + *
  5158. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5159. + * GNU General Public License ("GPL") version 2, as published by the Free
  5160. + * Software Foundation.
  5161. + *
  5162. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5163. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5164. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5165. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5166. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5167. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5168. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5169. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5170. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5171. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5172. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5173. + */
  5174. +
  5175. +#ifndef VCHIQ_ARM_H
  5176. +#define VCHIQ_ARM_H
  5177. +
  5178. +#include <linux/mutex.h>
  5179. +#include <linux/semaphore.h>
  5180. +#include <linux/atomic.h>
  5181. +#include "vchiq_core.h"
  5182. +#include "vchiq_debugfs.h"
  5183. +
  5184. +
  5185. +enum vc_suspend_status {
  5186. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  5187. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  5188. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  5189. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  5190. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  5191. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  5192. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  5193. +};
  5194. +
  5195. +enum vc_resume_status {
  5196. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  5197. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  5198. + VC_RESUME_REQUESTED, /* User has requested resume */
  5199. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  5200. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  5201. +};
  5202. +
  5203. +
  5204. +enum USE_TYPE_E {
  5205. + USE_TYPE_SERVICE,
  5206. + USE_TYPE_SERVICE_NO_RESUME,
  5207. + USE_TYPE_VCHIQ
  5208. +};
  5209. +
  5210. +
  5211. +
  5212. +typedef struct vchiq_arm_state_struct {
  5213. + /* Keepalive-related data */
  5214. + struct task_struct *ka_thread;
  5215. + struct completion ka_evt;
  5216. + atomic_t ka_use_count;
  5217. + atomic_t ka_use_ack_count;
  5218. + atomic_t ka_release_count;
  5219. +
  5220. + struct completion vc_suspend_complete;
  5221. + struct completion vc_resume_complete;
  5222. +
  5223. + rwlock_t susp_res_lock;
  5224. + enum vc_suspend_status vc_suspend_state;
  5225. + enum vc_resume_status vc_resume_state;
  5226. +
  5227. + unsigned int wake_address;
  5228. +
  5229. + struct timer_list suspend_timer;
  5230. + int suspend_timer_timeout;
  5231. + int suspend_timer_running;
  5232. +
  5233. + /* Global use count for videocore.
  5234. + ** This is equal to the sum of the use counts for all services. When
  5235. + ** this hits zero the videocore suspend procedure will be initiated.
  5236. + */
  5237. + int videocore_use_count;
  5238. +
  5239. + /* Use count to track requests from videocore peer.
  5240. + ** This use count is not associated with a service, so needs to be
  5241. + ** tracked separately with the state.
  5242. + */
  5243. + int peer_use_count;
  5244. +
  5245. + /* Flag to indicate whether resume is blocked. This happens when the
  5246. + ** ARM is suspending
  5247. + */
  5248. + struct completion resume_blocker;
  5249. + int resume_blocked;
  5250. + struct completion blocked_blocker;
  5251. + int blocked_count;
  5252. +
  5253. + int autosuspend_override;
  5254. +
  5255. + /* Flag to indicate that the first vchiq connect has made it through.
  5256. + ** This means that both sides should be fully ready, and we should
  5257. + ** be able to suspend after this point.
  5258. + */
  5259. + int first_connect;
  5260. +
  5261. + unsigned long long suspend_start_time;
  5262. + unsigned long long sleep_start_time;
  5263. + unsigned long long resume_start_time;
  5264. + unsigned long long last_wake_time;
  5265. +
  5266. +} VCHIQ_ARM_STATE_T;
  5267. +
  5268. +extern int vchiq_arm_log_level;
  5269. +extern int vchiq_susp_log_level;
  5270. +
  5271. +extern int __init
  5272. +vchiq_platform_init(VCHIQ_STATE_T *state);
  5273. +
  5274. +extern void __exit
  5275. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  5276. +
  5277. +extern VCHIQ_STATE_T *
  5278. +vchiq_get_state(void);
  5279. +
  5280. +extern VCHIQ_STATUS_T
  5281. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  5282. +
  5283. +extern VCHIQ_STATUS_T
  5284. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  5285. +
  5286. +extern int
  5287. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  5288. +
  5289. +extern VCHIQ_STATUS_T
  5290. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  5291. +
  5292. +extern VCHIQ_STATUS_T
  5293. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  5294. +
  5295. +extern int
  5296. +vchiq_check_resume(VCHIQ_STATE_T *state);
  5297. +
  5298. +extern void
  5299. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  5300. + VCHIQ_STATUS_T
  5301. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  5302. +
  5303. +extern VCHIQ_STATUS_T
  5304. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  5305. +
  5306. +extern VCHIQ_STATUS_T
  5307. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  5308. +
  5309. +extern VCHIQ_STATUS_T
  5310. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  5311. +
  5312. +extern int
  5313. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  5314. +
  5315. +extern int
  5316. +vchiq_platform_use_suspend_timer(void);
  5317. +
  5318. +extern void
  5319. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  5320. +
  5321. +extern void
  5322. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  5323. +
  5324. +extern VCHIQ_ARM_STATE_T*
  5325. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  5326. +
  5327. +extern int
  5328. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  5329. +
  5330. +extern VCHIQ_STATUS_T
  5331. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  5332. + enum USE_TYPE_E use_type);
  5333. +extern VCHIQ_STATUS_T
  5334. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  5335. +
  5336. +extern VCHIQ_DEBUGFS_NODE_T *
  5337. +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
  5338. +
  5339. +extern int
  5340. +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
  5341. +
  5342. +extern int
  5343. +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
  5344. +
  5345. +extern int
  5346. +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
  5347. +
  5348. +extern void
  5349. +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
  5350. +
  5351. +extern void
  5352. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  5353. + enum vc_suspend_status new_state);
  5354. +
  5355. +extern void
  5356. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  5357. + enum vc_resume_status new_state);
  5358. +
  5359. +extern void
  5360. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  5361. +
  5362. +
  5363. +#endif /* VCHIQ_ARM_H */
  5364. --- /dev/null
  5365. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  5366. @@ -0,0 +1,37 @@
  5367. +/**
  5368. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5369. + *
  5370. + * Redistribution and use in source and binary forms, with or without
  5371. + * modification, are permitted provided that the following conditions
  5372. + * are met:
  5373. + * 1. Redistributions of source code must retain the above copyright
  5374. + * notice, this list of conditions, and the following disclaimer,
  5375. + * without modification.
  5376. + * 2. Redistributions in binary form must reproduce the above copyright
  5377. + * notice, this list of conditions and the following disclaimer in the
  5378. + * documentation and/or other materials provided with the distribution.
  5379. + * 3. The names of the above-listed copyright holders may not be used
  5380. + * to endorse or promote products derived from this software without
  5381. + * specific prior written permission.
  5382. + *
  5383. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5384. + * GNU General Public License ("GPL") version 2, as published by the Free
  5385. + * Software Foundation.
  5386. + *
  5387. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5388. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5389. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5390. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5391. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5392. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5393. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5394. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5395. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5396. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5397. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5398. + */
  5399. +
  5400. +const char *vchiq_get_build_hostname(void);
  5401. +const char *vchiq_get_build_version(void);
  5402. +const char *vchiq_get_build_time(void);
  5403. +const char *vchiq_get_build_date(void);
  5404. --- /dev/null
  5405. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  5406. @@ -0,0 +1,66 @@
  5407. +/**
  5408. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  5409. + *
  5410. + * Redistribution and use in source and binary forms, with or without
  5411. + * modification, are permitted provided that the following conditions
  5412. + * are met:
  5413. + * 1. Redistributions of source code must retain the above copyright
  5414. + * notice, this list of conditions, and the following disclaimer,
  5415. + * without modification.
  5416. + * 2. Redistributions in binary form must reproduce the above copyright
  5417. + * notice, this list of conditions and the following disclaimer in the
  5418. + * documentation and/or other materials provided with the distribution.
  5419. + * 3. The names of the above-listed copyright holders may not be used
  5420. + * to endorse or promote products derived from this software without
  5421. + * specific prior written permission.
  5422. + *
  5423. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5424. + * GNU General Public License ("GPL") version 2, as published by the Free
  5425. + * Software Foundation.
  5426. + *
  5427. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5428. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5429. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5430. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5431. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5432. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5433. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5434. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5435. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5436. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5437. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5438. + */
  5439. +
  5440. +#ifndef VCHIQ_CFG_H
  5441. +#define VCHIQ_CFG_H
  5442. +
  5443. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  5444. +/* The version of VCHIQ - change with any non-trivial change */
  5445. +#define VCHIQ_VERSION 7
  5446. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  5447. +** incompatible change */
  5448. +#define VCHIQ_VERSION_MIN 3
  5449. +
  5450. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  5451. +#define VCHIQ_VERSION_LIB_VERSION 7
  5452. +
  5453. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  5454. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  5455. +
  5456. +#define VCHIQ_MAX_STATES 1
  5457. +#define VCHIQ_MAX_SERVICES 4096
  5458. +#define VCHIQ_MAX_SLOTS 128
  5459. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  5460. +
  5461. +#define VCHIQ_NUM_CURRENT_BULKS 32
  5462. +#define VCHIQ_NUM_SERVICE_BULKS 4
  5463. +
  5464. +#ifndef VCHIQ_ENABLE_DEBUG
  5465. +#define VCHIQ_ENABLE_DEBUG 1
  5466. +#endif
  5467. +
  5468. +#ifndef VCHIQ_ENABLE_STATS
  5469. +#define VCHIQ_ENABLE_STATS 1
  5470. +#endif
  5471. +
  5472. +#endif /* VCHIQ_CFG_H */
  5473. --- /dev/null
  5474. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  5475. @@ -0,0 +1,119 @@
  5476. +/**
  5477. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5478. + *
  5479. + * Redistribution and use in source and binary forms, with or without
  5480. + * modification, are permitted provided that the following conditions
  5481. + * are met:
  5482. + * 1. Redistributions of source code must retain the above copyright
  5483. + * notice, this list of conditions, and the following disclaimer,
  5484. + * without modification.
  5485. + * 2. Redistributions in binary form must reproduce the above copyright
  5486. + * notice, this list of conditions and the following disclaimer in the
  5487. + * documentation and/or other materials provided with the distribution.
  5488. + * 3. The names of the above-listed copyright holders may not be used
  5489. + * to endorse or promote products derived from this software without
  5490. + * specific prior written permission.
  5491. + *
  5492. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5493. + * GNU General Public License ("GPL") version 2, as published by the Free
  5494. + * Software Foundation.
  5495. + *
  5496. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5497. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5498. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5499. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5500. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5501. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5502. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5503. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5504. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5505. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5506. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5507. + */
  5508. +
  5509. +#include "vchiq_connected.h"
  5510. +#include "vchiq_core.h"
  5511. +#include <linux/module.h>
  5512. +#include <linux/mutex.h>
  5513. +
  5514. +#define MAX_CALLBACKS 10
  5515. +
  5516. +static int g_connected;
  5517. +static int g_num_deferred_callbacks;
  5518. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  5519. +static int g_once_init;
  5520. +static struct mutex g_connected_mutex;
  5521. +
  5522. +/****************************************************************************
  5523. +*
  5524. +* Function to initialize our lock.
  5525. +*
  5526. +***************************************************************************/
  5527. +
  5528. +static void connected_init(void)
  5529. +{
  5530. + if (!g_once_init) {
  5531. + mutex_init(&g_connected_mutex);
  5532. + g_once_init = 1;
  5533. + }
  5534. +}
  5535. +
  5536. +/****************************************************************************
  5537. +*
  5538. +* This function is used to defer initialization until the vchiq stack is
  5539. +* initialized. If the stack is already initialized, then the callback will
  5540. +* be made immediately, otherwise it will be deferred until
  5541. +* vchiq_call_connected_callbacks is called.
  5542. +*
  5543. +***************************************************************************/
  5544. +
  5545. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  5546. +{
  5547. + connected_init();
  5548. +
  5549. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5550. + return;
  5551. +
  5552. + if (g_connected)
  5553. + /* We're already connected. Call the callback immediately. */
  5554. +
  5555. + callback();
  5556. + else {
  5557. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  5558. + vchiq_log_error(vchiq_core_log_level,
  5559. + "There already %d callback registered - "
  5560. + "please increase MAX_CALLBACKS",
  5561. + g_num_deferred_callbacks);
  5562. + else {
  5563. + g_deferred_callback[g_num_deferred_callbacks] =
  5564. + callback;
  5565. + g_num_deferred_callbacks++;
  5566. + }
  5567. + }
  5568. + mutex_unlock(&g_connected_mutex);
  5569. +}
  5570. +
  5571. +/****************************************************************************
  5572. +*
  5573. +* This function is called by the vchiq stack once it has been connected to
  5574. +* the videocore and clients can start to use the stack.
  5575. +*
  5576. +***************************************************************************/
  5577. +
  5578. +void vchiq_call_connected_callbacks(void)
  5579. +{
  5580. + int i;
  5581. +
  5582. + connected_init();
  5583. +
  5584. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  5585. + return;
  5586. +
  5587. + for (i = 0; i < g_num_deferred_callbacks; i++)
  5588. + g_deferred_callback[i]();
  5589. +
  5590. + g_num_deferred_callbacks = 0;
  5591. + g_connected = 1;
  5592. + mutex_unlock(&g_connected_mutex);
  5593. +}
  5594. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  5595. --- /dev/null
  5596. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  5597. @@ -0,0 +1,50 @@
  5598. +/**
  5599. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5600. + *
  5601. + * Redistribution and use in source and binary forms, with or without
  5602. + * modification, are permitted provided that the following conditions
  5603. + * are met:
  5604. + * 1. Redistributions of source code must retain the above copyright
  5605. + * notice, this list of conditions, and the following disclaimer,
  5606. + * without modification.
  5607. + * 2. Redistributions in binary form must reproduce the above copyright
  5608. + * notice, this list of conditions and the following disclaimer in the
  5609. + * documentation and/or other materials provided with the distribution.
  5610. + * 3. The names of the above-listed copyright holders may not be used
  5611. + * to endorse or promote products derived from this software without
  5612. + * specific prior written permission.
  5613. + *
  5614. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5615. + * GNU General Public License ("GPL") version 2, as published by the Free
  5616. + * Software Foundation.
  5617. + *
  5618. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5619. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5620. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5621. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5622. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5623. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5624. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5625. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5626. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5627. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5628. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5629. + */
  5630. +
  5631. +#ifndef VCHIQ_CONNECTED_H
  5632. +#define VCHIQ_CONNECTED_H
  5633. +
  5634. +/* ---- Include Files ----------------------------------------------------- */
  5635. +
  5636. +/* ---- Constants and Types ---------------------------------------------- */
  5637. +
  5638. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  5639. +
  5640. +/* ---- Variable Externs ------------------------------------------------- */
  5641. +
  5642. +/* ---- Function Prototypes ---------------------------------------------- */
  5643. +
  5644. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  5645. +void vchiq_call_connected_callbacks(void);
  5646. +
  5647. +#endif /* VCHIQ_CONNECTED_H */
  5648. --- /dev/null
  5649. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  5650. @@ -0,0 +1,3861 @@
  5651. +/**
  5652. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  5653. + *
  5654. + * Redistribution and use in source and binary forms, with or without
  5655. + * modification, are permitted provided that the following conditions
  5656. + * are met:
  5657. + * 1. Redistributions of source code must retain the above copyright
  5658. + * notice, this list of conditions, and the following disclaimer,
  5659. + * without modification.
  5660. + * 2. Redistributions in binary form must reproduce the above copyright
  5661. + * notice, this list of conditions and the following disclaimer in the
  5662. + * documentation and/or other materials provided with the distribution.
  5663. + * 3. The names of the above-listed copyright holders may not be used
  5664. + * to endorse or promote products derived from this software without
  5665. + * specific prior written permission.
  5666. + *
  5667. + * ALTERNATIVELY, this software may be distributed under the terms of the
  5668. + * GNU General Public License ("GPL") version 2, as published by the Free
  5669. + * Software Foundation.
  5670. + *
  5671. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  5672. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  5673. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  5674. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  5675. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5676. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5677. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5678. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5679. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5680. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  5681. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5682. + */
  5683. +
  5684. +#include "vchiq_core.h"
  5685. +
  5686. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  5687. +
  5688. +#define HANDLE_STATE_SHIFT 12
  5689. +
  5690. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  5691. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  5692. +#define SLOT_INDEX_FROM_DATA(state, data) \
  5693. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  5694. + VCHIQ_SLOT_SIZE)
  5695. +#define SLOT_INDEX_FROM_INFO(state, info) \
  5696. + ((unsigned int)(info - state->slot_info))
  5697. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  5698. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  5699. +
  5700. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  5701. +
  5702. +#define SRVTRACE_LEVEL(srv) \
  5703. + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
  5704. +#define SRVTRACE_ENABLED(srv, lev) \
  5705. + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
  5706. +
  5707. +struct vchiq_open_payload {
  5708. + int fourcc;
  5709. + int client_id;
  5710. + short version;
  5711. + short version_min;
  5712. +};
  5713. +
  5714. +struct vchiq_openack_payload {
  5715. + short version;
  5716. +};
  5717. +
  5718. +/* we require this for consistency between endpoints */
  5719. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  5720. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  5721. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  5722. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  5723. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  5724. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  5725. +
  5726. +/* Run time control of log level, based on KERN_XXX level. */
  5727. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  5728. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  5729. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  5730. +
  5731. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  5732. +
  5733. +static DEFINE_SPINLOCK(service_spinlock);
  5734. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  5735. +DEFINE_SPINLOCK(quota_spinlock);
  5736. +
  5737. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  5738. +static unsigned int handle_seq;
  5739. +
  5740. +static const char *const srvstate_names[] = {
  5741. + "FREE",
  5742. + "HIDDEN",
  5743. + "LISTENING",
  5744. + "OPENING",
  5745. + "OPEN",
  5746. + "OPENSYNC",
  5747. + "CLOSESENT",
  5748. + "CLOSERECVD",
  5749. + "CLOSEWAIT",
  5750. + "CLOSED"
  5751. +};
  5752. +
  5753. +static const char *const reason_names[] = {
  5754. + "SERVICE_OPENED",
  5755. + "SERVICE_CLOSED",
  5756. + "MESSAGE_AVAILABLE",
  5757. + "BULK_TRANSMIT_DONE",
  5758. + "BULK_RECEIVE_DONE",
  5759. + "BULK_TRANSMIT_ABORTED",
  5760. + "BULK_RECEIVE_ABORTED"
  5761. +};
  5762. +
  5763. +static const char *const conn_state_names[] = {
  5764. + "DISCONNECTED",
  5765. + "CONNECTING",
  5766. + "CONNECTED",
  5767. + "PAUSING",
  5768. + "PAUSE_SENT",
  5769. + "PAUSED",
  5770. + "RESUMING",
  5771. + "PAUSE_TIMEOUT",
  5772. + "RESUME_TIMEOUT"
  5773. +};
  5774. +
  5775. +
  5776. +static void
  5777. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  5778. +
  5779. +static const char *msg_type_str(unsigned int msg_type)
  5780. +{
  5781. + switch (msg_type) {
  5782. + case VCHIQ_MSG_PADDING: return "PADDING";
  5783. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  5784. + case VCHIQ_MSG_OPEN: return "OPEN";
  5785. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  5786. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  5787. + case VCHIQ_MSG_DATA: return "DATA";
  5788. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  5789. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  5790. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  5791. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  5792. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  5793. + case VCHIQ_MSG_RESUME: return "RESUME";
  5794. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  5795. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  5796. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  5797. + }
  5798. + return "???";
  5799. +}
  5800. +
  5801. +static inline void
  5802. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  5803. +{
  5804. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  5805. + service->state->id, service->localport,
  5806. + srvstate_names[service->srvstate],
  5807. + srvstate_names[newstate]);
  5808. + service->srvstate = newstate;
  5809. +}
  5810. +
  5811. +VCHIQ_SERVICE_T *
  5812. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  5813. +{
  5814. + VCHIQ_SERVICE_T *service;
  5815. +
  5816. + spin_lock(&service_spinlock);
  5817. + service = handle_to_service(handle);
  5818. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  5819. + (service->handle == handle)) {
  5820. + BUG_ON(service->ref_count == 0);
  5821. + service->ref_count++;
  5822. + } else
  5823. + service = NULL;
  5824. + spin_unlock(&service_spinlock);
  5825. +
  5826. + if (!service)
  5827. + vchiq_log_info(vchiq_core_log_level,
  5828. + "Invalid service handle 0x%x", handle);
  5829. +
  5830. + return service;
  5831. +}
  5832. +
  5833. +VCHIQ_SERVICE_T *
  5834. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  5835. +{
  5836. + VCHIQ_SERVICE_T *service = NULL;
  5837. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  5838. + spin_lock(&service_spinlock);
  5839. + service = state->services[localport];
  5840. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  5841. + BUG_ON(service->ref_count == 0);
  5842. + service->ref_count++;
  5843. + } else
  5844. + service = NULL;
  5845. + spin_unlock(&service_spinlock);
  5846. + }
  5847. +
  5848. + if (!service)
  5849. + vchiq_log_info(vchiq_core_log_level,
  5850. + "Invalid port %d", localport);
  5851. +
  5852. + return service;
  5853. +}
  5854. +
  5855. +VCHIQ_SERVICE_T *
  5856. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  5857. + VCHIQ_SERVICE_HANDLE_T handle) {
  5858. + VCHIQ_SERVICE_T *service;
  5859. +
  5860. + spin_lock(&service_spinlock);
  5861. + service = handle_to_service(handle);
  5862. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  5863. + (service->handle == handle) &&
  5864. + (service->instance == instance)) {
  5865. + BUG_ON(service->ref_count == 0);
  5866. + service->ref_count++;
  5867. + } else
  5868. + service = NULL;
  5869. + spin_unlock(&service_spinlock);
  5870. +
  5871. + if (!service)
  5872. + vchiq_log_info(vchiq_core_log_level,
  5873. + "Invalid service handle 0x%x", handle);
  5874. +
  5875. + return service;
  5876. +}
  5877. +
  5878. +VCHIQ_SERVICE_T *
  5879. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  5880. + VCHIQ_SERVICE_HANDLE_T handle) {
  5881. + VCHIQ_SERVICE_T *service;
  5882. +
  5883. + spin_lock(&service_spinlock);
  5884. + service = handle_to_service(handle);
  5885. + if (service &&
  5886. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  5887. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  5888. + (service->handle == handle) &&
  5889. + (service->instance == instance)) {
  5890. + BUG_ON(service->ref_count == 0);
  5891. + service->ref_count++;
  5892. + } else
  5893. + service = NULL;
  5894. + spin_unlock(&service_spinlock);
  5895. +
  5896. + if (!service)
  5897. + vchiq_log_info(vchiq_core_log_level,
  5898. + "Invalid service handle 0x%x", handle);
  5899. +
  5900. + return service;
  5901. +}
  5902. +
  5903. +VCHIQ_SERVICE_T *
  5904. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  5905. + int *pidx)
  5906. +{
  5907. + VCHIQ_SERVICE_T *service = NULL;
  5908. + int idx = *pidx;
  5909. +
  5910. + spin_lock(&service_spinlock);
  5911. + while (idx < state->unused_service) {
  5912. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  5913. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  5914. + (srv->instance == instance)) {
  5915. + service = srv;
  5916. + BUG_ON(service->ref_count == 0);
  5917. + service->ref_count++;
  5918. + break;
  5919. + }
  5920. + }
  5921. + spin_unlock(&service_spinlock);
  5922. +
  5923. + *pidx = idx;
  5924. +
  5925. + return service;
  5926. +}
  5927. +
  5928. +void
  5929. +lock_service(VCHIQ_SERVICE_T *service)
  5930. +{
  5931. + spin_lock(&service_spinlock);
  5932. + BUG_ON(!service || (service->ref_count == 0));
  5933. + if (service)
  5934. + service->ref_count++;
  5935. + spin_unlock(&service_spinlock);
  5936. +}
  5937. +
  5938. +void
  5939. +unlock_service(VCHIQ_SERVICE_T *service)
  5940. +{
  5941. + VCHIQ_STATE_T *state = service->state;
  5942. + spin_lock(&service_spinlock);
  5943. + BUG_ON(!service || (service->ref_count == 0));
  5944. + if (service && service->ref_count) {
  5945. + service->ref_count--;
  5946. + if (!service->ref_count) {
  5947. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  5948. + state->services[service->localport] = NULL;
  5949. + } else
  5950. + service = NULL;
  5951. + }
  5952. + spin_unlock(&service_spinlock);
  5953. +
  5954. + if (service && service->userdata_term)
  5955. + service->userdata_term(service->base.userdata);
  5956. +
  5957. + kfree(service);
  5958. +}
  5959. +
  5960. +int
  5961. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  5962. +{
  5963. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  5964. + int id;
  5965. +
  5966. + id = service ? service->client_id : 0;
  5967. + if (service)
  5968. + unlock_service(service);
  5969. +
  5970. + return id;
  5971. +}
  5972. +
  5973. +void *
  5974. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  5975. +{
  5976. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  5977. +
  5978. + return service ? service->base.userdata : NULL;
  5979. +}
  5980. +
  5981. +int
  5982. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  5983. +{
  5984. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  5985. +
  5986. + return service ? service->base.fourcc : 0;
  5987. +}
  5988. +
  5989. +static void
  5990. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  5991. +{
  5992. + VCHIQ_STATE_T *state = service->state;
  5993. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  5994. +
  5995. + service->closing = 1;
  5996. +
  5997. + /* Synchronise with other threads. */
  5998. + mutex_lock(&state->recycle_mutex);
  5999. + mutex_unlock(&state->recycle_mutex);
  6000. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  6001. + /* If we're pausing then the slot_mutex is held until resume
  6002. + * by the slot handler. Therefore don't try to acquire this
  6003. + * mutex if we're the slot handler and in the pause sent state.
  6004. + * We don't need to in this case anyway. */
  6005. + mutex_lock(&state->slot_mutex);
  6006. + mutex_unlock(&state->slot_mutex);
  6007. + }
  6008. +
  6009. + /* Unblock any sending thread. */
  6010. + service_quota = &state->service_quotas[service->localport];
  6011. + up(&service_quota->quota_event);
  6012. +}
  6013. +
  6014. +static void
  6015. +mark_service_closing(VCHIQ_SERVICE_T *service)
  6016. +{
  6017. + mark_service_closing_internal(service, 0);
  6018. +}
  6019. +
  6020. +static inline VCHIQ_STATUS_T
  6021. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  6022. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  6023. +{
  6024. + VCHIQ_STATUS_T status;
  6025. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  6026. + service->state->id, service->localport, reason_names[reason],
  6027. + (unsigned int)header, (unsigned int)bulk_userdata);
  6028. + status = service->base.callback(reason, header, service->handle,
  6029. + bulk_userdata);
  6030. + if (status == VCHIQ_ERROR) {
  6031. + vchiq_log_warning(vchiq_core_log_level,
  6032. + "%d: ignoring ERROR from callback to service %x",
  6033. + service->state->id, service->handle);
  6034. + status = VCHIQ_SUCCESS;
  6035. + }
  6036. + return status;
  6037. +}
  6038. +
  6039. +inline void
  6040. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  6041. +{
  6042. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  6043. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  6044. + conn_state_names[oldstate],
  6045. + conn_state_names[newstate]);
  6046. + state->conn_state = newstate;
  6047. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  6048. +}
  6049. +
  6050. +static inline void
  6051. +remote_event_create(REMOTE_EVENT_T *event)
  6052. +{
  6053. + event->armed = 0;
  6054. + /* Don't clear the 'fired' flag because it may already have been set
  6055. + ** by the other side. */
  6056. + sema_init(event->event, 0);
  6057. +}
  6058. +
  6059. +static inline void
  6060. +remote_event_destroy(REMOTE_EVENT_T *event)
  6061. +{
  6062. + (void)event;
  6063. +}
  6064. +
  6065. +static inline int
  6066. +remote_event_wait(REMOTE_EVENT_T *event)
  6067. +{
  6068. + if (!event->fired) {
  6069. + event->armed = 1;
  6070. + dsb();
  6071. + if (!event->fired) {
  6072. + if (down_interruptible(event->event) != 0) {
  6073. + event->armed = 0;
  6074. + return 0;
  6075. + }
  6076. + }
  6077. + event->armed = 0;
  6078. + wmb();
  6079. + }
  6080. +
  6081. + event->fired = 0;
  6082. + return 1;
  6083. +}
  6084. +
  6085. +static inline void
  6086. +remote_event_signal_local(REMOTE_EVENT_T *event)
  6087. +{
  6088. + event->armed = 0;
  6089. + up(event->event);
  6090. +}
  6091. +
  6092. +static inline void
  6093. +remote_event_poll(REMOTE_EVENT_T *event)
  6094. +{
  6095. + if (event->fired && event->armed)
  6096. + remote_event_signal_local(event);
  6097. +}
  6098. +
  6099. +void
  6100. +remote_event_pollall(VCHIQ_STATE_T *state)
  6101. +{
  6102. + remote_event_poll(&state->local->sync_trigger);
  6103. + remote_event_poll(&state->local->sync_release);
  6104. + remote_event_poll(&state->local->trigger);
  6105. + remote_event_poll(&state->local->recycle);
  6106. +}
  6107. +
  6108. +/* Round up message sizes so that any space at the end of a slot is always big
  6109. +** enough for a header. This relies on header size being a power of two, which
  6110. +** has been verified earlier by a static assertion. */
  6111. +
  6112. +static inline unsigned int
  6113. +calc_stride(unsigned int size)
  6114. +{
  6115. + /* Allow room for the header */
  6116. + size += sizeof(VCHIQ_HEADER_T);
  6117. +
  6118. + /* Round up */
  6119. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  6120. + - 1);
  6121. +}
  6122. +
  6123. +/* Called by the slot handler thread */
  6124. +static VCHIQ_SERVICE_T *
  6125. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  6126. +{
  6127. + int i;
  6128. +
  6129. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  6130. +
  6131. + for (i = 0; i < state->unused_service; i++) {
  6132. + VCHIQ_SERVICE_T *service = state->services[i];
  6133. + if (service &&
  6134. + (service->public_fourcc == fourcc) &&
  6135. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  6136. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  6137. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  6138. + lock_service(service);
  6139. + return service;
  6140. + }
  6141. + }
  6142. +
  6143. + return NULL;
  6144. +}
  6145. +
  6146. +/* Called by the slot handler thread */
  6147. +static VCHIQ_SERVICE_T *
  6148. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  6149. +{
  6150. + int i;
  6151. + for (i = 0; i < state->unused_service; i++) {
  6152. + VCHIQ_SERVICE_T *service = state->services[i];
  6153. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  6154. + && (service->remoteport == port)) {
  6155. + lock_service(service);
  6156. + return service;
  6157. + }
  6158. + }
  6159. + return NULL;
  6160. +}
  6161. +
  6162. +inline void
  6163. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  6164. +{
  6165. + uint32_t value;
  6166. +
  6167. + if (service) {
  6168. + do {
  6169. + value = atomic_read(&service->poll_flags);
  6170. + } while (atomic_cmpxchg(&service->poll_flags, value,
  6171. + value | (1 << poll_type)) != value);
  6172. +
  6173. + do {
  6174. + value = atomic_read(&state->poll_services[
  6175. + service->localport>>5]);
  6176. + } while (atomic_cmpxchg(
  6177. + &state->poll_services[service->localport>>5],
  6178. + value, value | (1 << (service->localport & 0x1f)))
  6179. + != value);
  6180. + }
  6181. +
  6182. + state->poll_needed = 1;
  6183. + wmb();
  6184. +
  6185. + /* ... and ensure the slot handler runs. */
  6186. + remote_event_signal_local(&state->local->trigger);
  6187. +}
  6188. +
  6189. +/* Called from queue_message, by the slot handler and application threads,
  6190. +** with slot_mutex held */
  6191. +static VCHIQ_HEADER_T *
  6192. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  6193. +{
  6194. + VCHIQ_SHARED_STATE_T *local = state->local;
  6195. + int tx_pos = state->local_tx_pos;
  6196. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  6197. +
  6198. + if (space > slot_space) {
  6199. + VCHIQ_HEADER_T *header;
  6200. + /* Fill the remaining space with padding */
  6201. + WARN_ON(state->tx_data == NULL);
  6202. + header = (VCHIQ_HEADER_T *)
  6203. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6204. + header->msgid = VCHIQ_MSGID_PADDING;
  6205. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  6206. +
  6207. + tx_pos += slot_space;
  6208. + }
  6209. +
  6210. + /* If necessary, get the next slot. */
  6211. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  6212. + int slot_index;
  6213. +
  6214. + /* If there is no free slot... */
  6215. +
  6216. + if (down_trylock(&state->slot_available_event) != 0) {
  6217. + /* ...wait for one. */
  6218. +
  6219. + VCHIQ_STATS_INC(state, slot_stalls);
  6220. +
  6221. + /* But first, flush through the last slot. */
  6222. + state->local_tx_pos = tx_pos;
  6223. + local->tx_pos = tx_pos;
  6224. + remote_event_signal(&state->remote->trigger);
  6225. +
  6226. + if (!is_blocking ||
  6227. + (down_interruptible(
  6228. + &state->slot_available_event) != 0))
  6229. + return NULL; /* No space available */
  6230. + }
  6231. +
  6232. + BUG_ON(tx_pos ==
  6233. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  6234. +
  6235. + slot_index = local->slot_queue[
  6236. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  6237. + VCHIQ_SLOT_QUEUE_MASK];
  6238. + state->tx_data =
  6239. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6240. + }
  6241. +
  6242. + state->local_tx_pos = tx_pos + space;
  6243. +
  6244. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  6245. +}
  6246. +
  6247. +/* Called by the recycle thread. */
  6248. +static void
  6249. +process_free_queue(VCHIQ_STATE_T *state)
  6250. +{
  6251. + VCHIQ_SHARED_STATE_T *local = state->local;
  6252. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  6253. + int slot_queue_available;
  6254. +
  6255. + /* Use a read memory barrier to ensure that any state that may have
  6256. + ** been modified by another thread is not masked by stale prefetched
  6257. + ** values. */
  6258. + rmb();
  6259. +
  6260. + /* Find slots which have been freed by the other side, and return them
  6261. + ** to the available queue. */
  6262. + slot_queue_available = state->slot_queue_available;
  6263. +
  6264. + while (slot_queue_available != local->slot_queue_recycle) {
  6265. + unsigned int pos;
  6266. + int slot_index = local->slot_queue[slot_queue_available++ &
  6267. + VCHIQ_SLOT_QUEUE_MASK];
  6268. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  6269. + int data_found = 0;
  6270. +
  6271. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  6272. + state->id, slot_index, (unsigned int)data,
  6273. + local->slot_queue_recycle, slot_queue_available);
  6274. +
  6275. + /* Initialise the bitmask for services which have used this
  6276. + ** slot */
  6277. + BITSET_ZERO(service_found);
  6278. +
  6279. + pos = 0;
  6280. +
  6281. + while (pos < VCHIQ_SLOT_SIZE) {
  6282. + VCHIQ_HEADER_T *header =
  6283. + (VCHIQ_HEADER_T *)(data + pos);
  6284. + int msgid = header->msgid;
  6285. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  6286. + int port = VCHIQ_MSG_SRCPORT(msgid);
  6287. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  6288. + &state->service_quotas[port];
  6289. + int count;
  6290. + spin_lock(&quota_spinlock);
  6291. + count = service_quota->message_use_count;
  6292. + if (count > 0)
  6293. + service_quota->message_use_count =
  6294. + count - 1;
  6295. + spin_unlock(&quota_spinlock);
  6296. +
  6297. + if (count == service_quota->message_quota)
  6298. + /* Signal the service that it
  6299. + ** has dropped below its quota
  6300. + */
  6301. + up(&service_quota->quota_event);
  6302. + else if (count == 0) {
  6303. + vchiq_log_error(vchiq_core_log_level,
  6304. + "service %d "
  6305. + "message_use_count=%d "
  6306. + "(header %x, msgid %x, "
  6307. + "header->msgid %x, "
  6308. + "header->size %x)",
  6309. + port,
  6310. + service_quota->
  6311. + message_use_count,
  6312. + (unsigned int)header, msgid,
  6313. + header->msgid,
  6314. + header->size);
  6315. + WARN(1, "invalid message use count\n");
  6316. + }
  6317. + if (!BITSET_IS_SET(service_found, port)) {
  6318. + /* Set the found bit for this service */
  6319. + BITSET_SET(service_found, port);
  6320. +
  6321. + spin_lock(&quota_spinlock);
  6322. + count = service_quota->slot_use_count;
  6323. + if (count > 0)
  6324. + service_quota->slot_use_count =
  6325. + count - 1;
  6326. + spin_unlock(&quota_spinlock);
  6327. +
  6328. + if (count > 0) {
  6329. + /* Signal the service in case
  6330. + ** it has dropped below its
  6331. + ** quota */
  6332. + up(&service_quota->quota_event);
  6333. + vchiq_log_trace(
  6334. + vchiq_core_log_level,
  6335. + "%d: pfq:%d %x@%x - "
  6336. + "slot_use->%d",
  6337. + state->id, port,
  6338. + header->size,
  6339. + (unsigned int)header,
  6340. + count - 1);
  6341. + } else {
  6342. + vchiq_log_error(
  6343. + vchiq_core_log_level,
  6344. + "service %d "
  6345. + "slot_use_count"
  6346. + "=%d (header %x"
  6347. + ", msgid %x, "
  6348. + "header->msgid"
  6349. + " %x, header->"
  6350. + "size %x)",
  6351. + port, count,
  6352. + (unsigned int)header,
  6353. + msgid,
  6354. + header->msgid,
  6355. + header->size);
  6356. + WARN(1, "bad slot use count\n");
  6357. + }
  6358. + }
  6359. +
  6360. + data_found = 1;
  6361. + }
  6362. +
  6363. + pos += calc_stride(header->size);
  6364. + if (pos > VCHIQ_SLOT_SIZE) {
  6365. + vchiq_log_error(vchiq_core_log_level,
  6366. + "pfq - pos %x: header %x, msgid %x, "
  6367. + "header->msgid %x, header->size %x",
  6368. + pos, (unsigned int)header, msgid,
  6369. + header->msgid, header->size);
  6370. + WARN(1, "invalid slot position\n");
  6371. + }
  6372. + }
  6373. +
  6374. + if (data_found) {
  6375. + int count;
  6376. + spin_lock(&quota_spinlock);
  6377. + count = state->data_use_count;
  6378. + if (count > 0)
  6379. + state->data_use_count =
  6380. + count - 1;
  6381. + spin_unlock(&quota_spinlock);
  6382. + if (count == state->data_quota)
  6383. + up(&state->data_quota_event);
  6384. + }
  6385. +
  6386. + state->slot_queue_available = slot_queue_available;
  6387. + up(&state->slot_available_event);
  6388. + }
  6389. +}
  6390. +
  6391. +/* Called by the slot handler and application threads */
  6392. +static VCHIQ_STATUS_T
  6393. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6394. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6395. + int count, int size, int is_blocking)
  6396. +{
  6397. + VCHIQ_SHARED_STATE_T *local;
  6398. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  6399. + VCHIQ_HEADER_T *header;
  6400. + int type = VCHIQ_MSG_TYPE(msgid);
  6401. +
  6402. + unsigned int stride;
  6403. +
  6404. + local = state->local;
  6405. +
  6406. + stride = calc_stride(size);
  6407. +
  6408. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  6409. +
  6410. + if ((type != VCHIQ_MSG_RESUME) &&
  6411. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  6412. + return VCHIQ_RETRY;
  6413. +
  6414. + if (type == VCHIQ_MSG_DATA) {
  6415. + int tx_end_index;
  6416. +
  6417. + BUG_ON(!service);
  6418. +
  6419. + if (service->closing) {
  6420. + /* The service has been closed */
  6421. + mutex_unlock(&state->slot_mutex);
  6422. + return VCHIQ_ERROR;
  6423. + }
  6424. +
  6425. + service_quota = &state->service_quotas[service->localport];
  6426. +
  6427. + spin_lock(&quota_spinlock);
  6428. +
  6429. + /* Ensure this service doesn't use more than its quota of
  6430. + ** messages or slots */
  6431. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6432. + state->local_tx_pos + stride - 1);
  6433. +
  6434. + /* Ensure data messages don't use more than their quota of
  6435. + ** slots */
  6436. + while ((tx_end_index != state->previous_data_index) &&
  6437. + (state->data_use_count == state->data_quota)) {
  6438. + VCHIQ_STATS_INC(state, data_stalls);
  6439. + spin_unlock(&quota_spinlock);
  6440. + mutex_unlock(&state->slot_mutex);
  6441. +
  6442. + if (down_interruptible(&state->data_quota_event)
  6443. + != 0)
  6444. + return VCHIQ_RETRY;
  6445. +
  6446. + mutex_lock(&state->slot_mutex);
  6447. + spin_lock(&quota_spinlock);
  6448. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6449. + state->local_tx_pos + stride - 1);
  6450. + if ((tx_end_index == state->previous_data_index) ||
  6451. + (state->data_use_count < state->data_quota)) {
  6452. + /* Pass the signal on to other waiters */
  6453. + up(&state->data_quota_event);
  6454. + break;
  6455. + }
  6456. + }
  6457. +
  6458. + while ((service_quota->message_use_count ==
  6459. + service_quota->message_quota) ||
  6460. + ((tx_end_index != service_quota->previous_tx_index) &&
  6461. + (service_quota->slot_use_count ==
  6462. + service_quota->slot_quota))) {
  6463. + spin_unlock(&quota_spinlock);
  6464. + vchiq_log_trace(vchiq_core_log_level,
  6465. + "%d: qm:%d %s,%x - quota stall "
  6466. + "(msg %d, slot %d)",
  6467. + state->id, service->localport,
  6468. + msg_type_str(type), size,
  6469. + service_quota->message_use_count,
  6470. + service_quota->slot_use_count);
  6471. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  6472. + mutex_unlock(&state->slot_mutex);
  6473. + if (down_interruptible(&service_quota->quota_event)
  6474. + != 0)
  6475. + return VCHIQ_RETRY;
  6476. + if (service->closing)
  6477. + return VCHIQ_ERROR;
  6478. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  6479. + return VCHIQ_RETRY;
  6480. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  6481. + /* The service has been closed */
  6482. + mutex_unlock(&state->slot_mutex);
  6483. + return VCHIQ_ERROR;
  6484. + }
  6485. + spin_lock(&quota_spinlock);
  6486. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  6487. + state->local_tx_pos + stride - 1);
  6488. + }
  6489. +
  6490. + spin_unlock(&quota_spinlock);
  6491. + }
  6492. +
  6493. + header = reserve_space(state, stride, is_blocking);
  6494. +
  6495. + if (!header) {
  6496. + if (service)
  6497. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  6498. + mutex_unlock(&state->slot_mutex);
  6499. + return VCHIQ_RETRY;
  6500. + }
  6501. +
  6502. + if (type == VCHIQ_MSG_DATA) {
  6503. + int i, pos;
  6504. + int tx_end_index;
  6505. + int slot_use_count;
  6506. +
  6507. + vchiq_log_info(vchiq_core_log_level,
  6508. + "%d: qm %s@%x,%x (%d->%d)",
  6509. + state->id,
  6510. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6511. + (unsigned int)header, size,
  6512. + VCHIQ_MSG_SRCPORT(msgid),
  6513. + VCHIQ_MSG_DSTPORT(msgid));
  6514. +
  6515. + BUG_ON(!service);
  6516. +
  6517. + for (i = 0, pos = 0; i < (unsigned int)count;
  6518. + pos += elements[i++].size)
  6519. + if (elements[i].size) {
  6520. + if (vchiq_copy_from_user
  6521. + (header->data + pos, elements[i].data,
  6522. + (size_t) elements[i].size) !=
  6523. + VCHIQ_SUCCESS) {
  6524. + mutex_unlock(&state->slot_mutex);
  6525. + VCHIQ_SERVICE_STATS_INC(service,
  6526. + error_count);
  6527. + return VCHIQ_ERROR;
  6528. + }
  6529. + if (i == 0) {
  6530. + if (SRVTRACE_ENABLED(service,
  6531. + VCHIQ_LOG_INFO))
  6532. + vchiq_log_dump_mem("Sent", 0,
  6533. + header->data + pos,
  6534. + min(64u,
  6535. + elements[0].size));
  6536. + }
  6537. + }
  6538. +
  6539. + spin_lock(&quota_spinlock);
  6540. + service_quota->message_use_count++;
  6541. +
  6542. + tx_end_index =
  6543. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  6544. +
  6545. + /* If this transmission can't fit in the last slot used by any
  6546. + ** service, the data_use_count must be increased. */
  6547. + if (tx_end_index != state->previous_data_index) {
  6548. + state->previous_data_index = tx_end_index;
  6549. + state->data_use_count++;
  6550. + }
  6551. +
  6552. + /* If this isn't the same slot last used by this service,
  6553. + ** the service's slot_use_count must be increased. */
  6554. + if (tx_end_index != service_quota->previous_tx_index) {
  6555. + service_quota->previous_tx_index = tx_end_index;
  6556. + slot_use_count = ++service_quota->slot_use_count;
  6557. + } else {
  6558. + slot_use_count = 0;
  6559. + }
  6560. +
  6561. + spin_unlock(&quota_spinlock);
  6562. +
  6563. + if (slot_use_count)
  6564. + vchiq_log_trace(vchiq_core_log_level,
  6565. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  6566. + state->id, service->localport,
  6567. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  6568. + slot_use_count, header);
  6569. +
  6570. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6571. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6572. + } else {
  6573. + vchiq_log_info(vchiq_core_log_level,
  6574. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  6575. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6576. + (unsigned int)header, size,
  6577. + VCHIQ_MSG_SRCPORT(msgid),
  6578. + VCHIQ_MSG_DSTPORT(msgid));
  6579. + if (size != 0) {
  6580. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6581. + memcpy(header->data, elements[0].data,
  6582. + elements[0].size);
  6583. + }
  6584. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6585. + }
  6586. +
  6587. + header->msgid = msgid;
  6588. + header->size = size;
  6589. +
  6590. + {
  6591. + int svc_fourcc;
  6592. +
  6593. + svc_fourcc = service
  6594. + ? service->base.fourcc
  6595. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6596. +
  6597. + vchiq_log_info(SRVTRACE_LEVEL(service),
  6598. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6599. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6600. + VCHIQ_MSG_TYPE(msgid),
  6601. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6602. + VCHIQ_MSG_SRCPORT(msgid),
  6603. + VCHIQ_MSG_DSTPORT(msgid),
  6604. + size);
  6605. + }
  6606. +
  6607. + /* Make sure the new header is visible to the peer. */
  6608. + wmb();
  6609. +
  6610. + /* Make the new tx_pos visible to the peer. */
  6611. + local->tx_pos = state->local_tx_pos;
  6612. + wmb();
  6613. +
  6614. + if (service && (type == VCHIQ_MSG_CLOSE))
  6615. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  6616. +
  6617. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  6618. + mutex_unlock(&state->slot_mutex);
  6619. +
  6620. + remote_event_signal(&state->remote->trigger);
  6621. +
  6622. + return VCHIQ_SUCCESS;
  6623. +}
  6624. +
  6625. +/* Called by the slot handler and application threads */
  6626. +static VCHIQ_STATUS_T
  6627. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  6628. + int msgid, const VCHIQ_ELEMENT_T *elements,
  6629. + int count, int size, int is_blocking)
  6630. +{
  6631. + VCHIQ_SHARED_STATE_T *local;
  6632. + VCHIQ_HEADER_T *header;
  6633. +
  6634. + local = state->local;
  6635. +
  6636. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  6637. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  6638. + return VCHIQ_RETRY;
  6639. +
  6640. + remote_event_wait(&local->sync_release);
  6641. +
  6642. + rmb();
  6643. +
  6644. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  6645. + local->slot_sync);
  6646. +
  6647. + {
  6648. + int oldmsgid = header->msgid;
  6649. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  6650. + vchiq_log_error(vchiq_core_log_level,
  6651. + "%d: qms - msgid %x, not PADDING",
  6652. + state->id, oldmsgid);
  6653. + }
  6654. +
  6655. + if (service) {
  6656. + int i, pos;
  6657. +
  6658. + vchiq_log_info(vchiq_sync_log_level,
  6659. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6660. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6661. + (unsigned int)header, size,
  6662. + VCHIQ_MSG_SRCPORT(msgid),
  6663. + VCHIQ_MSG_DSTPORT(msgid));
  6664. +
  6665. + for (i = 0, pos = 0; i < (unsigned int)count;
  6666. + pos += elements[i++].size)
  6667. + if (elements[i].size) {
  6668. + if (vchiq_copy_from_user
  6669. + (header->data + pos, elements[i].data,
  6670. + (size_t) elements[i].size) !=
  6671. + VCHIQ_SUCCESS) {
  6672. + mutex_unlock(&state->sync_mutex);
  6673. + VCHIQ_SERVICE_STATS_INC(service,
  6674. + error_count);
  6675. + return VCHIQ_ERROR;
  6676. + }
  6677. + if (i == 0) {
  6678. + if (vchiq_sync_log_level >=
  6679. + VCHIQ_LOG_TRACE)
  6680. + vchiq_log_dump_mem("Sent Sync",
  6681. + 0, header->data + pos,
  6682. + min(64u,
  6683. + elements[0].size));
  6684. + }
  6685. + }
  6686. +
  6687. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  6688. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  6689. + } else {
  6690. + vchiq_log_info(vchiq_sync_log_level,
  6691. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  6692. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6693. + (unsigned int)header, size,
  6694. + VCHIQ_MSG_SRCPORT(msgid),
  6695. + VCHIQ_MSG_DSTPORT(msgid));
  6696. + if (size != 0) {
  6697. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  6698. + memcpy(header->data, elements[0].data,
  6699. + elements[0].size);
  6700. + }
  6701. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  6702. + }
  6703. +
  6704. + header->size = size;
  6705. + header->msgid = msgid;
  6706. +
  6707. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  6708. + int svc_fourcc;
  6709. +
  6710. + svc_fourcc = service
  6711. + ? service->base.fourcc
  6712. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  6713. +
  6714. + vchiq_log_trace(vchiq_sync_log_level,
  6715. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  6716. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  6717. + VCHIQ_MSG_TYPE(msgid),
  6718. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  6719. + VCHIQ_MSG_SRCPORT(msgid),
  6720. + VCHIQ_MSG_DSTPORT(msgid),
  6721. + size);
  6722. + }
  6723. +
  6724. + /* Make sure the new header is visible to the peer. */
  6725. + wmb();
  6726. +
  6727. + remote_event_signal(&state->remote->sync_trigger);
  6728. +
  6729. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  6730. + mutex_unlock(&state->sync_mutex);
  6731. +
  6732. + return VCHIQ_SUCCESS;
  6733. +}
  6734. +
  6735. +static inline void
  6736. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  6737. +{
  6738. + slot->use_count++;
  6739. +}
  6740. +
  6741. +static void
  6742. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  6743. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  6744. +{
  6745. + int release_count;
  6746. +
  6747. + mutex_lock(&state->recycle_mutex);
  6748. +
  6749. + if (header) {
  6750. + int msgid = header->msgid;
  6751. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  6752. + (service && service->closing)) {
  6753. + mutex_unlock(&state->recycle_mutex);
  6754. + return;
  6755. + }
  6756. +
  6757. + /* Rewrite the message header to prevent a double
  6758. + ** release */
  6759. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  6760. + }
  6761. +
  6762. + release_count = slot_info->release_count;
  6763. + slot_info->release_count = ++release_count;
  6764. +
  6765. + if (release_count == slot_info->use_count) {
  6766. + int slot_queue_recycle;
  6767. + /* Add to the freed queue */
  6768. +
  6769. + /* A read barrier is necessary here to prevent speculative
  6770. + ** fetches of remote->slot_queue_recycle from overtaking the
  6771. + ** mutex. */
  6772. + rmb();
  6773. +
  6774. + slot_queue_recycle = state->remote->slot_queue_recycle;
  6775. + state->remote->slot_queue[slot_queue_recycle &
  6776. + VCHIQ_SLOT_QUEUE_MASK] =
  6777. + SLOT_INDEX_FROM_INFO(state, slot_info);
  6778. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  6779. + vchiq_log_info(vchiq_core_log_level,
  6780. + "%d: release_slot %d - recycle->%x",
  6781. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  6782. + state->remote->slot_queue_recycle);
  6783. +
  6784. + /* A write barrier is necessary, but remote_event_signal
  6785. + ** contains one. */
  6786. + remote_event_signal(&state->remote->recycle);
  6787. + }
  6788. +
  6789. + mutex_unlock(&state->recycle_mutex);
  6790. +}
  6791. +
  6792. +/* Called by the slot handler - don't hold the bulk mutex */
  6793. +static VCHIQ_STATUS_T
  6794. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  6795. + int retry_poll)
  6796. +{
  6797. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  6798. +
  6799. + vchiq_log_trace(vchiq_core_log_level,
  6800. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  6801. + service->state->id, service->localport,
  6802. + (queue == &service->bulk_tx) ? 't' : 'r',
  6803. + queue->process, queue->remote_notify, queue->remove);
  6804. +
  6805. + if (service->state->is_master) {
  6806. + while (queue->remote_notify != queue->process) {
  6807. + VCHIQ_BULK_T *bulk =
  6808. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  6809. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  6810. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  6811. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  6812. + service->remoteport);
  6813. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  6814. + /* Only reply to non-dummy bulk requests */
  6815. + if (bulk->remote_data) {
  6816. + status = queue_message(service->state, NULL,
  6817. + msgid, &element, 1, 4, 0);
  6818. + if (status != VCHIQ_SUCCESS)
  6819. + break;
  6820. + }
  6821. + queue->remote_notify++;
  6822. + }
  6823. + } else {
  6824. + queue->remote_notify = queue->process;
  6825. + }
  6826. +
  6827. + if (status == VCHIQ_SUCCESS) {
  6828. + while (queue->remove != queue->remote_notify) {
  6829. + VCHIQ_BULK_T *bulk =
  6830. + &queue->bulks[BULK_INDEX(queue->remove)];
  6831. +
  6832. + /* Only generate callbacks for non-dummy bulk
  6833. + ** requests, and non-terminated services */
  6834. + if (bulk->data && service->instance) {
  6835. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  6836. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  6837. + VCHIQ_SERVICE_STATS_INC(service,
  6838. + bulk_tx_count);
  6839. + VCHIQ_SERVICE_STATS_ADD(service,
  6840. + bulk_tx_bytes,
  6841. + bulk->actual);
  6842. + } else {
  6843. + VCHIQ_SERVICE_STATS_INC(service,
  6844. + bulk_rx_count);
  6845. + VCHIQ_SERVICE_STATS_ADD(service,
  6846. + bulk_rx_bytes,
  6847. + bulk->actual);
  6848. + }
  6849. + } else {
  6850. + VCHIQ_SERVICE_STATS_INC(service,
  6851. + bulk_aborted_count);
  6852. + }
  6853. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  6854. + struct bulk_waiter *waiter;
  6855. + spin_lock(&bulk_waiter_spinlock);
  6856. + waiter = bulk->userdata;
  6857. + if (waiter) {
  6858. + waiter->actual = bulk->actual;
  6859. + up(&waiter->event);
  6860. + }
  6861. + spin_unlock(&bulk_waiter_spinlock);
  6862. + } else if (bulk->mode ==
  6863. + VCHIQ_BULK_MODE_CALLBACK) {
  6864. + VCHIQ_REASON_T reason = (bulk->dir ==
  6865. + VCHIQ_BULK_TRANSMIT) ?
  6866. + ((bulk->actual ==
  6867. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  6868. + VCHIQ_BULK_TRANSMIT_ABORTED :
  6869. + VCHIQ_BULK_TRANSMIT_DONE) :
  6870. + ((bulk->actual ==
  6871. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  6872. + VCHIQ_BULK_RECEIVE_ABORTED :
  6873. + VCHIQ_BULK_RECEIVE_DONE);
  6874. + status = make_service_callback(service,
  6875. + reason, NULL, bulk->userdata);
  6876. + if (status == VCHIQ_RETRY)
  6877. + break;
  6878. + }
  6879. + }
  6880. +
  6881. + queue->remove++;
  6882. + up(&service->bulk_remove_event);
  6883. + }
  6884. + if (!retry_poll)
  6885. + status = VCHIQ_SUCCESS;
  6886. + }
  6887. +
  6888. + if (status == VCHIQ_RETRY)
  6889. + request_poll(service->state, service,
  6890. + (queue == &service->bulk_tx) ?
  6891. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  6892. +
  6893. + return status;
  6894. +}
  6895. +
  6896. +/* Called by the slot handler thread */
  6897. +static void
  6898. +poll_services(VCHIQ_STATE_T *state)
  6899. +{
  6900. + int group, i;
  6901. +
  6902. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  6903. + uint32_t flags;
  6904. + flags = atomic_xchg(&state->poll_services[group], 0);
  6905. + for (i = 0; flags; i++) {
  6906. + if (flags & (1 << i)) {
  6907. + VCHIQ_SERVICE_T *service =
  6908. + find_service_by_port(state,
  6909. + (group<<5) + i);
  6910. + uint32_t service_flags;
  6911. + flags &= ~(1 << i);
  6912. + if (!service)
  6913. + continue;
  6914. + service_flags =
  6915. + atomic_xchg(&service->poll_flags, 0);
  6916. + if (service_flags &
  6917. + (1 << VCHIQ_POLL_REMOVE)) {
  6918. + vchiq_log_info(vchiq_core_log_level,
  6919. + "%d: ps - remove %d<->%d",
  6920. + state->id, service->localport,
  6921. + service->remoteport);
  6922. +
  6923. + /* Make it look like a client, because
  6924. + it must be removed and not left in
  6925. + the LISTENING state. */
  6926. + service->public_fourcc =
  6927. + VCHIQ_FOURCC_INVALID;
  6928. +
  6929. + if (vchiq_close_service_internal(
  6930. + service, 0/*!close_recvd*/) !=
  6931. + VCHIQ_SUCCESS)
  6932. + request_poll(state, service,
  6933. + VCHIQ_POLL_REMOVE);
  6934. + } else if (service_flags &
  6935. + (1 << VCHIQ_POLL_TERMINATE)) {
  6936. + vchiq_log_info(vchiq_core_log_level,
  6937. + "%d: ps - terminate %d<->%d",
  6938. + state->id, service->localport,
  6939. + service->remoteport);
  6940. + if (vchiq_close_service_internal(
  6941. + service, 0/*!close_recvd*/) !=
  6942. + VCHIQ_SUCCESS)
  6943. + request_poll(state, service,
  6944. + VCHIQ_POLL_TERMINATE);
  6945. + }
  6946. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  6947. + notify_bulks(service,
  6948. + &service->bulk_tx,
  6949. + 1/*retry_poll*/);
  6950. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  6951. + notify_bulks(service,
  6952. + &service->bulk_rx,
  6953. + 1/*retry_poll*/);
  6954. + unlock_service(service);
  6955. + }
  6956. + }
  6957. + }
  6958. +}
  6959. +
  6960. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  6961. +static int
  6962. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  6963. +{
  6964. + VCHIQ_STATE_T *state = service->state;
  6965. + int resolved = 0;
  6966. + int rc;
  6967. +
  6968. + while ((queue->process != queue->local_insert) &&
  6969. + (queue->process != queue->remote_insert)) {
  6970. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  6971. +
  6972. + vchiq_log_trace(vchiq_core_log_level,
  6973. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  6974. + state->id, service->localport,
  6975. + (queue == &service->bulk_tx) ? 't' : 'r',
  6976. + queue->local_insert, queue->remote_insert,
  6977. + queue->process);
  6978. +
  6979. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  6980. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  6981. +
  6982. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  6983. + if (rc != 0)
  6984. + break;
  6985. +
  6986. + vchiq_transfer_bulk(bulk);
  6987. + mutex_unlock(&state->bulk_transfer_mutex);
  6988. +
  6989. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  6990. + const char *header = (queue == &service->bulk_tx) ?
  6991. + "Send Bulk to" : "Recv Bulk from";
  6992. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  6993. + vchiq_log_info(SRVTRACE_LEVEL(service),
  6994. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  6995. + header,
  6996. + VCHIQ_FOURCC_AS_4CHARS(
  6997. + service->base.fourcc),
  6998. + service->remoteport,
  6999. + bulk->size,
  7000. + (unsigned int)bulk->data,
  7001. + (unsigned int)bulk->remote_data);
  7002. + else
  7003. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7004. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  7005. + " rx len:%d %x<->%x",
  7006. + header,
  7007. + VCHIQ_FOURCC_AS_4CHARS(
  7008. + service->base.fourcc),
  7009. + service->remoteport,
  7010. + bulk->size,
  7011. + bulk->remote_size,
  7012. + (unsigned int)bulk->data,
  7013. + (unsigned int)bulk->remote_data);
  7014. + }
  7015. +
  7016. + vchiq_complete_bulk(bulk);
  7017. + queue->process++;
  7018. + resolved++;
  7019. + }
  7020. + return resolved;
  7021. +}
  7022. +
  7023. +/* Called with the bulk_mutex held */
  7024. +static void
  7025. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  7026. +{
  7027. + int is_tx = (queue == &service->bulk_tx);
  7028. + vchiq_log_trace(vchiq_core_log_level,
  7029. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  7030. + service->state->id, service->localport, is_tx ? 't' : 'r',
  7031. + queue->local_insert, queue->remote_insert, queue->process);
  7032. +
  7033. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  7034. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  7035. +
  7036. + while ((queue->process != queue->local_insert) ||
  7037. + (queue->process != queue->remote_insert)) {
  7038. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  7039. +
  7040. + if (queue->process == queue->remote_insert) {
  7041. + /* fabricate a matching dummy bulk */
  7042. + bulk->remote_data = NULL;
  7043. + bulk->remote_size = 0;
  7044. + queue->remote_insert++;
  7045. + }
  7046. +
  7047. + if (queue->process != queue->local_insert) {
  7048. + vchiq_complete_bulk(bulk);
  7049. +
  7050. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7051. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  7052. + "rx len:%d",
  7053. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  7054. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7055. + service->remoteport,
  7056. + bulk->size,
  7057. + bulk->remote_size);
  7058. + } else {
  7059. + /* fabricate a matching dummy bulk */
  7060. + bulk->data = NULL;
  7061. + bulk->size = 0;
  7062. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  7063. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  7064. + VCHIQ_BULK_RECEIVE;
  7065. + queue->local_insert++;
  7066. + }
  7067. +
  7068. + queue->process++;
  7069. + }
  7070. +}
  7071. +
  7072. +/* Called from the slot handler thread */
  7073. +static void
  7074. +pause_bulks(VCHIQ_STATE_T *state)
  7075. +{
  7076. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  7077. + WARN_ON_ONCE(1);
  7078. + atomic_set(&pause_bulks_count, 1);
  7079. + return;
  7080. + }
  7081. +
  7082. + /* Block bulk transfers from all services */
  7083. + mutex_lock(&state->bulk_transfer_mutex);
  7084. +}
  7085. +
  7086. +/* Called from the slot handler thread */
  7087. +static void
  7088. +resume_bulks(VCHIQ_STATE_T *state)
  7089. +{
  7090. + int i;
  7091. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  7092. + WARN_ON_ONCE(1);
  7093. + atomic_set(&pause_bulks_count, 0);
  7094. + return;
  7095. + }
  7096. +
  7097. + /* Allow bulk transfers from all services */
  7098. + mutex_unlock(&state->bulk_transfer_mutex);
  7099. +
  7100. + if (state->deferred_bulks == 0)
  7101. + return;
  7102. +
  7103. + /* Deal with any bulks which had to be deferred due to being in
  7104. + * paused state. Don't try to match up to number of deferred bulks
  7105. + * in case we've had something come and close the service in the
  7106. + * interim - just process all bulk queues for all services */
  7107. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  7108. + __func__, state->deferred_bulks);
  7109. +
  7110. + for (i = 0; i < state->unused_service; i++) {
  7111. + VCHIQ_SERVICE_T *service = state->services[i];
  7112. + int resolved_rx = 0;
  7113. + int resolved_tx = 0;
  7114. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  7115. + continue;
  7116. +
  7117. + mutex_lock(&service->bulk_mutex);
  7118. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  7119. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  7120. + mutex_unlock(&service->bulk_mutex);
  7121. + if (resolved_rx)
  7122. + notify_bulks(service, &service->bulk_rx, 1);
  7123. + if (resolved_tx)
  7124. + notify_bulks(service, &service->bulk_tx, 1);
  7125. + }
  7126. + state->deferred_bulks = 0;
  7127. +}
  7128. +
  7129. +static int
  7130. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  7131. +{
  7132. + VCHIQ_SERVICE_T *service = NULL;
  7133. + int msgid, size;
  7134. + int type;
  7135. + unsigned int localport, remoteport;
  7136. +
  7137. + msgid = header->msgid;
  7138. + size = header->size;
  7139. + type = VCHIQ_MSG_TYPE(msgid);
  7140. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7141. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7142. + if (size >= sizeof(struct vchiq_open_payload)) {
  7143. + const struct vchiq_open_payload *payload =
  7144. + (struct vchiq_open_payload *)header->data;
  7145. + unsigned int fourcc;
  7146. +
  7147. + fourcc = payload->fourcc;
  7148. + vchiq_log_info(vchiq_core_log_level,
  7149. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  7150. + state->id, (unsigned int)header,
  7151. + localport,
  7152. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  7153. +
  7154. + service = get_listening_service(state, fourcc);
  7155. +
  7156. + if (service) {
  7157. + /* A matching service exists */
  7158. + short version = payload->version;
  7159. + short version_min = payload->version_min;
  7160. + if ((service->version < version_min) ||
  7161. + (version < service->version_min)) {
  7162. + /* Version mismatch */
  7163. + vchiq_loud_error_header();
  7164. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  7165. + "version mismatch - local (%d, min %d)"
  7166. + " vs. remote (%d, min %d)",
  7167. + state->id, service->localport,
  7168. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  7169. + service->version, service->version_min,
  7170. + version, version_min);
  7171. + vchiq_loud_error_footer();
  7172. + unlock_service(service);
  7173. + service = NULL;
  7174. + goto fail_open;
  7175. + }
  7176. + service->peer_version = version;
  7177. +
  7178. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  7179. + struct vchiq_openack_payload ack_payload = {
  7180. + service->version
  7181. + };
  7182. + VCHIQ_ELEMENT_T body = {
  7183. + &ack_payload,
  7184. + sizeof(ack_payload)
  7185. + };
  7186. +
  7187. + /* Acknowledge the OPEN */
  7188. + if (service->sync) {
  7189. + if (queue_message_sync(state, NULL,
  7190. + VCHIQ_MAKE_MSG(
  7191. + VCHIQ_MSG_OPENACK,
  7192. + service->localport,
  7193. + remoteport),
  7194. + &body, 1, sizeof(ack_payload),
  7195. + 0) == VCHIQ_RETRY)
  7196. + goto bail_not_ready;
  7197. + } else {
  7198. + if (queue_message(state, NULL,
  7199. + VCHIQ_MAKE_MSG(
  7200. + VCHIQ_MSG_OPENACK,
  7201. + service->localport,
  7202. + remoteport),
  7203. + &body, 1, sizeof(ack_payload),
  7204. + 0) == VCHIQ_RETRY)
  7205. + goto bail_not_ready;
  7206. + }
  7207. +
  7208. + /* The service is now open */
  7209. + vchiq_set_service_state(service,
  7210. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  7211. + : VCHIQ_SRVSTATE_OPEN);
  7212. + }
  7213. +
  7214. + service->remoteport = remoteport;
  7215. + service->client_id = ((int *)header->data)[1];
  7216. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  7217. + NULL, NULL) == VCHIQ_RETRY) {
  7218. + /* Bail out if not ready */
  7219. + service->remoteport = VCHIQ_PORT_FREE;
  7220. + goto bail_not_ready;
  7221. + }
  7222. +
  7223. + /* Success - the message has been dealt with */
  7224. + unlock_service(service);
  7225. + return 1;
  7226. + }
  7227. + }
  7228. +
  7229. +fail_open:
  7230. + /* No available service, or an invalid request - send a CLOSE */
  7231. + if (queue_message(state, NULL,
  7232. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  7233. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  7234. + goto bail_not_ready;
  7235. +
  7236. + return 1;
  7237. +
  7238. +bail_not_ready:
  7239. + if (service)
  7240. + unlock_service(service);
  7241. +
  7242. + return 0;
  7243. +}
  7244. +
  7245. +/* Called by the slot handler thread */
  7246. +static void
  7247. +parse_rx_slots(VCHIQ_STATE_T *state)
  7248. +{
  7249. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  7250. + VCHIQ_SERVICE_T *service = NULL;
  7251. + int tx_pos;
  7252. + DEBUG_INITIALISE(state->local)
  7253. +
  7254. + tx_pos = remote->tx_pos;
  7255. +
  7256. + while (state->rx_pos != tx_pos) {
  7257. + VCHIQ_HEADER_T *header;
  7258. + int msgid, size;
  7259. + int type;
  7260. + unsigned int localport, remoteport;
  7261. +
  7262. + DEBUG_TRACE(PARSE_LINE);
  7263. + if (!state->rx_data) {
  7264. + int rx_index;
  7265. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  7266. + rx_index = remote->slot_queue[
  7267. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  7268. + VCHIQ_SLOT_QUEUE_MASK];
  7269. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  7270. + rx_index);
  7271. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  7272. +
  7273. + /* Initialise use_count to one, and increment
  7274. + ** release_count at the end of the slot to avoid
  7275. + ** releasing the slot prematurely. */
  7276. + state->rx_info->use_count = 1;
  7277. + state->rx_info->release_count = 0;
  7278. + }
  7279. +
  7280. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  7281. + (state->rx_pos & VCHIQ_SLOT_MASK));
  7282. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  7283. + msgid = header->msgid;
  7284. + DEBUG_VALUE(PARSE_MSGID, msgid);
  7285. + size = header->size;
  7286. + type = VCHIQ_MSG_TYPE(msgid);
  7287. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7288. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7289. +
  7290. + if (type != VCHIQ_MSG_DATA)
  7291. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  7292. +
  7293. + switch (type) {
  7294. + case VCHIQ_MSG_OPENACK:
  7295. + case VCHIQ_MSG_CLOSE:
  7296. + case VCHIQ_MSG_DATA:
  7297. + case VCHIQ_MSG_BULK_RX:
  7298. + case VCHIQ_MSG_BULK_TX:
  7299. + case VCHIQ_MSG_BULK_RX_DONE:
  7300. + case VCHIQ_MSG_BULK_TX_DONE:
  7301. + service = find_service_by_port(state, localport);
  7302. + if ((!service ||
  7303. + ((service->remoteport != remoteport) &&
  7304. + (service->remoteport != VCHIQ_PORT_FREE))) &&
  7305. + (localport == 0) &&
  7306. + (type == VCHIQ_MSG_CLOSE)) {
  7307. + /* This could be a CLOSE from a client which
  7308. + hadn't yet received the OPENACK - look for
  7309. + the connected service */
  7310. + if (service)
  7311. + unlock_service(service);
  7312. + service = get_connected_service(state,
  7313. + remoteport);
  7314. + if (service)
  7315. + vchiq_log_warning(vchiq_core_log_level,
  7316. + "%d: prs %s@%x (%d->%d) - "
  7317. + "found connected service %d",
  7318. + state->id, msg_type_str(type),
  7319. + (unsigned int)header,
  7320. + remoteport, localport,
  7321. + service->localport);
  7322. + }
  7323. +
  7324. + if (!service) {
  7325. + vchiq_log_error(vchiq_core_log_level,
  7326. + "%d: prs %s@%x (%d->%d) - "
  7327. + "invalid/closed service %d",
  7328. + state->id, msg_type_str(type),
  7329. + (unsigned int)header,
  7330. + remoteport, localport, localport);
  7331. + goto skip_message;
  7332. + }
  7333. + break;
  7334. + default:
  7335. + break;
  7336. + }
  7337. +
  7338. + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
  7339. + int svc_fourcc;
  7340. +
  7341. + svc_fourcc = service
  7342. + ? service->base.fourcc
  7343. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7344. + vchiq_log_info(SRVTRACE_LEVEL(service),
  7345. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  7346. + "len:%d",
  7347. + msg_type_str(type), type,
  7348. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7349. + remoteport, localport, size);
  7350. + if (size > 0)
  7351. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7352. + min(64, size));
  7353. + }
  7354. +
  7355. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  7356. + > VCHIQ_SLOT_SIZE) {
  7357. + vchiq_log_error(vchiq_core_log_level,
  7358. + "header %x (msgid %x) - size %x too big for "
  7359. + "slot",
  7360. + (unsigned int)header, (unsigned int)msgid,
  7361. + (unsigned int)size);
  7362. + WARN(1, "oversized for slot\n");
  7363. + }
  7364. +
  7365. + switch (type) {
  7366. + case VCHIQ_MSG_OPEN:
  7367. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  7368. + if (!parse_open(state, header))
  7369. + goto bail_not_ready;
  7370. + break;
  7371. + case VCHIQ_MSG_OPENACK:
  7372. + if (size >= sizeof(struct vchiq_openack_payload)) {
  7373. + const struct vchiq_openack_payload *payload =
  7374. + (struct vchiq_openack_payload *)
  7375. + header->data;
  7376. + service->peer_version = payload->version;
  7377. + }
  7378. + vchiq_log_info(vchiq_core_log_level,
  7379. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  7380. + state->id, (unsigned int)header, size,
  7381. + remoteport, localport, service->peer_version);
  7382. + if (service->srvstate ==
  7383. + VCHIQ_SRVSTATE_OPENING) {
  7384. + service->remoteport = remoteport;
  7385. + vchiq_set_service_state(service,
  7386. + VCHIQ_SRVSTATE_OPEN);
  7387. + up(&service->remove_event);
  7388. + } else
  7389. + vchiq_log_error(vchiq_core_log_level,
  7390. + "OPENACK received in state %s",
  7391. + srvstate_names[service->srvstate]);
  7392. + break;
  7393. + case VCHIQ_MSG_CLOSE:
  7394. + WARN_ON(size != 0); /* There should be no data */
  7395. +
  7396. + vchiq_log_info(vchiq_core_log_level,
  7397. + "%d: prs CLOSE@%x (%d->%d)",
  7398. + state->id, (unsigned int)header,
  7399. + remoteport, localport);
  7400. +
  7401. + mark_service_closing_internal(service, 1);
  7402. +
  7403. + if (vchiq_close_service_internal(service,
  7404. + 1/*close_recvd*/) == VCHIQ_RETRY)
  7405. + goto bail_not_ready;
  7406. +
  7407. + vchiq_log_info(vchiq_core_log_level,
  7408. + "Close Service %c%c%c%c s:%u d:%d",
  7409. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  7410. + service->localport,
  7411. + service->remoteport);
  7412. + break;
  7413. + case VCHIQ_MSG_DATA:
  7414. + vchiq_log_trace(vchiq_core_log_level,
  7415. + "%d: prs DATA@%x,%x (%d->%d)",
  7416. + state->id, (unsigned int)header, size,
  7417. + remoteport, localport);
  7418. +
  7419. + if ((service->remoteport == remoteport)
  7420. + && (service->srvstate ==
  7421. + VCHIQ_SRVSTATE_OPEN)) {
  7422. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  7423. + claim_slot(state->rx_info);
  7424. + DEBUG_TRACE(PARSE_LINE);
  7425. + if (make_service_callback(service,
  7426. + VCHIQ_MESSAGE_AVAILABLE, header,
  7427. + NULL) == VCHIQ_RETRY) {
  7428. + DEBUG_TRACE(PARSE_LINE);
  7429. + goto bail_not_ready;
  7430. + }
  7431. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  7432. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  7433. + size);
  7434. + } else {
  7435. + VCHIQ_STATS_INC(state, error_count);
  7436. + }
  7437. + break;
  7438. + case VCHIQ_MSG_CONNECT:
  7439. + vchiq_log_info(vchiq_core_log_level,
  7440. + "%d: prs CONNECT@%x",
  7441. + state->id, (unsigned int)header);
  7442. + up(&state->connect);
  7443. + break;
  7444. + case VCHIQ_MSG_BULK_RX:
  7445. + case VCHIQ_MSG_BULK_TX: {
  7446. + VCHIQ_BULK_QUEUE_T *queue;
  7447. + WARN_ON(!state->is_master);
  7448. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  7449. + &service->bulk_tx : &service->bulk_rx;
  7450. + if ((service->remoteport == remoteport)
  7451. + && (service->srvstate ==
  7452. + VCHIQ_SRVSTATE_OPEN)) {
  7453. + VCHIQ_BULK_T *bulk;
  7454. + int resolved = 0;
  7455. +
  7456. + DEBUG_TRACE(PARSE_LINE);
  7457. + if (mutex_lock_interruptible(
  7458. + &service->bulk_mutex) != 0) {
  7459. + DEBUG_TRACE(PARSE_LINE);
  7460. + goto bail_not_ready;
  7461. + }
  7462. +
  7463. + WARN_ON(!(queue->remote_insert < queue->remove +
  7464. + VCHIQ_NUM_SERVICE_BULKS));
  7465. + bulk = &queue->bulks[
  7466. + BULK_INDEX(queue->remote_insert)];
  7467. + bulk->remote_data =
  7468. + (void *)((int *)header->data)[0];
  7469. + bulk->remote_size = ((int *)header->data)[1];
  7470. + wmb();
  7471. +
  7472. + vchiq_log_info(vchiq_core_log_level,
  7473. + "%d: prs %s@%x (%d->%d) %x@%x",
  7474. + state->id, msg_type_str(type),
  7475. + (unsigned int)header,
  7476. + remoteport, localport,
  7477. + bulk->remote_size,
  7478. + (unsigned int)bulk->remote_data);
  7479. +
  7480. + queue->remote_insert++;
  7481. +
  7482. + if (atomic_read(&pause_bulks_count)) {
  7483. + state->deferred_bulks++;
  7484. + vchiq_log_info(vchiq_core_log_level,
  7485. + "%s: deferring bulk (%d)",
  7486. + __func__,
  7487. + state->deferred_bulks);
  7488. + if (state->conn_state !=
  7489. + VCHIQ_CONNSTATE_PAUSE_SENT)
  7490. + vchiq_log_error(
  7491. + vchiq_core_log_level,
  7492. + "%s: bulks paused in "
  7493. + "unexpected state %s",
  7494. + __func__,
  7495. + conn_state_names[
  7496. + state->conn_state]);
  7497. + } else if (state->conn_state ==
  7498. + VCHIQ_CONNSTATE_CONNECTED) {
  7499. + DEBUG_TRACE(PARSE_LINE);
  7500. + resolved = resolve_bulks(service,
  7501. + queue);
  7502. + }
  7503. +
  7504. + mutex_unlock(&service->bulk_mutex);
  7505. + if (resolved)
  7506. + notify_bulks(service, queue,
  7507. + 1/*retry_poll*/);
  7508. + }
  7509. + } break;
  7510. + case VCHIQ_MSG_BULK_RX_DONE:
  7511. + case VCHIQ_MSG_BULK_TX_DONE:
  7512. + WARN_ON(state->is_master);
  7513. + if ((service->remoteport == remoteport)
  7514. + && (service->srvstate !=
  7515. + VCHIQ_SRVSTATE_FREE)) {
  7516. + VCHIQ_BULK_QUEUE_T *queue;
  7517. + VCHIQ_BULK_T *bulk;
  7518. +
  7519. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7520. + &service->bulk_rx : &service->bulk_tx;
  7521. +
  7522. + DEBUG_TRACE(PARSE_LINE);
  7523. + if (mutex_lock_interruptible(
  7524. + &service->bulk_mutex) != 0) {
  7525. + DEBUG_TRACE(PARSE_LINE);
  7526. + goto bail_not_ready;
  7527. + }
  7528. + if ((int)(queue->remote_insert -
  7529. + queue->local_insert) >= 0) {
  7530. + vchiq_log_error(vchiq_core_log_level,
  7531. + "%d: prs %s@%x (%d->%d) "
  7532. + "unexpected (ri=%d,li=%d)",
  7533. + state->id, msg_type_str(type),
  7534. + (unsigned int)header,
  7535. + remoteport, localport,
  7536. + queue->remote_insert,
  7537. + queue->local_insert);
  7538. + mutex_unlock(&service->bulk_mutex);
  7539. + break;
  7540. + }
  7541. +
  7542. + BUG_ON(queue->process == queue->local_insert);
  7543. + BUG_ON(queue->process != queue->remote_insert);
  7544. +
  7545. + bulk = &queue->bulks[
  7546. + BULK_INDEX(queue->remote_insert)];
  7547. + bulk->actual = *(int *)header->data;
  7548. + queue->remote_insert++;
  7549. +
  7550. + vchiq_log_info(vchiq_core_log_level,
  7551. + "%d: prs %s@%x (%d->%d) %x@%x",
  7552. + state->id, msg_type_str(type),
  7553. + (unsigned int)header,
  7554. + remoteport, localport,
  7555. + bulk->actual, (unsigned int)bulk->data);
  7556. +
  7557. + vchiq_log_trace(vchiq_core_log_level,
  7558. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  7559. + state->id, localport,
  7560. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  7561. + 'r' : 't',
  7562. + queue->local_insert,
  7563. + queue->remote_insert, queue->process);
  7564. +
  7565. + DEBUG_TRACE(PARSE_LINE);
  7566. + WARN_ON(queue->process == queue->local_insert);
  7567. + vchiq_complete_bulk(bulk);
  7568. + queue->process++;
  7569. + mutex_unlock(&service->bulk_mutex);
  7570. + DEBUG_TRACE(PARSE_LINE);
  7571. + notify_bulks(service, queue, 1/*retry_poll*/);
  7572. + DEBUG_TRACE(PARSE_LINE);
  7573. + }
  7574. + break;
  7575. + case VCHIQ_MSG_PADDING:
  7576. + vchiq_log_trace(vchiq_core_log_level,
  7577. + "%d: prs PADDING@%x,%x",
  7578. + state->id, (unsigned int)header, size);
  7579. + break;
  7580. + case VCHIQ_MSG_PAUSE:
  7581. + /* If initiated, signal the application thread */
  7582. + vchiq_log_trace(vchiq_core_log_level,
  7583. + "%d: prs PAUSE@%x,%x",
  7584. + state->id, (unsigned int)header, size);
  7585. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  7586. + vchiq_log_error(vchiq_core_log_level,
  7587. + "%d: PAUSE received in state PAUSED",
  7588. + state->id);
  7589. + break;
  7590. + }
  7591. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  7592. + /* Send a PAUSE in response */
  7593. + if (queue_message(state, NULL,
  7594. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7595. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  7596. + goto bail_not_ready;
  7597. + if (state->is_master)
  7598. + pause_bulks(state);
  7599. + }
  7600. + /* At this point slot_mutex is held */
  7601. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  7602. + vchiq_platform_paused(state);
  7603. + break;
  7604. + case VCHIQ_MSG_RESUME:
  7605. + vchiq_log_trace(vchiq_core_log_level,
  7606. + "%d: prs RESUME@%x,%x",
  7607. + state->id, (unsigned int)header, size);
  7608. + /* Release the slot mutex */
  7609. + mutex_unlock(&state->slot_mutex);
  7610. + if (state->is_master)
  7611. + resume_bulks(state);
  7612. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  7613. + vchiq_platform_resumed(state);
  7614. + break;
  7615. +
  7616. + case VCHIQ_MSG_REMOTE_USE:
  7617. + vchiq_on_remote_use(state);
  7618. + break;
  7619. + case VCHIQ_MSG_REMOTE_RELEASE:
  7620. + vchiq_on_remote_release(state);
  7621. + break;
  7622. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  7623. + vchiq_on_remote_use_active(state);
  7624. + break;
  7625. +
  7626. + default:
  7627. + vchiq_log_error(vchiq_core_log_level,
  7628. + "%d: prs invalid msgid %x@%x,%x",
  7629. + state->id, msgid, (unsigned int)header, size);
  7630. + WARN(1, "invalid message\n");
  7631. + break;
  7632. + }
  7633. +
  7634. +skip_message:
  7635. + if (service) {
  7636. + unlock_service(service);
  7637. + service = NULL;
  7638. + }
  7639. +
  7640. + state->rx_pos += calc_stride(size);
  7641. +
  7642. + DEBUG_TRACE(PARSE_LINE);
  7643. + /* Perform some housekeeping when the end of the slot is
  7644. + ** reached. */
  7645. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  7646. + /* Remove the extra reference count. */
  7647. + release_slot(state, state->rx_info, NULL, NULL);
  7648. + state->rx_data = NULL;
  7649. + }
  7650. + }
  7651. +
  7652. +bail_not_ready:
  7653. + if (service)
  7654. + unlock_service(service);
  7655. +}
  7656. +
  7657. +/* Called by the slot handler thread */
  7658. +static int
  7659. +slot_handler_func(void *v)
  7660. +{
  7661. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7662. + VCHIQ_SHARED_STATE_T *local = state->local;
  7663. + DEBUG_INITIALISE(local)
  7664. +
  7665. + while (1) {
  7666. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  7667. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7668. + remote_event_wait(&local->trigger);
  7669. +
  7670. + rmb();
  7671. +
  7672. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7673. + if (state->poll_needed) {
  7674. + /* Check if we need to suspend - may change our
  7675. + * conn_state */
  7676. + vchiq_platform_check_suspend(state);
  7677. +
  7678. + state->poll_needed = 0;
  7679. +
  7680. + /* Handle service polling and other rare conditions here
  7681. + ** out of the mainline code */
  7682. + switch (state->conn_state) {
  7683. + case VCHIQ_CONNSTATE_CONNECTED:
  7684. + /* Poll the services as requested */
  7685. + poll_services(state);
  7686. + break;
  7687. +
  7688. + case VCHIQ_CONNSTATE_PAUSING:
  7689. + if (state->is_master)
  7690. + pause_bulks(state);
  7691. + if (queue_message(state, NULL,
  7692. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  7693. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  7694. + vchiq_set_conn_state(state,
  7695. + VCHIQ_CONNSTATE_PAUSE_SENT);
  7696. + } else {
  7697. + if (state->is_master)
  7698. + resume_bulks(state);
  7699. + /* Retry later */
  7700. + state->poll_needed = 1;
  7701. + }
  7702. + break;
  7703. +
  7704. + case VCHIQ_CONNSTATE_PAUSED:
  7705. + vchiq_platform_resume(state);
  7706. + break;
  7707. +
  7708. + case VCHIQ_CONNSTATE_RESUMING:
  7709. + if (queue_message(state, NULL,
  7710. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  7711. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  7712. + if (state->is_master)
  7713. + resume_bulks(state);
  7714. + vchiq_set_conn_state(state,
  7715. + VCHIQ_CONNSTATE_CONNECTED);
  7716. + vchiq_platform_resumed(state);
  7717. + } else {
  7718. + /* This should really be impossible,
  7719. + ** since the PAUSE should have flushed
  7720. + ** through outstanding messages. */
  7721. + vchiq_log_error(vchiq_core_log_level,
  7722. + "Failed to send RESUME "
  7723. + "message");
  7724. + BUG();
  7725. + }
  7726. + break;
  7727. +
  7728. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  7729. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  7730. + vchiq_platform_handle_timeout(state);
  7731. + break;
  7732. + default:
  7733. + break;
  7734. + }
  7735. +
  7736. +
  7737. + }
  7738. +
  7739. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  7740. + parse_rx_slots(state);
  7741. + }
  7742. + return 0;
  7743. +}
  7744. +
  7745. +
  7746. +/* Called by the recycle thread */
  7747. +static int
  7748. +recycle_func(void *v)
  7749. +{
  7750. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7751. + VCHIQ_SHARED_STATE_T *local = state->local;
  7752. +
  7753. + while (1) {
  7754. + remote_event_wait(&local->recycle);
  7755. +
  7756. + process_free_queue(state);
  7757. + }
  7758. + return 0;
  7759. +}
  7760. +
  7761. +
  7762. +/* Called by the sync thread */
  7763. +static int
  7764. +sync_func(void *v)
  7765. +{
  7766. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  7767. + VCHIQ_SHARED_STATE_T *local = state->local;
  7768. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  7769. + state->remote->slot_sync);
  7770. +
  7771. + while (1) {
  7772. + VCHIQ_SERVICE_T *service;
  7773. + int msgid, size;
  7774. + int type;
  7775. + unsigned int localport, remoteport;
  7776. +
  7777. + remote_event_wait(&local->sync_trigger);
  7778. +
  7779. + rmb();
  7780. +
  7781. + msgid = header->msgid;
  7782. + size = header->size;
  7783. + type = VCHIQ_MSG_TYPE(msgid);
  7784. + localport = VCHIQ_MSG_DSTPORT(msgid);
  7785. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  7786. +
  7787. + service = find_service_by_port(state, localport);
  7788. +
  7789. + if (!service) {
  7790. + vchiq_log_error(vchiq_sync_log_level,
  7791. + "%d: sf %s@%x (%d->%d) - "
  7792. + "invalid/closed service %d",
  7793. + state->id, msg_type_str(type),
  7794. + (unsigned int)header,
  7795. + remoteport, localport, localport);
  7796. + release_message_sync(state, header);
  7797. + continue;
  7798. + }
  7799. +
  7800. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  7801. + int svc_fourcc;
  7802. +
  7803. + svc_fourcc = service
  7804. + ? service->base.fourcc
  7805. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  7806. + vchiq_log_trace(vchiq_sync_log_level,
  7807. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  7808. + msg_type_str(type),
  7809. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  7810. + remoteport, localport, size);
  7811. + if (size > 0)
  7812. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  7813. + min(64, size));
  7814. + }
  7815. +
  7816. + switch (type) {
  7817. + case VCHIQ_MSG_OPENACK:
  7818. + if (size >= sizeof(struct vchiq_openack_payload)) {
  7819. + const struct vchiq_openack_payload *payload =
  7820. + (struct vchiq_openack_payload *)
  7821. + header->data;
  7822. + service->peer_version = payload->version;
  7823. + }
  7824. + vchiq_log_info(vchiq_sync_log_level,
  7825. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  7826. + state->id, (unsigned int)header, size,
  7827. + remoteport, localport, service->peer_version);
  7828. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  7829. + service->remoteport = remoteport;
  7830. + vchiq_set_service_state(service,
  7831. + VCHIQ_SRVSTATE_OPENSYNC);
  7832. + up(&service->remove_event);
  7833. + }
  7834. + release_message_sync(state, header);
  7835. + break;
  7836. +
  7837. + case VCHIQ_MSG_DATA:
  7838. + vchiq_log_trace(vchiq_sync_log_level,
  7839. + "%d: sf DATA@%x,%x (%d->%d)",
  7840. + state->id, (unsigned int)header, size,
  7841. + remoteport, localport);
  7842. +
  7843. + if ((service->remoteport == remoteport) &&
  7844. + (service->srvstate ==
  7845. + VCHIQ_SRVSTATE_OPENSYNC)) {
  7846. + if (make_service_callback(service,
  7847. + VCHIQ_MESSAGE_AVAILABLE, header,
  7848. + NULL) == VCHIQ_RETRY)
  7849. + vchiq_log_error(vchiq_sync_log_level,
  7850. + "synchronous callback to "
  7851. + "service %d returns "
  7852. + "VCHIQ_RETRY",
  7853. + localport);
  7854. + }
  7855. + break;
  7856. +
  7857. + default:
  7858. + vchiq_log_error(vchiq_sync_log_level,
  7859. + "%d: sf unexpected msgid %x@%x,%x",
  7860. + state->id, msgid, (unsigned int)header, size);
  7861. + release_message_sync(state, header);
  7862. + break;
  7863. + }
  7864. +
  7865. + unlock_service(service);
  7866. + }
  7867. +
  7868. + return 0;
  7869. +}
  7870. +
  7871. +
  7872. +static void
  7873. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  7874. +{
  7875. + queue->local_insert = 0;
  7876. + queue->remote_insert = 0;
  7877. + queue->process = 0;
  7878. + queue->remote_notify = 0;
  7879. + queue->remove = 0;
  7880. +}
  7881. +
  7882. +
  7883. +inline const char *
  7884. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  7885. +{
  7886. + return conn_state_names[conn_state];
  7887. +}
  7888. +
  7889. +
  7890. +VCHIQ_SLOT_ZERO_T *
  7891. +vchiq_init_slots(void *mem_base, int mem_size)
  7892. +{
  7893. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  7894. + VCHIQ_SLOT_ZERO_T *slot_zero =
  7895. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  7896. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  7897. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  7898. +
  7899. + /* Ensure there is enough memory to run an absolutely minimum system */
  7900. + num_slots -= first_data_slot;
  7901. +
  7902. + if (num_slots < 4) {
  7903. + vchiq_log_error(vchiq_core_log_level,
  7904. + "vchiq_init_slots - insufficient memory %x bytes",
  7905. + mem_size);
  7906. + return NULL;
  7907. + }
  7908. +
  7909. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  7910. +
  7911. + slot_zero->magic = VCHIQ_MAGIC;
  7912. + slot_zero->version = VCHIQ_VERSION;
  7913. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  7914. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  7915. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  7916. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  7917. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  7918. +
  7919. + slot_zero->master.slot_sync = first_data_slot;
  7920. + slot_zero->master.slot_first = first_data_slot + 1;
  7921. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  7922. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  7923. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  7924. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  7925. +
  7926. + return slot_zero;
  7927. +}
  7928. +
  7929. +VCHIQ_STATUS_T
  7930. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  7931. + int is_master)
  7932. +{
  7933. + VCHIQ_SHARED_STATE_T *local;
  7934. + VCHIQ_SHARED_STATE_T *remote;
  7935. + VCHIQ_STATUS_T status;
  7936. + char threadname[10];
  7937. + static int id;
  7938. + int i;
  7939. +
  7940. + vchiq_log_warning(vchiq_core_log_level,
  7941. + "%s: slot_zero = 0x%08lx, is_master = %d",
  7942. + __func__, (unsigned long)slot_zero, is_master);
  7943. +
  7944. + /* Check the input configuration */
  7945. +
  7946. + if (slot_zero->magic != VCHIQ_MAGIC) {
  7947. + vchiq_loud_error_header();
  7948. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  7949. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  7950. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  7951. + vchiq_loud_error_footer();
  7952. + return VCHIQ_ERROR;
  7953. + }
  7954. +
  7955. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  7956. + vchiq_loud_error_header();
  7957. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  7958. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  7959. + "(minimum %d)",
  7960. + (unsigned int)slot_zero, slot_zero->version,
  7961. + VCHIQ_VERSION_MIN);
  7962. + vchiq_loud_error("Restart with a newer VideoCore image.");
  7963. + vchiq_loud_error_footer();
  7964. + return VCHIQ_ERROR;
  7965. + }
  7966. +
  7967. + if (VCHIQ_VERSION < slot_zero->version_min) {
  7968. + vchiq_loud_error_header();
  7969. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  7970. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  7971. + "minimum %d)",
  7972. + (unsigned int)slot_zero, VCHIQ_VERSION,
  7973. + slot_zero->version_min);
  7974. + vchiq_loud_error("Restart with a newer kernel.");
  7975. + vchiq_loud_error_footer();
  7976. + return VCHIQ_ERROR;
  7977. + }
  7978. +
  7979. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  7980. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  7981. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  7982. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  7983. + vchiq_loud_error_header();
  7984. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  7985. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  7986. + "(expected %x)",
  7987. + (unsigned int)slot_zero,
  7988. + slot_zero->slot_zero_size,
  7989. + sizeof(VCHIQ_SLOT_ZERO_T));
  7990. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  7991. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  7992. + "(expected %d",
  7993. + (unsigned int)slot_zero, slot_zero->slot_size,
  7994. + VCHIQ_SLOT_SIZE);
  7995. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  7996. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  7997. + "(expected %d)",
  7998. + (unsigned int)slot_zero, slot_zero->max_slots,
  7999. + VCHIQ_MAX_SLOTS);
  8000. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  8001. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  8002. + "(expected %d)",
  8003. + (unsigned int)slot_zero,
  8004. + slot_zero->max_slots_per_side,
  8005. + VCHIQ_MAX_SLOTS_PER_SIDE);
  8006. + vchiq_loud_error_footer();
  8007. + return VCHIQ_ERROR;
  8008. + }
  8009. +
  8010. + if (is_master) {
  8011. + local = &slot_zero->master;
  8012. + remote = &slot_zero->slave;
  8013. + } else {
  8014. + local = &slot_zero->slave;
  8015. + remote = &slot_zero->master;
  8016. + }
  8017. +
  8018. + if (local->initialised) {
  8019. + vchiq_loud_error_header();
  8020. + if (remote->initialised)
  8021. + vchiq_loud_error("local state has already been "
  8022. + "initialised");
  8023. + else
  8024. + vchiq_loud_error("master/slave mismatch - two %ss",
  8025. + is_master ? "master" : "slave");
  8026. + vchiq_loud_error_footer();
  8027. + return VCHIQ_ERROR;
  8028. + }
  8029. +
  8030. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  8031. +
  8032. + state->id = id++;
  8033. + state->is_master = is_master;
  8034. +
  8035. + /*
  8036. + initialize shared state pointers
  8037. + */
  8038. +
  8039. + state->local = local;
  8040. + state->remote = remote;
  8041. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  8042. +
  8043. + /*
  8044. + initialize events and mutexes
  8045. + */
  8046. +
  8047. + sema_init(&state->connect, 0);
  8048. + mutex_init(&state->mutex);
  8049. + sema_init(&state->trigger_event, 0);
  8050. + sema_init(&state->recycle_event, 0);
  8051. + sema_init(&state->sync_trigger_event, 0);
  8052. + sema_init(&state->sync_release_event, 0);
  8053. +
  8054. + mutex_init(&state->slot_mutex);
  8055. + mutex_init(&state->recycle_mutex);
  8056. + mutex_init(&state->sync_mutex);
  8057. + mutex_init(&state->bulk_transfer_mutex);
  8058. +
  8059. + sema_init(&state->slot_available_event, 0);
  8060. + sema_init(&state->slot_remove_event, 0);
  8061. + sema_init(&state->data_quota_event, 0);
  8062. +
  8063. + state->slot_queue_available = 0;
  8064. +
  8065. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  8066. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8067. + &state->service_quotas[i];
  8068. + sema_init(&service_quota->quota_event, 0);
  8069. + }
  8070. +
  8071. + for (i = local->slot_first; i <= local->slot_last; i++) {
  8072. + local->slot_queue[state->slot_queue_available++] = i;
  8073. + up(&state->slot_available_event);
  8074. + }
  8075. +
  8076. + state->default_slot_quota = state->slot_queue_available/2;
  8077. + state->default_message_quota =
  8078. + min((unsigned short)(state->default_slot_quota * 256),
  8079. + (unsigned short)~0);
  8080. +
  8081. + state->previous_data_index = -1;
  8082. + state->data_use_count = 0;
  8083. + state->data_quota = state->slot_queue_available - 1;
  8084. +
  8085. + local->trigger.event = &state->trigger_event;
  8086. + remote_event_create(&local->trigger);
  8087. + local->tx_pos = 0;
  8088. +
  8089. + local->recycle.event = &state->recycle_event;
  8090. + remote_event_create(&local->recycle);
  8091. + local->slot_queue_recycle = state->slot_queue_available;
  8092. +
  8093. + local->sync_trigger.event = &state->sync_trigger_event;
  8094. + remote_event_create(&local->sync_trigger);
  8095. +
  8096. + local->sync_release.event = &state->sync_release_event;
  8097. + remote_event_create(&local->sync_release);
  8098. +
  8099. + /* At start-of-day, the slot is empty and available */
  8100. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  8101. + = VCHIQ_MSGID_PADDING;
  8102. + remote_event_signal_local(&local->sync_release);
  8103. +
  8104. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  8105. +
  8106. + status = vchiq_platform_init_state(state);
  8107. +
  8108. + /*
  8109. + bring up slot handler thread
  8110. + */
  8111. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  8112. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  8113. + (void *)state,
  8114. + threadname);
  8115. +
  8116. + if (state->slot_handler_thread == NULL) {
  8117. + vchiq_loud_error_header();
  8118. + vchiq_loud_error("couldn't create thread %s", threadname);
  8119. + vchiq_loud_error_footer();
  8120. + return VCHIQ_ERROR;
  8121. + }
  8122. + set_user_nice(state->slot_handler_thread, -19);
  8123. + wake_up_process(state->slot_handler_thread);
  8124. +
  8125. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  8126. + state->recycle_thread = kthread_create(&recycle_func,
  8127. + (void *)state,
  8128. + threadname);
  8129. + if (state->recycle_thread == NULL) {
  8130. + vchiq_loud_error_header();
  8131. + vchiq_loud_error("couldn't create thread %s", threadname);
  8132. + vchiq_loud_error_footer();
  8133. + return VCHIQ_ERROR;
  8134. + }
  8135. + set_user_nice(state->recycle_thread, -19);
  8136. + wake_up_process(state->recycle_thread);
  8137. +
  8138. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  8139. + state->sync_thread = kthread_create(&sync_func,
  8140. + (void *)state,
  8141. + threadname);
  8142. + if (state->sync_thread == NULL) {
  8143. + vchiq_loud_error_header();
  8144. + vchiq_loud_error("couldn't create thread %s", threadname);
  8145. + vchiq_loud_error_footer();
  8146. + return VCHIQ_ERROR;
  8147. + }
  8148. + set_user_nice(state->sync_thread, -20);
  8149. + wake_up_process(state->sync_thread);
  8150. +
  8151. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  8152. + vchiq_states[state->id] = state;
  8153. +
  8154. + /* Indicate readiness to the other side */
  8155. + local->initialised = 1;
  8156. +
  8157. + return status;
  8158. +}
  8159. +
  8160. +/* Called from application thread when a client or server service is created. */
  8161. +VCHIQ_SERVICE_T *
  8162. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  8163. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  8164. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  8165. +{
  8166. + VCHIQ_SERVICE_T *service;
  8167. +
  8168. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  8169. + if (service) {
  8170. + service->base.fourcc = params->fourcc;
  8171. + service->base.callback = params->callback;
  8172. + service->base.userdata = params->userdata;
  8173. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  8174. + service->ref_count = 1;
  8175. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  8176. + service->userdata_term = userdata_term;
  8177. + service->localport = VCHIQ_PORT_FREE;
  8178. + service->remoteport = VCHIQ_PORT_FREE;
  8179. +
  8180. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  8181. + VCHIQ_FOURCC_INVALID : params->fourcc;
  8182. + service->client_id = 0;
  8183. + service->auto_close = 1;
  8184. + service->sync = 0;
  8185. + service->closing = 0;
  8186. + service->trace = 0;
  8187. + atomic_set(&service->poll_flags, 0);
  8188. + service->version = params->version;
  8189. + service->version_min = params->version_min;
  8190. + service->state = state;
  8191. + service->instance = instance;
  8192. + service->service_use_count = 0;
  8193. + init_bulk_queue(&service->bulk_tx);
  8194. + init_bulk_queue(&service->bulk_rx);
  8195. + sema_init(&service->remove_event, 0);
  8196. + sema_init(&service->bulk_remove_event, 0);
  8197. + mutex_init(&service->bulk_mutex);
  8198. + memset(&service->stats, 0, sizeof(service->stats));
  8199. + } else {
  8200. + vchiq_log_error(vchiq_core_log_level,
  8201. + "Out of memory");
  8202. + }
  8203. +
  8204. + if (service) {
  8205. + VCHIQ_SERVICE_T **pservice = NULL;
  8206. + int i;
  8207. +
  8208. + /* Although it is perfectly possible to use service_spinlock
  8209. + ** to protect the creation of services, it is overkill as it
  8210. + ** disables interrupts while the array is searched.
  8211. + ** The only danger is of another thread trying to create a
  8212. + ** service - service deletion is safe.
  8213. + ** Therefore it is preferable to use state->mutex which,
  8214. + ** although slower to claim, doesn't block interrupts while
  8215. + ** it is held.
  8216. + */
  8217. +
  8218. + mutex_lock(&state->mutex);
  8219. +
  8220. + /* Prepare to use a previously unused service */
  8221. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  8222. + pservice = &state->services[state->unused_service];
  8223. +
  8224. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  8225. + for (i = 0; i < state->unused_service; i++) {
  8226. + VCHIQ_SERVICE_T *srv = state->services[i];
  8227. + if (!srv) {
  8228. + pservice = &state->services[i];
  8229. + break;
  8230. + }
  8231. + }
  8232. + } else {
  8233. + for (i = (state->unused_service - 1); i >= 0; i--) {
  8234. + VCHIQ_SERVICE_T *srv = state->services[i];
  8235. + if (!srv)
  8236. + pservice = &state->services[i];
  8237. + else if ((srv->public_fourcc == params->fourcc)
  8238. + && ((srv->instance != instance) ||
  8239. + (srv->base.callback !=
  8240. + params->callback))) {
  8241. + /* There is another server using this
  8242. + ** fourcc which doesn't match. */
  8243. + pservice = NULL;
  8244. + break;
  8245. + }
  8246. + }
  8247. + }
  8248. +
  8249. + if (pservice) {
  8250. + service->localport = (pservice - state->services);
  8251. + if (!handle_seq)
  8252. + handle_seq = VCHIQ_MAX_STATES *
  8253. + VCHIQ_MAX_SERVICES;
  8254. + service->handle = handle_seq |
  8255. + (state->id * VCHIQ_MAX_SERVICES) |
  8256. + service->localport;
  8257. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  8258. + *pservice = service;
  8259. + if (pservice == &state->services[state->unused_service])
  8260. + state->unused_service++;
  8261. + }
  8262. +
  8263. + mutex_unlock(&state->mutex);
  8264. +
  8265. + if (!pservice) {
  8266. + kfree(service);
  8267. + service = NULL;
  8268. + }
  8269. + }
  8270. +
  8271. + if (service) {
  8272. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  8273. + &state->service_quotas[service->localport];
  8274. + service_quota->slot_quota = state->default_slot_quota;
  8275. + service_quota->message_quota = state->default_message_quota;
  8276. + if (service_quota->slot_use_count == 0)
  8277. + service_quota->previous_tx_index =
  8278. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  8279. + - 1;
  8280. +
  8281. + /* Bring this service online */
  8282. + vchiq_set_service_state(service, srvstate);
  8283. +
  8284. + vchiq_log_info(vchiq_core_msg_log_level,
  8285. + "%s Service %c%c%c%c SrcPort:%d",
  8286. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  8287. + ? "Open" : "Add",
  8288. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  8289. + service->localport);
  8290. + }
  8291. +
  8292. + /* Don't unlock the service - leave it with a ref_count of 1. */
  8293. +
  8294. + return service;
  8295. +}
  8296. +
  8297. +VCHIQ_STATUS_T
  8298. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  8299. +{
  8300. + struct vchiq_open_payload payload = {
  8301. + service->base.fourcc,
  8302. + client_id,
  8303. + service->version,
  8304. + service->version_min
  8305. + };
  8306. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  8307. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8308. +
  8309. + service->client_id = client_id;
  8310. + vchiq_use_service_internal(service);
  8311. + status = queue_message(service->state, NULL,
  8312. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  8313. + &body, 1, sizeof(payload), 1);
  8314. + if (status == VCHIQ_SUCCESS) {
  8315. + /* Wait for the ACK/NAK */
  8316. + if (down_interruptible(&service->remove_event) != 0) {
  8317. + status = VCHIQ_RETRY;
  8318. + vchiq_release_service_internal(service);
  8319. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  8320. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  8321. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  8322. + vchiq_log_error(vchiq_core_log_level,
  8323. + "%d: osi - srvstate = %s (ref %d)",
  8324. + service->state->id,
  8325. + srvstate_names[service->srvstate],
  8326. + service->ref_count);
  8327. + status = VCHIQ_ERROR;
  8328. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  8329. + vchiq_release_service_internal(service);
  8330. + }
  8331. + }
  8332. + return status;
  8333. +}
  8334. +
  8335. +static void
  8336. +release_service_messages(VCHIQ_SERVICE_T *service)
  8337. +{
  8338. + VCHIQ_STATE_T *state = service->state;
  8339. + int slot_last = state->remote->slot_last;
  8340. + int i;
  8341. +
  8342. + /* Release any claimed messages */
  8343. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  8344. + VCHIQ_SLOT_INFO_T *slot_info =
  8345. + SLOT_INFO_FROM_INDEX(state, i);
  8346. + if (slot_info->release_count != slot_info->use_count) {
  8347. + char *data =
  8348. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  8349. + unsigned int pos, end;
  8350. +
  8351. + end = VCHIQ_SLOT_SIZE;
  8352. + if (data == state->rx_data)
  8353. + /* This buffer is still being read from - stop
  8354. + ** at the current read position */
  8355. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  8356. +
  8357. + pos = 0;
  8358. +
  8359. + while (pos < end) {
  8360. + VCHIQ_HEADER_T *header =
  8361. + (VCHIQ_HEADER_T *)(data + pos);
  8362. + int msgid = header->msgid;
  8363. + int port = VCHIQ_MSG_DSTPORT(msgid);
  8364. + if ((port == service->localport) &&
  8365. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  8366. + vchiq_log_info(vchiq_core_log_level,
  8367. + " fsi - hdr %x",
  8368. + (unsigned int)header);
  8369. + release_slot(state, slot_info, header,
  8370. + NULL);
  8371. + }
  8372. + pos += calc_stride(header->size);
  8373. + if (pos > VCHIQ_SLOT_SIZE) {
  8374. + vchiq_log_error(vchiq_core_log_level,
  8375. + "fsi - pos %x: header %x, "
  8376. + "msgid %x, header->msgid %x, "
  8377. + "header->size %x",
  8378. + pos, (unsigned int)header,
  8379. + msgid, header->msgid,
  8380. + header->size);
  8381. + WARN(1, "invalid slot position\n");
  8382. + }
  8383. + }
  8384. + }
  8385. + }
  8386. +}
  8387. +
  8388. +static int
  8389. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  8390. +{
  8391. + VCHIQ_STATUS_T status;
  8392. +
  8393. + /* Abort any outstanding bulk transfers */
  8394. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  8395. + return 0;
  8396. + abort_outstanding_bulks(service, &service->bulk_tx);
  8397. + abort_outstanding_bulks(service, &service->bulk_rx);
  8398. + mutex_unlock(&service->bulk_mutex);
  8399. +
  8400. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  8401. + if (status == VCHIQ_SUCCESS)
  8402. + status = notify_bulks(service, &service->bulk_rx,
  8403. + 0/*!retry_poll*/);
  8404. + return (status == VCHIQ_SUCCESS);
  8405. +}
  8406. +
  8407. +static VCHIQ_STATUS_T
  8408. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  8409. +{
  8410. + VCHIQ_STATUS_T status;
  8411. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8412. + int newstate;
  8413. +
  8414. + switch (service->srvstate) {
  8415. + case VCHIQ_SRVSTATE_OPEN:
  8416. + case VCHIQ_SRVSTATE_CLOSESENT:
  8417. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8418. + if (is_server) {
  8419. + if (service->auto_close) {
  8420. + service->client_id = 0;
  8421. + service->remoteport = VCHIQ_PORT_FREE;
  8422. + newstate = VCHIQ_SRVSTATE_LISTENING;
  8423. + } else
  8424. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  8425. + } else
  8426. + newstate = VCHIQ_SRVSTATE_CLOSED;
  8427. + vchiq_set_service_state(service, newstate);
  8428. + break;
  8429. + case VCHIQ_SRVSTATE_LISTENING:
  8430. + break;
  8431. + default:
  8432. + vchiq_log_error(vchiq_core_log_level,
  8433. + "close_service_complete(%x) called in state %s",
  8434. + service->handle, srvstate_names[service->srvstate]);
  8435. + WARN(1, "close_service_complete in unexpected state\n");
  8436. + return VCHIQ_ERROR;
  8437. + }
  8438. +
  8439. + status = make_service_callback(service,
  8440. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  8441. +
  8442. + if (status != VCHIQ_RETRY) {
  8443. + int uc = service->service_use_count;
  8444. + int i;
  8445. + /* Complete the close process */
  8446. + for (i = 0; i < uc; i++)
  8447. + /* cater for cases where close is forced and the
  8448. + ** client may not close all it's handles */
  8449. + vchiq_release_service_internal(service);
  8450. +
  8451. + service->client_id = 0;
  8452. + service->remoteport = VCHIQ_PORT_FREE;
  8453. +
  8454. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  8455. + vchiq_free_service_internal(service);
  8456. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  8457. + if (is_server)
  8458. + service->closing = 0;
  8459. +
  8460. + up(&service->remove_event);
  8461. + }
  8462. + } else
  8463. + vchiq_set_service_state(service, failstate);
  8464. +
  8465. + return status;
  8466. +}
  8467. +
  8468. +/* Called by the slot handler */
  8469. +VCHIQ_STATUS_T
  8470. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  8471. +{
  8472. + VCHIQ_STATE_T *state = service->state;
  8473. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8474. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  8475. +
  8476. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  8477. + service->state->id, service->localport, close_recvd,
  8478. + srvstate_names[service->srvstate]);
  8479. +
  8480. + switch (service->srvstate) {
  8481. + case VCHIQ_SRVSTATE_CLOSED:
  8482. + case VCHIQ_SRVSTATE_HIDDEN:
  8483. + case VCHIQ_SRVSTATE_LISTENING:
  8484. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8485. + if (close_recvd)
  8486. + vchiq_log_error(vchiq_core_log_level,
  8487. + "vchiq_close_service_internal(1) called "
  8488. + "in state %s",
  8489. + srvstate_names[service->srvstate]);
  8490. + else if (is_server) {
  8491. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  8492. + status = VCHIQ_ERROR;
  8493. + } else {
  8494. + service->client_id = 0;
  8495. + service->remoteport = VCHIQ_PORT_FREE;
  8496. + if (service->srvstate ==
  8497. + VCHIQ_SRVSTATE_CLOSEWAIT)
  8498. + vchiq_set_service_state(service,
  8499. + VCHIQ_SRVSTATE_LISTENING);
  8500. + }
  8501. + up(&service->remove_event);
  8502. + } else
  8503. + vchiq_free_service_internal(service);
  8504. + break;
  8505. + case VCHIQ_SRVSTATE_OPENING:
  8506. + if (close_recvd) {
  8507. + /* The open was rejected - tell the user */
  8508. + vchiq_set_service_state(service,
  8509. + VCHIQ_SRVSTATE_CLOSEWAIT);
  8510. + up(&service->remove_event);
  8511. + } else {
  8512. + /* Shutdown mid-open - let the other side know */
  8513. + status = queue_message(state, service,
  8514. + VCHIQ_MAKE_MSG
  8515. + (VCHIQ_MSG_CLOSE,
  8516. + service->localport,
  8517. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8518. + NULL, 0, 0, 0);
  8519. + }
  8520. + break;
  8521. +
  8522. + case VCHIQ_SRVSTATE_OPENSYNC:
  8523. + mutex_lock(&state->sync_mutex);
  8524. + /* Drop through */
  8525. +
  8526. + case VCHIQ_SRVSTATE_OPEN:
  8527. + if (state->is_master || close_recvd) {
  8528. + if (!do_abort_bulks(service))
  8529. + status = VCHIQ_RETRY;
  8530. + }
  8531. +
  8532. + release_service_messages(service);
  8533. +
  8534. + if (status == VCHIQ_SUCCESS)
  8535. + status = queue_message(state, service,
  8536. + VCHIQ_MAKE_MSG
  8537. + (VCHIQ_MSG_CLOSE,
  8538. + service->localport,
  8539. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  8540. + NULL, 0, 0, 0);
  8541. +
  8542. + if (status == VCHIQ_SUCCESS) {
  8543. + if (!close_recvd)
  8544. + break;
  8545. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  8546. + mutex_unlock(&state->sync_mutex);
  8547. + break;
  8548. + } else
  8549. + break;
  8550. +
  8551. + status = close_service_complete(service,
  8552. + VCHIQ_SRVSTATE_CLOSERECVD);
  8553. + break;
  8554. +
  8555. + case VCHIQ_SRVSTATE_CLOSESENT:
  8556. + if (!close_recvd)
  8557. + /* This happens when a process is killed mid-close */
  8558. + break;
  8559. +
  8560. + if (!state->is_master) {
  8561. + if (!do_abort_bulks(service)) {
  8562. + status = VCHIQ_RETRY;
  8563. + break;
  8564. + }
  8565. + }
  8566. +
  8567. + if (status == VCHIQ_SUCCESS)
  8568. + status = close_service_complete(service,
  8569. + VCHIQ_SRVSTATE_CLOSERECVD);
  8570. + break;
  8571. +
  8572. + case VCHIQ_SRVSTATE_CLOSERECVD:
  8573. + if (!close_recvd && is_server)
  8574. + /* Force into LISTENING mode */
  8575. + vchiq_set_service_state(service,
  8576. + VCHIQ_SRVSTATE_LISTENING);
  8577. + status = close_service_complete(service,
  8578. + VCHIQ_SRVSTATE_CLOSERECVD);
  8579. + break;
  8580. +
  8581. + default:
  8582. + vchiq_log_error(vchiq_core_log_level,
  8583. + "vchiq_close_service_internal(%d) called in state %s",
  8584. + close_recvd, srvstate_names[service->srvstate]);
  8585. + break;
  8586. + }
  8587. +
  8588. + return status;
  8589. +}
  8590. +
  8591. +/* Called from the application process upon process death */
  8592. +void
  8593. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  8594. +{
  8595. + VCHIQ_STATE_T *state = service->state;
  8596. +
  8597. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  8598. + state->id, service->localport, service->remoteport);
  8599. +
  8600. + mark_service_closing(service);
  8601. +
  8602. + /* Mark the service for removal by the slot handler */
  8603. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  8604. +}
  8605. +
  8606. +/* Called from the slot handler */
  8607. +void
  8608. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  8609. +{
  8610. + VCHIQ_STATE_T *state = service->state;
  8611. +
  8612. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  8613. + state->id, service->localport);
  8614. +
  8615. + switch (service->srvstate) {
  8616. + case VCHIQ_SRVSTATE_OPENING:
  8617. + case VCHIQ_SRVSTATE_CLOSED:
  8618. + case VCHIQ_SRVSTATE_HIDDEN:
  8619. + case VCHIQ_SRVSTATE_LISTENING:
  8620. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  8621. + break;
  8622. + default:
  8623. + vchiq_log_error(vchiq_core_log_level,
  8624. + "%d: fsi - (%d) in state %s",
  8625. + state->id, service->localport,
  8626. + srvstate_names[service->srvstate]);
  8627. + return;
  8628. + }
  8629. +
  8630. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  8631. +
  8632. + up(&service->remove_event);
  8633. +
  8634. + /* Release the initial lock */
  8635. + unlock_service(service);
  8636. +}
  8637. +
  8638. +VCHIQ_STATUS_T
  8639. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8640. +{
  8641. + VCHIQ_SERVICE_T *service;
  8642. + int i;
  8643. +
  8644. + /* Find all services registered to this client and enable them. */
  8645. + i = 0;
  8646. + while ((service = next_service_by_instance(state, instance,
  8647. + &i)) != NULL) {
  8648. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  8649. + vchiq_set_service_state(service,
  8650. + VCHIQ_SRVSTATE_LISTENING);
  8651. + unlock_service(service);
  8652. + }
  8653. +
  8654. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  8655. + if (queue_message(state, NULL,
  8656. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  8657. + 0, 1) == VCHIQ_RETRY)
  8658. + return VCHIQ_RETRY;
  8659. +
  8660. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  8661. + }
  8662. +
  8663. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  8664. + if (down_interruptible(&state->connect) != 0)
  8665. + return VCHIQ_RETRY;
  8666. +
  8667. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  8668. + up(&state->connect);
  8669. + }
  8670. +
  8671. + return VCHIQ_SUCCESS;
  8672. +}
  8673. +
  8674. +VCHIQ_STATUS_T
  8675. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  8676. +{
  8677. + VCHIQ_SERVICE_T *service;
  8678. + int i;
  8679. +
  8680. + /* Find all services registered to this client and enable them. */
  8681. + i = 0;
  8682. + while ((service = next_service_by_instance(state, instance,
  8683. + &i)) != NULL) {
  8684. + (void)vchiq_remove_service(service->handle);
  8685. + unlock_service(service);
  8686. + }
  8687. +
  8688. + return VCHIQ_SUCCESS;
  8689. +}
  8690. +
  8691. +VCHIQ_STATUS_T
  8692. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  8693. +{
  8694. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8695. +
  8696. + switch (state->conn_state) {
  8697. + case VCHIQ_CONNSTATE_CONNECTED:
  8698. + /* Request a pause */
  8699. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  8700. + request_poll(state, NULL, 0);
  8701. + break;
  8702. + default:
  8703. + vchiq_log_error(vchiq_core_log_level,
  8704. + "vchiq_pause_internal in state %s\n",
  8705. + conn_state_names[state->conn_state]);
  8706. + status = VCHIQ_ERROR;
  8707. + VCHIQ_STATS_INC(state, error_count);
  8708. + break;
  8709. + }
  8710. +
  8711. + return status;
  8712. +}
  8713. +
  8714. +VCHIQ_STATUS_T
  8715. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  8716. +{
  8717. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8718. +
  8719. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  8720. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  8721. + request_poll(state, NULL, 0);
  8722. + } else {
  8723. + status = VCHIQ_ERROR;
  8724. + VCHIQ_STATS_INC(state, error_count);
  8725. + }
  8726. +
  8727. + return status;
  8728. +}
  8729. +
  8730. +VCHIQ_STATUS_T
  8731. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  8732. +{
  8733. + /* Unregister the service */
  8734. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  8735. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8736. +
  8737. + if (!service)
  8738. + return VCHIQ_ERROR;
  8739. +
  8740. + vchiq_log_info(vchiq_core_log_level,
  8741. + "%d: close_service:%d",
  8742. + service->state->id, service->localport);
  8743. +
  8744. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8745. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8746. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  8747. + unlock_service(service);
  8748. + return VCHIQ_ERROR;
  8749. + }
  8750. +
  8751. + mark_service_closing(service);
  8752. +
  8753. + if (current == service->state->slot_handler_thread) {
  8754. + status = vchiq_close_service_internal(service,
  8755. + 0/*!close_recvd*/);
  8756. + BUG_ON(status == VCHIQ_RETRY);
  8757. + } else {
  8758. + /* Mark the service for termination by the slot handler */
  8759. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  8760. + }
  8761. +
  8762. + while (1) {
  8763. + if (down_interruptible(&service->remove_event) != 0) {
  8764. + status = VCHIQ_RETRY;
  8765. + break;
  8766. + }
  8767. +
  8768. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8769. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  8770. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  8771. + break;
  8772. +
  8773. + vchiq_log_warning(vchiq_core_log_level,
  8774. + "%d: close_service:%d - waiting in state %s",
  8775. + service->state->id, service->localport,
  8776. + srvstate_names[service->srvstate]);
  8777. + }
  8778. +
  8779. + if ((status == VCHIQ_SUCCESS) &&
  8780. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  8781. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  8782. + status = VCHIQ_ERROR;
  8783. +
  8784. + unlock_service(service);
  8785. +
  8786. + return status;
  8787. +}
  8788. +
  8789. +VCHIQ_STATUS_T
  8790. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  8791. +{
  8792. + /* Unregister the service */
  8793. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  8794. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  8795. +
  8796. + if (!service)
  8797. + return VCHIQ_ERROR;
  8798. +
  8799. + vchiq_log_info(vchiq_core_log_level,
  8800. + "%d: remove_service:%d",
  8801. + service->state->id, service->localport);
  8802. +
  8803. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  8804. + unlock_service(service);
  8805. + return VCHIQ_ERROR;
  8806. + }
  8807. +
  8808. + mark_service_closing(service);
  8809. +
  8810. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  8811. + (current == service->state->slot_handler_thread)) {
  8812. + /* Make it look like a client, because it must be removed and
  8813. + not left in the LISTENING state. */
  8814. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  8815. +
  8816. + status = vchiq_close_service_internal(service,
  8817. + 0/*!close_recvd*/);
  8818. + BUG_ON(status == VCHIQ_RETRY);
  8819. + } else {
  8820. + /* Mark the service for removal by the slot handler */
  8821. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  8822. + }
  8823. + while (1) {
  8824. + if (down_interruptible(&service->remove_event) != 0) {
  8825. + status = VCHIQ_RETRY;
  8826. + break;
  8827. + }
  8828. +
  8829. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  8830. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  8831. + break;
  8832. +
  8833. + vchiq_log_warning(vchiq_core_log_level,
  8834. + "%d: remove_service:%d - waiting in state %s",
  8835. + service->state->id, service->localport,
  8836. + srvstate_names[service->srvstate]);
  8837. + }
  8838. +
  8839. + if ((status == VCHIQ_SUCCESS) &&
  8840. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  8841. + status = VCHIQ_ERROR;
  8842. +
  8843. + unlock_service(service);
  8844. +
  8845. + return status;
  8846. +}
  8847. +
  8848. +
  8849. +/* This function may be called by kernel threads or user threads.
  8850. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  8851. + * received and the call should be retried after being returned to user
  8852. + * context.
  8853. + * When called in blocking mode, the userdata field points to a bulk_waiter
  8854. + * structure.
  8855. + */
  8856. +VCHIQ_STATUS_T
  8857. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  8858. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  8859. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  8860. +{
  8861. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  8862. + VCHIQ_BULK_QUEUE_T *queue;
  8863. + VCHIQ_BULK_T *bulk;
  8864. + VCHIQ_STATE_T *state;
  8865. + struct bulk_waiter *bulk_waiter = NULL;
  8866. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  8867. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  8868. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  8869. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  8870. +
  8871. + if (!service ||
  8872. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  8873. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  8874. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  8875. + goto error_exit;
  8876. +
  8877. + switch (mode) {
  8878. + case VCHIQ_BULK_MODE_NOCALLBACK:
  8879. + case VCHIQ_BULK_MODE_CALLBACK:
  8880. + break;
  8881. + case VCHIQ_BULK_MODE_BLOCKING:
  8882. + bulk_waiter = (struct bulk_waiter *)userdata;
  8883. + sema_init(&bulk_waiter->event, 0);
  8884. + bulk_waiter->actual = 0;
  8885. + bulk_waiter->bulk = NULL;
  8886. + break;
  8887. + case VCHIQ_BULK_MODE_WAITING:
  8888. + bulk_waiter = (struct bulk_waiter *)userdata;
  8889. + bulk = bulk_waiter->bulk;
  8890. + goto waiting;
  8891. + default:
  8892. + goto error_exit;
  8893. + }
  8894. +
  8895. + state = service->state;
  8896. +
  8897. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  8898. + &service->bulk_tx : &service->bulk_rx;
  8899. +
  8900. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  8901. + status = VCHIQ_RETRY;
  8902. + goto error_exit;
  8903. + }
  8904. +
  8905. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  8906. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  8907. + do {
  8908. + mutex_unlock(&service->bulk_mutex);
  8909. + if (down_interruptible(&service->bulk_remove_event)
  8910. + != 0) {
  8911. + status = VCHIQ_RETRY;
  8912. + goto error_exit;
  8913. + }
  8914. + if (mutex_lock_interruptible(&service->bulk_mutex)
  8915. + != 0) {
  8916. + status = VCHIQ_RETRY;
  8917. + goto error_exit;
  8918. + }
  8919. + } while (queue->local_insert == queue->remove +
  8920. + VCHIQ_NUM_SERVICE_BULKS);
  8921. + }
  8922. +
  8923. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  8924. +
  8925. + bulk->mode = mode;
  8926. + bulk->dir = dir;
  8927. + bulk->userdata = userdata;
  8928. + bulk->size = size;
  8929. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  8930. +
  8931. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  8932. + VCHIQ_SUCCESS)
  8933. + goto unlock_error_exit;
  8934. +
  8935. + wmb();
  8936. +
  8937. + vchiq_log_info(vchiq_core_log_level,
  8938. + "%d: bt (%d->%d) %cx %x@%x %x",
  8939. + state->id,
  8940. + service->localport, service->remoteport, dir_char,
  8941. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  8942. +
  8943. + if (state->is_master) {
  8944. + queue->local_insert++;
  8945. + if (resolve_bulks(service, queue))
  8946. + request_poll(state, service,
  8947. + (dir == VCHIQ_BULK_TRANSMIT) ?
  8948. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  8949. + } else {
  8950. + int payload[2] = { (int)bulk->data, bulk->size };
  8951. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  8952. +
  8953. + status = queue_message(state, NULL,
  8954. + VCHIQ_MAKE_MSG(dir_msgtype,
  8955. + service->localport, service->remoteport),
  8956. + &element, 1, sizeof(payload), 1);
  8957. + if (status != VCHIQ_SUCCESS) {
  8958. + vchiq_complete_bulk(bulk);
  8959. + goto unlock_error_exit;
  8960. + }
  8961. + queue->local_insert++;
  8962. + }
  8963. +
  8964. + mutex_unlock(&service->bulk_mutex);
  8965. +
  8966. + vchiq_log_trace(vchiq_core_log_level,
  8967. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  8968. + state->id,
  8969. + service->localport, dir_char,
  8970. + queue->local_insert, queue->remote_insert, queue->process);
  8971. +
  8972. +waiting:
  8973. + unlock_service(service);
  8974. +
  8975. + status = VCHIQ_SUCCESS;
  8976. +
  8977. + if (bulk_waiter) {
  8978. + bulk_waiter->bulk = bulk;
  8979. + if (down_interruptible(&bulk_waiter->event) != 0)
  8980. + status = VCHIQ_RETRY;
  8981. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  8982. + status = VCHIQ_ERROR;
  8983. + }
  8984. +
  8985. + return status;
  8986. +
  8987. +unlock_error_exit:
  8988. + mutex_unlock(&service->bulk_mutex);
  8989. +
  8990. +error_exit:
  8991. + if (service)
  8992. + unlock_service(service);
  8993. + return status;
  8994. +}
  8995. +
  8996. +VCHIQ_STATUS_T
  8997. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  8998. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  8999. +{
  9000. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9001. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9002. +
  9003. + unsigned int size = 0;
  9004. + unsigned int i;
  9005. +
  9006. + if (!service ||
  9007. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  9008. + goto error_exit;
  9009. +
  9010. + for (i = 0; i < (unsigned int)count; i++) {
  9011. + if (elements[i].size) {
  9012. + if (elements[i].data == NULL) {
  9013. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9014. + goto error_exit;
  9015. + }
  9016. + size += elements[i].size;
  9017. + }
  9018. + }
  9019. +
  9020. + if (size > VCHIQ_MAX_MSG_SIZE) {
  9021. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  9022. + goto error_exit;
  9023. + }
  9024. +
  9025. + switch (service->srvstate) {
  9026. + case VCHIQ_SRVSTATE_OPEN:
  9027. + status = queue_message(service->state, service,
  9028. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9029. + service->localport,
  9030. + service->remoteport),
  9031. + elements, count, size, 1);
  9032. + break;
  9033. + case VCHIQ_SRVSTATE_OPENSYNC:
  9034. + status = queue_message_sync(service->state, service,
  9035. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  9036. + service->localport,
  9037. + service->remoteport),
  9038. + elements, count, size, 1);
  9039. + break;
  9040. + default:
  9041. + status = VCHIQ_ERROR;
  9042. + break;
  9043. + }
  9044. +
  9045. +error_exit:
  9046. + if (service)
  9047. + unlock_service(service);
  9048. +
  9049. + return status;
  9050. +}
  9051. +
  9052. +void
  9053. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  9054. +{
  9055. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9056. + VCHIQ_SHARED_STATE_T *remote;
  9057. + VCHIQ_STATE_T *state;
  9058. + int slot_index;
  9059. +
  9060. + if (!service)
  9061. + return;
  9062. +
  9063. + state = service->state;
  9064. + remote = state->remote;
  9065. +
  9066. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  9067. +
  9068. + if ((slot_index >= remote->slot_first) &&
  9069. + (slot_index <= remote->slot_last)) {
  9070. + int msgid = header->msgid;
  9071. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  9072. + VCHIQ_SLOT_INFO_T *slot_info =
  9073. + SLOT_INFO_FROM_INDEX(state, slot_index);
  9074. +
  9075. + release_slot(state, slot_info, header, service);
  9076. + }
  9077. + } else if (slot_index == remote->slot_sync)
  9078. + release_message_sync(state, header);
  9079. +
  9080. + unlock_service(service);
  9081. +}
  9082. +
  9083. +static void
  9084. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  9085. +{
  9086. + header->msgid = VCHIQ_MSGID_PADDING;
  9087. + wmb();
  9088. + remote_event_signal(&state->remote->sync_release);
  9089. +}
  9090. +
  9091. +VCHIQ_STATUS_T
  9092. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  9093. +{
  9094. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9095. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9096. +
  9097. + if (!service ||
  9098. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  9099. + !peer_version)
  9100. + goto exit;
  9101. + *peer_version = service->peer_version;
  9102. + status = VCHIQ_SUCCESS;
  9103. +
  9104. +exit:
  9105. + if (service)
  9106. + unlock_service(service);
  9107. + return status;
  9108. +}
  9109. +
  9110. +VCHIQ_STATUS_T
  9111. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  9112. + int config_size, VCHIQ_CONFIG_T *pconfig)
  9113. +{
  9114. + VCHIQ_CONFIG_T config;
  9115. +
  9116. + (void)instance;
  9117. +
  9118. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  9119. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  9120. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  9121. + config.max_services = VCHIQ_MAX_SERVICES;
  9122. + config.version = VCHIQ_VERSION;
  9123. + config.version_min = VCHIQ_VERSION_MIN;
  9124. +
  9125. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  9126. + return VCHIQ_ERROR;
  9127. +
  9128. + memcpy(pconfig, &config,
  9129. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  9130. +
  9131. + return VCHIQ_SUCCESS;
  9132. +}
  9133. +
  9134. +VCHIQ_STATUS_T
  9135. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  9136. + VCHIQ_SERVICE_OPTION_T option, int value)
  9137. +{
  9138. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  9139. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  9140. +
  9141. + if (service) {
  9142. + switch (option) {
  9143. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  9144. + service->auto_close = value;
  9145. + status = VCHIQ_SUCCESS;
  9146. + break;
  9147. +
  9148. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  9149. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9150. + &service->state->service_quotas[
  9151. + service->localport];
  9152. + if (value == 0)
  9153. + value = service->state->default_slot_quota;
  9154. + if ((value >= service_quota->slot_use_count) &&
  9155. + (value < (unsigned short)~0)) {
  9156. + service_quota->slot_quota = value;
  9157. + if ((value >= service_quota->slot_use_count) &&
  9158. + (service_quota->message_quota >=
  9159. + service_quota->message_use_count)) {
  9160. + /* Signal the service that it may have
  9161. + ** dropped below its quota */
  9162. + up(&service_quota->quota_event);
  9163. + }
  9164. + status = VCHIQ_SUCCESS;
  9165. + }
  9166. + } break;
  9167. +
  9168. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  9169. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9170. + &service->state->service_quotas[
  9171. + service->localport];
  9172. + if (value == 0)
  9173. + value = service->state->default_message_quota;
  9174. + if ((value >= service_quota->message_use_count) &&
  9175. + (value < (unsigned short)~0)) {
  9176. + service_quota->message_quota = value;
  9177. + if ((value >=
  9178. + service_quota->message_use_count) &&
  9179. + (service_quota->slot_quota >=
  9180. + service_quota->slot_use_count))
  9181. + /* Signal the service that it may have
  9182. + ** dropped below its quota */
  9183. + up(&service_quota->quota_event);
  9184. + status = VCHIQ_SUCCESS;
  9185. + }
  9186. + } break;
  9187. +
  9188. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  9189. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  9190. + (service->srvstate ==
  9191. + VCHIQ_SRVSTATE_LISTENING)) {
  9192. + service->sync = value;
  9193. + status = VCHIQ_SUCCESS;
  9194. + }
  9195. + break;
  9196. +
  9197. + case VCHIQ_SERVICE_OPTION_TRACE:
  9198. + service->trace = value;
  9199. + status = VCHIQ_SUCCESS;
  9200. + break;
  9201. +
  9202. + default:
  9203. + break;
  9204. + }
  9205. + unlock_service(service);
  9206. + }
  9207. +
  9208. + return status;
  9209. +}
  9210. +
  9211. +void
  9212. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  9213. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  9214. +{
  9215. + static const char *const debug_names[] = {
  9216. + "<entries>",
  9217. + "SLOT_HANDLER_COUNT",
  9218. + "SLOT_HANDLER_LINE",
  9219. + "PARSE_LINE",
  9220. + "PARSE_HEADER",
  9221. + "PARSE_MSGID",
  9222. + "AWAIT_COMPLETION_LINE",
  9223. + "DEQUEUE_MESSAGE_LINE",
  9224. + "SERVICE_CALLBACK_LINE",
  9225. + "MSG_QUEUE_FULL_COUNT",
  9226. + "COMPLETION_QUEUE_FULL_COUNT"
  9227. + };
  9228. + int i;
  9229. +
  9230. + char buf[80];
  9231. + int len;
  9232. + len = snprintf(buf, sizeof(buf),
  9233. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  9234. + label, shared->slot_first, shared->slot_last,
  9235. + shared->tx_pos, shared->slot_queue_recycle);
  9236. + vchiq_dump(dump_context, buf, len + 1);
  9237. +
  9238. + len = snprintf(buf, sizeof(buf),
  9239. + " Slots claimed:");
  9240. + vchiq_dump(dump_context, buf, len + 1);
  9241. +
  9242. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  9243. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  9244. + if (slot_info.use_count != slot_info.release_count) {
  9245. + len = snprintf(buf, sizeof(buf),
  9246. + " %d: %d/%d", i, slot_info.use_count,
  9247. + slot_info.release_count);
  9248. + vchiq_dump(dump_context, buf, len + 1);
  9249. + }
  9250. + }
  9251. +
  9252. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  9253. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  9254. + debug_names[i], shared->debug[i], shared->debug[i]);
  9255. + vchiq_dump(dump_context, buf, len + 1);
  9256. + }
  9257. +}
  9258. +
  9259. +void
  9260. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  9261. +{
  9262. + char buf[80];
  9263. + int len;
  9264. + int i;
  9265. +
  9266. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  9267. + conn_state_names[state->conn_state]);
  9268. + vchiq_dump(dump_context, buf, len + 1);
  9269. +
  9270. + len = snprintf(buf, sizeof(buf),
  9271. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  9272. + state->local->tx_pos,
  9273. + (uint32_t)state->tx_data +
  9274. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  9275. + state->rx_pos,
  9276. + (uint32_t)state->rx_data +
  9277. + (state->rx_pos & VCHIQ_SLOT_MASK));
  9278. + vchiq_dump(dump_context, buf, len + 1);
  9279. +
  9280. + len = snprintf(buf, sizeof(buf),
  9281. + " Version: %d (min %d)",
  9282. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  9283. + vchiq_dump(dump_context, buf, len + 1);
  9284. +
  9285. + if (VCHIQ_ENABLE_STATS) {
  9286. + len = snprintf(buf, sizeof(buf),
  9287. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  9288. + "error_count=%d",
  9289. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  9290. + state->stats.error_count);
  9291. + vchiq_dump(dump_context, buf, len + 1);
  9292. + }
  9293. +
  9294. + len = snprintf(buf, sizeof(buf),
  9295. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  9296. + "(%d data)",
  9297. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  9298. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  9299. + state->data_quota - state->data_use_count,
  9300. + state->local->slot_queue_recycle - state->slot_queue_available,
  9301. + state->stats.slot_stalls, state->stats.data_stalls);
  9302. + vchiq_dump(dump_context, buf, len + 1);
  9303. +
  9304. + vchiq_dump_platform_state(dump_context);
  9305. +
  9306. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  9307. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  9308. +
  9309. + vchiq_dump_platform_instances(dump_context);
  9310. +
  9311. + for (i = 0; i < state->unused_service; i++) {
  9312. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  9313. +
  9314. + if (service) {
  9315. + vchiq_dump_service_state(dump_context, service);
  9316. + unlock_service(service);
  9317. + }
  9318. + }
  9319. +}
  9320. +
  9321. +void
  9322. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  9323. +{
  9324. + char buf[80];
  9325. + int len;
  9326. +
  9327. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  9328. + service->localport, srvstate_names[service->srvstate],
  9329. + service->ref_count - 1); /*Don't include the lock just taken*/
  9330. +
  9331. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  9332. + char remoteport[30];
  9333. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  9334. + &service->state->service_quotas[service->localport];
  9335. + int fourcc = service->base.fourcc;
  9336. + int tx_pending, rx_pending;
  9337. + if (service->remoteport != VCHIQ_PORT_FREE) {
  9338. + int len2 = snprintf(remoteport, sizeof(remoteport),
  9339. + "%d", service->remoteport);
  9340. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  9341. + snprintf(remoteport + len2,
  9342. + sizeof(remoteport) - len2,
  9343. + " (client %x)", service->client_id);
  9344. + } else
  9345. + strcpy(remoteport, "n/a");
  9346. +
  9347. + len += snprintf(buf + len, sizeof(buf) - len,
  9348. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  9349. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  9350. + remoteport,
  9351. + service_quota->message_use_count,
  9352. + service_quota->message_quota,
  9353. + service_quota->slot_use_count,
  9354. + service_quota->slot_quota);
  9355. +
  9356. + vchiq_dump(dump_context, buf, len + 1);
  9357. +
  9358. + tx_pending = service->bulk_tx.local_insert -
  9359. + service->bulk_tx.remote_insert;
  9360. +
  9361. + rx_pending = service->bulk_rx.local_insert -
  9362. + service->bulk_rx.remote_insert;
  9363. +
  9364. + len = snprintf(buf, sizeof(buf),
  9365. + " Bulk: tx_pending=%d (size %d),"
  9366. + " rx_pending=%d (size %d)",
  9367. + tx_pending,
  9368. + tx_pending ? service->bulk_tx.bulks[
  9369. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  9370. + rx_pending,
  9371. + rx_pending ? service->bulk_rx.bulks[
  9372. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  9373. +
  9374. + if (VCHIQ_ENABLE_STATS) {
  9375. + vchiq_dump(dump_context, buf, len + 1);
  9376. +
  9377. + len = snprintf(buf, sizeof(buf),
  9378. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  9379. + "rx_count=%d, rx_bytes=%llu",
  9380. + service->stats.ctrl_tx_count,
  9381. + service->stats.ctrl_tx_bytes,
  9382. + service->stats.ctrl_rx_count,
  9383. + service->stats.ctrl_rx_bytes);
  9384. + vchiq_dump(dump_context, buf, len + 1);
  9385. +
  9386. + len = snprintf(buf, sizeof(buf),
  9387. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  9388. + "rx_count=%d, rx_bytes=%llu",
  9389. + service->stats.bulk_tx_count,
  9390. + service->stats.bulk_tx_bytes,
  9391. + service->stats.bulk_rx_count,
  9392. + service->stats.bulk_rx_bytes);
  9393. + vchiq_dump(dump_context, buf, len + 1);
  9394. +
  9395. + len = snprintf(buf, sizeof(buf),
  9396. + " %d quota stalls, %d slot stalls, "
  9397. + "%d bulk stalls, %d aborted, %d errors",
  9398. + service->stats.quota_stalls,
  9399. + service->stats.slot_stalls,
  9400. + service->stats.bulk_stalls,
  9401. + service->stats.bulk_aborted_count,
  9402. + service->stats.error_count);
  9403. + }
  9404. + }
  9405. +
  9406. + vchiq_dump(dump_context, buf, len + 1);
  9407. +
  9408. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  9409. + vchiq_dump_platform_service_state(dump_context, service);
  9410. +}
  9411. +
  9412. +
  9413. +void
  9414. +vchiq_loud_error_header(void)
  9415. +{
  9416. + vchiq_log_error(vchiq_core_log_level,
  9417. + "============================================================"
  9418. + "================");
  9419. + vchiq_log_error(vchiq_core_log_level,
  9420. + "============================================================"
  9421. + "================");
  9422. + vchiq_log_error(vchiq_core_log_level, "=====");
  9423. +}
  9424. +
  9425. +void
  9426. +vchiq_loud_error_footer(void)
  9427. +{
  9428. + vchiq_log_error(vchiq_core_log_level, "=====");
  9429. + vchiq_log_error(vchiq_core_log_level,
  9430. + "============================================================"
  9431. + "================");
  9432. + vchiq_log_error(vchiq_core_log_level,
  9433. + "============================================================"
  9434. + "================");
  9435. +}
  9436. +
  9437. +
  9438. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  9439. +{
  9440. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9441. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9442. + status = queue_message(state, NULL,
  9443. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  9444. + NULL, 0, 0, 0);
  9445. + return status;
  9446. +}
  9447. +
  9448. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  9449. +{
  9450. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9451. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9452. + status = queue_message(state, NULL,
  9453. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  9454. + NULL, 0, 0, 0);
  9455. + return status;
  9456. +}
  9457. +
  9458. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  9459. +{
  9460. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  9461. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  9462. + status = queue_message(state, NULL,
  9463. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  9464. + NULL, 0, 0, 0);
  9465. + return status;
  9466. +}
  9467. +
  9468. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  9469. + size_t numBytes)
  9470. +{
  9471. + const uint8_t *mem = (const uint8_t *)voidMem;
  9472. + size_t offset;
  9473. + char lineBuf[100];
  9474. + char *s;
  9475. +
  9476. + while (numBytes > 0) {
  9477. + s = lineBuf;
  9478. +
  9479. + for (offset = 0; offset < 16; offset++) {
  9480. + if (offset < numBytes)
  9481. + s += snprintf(s, 4, "%02x ", mem[offset]);
  9482. + else
  9483. + s += snprintf(s, 4, " ");
  9484. + }
  9485. +
  9486. + for (offset = 0; offset < 16; offset++) {
  9487. + if (offset < numBytes) {
  9488. + uint8_t ch = mem[offset];
  9489. +
  9490. + if ((ch < ' ') || (ch > '~'))
  9491. + ch = '.';
  9492. + *s++ = (char)ch;
  9493. + }
  9494. + }
  9495. + *s++ = '\0';
  9496. +
  9497. + if ((label != NULL) && (*label != '\0'))
  9498. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9499. + "%s: %08x: %s", label, addr, lineBuf);
  9500. + else
  9501. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  9502. + "%08x: %s", addr, lineBuf);
  9503. +
  9504. + addr += 16;
  9505. + mem += 16;
  9506. + if (numBytes > 16)
  9507. + numBytes -= 16;
  9508. + else
  9509. + numBytes = 0;
  9510. + }
  9511. +}
  9512. --- /dev/null
  9513. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  9514. @@ -0,0 +1,711 @@
  9515. +/**
  9516. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  9517. + *
  9518. + * Redistribution and use in source and binary forms, with or without
  9519. + * modification, are permitted provided that the following conditions
  9520. + * are met:
  9521. + * 1. Redistributions of source code must retain the above copyright
  9522. + * notice, this list of conditions, and the following disclaimer,
  9523. + * without modification.
  9524. + * 2. Redistributions in binary form must reproduce the above copyright
  9525. + * notice, this list of conditions and the following disclaimer in the
  9526. + * documentation and/or other materials provided with the distribution.
  9527. + * 3. The names of the above-listed copyright holders may not be used
  9528. + * to endorse or promote products derived from this software without
  9529. + * specific prior written permission.
  9530. + *
  9531. + * ALTERNATIVELY, this software may be distributed under the terms of the
  9532. + * GNU General Public License ("GPL") version 2, as published by the Free
  9533. + * Software Foundation.
  9534. + *
  9535. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  9536. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  9537. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  9538. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  9539. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9540. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9541. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9542. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  9543. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  9544. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  9545. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9546. + */
  9547. +
  9548. +#ifndef VCHIQ_CORE_H
  9549. +#define VCHIQ_CORE_H
  9550. +
  9551. +#include <linux/mutex.h>
  9552. +#include <linux/semaphore.h>
  9553. +#include <linux/kthread.h>
  9554. +
  9555. +#include "vchiq_cfg.h"
  9556. +
  9557. +#include "vchiq.h"
  9558. +
  9559. +/* Run time control of log level, based on KERN_XXX level. */
  9560. +#define VCHIQ_LOG_DEFAULT 4
  9561. +#define VCHIQ_LOG_ERROR 3
  9562. +#define VCHIQ_LOG_WARNING 4
  9563. +#define VCHIQ_LOG_INFO 6
  9564. +#define VCHIQ_LOG_TRACE 7
  9565. +
  9566. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  9567. +
  9568. +#ifndef vchiq_log_error
  9569. +#define vchiq_log_error(cat, fmt, ...) \
  9570. + do { if (cat >= VCHIQ_LOG_ERROR) \
  9571. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9572. +#endif
  9573. +#ifndef vchiq_log_warning
  9574. +#define vchiq_log_warning(cat, fmt, ...) \
  9575. + do { if (cat >= VCHIQ_LOG_WARNING) \
  9576. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9577. +#endif
  9578. +#ifndef vchiq_log_info
  9579. +#define vchiq_log_info(cat, fmt, ...) \
  9580. + do { if (cat >= VCHIQ_LOG_INFO) \
  9581. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9582. +#endif
  9583. +#ifndef vchiq_log_trace
  9584. +#define vchiq_log_trace(cat, fmt, ...) \
  9585. + do { if (cat >= VCHIQ_LOG_TRACE) \
  9586. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  9587. +#endif
  9588. +
  9589. +#define vchiq_loud_error(...) \
  9590. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  9591. +
  9592. +#ifndef vchiq_static_assert
  9593. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  9594. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  9595. +#endif
  9596. +
  9597. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  9598. +
  9599. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  9600. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  9601. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  9602. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  9603. +
  9604. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  9605. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  9606. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  9607. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  9608. +
  9609. +#define VCHIQ_MSG_PADDING 0 /* - */
  9610. +#define VCHIQ_MSG_CONNECT 1 /* - */
  9611. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  9612. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  9613. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  9614. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  9615. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  9616. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  9617. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  9618. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  9619. +#define VCHIQ_MSG_PAUSE 10 /* - */
  9620. +#define VCHIQ_MSG_RESUME 11 /* - */
  9621. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  9622. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  9623. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  9624. +
  9625. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  9626. +#define VCHIQ_PORT_FREE 0x1000
  9627. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  9628. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  9629. + ((type<<24) | (srcport<<12) | (dstport<<0))
  9630. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  9631. +#define VCHIQ_MSG_SRCPORT(msgid) \
  9632. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  9633. +#define VCHIQ_MSG_DSTPORT(msgid) \
  9634. + ((unsigned short)msgid & 0xfff)
  9635. +
  9636. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  9637. + ((fourcc) >> 24) & 0xff, \
  9638. + ((fourcc) >> 16) & 0xff, \
  9639. + ((fourcc) >> 8) & 0xff, \
  9640. + (fourcc) & 0xff
  9641. +
  9642. +/* Ensure the fields are wide enough */
  9643. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  9644. + == 0);
  9645. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  9646. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  9647. + (unsigned int)VCHIQ_PORT_FREE);
  9648. +
  9649. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  9650. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  9651. +
  9652. +#define VCHIQ_FOURCC_INVALID 0x00000000
  9653. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  9654. +
  9655. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  9656. +
  9657. +typedef uint32_t BITSET_T;
  9658. +
  9659. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  9660. +
  9661. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  9662. +#define BITSET_WORD(b) (b >> 5)
  9663. +#define BITSET_BIT(b) (1 << (b & 31))
  9664. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  9665. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  9666. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  9667. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  9668. +
  9669. +#if VCHIQ_ENABLE_STATS
  9670. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  9671. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  9672. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  9673. + (service->stats. stat += addend)
  9674. +#else
  9675. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  9676. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  9677. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  9678. +#endif
  9679. +
  9680. +enum {
  9681. + DEBUG_ENTRIES,
  9682. +#if VCHIQ_ENABLE_DEBUG
  9683. + DEBUG_SLOT_HANDLER_COUNT,
  9684. + DEBUG_SLOT_HANDLER_LINE,
  9685. + DEBUG_PARSE_LINE,
  9686. + DEBUG_PARSE_HEADER,
  9687. + DEBUG_PARSE_MSGID,
  9688. + DEBUG_AWAIT_COMPLETION_LINE,
  9689. + DEBUG_DEQUEUE_MESSAGE_LINE,
  9690. + DEBUG_SERVICE_CALLBACK_LINE,
  9691. + DEBUG_MSG_QUEUE_FULL_COUNT,
  9692. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  9693. +#endif
  9694. + DEBUG_MAX
  9695. +};
  9696. +
  9697. +#if VCHIQ_ENABLE_DEBUG
  9698. +
  9699. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  9700. +#define DEBUG_TRACE(d) \
  9701. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  9702. +#define DEBUG_VALUE(d, v) \
  9703. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  9704. +#define DEBUG_COUNT(d) \
  9705. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  9706. +
  9707. +#else /* VCHIQ_ENABLE_DEBUG */
  9708. +
  9709. +#define DEBUG_INITIALISE(local)
  9710. +#define DEBUG_TRACE(d)
  9711. +#define DEBUG_VALUE(d, v)
  9712. +#define DEBUG_COUNT(d)
  9713. +
  9714. +#endif /* VCHIQ_ENABLE_DEBUG */
  9715. +
  9716. +typedef enum {
  9717. + VCHIQ_CONNSTATE_DISCONNECTED,
  9718. + VCHIQ_CONNSTATE_CONNECTING,
  9719. + VCHIQ_CONNSTATE_CONNECTED,
  9720. + VCHIQ_CONNSTATE_PAUSING,
  9721. + VCHIQ_CONNSTATE_PAUSE_SENT,
  9722. + VCHIQ_CONNSTATE_PAUSED,
  9723. + VCHIQ_CONNSTATE_RESUMING,
  9724. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  9725. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  9726. +} VCHIQ_CONNSTATE_T;
  9727. +
  9728. +enum {
  9729. + VCHIQ_SRVSTATE_FREE,
  9730. + VCHIQ_SRVSTATE_HIDDEN,
  9731. + VCHIQ_SRVSTATE_LISTENING,
  9732. + VCHIQ_SRVSTATE_OPENING,
  9733. + VCHIQ_SRVSTATE_OPEN,
  9734. + VCHIQ_SRVSTATE_OPENSYNC,
  9735. + VCHIQ_SRVSTATE_CLOSESENT,
  9736. + VCHIQ_SRVSTATE_CLOSERECVD,
  9737. + VCHIQ_SRVSTATE_CLOSEWAIT,
  9738. + VCHIQ_SRVSTATE_CLOSED
  9739. +};
  9740. +
  9741. +enum {
  9742. + VCHIQ_POLL_TERMINATE,
  9743. + VCHIQ_POLL_REMOVE,
  9744. + VCHIQ_POLL_TXNOTIFY,
  9745. + VCHIQ_POLL_RXNOTIFY,
  9746. + VCHIQ_POLL_COUNT
  9747. +};
  9748. +
  9749. +typedef enum {
  9750. + VCHIQ_BULK_TRANSMIT,
  9751. + VCHIQ_BULK_RECEIVE
  9752. +} VCHIQ_BULK_DIR_T;
  9753. +
  9754. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  9755. +
  9756. +typedef struct vchiq_bulk_struct {
  9757. + short mode;
  9758. + short dir;
  9759. + void *userdata;
  9760. + VCHI_MEM_HANDLE_T handle;
  9761. + void *data;
  9762. + int size;
  9763. + void *remote_data;
  9764. + int remote_size;
  9765. + int actual;
  9766. +} VCHIQ_BULK_T;
  9767. +
  9768. +typedef struct vchiq_bulk_queue_struct {
  9769. + int local_insert; /* Where to insert the next local bulk */
  9770. + int remote_insert; /* Where to insert the next remote bulk (master) */
  9771. + int process; /* Bulk to transfer next */
  9772. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  9773. + int remove; /* Bulk to notify the local client of, and remove,
  9774. + ** next */
  9775. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  9776. +} VCHIQ_BULK_QUEUE_T;
  9777. +
  9778. +typedef struct remote_event_struct {
  9779. + int armed;
  9780. + int fired;
  9781. + struct semaphore *event;
  9782. +} REMOTE_EVENT_T;
  9783. +
  9784. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  9785. +
  9786. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  9787. +
  9788. +typedef struct vchiq_slot_struct {
  9789. + char data[VCHIQ_SLOT_SIZE];
  9790. +} VCHIQ_SLOT_T;
  9791. +
  9792. +typedef struct vchiq_slot_info_struct {
  9793. + /* Use two counters rather than one to avoid the need for a mutex. */
  9794. + short use_count;
  9795. + short release_count;
  9796. +} VCHIQ_SLOT_INFO_T;
  9797. +
  9798. +typedef struct vchiq_service_struct {
  9799. + VCHIQ_SERVICE_BASE_T base;
  9800. + VCHIQ_SERVICE_HANDLE_T handle;
  9801. + unsigned int ref_count;
  9802. + int srvstate;
  9803. + VCHIQ_USERDATA_TERM_T userdata_term;
  9804. + unsigned int localport;
  9805. + unsigned int remoteport;
  9806. + int public_fourcc;
  9807. + int client_id;
  9808. + char auto_close;
  9809. + char sync;
  9810. + char closing;
  9811. + char trace;
  9812. + atomic_t poll_flags;
  9813. + short version;
  9814. + short version_min;
  9815. + short peer_version;
  9816. +
  9817. + VCHIQ_STATE_T *state;
  9818. + VCHIQ_INSTANCE_T instance;
  9819. +
  9820. + int service_use_count;
  9821. +
  9822. + VCHIQ_BULK_QUEUE_T bulk_tx;
  9823. + VCHIQ_BULK_QUEUE_T bulk_rx;
  9824. +
  9825. + struct semaphore remove_event;
  9826. + struct semaphore bulk_remove_event;
  9827. + struct mutex bulk_mutex;
  9828. +
  9829. + struct service_stats_struct {
  9830. + int quota_stalls;
  9831. + int slot_stalls;
  9832. + int bulk_stalls;
  9833. + int error_count;
  9834. + int ctrl_tx_count;
  9835. + int ctrl_rx_count;
  9836. + int bulk_tx_count;
  9837. + int bulk_rx_count;
  9838. + int bulk_aborted_count;
  9839. + uint64_t ctrl_tx_bytes;
  9840. + uint64_t ctrl_rx_bytes;
  9841. + uint64_t bulk_tx_bytes;
  9842. + uint64_t bulk_rx_bytes;
  9843. + } stats;
  9844. +} VCHIQ_SERVICE_T;
  9845. +
  9846. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  9847. + statically allocated, since for accounting reasons a service's slot
  9848. + usage is carried over between users of the same port number.
  9849. + */
  9850. +typedef struct vchiq_service_quota_struct {
  9851. + unsigned short slot_quota;
  9852. + unsigned short slot_use_count;
  9853. + unsigned short message_quota;
  9854. + unsigned short message_use_count;
  9855. + struct semaphore quota_event;
  9856. + int previous_tx_index;
  9857. +} VCHIQ_SERVICE_QUOTA_T;
  9858. +
  9859. +typedef struct vchiq_shared_state_struct {
  9860. +
  9861. + /* A non-zero value here indicates that the content is valid. */
  9862. + int initialised;
  9863. +
  9864. + /* The first and last (inclusive) slots allocated to the owner. */
  9865. + int slot_first;
  9866. + int slot_last;
  9867. +
  9868. + /* The slot allocated to synchronous messages from the owner. */
  9869. + int slot_sync;
  9870. +
  9871. + /* Signalling this event indicates that owner's slot handler thread
  9872. + ** should run. */
  9873. + REMOTE_EVENT_T trigger;
  9874. +
  9875. + /* Indicates the byte position within the stream where the next message
  9876. + ** will be written. The least significant bits are an index into the
  9877. + ** slot. The next bits are the index of the slot in slot_queue. */
  9878. + int tx_pos;
  9879. +
  9880. + /* This event should be signalled when a slot is recycled. */
  9881. + REMOTE_EVENT_T recycle;
  9882. +
  9883. + /* The slot_queue index where the next recycled slot will be written. */
  9884. + int slot_queue_recycle;
  9885. +
  9886. + /* This event should be signalled when a synchronous message is sent. */
  9887. + REMOTE_EVENT_T sync_trigger;
  9888. +
  9889. + /* This event should be signalled when a synchronous message has been
  9890. + ** released. */
  9891. + REMOTE_EVENT_T sync_release;
  9892. +
  9893. + /* A circular buffer of slot indexes. */
  9894. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  9895. +
  9896. + /* Debugging state */
  9897. + int debug[DEBUG_MAX];
  9898. +} VCHIQ_SHARED_STATE_T;
  9899. +
  9900. +typedef struct vchiq_slot_zero_struct {
  9901. + int magic;
  9902. + short version;
  9903. + short version_min;
  9904. + int slot_zero_size;
  9905. + int slot_size;
  9906. + int max_slots;
  9907. + int max_slots_per_side;
  9908. + int platform_data[2];
  9909. + VCHIQ_SHARED_STATE_T master;
  9910. + VCHIQ_SHARED_STATE_T slave;
  9911. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  9912. +} VCHIQ_SLOT_ZERO_T;
  9913. +
  9914. +struct vchiq_state_struct {
  9915. + int id;
  9916. + int initialised;
  9917. + VCHIQ_CONNSTATE_T conn_state;
  9918. + int is_master;
  9919. +
  9920. + VCHIQ_SHARED_STATE_T *local;
  9921. + VCHIQ_SHARED_STATE_T *remote;
  9922. + VCHIQ_SLOT_T *slot_data;
  9923. +
  9924. + unsigned short default_slot_quota;
  9925. + unsigned short default_message_quota;
  9926. +
  9927. + /* Event indicating connect message received */
  9928. + struct semaphore connect;
  9929. +
  9930. + /* Mutex protecting services */
  9931. + struct mutex mutex;
  9932. + VCHIQ_INSTANCE_T *instance;
  9933. +
  9934. + /* Processes incoming messages */
  9935. + struct task_struct *slot_handler_thread;
  9936. +
  9937. + /* Processes recycled slots */
  9938. + struct task_struct *recycle_thread;
  9939. +
  9940. + /* Processes synchronous messages */
  9941. + struct task_struct *sync_thread;
  9942. +
  9943. + /* Local implementation of the trigger remote event */
  9944. + struct semaphore trigger_event;
  9945. +
  9946. + /* Local implementation of the recycle remote event */
  9947. + struct semaphore recycle_event;
  9948. +
  9949. + /* Local implementation of the sync trigger remote event */
  9950. + struct semaphore sync_trigger_event;
  9951. +
  9952. + /* Local implementation of the sync release remote event */
  9953. + struct semaphore sync_release_event;
  9954. +
  9955. + char *tx_data;
  9956. + char *rx_data;
  9957. + VCHIQ_SLOT_INFO_T *rx_info;
  9958. +
  9959. + struct mutex slot_mutex;
  9960. +
  9961. + struct mutex recycle_mutex;
  9962. +
  9963. + struct mutex sync_mutex;
  9964. +
  9965. + struct mutex bulk_transfer_mutex;
  9966. +
  9967. + /* Indicates the byte position within the stream from where the next
  9968. + ** message will be read. The least significant bits are an index into
  9969. + ** the slot.The next bits are the index of the slot in
  9970. + ** remote->slot_queue. */
  9971. + int rx_pos;
  9972. +
  9973. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  9974. + from remote->tx_pos. */
  9975. + int local_tx_pos;
  9976. +
  9977. + /* The slot_queue index of the slot to become available next. */
  9978. + int slot_queue_available;
  9979. +
  9980. + /* A flag to indicate if any poll has been requested */
  9981. + int poll_needed;
  9982. +
  9983. + /* Ths index of the previous slot used for data messages. */
  9984. + int previous_data_index;
  9985. +
  9986. + /* The number of slots occupied by data messages. */
  9987. + unsigned short data_use_count;
  9988. +
  9989. + /* The maximum number of slots to be occupied by data messages. */
  9990. + unsigned short data_quota;
  9991. +
  9992. + /* An array of bit sets indicating which services must be polled. */
  9993. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  9994. +
  9995. + /* The number of the first unused service */
  9996. + int unused_service;
  9997. +
  9998. + /* Signalled when a free slot becomes available. */
  9999. + struct semaphore slot_available_event;
  10000. +
  10001. + struct semaphore slot_remove_event;
  10002. +
  10003. + /* Signalled when a free data slot becomes available. */
  10004. + struct semaphore data_quota_event;
  10005. +
  10006. + /* Incremented when there are bulk transfers which cannot be processed
  10007. + * whilst paused and must be processed on resume */
  10008. + int deferred_bulks;
  10009. +
  10010. + struct state_stats_struct {
  10011. + int slot_stalls;
  10012. + int data_stalls;
  10013. + int ctrl_tx_count;
  10014. + int ctrl_rx_count;
  10015. + int error_count;
  10016. + } stats;
  10017. +
  10018. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  10019. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  10020. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  10021. +
  10022. + VCHIQ_PLATFORM_STATE_T platform_state;
  10023. +};
  10024. +
  10025. +struct bulk_waiter {
  10026. + VCHIQ_BULK_T *bulk;
  10027. + struct semaphore event;
  10028. + int actual;
  10029. +};
  10030. +
  10031. +extern spinlock_t bulk_waiter_spinlock;
  10032. +
  10033. +extern int vchiq_core_log_level;
  10034. +extern int vchiq_core_msg_log_level;
  10035. +extern int vchiq_sync_log_level;
  10036. +
  10037. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  10038. +
  10039. +extern const char *
  10040. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  10041. +
  10042. +extern VCHIQ_SLOT_ZERO_T *
  10043. +vchiq_init_slots(void *mem_base, int mem_size);
  10044. +
  10045. +extern VCHIQ_STATUS_T
  10046. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  10047. + int is_master);
  10048. +
  10049. +extern VCHIQ_STATUS_T
  10050. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10051. +
  10052. +extern VCHIQ_SERVICE_T *
  10053. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  10054. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  10055. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  10056. +
  10057. +extern VCHIQ_STATUS_T
  10058. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  10059. +
  10060. +extern VCHIQ_STATUS_T
  10061. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  10062. +
  10063. +extern void
  10064. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  10065. +
  10066. +extern void
  10067. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  10068. +
  10069. +extern VCHIQ_STATUS_T
  10070. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  10071. +
  10072. +extern VCHIQ_STATUS_T
  10073. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  10074. +
  10075. +extern VCHIQ_STATUS_T
  10076. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  10077. +
  10078. +extern void
  10079. +remote_event_pollall(VCHIQ_STATE_T *state);
  10080. +
  10081. +extern VCHIQ_STATUS_T
  10082. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  10083. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  10084. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  10085. +
  10086. +extern void
  10087. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  10088. +
  10089. +extern void
  10090. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  10091. +
  10092. +extern void
  10093. +vchiq_loud_error_header(void);
  10094. +
  10095. +extern void
  10096. +vchiq_loud_error_footer(void);
  10097. +
  10098. +extern void
  10099. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  10100. +
  10101. +static inline VCHIQ_SERVICE_T *
  10102. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  10103. +{
  10104. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  10105. + (VCHIQ_MAX_STATES - 1)];
  10106. + if (!state)
  10107. + return NULL;
  10108. +
  10109. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  10110. +}
  10111. +
  10112. +extern VCHIQ_SERVICE_T *
  10113. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  10114. +
  10115. +extern VCHIQ_SERVICE_T *
  10116. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  10117. +
  10118. +extern VCHIQ_SERVICE_T *
  10119. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  10120. + VCHIQ_SERVICE_HANDLE_T handle);
  10121. +
  10122. +extern VCHIQ_SERVICE_T *
  10123. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  10124. + VCHIQ_SERVICE_HANDLE_T handle);
  10125. +
  10126. +extern VCHIQ_SERVICE_T *
  10127. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  10128. + int *pidx);
  10129. +
  10130. +extern void
  10131. +lock_service(VCHIQ_SERVICE_T *service);
  10132. +
  10133. +extern void
  10134. +unlock_service(VCHIQ_SERVICE_T *service);
  10135. +
  10136. +/* The following functions are called from vchiq_core, and external
  10137. +** implementations must be provided. */
  10138. +
  10139. +extern VCHIQ_STATUS_T
  10140. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  10141. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  10142. +
  10143. +extern void
  10144. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  10145. +
  10146. +extern void
  10147. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  10148. +
  10149. +extern VCHIQ_STATUS_T
  10150. +vchiq_copy_from_user(void *dst, const void *src, int size);
  10151. +
  10152. +extern void
  10153. +remote_event_signal(REMOTE_EVENT_T *event);
  10154. +
  10155. +void
  10156. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  10157. +
  10158. +extern void
  10159. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  10160. +
  10161. +extern VCHIQ_STATUS_T
  10162. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  10163. +
  10164. +extern void
  10165. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  10166. +
  10167. +extern void
  10168. +vchiq_dump(void *dump_context, const char *str, int len);
  10169. +
  10170. +extern void
  10171. +vchiq_dump_platform_state(void *dump_context);
  10172. +
  10173. +extern void
  10174. +vchiq_dump_platform_instances(void *dump_context);
  10175. +
  10176. +extern void
  10177. +vchiq_dump_platform_service_state(void *dump_context,
  10178. + VCHIQ_SERVICE_T *service);
  10179. +
  10180. +extern VCHIQ_STATUS_T
  10181. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  10182. +
  10183. +extern VCHIQ_STATUS_T
  10184. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  10185. +
  10186. +extern void
  10187. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  10188. +
  10189. +extern void
  10190. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  10191. +
  10192. +extern VCHIQ_STATUS_T
  10193. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  10194. +
  10195. +extern VCHIQ_STATUS_T
  10196. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  10197. +
  10198. +extern void
  10199. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  10200. +
  10201. +extern VCHIQ_STATUS_T
  10202. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  10203. +
  10204. +extern VCHIQ_STATUS_T
  10205. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  10206. +
  10207. +extern VCHIQ_STATUS_T
  10208. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  10209. +
  10210. +extern void
  10211. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  10212. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  10213. +
  10214. +extern void
  10215. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  10216. +
  10217. +extern void
  10218. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  10219. +
  10220. +
  10221. +extern void
  10222. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  10223. + size_t numBytes);
  10224. +
  10225. +#endif
  10226. --- /dev/null
  10227. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
  10228. @@ -0,0 +1,383 @@
  10229. +/**
  10230. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10231. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10232. + *
  10233. + * Redistribution and use in source and binary forms, with or without
  10234. + * modification, are permitted provided that the following conditions
  10235. + * are met:
  10236. + * 1. Redistributions of source code must retain the above copyright
  10237. + * notice, this list of conditions, and the following disclaimer,
  10238. + * without modification.
  10239. + * 2. Redistributions in binary form must reproduce the above copyright
  10240. + * notice, this list of conditions and the following disclaimer in the
  10241. + * documentation and/or other materials provided with the distribution.
  10242. + * 3. The names of the above-listed copyright holders may not be used
  10243. + * to endorse or promote products derived from this software without
  10244. + * specific prior written permission.
  10245. + *
  10246. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10247. + * GNU General Public License ("GPL") version 2, as published by the Free
  10248. + * Software Foundation.
  10249. + *
  10250. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10251. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10252. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10253. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10254. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10255. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10256. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10257. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10258. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10259. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10260. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10261. + */
  10262. +
  10263. +
  10264. +#include <linux/debugfs.h>
  10265. +#include "vchiq_core.h"
  10266. +#include "vchiq_arm.h"
  10267. +#include "vchiq_debugfs.h"
  10268. +
  10269. +#ifdef CONFIG_DEBUG_FS
  10270. +
  10271. +/****************************************************************************
  10272. +*
  10273. +* log category entries
  10274. +*
  10275. +***************************************************************************/
  10276. +#define DEBUGFS_WRITE_BUF_SIZE 256
  10277. +
  10278. +#define VCHIQ_LOG_ERROR_STR "error"
  10279. +#define VCHIQ_LOG_WARNING_STR "warning"
  10280. +#define VCHIQ_LOG_INFO_STR "info"
  10281. +#define VCHIQ_LOG_TRACE_STR "trace"
  10282. +
  10283. +
  10284. +/* Top-level debug info */
  10285. +struct vchiq_debugfs_info {
  10286. + /* Global 'vchiq' debugfs entry used by all instances */
  10287. + struct dentry *vchiq_cfg_dir;
  10288. +
  10289. + /* one entry per client process */
  10290. + struct dentry *clients;
  10291. +
  10292. + /* log categories */
  10293. + struct dentry *log_categories;
  10294. +};
  10295. +
  10296. +static struct vchiq_debugfs_info debugfs_info;
  10297. +
  10298. +/* Log category debugfs entries */
  10299. +struct vchiq_debugfs_log_entry {
  10300. + const char *name;
  10301. + int *plevel;
  10302. + struct dentry *dir;
  10303. +};
  10304. +
  10305. +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
  10306. + { "core", &vchiq_core_log_level },
  10307. + { "msg", &vchiq_core_msg_log_level },
  10308. + { "sync", &vchiq_sync_log_level },
  10309. + { "susp", &vchiq_susp_log_level },
  10310. + { "arm", &vchiq_arm_log_level },
  10311. +};
  10312. +static int n_log_entries =
  10313. + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
  10314. +
  10315. +
  10316. +static struct dentry *vchiq_clients_top(void);
  10317. +static struct dentry *vchiq_debugfs_top(void);
  10318. +
  10319. +static int debugfs_log_show(struct seq_file *f, void *offset)
  10320. +{
  10321. + int *levp = f->private;
  10322. + char *log_value = NULL;
  10323. +
  10324. + switch (*levp) {
  10325. + case VCHIQ_LOG_ERROR:
  10326. + log_value = VCHIQ_LOG_ERROR_STR;
  10327. + break;
  10328. + case VCHIQ_LOG_WARNING:
  10329. + log_value = VCHIQ_LOG_WARNING_STR;
  10330. + break;
  10331. + case VCHIQ_LOG_INFO:
  10332. + log_value = VCHIQ_LOG_INFO_STR;
  10333. + break;
  10334. + case VCHIQ_LOG_TRACE:
  10335. + log_value = VCHIQ_LOG_TRACE_STR;
  10336. + break;
  10337. + default:
  10338. + break;
  10339. + }
  10340. +
  10341. + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
  10342. +
  10343. + return 0;
  10344. +}
  10345. +
  10346. +static int debugfs_log_open(struct inode *inode, struct file *file)
  10347. +{
  10348. + return single_open(file, debugfs_log_show, inode->i_private);
  10349. +}
  10350. +
  10351. +static int debugfs_log_write(struct file *file,
  10352. + const char __user *buffer,
  10353. + size_t count, loff_t *ppos)
  10354. +{
  10355. + struct seq_file *f = (struct seq_file *)file->private_data;
  10356. + int *levp = f->private;
  10357. + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
  10358. +
  10359. + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
  10360. + if (count >= DEBUGFS_WRITE_BUF_SIZE)
  10361. + count = DEBUGFS_WRITE_BUF_SIZE;
  10362. +
  10363. + if (copy_from_user(kbuf, buffer, count) != 0)
  10364. + return -EFAULT;
  10365. + kbuf[count - 1] = 0;
  10366. +
  10367. + if (strncmp("error", kbuf, strlen("error")) == 0)
  10368. + *levp = VCHIQ_LOG_ERROR;
  10369. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  10370. + *levp = VCHIQ_LOG_WARNING;
  10371. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  10372. + *levp = VCHIQ_LOG_INFO;
  10373. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  10374. + *levp = VCHIQ_LOG_TRACE;
  10375. + else
  10376. + *levp = VCHIQ_LOG_DEFAULT;
  10377. +
  10378. + *ppos += count;
  10379. +
  10380. + return count;
  10381. +}
  10382. +
  10383. +static const struct file_operations debugfs_log_fops = {
  10384. + .owner = THIS_MODULE,
  10385. + .open = debugfs_log_open,
  10386. + .write = debugfs_log_write,
  10387. + .read = seq_read,
  10388. + .llseek = seq_lseek,
  10389. + .release = single_release,
  10390. +};
  10391. +
  10392. +/* create an entry under <debugfs>/vchiq/log for each log category */
  10393. +static int vchiq_debugfs_create_log_entries(struct dentry *top)
  10394. +{
  10395. + struct dentry *dir;
  10396. + size_t i;
  10397. + int ret = 0;
  10398. + dir = debugfs_create_dir("log", vchiq_debugfs_top());
  10399. + if (!dir)
  10400. + return -ENOMEM;
  10401. + debugfs_info.log_categories = dir;
  10402. +
  10403. + for (i = 0; i < n_log_entries; i++) {
  10404. + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
  10405. + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
  10406. + 0644,
  10407. + debugfs_info.log_categories,
  10408. + levp,
  10409. + &debugfs_log_fops);
  10410. + if (!dir) {
  10411. + ret = -ENOMEM;
  10412. + break;
  10413. + }
  10414. +
  10415. + vchiq_debugfs_log_entries[i].dir = dir;
  10416. + }
  10417. + return ret;
  10418. +}
  10419. +
  10420. +static int debugfs_usecount_show(struct seq_file *f, void *offset)
  10421. +{
  10422. + VCHIQ_INSTANCE_T instance = f->private;
  10423. + int use_count;
  10424. +
  10425. + use_count = vchiq_instance_get_use_count(instance);
  10426. + seq_printf(f, "%d\n", use_count);
  10427. +
  10428. + return 0;
  10429. +}
  10430. +
  10431. +static int debugfs_usecount_open(struct inode *inode, struct file *file)
  10432. +{
  10433. + return single_open(file, debugfs_usecount_show, inode->i_private);
  10434. +}
  10435. +
  10436. +static const struct file_operations debugfs_usecount_fops = {
  10437. + .owner = THIS_MODULE,
  10438. + .open = debugfs_usecount_open,
  10439. + .read = seq_read,
  10440. + .llseek = seq_lseek,
  10441. + .release = single_release,
  10442. +};
  10443. +
  10444. +static int debugfs_trace_show(struct seq_file *f, void *offset)
  10445. +{
  10446. + VCHIQ_INSTANCE_T instance = f->private;
  10447. + int trace;
  10448. +
  10449. + trace = vchiq_instance_get_trace(instance);
  10450. + seq_printf(f, "%s\n", trace ? "Y" : "N");
  10451. +
  10452. + return 0;
  10453. +}
  10454. +
  10455. +static int debugfs_trace_open(struct inode *inode, struct file *file)
  10456. +{
  10457. + return single_open(file, debugfs_trace_show, inode->i_private);
  10458. +}
  10459. +
  10460. +static int debugfs_trace_write(struct file *file,
  10461. + const char __user *buffer,
  10462. + size_t count, loff_t *ppos)
  10463. +{
  10464. + struct seq_file *f = (struct seq_file *)file->private_data;
  10465. + VCHIQ_INSTANCE_T instance = f->private;
  10466. + char firstchar;
  10467. +
  10468. + if (copy_from_user(&firstchar, buffer, 1) != 0)
  10469. + return -EFAULT;
  10470. +
  10471. + switch (firstchar) {
  10472. + case 'Y':
  10473. + case 'y':
  10474. + case '1':
  10475. + vchiq_instance_set_trace(instance, 1);
  10476. + break;
  10477. + case 'N':
  10478. + case 'n':
  10479. + case '0':
  10480. + vchiq_instance_set_trace(instance, 0);
  10481. + break;
  10482. + default:
  10483. + break;
  10484. + }
  10485. +
  10486. + *ppos += count;
  10487. +
  10488. + return count;
  10489. +}
  10490. +
  10491. +static const struct file_operations debugfs_trace_fops = {
  10492. + .owner = THIS_MODULE,
  10493. + .open = debugfs_trace_open,
  10494. + .write = debugfs_trace_write,
  10495. + .read = seq_read,
  10496. + .llseek = seq_lseek,
  10497. + .release = single_release,
  10498. +};
  10499. +
  10500. +/* add an instance (process) to the debugfs entries */
  10501. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10502. +{
  10503. + char pidstr[16];
  10504. + struct dentry *top, *use_count, *trace;
  10505. + struct dentry *clients = vchiq_clients_top();
  10506. +
  10507. + snprintf(pidstr, sizeof(pidstr), "%d",
  10508. + vchiq_instance_get_pid(instance));
  10509. +
  10510. + top = debugfs_create_dir(pidstr, clients);
  10511. + if (!top)
  10512. + goto fail_top;
  10513. +
  10514. + use_count = debugfs_create_file("use_count",
  10515. + 0444, top,
  10516. + instance,
  10517. + &debugfs_usecount_fops);
  10518. + if (!use_count)
  10519. + goto fail_use_count;
  10520. +
  10521. + trace = debugfs_create_file("trace",
  10522. + 0644, top,
  10523. + instance,
  10524. + &debugfs_trace_fops);
  10525. + if (!trace)
  10526. + goto fail_trace;
  10527. +
  10528. + vchiq_instance_get_debugfs_node(instance)->dentry = top;
  10529. +
  10530. + return 0;
  10531. +
  10532. +fail_trace:
  10533. + debugfs_remove(use_count);
  10534. +fail_use_count:
  10535. + debugfs_remove(top);
  10536. +fail_top:
  10537. + return -ENOMEM;
  10538. +}
  10539. +
  10540. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10541. +{
  10542. + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
  10543. + debugfs_remove_recursive(node->dentry);
  10544. +}
  10545. +
  10546. +
  10547. +int vchiq_debugfs_init(void)
  10548. +{
  10549. + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
  10550. +
  10551. + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
  10552. + if (debugfs_info.vchiq_cfg_dir == NULL)
  10553. + goto fail;
  10554. +
  10555. + debugfs_info.clients = debugfs_create_dir("clients",
  10556. + vchiq_debugfs_top());
  10557. + if (!debugfs_info.clients)
  10558. + goto fail;
  10559. +
  10560. + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
  10561. + goto fail;
  10562. +
  10563. + return 0;
  10564. +
  10565. +fail:
  10566. + vchiq_debugfs_deinit();
  10567. + vchiq_log_error(vchiq_arm_log_level,
  10568. + "%s: failed to create debugfs directory",
  10569. + __func__);
  10570. +
  10571. + return -ENOMEM;
  10572. +}
  10573. +
  10574. +/* remove all the debugfs entries */
  10575. +void vchiq_debugfs_deinit(void)
  10576. +{
  10577. + debugfs_remove_recursive(vchiq_debugfs_top());
  10578. +}
  10579. +
  10580. +static struct dentry *vchiq_clients_top(void)
  10581. +{
  10582. + return debugfs_info.clients;
  10583. +}
  10584. +
  10585. +static struct dentry *vchiq_debugfs_top(void)
  10586. +{
  10587. + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
  10588. + return debugfs_info.vchiq_cfg_dir;
  10589. +}
  10590. +
  10591. +#else /* CONFIG_DEBUG_FS */
  10592. +
  10593. +int vchiq_debugfs_init(void)
  10594. +{
  10595. + return 0;
  10596. +}
  10597. +
  10598. +void vchiq_debugfs_deinit(void)
  10599. +{
  10600. +}
  10601. +
  10602. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
  10603. +{
  10604. + return 0;
  10605. +}
  10606. +
  10607. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
  10608. +{
  10609. +}
  10610. +
  10611. +#endif /* CONFIG_DEBUG_FS */
  10612. --- /dev/null
  10613. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
  10614. @@ -0,0 +1,52 @@
  10615. +/**
  10616. + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
  10617. + *
  10618. + * Redistribution and use in source and binary forms, with or without
  10619. + * modification, are permitted provided that the following conditions
  10620. + * are met:
  10621. + * 1. Redistributions of source code must retain the above copyright
  10622. + * notice, this list of conditions, and the following disclaimer,
  10623. + * without modification.
  10624. + * 2. Redistributions in binary form must reproduce the above copyright
  10625. + * notice, this list of conditions and the following disclaimer in the
  10626. + * documentation and/or other materials provided with the distribution.
  10627. + * 3. The names of the above-listed copyright holders may not be used
  10628. + * to endorse or promote products derived from this software without
  10629. + * specific prior written permission.
  10630. + *
  10631. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10632. + * GNU General Public License ("GPL") version 2, as published by the Free
  10633. + * Software Foundation.
  10634. + *
  10635. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10636. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10637. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10638. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10639. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10640. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10641. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10642. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10643. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10644. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10645. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10646. + */
  10647. +
  10648. +#ifndef VCHIQ_DEBUGFS_H
  10649. +#define VCHIQ_DEBUGFS_H
  10650. +
  10651. +#include "vchiq_core.h"
  10652. +
  10653. +typedef struct vchiq_debugfs_node_struct
  10654. +{
  10655. + struct dentry *dentry;
  10656. +} VCHIQ_DEBUGFS_NODE_T;
  10657. +
  10658. +int vchiq_debugfs_init(void);
  10659. +
  10660. +void vchiq_debugfs_deinit(void);
  10661. +
  10662. +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
  10663. +
  10664. +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
  10665. +
  10666. +#endif /* VCHIQ_DEBUGFS_H */
  10667. --- /dev/null
  10668. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  10669. @@ -0,0 +1,87 @@
  10670. +#!/usr/bin/perl -w
  10671. +
  10672. +use strict;
  10673. +
  10674. +#
  10675. +# Generate a version from available information
  10676. +#
  10677. +
  10678. +my $prefix = shift @ARGV;
  10679. +my $root = shift @ARGV;
  10680. +
  10681. +
  10682. +if ( not defined $root ) {
  10683. + die "usage: $0 prefix root-dir\n";
  10684. +}
  10685. +
  10686. +if ( ! -d $root ) {
  10687. + die "root directory $root not found\n";
  10688. +}
  10689. +
  10690. +my $version = "unknown";
  10691. +my $tainted = "";
  10692. +
  10693. +if ( -d "$root/.git" ) {
  10694. + # attempt to work out git version. only do so
  10695. + # on a linux build host, as cygwin builds are
  10696. + # already slow enough
  10697. +
  10698. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  10699. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  10700. + $version = "no git version";
  10701. + }
  10702. + else {
  10703. + $version = <F>;
  10704. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10705. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10706. + }
  10707. +
  10708. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  10709. + $tainted = <G>;
  10710. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10711. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10712. + if (length $tainted) {
  10713. + $version = join ' ', $version, "(tainted)";
  10714. + }
  10715. + else {
  10716. + $version = join ' ', $version, "(clean)";
  10717. + }
  10718. + }
  10719. + }
  10720. +}
  10721. +
  10722. +my $hostname = `hostname`;
  10723. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  10724. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  10725. +
  10726. +
  10727. +print STDERR "Version $version\n";
  10728. +print <<EOF;
  10729. +#include "${prefix}_build_info.h"
  10730. +#include <linux/broadcom/vc_debug_sym.h>
  10731. +
  10732. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  10733. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  10734. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  10735. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  10736. +
  10737. +const char *vchiq_get_build_hostname( void )
  10738. +{
  10739. + return vchiq_build_hostname;
  10740. +}
  10741. +
  10742. +const char *vchiq_get_build_version( void )
  10743. +{
  10744. + return vchiq_build_version;
  10745. +}
  10746. +
  10747. +const char *vchiq_get_build_date( void )
  10748. +{
  10749. + return vchiq_build_date;
  10750. +}
  10751. +
  10752. +const char *vchiq_get_build_time( void )
  10753. +{
  10754. + return vchiq_build_time;
  10755. +}
  10756. +EOF
  10757. --- /dev/null
  10758. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  10759. @@ -0,0 +1,189 @@
  10760. +/**
  10761. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10762. + *
  10763. + * Redistribution and use in source and binary forms, with or without
  10764. + * modification, are permitted provided that the following conditions
  10765. + * are met:
  10766. + * 1. Redistributions of source code must retain the above copyright
  10767. + * notice, this list of conditions, and the following disclaimer,
  10768. + * without modification.
  10769. + * 2. Redistributions in binary form must reproduce the above copyright
  10770. + * notice, this list of conditions and the following disclaimer in the
  10771. + * documentation and/or other materials provided with the distribution.
  10772. + * 3. The names of the above-listed copyright holders may not be used
  10773. + * to endorse or promote products derived from this software without
  10774. + * specific prior written permission.
  10775. + *
  10776. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10777. + * GNU General Public License ("GPL") version 2, as published by the Free
  10778. + * Software Foundation.
  10779. + *
  10780. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10781. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10782. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10783. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10784. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10785. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10786. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10787. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10788. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10789. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10790. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10791. + */
  10792. +
  10793. +#ifndef VCHIQ_IF_H
  10794. +#define VCHIQ_IF_H
  10795. +
  10796. +#include "interface/vchi/vchi_mh.h"
  10797. +
  10798. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  10799. +
  10800. +#define VCHIQ_SLOT_SIZE 4096
  10801. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  10802. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  10803. +
  10804. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  10805. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  10806. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  10807. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  10808. +
  10809. +typedef enum {
  10810. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  10811. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  10812. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  10813. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  10814. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  10815. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  10816. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  10817. +} VCHIQ_REASON_T;
  10818. +
  10819. +typedef enum {
  10820. + VCHIQ_ERROR = -1,
  10821. + VCHIQ_SUCCESS = 0,
  10822. + VCHIQ_RETRY = 1
  10823. +} VCHIQ_STATUS_T;
  10824. +
  10825. +typedef enum {
  10826. + VCHIQ_BULK_MODE_CALLBACK,
  10827. + VCHIQ_BULK_MODE_BLOCKING,
  10828. + VCHIQ_BULK_MODE_NOCALLBACK,
  10829. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  10830. +} VCHIQ_BULK_MODE_T;
  10831. +
  10832. +typedef enum {
  10833. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  10834. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  10835. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  10836. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
  10837. + VCHIQ_SERVICE_OPTION_TRACE
  10838. +} VCHIQ_SERVICE_OPTION_T;
  10839. +
  10840. +typedef struct vchiq_header_struct {
  10841. + /* The message identifier - opaque to applications. */
  10842. + int msgid;
  10843. +
  10844. + /* Size of message data. */
  10845. + unsigned int size;
  10846. +
  10847. + char data[0]; /* message */
  10848. +} VCHIQ_HEADER_T;
  10849. +
  10850. +typedef struct {
  10851. + const void *data;
  10852. + unsigned int size;
  10853. +} VCHIQ_ELEMENT_T;
  10854. +
  10855. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  10856. +
  10857. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  10858. + VCHIQ_SERVICE_HANDLE_T, void *);
  10859. +
  10860. +typedef struct vchiq_service_base_struct {
  10861. + int fourcc;
  10862. + VCHIQ_CALLBACK_T callback;
  10863. + void *userdata;
  10864. +} VCHIQ_SERVICE_BASE_T;
  10865. +
  10866. +typedef struct vchiq_service_params_struct {
  10867. + int fourcc;
  10868. + VCHIQ_CALLBACK_T callback;
  10869. + void *userdata;
  10870. + short version; /* Increment for non-trivial changes */
  10871. + short version_min; /* Update for incompatible changes */
  10872. +} VCHIQ_SERVICE_PARAMS_T;
  10873. +
  10874. +typedef struct vchiq_config_struct {
  10875. + unsigned int max_msg_size;
  10876. + unsigned int bulk_threshold; /* The message size above which it
  10877. + is better to use a bulk transfer
  10878. + (<= max_msg_size) */
  10879. + unsigned int max_outstanding_bulks;
  10880. + unsigned int max_services;
  10881. + short version; /* The version of VCHIQ */
  10882. + short version_min; /* The minimum compatible version of VCHIQ */
  10883. +} VCHIQ_CONFIG_T;
  10884. +
  10885. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  10886. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  10887. +
  10888. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  10889. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  10890. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  10891. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  10892. + const VCHIQ_SERVICE_PARAMS_T *params,
  10893. + VCHIQ_SERVICE_HANDLE_T *pservice);
  10894. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  10895. + const VCHIQ_SERVICE_PARAMS_T *params,
  10896. + VCHIQ_SERVICE_HANDLE_T *pservice);
  10897. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  10898. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  10899. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  10900. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  10901. + VCHIQ_SERVICE_HANDLE_T service);
  10902. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  10903. +
  10904. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  10905. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  10906. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  10907. + VCHIQ_HEADER_T *header);
  10908. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  10909. + const void *data, unsigned int size, void *userdata);
  10910. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  10911. + void *data, unsigned int size, void *userdata);
  10912. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  10913. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  10914. + const void *offset, unsigned int size, void *userdata);
  10915. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  10916. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  10917. + void *offset, unsigned int size, void *userdata);
  10918. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  10919. + const void *data, unsigned int size, void *userdata,
  10920. + VCHIQ_BULK_MODE_T mode);
  10921. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  10922. + void *data, unsigned int size, void *userdata,
  10923. + VCHIQ_BULK_MODE_T mode);
  10924. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  10925. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  10926. + void *userdata, VCHIQ_BULK_MODE_T mode);
  10927. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  10928. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  10929. + void *userdata, VCHIQ_BULK_MODE_T mode);
  10930. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  10931. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  10932. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  10933. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  10934. + int config_size, VCHIQ_CONFIG_T *pconfig);
  10935. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  10936. + VCHIQ_SERVICE_OPTION_T option, int value);
  10937. +
  10938. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  10939. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  10940. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  10941. +
  10942. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  10943. + void *ptr, size_t num_bytes);
  10944. +
  10945. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  10946. + short *peer_version);
  10947. +
  10948. +#endif /* VCHIQ_IF_H */
  10949. --- /dev/null
  10950. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  10951. @@ -0,0 +1,131 @@
  10952. +/**
  10953. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  10954. + *
  10955. + * Redistribution and use in source and binary forms, with or without
  10956. + * modification, are permitted provided that the following conditions
  10957. + * are met:
  10958. + * 1. Redistributions of source code must retain the above copyright
  10959. + * notice, this list of conditions, and the following disclaimer,
  10960. + * without modification.
  10961. + * 2. Redistributions in binary form must reproduce the above copyright
  10962. + * notice, this list of conditions and the following disclaimer in the
  10963. + * documentation and/or other materials provided with the distribution.
  10964. + * 3. The names of the above-listed copyright holders may not be used
  10965. + * to endorse or promote products derived from this software without
  10966. + * specific prior written permission.
  10967. + *
  10968. + * ALTERNATIVELY, this software may be distributed under the terms of the
  10969. + * GNU General Public License ("GPL") version 2, as published by the Free
  10970. + * Software Foundation.
  10971. + *
  10972. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  10973. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10974. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  10975. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  10976. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10977. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10978. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10979. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  10980. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  10981. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  10982. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10983. + */
  10984. +
  10985. +#ifndef VCHIQ_IOCTLS_H
  10986. +#define VCHIQ_IOCTLS_H
  10987. +
  10988. +#include <linux/ioctl.h>
  10989. +#include "vchiq_if.h"
  10990. +
  10991. +#define VCHIQ_IOC_MAGIC 0xc4
  10992. +#define VCHIQ_INVALID_HANDLE (~0)
  10993. +
  10994. +typedef struct {
  10995. + VCHIQ_SERVICE_PARAMS_T params;
  10996. + int is_open;
  10997. + int is_vchi;
  10998. + unsigned int handle; /* OUT */
  10999. +} VCHIQ_CREATE_SERVICE_T;
  11000. +
  11001. +typedef struct {
  11002. + unsigned int handle;
  11003. + unsigned int count;
  11004. + const VCHIQ_ELEMENT_T *elements;
  11005. +} VCHIQ_QUEUE_MESSAGE_T;
  11006. +
  11007. +typedef struct {
  11008. + unsigned int handle;
  11009. + void *data;
  11010. + unsigned int size;
  11011. + void *userdata;
  11012. + VCHIQ_BULK_MODE_T mode;
  11013. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  11014. +
  11015. +typedef struct {
  11016. + VCHIQ_REASON_T reason;
  11017. + VCHIQ_HEADER_T *header;
  11018. + void *service_userdata;
  11019. + void *bulk_userdata;
  11020. +} VCHIQ_COMPLETION_DATA_T;
  11021. +
  11022. +typedef struct {
  11023. + unsigned int count;
  11024. + VCHIQ_COMPLETION_DATA_T *buf;
  11025. + unsigned int msgbufsize;
  11026. + unsigned int msgbufcount; /* IN/OUT */
  11027. + void **msgbufs;
  11028. +} VCHIQ_AWAIT_COMPLETION_T;
  11029. +
  11030. +typedef struct {
  11031. + unsigned int handle;
  11032. + int blocking;
  11033. + unsigned int bufsize;
  11034. + void *buf;
  11035. +} VCHIQ_DEQUEUE_MESSAGE_T;
  11036. +
  11037. +typedef struct {
  11038. + unsigned int config_size;
  11039. + VCHIQ_CONFIG_T *pconfig;
  11040. +} VCHIQ_GET_CONFIG_T;
  11041. +
  11042. +typedef struct {
  11043. + unsigned int handle;
  11044. + VCHIQ_SERVICE_OPTION_T option;
  11045. + int value;
  11046. +} VCHIQ_SET_SERVICE_OPTION_T;
  11047. +
  11048. +typedef struct {
  11049. + void *virt_addr;
  11050. + size_t num_bytes;
  11051. +} VCHIQ_DUMP_MEM_T;
  11052. +
  11053. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  11054. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  11055. +#define VCHIQ_IOC_CREATE_SERVICE \
  11056. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  11057. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  11058. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  11059. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  11060. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  11061. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11062. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  11063. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  11064. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  11065. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  11066. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  11067. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  11068. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  11069. +#define VCHIQ_IOC_GET_CONFIG \
  11070. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  11071. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  11072. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  11073. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  11074. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  11075. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  11076. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  11077. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  11078. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  11079. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  11080. +#define VCHIQ_IOC_MAX 17
  11081. +
  11082. +#endif
  11083. --- /dev/null
  11084. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  11085. @@ -0,0 +1,456 @@
  11086. +/**
  11087. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11088. + *
  11089. + * Redistribution and use in source and binary forms, with or without
  11090. + * modification, are permitted provided that the following conditions
  11091. + * are met:
  11092. + * 1. Redistributions of source code must retain the above copyright
  11093. + * notice, this list of conditions, and the following disclaimer,
  11094. + * without modification.
  11095. + * 2. Redistributions in binary form must reproduce the above copyright
  11096. + * notice, this list of conditions and the following disclaimer in the
  11097. + * documentation and/or other materials provided with the distribution.
  11098. + * 3. The names of the above-listed copyright holders may not be used
  11099. + * to endorse or promote products derived from this software without
  11100. + * specific prior written permission.
  11101. + *
  11102. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11103. + * GNU General Public License ("GPL") version 2, as published by the Free
  11104. + * Software Foundation.
  11105. + *
  11106. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11107. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11108. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11109. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11110. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11111. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11112. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11113. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11114. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11115. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11116. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11117. + */
  11118. +
  11119. +/* ---- Include Files ---------------------------------------------------- */
  11120. +
  11121. +#include <linux/kernel.h>
  11122. +#include <linux/module.h>
  11123. +#include <linux/mutex.h>
  11124. +
  11125. +#include "vchiq_core.h"
  11126. +#include "vchiq_arm.h"
  11127. +
  11128. +/* ---- Public Variables ------------------------------------------------- */
  11129. +
  11130. +/* ---- Private Constants and Types -------------------------------------- */
  11131. +
  11132. +struct bulk_waiter_node {
  11133. + struct bulk_waiter bulk_waiter;
  11134. + int pid;
  11135. + struct list_head list;
  11136. +};
  11137. +
  11138. +struct vchiq_instance_struct {
  11139. + VCHIQ_STATE_T *state;
  11140. +
  11141. + int connected;
  11142. +
  11143. + struct list_head bulk_waiter_list;
  11144. + struct mutex bulk_waiter_list_mutex;
  11145. +};
  11146. +
  11147. +static VCHIQ_STATUS_T
  11148. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11149. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  11150. +
  11151. +/****************************************************************************
  11152. +*
  11153. +* vchiq_initialise
  11154. +*
  11155. +***************************************************************************/
  11156. +#define VCHIQ_INIT_RETRIES 10
  11157. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  11158. +{
  11159. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11160. + VCHIQ_STATE_T *state;
  11161. + VCHIQ_INSTANCE_T instance = NULL;
  11162. + int i;
  11163. +
  11164. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  11165. +
  11166. + /* VideoCore may not be ready due to boot up timing.
  11167. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  11168. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  11169. + state = vchiq_get_state();
  11170. + if (state)
  11171. + break;
  11172. + udelay(500);
  11173. + }
  11174. + if (i==VCHIQ_INIT_RETRIES) {
  11175. + vchiq_log_error(vchiq_core_log_level,
  11176. + "%s: videocore not initialized\n", __func__);
  11177. + goto failed;
  11178. + } else if (i>0) {
  11179. + vchiq_log_warning(vchiq_core_log_level,
  11180. + "%s: videocore initialized after %d retries\n", __func__, i);
  11181. + }
  11182. +
  11183. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  11184. + if (!instance) {
  11185. + vchiq_log_error(vchiq_core_log_level,
  11186. + "%s: error allocating vchiq instance\n", __func__);
  11187. + goto failed;
  11188. + }
  11189. +
  11190. + instance->connected = 0;
  11191. + instance->state = state;
  11192. + mutex_init(&instance->bulk_waiter_list_mutex);
  11193. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  11194. +
  11195. + *instanceOut = instance;
  11196. +
  11197. + status = VCHIQ_SUCCESS;
  11198. +
  11199. +failed:
  11200. + vchiq_log_trace(vchiq_core_log_level,
  11201. + "%s(%p): returning %d", __func__, instance, status);
  11202. +
  11203. + return status;
  11204. +}
  11205. +EXPORT_SYMBOL(vchiq_initialise);
  11206. +
  11207. +/****************************************************************************
  11208. +*
  11209. +* vchiq_shutdown
  11210. +*
  11211. +***************************************************************************/
  11212. +
  11213. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  11214. +{
  11215. + VCHIQ_STATUS_T status;
  11216. + VCHIQ_STATE_T *state = instance->state;
  11217. +
  11218. + vchiq_log_trace(vchiq_core_log_level,
  11219. + "%s(%p) called", __func__, instance);
  11220. +
  11221. + if (mutex_lock_interruptible(&state->mutex) != 0)
  11222. + return VCHIQ_RETRY;
  11223. +
  11224. + /* Remove all services */
  11225. + status = vchiq_shutdown_internal(state, instance);
  11226. +
  11227. + mutex_unlock(&state->mutex);
  11228. +
  11229. + vchiq_log_trace(vchiq_core_log_level,
  11230. + "%s(%p): returning %d", __func__, instance, status);
  11231. +
  11232. + if (status == VCHIQ_SUCCESS) {
  11233. + struct list_head *pos, *next;
  11234. + list_for_each_safe(pos, next,
  11235. + &instance->bulk_waiter_list) {
  11236. + struct bulk_waiter_node *waiter;
  11237. + waiter = list_entry(pos,
  11238. + struct bulk_waiter_node,
  11239. + list);
  11240. + list_del(pos);
  11241. + vchiq_log_info(vchiq_arm_log_level,
  11242. + "bulk_waiter - cleaned up %x "
  11243. + "for pid %d",
  11244. + (unsigned int)waiter, waiter->pid);
  11245. + kfree(waiter);
  11246. + }
  11247. + kfree(instance);
  11248. + }
  11249. +
  11250. + return status;
  11251. +}
  11252. +EXPORT_SYMBOL(vchiq_shutdown);
  11253. +
  11254. +/****************************************************************************
  11255. +*
  11256. +* vchiq_is_connected
  11257. +*
  11258. +***************************************************************************/
  11259. +
  11260. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  11261. +{
  11262. + return instance->connected;
  11263. +}
  11264. +
  11265. +/****************************************************************************
  11266. +*
  11267. +* vchiq_connect
  11268. +*
  11269. +***************************************************************************/
  11270. +
  11271. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  11272. +{
  11273. + VCHIQ_STATUS_T status;
  11274. + VCHIQ_STATE_T *state = instance->state;
  11275. +
  11276. + vchiq_log_trace(vchiq_core_log_level,
  11277. + "%s(%p) called", __func__, instance);
  11278. +
  11279. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  11280. + vchiq_log_trace(vchiq_core_log_level,
  11281. + "%s: call to mutex_lock failed", __func__);
  11282. + status = VCHIQ_RETRY;
  11283. + goto failed;
  11284. + }
  11285. + status = vchiq_connect_internal(state, instance);
  11286. +
  11287. + if (status == VCHIQ_SUCCESS)
  11288. + instance->connected = 1;
  11289. +
  11290. + mutex_unlock(&state->mutex);
  11291. +
  11292. +failed:
  11293. + vchiq_log_trace(vchiq_core_log_level,
  11294. + "%s(%p): returning %d", __func__, instance, status);
  11295. +
  11296. + return status;
  11297. +}
  11298. +EXPORT_SYMBOL(vchiq_connect);
  11299. +
  11300. +/****************************************************************************
  11301. +*
  11302. +* vchiq_add_service
  11303. +*
  11304. +***************************************************************************/
  11305. +
  11306. +VCHIQ_STATUS_T vchiq_add_service(
  11307. + VCHIQ_INSTANCE_T instance,
  11308. + const VCHIQ_SERVICE_PARAMS_T *params,
  11309. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11310. +{
  11311. + VCHIQ_STATUS_T status;
  11312. + VCHIQ_STATE_T *state = instance->state;
  11313. + VCHIQ_SERVICE_T *service = NULL;
  11314. + int srvstate;
  11315. +
  11316. + vchiq_log_trace(vchiq_core_log_level,
  11317. + "%s(%p) called", __func__, instance);
  11318. +
  11319. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11320. +
  11321. + srvstate = vchiq_is_connected(instance)
  11322. + ? VCHIQ_SRVSTATE_LISTENING
  11323. + : VCHIQ_SRVSTATE_HIDDEN;
  11324. +
  11325. + service = vchiq_add_service_internal(
  11326. + state,
  11327. + params,
  11328. + srvstate,
  11329. + instance,
  11330. + NULL);
  11331. +
  11332. + if (service) {
  11333. + *phandle = service->handle;
  11334. + status = VCHIQ_SUCCESS;
  11335. + } else
  11336. + status = VCHIQ_ERROR;
  11337. +
  11338. + vchiq_log_trace(vchiq_core_log_level,
  11339. + "%s(%p): returning %d", __func__, instance, status);
  11340. +
  11341. + return status;
  11342. +}
  11343. +EXPORT_SYMBOL(vchiq_add_service);
  11344. +
  11345. +/****************************************************************************
  11346. +*
  11347. +* vchiq_open_service
  11348. +*
  11349. +***************************************************************************/
  11350. +
  11351. +VCHIQ_STATUS_T vchiq_open_service(
  11352. + VCHIQ_INSTANCE_T instance,
  11353. + const VCHIQ_SERVICE_PARAMS_T *params,
  11354. + VCHIQ_SERVICE_HANDLE_T *phandle)
  11355. +{
  11356. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  11357. + VCHIQ_STATE_T *state = instance->state;
  11358. + VCHIQ_SERVICE_T *service = NULL;
  11359. +
  11360. + vchiq_log_trace(vchiq_core_log_level,
  11361. + "%s(%p) called", __func__, instance);
  11362. +
  11363. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  11364. +
  11365. + if (!vchiq_is_connected(instance))
  11366. + goto failed;
  11367. +
  11368. + service = vchiq_add_service_internal(state,
  11369. + params,
  11370. + VCHIQ_SRVSTATE_OPENING,
  11371. + instance,
  11372. + NULL);
  11373. +
  11374. + if (service) {
  11375. + status = vchiq_open_service_internal(service, current->pid);
  11376. + if (status == VCHIQ_SUCCESS)
  11377. + *phandle = service->handle;
  11378. + else
  11379. + vchiq_remove_service(service->handle);
  11380. + }
  11381. +
  11382. +failed:
  11383. + vchiq_log_trace(vchiq_core_log_level,
  11384. + "%s(%p): returning %d", __func__, instance, status);
  11385. +
  11386. + return status;
  11387. +}
  11388. +EXPORT_SYMBOL(vchiq_open_service);
  11389. +
  11390. +VCHIQ_STATUS_T
  11391. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  11392. + const void *data, unsigned int size, void *userdata)
  11393. +{
  11394. + return vchiq_bulk_transfer(handle,
  11395. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11396. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  11397. +}
  11398. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  11399. +
  11400. +VCHIQ_STATUS_T
  11401. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11402. + unsigned int size, void *userdata)
  11403. +{
  11404. + return vchiq_bulk_transfer(handle,
  11405. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11406. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  11407. +}
  11408. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  11409. +
  11410. +VCHIQ_STATUS_T
  11411. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  11412. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11413. +{
  11414. + VCHIQ_STATUS_T status;
  11415. +
  11416. + switch (mode) {
  11417. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11418. + case VCHIQ_BULK_MODE_CALLBACK:
  11419. + status = vchiq_bulk_transfer(handle,
  11420. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  11421. + mode, VCHIQ_BULK_TRANSMIT);
  11422. + break;
  11423. + case VCHIQ_BULK_MODE_BLOCKING:
  11424. + status = vchiq_blocking_bulk_transfer(handle,
  11425. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  11426. + break;
  11427. + default:
  11428. + return VCHIQ_ERROR;
  11429. + }
  11430. +
  11431. + return status;
  11432. +}
  11433. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  11434. +
  11435. +VCHIQ_STATUS_T
  11436. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11437. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  11438. +{
  11439. + VCHIQ_STATUS_T status;
  11440. +
  11441. + switch (mode) {
  11442. + case VCHIQ_BULK_MODE_NOCALLBACK:
  11443. + case VCHIQ_BULK_MODE_CALLBACK:
  11444. + status = vchiq_bulk_transfer(handle,
  11445. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  11446. + mode, VCHIQ_BULK_RECEIVE);
  11447. + break;
  11448. + case VCHIQ_BULK_MODE_BLOCKING:
  11449. + status = vchiq_blocking_bulk_transfer(handle,
  11450. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  11451. + break;
  11452. + default:
  11453. + return VCHIQ_ERROR;
  11454. + }
  11455. +
  11456. + return status;
  11457. +}
  11458. +EXPORT_SYMBOL(vchiq_bulk_receive);
  11459. +
  11460. +static VCHIQ_STATUS_T
  11461. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  11462. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  11463. +{
  11464. + VCHIQ_INSTANCE_T instance;
  11465. + VCHIQ_SERVICE_T *service;
  11466. + VCHIQ_STATUS_T status;
  11467. + struct bulk_waiter_node *waiter = NULL;
  11468. + struct list_head *pos;
  11469. +
  11470. + service = find_service_by_handle(handle);
  11471. + if (!service)
  11472. + return VCHIQ_ERROR;
  11473. +
  11474. + instance = service->instance;
  11475. +
  11476. + unlock_service(service);
  11477. +
  11478. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11479. + list_for_each(pos, &instance->bulk_waiter_list) {
  11480. + if (list_entry(pos, struct bulk_waiter_node,
  11481. + list)->pid == current->pid) {
  11482. + waiter = list_entry(pos,
  11483. + struct bulk_waiter_node,
  11484. + list);
  11485. + list_del(pos);
  11486. + break;
  11487. + }
  11488. + }
  11489. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11490. +
  11491. + if (waiter) {
  11492. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11493. + if (bulk) {
  11494. + /* This thread has an outstanding bulk transfer. */
  11495. + if ((bulk->data != data) ||
  11496. + (bulk->size != size)) {
  11497. + /* This is not a retry of the previous one.
  11498. + ** Cancel the signal when the transfer
  11499. + ** completes. */
  11500. + spin_lock(&bulk_waiter_spinlock);
  11501. + bulk->userdata = NULL;
  11502. + spin_unlock(&bulk_waiter_spinlock);
  11503. + }
  11504. + }
  11505. + }
  11506. +
  11507. + if (!waiter) {
  11508. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  11509. + if (!waiter) {
  11510. + vchiq_log_error(vchiq_core_log_level,
  11511. + "%s - out of memory", __func__);
  11512. + return VCHIQ_ERROR;
  11513. + }
  11514. + }
  11515. +
  11516. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  11517. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  11518. + dir);
  11519. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  11520. + !waiter->bulk_waiter.bulk) {
  11521. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  11522. + if (bulk) {
  11523. + /* Cancel the signal when the transfer
  11524. + ** completes. */
  11525. + spin_lock(&bulk_waiter_spinlock);
  11526. + bulk->userdata = NULL;
  11527. + spin_unlock(&bulk_waiter_spinlock);
  11528. + }
  11529. + kfree(waiter);
  11530. + } else {
  11531. + waiter->pid = current->pid;
  11532. + mutex_lock(&instance->bulk_waiter_list_mutex);
  11533. + list_add(&waiter->list, &instance->bulk_waiter_list);
  11534. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  11535. + vchiq_log_info(vchiq_arm_log_level,
  11536. + "saved bulk_waiter %x for pid %d",
  11537. + (unsigned int)waiter, current->pid);
  11538. + }
  11539. +
  11540. + return status;
  11541. +}
  11542. --- /dev/null
  11543. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  11544. @@ -0,0 +1,71 @@
  11545. +/**
  11546. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11547. + *
  11548. + * Redistribution and use in source and binary forms, with or without
  11549. + * modification, are permitted provided that the following conditions
  11550. + * are met:
  11551. + * 1. Redistributions of source code must retain the above copyright
  11552. + * notice, this list of conditions, and the following disclaimer,
  11553. + * without modification.
  11554. + * 2. Redistributions in binary form must reproduce the above copyright
  11555. + * notice, this list of conditions and the following disclaimer in the
  11556. + * documentation and/or other materials provided with the distribution.
  11557. + * 3. The names of the above-listed copyright holders may not be used
  11558. + * to endorse or promote products derived from this software without
  11559. + * specific prior written permission.
  11560. + *
  11561. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11562. + * GNU General Public License ("GPL") version 2, as published by the Free
  11563. + * Software Foundation.
  11564. + *
  11565. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11566. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11567. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11568. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11569. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11570. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11571. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11572. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11573. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11574. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11575. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11576. + */
  11577. +
  11578. +#ifndef VCHIQ_MEMDRV_H
  11579. +#define VCHIQ_MEMDRV_H
  11580. +
  11581. +/* ---- Include Files ----------------------------------------------------- */
  11582. +
  11583. +#include <linux/kernel.h>
  11584. +#include "vchiq_if.h"
  11585. +
  11586. +/* ---- Constants and Types ---------------------------------------------- */
  11587. +
  11588. +typedef struct {
  11589. + void *armSharedMemVirt;
  11590. + dma_addr_t armSharedMemPhys;
  11591. + size_t armSharedMemSize;
  11592. +
  11593. + void *vcSharedMemVirt;
  11594. + dma_addr_t vcSharedMemPhys;
  11595. + size_t vcSharedMemSize;
  11596. +} VCHIQ_SHARED_MEM_INFO_T;
  11597. +
  11598. +/* ---- Variable Externs ------------------------------------------------- */
  11599. +
  11600. +/* ---- Function Prototypes ---------------------------------------------- */
  11601. +
  11602. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  11603. +
  11604. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  11605. +
  11606. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  11607. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11608. +
  11609. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  11610. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11611. +
  11612. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  11613. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  11614. +
  11615. +#endif
  11616. --- /dev/null
  11617. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  11618. @@ -0,0 +1,58 @@
  11619. +/**
  11620. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11621. + *
  11622. + * Redistribution and use in source and binary forms, with or without
  11623. + * modification, are permitted provided that the following conditions
  11624. + * are met:
  11625. + * 1. Redistributions of source code must retain the above copyright
  11626. + * notice, this list of conditions, and the following disclaimer,
  11627. + * without modification.
  11628. + * 2. Redistributions in binary form must reproduce the above copyright
  11629. + * notice, this list of conditions and the following disclaimer in the
  11630. + * documentation and/or other materials provided with the distribution.
  11631. + * 3. The names of the above-listed copyright holders may not be used
  11632. + * to endorse or promote products derived from this software without
  11633. + * specific prior written permission.
  11634. + *
  11635. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11636. + * GNU General Public License ("GPL") version 2, as published by the Free
  11637. + * Software Foundation.
  11638. + *
  11639. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11640. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11641. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11642. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11643. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11644. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11645. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11646. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11647. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11648. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11649. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11650. + */
  11651. +
  11652. +#ifndef VCHIQ_PAGELIST_H
  11653. +#define VCHIQ_PAGELIST_H
  11654. +
  11655. +#ifndef PAGE_SIZE
  11656. +#define PAGE_SIZE 4096
  11657. +#endif
  11658. +#define CACHE_LINE_SIZE 32
  11659. +#define PAGELIST_WRITE 0
  11660. +#define PAGELIST_READ 1
  11661. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  11662. +
  11663. +typedef struct pagelist_struct {
  11664. + unsigned long length;
  11665. + unsigned short type;
  11666. + unsigned short offset;
  11667. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  11668. + pages at consecutive addresses. */
  11669. +} PAGELIST_T;
  11670. +
  11671. +typedef struct fragments_struct {
  11672. + char headbuf[CACHE_LINE_SIZE];
  11673. + char tailbuf[CACHE_LINE_SIZE];
  11674. +} FRAGMENTS_T;
  11675. +
  11676. +#endif /* VCHIQ_PAGELIST_H */
  11677. --- /dev/null
  11678. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  11679. @@ -0,0 +1,853 @@
  11680. +/**
  11681. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  11682. + *
  11683. + * Redistribution and use in source and binary forms, with or without
  11684. + * modification, are permitted provided that the following conditions
  11685. + * are met:
  11686. + * 1. Redistributions of source code must retain the above copyright
  11687. + * notice, this list of conditions, and the following disclaimer,
  11688. + * without modification.
  11689. + * 2. Redistributions in binary form must reproduce the above copyright
  11690. + * notice, this list of conditions and the following disclaimer in the
  11691. + * documentation and/or other materials provided with the distribution.
  11692. + * 3. The names of the above-listed copyright holders may not be used
  11693. + * to endorse or promote products derived from this software without
  11694. + * specific prior written permission.
  11695. + *
  11696. + * ALTERNATIVELY, this software may be distributed under the terms of the
  11697. + * GNU General Public License ("GPL") version 2, as published by the Free
  11698. + * Software Foundation.
  11699. + *
  11700. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  11701. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  11702. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11703. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  11704. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  11705. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  11706. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  11707. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  11708. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  11709. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  11710. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  11711. + */
  11712. +#include <linux/module.h>
  11713. +#include <linux/types.h>
  11714. +
  11715. +#include "interface/vchi/vchi.h"
  11716. +#include "vchiq.h"
  11717. +#include "vchiq_core.h"
  11718. +
  11719. +#include "vchiq_util.h"
  11720. +
  11721. +#include <stddef.h>
  11722. +
  11723. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  11724. +
  11725. +typedef struct {
  11726. + VCHIQ_SERVICE_HANDLE_T handle;
  11727. +
  11728. + VCHIU_QUEUE_T queue;
  11729. +
  11730. + VCHI_CALLBACK_T callback;
  11731. + void *callback_param;
  11732. +} SHIM_SERVICE_T;
  11733. +
  11734. +/* ----------------------------------------------------------------------
  11735. + * return pointer to the mphi message driver function table
  11736. + * -------------------------------------------------------------------- */
  11737. +const VCHI_MESSAGE_DRIVER_T *
  11738. +vchi_mphi_message_driver_func_table(void)
  11739. +{
  11740. + return NULL;
  11741. +}
  11742. +
  11743. +/* ----------------------------------------------------------------------
  11744. + * return a pointer to the 'single' connection driver fops
  11745. + * -------------------------------------------------------------------- */
  11746. +const VCHI_CONNECTION_API_T *
  11747. +single_get_func_table(void)
  11748. +{
  11749. + return NULL;
  11750. +}
  11751. +
  11752. +VCHI_CONNECTION_T *vchi_create_connection(
  11753. + const VCHI_CONNECTION_API_T *function_table,
  11754. + const VCHI_MESSAGE_DRIVER_T *low_level)
  11755. +{
  11756. + (void)function_table;
  11757. + (void)low_level;
  11758. + return NULL;
  11759. +}
  11760. +
  11761. +/***********************************************************
  11762. + * Name: vchi_msg_peek
  11763. + *
  11764. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  11765. + * void **data,
  11766. + * uint32_t *msg_size,
  11767. +
  11768. +
  11769. + * VCHI_FLAGS_T flags
  11770. + *
  11771. + * Description: Routine to return a pointer to the current message (to allow in
  11772. + * place processing). The message can be removed using
  11773. + * vchi_msg_remove when you're finished
  11774. + *
  11775. + * Returns: int32_t - success == 0
  11776. + *
  11777. + ***********************************************************/
  11778. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  11779. + void **data,
  11780. + uint32_t *msg_size,
  11781. + VCHI_FLAGS_T flags)
  11782. +{
  11783. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  11784. + VCHIQ_HEADER_T *header;
  11785. +
  11786. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  11787. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  11788. +
  11789. + if (flags == VCHI_FLAGS_NONE)
  11790. + if (vchiu_queue_is_empty(&service->queue))
  11791. + return -1;
  11792. +
  11793. + header = vchiu_queue_peek(&service->queue);
  11794. +
  11795. + *data = header->data;
  11796. + *msg_size = header->size;
  11797. +
  11798. + return 0;
  11799. +}
  11800. +EXPORT_SYMBOL(vchi_msg_peek);
  11801. +
  11802. +/***********************************************************
  11803. + * Name: vchi_msg_remove
  11804. + *
  11805. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  11806. + *
  11807. + * Description: Routine to remove a message (after it has been read with
  11808. + * vchi_msg_peek)
  11809. + *
  11810. + * Returns: int32_t - success == 0
  11811. + *
  11812. + ***********************************************************/
  11813. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  11814. +{
  11815. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  11816. + VCHIQ_HEADER_T *header;
  11817. +
  11818. + header = vchiu_queue_pop(&service->queue);
  11819. +
  11820. + vchiq_release_message(service->handle, header);
  11821. +
  11822. + return 0;
  11823. +}
  11824. +EXPORT_SYMBOL(vchi_msg_remove);
  11825. +
  11826. +/***********************************************************
  11827. + * Name: vchi_msg_queue
  11828. + *
  11829. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  11830. + * const void *data,
  11831. + * uint32_t data_size,
  11832. + * VCHI_FLAGS_T flags,
  11833. + * void *msg_handle,
  11834. + *
  11835. + * Description: Thin wrapper to queue a message onto a connection
  11836. + *
  11837. + * Returns: int32_t - success == 0
  11838. + *
  11839. + ***********************************************************/
  11840. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  11841. + const void *data,
  11842. + uint32_t data_size,
  11843. + VCHI_FLAGS_T flags,
  11844. + void *msg_handle)
  11845. +{
  11846. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  11847. + VCHIQ_ELEMENT_T element = {data, data_size};
  11848. + VCHIQ_STATUS_T status;
  11849. +
  11850. + (void)msg_handle;
  11851. +
  11852. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  11853. +
  11854. + status = vchiq_queue_message(service->handle, &element, 1);
  11855. +
  11856. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  11857. + ** implement a retry mechanism since this function is supposed
  11858. + ** to block until queued
  11859. + */
  11860. + while (status == VCHIQ_RETRY) {
  11861. + msleep(1);
  11862. + status = vchiq_queue_message(service->handle, &element, 1);
  11863. + }
  11864. +
  11865. + return vchiq_status_to_vchi(status);
  11866. +}
  11867. +EXPORT_SYMBOL(vchi_msg_queue);
  11868. +
  11869. +/***********************************************************
  11870. + * Name: vchi_bulk_queue_receive
  11871. + *
  11872. + * Arguments: VCHI_BULK_HANDLE_T handle,
  11873. + * void *data_dst,
  11874. + * const uint32_t data_size,
  11875. + * VCHI_FLAGS_T flags
  11876. + * void *bulk_handle
  11877. + *
  11878. + * Description: Routine to setup a rcv buffer
  11879. + *
  11880. + * Returns: int32_t - success == 0
  11881. + *
  11882. + ***********************************************************/
  11883. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  11884. + void *data_dst,
  11885. + uint32_t data_size,
  11886. + VCHI_FLAGS_T flags,
  11887. + void *bulk_handle)
  11888. +{
  11889. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  11890. + VCHIQ_BULK_MODE_T mode;
  11891. + VCHIQ_STATUS_T status;
  11892. +
  11893. + switch ((int)flags) {
  11894. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  11895. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  11896. + WARN_ON(!service->callback);
  11897. + mode = VCHIQ_BULK_MODE_CALLBACK;
  11898. + break;
  11899. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  11900. + mode = VCHIQ_BULK_MODE_BLOCKING;
  11901. + break;
  11902. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  11903. + case VCHI_FLAGS_NONE:
  11904. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  11905. + break;
  11906. + default:
  11907. + WARN(1, "unsupported message\n");
  11908. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  11909. + }
  11910. +
  11911. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  11912. + bulk_handle, mode);
  11913. +
  11914. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  11915. + ** implement a retry mechanism since this function is supposed
  11916. + ** to block until queued
  11917. + */
  11918. + while (status == VCHIQ_RETRY) {
  11919. + msleep(1);
  11920. + status = vchiq_bulk_receive(service->handle, data_dst,
  11921. + data_size, bulk_handle, mode);
  11922. + }
  11923. +
  11924. + return vchiq_status_to_vchi(status);
  11925. +}
  11926. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  11927. +
  11928. +/***********************************************************
  11929. + * Name: vchi_bulk_queue_transmit
  11930. + *
  11931. + * Arguments: VCHI_BULK_HANDLE_T handle,
  11932. + * const void *data_src,
  11933. + * uint32_t data_size,
  11934. + * VCHI_FLAGS_T flags,
  11935. + * void *bulk_handle
  11936. + *
  11937. + * Description: Routine to transmit some data
  11938. + *
  11939. + * Returns: int32_t - success == 0
  11940. + *
  11941. + ***********************************************************/
  11942. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  11943. + const void *data_src,
  11944. + uint32_t data_size,
  11945. + VCHI_FLAGS_T flags,
  11946. + void *bulk_handle)
  11947. +{
  11948. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  11949. + VCHIQ_BULK_MODE_T mode;
  11950. + VCHIQ_STATUS_T status;
  11951. +
  11952. + switch ((int)flags) {
  11953. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  11954. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  11955. + WARN_ON(!service->callback);
  11956. + mode = VCHIQ_BULK_MODE_CALLBACK;
  11957. + break;
  11958. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  11959. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  11960. + mode = VCHIQ_BULK_MODE_BLOCKING;
  11961. + break;
  11962. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  11963. + case VCHI_FLAGS_NONE:
  11964. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  11965. + break;
  11966. + default:
  11967. + WARN(1, "unsupported message\n");
  11968. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  11969. + }
  11970. +
  11971. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  11972. + bulk_handle, mode);
  11973. +
  11974. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  11975. + ** implement a retry mechanism since this function is supposed
  11976. + ** to block until queued
  11977. + */
  11978. + while (status == VCHIQ_RETRY) {
  11979. + msleep(1);
  11980. + status = vchiq_bulk_transmit(service->handle, data_src,
  11981. + data_size, bulk_handle, mode);
  11982. + }
  11983. +
  11984. + return vchiq_status_to_vchi(status);
  11985. +}
  11986. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  11987. +
  11988. +/***********************************************************
  11989. + * Name: vchi_msg_dequeue
  11990. + *
  11991. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  11992. + * void *data,
  11993. + * uint32_t max_data_size_to_read,
  11994. + * uint32_t *actual_msg_size
  11995. + * VCHI_FLAGS_T flags
  11996. + *
  11997. + * Description: Routine to dequeue a message into the supplied buffer
  11998. + *
  11999. + * Returns: int32_t - success == 0
  12000. + *
  12001. + ***********************************************************/
  12002. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  12003. + void *data,
  12004. + uint32_t max_data_size_to_read,
  12005. + uint32_t *actual_msg_size,
  12006. + VCHI_FLAGS_T flags)
  12007. +{
  12008. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12009. + VCHIQ_HEADER_T *header;
  12010. +
  12011. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12012. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12013. +
  12014. + if (flags == VCHI_FLAGS_NONE)
  12015. + if (vchiu_queue_is_empty(&service->queue))
  12016. + return -1;
  12017. +
  12018. + header = vchiu_queue_pop(&service->queue);
  12019. +
  12020. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  12021. + header->size : max_data_size_to_read);
  12022. +
  12023. + *actual_msg_size = header->size;
  12024. +
  12025. + vchiq_release_message(service->handle, header);
  12026. +
  12027. + return 0;
  12028. +}
  12029. +EXPORT_SYMBOL(vchi_msg_dequeue);
  12030. +
  12031. +/***********************************************************
  12032. + * Name: vchi_msg_queuev
  12033. + *
  12034. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12035. + * VCHI_MSG_VECTOR_T *vector,
  12036. + * uint32_t count,
  12037. + * VCHI_FLAGS_T flags,
  12038. + * void *msg_handle
  12039. + *
  12040. + * Description: Thin wrapper to queue a message onto a connection
  12041. + *
  12042. + * Returns: int32_t - success == 0
  12043. + *
  12044. + ***********************************************************/
  12045. +
  12046. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  12047. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  12048. + offsetof(VCHIQ_ELEMENT_T, data));
  12049. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  12050. + offsetof(VCHIQ_ELEMENT_T, size));
  12051. +
  12052. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  12053. + VCHI_MSG_VECTOR_T *vector,
  12054. + uint32_t count,
  12055. + VCHI_FLAGS_T flags,
  12056. + void *msg_handle)
  12057. +{
  12058. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12059. +
  12060. + (void)msg_handle;
  12061. +
  12062. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  12063. +
  12064. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  12065. + (const VCHIQ_ELEMENT_T *)vector, count));
  12066. +}
  12067. +EXPORT_SYMBOL(vchi_msg_queuev);
  12068. +
  12069. +/***********************************************************
  12070. + * Name: vchi_held_msg_release
  12071. + *
  12072. + * Arguments: VCHI_HELD_MSG_T *message
  12073. + *
  12074. + * Description: Routine to release a held message (after it has been read with
  12075. + * vchi_msg_hold)
  12076. + *
  12077. + * Returns: int32_t - success == 0
  12078. + *
  12079. + ***********************************************************/
  12080. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  12081. +{
  12082. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  12083. + (VCHIQ_HEADER_T *)message->message);
  12084. +
  12085. + return 0;
  12086. +}
  12087. +EXPORT_SYMBOL(vchi_held_msg_release);
  12088. +
  12089. +/***********************************************************
  12090. + * Name: vchi_msg_hold
  12091. + *
  12092. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  12093. + * void **data,
  12094. + * uint32_t *msg_size,
  12095. + * VCHI_FLAGS_T flags,
  12096. + * VCHI_HELD_MSG_T *message_handle
  12097. + *
  12098. + * Description: Routine to return a pointer to the current message (to allow
  12099. + * in place processing). The message is dequeued - don't forget
  12100. + * to release the message using vchi_held_msg_release when you're
  12101. + * finished.
  12102. + *
  12103. + * Returns: int32_t - success == 0
  12104. + *
  12105. + ***********************************************************/
  12106. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  12107. + void **data,
  12108. + uint32_t *msg_size,
  12109. + VCHI_FLAGS_T flags,
  12110. + VCHI_HELD_MSG_T *message_handle)
  12111. +{
  12112. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12113. + VCHIQ_HEADER_T *header;
  12114. +
  12115. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  12116. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  12117. +
  12118. + if (flags == VCHI_FLAGS_NONE)
  12119. + if (vchiu_queue_is_empty(&service->queue))
  12120. + return -1;
  12121. +
  12122. + header = vchiu_queue_pop(&service->queue);
  12123. +
  12124. + *data = header->data;
  12125. + *msg_size = header->size;
  12126. +
  12127. + message_handle->service =
  12128. + (struct opaque_vchi_service_t *)service->handle;
  12129. + message_handle->message = header;
  12130. +
  12131. + return 0;
  12132. +}
  12133. +EXPORT_SYMBOL(vchi_msg_hold);
  12134. +
  12135. +/***********************************************************
  12136. + * Name: vchi_initialise
  12137. + *
  12138. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12139. + *
  12140. + * Description: Initialises the hardware but does not transmit anything
  12141. + * When run as a Host App this will be called twice hence the need
  12142. + * to malloc the state information
  12143. + *
  12144. + * Returns: 0 if successful, failure otherwise
  12145. + *
  12146. + ***********************************************************/
  12147. +
  12148. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  12149. +{
  12150. + VCHIQ_INSTANCE_T instance;
  12151. + VCHIQ_STATUS_T status;
  12152. +
  12153. + status = vchiq_initialise(&instance);
  12154. +
  12155. + *instance_handle = (VCHI_INSTANCE_T)instance;
  12156. +
  12157. + return vchiq_status_to_vchi(status);
  12158. +}
  12159. +EXPORT_SYMBOL(vchi_initialise);
  12160. +
  12161. +/***********************************************************
  12162. + * Name: vchi_connect
  12163. + *
  12164. + * Arguments: VCHI_CONNECTION_T **connections
  12165. + * const uint32_t num_connections
  12166. + * VCHI_INSTANCE_T instance_handle)
  12167. + *
  12168. + * Description: Starts the command service on each connection,
  12169. + * causing INIT messages to be pinged back and forth
  12170. + *
  12171. + * Returns: 0 if successful, failure otherwise
  12172. + *
  12173. + ***********************************************************/
  12174. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  12175. + const uint32_t num_connections,
  12176. + VCHI_INSTANCE_T instance_handle)
  12177. +{
  12178. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12179. +
  12180. + (void)connections;
  12181. + (void)num_connections;
  12182. +
  12183. + return vchiq_connect(instance);
  12184. +}
  12185. +EXPORT_SYMBOL(vchi_connect);
  12186. +
  12187. +
  12188. +/***********************************************************
  12189. + * Name: vchi_disconnect
  12190. + *
  12191. + * Arguments: VCHI_INSTANCE_T instance_handle
  12192. + *
  12193. + * Description: Stops the command service on each connection,
  12194. + * causing DE-INIT messages to be pinged back and forth
  12195. + *
  12196. + * Returns: 0 if successful, failure otherwise
  12197. + *
  12198. + ***********************************************************/
  12199. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  12200. +{
  12201. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12202. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  12203. +}
  12204. +EXPORT_SYMBOL(vchi_disconnect);
  12205. +
  12206. +
  12207. +/***********************************************************
  12208. + * Name: vchi_service_open
  12209. + * Name: vchi_service_create
  12210. + *
  12211. + * Arguments: VCHI_INSTANCE_T *instance_handle
  12212. + * SERVICE_CREATION_T *setup,
  12213. + * VCHI_SERVICE_HANDLE_T *handle
  12214. + *
  12215. + * Description: Routine to open a service
  12216. + *
  12217. + * Returns: int32_t - success == 0
  12218. + *
  12219. + ***********************************************************/
  12220. +
  12221. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  12222. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  12223. +{
  12224. + SHIM_SERVICE_T *service =
  12225. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  12226. +
  12227. + if (!service->callback)
  12228. + goto release;
  12229. +
  12230. + switch (reason) {
  12231. + case VCHIQ_MESSAGE_AVAILABLE:
  12232. + vchiu_queue_push(&service->queue, header);
  12233. +
  12234. + service->callback(service->callback_param,
  12235. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  12236. +
  12237. + goto done;
  12238. + break;
  12239. +
  12240. + case VCHIQ_BULK_TRANSMIT_DONE:
  12241. + service->callback(service->callback_param,
  12242. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  12243. + break;
  12244. +
  12245. + case VCHIQ_BULK_RECEIVE_DONE:
  12246. + service->callback(service->callback_param,
  12247. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  12248. + break;
  12249. +
  12250. + case VCHIQ_SERVICE_CLOSED:
  12251. + service->callback(service->callback_param,
  12252. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  12253. + break;
  12254. +
  12255. + case VCHIQ_SERVICE_OPENED:
  12256. + /* No equivalent VCHI reason */
  12257. + break;
  12258. +
  12259. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  12260. + service->callback(service->callback_param,
  12261. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  12262. + bulk_user);
  12263. + break;
  12264. +
  12265. + case VCHIQ_BULK_RECEIVE_ABORTED:
  12266. + service->callback(service->callback_param,
  12267. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  12268. + bulk_user);
  12269. + break;
  12270. +
  12271. + default:
  12272. + WARN(1, "not supported\n");
  12273. + break;
  12274. + }
  12275. +
  12276. +release:
  12277. + vchiq_release_message(service->handle, header);
  12278. +done:
  12279. + return VCHIQ_SUCCESS;
  12280. +}
  12281. +
  12282. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  12283. + SERVICE_CREATION_T *setup)
  12284. +{
  12285. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  12286. +
  12287. + (void)instance;
  12288. +
  12289. + if (service) {
  12290. + if (vchiu_queue_init(&service->queue, 64)) {
  12291. + service->callback = setup->callback;
  12292. + service->callback_param = setup->callback_param;
  12293. + } else {
  12294. + kfree(service);
  12295. + service = NULL;
  12296. + }
  12297. + }
  12298. +
  12299. + return service;
  12300. +}
  12301. +
  12302. +static void service_free(SHIM_SERVICE_T *service)
  12303. +{
  12304. + if (service) {
  12305. + vchiu_queue_delete(&service->queue);
  12306. + kfree(service);
  12307. + }
  12308. +}
  12309. +
  12310. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  12311. + SERVICE_CREATION_T *setup,
  12312. + VCHI_SERVICE_HANDLE_T *handle)
  12313. +{
  12314. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12315. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12316. + if (service) {
  12317. + VCHIQ_SERVICE_PARAMS_T params;
  12318. + VCHIQ_STATUS_T status;
  12319. +
  12320. + memset(&params, 0, sizeof(params));
  12321. + params.fourcc = setup->service_id;
  12322. + params.callback = shim_callback;
  12323. + params.userdata = service;
  12324. + params.version = setup->version.version;
  12325. + params.version_min = setup->version.version_min;
  12326. +
  12327. + status = vchiq_open_service(instance, &params,
  12328. + &service->handle);
  12329. + if (status != VCHIQ_SUCCESS) {
  12330. + service_free(service);
  12331. + service = NULL;
  12332. + }
  12333. + }
  12334. +
  12335. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12336. +
  12337. + return (service != NULL) ? 0 : -1;
  12338. +}
  12339. +EXPORT_SYMBOL(vchi_service_open);
  12340. +
  12341. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  12342. + SERVICE_CREATION_T *setup,
  12343. + VCHI_SERVICE_HANDLE_T *handle)
  12344. +{
  12345. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  12346. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  12347. + if (service) {
  12348. + VCHIQ_SERVICE_PARAMS_T params;
  12349. + VCHIQ_STATUS_T status;
  12350. +
  12351. + memset(&params, 0, sizeof(params));
  12352. + params.fourcc = setup->service_id;
  12353. + params.callback = shim_callback;
  12354. + params.userdata = service;
  12355. + params.version = setup->version.version;
  12356. + params.version_min = setup->version.version_min;
  12357. + status = vchiq_add_service(instance, &params, &service->handle);
  12358. +
  12359. + if (status != VCHIQ_SUCCESS) {
  12360. + service_free(service);
  12361. + service = NULL;
  12362. + }
  12363. + }
  12364. +
  12365. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  12366. +
  12367. + return (service != NULL) ? 0 : -1;
  12368. +}
  12369. +EXPORT_SYMBOL(vchi_service_create);
  12370. +
  12371. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  12372. +{
  12373. + int32_t ret = -1;
  12374. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12375. + if (service) {
  12376. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  12377. + if (status == VCHIQ_SUCCESS) {
  12378. + service_free(service);
  12379. + service = NULL;
  12380. + }
  12381. +
  12382. + ret = vchiq_status_to_vchi(status);
  12383. + }
  12384. + return ret;
  12385. +}
  12386. +EXPORT_SYMBOL(vchi_service_close);
  12387. +
  12388. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  12389. +{
  12390. + int32_t ret = -1;
  12391. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12392. + if (service) {
  12393. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  12394. + if (status == VCHIQ_SUCCESS) {
  12395. + service_free(service);
  12396. + service = NULL;
  12397. + }
  12398. +
  12399. + ret = vchiq_status_to_vchi(status);
  12400. + }
  12401. + return ret;
  12402. +}
  12403. +EXPORT_SYMBOL(vchi_service_destroy);
  12404. +
  12405. +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
  12406. + VCHI_SERVICE_OPTION_T option,
  12407. + int value)
  12408. +{
  12409. + int32_t ret = -1;
  12410. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12411. + VCHIQ_SERVICE_OPTION_T vchiq_option;
  12412. + switch (option) {
  12413. + case VCHI_SERVICE_OPTION_TRACE:
  12414. + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
  12415. + break;
  12416. + default:
  12417. + service = NULL;
  12418. + break;
  12419. + }
  12420. + if (service) {
  12421. + VCHIQ_STATUS_T status =
  12422. + vchiq_set_service_option(service->handle,
  12423. + vchiq_option,
  12424. + value);
  12425. +
  12426. + ret = vchiq_status_to_vchi(status);
  12427. + }
  12428. + return ret;
  12429. +}
  12430. +EXPORT_SYMBOL(vchi_service_set_option);
  12431. +
  12432. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  12433. +{
  12434. + int32_t ret = -1;
  12435. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12436. + if(service)
  12437. + {
  12438. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  12439. + ret = vchiq_status_to_vchi( status );
  12440. + }
  12441. + return ret;
  12442. +}
  12443. +EXPORT_SYMBOL(vchi_get_peer_version);
  12444. +
  12445. +/* ----------------------------------------------------------------------
  12446. + * read a uint32_t from buffer.
  12447. + * network format is defined to be little endian
  12448. + * -------------------------------------------------------------------- */
  12449. +uint32_t
  12450. +vchi_readbuf_uint32(const void *_ptr)
  12451. +{
  12452. + const unsigned char *ptr = _ptr;
  12453. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  12454. +}
  12455. +
  12456. +/* ----------------------------------------------------------------------
  12457. + * write a uint32_t to buffer.
  12458. + * network format is defined to be little endian
  12459. + * -------------------------------------------------------------------- */
  12460. +void
  12461. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  12462. +{
  12463. + unsigned char *ptr = _ptr;
  12464. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  12465. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  12466. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  12467. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  12468. +}
  12469. +
  12470. +/* ----------------------------------------------------------------------
  12471. + * read a uint16_t from buffer.
  12472. + * network format is defined to be little endian
  12473. + * -------------------------------------------------------------------- */
  12474. +uint16_t
  12475. +vchi_readbuf_uint16(const void *_ptr)
  12476. +{
  12477. + const unsigned char *ptr = _ptr;
  12478. + return ptr[0] | (ptr[1] << 8);
  12479. +}
  12480. +
  12481. +/* ----------------------------------------------------------------------
  12482. + * write a uint16_t into the buffer.
  12483. + * network format is defined to be little endian
  12484. + * -------------------------------------------------------------------- */
  12485. +void
  12486. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  12487. +{
  12488. + unsigned char *ptr = _ptr;
  12489. + ptr[0] = (value >> 0) & 0xFF;
  12490. + ptr[1] = (value >> 8) & 0xFF;
  12491. +}
  12492. +
  12493. +/***********************************************************
  12494. + * Name: vchi_service_use
  12495. + *
  12496. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12497. + *
  12498. + * Description: Routine to increment refcount on a service
  12499. + *
  12500. + * Returns: void
  12501. + *
  12502. + ***********************************************************/
  12503. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  12504. +{
  12505. + int32_t ret = -1;
  12506. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12507. + if (service)
  12508. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  12509. + return ret;
  12510. +}
  12511. +EXPORT_SYMBOL(vchi_service_use);
  12512. +
  12513. +/***********************************************************
  12514. + * Name: vchi_service_release
  12515. + *
  12516. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  12517. + *
  12518. + * Description: Routine to decrement refcount on a service
  12519. + *
  12520. + * Returns: void
  12521. + *
  12522. + ***********************************************************/
  12523. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  12524. +{
  12525. + int32_t ret = -1;
  12526. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  12527. + if (service)
  12528. + ret = vchiq_status_to_vchi(
  12529. + vchiq_release_service(service->handle));
  12530. + return ret;
  12531. +}
  12532. +EXPORT_SYMBOL(vchi_service_release);
  12533. --- /dev/null
  12534. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  12535. @@ -0,0 +1,151 @@
  12536. +/**
  12537. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  12538. + *
  12539. + * Redistribution and use in source and binary forms, with or without
  12540. + * modification, are permitted provided that the following conditions
  12541. + * are met:
  12542. + * 1. Redistributions of source code must retain the above copyright
  12543. + * notice, this list of conditions, and the following disclaimer,
  12544. + * without modification.
  12545. + * 2. Redistributions in binary form must reproduce the above copyright
  12546. + * notice, this list of conditions and the following disclaimer in the
  12547. + * documentation and/or other materials provided with the distribution.
  12548. + * 3. The names of the above-listed copyright holders may not be used
  12549. + * to endorse or promote products derived from this software without
  12550. + * specific prior written permission.
  12551. + *
  12552. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12553. + * GNU General Public License ("GPL") version 2, as published by the Free
  12554. + * Software Foundation.
  12555. + *
  12556. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12557. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12558. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12559. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12560. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12561. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12562. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12563. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12564. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12565. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12566. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12567. + */
  12568. +
  12569. +#include "vchiq_util.h"
  12570. +
  12571. +static inline int is_pow2(int i)
  12572. +{
  12573. + return i && !(i & (i - 1));
  12574. +}
  12575. +
  12576. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  12577. +{
  12578. + WARN_ON(!is_pow2(size));
  12579. +
  12580. + queue->size = size;
  12581. + queue->read = 0;
  12582. + queue->write = 0;
  12583. +
  12584. + sema_init(&queue->pop, 0);
  12585. + sema_init(&queue->push, 0);
  12586. +
  12587. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  12588. + if (queue->storage == NULL) {
  12589. + vchiu_queue_delete(queue);
  12590. + return 0;
  12591. + }
  12592. + return 1;
  12593. +}
  12594. +
  12595. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  12596. +{
  12597. + if (queue->storage != NULL)
  12598. + kfree(queue->storage);
  12599. +}
  12600. +
  12601. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  12602. +{
  12603. + return queue->read == queue->write;
  12604. +}
  12605. +
  12606. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  12607. +{
  12608. + return queue->write == queue->read + queue->size;
  12609. +}
  12610. +
  12611. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  12612. +{
  12613. + while (queue->write == queue->read + queue->size) {
  12614. + if (down_interruptible(&queue->pop) != 0) {
  12615. + flush_signals(current);
  12616. + }
  12617. + }
  12618. +
  12619. + /*
  12620. + * Write to queue->storage must be visible after read from
  12621. + * queue->read
  12622. + */
  12623. + smp_mb();
  12624. +
  12625. + queue->storage[queue->write & (queue->size - 1)] = header;
  12626. +
  12627. + /*
  12628. + * Write to queue->storage must be visible before write to
  12629. + * queue->write
  12630. + */
  12631. + smp_wmb();
  12632. +
  12633. + queue->write++;
  12634. +
  12635. + up(&queue->push);
  12636. +}
  12637. +
  12638. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  12639. +{
  12640. + while (queue->write == queue->read) {
  12641. + if (down_interruptible(&queue->push) != 0) {
  12642. + flush_signals(current);
  12643. + }
  12644. + }
  12645. +
  12646. + up(&queue->push); // We haven't removed anything from the queue.
  12647. +
  12648. + /*
  12649. + * Read from queue->storage must be visible after read from
  12650. + * queue->write
  12651. + */
  12652. + smp_rmb();
  12653. +
  12654. + return queue->storage[queue->read & (queue->size - 1)];
  12655. +}
  12656. +
  12657. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  12658. +{
  12659. + VCHIQ_HEADER_T *header;
  12660. +
  12661. + while (queue->write == queue->read) {
  12662. + if (down_interruptible(&queue->push) != 0) {
  12663. + flush_signals(current);
  12664. + }
  12665. + }
  12666. +
  12667. + /*
  12668. + * Read from queue->storage must be visible after read from
  12669. + * queue->write
  12670. + */
  12671. + smp_rmb();
  12672. +
  12673. + header = queue->storage[queue->read & (queue->size - 1)];
  12674. +
  12675. + /*
  12676. + * Read from queue->storage must be visible before write to
  12677. + * queue->read
  12678. + */
  12679. + smp_mb();
  12680. +
  12681. + queue->read++;
  12682. +
  12683. + up(&queue->pop);
  12684. +
  12685. + return header;
  12686. +}
  12687. --- /dev/null
  12688. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  12689. @@ -0,0 +1,81 @@
  12690. +/**
  12691. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  12692. + *
  12693. + * Redistribution and use in source and binary forms, with or without
  12694. + * modification, are permitted provided that the following conditions
  12695. + * are met:
  12696. + * 1. Redistributions of source code must retain the above copyright
  12697. + * notice, this list of conditions, and the following disclaimer,
  12698. + * without modification.
  12699. + * 2. Redistributions in binary form must reproduce the above copyright
  12700. + * notice, this list of conditions and the following disclaimer in the
  12701. + * documentation and/or other materials provided with the distribution.
  12702. + * 3. The names of the above-listed copyright holders may not be used
  12703. + * to endorse or promote products derived from this software without
  12704. + * specific prior written permission.
  12705. + *
  12706. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12707. + * GNU General Public License ("GPL") version 2, as published by the Free
  12708. + * Software Foundation.
  12709. + *
  12710. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12711. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12712. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12713. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12714. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12715. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12716. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12717. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12718. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12719. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12720. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12721. + */
  12722. +
  12723. +#ifndef VCHIQ_UTIL_H
  12724. +#define VCHIQ_UTIL_H
  12725. +
  12726. +#include <linux/types.h>
  12727. +#include <linux/semaphore.h>
  12728. +#include <linux/mutex.h>
  12729. +#include <linux/bitops.h>
  12730. +#include <linux/kthread.h>
  12731. +#include <linux/wait.h>
  12732. +#include <linux/vmalloc.h>
  12733. +#include <linux/jiffies.h>
  12734. +#include <linux/delay.h>
  12735. +#include <linux/string.h>
  12736. +#include <linux/types.h>
  12737. +#include <linux/interrupt.h>
  12738. +#include <linux/random.h>
  12739. +#include <linux/sched.h>
  12740. +#include <linux/ctype.h>
  12741. +#include <linux/uaccess.h>
  12742. +#include <linux/time.h> /* for time_t */
  12743. +#include <linux/slab.h>
  12744. +#include <linux/vmalloc.h>
  12745. +
  12746. +#include "vchiq_if.h"
  12747. +
  12748. +typedef struct {
  12749. + int size;
  12750. + int read;
  12751. + int write;
  12752. +
  12753. + struct semaphore pop;
  12754. + struct semaphore push;
  12755. +
  12756. + VCHIQ_HEADER_T **storage;
  12757. +} VCHIU_QUEUE_T;
  12758. +
  12759. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  12760. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  12761. +
  12762. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  12763. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  12764. +
  12765. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  12766. +
  12767. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  12768. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  12769. +
  12770. +#endif
  12771. --- /dev/null
  12772. +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  12773. @@ -0,0 +1,59 @@
  12774. +/**
  12775. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  12776. + *
  12777. + * Redistribution and use in source and binary forms, with or without
  12778. + * modification, are permitted provided that the following conditions
  12779. + * are met:
  12780. + * 1. Redistributions of source code must retain the above copyright
  12781. + * notice, this list of conditions, and the following disclaimer,
  12782. + * without modification.
  12783. + * 2. Redistributions in binary form must reproduce the above copyright
  12784. + * notice, this list of conditions and the following disclaimer in the
  12785. + * documentation and/or other materials provided with the distribution.
  12786. + * 3. The names of the above-listed copyright holders may not be used
  12787. + * to endorse or promote products derived from this software without
  12788. + * specific prior written permission.
  12789. + *
  12790. + * ALTERNATIVELY, this software may be distributed under the terms of the
  12791. + * GNU General Public License ("GPL") version 2, as published by the Free
  12792. + * Software Foundation.
  12793. + *
  12794. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  12795. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  12796. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  12797. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  12798. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12799. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12800. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12801. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  12802. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  12803. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  12804. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  12805. + */
  12806. +#include "vchiq_build_info.h"
  12807. +#include <linux/broadcom/vc_debug_sym.h>
  12808. +
  12809. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  12810. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  12811. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  12812. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  12813. +
  12814. +const char *vchiq_get_build_hostname( void )
  12815. +{
  12816. + return vchiq_build_hostname;
  12817. +}
  12818. +
  12819. +const char *vchiq_get_build_version( void )
  12820. +{
  12821. + return vchiq_build_version;
  12822. +}
  12823. +
  12824. +const char *vchiq_get_build_date( void )
  12825. +{
  12826. + return vchiq_build_date;
  12827. +}
  12828. +
  12829. +const char *vchiq_get_build_time( void )
  12830. +{
  12831. + return vchiq_build_time;
  12832. +}