cam_isp_context.c 176 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/debugfs.h>
  6. #include <linux/videodev2.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #include <linux/ratelimit.h>
  10. #include "cam_mem_mgr.h"
  11. #include "cam_sync_api.h"
  12. #include "cam_req_mgr_dev.h"
  13. #include "cam_trace.h"
  14. #include "cam_debug_util.h"
  15. #include "cam_packet_util.h"
  16. #include "cam_context_utils.h"
  17. #include "cam_cdm_util.h"
  18. #include "cam_isp_context.h"
  19. #include "cam_common_util.h"
  20. #include "cam_req_mgr_debug.h"
  21. #include "cam_cpas_api.h"
  22. static const char isp_dev_name[] = "cam-isp";
  23. static struct cam_isp_ctx_debug isp_ctx_debug;
  24. #define INC_HEAD(head, max_entries, ret) \
  25. div_u64_rem(atomic64_add_return(1, head),\
  26. max_entries, (ret))
  27. static int cam_isp_context_dump_requests(void *data,
  28. struct cam_smmu_pf_info *pf_info);
  29. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  30. struct cam_start_stop_dev_cmd *cmd);
  31. static const char *__cam_isp_evt_val_to_type(
  32. uint32_t evt_id)
  33. {
  34. switch (evt_id) {
  35. case CAM_ISP_CTX_EVENT_SUBMIT:
  36. return "SUBMIT";
  37. case CAM_ISP_CTX_EVENT_APPLY:
  38. return "APPLY";
  39. case CAM_ISP_CTX_EVENT_EPOCH:
  40. return "EPOCH";
  41. case CAM_ISP_CTX_EVENT_RUP:
  42. return "RUP";
  43. case CAM_ISP_CTX_EVENT_BUFDONE:
  44. return "BUFDONE";
  45. default:
  46. return "CAM_ISP_EVENT_INVALID";
  47. }
  48. }
  49. static void __cam_isp_ctx_update_event_record(
  50. struct cam_isp_context *ctx_isp,
  51. enum cam_isp_ctx_event event,
  52. struct cam_ctx_request *req)
  53. {
  54. int iterator = 0;
  55. ktime_t cur_time;
  56. struct cam_isp_ctx_req *req_isp;
  57. if (!ctx_isp) {
  58. CAM_ERR(CAM_ISP, "Invalid Args");
  59. return;
  60. }
  61. switch (event) {
  62. case CAM_ISP_CTX_EVENT_EPOCH:
  63. case CAM_ISP_CTX_EVENT_RUP:
  64. case CAM_ISP_CTX_EVENT_BUFDONE:
  65. break;
  66. case CAM_ISP_CTX_EVENT_SUBMIT:
  67. case CAM_ISP_CTX_EVENT_APPLY:
  68. if (!req) {
  69. CAM_ERR(CAM_ISP, "Invalid arg for event %d", event);
  70. return;
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. INC_HEAD(&ctx_isp->event_record_head[event],
  77. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES, &iterator);
  78. cur_time = ktime_get();
  79. if (req) {
  80. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  81. ctx_isp->event_record[event][iterator].req_id =
  82. req->request_id;
  83. req_isp->event_timestamp[event] = cur_time;
  84. } else {
  85. ctx_isp->event_record[event][iterator].req_id = 0;
  86. }
  87. ctx_isp->event_record[event][iterator].timestamp = cur_time;
  88. }
  89. static int __cam_isp_ctx_dump_event_record(
  90. struct cam_isp_context *ctx_isp,
  91. uintptr_t cpu_addr,
  92. size_t buf_len,
  93. size_t *offset)
  94. {
  95. int i, j;
  96. int index;
  97. size_t remain_len;
  98. uint8_t *dst;
  99. uint32_t oldest_entry, num_entries;
  100. uint32_t min_len;
  101. uint64_t *addr, *start;
  102. uint64_t state_head;
  103. struct timespec64 ts;
  104. struct cam_isp_context_dump_header *hdr;
  105. struct cam_isp_context_event_record *record;
  106. if (!cpu_addr || !buf_len || !offset || !ctx_isp) {
  107. CAM_ERR(CAM_ISP, "Invalid args %pK %zu %pK %pK",
  108. cpu_addr, buf_len, offset, ctx_isp);
  109. return -EINVAL;
  110. }
  111. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  112. state_head = atomic64_read(&ctx_isp->event_record_head[i]);
  113. if (state_head == -1) {
  114. return 0;
  115. } else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  116. num_entries = state_head + 1;
  117. oldest_entry = 0;
  118. } else {
  119. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  120. div_u64_rem(state_head + 1,
  121. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  122. &oldest_entry);
  123. }
  124. index = oldest_entry;
  125. if (buf_len <= *offset) {
  126. CAM_WARN(CAM_ISP,
  127. "Dump buffer overshoot len %zu offset %zu",
  128. buf_len, *offset);
  129. return -ENOSPC;
  130. }
  131. min_len = sizeof(struct cam_isp_context_dump_header) +
  132. ((num_entries * CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS) *
  133. sizeof(uint64_t));
  134. remain_len = buf_len - *offset;
  135. if (remain_len < min_len) {
  136. CAM_WARN(CAM_ISP,
  137. "Dump buffer exhaust remain %zu min %u",
  138. remain_len, min_len);
  139. return -ENOSPC;
  140. }
  141. dst = (uint8_t *)cpu_addr + *offset;
  142. hdr = (struct cam_isp_context_dump_header *)dst;
  143. scnprintf(hdr->tag,
  144. CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN, "ISP_EVT_%s:",
  145. __cam_isp_evt_val_to_type(i));
  146. hdr->word_size = sizeof(uint64_t);
  147. addr = (uint64_t *)(dst +
  148. sizeof(struct cam_isp_context_dump_header));
  149. start = addr;
  150. for (j = 0; j < num_entries; j++) {
  151. record = &ctx_isp->event_record[i][index];
  152. ts = ktime_to_timespec64(record->timestamp);
  153. *addr++ = record->req_id;
  154. *addr++ = ts.tv_sec;
  155. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  156. index = (index + 1) %
  157. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  158. }
  159. hdr->size = hdr->word_size * (addr - start);
  160. *offset += hdr->size +
  161. sizeof(struct cam_isp_context_dump_header);
  162. }
  163. return 0;
  164. }
  165. static void __cam_isp_ctx_update_state_monitor_array(
  166. struct cam_isp_context *ctx_isp,
  167. enum cam_isp_state_change_trigger trigger_type,
  168. uint64_t req_id)
  169. {
  170. int iterator;
  171. INC_HEAD(&ctx_isp->state_monitor_head,
  172. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &iterator);
  173. ctx_isp->cam_isp_ctx_state_monitor[iterator].curr_state =
  174. ctx_isp->substate_activated;
  175. ctx_isp->cam_isp_ctx_state_monitor[iterator].frame_id =
  176. ctx_isp->frame_id;
  177. ctx_isp->cam_isp_ctx_state_monitor[iterator].trigger =
  178. trigger_type;
  179. ctx_isp->cam_isp_ctx_state_monitor[iterator].req_id =
  180. req_id;
  181. ctx_isp->cam_isp_ctx_state_monitor[iterator].evt_time_stamp =
  182. jiffies_to_msecs(jiffies) - ctx_isp->init_timestamp;
  183. }
  184. static const char *__cam_isp_ctx_substate_val_to_type(
  185. enum cam_isp_ctx_activated_substate type)
  186. {
  187. switch (type) {
  188. case CAM_ISP_CTX_ACTIVATED_SOF:
  189. return "SOF";
  190. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  191. return "APPLIED";
  192. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  193. return "EPOCH";
  194. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  195. return "BUBBLE";
  196. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  197. return "BUBBLE_APPLIED";
  198. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  199. return "HW_ERROR";
  200. case CAM_ISP_CTX_ACTIVATED_HALT:
  201. return "HALT";
  202. default:
  203. return "INVALID";
  204. }
  205. }
  206. static const char *__cam_isp_hw_evt_val_to_type(
  207. uint32_t evt_id)
  208. {
  209. switch (evt_id) {
  210. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  211. return "ERROR";
  212. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  213. return "APPLIED";
  214. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  215. return "SOF";
  216. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  217. return "REG_UPDATE";
  218. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  219. return "EPOCH";
  220. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  221. return "EOF";
  222. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  223. return "DONE";
  224. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  225. return "FLUSH";
  226. default:
  227. return "CAM_ISP_EVENT_INVALID";
  228. }
  229. }
  230. static void __cam_isp_ctx_dump_state_monitor_array(
  231. struct cam_isp_context *ctx_isp)
  232. {
  233. int i = 0;
  234. int64_t state_head = 0;
  235. uint32_t index, num_entries, oldest_entry;
  236. state_head = atomic64_read(&ctx_isp->state_monitor_head);
  237. if (state_head == -1) {
  238. return;
  239. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  240. num_entries = state_head;
  241. oldest_entry = 0;
  242. } else {
  243. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  244. div_u64_rem(state_head + 1,
  245. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &oldest_entry);
  246. }
  247. CAM_ERR(CAM_ISP,
  248. "Dumping state information for preceding requests");
  249. index = oldest_entry;
  250. for (i = 0; i < num_entries; i++) {
  251. CAM_ERR(CAM_ISP,
  252. "Index[%d] time[%d] : Substate[%s] Frame[%lld] ReqId[%llu] evt_type[%s]",
  253. index,
  254. ctx_isp->cam_isp_ctx_state_monitor[index].evt_time_stamp,
  255. __cam_isp_ctx_substate_val_to_type(
  256. ctx_isp->cam_isp_ctx_state_monitor[index].curr_state),
  257. ctx_isp->cam_isp_ctx_state_monitor[index].frame_id,
  258. ctx_isp->cam_isp_ctx_state_monitor[index].req_id,
  259. __cam_isp_hw_evt_val_to_type(
  260. ctx_isp->cam_isp_ctx_state_monitor[index].trigger));
  261. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  262. }
  263. }
  264. static int cam_isp_context_info_dump(void *context,
  265. enum cam_context_dump_id id)
  266. {
  267. struct cam_context *ctx = (struct cam_context *)context;
  268. switch (id) {
  269. case CAM_CTX_DUMP_ACQ_INFO: {
  270. cam_context_dump_hw_acq_info(ctx);
  271. break;
  272. }
  273. default:
  274. CAM_DBG(CAM_ISP, "DUMP id not valid %u", id);
  275. break;
  276. }
  277. return 0;
  278. }
  279. static int cam_isp_ctx_dump_req(
  280. struct cam_isp_ctx_req *req_isp,
  281. uintptr_t cpu_addr,
  282. size_t buf_len,
  283. size_t *offset,
  284. bool dump_to_buff)
  285. {
  286. int i, rc = 0;
  287. size_t len = 0;
  288. uint32_t *buf_addr;
  289. uint32_t *buf_start, *buf_end;
  290. size_t remain_len = 0;
  291. struct cam_cdm_cmd_buf_dump_info dump_info;
  292. for (i = 0; i < req_isp->num_cfg; i++) {
  293. rc = cam_packet_util_get_cmd_mem_addr(
  294. req_isp->cfg[i].handle, &buf_addr, &len);
  295. if (rc) {
  296. CAM_ERR_RATE_LIMIT(CAM_ISP,
  297. "Failed to get_cmd_mem_addr, rc=%d",
  298. rc);
  299. } else {
  300. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  301. CAM_ERR(CAM_ISP,
  302. "Invalid offset exp %u actual %u",
  303. req_isp->cfg[i].offset, (uint32_t)len);
  304. return -EINVAL;
  305. }
  306. remain_len = len - req_isp->cfg[i].offset;
  307. if (req_isp->cfg[i].len >
  308. ((uint32_t)remain_len)) {
  309. CAM_ERR(CAM_ISP,
  310. "Invalid len exp %u remain_len %u",
  311. req_isp->cfg[i].len,
  312. (uint32_t)remain_len);
  313. return -EINVAL;
  314. }
  315. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  316. req_isp->cfg[i].offset);
  317. buf_end = (uint32_t *)((uint8_t *) buf_start +
  318. req_isp->cfg[i].len - 1);
  319. if (dump_to_buff) {
  320. if (!cpu_addr || !offset || !buf_len) {
  321. CAM_ERR(CAM_ISP, "Invalid args");
  322. break;
  323. }
  324. dump_info.src_start = buf_start;
  325. dump_info.src_end = buf_end;
  326. dump_info.dst_start = cpu_addr;
  327. dump_info.dst_offset = *offset;
  328. dump_info.dst_max_size = buf_len;
  329. rc = cam_cdm_util_dump_cmd_bufs_v2(
  330. &dump_info);
  331. *offset = dump_info.dst_offset;
  332. if (rc)
  333. return rc;
  334. } else
  335. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  336. }
  337. }
  338. return rc;
  339. }
  340. static int __cam_isp_ctx_enqueue_request_in_order(
  341. struct cam_context *ctx, struct cam_ctx_request *req)
  342. {
  343. struct cam_ctx_request *req_current;
  344. struct cam_ctx_request *req_prev;
  345. struct list_head temp_list;
  346. struct cam_isp_context *ctx_isp;
  347. INIT_LIST_HEAD(&temp_list);
  348. spin_lock_bh(&ctx->lock);
  349. if (list_empty(&ctx->pending_req_list)) {
  350. list_add_tail(&req->list, &ctx->pending_req_list);
  351. } else {
  352. list_for_each_entry_safe_reverse(
  353. req_current, req_prev, &ctx->pending_req_list, list) {
  354. if (req->request_id < req_current->request_id) {
  355. list_del_init(&req_current->list);
  356. list_add(&req_current->list, &temp_list);
  357. continue;
  358. } else if (req->request_id == req_current->request_id) {
  359. CAM_WARN(CAM_ISP,
  360. "Received duplicated request %lld",
  361. req->request_id);
  362. }
  363. break;
  364. }
  365. list_add_tail(&req->list, &ctx->pending_req_list);
  366. if (!list_empty(&temp_list)) {
  367. list_for_each_entry_safe(
  368. req_current, req_prev, &temp_list, list) {
  369. list_del_init(&req_current->list);
  370. list_add_tail(&req_current->list,
  371. &ctx->pending_req_list);
  372. }
  373. }
  374. }
  375. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  376. __cam_isp_ctx_update_event_record(ctx_isp,
  377. CAM_ISP_CTX_EVENT_SUBMIT, req);
  378. spin_unlock_bh(&ctx->lock);
  379. return 0;
  380. }
  381. static int __cam_isp_ctx_enqueue_init_request(
  382. struct cam_context *ctx, struct cam_ctx_request *req)
  383. {
  384. int rc = 0;
  385. struct cam_ctx_request *req_old;
  386. struct cam_isp_ctx_req *req_isp_old;
  387. struct cam_isp_ctx_req *req_isp_new;
  388. struct cam_isp_prepare_hw_update_data *req_update_old;
  389. struct cam_isp_prepare_hw_update_data *req_update_new;
  390. struct cam_isp_prepare_hw_update_data *hw_update_data;
  391. spin_lock_bh(&ctx->lock);
  392. if (list_empty(&ctx->pending_req_list)) {
  393. list_add_tail(&req->list, &ctx->pending_req_list);
  394. CAM_DBG(CAM_ISP, "INIT packet added req id= %d",
  395. req->request_id);
  396. goto end;
  397. }
  398. req_old = list_first_entry(&ctx->pending_req_list,
  399. struct cam_ctx_request, list);
  400. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  401. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  402. if (req_isp_old->hw_update_data.packet_opcode_type ==
  403. CAM_ISP_PACKET_INIT_DEV) {
  404. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  405. ctx->max_hw_update_entries) {
  406. CAM_WARN(CAM_ISP,
  407. "Can not merge INIT pkt num_cfgs = %d",
  408. (req_isp_old->num_cfg +
  409. req_isp_new->num_cfg));
  410. rc = -ENOMEM;
  411. }
  412. if (req_isp_old->num_fence_map_out != 0 ||
  413. req_isp_old->num_fence_map_in != 0) {
  414. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence");
  415. rc = -EINVAL;
  416. }
  417. if (!rc) {
  418. memcpy(req_isp_old->fence_map_out,
  419. req_isp_new->fence_map_out,
  420. sizeof(req_isp_new->fence_map_out[0])*
  421. req_isp_new->num_fence_map_out);
  422. req_isp_old->num_fence_map_out =
  423. req_isp_new->num_fence_map_out;
  424. memcpy(req_isp_old->fence_map_in,
  425. req_isp_new->fence_map_in,
  426. sizeof(req_isp_new->fence_map_in[0])*
  427. req_isp_new->num_fence_map_in);
  428. req_isp_old->num_fence_map_in =
  429. req_isp_new->num_fence_map_in;
  430. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  431. req_isp_new->cfg,
  432. sizeof(req_isp_new->cfg[0]) *
  433. req_isp_new->num_cfg);
  434. req_isp_old->num_cfg += req_isp_new->num_cfg;
  435. memcpy(&req_old->pf_data, &req->pf_data,
  436. sizeof(struct cam_hw_mgr_dump_pf_data));
  437. if (req_isp_new->hw_update_data.num_reg_dump_buf) {
  438. req_update_new = &req_isp_new->hw_update_data;
  439. req_update_old = &req_isp_old->hw_update_data;
  440. memcpy(&req_update_old->reg_dump_buf_desc,
  441. &req_update_new->reg_dump_buf_desc,
  442. sizeof(struct cam_cmd_buf_desc) *
  443. req_update_new->num_reg_dump_buf);
  444. req_update_old->num_reg_dump_buf =
  445. req_update_new->num_reg_dump_buf;
  446. }
  447. /* Update frame header params for EPCR */
  448. hw_update_data = &req_isp_new->hw_update_data;
  449. req_isp_old->hw_update_data.frame_header_res_id =
  450. req_isp_new->hw_update_data.frame_header_res_id;
  451. req_isp_old->hw_update_data.frame_header_cpu_addr =
  452. hw_update_data->frame_header_cpu_addr;
  453. req_old->request_id = req->request_id;
  454. list_add_tail(&req->list, &ctx->free_req_list);
  455. }
  456. } else {
  457. CAM_WARN(CAM_ISP,
  458. "Received Update pkt before INIT pkt. req_id= %lld",
  459. req->request_id);
  460. rc = -EINVAL;
  461. }
  462. end:
  463. spin_unlock_bh(&ctx->lock);
  464. return rc;
  465. }
  466. static const char *__cam_isp_resource_handle_id_to_type(
  467. uint32_t resource_handle)
  468. {
  469. switch (resource_handle) {
  470. case CAM_ISP_IFE_OUT_RES_FULL:
  471. return "FULL";
  472. case CAM_ISP_IFE_OUT_RES_DS4:
  473. return "DS4";
  474. case CAM_ISP_IFE_OUT_RES_DS16:
  475. return "DS16";
  476. case CAM_ISP_IFE_OUT_RES_RAW_DUMP:
  477. return "RAW_DUMP";
  478. case CAM_ISP_IFE_OUT_RES_FD:
  479. return "FD";
  480. case CAM_ISP_IFE_OUT_RES_PDAF:
  481. return "PDAF";
  482. case CAM_ISP_IFE_OUT_RES_RDI_0:
  483. return "RDI_0";
  484. case CAM_ISP_IFE_OUT_RES_RDI_1:
  485. return "RDI_1";
  486. case CAM_ISP_IFE_OUT_RES_RDI_2:
  487. return "RDI_2";
  488. case CAM_ISP_IFE_OUT_RES_RDI_3:
  489. return "RDI_3";
  490. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE:
  491. return "STATS_HDR_BE";
  492. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST:
  493. return "STATS_HDR_BHIST";
  494. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG:
  495. return "STATS_TL_BG";
  496. case CAM_ISP_IFE_OUT_RES_STATS_BF:
  497. return "STATS_BF";
  498. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG:
  499. return "STATS_AWB_BG";
  500. case CAM_ISP_IFE_OUT_RES_STATS_BHIST:
  501. return "STATS_BHIST";
  502. case CAM_ISP_IFE_OUT_RES_STATS_RS:
  503. return "STATS_RS";
  504. case CAM_ISP_IFE_OUT_RES_STATS_CS:
  505. return "STATS_CS";
  506. case CAM_ISP_IFE_OUT_RES_STATS_IHIST:
  507. return "STATS_IHIST";
  508. case CAM_ISP_IFE_OUT_RES_FULL_DISP:
  509. return "FULL_DISP";
  510. case CAM_ISP_IFE_OUT_RES_DS4_DISP:
  511. return "DS4_DISP";
  512. case CAM_ISP_IFE_OUT_RES_DS16_DISP:
  513. return "DS16_DISP";
  514. case CAM_ISP_IFE_OUT_RES_2PD:
  515. return "2PD";
  516. case CAM_ISP_IFE_OUT_RES_RDI_RD:
  517. return "RDI_RD";
  518. case CAM_ISP_IFE_OUT_RES_LCR:
  519. return "LCR";
  520. case CAM_ISP_IFE_OUT_RES_AWB_BFW:
  521. return "AWB_BFW";
  522. case CAM_ISP_IFE_OUT_RES_PREPROCESS_2PD:
  523. return "PREPROCESS_2PD";
  524. case CAM_ISP_IFE_OUT_RES_STATS_AEC_BE:
  525. return "STATS_AEC_BE";
  526. case CAM_ISP_IFE_OUT_RES_LTM_STATS:
  527. return "LTM_STATS";
  528. case CAM_ISP_IFE_OUT_RES_STATS_GTM_BHIST:
  529. return "STATS_GTM_BHIST";
  530. case CAM_ISP_IFE_LITE_OUT_RES_STATS_BG:
  531. return "STATS_BG";
  532. case CAM_ISP_IFE_LITE_OUT_RES_PREPROCESS_RAW:
  533. return "PREPROCESS_RAW";
  534. case CAM_ISP_IFE_OUT_RES_SPARSE_PD:
  535. return "SPARSE_PD";
  536. default:
  537. return "CAM_ISP_Invalid_Resource_Type";
  538. }
  539. }
  540. static const char *__cam_isp_tfe_resource_handle_id_to_type(
  541. uint32_t resource_handle)
  542. {
  543. switch (resource_handle) {
  544. case CAM_ISP_TFE_OUT_RES_FULL:
  545. return "FULL";
  546. case CAM_ISP_TFE_OUT_RES_RAW_DUMP:
  547. return "RAW_DUMP";
  548. case CAM_ISP_TFE_OUT_RES_PDAF:
  549. return "PDAF";
  550. case CAM_ISP_TFE_OUT_RES_RDI_0:
  551. return "RDI_0";
  552. case CAM_ISP_TFE_OUT_RES_RDI_1:
  553. return "RDI_1";
  554. case CAM_ISP_TFE_OUT_RES_RDI_2:
  555. return "RDI_2";
  556. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BE:
  557. return "STATS_HDR_BE";
  558. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BHIST:
  559. return "STATS_HDR_BHIST";
  560. case CAM_ISP_TFE_OUT_RES_STATS_TL_BG:
  561. return "STATS_TL_BG";
  562. case CAM_ISP_TFE_OUT_RES_STATS_BF:
  563. return "STATS_BF";
  564. case CAM_ISP_TFE_OUT_RES_STATS_AWB_BG:
  565. return "STATS_AWB_BG";
  566. case CAM_ISP_TFE_OUT_RES_STATS_RS:
  567. return "STATS_RS";
  568. case CAM_ISP_TFE_OUT_RES_DS4:
  569. return "DS_4";
  570. case CAM_ISP_TFE_OUT_RES_DS16:
  571. return "DS_16";
  572. case CAM_ISP_TFE_OUT_RES_AI:
  573. return "AI";
  574. default:
  575. return "CAM_ISP_Invalid_Resource_Type";
  576. }
  577. }
  578. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  579. {
  580. uint64_t ts = 0;
  581. if (!evt_data)
  582. return 0;
  583. switch (evt_id) {
  584. case CAM_ISP_HW_EVENT_ERROR:
  585. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  586. timestamp;
  587. break;
  588. case CAM_ISP_HW_EVENT_SOF:
  589. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  590. timestamp;
  591. break;
  592. case CAM_ISP_HW_EVENT_REG_UPDATE:
  593. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  594. timestamp;
  595. break;
  596. case CAM_ISP_HW_EVENT_EPOCH:
  597. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  598. timestamp;
  599. break;
  600. case CAM_ISP_HW_EVENT_EOF:
  601. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  602. timestamp;
  603. break;
  604. case CAM_ISP_HW_EVENT_DONE:
  605. break;
  606. default:
  607. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  608. }
  609. return ts;
  610. }
  611. static void __cam_isp_ctx_send_sof_boot_timestamp(
  612. struct cam_isp_context *ctx_isp, uint64_t request_id,
  613. uint32_t sof_event_status)
  614. {
  615. struct cam_req_mgr_message req_msg;
  616. req_msg.session_hdl = ctx_isp->base->session_hdl;
  617. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  618. req_msg.u.frame_msg.request_id = request_id;
  619. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  620. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  621. req_msg.u.frame_msg.sof_status = sof_event_status;
  622. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  623. CAM_DBG(CAM_ISP,
  624. "request id:%lld frame number:%lld boot time stamp:0x%llx status:%u",
  625. request_id, ctx_isp->frame_id,
  626. ctx_isp->boot_timestamp, sof_event_status);
  627. if (cam_req_mgr_notify_message(&req_msg,
  628. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  629. V4L_EVENT_CAM_REQ_MGR_EVENT))
  630. CAM_ERR(CAM_ISP,
  631. "Error in notifying the boot time for req id:%lld",
  632. request_id);
  633. }
  634. static void __cam_isp_ctx_send_sof_timestamp_frame_header(
  635. struct cam_isp_context *ctx_isp, uint32_t *frame_header_cpu_addr,
  636. uint64_t request_id, uint32_t sof_event_status)
  637. {
  638. uint32_t *time32 = NULL;
  639. uint64_t timestamp = 0;
  640. struct cam_req_mgr_message req_msg;
  641. time32 = frame_header_cpu_addr;
  642. timestamp = (uint64_t) time32[1];
  643. timestamp = timestamp << 24;
  644. timestamp |= (uint64_t)(time32[0] >> 8);
  645. timestamp = mul_u64_u32_div(timestamp,
  646. CAM_IFE_QTIMER_MUL_FACTOR,
  647. CAM_IFE_QTIMER_DIV_FACTOR);
  648. ctx_isp->sof_timestamp_val = timestamp;
  649. req_msg.session_hdl = ctx_isp->base->session_hdl;
  650. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  651. req_msg.u.frame_msg.request_id = request_id;
  652. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  653. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  654. req_msg.u.frame_msg.sof_status = sof_event_status;
  655. CAM_DBG(CAM_ISP,
  656. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  657. request_id, ctx_isp->frame_id,
  658. ctx_isp->sof_timestamp_val, sof_event_status);
  659. if (cam_req_mgr_notify_message(&req_msg,
  660. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  661. CAM_ERR(CAM_ISP,
  662. "Error in notifying the sof time for req id:%lld",
  663. request_id);
  664. }
  665. static void __cam_isp_ctx_send_sof_timestamp(
  666. struct cam_isp_context *ctx_isp, uint64_t request_id,
  667. uint32_t sof_event_status)
  668. {
  669. struct cam_req_mgr_message req_msg;
  670. if ((ctx_isp->use_frame_header_ts) || (request_id == 0))
  671. goto end;
  672. req_msg.session_hdl = ctx_isp->base->session_hdl;
  673. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  674. req_msg.u.frame_msg.request_id = request_id;
  675. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  676. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  677. req_msg.u.frame_msg.sof_status = sof_event_status;
  678. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  679. CAM_DBG(CAM_ISP,
  680. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  681. request_id, ctx_isp->frame_id,
  682. ctx_isp->sof_timestamp_val, sof_event_status);
  683. if (cam_req_mgr_notify_message(&req_msg,
  684. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  685. CAM_ERR(CAM_ISP,
  686. "Error in notifying the sof time for req id:%lld",
  687. request_id);
  688. end:
  689. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  690. request_id, sof_event_status);
  691. }
  692. static void __cam_isp_ctx_handle_buf_done_fail_log(
  693. uint64_t request_id, struct cam_isp_ctx_req *req_isp,
  694. uint32_t isp_device_type)
  695. {
  696. int i;
  697. const char *handle_type;
  698. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  699. CAM_ERR(CAM_ISP,
  700. "Num Resources exceed mMAX %d >= %d ",
  701. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  702. return;
  703. }
  704. CAM_WARN_RATE_LIMIT(CAM_ISP,
  705. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  706. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  707. req_isp->bubble_report, req_isp->bubble_detected);
  708. CAM_WARN_RATE_LIMIT(CAM_ISP,
  709. "Resource Handles that fail to generate buf_done in prev frame");
  710. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  711. if (req_isp->fence_map_out[i].sync_id != -1) {
  712. if (isp_device_type == CAM_IFE_DEVICE_TYPE) {
  713. handle_type =
  714. __cam_isp_resource_handle_id_to_type(
  715. req_isp->fence_map_out[i].resource_handle);
  716. trace_cam_log_event("Buf_done Congestion",
  717. __cam_isp_resource_handle_id_to_type(
  718. req_isp->fence_map_out[i].resource_handle),
  719. request_id, req_isp->fence_map_out[i].sync_id);
  720. } else {
  721. handle_type =
  722. __cam_isp_tfe_resource_handle_id_to_type(
  723. req_isp->fence_map_out[i].resource_handle);
  724. trace_cam_log_event("Buf_done Congestion",
  725. __cam_isp_tfe_resource_handle_id_to_type(
  726. req_isp->fence_map_out[i].resource_handle),
  727. request_id, req_isp->fence_map_out[i].sync_id);
  728. }
  729. CAM_WARN_RATE_LIMIT(CAM_ISP,
  730. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  731. handle_type,
  732. req_isp->fence_map_out[i].resource_handle,
  733. req_isp->fence_map_out[i].sync_id);
  734. }
  735. }
  736. }
  737. static int __cam_isp_ctx_handle_buf_done_for_req_list(
  738. struct cam_isp_context *ctx_isp,
  739. struct cam_ctx_request *req)
  740. {
  741. int rc = 0, i;
  742. uint64_t buf_done_req_id;
  743. struct cam_isp_ctx_req *req_isp;
  744. struct cam_context *ctx = ctx_isp->base;
  745. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  746. ctx_isp->active_req_cnt--;
  747. buf_done_req_id = req->request_id;
  748. if (req_isp->bubble_detected && req_isp->bubble_report) {
  749. req_isp->num_acked = 0;
  750. req_isp->num_deferred_acks = 0;
  751. req_isp->bubble_detected = false;
  752. list_del_init(&req->list);
  753. atomic_set(&ctx_isp->process_bubble, 0);
  754. req_isp->cdm_reset_before_apply = false;
  755. ctx_isp->bubble_frame_cnt = 0;
  756. if (buf_done_req_id <= ctx->last_flush_req) {
  757. for (i = 0; i < req_isp->num_fence_map_out; i++)
  758. rc = cam_sync_signal(
  759. req_isp->fence_map_out[i].sync_id,
  760. CAM_SYNC_STATE_SIGNALED_ERROR,
  761. CAM_SYNC_ISP_EVENT_BUBBLE);
  762. list_add_tail(&req->list, &ctx->free_req_list);
  763. CAM_DBG(CAM_REQ,
  764. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u",
  765. buf_done_req_id, ctx_isp->active_req_cnt,
  766. ctx->ctx_id);
  767. ctx_isp->last_bufdone_err_apply_req_id = 0;
  768. } else {
  769. list_add(&req->list, &ctx->pending_req_list);
  770. CAM_DBG(CAM_REQ,
  771. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u",
  772. req->request_id, ctx_isp->active_req_cnt,
  773. ctx->ctx_id);
  774. }
  775. } else {
  776. if (!ctx_isp->use_frame_header_ts) {
  777. if (ctx_isp->reported_req_id < buf_done_req_id) {
  778. ctx_isp->reported_req_id = buf_done_req_id;
  779. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  780. buf_done_req_id,
  781. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  782. }
  783. }
  784. list_del_init(&req->list);
  785. list_add_tail(&req->list, &ctx->free_req_list);
  786. req_isp->reapply = false;
  787. req_isp->cdm_reset_before_apply = false;
  788. CAM_DBG(CAM_REQ,
  789. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u",
  790. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  791. ctx_isp->req_info.last_bufdone_req_id = req->request_id;
  792. ctx_isp->last_bufdone_err_apply_req_id = 0;
  793. }
  794. cam_cpas_notify_event("IFE BufDone", buf_done_req_id);
  795. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  796. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  797. __cam_isp_ctx_update_event_record(ctx_isp,
  798. CAM_ISP_CTX_EVENT_BUFDONE, req);
  799. return rc;
  800. }
  801. static int __cam_isp_ctx_handle_buf_done_for_request(
  802. struct cam_isp_context *ctx_isp,
  803. struct cam_ctx_request *req,
  804. struct cam_isp_hw_done_event_data *done,
  805. uint32_t bubble_state,
  806. struct cam_isp_hw_done_event_data *done_next_req)
  807. {
  808. int rc = 0;
  809. int i, j;
  810. struct cam_isp_ctx_req *req_isp;
  811. struct cam_context *ctx = ctx_isp->base;
  812. const char *handle_type;
  813. trace_cam_buf_done("ISP", ctx, req);
  814. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  815. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  816. bubble_state, req_isp->bubble_detected);
  817. done_next_req->num_handles = 0;
  818. done_next_req->timestamp = done->timestamp;
  819. for (i = 0; i < done->num_handles; i++) {
  820. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  821. if (done->resource_handle[i] ==
  822. req_isp->fence_map_out[j].resource_handle)
  823. break;
  824. }
  825. if (j == req_isp->num_fence_map_out) {
  826. /*
  827. * If not found in current request, it could be
  828. * belonging to next request, this can happen if
  829. * IRQ delay happens. It is only valid when the
  830. * platform doesn't have last consumed address.
  831. */
  832. CAM_WARN(CAM_ISP,
  833. "BUF_DONE for res %s not found in Req %lld ",
  834. __cam_isp_resource_handle_id_to_type(
  835. done->resource_handle[i]),
  836. req->request_id);
  837. done_next_req->resource_handle
  838. [done_next_req->num_handles++] =
  839. done->resource_handle[i];
  840. continue;
  841. }
  842. if (req_isp->fence_map_out[j].sync_id == -1) {
  843. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  844. handle_type =
  845. __cam_isp_resource_handle_id_to_type(
  846. req_isp->fence_map_out[i].resource_handle);
  847. else
  848. handle_type =
  849. __cam_isp_tfe_resource_handle_id_to_type(
  850. req_isp->fence_map_out[i].resource_handle);
  851. CAM_WARN(CAM_ISP,
  852. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  853. req->request_id, i, j, handle_type);
  854. trace_cam_log_event("Duplicate BufDone",
  855. handle_type, req->request_id, ctx->ctx_id);
  856. done_next_req->resource_handle
  857. [done_next_req->num_handles++] =
  858. done->resource_handle[i];
  859. continue;
  860. }
  861. if (!req_isp->bubble_detected) {
  862. CAM_DBG(CAM_ISP,
  863. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  864. req->request_id,
  865. req_isp->fence_map_out[j].resource_handle,
  866. req_isp->fence_map_out[j].sync_id,
  867. ctx->ctx_id);
  868. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  869. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  870. CAM_SYNC_COMMON_EVENT_SUCCESS);
  871. if (rc)
  872. CAM_DBG(CAM_ISP, "Sync failed with rc = %d",
  873. rc);
  874. } else if (!req_isp->bubble_report) {
  875. CAM_DBG(CAM_ISP,
  876. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  877. req->request_id,
  878. req_isp->fence_map_out[j].resource_handle,
  879. req_isp->fence_map_out[j].sync_id,
  880. ctx->ctx_id);
  881. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  882. CAM_SYNC_STATE_SIGNALED_ERROR,
  883. CAM_SYNC_ISP_EVENT_BUBBLE);
  884. if (rc)
  885. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  886. rc);
  887. } else {
  888. /*
  889. * Ignore the buffer done if bubble detect is on
  890. * Increment the ack number here, and queue the
  891. * request back to pending list whenever all the
  892. * buffers are done.
  893. */
  894. req_isp->num_acked++;
  895. CAM_DBG(CAM_ISP,
  896. "buf done with bubble state %d recovery %d",
  897. bubble_state, req_isp->bubble_report);
  898. continue;
  899. }
  900. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  901. req->request_id,
  902. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  903. if (!rc) {
  904. req_isp->num_acked++;
  905. req_isp->fence_map_out[j].sync_id = -1;
  906. }
  907. if ((ctx_isp->use_frame_header_ts) &&
  908. (req_isp->hw_update_data.frame_header_res_id ==
  909. req_isp->fence_map_out[j].resource_handle))
  910. __cam_isp_ctx_send_sof_timestamp_frame_header(
  911. ctx_isp,
  912. req_isp->hw_update_data.frame_header_cpu_addr,
  913. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  914. }
  915. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  916. /* Should not happen */
  917. CAM_ERR(CAM_ISP,
  918. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  919. req->request_id, req_isp->num_acked,
  920. req_isp->num_fence_map_out, ctx->ctx_id);
  921. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  922. }
  923. if (req_isp->num_acked != req_isp->num_fence_map_out)
  924. return rc;
  925. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  926. return rc;
  927. }
  928. static int __cam_isp_handle_deferred_buf_done(
  929. struct cam_isp_context *ctx_isp,
  930. struct cam_ctx_request *req,
  931. bool bubble_handling,
  932. uint32_t status, uint32_t event_cause)
  933. {
  934. int i, j;
  935. int rc = 0;
  936. struct cam_isp_ctx_req *req_isp =
  937. (struct cam_isp_ctx_req *) req->req_priv;
  938. struct cam_context *ctx = ctx_isp->base;
  939. CAM_DBG(CAM_ISP,
  940. "ctx[%d] : Req %llu : Handling %d deferred buf_dones num_acked=%d, bubble_handling=%d",
  941. ctx->ctx_id, req->request_id, req_isp->num_deferred_acks,
  942. req_isp->num_acked, bubble_handling);
  943. for (i = 0; i < req_isp->num_deferred_acks; i++) {
  944. j = req_isp->deferred_fence_map_index[i];
  945. CAM_DBG(CAM_ISP,
  946. "ctx[%d] : Sync with status=%d, event_cause=%d: req %lld res 0x%x sync_id 0x%x",
  947. ctx->ctx_id, status, event_cause,
  948. req->request_id,
  949. req_isp->fence_map_out[j].resource_handle,
  950. req_isp->fence_map_out[j].sync_id);
  951. if (req_isp->fence_map_out[j].sync_id == -1) {
  952. CAM_WARN(CAM_ISP,
  953. "ctx[%d Deferred buf_done already signalled, req_id=%llu, j=%d, res=0x%x",
  954. ctx->ctx_id, req->request_id, j,
  955. req_isp->fence_map_out[j].resource_handle);
  956. continue;
  957. }
  958. if (!bubble_handling) {
  959. CAM_WARN(CAM_ISP,
  960. "ctx[%d] : Req %llu, status=%d res=0x%x should never happen",
  961. ctx->ctx_id, req->request_id, status,
  962. req_isp->fence_map_out[j].resource_handle);
  963. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  964. status, event_cause);
  965. if (rc) {
  966. CAM_ERR(CAM_ISP,
  967. "ctx[%d] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  968. ctx->ctx_id, req->request_id,
  969. req_isp->fence_map_out[j].sync_id,
  970. status, rc);
  971. } else {
  972. req_isp->num_acked++;
  973. req_isp->fence_map_out[j].sync_id = -1;
  974. }
  975. } else {
  976. req_isp->num_acked++;
  977. }
  978. }
  979. req_isp->num_deferred_acks = 0;
  980. return rc;
  981. }
  982. static int __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  983. struct cam_isp_context *ctx_isp,
  984. struct cam_ctx_request *req,
  985. struct cam_isp_hw_done_event_data *done,
  986. uint32_t bubble_state,
  987. bool verify_consumed_addr,
  988. bool defer_buf_done)
  989. {
  990. int rc = 0;
  991. int i, j;
  992. struct cam_isp_ctx_req *req_isp;
  993. struct cam_context *ctx = ctx_isp->base;
  994. const char *handle_type;
  995. trace_cam_buf_done("ISP", ctx, req);
  996. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  997. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d",
  998. bubble_state, req_isp->bubble_detected);
  999. for (i = 0; i < done->num_handles; i++) {
  1000. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1001. if (verify_consumed_addr &&
  1002. (done->last_consumed_addr[i] !=
  1003. req_isp->fence_map_out[j].image_buf_addr[0]))
  1004. continue;
  1005. if (done->resource_handle[i] ==
  1006. req_isp->fence_map_out[j].resource_handle)
  1007. break;
  1008. }
  1009. if (j == req_isp->num_fence_map_out) {
  1010. /*
  1011. * If not found in current request, it could be
  1012. * belonging to next request, this can happen if
  1013. * IRQ delay happens. It is only valid when the
  1014. * platform doesn't have last consumed address.
  1015. */
  1016. CAM_DBG(CAM_ISP,
  1017. "BUF_DONE for res %s not found in Req %lld ",
  1018. __cam_isp_resource_handle_id_to_type(
  1019. done->resource_handle[i]),
  1020. req->request_id);
  1021. continue;
  1022. }
  1023. if (req_isp->fence_map_out[j].sync_id == -1) {
  1024. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  1025. handle_type =
  1026. __cam_isp_resource_handle_id_to_type(
  1027. req_isp->fence_map_out[i].resource_handle);
  1028. else
  1029. handle_type =
  1030. __cam_isp_tfe_resource_handle_id_to_type(
  1031. req_isp->fence_map_out[i].resource_handle);
  1032. CAM_WARN(CAM_ISP,
  1033. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s",
  1034. req->request_id, i, j, handle_type);
  1035. trace_cam_log_event("Duplicate BufDone",
  1036. handle_type, req->request_id, ctx->ctx_id);
  1037. continue;
  1038. }
  1039. if (defer_buf_done) {
  1040. uint32_t deferred_indx = req_isp->num_deferred_acks;
  1041. /*
  1042. * If we are handling this BUF_DONE event for a request
  1043. * that is still in wait_list, do not signal now,
  1044. * instead mark it as done and handle it later -
  1045. * if this request is going into BUBBLE state later
  1046. * it will automatically be re-applied. If this is not
  1047. * going into BUBBLE, signal fences later.
  1048. * Note - we will come here only if the last consumed
  1049. * address matches with this ports buffer.
  1050. */
  1051. req_isp->deferred_fence_map_index[deferred_indx] = j;
  1052. req_isp->num_deferred_acks++;
  1053. CAM_DBG(CAM_ISP,
  1054. "ctx[%d] : Deferred buf done for %llu with bubble state %d recovery %d",
  1055. ctx->ctx_id, req->request_id, bubble_state,
  1056. req_isp->bubble_report);
  1057. CAM_DBG(CAM_ISP,
  1058. "ctx[%d] : Deferred info : num_acks=%d, fence_map_index=%d, resource_handle=0x%x, sync_id=%d",
  1059. ctx->ctx_id, req_isp->num_deferred_acks, j,
  1060. req_isp->fence_map_out[j].resource_handle,
  1061. req_isp->fence_map_out[j].sync_id);
  1062. continue;
  1063. } else if (!req_isp->bubble_detected) {
  1064. CAM_DBG(CAM_ISP,
  1065. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u",
  1066. req->request_id,
  1067. req_isp->fence_map_out[j].resource_handle,
  1068. req_isp->fence_map_out[j].sync_id,
  1069. ctx->ctx_id);
  1070. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1071. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1072. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1073. if (rc) {
  1074. CAM_DBG(CAM_ISP, "Sync failed with rc = %d",
  1075. rc);
  1076. } else if (req_isp->num_deferred_acks) {
  1077. /* Process deferred buf_done acks */
  1078. __cam_isp_handle_deferred_buf_done(ctx_isp,
  1079. req, false,
  1080. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1081. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1082. }
  1083. } else if (!req_isp->bubble_report) {
  1084. CAM_DBG(CAM_ISP,
  1085. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u",
  1086. req->request_id,
  1087. req_isp->fence_map_out[j].resource_handle,
  1088. req_isp->fence_map_out[j].sync_id,
  1089. ctx->ctx_id);
  1090. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1091. CAM_SYNC_STATE_SIGNALED_ERROR,
  1092. CAM_SYNC_ISP_EVENT_BUBBLE);
  1093. if (rc) {
  1094. CAM_ERR(CAM_ISP, "Sync failed with rc = %d",
  1095. rc);
  1096. } else if (req_isp->num_deferred_acks) {
  1097. /* Process deferred buf_done acks */
  1098. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1099. false,
  1100. CAM_SYNC_STATE_SIGNALED_ERROR,
  1101. CAM_SYNC_ISP_EVENT_BUBBLE);
  1102. }
  1103. } else {
  1104. /*
  1105. * Ignore the buffer done if bubble detect is on
  1106. * Increment the ack number here, and queue the
  1107. * request back to pending list whenever all the
  1108. * buffers are done.
  1109. */
  1110. req_isp->num_acked++;
  1111. CAM_DBG(CAM_ISP,
  1112. "buf done with bubble state %d recovery %d",
  1113. bubble_state, req_isp->bubble_report);
  1114. /* Process deferred buf_done acks */
  1115. if (req_isp->num_deferred_acks)
  1116. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1117. true,
  1118. CAM_SYNC_STATE_SIGNALED_ERROR,
  1119. CAM_SYNC_ISP_EVENT_BUBBLE);
  1120. continue;
  1121. }
  1122. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u",
  1123. req->request_id,
  1124. req_isp->fence_map_out[j].sync_id, ctx->ctx_id);
  1125. if (!rc) {
  1126. req_isp->num_acked++;
  1127. req_isp->fence_map_out[j].sync_id = -1;
  1128. }
  1129. if ((ctx_isp->use_frame_header_ts) &&
  1130. (req_isp->hw_update_data.frame_header_res_id ==
  1131. req_isp->fence_map_out[j].resource_handle))
  1132. __cam_isp_ctx_send_sof_timestamp_frame_header(
  1133. ctx_isp,
  1134. req_isp->hw_update_data.frame_header_cpu_addr,
  1135. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1136. }
  1137. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1138. /* Should not happen */
  1139. CAM_ERR(CAM_ISP,
  1140. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u",
  1141. req->request_id, req_isp->num_acked,
  1142. req_isp->num_fence_map_out, ctx->ctx_id);
  1143. }
  1144. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1145. return rc;
  1146. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1147. return rc;
  1148. }
  1149. static int __cam_isp_ctx_handle_buf_done(
  1150. struct cam_isp_context *ctx_isp,
  1151. struct cam_isp_hw_done_event_data *done,
  1152. uint32_t bubble_state)
  1153. {
  1154. int rc = 0;
  1155. struct cam_ctx_request *req;
  1156. struct cam_context *ctx = ctx_isp->base;
  1157. struct cam_isp_hw_done_event_data done_next_req;
  1158. if (list_empty(&ctx->active_req_list)) {
  1159. CAM_WARN(CAM_ISP, "Buf done with no active request");
  1160. return 0;
  1161. }
  1162. req = list_first_entry(&ctx->active_req_list,
  1163. struct cam_ctx_request, list);
  1164. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  1165. bubble_state, &done_next_req);
  1166. if (done_next_req.num_handles) {
  1167. struct cam_isp_hw_done_event_data unhandled_res;
  1168. struct cam_ctx_request *next_req = list_last_entry(
  1169. &ctx->active_req_list, struct cam_ctx_request, list);
  1170. if (next_req->request_id != req->request_id) {
  1171. /*
  1172. * Few resource handles are already signalled in the
  1173. * current request, lets check if there is another
  1174. * request waiting for these resources. This can
  1175. * happen if handling some of next request's buf done
  1176. * events are happening first before handling current
  1177. * request's remaining buf dones due to IRQ scheduling.
  1178. * Lets check only one more request as we will have
  1179. * maximum of 2 requests in active_list at any time.
  1180. */
  1181. CAM_WARN(CAM_ISP,
  1182. "Unhandled buf done resources for req %lld, trying next request %lld in active_list",
  1183. req->request_id, next_req->request_id);
  1184. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  1185. next_req, &done_next_req,
  1186. bubble_state, &unhandled_res);
  1187. if (unhandled_res.num_handles == 0)
  1188. CAM_INFO(CAM_ISP,
  1189. "BUF Done event handed for next request %lld",
  1190. next_req->request_id);
  1191. else
  1192. CAM_ERR(CAM_ISP,
  1193. "BUF Done not handled for next request %lld",
  1194. next_req->request_id);
  1195. } else {
  1196. CAM_WARN(CAM_ISP,
  1197. "Req %lld only active request, spurious buf_done rxd",
  1198. req->request_id);
  1199. }
  1200. }
  1201. return rc;
  1202. }
  1203. static void __cam_isp_ctx_buf_done_match_req(
  1204. struct cam_ctx_request *req,
  1205. struct cam_isp_hw_done_event_data *done,
  1206. bool *irq_delay_detected)
  1207. {
  1208. int i, j;
  1209. uint32_t match_count = 0;
  1210. struct cam_isp_ctx_req *req_isp;
  1211. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1212. for (i = 0; i < done->num_handles; i++) {
  1213. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1214. if ((done->resource_handle[i] ==
  1215. req_isp->fence_map_out[j].resource_handle) &&
  1216. (done->last_consumed_addr[i] ==
  1217. req_isp->fence_map_out[j].image_buf_addr[0])) {
  1218. match_count++;
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. if (match_count > 0)
  1224. *irq_delay_detected = true;
  1225. else
  1226. *irq_delay_detected = false;
  1227. CAM_DBG(CAM_ISP,
  1228. "buf done num handles %d match count %d for next req:%lld",
  1229. done->num_handles, match_count, req->request_id);
  1230. CAM_DBG(CAM_ISP,
  1231. "irq_delay_detected %d", *irq_delay_detected);
  1232. }
  1233. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  1234. struct cam_isp_context *ctx_isp,
  1235. struct cam_isp_hw_done_event_data *done,
  1236. uint32_t bubble_state)
  1237. {
  1238. int rc = 0;
  1239. bool irq_delay_detected = false;
  1240. struct cam_ctx_request *req;
  1241. struct cam_ctx_request *next_req = NULL;
  1242. struct cam_context *ctx = ctx_isp->base;
  1243. bool req_in_wait_list = false;
  1244. if (list_empty(&ctx->active_req_list)) {
  1245. if (!list_empty(&ctx->wait_req_list)) {
  1246. struct cam_isp_ctx_req *req_isp;
  1247. req = list_first_entry(&ctx->wait_req_list,
  1248. struct cam_ctx_request, list);
  1249. req_in_wait_list = true;
  1250. if (ctx_isp->last_applied_req_id !=
  1251. ctx_isp->last_bufdone_err_apply_req_id) {
  1252. CAM_WARN(CAM_ISP,
  1253. "Buf done with no active request but with req in wait list, req %llu last apply id:%lld last err id:%lld",
  1254. req->request_id,
  1255. ctx_isp->last_applied_req_id,
  1256. ctx_isp->last_bufdone_err_apply_req_id);
  1257. ctx_isp->last_bufdone_err_apply_req_id =
  1258. ctx_isp->last_applied_req_id;
  1259. }
  1260. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1261. /*
  1262. * Verify consumed address for this request to make sure
  1263. * we are handling the buf_done for the correct
  1264. * buffer. Also defer actual buf_done handling, i.e
  1265. * do not signal the fence as this request may go into
  1266. * Bubble state eventully.
  1267. */
  1268. rc =
  1269. __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1270. ctx_isp, req, done, bubble_state, true, true);
  1271. }
  1272. if (!req_in_wait_list && (ctx_isp->last_applied_req_id !=
  1273. ctx_isp->last_bufdone_err_apply_req_id)) {
  1274. CAM_WARN(CAM_ISP,
  1275. "Buf done with no active request bubble_state=%d last_applied_req_id:%lld ",
  1276. bubble_state, ctx_isp->last_applied_req_id);
  1277. ctx_isp->last_bufdone_err_apply_req_id =
  1278. ctx_isp->last_applied_req_id;
  1279. }
  1280. return 0;
  1281. }
  1282. req = list_first_entry(&ctx->active_req_list,
  1283. struct cam_ctx_request, list);
  1284. if (ctx_isp->active_req_cnt > 1) {
  1285. next_req = list_last_entry(
  1286. &ctx->active_req_list,
  1287. struct cam_ctx_request, list);
  1288. if (next_req->request_id != req->request_id)
  1289. __cam_isp_ctx_buf_done_match_req(next_req, done,
  1290. &irq_delay_detected);
  1291. else
  1292. CAM_WARN(CAM_ISP,
  1293. "Req %lld only active request, spurious buf_done rxd",
  1294. req->request_id);
  1295. }
  1296. /*
  1297. * If irq delay isn't detected, then we need to verify
  1298. * the consumed address for current req, otherwise, we
  1299. * can't verify the consumed address.
  1300. */
  1301. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1302. ctx_isp, req, done, bubble_state,
  1303. !irq_delay_detected, false);
  1304. /*
  1305. * Verify the consumed address for next req all the time,
  1306. * since the reported buf done event may belong to current
  1307. * req, then we can't signal this event for next req.
  1308. */
  1309. if (!rc && irq_delay_detected)
  1310. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1311. ctx_isp, next_req, done,
  1312. bubble_state, true, false);
  1313. return rc;
  1314. }
  1315. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  1316. struct cam_isp_context *ctx_isp,
  1317. struct cam_isp_hw_done_event_data *done,
  1318. uint32_t bubble_state)
  1319. {
  1320. int rc = 0;
  1321. if (ctx_isp->support_consumed_addr)
  1322. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  1323. ctx_isp, done, bubble_state);
  1324. else
  1325. rc = __cam_isp_ctx_handle_buf_done(
  1326. ctx_isp, done, bubble_state);
  1327. return rc;
  1328. }
  1329. static int __cam_isp_ctx_apply_req_offline(
  1330. void *priv, void *data)
  1331. {
  1332. int rc = 0;
  1333. int64_t prev_applied_req;
  1334. struct cam_context *ctx = NULL;
  1335. struct cam_isp_context *ctx_isp = priv;
  1336. struct cam_ctx_request *req;
  1337. struct cam_isp_ctx_req *req_isp;
  1338. struct cam_hw_config_args cfg;
  1339. if (!ctx_isp) {
  1340. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  1341. rc = -EINVAL;
  1342. goto end;
  1343. }
  1344. ctx = ctx_isp->base;
  1345. if (list_empty(&ctx->pending_req_list)) {
  1346. CAM_DBG(CAM_ISP, "No pending requests to apply");
  1347. rc = -EFAULT;
  1348. goto end;
  1349. }
  1350. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  1351. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  1352. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  1353. goto end;
  1354. if (ctx_isp->active_req_cnt >= 2)
  1355. goto end;
  1356. spin_lock_bh(&ctx->lock);
  1357. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1358. list);
  1359. spin_unlock_bh(&ctx->lock);
  1360. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1361. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1362. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1363. memset(&cfg, 0, sizeof(cfg));
  1364. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1365. cfg.request_id = req->request_id;
  1366. cfg.hw_update_entries = req_isp->cfg;
  1367. cfg.num_hw_update_entries = req_isp->num_cfg;
  1368. cfg.priv = &req_isp->hw_update_data;
  1369. cfg.init_packet = 0;
  1370. /*
  1371. * Offline mode may receive the SOF and REG_UPD earlier than
  1372. * CDM processing return back, so we set the substate before
  1373. * apply setting.
  1374. */
  1375. spin_lock_bh(&ctx->lock);
  1376. atomic_set(&ctx_isp->rxd_epoch, 0);
  1377. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  1378. prev_applied_req = ctx_isp->last_applied_req_id;
  1379. ctx_isp->last_applied_req_id = req->request_id;
  1380. atomic_set(&ctx_isp->apply_in_progress, 1);
  1381. list_del_init(&req->list);
  1382. list_add_tail(&req->list, &ctx->wait_req_list);
  1383. spin_unlock_bh(&ctx->lock);
  1384. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1385. if (rc) {
  1386. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1387. spin_lock_bh(&ctx->lock);
  1388. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1389. ctx_isp->last_applied_req_id = prev_applied_req;
  1390. atomic_set(&ctx_isp->apply_in_progress, 0);
  1391. list_del_init(&req->list);
  1392. list_add(&req->list, &ctx->pending_req_list);
  1393. spin_unlock_bh(&ctx->lock);
  1394. } else {
  1395. atomic_set(&ctx_isp->apply_in_progress, 0);
  1396. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld",
  1397. CAM_ISP_CTX_ACTIVATED_APPLIED,
  1398. ctx_isp->last_applied_req_id);
  1399. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1400. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1401. req->request_id);
  1402. }
  1403. end:
  1404. return rc;
  1405. }
  1406. static int __cam_isp_ctx_schedule_apply_req_offline(
  1407. struct cam_isp_context *ctx_isp)
  1408. {
  1409. int rc = 0;
  1410. struct crm_workq_task *task;
  1411. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  1412. if (!task) {
  1413. CAM_ERR(CAM_ISP, "No task for worker");
  1414. return -ENOMEM;
  1415. }
  1416. task->process_cb = __cam_isp_ctx_apply_req_offline;
  1417. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  1418. if (rc)
  1419. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  1420. return rc;
  1421. }
  1422. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  1423. struct cam_isp_context *ctx_isp, void *evt_data)
  1424. {
  1425. struct cam_context *ctx = ctx_isp->base;
  1426. struct cam_ctx_request *req, *req_temp;
  1427. uint64_t request_id = 0;
  1428. atomic_set(&ctx_isp->rxd_epoch, 1);
  1429. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u", ctx_isp->frame_id,
  1430. ctx->ctx_id);
  1431. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  1432. if (req->request_id > ctx_isp->reported_req_id) {
  1433. request_id = req->request_id;
  1434. ctx_isp->reported_req_id = request_id;
  1435. break;
  1436. }
  1437. }
  1438. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  1439. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1440. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1441. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1442. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1443. request_id);
  1444. return 0;
  1445. }
  1446. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  1447. struct cam_isp_context *ctx_isp, void *evt_data)
  1448. {
  1449. if (ctx_isp->frame_id == 1)
  1450. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  1451. __cam_isp_ctx_substate_val_to_type(
  1452. ctx_isp->substate_activated));
  1453. else
  1454. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1455. "ctx_id:%d Unexpected reg update in activated Substate[%s] for frame_id:%lld",
  1456. ctx_isp->base->ctx_id,
  1457. __cam_isp_ctx_substate_val_to_type(
  1458. ctx_isp->substate_activated),
  1459. ctx_isp->frame_id);
  1460. return 0;
  1461. }
  1462. static int __cam_isp_ctx_reg_upd_in_applied_state(
  1463. struct cam_isp_context *ctx_isp, void *evt_data)
  1464. {
  1465. int rc = 0;
  1466. struct cam_ctx_request *req;
  1467. struct cam_context *ctx = ctx_isp->base;
  1468. struct cam_isp_ctx_req *req_isp;
  1469. uint64_t request_id = 0;
  1470. if (list_empty(&ctx->wait_req_list)) {
  1471. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1472. goto end;
  1473. }
  1474. req = list_first_entry(&ctx->wait_req_list,
  1475. struct cam_ctx_request, list);
  1476. list_del_init(&req->list);
  1477. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1478. if (req_isp->num_fence_map_out != 0) {
  1479. list_add_tail(&req->list, &ctx->active_req_list);
  1480. ctx_isp->active_req_cnt++;
  1481. request_id = req->request_id;
  1482. CAM_DBG(CAM_REQ,
  1483. "move request %lld to active list(cnt = %d), ctx %u",
  1484. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1485. __cam_isp_ctx_update_event_record(ctx_isp,
  1486. CAM_ISP_CTX_EVENT_RUP, req);
  1487. } else {
  1488. /* no io config, so the request is completed. */
  1489. list_add_tail(&req->list, &ctx->free_req_list);
  1490. CAM_DBG(CAM_ISP,
  1491. "move active request %lld to free list(cnt = %d), ctx %u",
  1492. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1493. }
  1494. /*
  1495. * This function only called directly from applied and bubble applied
  1496. * state so change substate here.
  1497. */
  1498. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1499. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1500. __cam_isp_ctx_substate_val_to_type(
  1501. ctx_isp->substate_activated));
  1502. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1503. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  1504. end:
  1505. return rc;
  1506. }
  1507. static int __cam_isp_ctx_notify_sof_in_activated_state(
  1508. struct cam_isp_context *ctx_isp, void *evt_data)
  1509. {
  1510. int rc = 0;
  1511. uint64_t request_id = 0;
  1512. struct cam_req_mgr_trigger_notify notify;
  1513. struct cam_context *ctx = ctx_isp->base;
  1514. struct cam_ctx_request *req;
  1515. struct cam_isp_ctx_req *req_isp;
  1516. struct cam_hw_cmd_args hw_cmd_args;
  1517. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1518. uint64_t last_cdm_done_req = 0;
  1519. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1520. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1521. if (!evt_data) {
  1522. CAM_ERR(CAM_ISP, "invalid event data");
  1523. return -EINVAL;
  1524. }
  1525. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1526. if (atomic_read(&ctx_isp->process_bubble)) {
  1527. if (list_empty(&ctx->active_req_list)) {
  1528. CAM_ERR(CAM_ISP,
  1529. "No available active req in bubble");
  1530. atomic_set(&ctx_isp->process_bubble, 0);
  1531. ctx_isp->bubble_frame_cnt = 0;
  1532. rc = -EINVAL;
  1533. return rc;
  1534. }
  1535. if (ctx_isp->last_sof_timestamp ==
  1536. ctx_isp->sof_timestamp_val) {
  1537. CAM_DBG(CAM_ISP,
  1538. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld",
  1539. ctx_isp->sof_timestamp_val);
  1540. goto notify_only;
  1541. }
  1542. req = list_first_entry(&ctx->active_req_list,
  1543. struct cam_ctx_request, list);
  1544. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1545. if (ctx_isp->bubble_frame_cnt >= 1 &&
  1546. req_isp->bubble_detected) {
  1547. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1548. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1549. isp_hw_cmd_args.cmd_type =
  1550. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  1551. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  1552. rc = ctx->hw_mgr_intf->hw_cmd(
  1553. ctx->hw_mgr_intf->hw_mgr_priv,
  1554. &hw_cmd_args);
  1555. if (rc) {
  1556. CAM_ERR(CAM_ISP, "HW command failed");
  1557. return rc;
  1558. }
  1559. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  1560. CAM_DBG(CAM_ISP, "last_cdm_done req: %d",
  1561. last_cdm_done_req);
  1562. if (last_cdm_done_req >= req->request_id) {
  1563. CAM_DBG(CAM_ISP,
  1564. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  1565. req->request_id);
  1566. ctx_isp->bubble_frame_cnt = 0;
  1567. } else {
  1568. CAM_DBG(CAM_ISP,
  1569. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  1570. req->request_id);
  1571. req_isp->num_acked = 0;
  1572. req_isp->num_deferred_acks = 0;
  1573. ctx_isp->bubble_frame_cnt = 0;
  1574. req_isp->bubble_detected = false;
  1575. req_isp->cdm_reset_before_apply = true;
  1576. list_del_init(&req->list);
  1577. list_add(&req->list, &ctx->pending_req_list);
  1578. atomic_set(&ctx_isp->process_bubble, 0);
  1579. ctx_isp->active_req_cnt--;
  1580. CAM_DBG(CAM_REQ,
  1581. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  1582. req->request_id,
  1583. ctx_isp->active_req_cnt, ctx->ctx_id);
  1584. }
  1585. } else if (req_isp->bubble_detected) {
  1586. ctx_isp->bubble_frame_cnt++;
  1587. CAM_DBG(CAM_ISP,
  1588. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld",
  1589. req->request_id,
  1590. ctx_isp->bubble_frame_cnt);
  1591. } else {
  1592. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld",
  1593. req->request_id);
  1594. }
  1595. }
  1596. notify_only:
  1597. /*
  1598. * notify reqmgr with sof signal. Note, due to scheduling delay
  1599. * we can run into situation that two active requests has already
  1600. * be in the active queue while we try to do the notification.
  1601. * In this case, we need to skip the current notification. This
  1602. * helps the state machine to catch up the delay.
  1603. */
  1604. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1605. ctx_isp->active_req_cnt <= 2) {
  1606. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1607. notify.link_hdl = ctx->link_hdl;
  1608. notify.dev_hdl = ctx->dev_hdl;
  1609. notify.frame_id = ctx_isp->frame_id;
  1610. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1611. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  1612. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1613. notify.trigger_id = ctx_isp->trigger_id;
  1614. ctx->ctx_crm_intf->notify_trigger(&notify);
  1615. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld ctx %u",
  1616. ctx_isp->frame_id, ctx->ctx_id);
  1617. }
  1618. list_for_each_entry(req, &ctx->active_req_list, list) {
  1619. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1620. if ((!req_isp->bubble_detected) &&
  1621. (req->request_id > ctx_isp->reported_req_id)) {
  1622. request_id = req->request_id;
  1623. ctx_isp->reported_req_id = request_id;
  1624. __cam_isp_ctx_update_event_record(ctx_isp,
  1625. CAM_ISP_CTX_EVENT_EPOCH, req);
  1626. break;
  1627. }
  1628. }
  1629. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  1630. request_id = 0;
  1631. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1632. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1633. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1634. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1635. request_id);
  1636. } else {
  1637. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1638. "Can not notify SOF to CRM for ctx %u",
  1639. ctx->ctx_id);
  1640. rc = -EFAULT;
  1641. }
  1642. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  1643. return 0;
  1644. }
  1645. static int __cam_isp_ctx_notify_eof_in_activated_state(
  1646. struct cam_isp_context *ctx_isp, void *evt_data)
  1647. {
  1648. int rc = 0;
  1649. struct cam_req_mgr_trigger_notify notify;
  1650. struct cam_context *ctx = ctx_isp->base;
  1651. if (!(ctx_isp->subscribe_event & CAM_TRIGGER_POINT_EOF))
  1652. return rc;
  1653. /* notify reqmgr with eof signal */
  1654. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  1655. notify.link_hdl = ctx->link_hdl;
  1656. notify.dev_hdl = ctx->dev_hdl;
  1657. notify.frame_id = ctx_isp->frame_id;
  1658. notify.trigger = CAM_TRIGGER_POINT_EOF;
  1659. notify.trigger_id = ctx_isp->trigger_id;
  1660. ctx->ctx_crm_intf->notify_trigger(&notify);
  1661. CAM_DBG(CAM_ISP, "Notify CRM EOF frame %lld ctx %u",
  1662. ctx_isp->frame_id, ctx->ctx_id);
  1663. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1664. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  1665. } else {
  1666. CAM_ERR(CAM_ISP, "Can not notify EOF to CRM for ctx %u",
  1667. ctx->ctx_id);
  1668. rc = -EFAULT;
  1669. }
  1670. return rc;
  1671. }
  1672. static int __cam_isp_ctx_reg_upd_in_hw_error(
  1673. struct cam_isp_context *ctx_isp, void *evt_data)
  1674. {
  1675. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1676. return 0;
  1677. }
  1678. static int __cam_isp_ctx_sof_in_activated_state(
  1679. struct cam_isp_context *ctx_isp, void *evt_data)
  1680. {
  1681. int rc = 0;
  1682. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1683. struct cam_ctx_request *req = NULL;
  1684. struct cam_context *ctx = ctx_isp->base;
  1685. uint64_t request_id = 0;
  1686. /* First check if there is a valid request in active list */
  1687. list_for_each_entry(req, &ctx->active_req_list, list) {
  1688. if (req->request_id > ctx_isp->reported_req_id) {
  1689. request_id = req->request_id;
  1690. break;
  1691. }
  1692. }
  1693. /*
  1694. * If nothing in active list, current request might have not moved
  1695. * from wait to active list. This could happen if REG_UPDATE to sw
  1696. * is coming immediately after SOF
  1697. */
  1698. if (request_id == 0) {
  1699. req = list_first_entry(&ctx->wait_req_list,
  1700. struct cam_ctx_request, list);
  1701. if (req)
  1702. request_id = req->request_id;
  1703. }
  1704. if (!evt_data) {
  1705. CAM_ERR(CAM_ISP, "in valid sof event data");
  1706. return -EINVAL;
  1707. }
  1708. ctx_isp->frame_id++;
  1709. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1710. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1711. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1712. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  1713. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  1714. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  1715. return rc;
  1716. }
  1717. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  1718. void *evt_data)
  1719. {
  1720. int rc = 0;
  1721. struct cam_ctx_request *req = NULL;
  1722. struct cam_isp_ctx_req *req_isp;
  1723. struct cam_context *ctx = ctx_isp->base;
  1724. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  1725. CAM_DBG(CAM_ISP, "invalid RUP");
  1726. goto end;
  1727. }
  1728. /*
  1729. * This is for the first update. The initial setting will
  1730. * cause the reg_upd in the first frame.
  1731. */
  1732. if (!list_empty(&ctx->wait_req_list)) {
  1733. req = list_first_entry(&ctx->wait_req_list,
  1734. struct cam_ctx_request, list);
  1735. list_del_init(&req->list);
  1736. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1737. if (req_isp->num_fence_map_out == req_isp->num_acked)
  1738. list_add_tail(&req->list, &ctx->free_req_list);
  1739. else
  1740. CAM_ERR(CAM_ISP,
  1741. "receive rup in unexpected state");
  1742. }
  1743. if (req != NULL) {
  1744. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1745. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  1746. req->request_id);
  1747. }
  1748. end:
  1749. return rc;
  1750. }
  1751. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  1752. void *evt_data)
  1753. {
  1754. uint64_t request_id = 0;
  1755. struct cam_req_mgr_trigger_notify notify;
  1756. struct cam_ctx_request *req;
  1757. struct cam_isp_ctx_req *req_isp;
  1758. struct cam_context *ctx = ctx_isp->base;
  1759. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1760. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1761. if (!evt_data) {
  1762. CAM_ERR(CAM_ISP, "invalid event data");
  1763. return -EINVAL;
  1764. }
  1765. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1766. if (list_empty(&ctx->wait_req_list)) {
  1767. /*
  1768. * If no wait req in epoch, this is an error case.
  1769. * The recovery is to go back to sof state
  1770. */
  1771. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  1772. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1773. /* Send SOF event as empty frame*/
  1774. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1775. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1776. __cam_isp_ctx_update_event_record(ctx_isp,
  1777. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1778. goto end;
  1779. }
  1780. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1781. list);
  1782. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1783. req_isp->bubble_detected = true;
  1784. req_isp->reapply = true;
  1785. req_isp->cdm_reset_before_apply = false;
  1786. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%d Report Bubble flag %d req id:%lld",
  1787. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  1788. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1789. ctx->ctx_crm_intf->notify_err) {
  1790. struct cam_req_mgr_error_notify notify;
  1791. notify.link_hdl = ctx->link_hdl;
  1792. notify.dev_hdl = ctx->dev_hdl;
  1793. notify.req_id = req->request_id;
  1794. notify.error = CRM_KMD_ERR_BUBBLE;
  1795. notify.trigger = 0;
  1796. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  1797. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1798. notify.frame_id = ctx_isp->frame_id;
  1799. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1800. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1801. "Notify CRM about Bubble req %lld frame %lld, ctx %u",
  1802. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1803. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  1804. req->request_id, ctx->ctx_id);
  1805. ctx->ctx_crm_intf->notify_err(&notify);
  1806. atomic_set(&ctx_isp->process_bubble, 1);
  1807. } else {
  1808. req_isp->bubble_report = 0;
  1809. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  1810. req->request_id, ctx->ctx_id);
  1811. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1812. ctx_isp->active_req_cnt <= 1) {
  1813. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1814. notify.link_hdl = ctx->link_hdl;
  1815. notify.dev_hdl = ctx->dev_hdl;
  1816. notify.frame_id = ctx_isp->frame_id;
  1817. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1818. notify.req_id =
  1819. ctx_isp->req_info.last_bufdone_req_id;
  1820. notify.sof_timestamp_val =
  1821. ctx_isp->sof_timestamp_val;
  1822. notify.trigger_id = ctx_isp->trigger_id;
  1823. ctx->ctx_crm_intf->notify_trigger(&notify);
  1824. CAM_DBG(CAM_ISP,
  1825. "Notify CRM SOF frame %lld ctx %u",
  1826. ctx_isp->frame_id, ctx->ctx_id);
  1827. }
  1828. }
  1829. }
  1830. /*
  1831. * Always move the request to active list. Let buf done
  1832. * function handles the rest.
  1833. */
  1834. list_del_init(&req->list);
  1835. list_add_tail(&req->list, &ctx->active_req_list);
  1836. ctx_isp->active_req_cnt++;
  1837. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  1838. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1839. if ((req->request_id > ctx_isp->reported_req_id)
  1840. && !req_isp->bubble_report) {
  1841. request_id = req->request_id;
  1842. ctx_isp->reported_req_id = request_id;
  1843. }
  1844. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1845. CAM_REQ_MGR_SOF_EVENT_ERROR);
  1846. __cam_isp_ctx_update_event_record(ctx_isp,
  1847. CAM_ISP_CTX_EVENT_EPOCH, req);
  1848. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1849. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1850. __cam_isp_ctx_substate_val_to_type(
  1851. ctx_isp->substate_activated));
  1852. cam_req_mgr_debug_delay_detect();
  1853. trace_cam_delay_detect("ISP",
  1854. "bubble epoch_in_applied", req->request_id,
  1855. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  1856. CAM_DEFAULT_VALUE);
  1857. end:
  1858. if (request_id == 0) {
  1859. req = list_last_entry(&ctx->active_req_list,
  1860. struct cam_ctx_request, list);
  1861. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1862. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  1863. } else {
  1864. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1865. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  1866. }
  1867. return 0;
  1868. }
  1869. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  1870. void *evt_data)
  1871. {
  1872. int rc = 0;
  1873. struct cam_isp_hw_done_event_data *done =
  1874. (struct cam_isp_hw_done_event_data *) evt_data;
  1875. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1876. return rc;
  1877. }
  1878. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  1879. void *evt_data)
  1880. {
  1881. int rc = 0;
  1882. struct cam_context *ctx = ctx_isp->base;
  1883. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  1884. struct cam_ctx_request *req;
  1885. if (!evt_data) {
  1886. CAM_ERR(CAM_ISP, "in valid sof event data");
  1887. return -EINVAL;
  1888. }
  1889. if (atomic_read(&ctx_isp->apply_in_progress))
  1890. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF");
  1891. ctx_isp->frame_id++;
  1892. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  1893. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  1894. if (list_empty(&ctx->active_req_list))
  1895. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1896. else
  1897. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  1898. req = list_last_entry(&ctx->active_req_list,
  1899. struct cam_ctx_request, list);
  1900. if (req)
  1901. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1902. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  1903. req->request_id);
  1904. if (ctx_isp->frame_id == 1)
  1905. CAM_INFO(CAM_ISP,
  1906. "First SOF in EPCR ctx:%d frame_id:%lld next substate %s",
  1907. ctx->ctx_id, ctx_isp->frame_id,
  1908. __cam_isp_ctx_substate_val_to_type(
  1909. ctx_isp->substate_activated));
  1910. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%d frame_id:%lld next substate:%s",
  1911. ctx->ctx_id, ctx_isp->frame_id,
  1912. __cam_isp_ctx_substate_val_to_type(
  1913. ctx_isp->substate_activated));
  1914. return rc;
  1915. }
  1916. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  1917. void *evt_data)
  1918. {
  1919. int rc = 0;
  1920. struct cam_isp_hw_done_event_data *done =
  1921. (struct cam_isp_hw_done_event_data *) evt_data;
  1922. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  1923. return rc;
  1924. }
  1925. static int __cam_isp_ctx_buf_done_in_bubble(
  1926. struct cam_isp_context *ctx_isp, void *evt_data)
  1927. {
  1928. int rc = 0;
  1929. struct cam_isp_hw_done_event_data *done =
  1930. (struct cam_isp_hw_done_event_data *) evt_data;
  1931. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  1932. return rc;
  1933. }
  1934. static int __cam_isp_ctx_epoch_in_bubble_applied(
  1935. struct cam_isp_context *ctx_isp, void *evt_data)
  1936. {
  1937. uint64_t request_id = 0;
  1938. struct cam_req_mgr_trigger_notify notify;
  1939. struct cam_ctx_request *req;
  1940. struct cam_isp_ctx_req *req_isp;
  1941. struct cam_context *ctx = ctx_isp->base;
  1942. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1943. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1944. if (!evt_data) {
  1945. CAM_ERR(CAM_ISP, "invalid event data");
  1946. return -EINVAL;
  1947. }
  1948. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1949. /*
  1950. * This means we missed the reg upd ack. So we need to
  1951. * transition to BUBBLE state again.
  1952. */
  1953. if (list_empty(&ctx->wait_req_list)) {
  1954. /*
  1955. * If no pending req in epoch, this is an error case.
  1956. * Just go back to the bubble state.
  1957. */
  1958. CAM_ERR(CAM_ISP, "ctx:%d No pending request.", ctx->ctx_id);
  1959. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1960. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1961. __cam_isp_ctx_update_event_record(ctx_isp,
  1962. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  1963. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1964. goto end;
  1965. }
  1966. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  1967. list);
  1968. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1969. req_isp->bubble_detected = true;
  1970. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  1971. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  1972. req_isp->reapply = true;
  1973. req_isp->cdm_reset_before_apply = false;
  1974. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  1975. ctx->ctx_crm_intf->notify_err) {
  1976. struct cam_req_mgr_error_notify notify;
  1977. notify.link_hdl = ctx->link_hdl;
  1978. notify.dev_hdl = ctx->dev_hdl;
  1979. notify.req_id = req->request_id;
  1980. notify.error = CRM_KMD_ERR_BUBBLE;
  1981. notify.trigger = 0;
  1982. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  1983. notify.trigger = CAM_TRIGGER_POINT_SOF;
  1984. notify.frame_id = ctx_isp->frame_id;
  1985. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  1986. CAM_WARN_RATE_LIMIT(CAM_REQ,
  1987. "Notify CRM about Bubble req_id %llu frame %lld, ctx %u",
  1988. req->request_id, ctx_isp->frame_id, ctx->ctx_id);
  1989. ctx->ctx_crm_intf->notify_err(&notify);
  1990. atomic_set(&ctx_isp->process_bubble, 1);
  1991. } else {
  1992. req_isp->bubble_report = 0;
  1993. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  1994. req->request_id, ctx->ctx_id);
  1995. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  1996. ctx_isp->active_req_cnt <= 1) {
  1997. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  1998. notify.link_hdl = ctx->link_hdl;
  1999. notify.dev_hdl = ctx->dev_hdl;
  2000. notify.frame_id = ctx_isp->frame_id;
  2001. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2002. notify.req_id =
  2003. ctx_isp->req_info.last_bufdone_req_id;
  2004. notify.sof_timestamp_val =
  2005. ctx_isp->sof_timestamp_val;
  2006. notify.trigger_id = ctx_isp->trigger_id;
  2007. ctx->ctx_crm_intf->notify_trigger(&notify);
  2008. CAM_DBG(CAM_ISP,
  2009. "Notify CRM SOF frame %lld ctx %u",
  2010. ctx_isp->frame_id, ctx->ctx_id);
  2011. }
  2012. }
  2013. }
  2014. /*
  2015. * Always move the request to active list. Let buf done
  2016. * function handles the rest.
  2017. */
  2018. list_del_init(&req->list);
  2019. list_add_tail(&req->list, &ctx->active_req_list);
  2020. ctx_isp->active_req_cnt++;
  2021. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  2022. req->request_id, ctx_isp->active_req_cnt);
  2023. if (!req_isp->bubble_report) {
  2024. if (req->request_id > ctx_isp->reported_req_id) {
  2025. request_id = req->request_id;
  2026. ctx_isp->reported_req_id = request_id;
  2027. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2028. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2029. __cam_isp_ctx_update_event_record(ctx_isp,
  2030. CAM_ISP_CTX_EVENT_EPOCH, req);
  2031. } else {
  2032. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2033. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2034. __cam_isp_ctx_update_event_record(ctx_isp,
  2035. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2036. }
  2037. } else {
  2038. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2039. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2040. __cam_isp_ctx_update_event_record(ctx_isp,
  2041. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2042. }
  2043. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2044. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2045. __cam_isp_ctx_substate_val_to_type(
  2046. ctx_isp->substate_activated));
  2047. cam_req_mgr_debug_delay_detect();
  2048. trace_cam_delay_detect("ISP",
  2049. "bubble epoch_in_bubble_applied",
  2050. req->request_id, ctx->ctx_id,
  2051. ctx->link_hdl, ctx->session_hdl,
  2052. CAM_DEFAULT_VALUE);
  2053. end:
  2054. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  2055. list);
  2056. if (req)
  2057. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2058. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2059. return 0;
  2060. }
  2061. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  2062. struct cam_isp_context *ctx_isp, void *evt_data)
  2063. {
  2064. int rc = 0;
  2065. struct cam_isp_hw_done_event_data *done =
  2066. (struct cam_isp_hw_done_event_data *) evt_data;
  2067. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2068. return rc;
  2069. }
  2070. static uint32_t get_evt_param(uint32_t error_type)
  2071. {
  2072. switch (error_type) {
  2073. case CAM_ISP_HW_ERROR_OVERFLOW:
  2074. return CAM_SYNC_ISP_EVENT_OVERFLOW;
  2075. case CAM_ISP_HW_ERROR_P2I_ERROR:
  2076. return CAM_SYNC_ISP_EVENT_P2I_ERROR;
  2077. case CAM_ISP_HW_ERROR_VIOLATION:
  2078. return CAM_SYNC_ISP_EVENT_VIOLATION;
  2079. case CAM_ISP_HW_ERROR_BUSIF_OVERFLOW:
  2080. return CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  2081. default:
  2082. return CAM_SYNC_ISP_EVENT_UNKNOWN;
  2083. }
  2084. }
  2085. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  2086. void *evt_data)
  2087. {
  2088. int rc = 0;
  2089. uint32_t i = 0;
  2090. bool found = 0;
  2091. struct cam_ctx_request *req = NULL;
  2092. struct cam_ctx_request *req_to_report = NULL;
  2093. struct cam_ctx_request *req_to_dump = NULL;
  2094. struct cam_ctx_request *req_temp;
  2095. struct cam_isp_ctx_req *req_isp = NULL;
  2096. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  2097. struct cam_req_mgr_error_notify notify = {};
  2098. uint64_t error_request_id;
  2099. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  2100. struct cam_req_mgr_message req_msg;
  2101. uint32_t evt_param;
  2102. struct cam_context *ctx = ctx_isp->base;
  2103. struct cam_isp_hw_error_event_data *error_event_data =
  2104. (struct cam_isp_hw_error_event_data *)evt_data;
  2105. uint32_t error_type = error_event_data->error_type;
  2106. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  2107. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  2108. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  2109. (error_type == CAM_ISP_HW_ERROR_VIOLATION)) {
  2110. struct cam_hw_cmd_args hw_cmd_args;
  2111. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  2112. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  2113. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR;
  2114. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2115. &hw_cmd_args);
  2116. if (rc) {
  2117. CAM_ERR(CAM_ISP, "Reg dump on error failed rc: %d", rc);
  2118. rc = 0;
  2119. }
  2120. }
  2121. evt_param = get_evt_param(error_type);
  2122. /*
  2123. * The error is likely caused by first request on the active list.
  2124. * If active list is empty check wait list (maybe error hit as soon
  2125. * as RUP and we handle error before RUP.
  2126. */
  2127. if (list_empty(&ctx->active_req_list)) {
  2128. CAM_DBG(CAM_ISP,
  2129. "handling error with no active request");
  2130. if (list_empty(&ctx->wait_req_list)) {
  2131. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2132. "Error with no active/wait request");
  2133. goto end;
  2134. } else {
  2135. req_to_dump = list_first_entry(&ctx->wait_req_list,
  2136. struct cam_ctx_request, list);
  2137. }
  2138. } else {
  2139. req_to_dump = list_first_entry(&ctx->active_req_list,
  2140. struct cam_ctx_request, list);
  2141. }
  2142. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  2143. if (error_event_data->enable_req_dump)
  2144. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  2145. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2146. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  2147. list_for_each_entry_safe(req, req_temp,
  2148. &ctx->active_req_list, list) {
  2149. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2150. if (!req_isp->bubble_report) {
  2151. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2152. fence_map_out =
  2153. &req_isp->fence_map_out[i];
  2154. CAM_ERR(CAM_ISP,
  2155. "req %llu, Sync fd 0x%x ctx %u",
  2156. req->request_id,
  2157. req_isp->fence_map_out[i].sync_id,
  2158. ctx->ctx_id);
  2159. if (req_isp->fence_map_out[i].sync_id != -1) {
  2160. rc = cam_sync_signal(
  2161. fence_map_out->sync_id,
  2162. CAM_SYNC_STATE_SIGNALED_ERROR,
  2163. evt_param);
  2164. fence_map_out->sync_id = -1;
  2165. }
  2166. }
  2167. list_del_init(&req->list);
  2168. list_add_tail(&req->list, &ctx->free_req_list);
  2169. ctx_isp->active_req_cnt--;
  2170. } else {
  2171. found = 1;
  2172. break;
  2173. }
  2174. }
  2175. if (found)
  2176. goto move_to_pending;
  2177. list_for_each_entry_safe(req, req_temp,
  2178. &ctx->wait_req_list, list) {
  2179. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2180. if (!req_isp->bubble_report) {
  2181. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2182. fence_map_out =
  2183. &req_isp->fence_map_out[i];
  2184. CAM_ERR(CAM_ISP,
  2185. "req %llu, Sync fd 0x%x ctx %u",
  2186. req->request_id,
  2187. req_isp->fence_map_out[i].sync_id,
  2188. ctx->ctx_id);
  2189. if (req_isp->fence_map_out[i].sync_id != -1) {
  2190. rc = cam_sync_signal(
  2191. fence_map_out->sync_id,
  2192. CAM_SYNC_STATE_SIGNALED_ERROR,
  2193. evt_param);
  2194. fence_map_out->sync_id = -1;
  2195. }
  2196. }
  2197. list_del_init(&req->list);
  2198. list_add_tail(&req->list, &ctx->free_req_list);
  2199. ctx_isp->active_req_cnt--;
  2200. } else {
  2201. found = 1;
  2202. break;
  2203. }
  2204. }
  2205. move_to_pending:
  2206. /*
  2207. * If bubble recovery is enabled on any request we need to move that
  2208. * request and all the subsequent requests to the pending list.
  2209. * Note:
  2210. * We need to traverse the active list in reverse order and add
  2211. * to head of pending list.
  2212. * e.g. pending current state: 10, 11 | active current state: 8, 9
  2213. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  2214. * final state - pending: 8, 9, 10, 11 | active: NULL
  2215. */
  2216. if (found) {
  2217. list_for_each_entry_safe_reverse(req, req_temp,
  2218. &ctx->active_req_list, list) {
  2219. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2220. list_del_init(&req->list);
  2221. list_add(&req->list, &ctx->pending_req_list);
  2222. ctx_isp->active_req_cnt--;
  2223. }
  2224. list_for_each_entry_safe_reverse(req, req_temp,
  2225. &ctx->wait_req_list, list) {
  2226. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2227. list_del_init(&req->list);
  2228. list_add(&req->list, &ctx->pending_req_list);
  2229. ctx_isp->active_req_cnt--;
  2230. }
  2231. }
  2232. end:
  2233. do {
  2234. if (list_empty(&ctx->pending_req_list)) {
  2235. error_request_id = ctx_isp->last_applied_req_id;
  2236. req_isp = NULL;
  2237. break;
  2238. }
  2239. req = list_first_entry(&ctx->pending_req_list,
  2240. struct cam_ctx_request, list);
  2241. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2242. error_request_id = ctx_isp->last_applied_req_id;
  2243. if (req_isp->bubble_report) {
  2244. req_to_report = req;
  2245. req_isp_to_report = req_to_report->req_priv;
  2246. break;
  2247. }
  2248. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2249. if (req_isp->fence_map_out[i].sync_id != -1)
  2250. rc = cam_sync_signal(
  2251. req_isp->fence_map_out[i].sync_id,
  2252. CAM_SYNC_STATE_SIGNALED_ERROR,
  2253. evt_param);
  2254. req_isp->fence_map_out[i].sync_id = -1;
  2255. }
  2256. list_del_init(&req->list);
  2257. list_add_tail(&req->list, &ctx->free_req_list);
  2258. } while (req->request_id < ctx_isp->last_applied_req_id);
  2259. if (ctx_isp->offline_context)
  2260. goto exit;
  2261. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_err) {
  2262. notify.link_hdl = ctx->link_hdl;
  2263. notify.dev_hdl = ctx->dev_hdl;
  2264. notify.req_id = error_request_id;
  2265. notify.error = CRM_KMD_ERR_FATAL;
  2266. if (req_isp_to_report && req_isp_to_report->bubble_report)
  2267. if (error_event_data->recovery_enabled)
  2268. notify.error = CRM_KMD_ERR_BUBBLE;
  2269. CAM_WARN(CAM_ISP,
  2270. "Notify CRM: req %lld, frame %lld ctx %u, error %d",
  2271. error_request_id, ctx_isp->frame_id, ctx->ctx_id,
  2272. notify.error);
  2273. ctx->ctx_crm_intf->notify_err(&notify);
  2274. /*
  2275. * Need to send error occurred in KMD
  2276. * This will help UMD to take necessary action
  2277. * and to dump relevant info
  2278. */
  2279. if (notify.error == CRM_KMD_ERR_FATAL) {
  2280. req_msg.session_hdl = ctx_isp->base->session_hdl;
  2281. req_msg.u.err_msg.device_hdl = ctx_isp->base->dev_hdl;
  2282. if (error_type == CAM_ISP_HW_ERROR_CSID_FATAL)
  2283. req_msg.u.err_msg.error_type =
  2284. CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2285. else
  2286. req_msg.u.err_msg.error_type =
  2287. CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2288. req_msg.u.err_msg.link_hdl = ctx_isp->base->link_hdl;
  2289. req_msg.u.err_msg.request_id = error_request_id;
  2290. req_msg.u.err_msg.resource_size = 0x0;
  2291. if (cam_req_mgr_notify_message(&req_msg,
  2292. V4L_EVENT_CAM_REQ_MGR_ERROR,
  2293. V4L_EVENT_CAM_REQ_MGR_EVENT))
  2294. CAM_ERR(CAM_ISP,
  2295. "Error in notifying the error time for req id:%lld ctx %u",
  2296. ctx_isp->last_applied_req_id,
  2297. ctx->ctx_id);
  2298. }
  2299. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  2300. } else {
  2301. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2302. "Can not notify ERRROR to CRM for ctx %u",
  2303. ctx->ctx_id);
  2304. rc = -EFAULT;
  2305. }
  2306. CAM_DBG(CAM_ISP, "Exit");
  2307. exit:
  2308. return rc;
  2309. }
  2310. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  2311. struct cam_isp_context *ctx_isp, void *evt_data)
  2312. {
  2313. int rc = 0;
  2314. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2315. struct cam_ctx_request *req;
  2316. struct cam_context *ctx = ctx_isp->base;
  2317. struct cam_req_mgr_trigger_notify notify;
  2318. uint64_t request_id = 0;
  2319. if (!evt_data) {
  2320. CAM_ERR(CAM_ISP, "in valid sof event data");
  2321. return -EINVAL;
  2322. }
  2323. ctx_isp->frame_id++;
  2324. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2325. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2326. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2327. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2328. if (!(list_empty(&ctx->wait_req_list)))
  2329. goto end;
  2330. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2331. ctx_isp->active_req_cnt <= 2) {
  2332. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2333. notify.link_hdl = ctx->link_hdl;
  2334. notify.dev_hdl = ctx->dev_hdl;
  2335. notify.frame_id = ctx_isp->frame_id;
  2336. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2337. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2338. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2339. ctx->ctx_crm_intf->notify_trigger(&notify);
  2340. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2341. ctx_isp->frame_id);
  2342. }
  2343. list_for_each_entry(req, &ctx->active_req_list, list) {
  2344. if (req->request_id > ctx_isp->reported_req_id) {
  2345. request_id = req->request_id;
  2346. ctx_isp->reported_req_id = request_id;
  2347. break;
  2348. }
  2349. }
  2350. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2351. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2352. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2353. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2354. } else {
  2355. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2356. rc = -EFAULT;
  2357. }
  2358. end:
  2359. return rc;
  2360. }
  2361. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  2362. void *evt_data)
  2363. {
  2364. int rc = 0;
  2365. struct cam_isp_hw_done_event_data *done =
  2366. (struct cam_isp_hw_done_event_data *) evt_data;
  2367. struct cam_context *ctx = ctx_isp->base;
  2368. int prev_active_req_cnt = 0;
  2369. int curr_req_id = 0;
  2370. struct cam_ctx_request *req;
  2371. prev_active_req_cnt = ctx_isp->active_req_cnt;
  2372. req = list_first_entry(&ctx->active_req_list,
  2373. struct cam_ctx_request, list);
  2374. if (req)
  2375. curr_req_id = req->request_id;
  2376. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2377. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  2378. if (list_empty(&ctx->wait_req_list) &&
  2379. list_empty(&ctx->active_req_list)) {
  2380. CAM_DBG(CAM_ISP, "No request, move to SOF");
  2381. ctx_isp->substate_activated =
  2382. CAM_ISP_CTX_ACTIVATED_SOF;
  2383. if (ctx_isp->reported_req_id < curr_req_id) {
  2384. ctx_isp->reported_req_id = curr_req_id;
  2385. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  2386. curr_req_id,
  2387. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2388. }
  2389. }
  2390. }
  2391. return rc;
  2392. }
  2393. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2394. void *evt_data)
  2395. {
  2396. int rc = 0;
  2397. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2398. return rc;
  2399. }
  2400. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  2401. struct cam_isp_context *ctx_isp,
  2402. void *evt_data)
  2403. {
  2404. int rc = 0;
  2405. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2406. return rc;
  2407. }
  2408. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2409. void *evt_data)
  2410. {
  2411. int rc = 0;
  2412. struct cam_ctx_request *req = NULL;
  2413. struct cam_isp_ctx_req *req_isp;
  2414. struct cam_context *ctx = ctx_isp->base;
  2415. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2416. CAM_DBG(CAM_ISP, "invalid RUP");
  2417. goto end;
  2418. }
  2419. /*
  2420. * This is for the first update. The initial setting will
  2421. * cause the reg_upd in the first frame.
  2422. */
  2423. if (!list_empty(&ctx->wait_req_list)) {
  2424. req = list_first_entry(&ctx->wait_req_list,
  2425. struct cam_ctx_request, list);
  2426. list_del_init(&req->list);
  2427. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2428. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2429. list_add_tail(&req->list, &ctx->free_req_list);
  2430. else
  2431. CAM_ERR(CAM_ISP,
  2432. "receive rup in unexpected state");
  2433. }
  2434. if (req != NULL) {
  2435. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2436. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2437. req->request_id);
  2438. }
  2439. end:
  2440. return rc;
  2441. }
  2442. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  2443. struct cam_isp_context *ctx_isp, void *evt_data)
  2444. {
  2445. int rc = 0;
  2446. struct cam_ctx_request *req = NULL;
  2447. struct cam_context *ctx = ctx_isp->base;
  2448. struct cam_isp_ctx_req *req_isp;
  2449. struct cam_req_mgr_trigger_notify notify;
  2450. uint64_t request_id = 0;
  2451. if (list_empty(&ctx->wait_req_list)) {
  2452. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2453. goto end;
  2454. }
  2455. req = list_first_entry(&ctx->wait_req_list,
  2456. struct cam_ctx_request, list);
  2457. list_del_init(&req->list);
  2458. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2459. if (req_isp->num_fence_map_out != 0) {
  2460. list_add_tail(&req->list, &ctx->active_req_list);
  2461. ctx_isp->active_req_cnt++;
  2462. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  2463. req->request_id, ctx_isp->active_req_cnt);
  2464. } else {
  2465. /* no io config, so the request is completed. */
  2466. list_add_tail(&req->list, &ctx->free_req_list);
  2467. }
  2468. /*
  2469. * This function only called directly from applied and bubble applied
  2470. * state so change substate here.
  2471. */
  2472. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2473. if (req_isp->num_fence_map_out != 1)
  2474. goto end;
  2475. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  2476. ctx_isp->active_req_cnt <= 2) {
  2477. list_for_each_entry(req, &ctx->active_req_list, list) {
  2478. if (req->request_id > ctx_isp->reported_req_id) {
  2479. request_id = req->request_id;
  2480. ctx_isp->reported_req_id = request_id;
  2481. break;
  2482. }
  2483. }
  2484. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2485. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2486. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF) {
  2487. notify.link_hdl = ctx->link_hdl;
  2488. notify.dev_hdl = ctx->dev_hdl;
  2489. notify.frame_id = ctx_isp->frame_id;
  2490. notify.trigger = CAM_TRIGGER_POINT_SOF;
  2491. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  2492. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  2493. ctx->ctx_crm_intf->notify_trigger(&notify);
  2494. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  2495. ctx_isp->frame_id);
  2496. }
  2497. } else {
  2498. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  2499. rc = -EFAULT;
  2500. }
  2501. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2502. __cam_isp_ctx_substate_val_to_type(
  2503. ctx_isp->substate_activated));
  2504. end:
  2505. if (req != NULL && !rc) {
  2506. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2507. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2508. req->request_id);
  2509. }
  2510. return rc;
  2511. }
  2512. static struct cam_isp_ctx_irq_ops
  2513. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2514. /* SOF */
  2515. {
  2516. .irq_ops = {
  2517. __cam_isp_ctx_handle_error,
  2518. __cam_isp_ctx_sof_in_activated_state,
  2519. __cam_isp_ctx_reg_upd_in_sof,
  2520. __cam_isp_ctx_notify_sof_in_activated_state,
  2521. __cam_isp_ctx_notify_eof_in_activated_state,
  2522. NULL,
  2523. },
  2524. },
  2525. /* APPLIED */
  2526. {
  2527. .irq_ops = {
  2528. __cam_isp_ctx_handle_error,
  2529. __cam_isp_ctx_sof_in_activated_state,
  2530. __cam_isp_ctx_reg_upd_in_applied_state,
  2531. __cam_isp_ctx_epoch_in_applied,
  2532. __cam_isp_ctx_notify_eof_in_activated_state,
  2533. __cam_isp_ctx_buf_done_in_applied,
  2534. },
  2535. },
  2536. /* EPOCH */
  2537. {
  2538. .irq_ops = {
  2539. __cam_isp_ctx_handle_error,
  2540. __cam_isp_ctx_sof_in_epoch,
  2541. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2542. __cam_isp_ctx_notify_sof_in_activated_state,
  2543. __cam_isp_ctx_notify_eof_in_activated_state,
  2544. __cam_isp_ctx_buf_done_in_epoch,
  2545. },
  2546. },
  2547. /* BUBBLE */
  2548. {
  2549. .irq_ops = {
  2550. __cam_isp_ctx_handle_error,
  2551. __cam_isp_ctx_sof_in_activated_state,
  2552. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2553. __cam_isp_ctx_notify_sof_in_activated_state,
  2554. __cam_isp_ctx_notify_eof_in_activated_state,
  2555. __cam_isp_ctx_buf_done_in_bubble,
  2556. },
  2557. },
  2558. /* Bubble Applied */
  2559. {
  2560. .irq_ops = {
  2561. __cam_isp_ctx_handle_error,
  2562. __cam_isp_ctx_sof_in_activated_state,
  2563. __cam_isp_ctx_reg_upd_in_applied_state,
  2564. __cam_isp_ctx_epoch_in_bubble_applied,
  2565. NULL,
  2566. __cam_isp_ctx_buf_done_in_bubble_applied,
  2567. },
  2568. },
  2569. /* HW ERROR */
  2570. {
  2571. .irq_ops = {
  2572. NULL,
  2573. __cam_isp_ctx_sof_in_activated_state,
  2574. __cam_isp_ctx_reg_upd_in_hw_error,
  2575. NULL,
  2576. NULL,
  2577. NULL,
  2578. },
  2579. },
  2580. /* HALT */
  2581. {
  2582. },
  2583. };
  2584. static struct cam_isp_ctx_irq_ops
  2585. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2586. /* SOF */
  2587. {
  2588. .irq_ops = {
  2589. __cam_isp_ctx_handle_error,
  2590. __cam_isp_ctx_fs2_sof_in_sof_state,
  2591. __cam_isp_ctx_fs2_reg_upd_in_sof,
  2592. __cam_isp_ctx_fs2_sof_in_sof_state,
  2593. __cam_isp_ctx_notify_eof_in_activated_state,
  2594. NULL,
  2595. },
  2596. },
  2597. /* APPLIED */
  2598. {
  2599. .irq_ops = {
  2600. __cam_isp_ctx_handle_error,
  2601. __cam_isp_ctx_sof_in_activated_state,
  2602. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  2603. __cam_isp_ctx_epoch_in_applied,
  2604. __cam_isp_ctx_notify_eof_in_activated_state,
  2605. __cam_isp_ctx_fs2_buf_done_in_applied,
  2606. },
  2607. },
  2608. /* EPOCH */
  2609. {
  2610. .irq_ops = {
  2611. __cam_isp_ctx_handle_error,
  2612. __cam_isp_ctx_sof_in_epoch,
  2613. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2614. __cam_isp_ctx_notify_sof_in_activated_state,
  2615. __cam_isp_ctx_notify_eof_in_activated_state,
  2616. __cam_isp_ctx_fs2_buf_done_in_epoch,
  2617. },
  2618. },
  2619. /* BUBBLE */
  2620. {
  2621. .irq_ops = {
  2622. __cam_isp_ctx_handle_error,
  2623. __cam_isp_ctx_sof_in_activated_state,
  2624. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  2625. __cam_isp_ctx_notify_sof_in_activated_state,
  2626. __cam_isp_ctx_notify_eof_in_activated_state,
  2627. __cam_isp_ctx_buf_done_in_bubble,
  2628. },
  2629. },
  2630. /* Bubble Applied */
  2631. {
  2632. .irq_ops = {
  2633. __cam_isp_ctx_handle_error,
  2634. __cam_isp_ctx_sof_in_activated_state,
  2635. __cam_isp_ctx_reg_upd_in_applied_state,
  2636. __cam_isp_ctx_epoch_in_bubble_applied,
  2637. NULL,
  2638. __cam_isp_ctx_buf_done_in_bubble_applied,
  2639. },
  2640. },
  2641. /* HW ERROR */
  2642. {
  2643. .irq_ops = {
  2644. NULL,
  2645. __cam_isp_ctx_sof_in_activated_state,
  2646. __cam_isp_ctx_reg_upd_in_hw_error,
  2647. NULL,
  2648. NULL,
  2649. NULL,
  2650. },
  2651. },
  2652. /* HALT */
  2653. {
  2654. },
  2655. };
  2656. static struct cam_isp_ctx_irq_ops
  2657. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  2658. /* SOF */
  2659. {
  2660. .irq_ops = {
  2661. __cam_isp_ctx_handle_error,
  2662. NULL,
  2663. NULL,
  2664. NULL,
  2665. NULL,
  2666. NULL,
  2667. },
  2668. },
  2669. /* APPLIED */
  2670. {
  2671. .irq_ops = {
  2672. __cam_isp_ctx_handle_error,
  2673. __cam_isp_ctx_sof_in_activated_state,
  2674. __cam_isp_ctx_reg_upd_in_applied_state,
  2675. NULL,
  2676. NULL,
  2677. __cam_isp_ctx_buf_done_in_applied,
  2678. },
  2679. },
  2680. /* EPOCH */
  2681. {
  2682. .irq_ops = {
  2683. __cam_isp_ctx_handle_error,
  2684. NULL,
  2685. NULL,
  2686. __cam_isp_ctx_offline_epoch_in_activated_state,
  2687. NULL,
  2688. __cam_isp_ctx_buf_done_in_epoch,
  2689. },
  2690. },
  2691. /* BUBBLE */
  2692. {
  2693. },
  2694. /* Bubble Applied */
  2695. {
  2696. },
  2697. /* HW ERROR */
  2698. {
  2699. .irq_ops = {
  2700. NULL,
  2701. __cam_isp_ctx_sof_in_activated_state,
  2702. __cam_isp_ctx_reg_upd_in_hw_error,
  2703. NULL,
  2704. NULL,
  2705. NULL,
  2706. },
  2707. },
  2708. /* HALT */
  2709. {
  2710. },
  2711. };
  2712. static int __cam_isp_ctx_apply_req_in_activated_state(
  2713. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  2714. enum cam_isp_ctx_activated_substate next_state)
  2715. {
  2716. int rc = 0;
  2717. struct cam_ctx_request *req;
  2718. struct cam_ctx_request *active_req = NULL;
  2719. struct cam_isp_ctx_req *req_isp;
  2720. struct cam_isp_ctx_req *active_req_isp;
  2721. struct cam_isp_context *ctx_isp = NULL;
  2722. struct cam_hw_config_args cfg = {0};
  2723. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  2724. if (apply->re_apply)
  2725. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  2726. CAM_INFO_RATE_LIMIT(CAM_ISP,
  2727. "ctx_id:%d Trying to reapply the same request %llu again",
  2728. ctx->ctx_id,
  2729. apply->request_id);
  2730. return 0;
  2731. }
  2732. if (list_empty(&ctx->pending_req_list)) {
  2733. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2734. "ctx_id:%d No available request for Apply id %lld",
  2735. ctx->ctx_id,
  2736. apply->request_id);
  2737. rc = -EFAULT;
  2738. goto end;
  2739. }
  2740. /*
  2741. * When the pipeline has issue, the requests can be queued up in the
  2742. * pipeline. In this case, we should reject the additional request.
  2743. * The maximum number of request allowed to be outstanding is 2.
  2744. *
  2745. */
  2746. if (atomic_read(&ctx_isp->process_bubble)) {
  2747. CAM_INFO_RATE_LIMIT(CAM_ISP,
  2748. "ctx_id:%d Processing bubble cannot apply Request Id %llu",
  2749. ctx->ctx_id,
  2750. apply->request_id);
  2751. rc = -EAGAIN;
  2752. goto end;
  2753. }
  2754. spin_lock_bh(&ctx->lock);
  2755. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2756. list);
  2757. spin_unlock_bh(&ctx->lock);
  2758. /*
  2759. * Check whether the request id is matching the tip, if not, this means
  2760. * we are in the middle of the error handling. Need to reject this apply
  2761. */
  2762. if (req->request_id != apply->request_id) {
  2763. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2764. "ctx_id:%d Invalid Request Id asking %llu existing %llu",
  2765. ctx->ctx_id,
  2766. apply->request_id, req->request_id);
  2767. rc = -EFAULT;
  2768. goto end;
  2769. }
  2770. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u",
  2771. req->request_id,
  2772. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  2773. ctx->ctx_id);
  2774. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2775. if (ctx_isp->active_req_cnt >= 2) {
  2776. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2777. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  2778. req->request_id,
  2779. ctx_isp->active_req_cnt,
  2780. ctx->ctx_id);
  2781. spin_lock_bh(&ctx->lock);
  2782. if (!list_empty(&ctx->active_req_list))
  2783. active_req = list_first_entry(&ctx->active_req_list,
  2784. struct cam_ctx_request, list);
  2785. else
  2786. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2787. "WARNING: should not happen (cnt = %d) but active_list empty",
  2788. ctx_isp->active_req_cnt);
  2789. spin_unlock_bh(&ctx->lock);
  2790. if (active_req) {
  2791. active_req_isp =
  2792. (struct cam_isp_ctx_req *) active_req->req_priv;
  2793. __cam_isp_ctx_handle_buf_done_fail_log(
  2794. active_req->request_id, active_req_isp,
  2795. ctx_isp->isp_device_type);
  2796. }
  2797. rc = -EFAULT;
  2798. goto end;
  2799. }
  2800. req_isp->bubble_report = apply->report_if_bubble;
  2801. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2802. cfg.request_id = req->request_id;
  2803. cfg.hw_update_entries = req_isp->cfg;
  2804. cfg.num_hw_update_entries = req_isp->num_cfg;
  2805. cfg.priv = &req_isp->hw_update_data;
  2806. cfg.init_packet = 0;
  2807. cfg.reapply = req_isp->reapply;
  2808. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  2809. atomic_set(&ctx_isp->apply_in_progress, 1);
  2810. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2811. if (!rc) {
  2812. spin_lock_bh(&ctx->lock);
  2813. ctx_isp->substate_activated = next_state;
  2814. ctx_isp->last_applied_req_id = apply->request_id;
  2815. list_del_init(&req->list);
  2816. list_add_tail(&req->list, &ctx->wait_req_list);
  2817. CAM_DBG(CAM_ISP, "new substate Substate[%s], applied req %lld",
  2818. __cam_isp_ctx_substate_val_to_type(next_state),
  2819. ctx_isp->last_applied_req_id);
  2820. spin_unlock_bh(&ctx->lock);
  2821. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2822. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2823. req->request_id);
  2824. __cam_isp_ctx_update_event_record(ctx_isp,
  2825. CAM_ISP_CTX_EVENT_APPLY, req);
  2826. } else if (rc == -EALREADY) {
  2827. spin_lock_bh(&ctx->lock);
  2828. req_isp->bubble_detected = true;
  2829. req_isp->cdm_reset_before_apply = false;
  2830. atomic_set(&ctx_isp->process_bubble, 1);
  2831. list_del_init(&req->list);
  2832. list_add(&req->list, &ctx->active_req_list);
  2833. ctx_isp->active_req_cnt++;
  2834. spin_unlock_bh(&ctx->lock);
  2835. CAM_DBG(CAM_REQ,
  2836. "move request %lld to active list(cnt = %d), ctx %u",
  2837. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2838. } else {
  2839. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2840. "ctx_id:%d ,Can not apply (req %lld) the configuration, rc %d",
  2841. ctx->ctx_id, apply->request_id, rc);
  2842. }
  2843. atomic_set(&ctx_isp->apply_in_progress, 0);
  2844. end:
  2845. return rc;
  2846. }
  2847. static int __cam_isp_ctx_apply_req_in_sof(
  2848. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2849. {
  2850. int rc = 0;
  2851. struct cam_isp_context *ctx_isp =
  2852. (struct cam_isp_context *) ctx->ctx_priv;
  2853. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2854. __cam_isp_ctx_substate_val_to_type(
  2855. ctx_isp->substate_activated));
  2856. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2857. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2858. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2859. __cam_isp_ctx_substate_val_to_type(
  2860. ctx_isp->substate_activated));
  2861. if (rc)
  2862. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2863. __cam_isp_ctx_substate_val_to_type(
  2864. ctx_isp->substate_activated), rc);
  2865. return rc;
  2866. }
  2867. static int __cam_isp_ctx_apply_req_in_epoch(
  2868. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2869. {
  2870. int rc = 0;
  2871. struct cam_isp_context *ctx_isp =
  2872. (struct cam_isp_context *) ctx->ctx_priv;
  2873. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2874. __cam_isp_ctx_substate_val_to_type(
  2875. ctx_isp->substate_activated));
  2876. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2877. CAM_ISP_CTX_ACTIVATED_APPLIED);
  2878. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2879. __cam_isp_ctx_substate_val_to_type(
  2880. ctx_isp->substate_activated));
  2881. if (rc)
  2882. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2883. __cam_isp_ctx_substate_val_to_type(
  2884. ctx_isp->substate_activated), rc);
  2885. return rc;
  2886. }
  2887. static int __cam_isp_ctx_apply_req_in_bubble(
  2888. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2889. {
  2890. int rc = 0;
  2891. struct cam_isp_context *ctx_isp =
  2892. (struct cam_isp_context *) ctx->ctx_priv;
  2893. CAM_DBG(CAM_ISP, "current Substate[%s]",
  2894. __cam_isp_ctx_substate_val_to_type(
  2895. ctx_isp->substate_activated));
  2896. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  2897. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  2898. CAM_DBG(CAM_ISP, "new Substate[%s]",
  2899. __cam_isp_ctx_substate_val_to_type(
  2900. ctx_isp->substate_activated));
  2901. if (rc)
  2902. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  2903. __cam_isp_ctx_substate_val_to_type(
  2904. ctx_isp->substate_activated), rc);
  2905. return rc;
  2906. }
  2907. static int __cam_isp_ctx_apply_default_req_settings(
  2908. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  2909. {
  2910. int rc = 0;
  2911. struct cam_isp_context *isp_ctx =
  2912. (struct cam_isp_context *) ctx->ctx_priv;
  2913. struct cam_hw_cmd_args hw_cmd_args;
  2914. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2915. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  2916. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2917. isp_hw_cmd_args.cmd_type =
  2918. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  2919. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2920. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  2921. &hw_cmd_args);
  2922. if (rc)
  2923. CAM_ERR(CAM_ISP,
  2924. "Failed to apply default settings rc %d", rc);
  2925. else
  2926. CAM_DBG(CAM_ISP, "Applied default settings rc %d", rc);
  2927. return rc;
  2928. }
  2929. static int __cam_isp_ctx_dump_req_info(
  2930. struct cam_context *ctx,
  2931. struct cam_ctx_request *req,
  2932. uintptr_t cpu_addr,
  2933. size_t buf_len,
  2934. size_t *offset)
  2935. {
  2936. int i, rc;
  2937. uint8_t *dst;
  2938. int32_t *addr, *start;
  2939. uint32_t min_len;
  2940. size_t remain_len;
  2941. struct cam_isp_ctx_req *req_isp;
  2942. struct cam_isp_context *ctx_isp;
  2943. struct cam_isp_context_dump_header *hdr;
  2944. if (!req || !ctx || !offset || !cpu_addr || !buf_len) {
  2945. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK %zu",
  2946. req, ctx, offset, buf_len);
  2947. return -EINVAL;
  2948. }
  2949. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2950. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  2951. if (buf_len <= *offset) {
  2952. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  2953. buf_len, *offset);
  2954. return -ENOSPC;
  2955. }
  2956. remain_len = buf_len - *offset;
  2957. min_len = sizeof(struct cam_isp_context_dump_header) +
  2958. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  2959. req_isp->num_fence_map_out *
  2960. sizeof(int32_t));
  2961. if (remain_len < min_len) {
  2962. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  2963. remain_len, min_len);
  2964. return -ENOSPC;
  2965. }
  2966. dst = (uint8_t *)cpu_addr + *offset;
  2967. hdr = (struct cam_isp_context_dump_header *)dst;
  2968. hdr->word_size = sizeof(int32_t);
  2969. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  2970. "ISP_OUT_FENCE:");
  2971. addr = (int32_t *)(dst + sizeof(struct cam_isp_context_dump_header));
  2972. start = addr;
  2973. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2974. *addr++ = req_isp->fence_map_out[i].resource_handle;
  2975. *addr++ = req_isp->fence_map_out[i].sync_id;
  2976. }
  2977. hdr->size = hdr->word_size * (addr - start);
  2978. *offset += hdr->size + sizeof(struct cam_isp_context_dump_header);
  2979. rc = cam_isp_ctx_dump_req(req_isp, cpu_addr, buf_len,
  2980. offset, true);
  2981. return rc;
  2982. }
  2983. static int __cam_isp_ctx_dump_in_top_state(
  2984. struct cam_context *ctx,
  2985. struct cam_req_mgr_dump_info *dump_info)
  2986. {
  2987. int rc = 0;
  2988. bool dump_only_event_record = false;
  2989. size_t buf_len;
  2990. size_t remain_len;
  2991. uint8_t *dst;
  2992. ktime_t cur_time;
  2993. uint32_t min_len;
  2994. uint64_t diff;
  2995. uint64_t *addr, *start;
  2996. uintptr_t cpu_addr;
  2997. struct timespec64 ts;
  2998. struct cam_isp_context *ctx_isp;
  2999. struct cam_ctx_request *req = NULL;
  3000. struct cam_isp_ctx_req *req_isp;
  3001. struct cam_ctx_request *req_temp;
  3002. struct cam_hw_dump_args dump_args;
  3003. struct cam_isp_context_dump_header *hdr;
  3004. spin_lock_bh(&ctx->lock);
  3005. list_for_each_entry_safe(req, req_temp,
  3006. &ctx->active_req_list, list) {
  3007. if (req->request_id == dump_info->req_id) {
  3008. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  3009. dump_info->req_id);
  3010. goto hw_dump;
  3011. }
  3012. }
  3013. list_for_each_entry_safe(req, req_temp,
  3014. &ctx->wait_req_list, list) {
  3015. if (req->request_id == dump_info->req_id) {
  3016. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  3017. dump_info->req_id);
  3018. goto hw_dump;
  3019. }
  3020. }
  3021. spin_unlock_bh(&ctx->lock);
  3022. return rc;
  3023. hw_dump:
  3024. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  3025. &cpu_addr, &buf_len);
  3026. if (rc) {
  3027. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  3028. dump_info->buf_handle, rc);
  3029. spin_unlock_bh(&ctx->lock);
  3030. return rc;
  3031. }
  3032. if (buf_len <= dump_info->offset) {
  3033. spin_unlock_bh(&ctx->lock);
  3034. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3035. buf_len, dump_info->offset);
  3036. return -ENOSPC;
  3037. }
  3038. remain_len = buf_len - dump_info->offset;
  3039. min_len = sizeof(struct cam_isp_context_dump_header) +
  3040. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  3041. if (remain_len < min_len) {
  3042. spin_unlock_bh(&ctx->lock);
  3043. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3044. remain_len, min_len);
  3045. return -ENOSPC;
  3046. }
  3047. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3048. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3049. cur_time = ktime_get();
  3050. diff = ktime_us_delta(
  3051. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  3052. cur_time);
  3053. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  3054. CAM_INFO(CAM_ISP, "req %lld found no error",
  3055. req->request_id);
  3056. dump_only_event_record = true;
  3057. }
  3058. dst = (uint8_t *)cpu_addr + dump_info->offset;
  3059. hdr = (struct cam_isp_context_dump_header *)dst;
  3060. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3061. "ISP_CTX_DUMP:");
  3062. hdr->word_size = sizeof(uint64_t);
  3063. addr = (uint64_t *)(dst +
  3064. sizeof(struct cam_isp_context_dump_header));
  3065. start = addr;
  3066. *addr++ = req->request_id;
  3067. ts = ktime_to_timespec64(
  3068. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]);
  3069. *addr++ = ts.tv_sec;
  3070. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3071. ts = ktime_to_timespec64(cur_time);
  3072. *addr++ = ts.tv_sec;
  3073. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3074. hdr->size = hdr->word_size * (addr - start);
  3075. dump_info->offset += hdr->size +
  3076. sizeof(struct cam_isp_context_dump_header);
  3077. rc = __cam_isp_ctx_dump_event_record(ctx_isp, cpu_addr,
  3078. buf_len, &dump_info->offset);
  3079. if (rc) {
  3080. CAM_ERR(CAM_ISP, "Dump event fail %lld",
  3081. req->request_id);
  3082. spin_unlock_bh(&ctx->lock);
  3083. return rc;
  3084. }
  3085. if (dump_only_event_record) {
  3086. spin_unlock_bh(&ctx->lock);
  3087. return rc;
  3088. }
  3089. rc = __cam_isp_ctx_dump_req_info(ctx, req, cpu_addr,
  3090. buf_len, &dump_info->offset);
  3091. if (rc) {
  3092. CAM_ERR(CAM_ISP, "Dump Req info fail %lld",
  3093. req->request_id);
  3094. spin_unlock_bh(&ctx->lock);
  3095. return rc;
  3096. }
  3097. spin_unlock_bh(&ctx->lock);
  3098. if (ctx->hw_mgr_intf->hw_dump) {
  3099. dump_args.offset = dump_info->offset;
  3100. dump_args.request_id = dump_info->req_id;
  3101. dump_args.buf_handle = dump_info->buf_handle;
  3102. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3103. rc = ctx->hw_mgr_intf->hw_dump(
  3104. ctx->hw_mgr_intf->hw_mgr_priv,
  3105. &dump_args);
  3106. dump_info->offset = dump_args.offset;
  3107. }
  3108. return rc;
  3109. }
  3110. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3111. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3112. {
  3113. int i, rc, tmp = 0;
  3114. uint32_t cancel_req_id_found = 0;
  3115. struct cam_ctx_request *req;
  3116. struct cam_ctx_request *req_temp;
  3117. struct cam_isp_ctx_req *req_isp;
  3118. struct list_head flush_list;
  3119. struct cam_isp_context *ctx_isp = NULL;
  3120. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3121. INIT_LIST_HEAD(&flush_list);
  3122. if (list_empty(req_list)) {
  3123. CAM_DBG(CAM_ISP, "request list is empty");
  3124. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3125. CAM_ERR(CAM_ISP, "no request to cancel");
  3126. return -EINVAL;
  3127. } else
  3128. return 0;
  3129. }
  3130. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3131. flush_req->type, flush_req->req_id);
  3132. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3133. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3134. if (req->request_id != flush_req->req_id) {
  3135. continue;
  3136. } else {
  3137. list_del_init(&req->list);
  3138. list_add_tail(&req->list, &flush_list);
  3139. cancel_req_id_found = 1;
  3140. __cam_isp_ctx_update_state_monitor_array(
  3141. ctx_isp,
  3142. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3143. req->request_id);
  3144. break;
  3145. }
  3146. }
  3147. list_del_init(&req->list);
  3148. list_add_tail(&req->list, &flush_list);
  3149. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3150. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3151. }
  3152. if (list_empty(&flush_list)) {
  3153. /*
  3154. * Maybe the req isn't sent to KMD since UMD already skip
  3155. * req in CSL layer.
  3156. */
  3157. CAM_INFO(CAM_ISP,
  3158. "flush list is empty, flush type %d for req %llu",
  3159. flush_req->type, flush_req->req_id);
  3160. return 0;
  3161. }
  3162. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3163. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3164. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3165. if (req_isp->fence_map_out[i].sync_id != -1) {
  3166. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3167. req->request_id,
  3168. req_isp->fence_map_out[i].sync_id);
  3169. rc = cam_sync_signal(
  3170. req_isp->fence_map_out[i].sync_id,
  3171. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3172. CAM_SYNC_ISP_EVENT_FLUSH);
  3173. if (rc) {
  3174. tmp = req_isp->fence_map_out[i].sync_id;
  3175. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3176. "signal fence %d failed", tmp);
  3177. }
  3178. req_isp->fence_map_out[i].sync_id = -1;
  3179. }
  3180. }
  3181. req_isp->reapply = false;
  3182. req_isp->cdm_reset_before_apply = false;
  3183. list_del_init(&req->list);
  3184. list_add_tail(&req->list, &ctx->free_req_list);
  3185. }
  3186. return 0;
  3187. }
  3188. static int __cam_isp_ctx_flush_req_in_top_state(
  3189. struct cam_context *ctx,
  3190. struct cam_req_mgr_flush_request *flush_req)
  3191. {
  3192. int rc = 0;
  3193. struct cam_isp_context *ctx_isp;
  3194. struct cam_isp_stop_args stop_isp;
  3195. struct cam_hw_stop_args stop_args;
  3196. struct cam_hw_reset_args reset_args;
  3197. struct cam_hw_cmd_args hw_cmd_args;
  3198. struct cam_req_mgr_timer_notify timer;
  3199. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3200. CAM_DBG(CAM_ISP, "Flush pending list");
  3201. spin_lock_bh(&ctx->lock);
  3202. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3203. spin_unlock_bh(&ctx->lock);
  3204. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3205. if (ctx->state <= CAM_CTX_READY) {
  3206. ctx->state = CAM_CTX_ACQUIRED;
  3207. goto end;
  3208. }
  3209. spin_lock_bh(&ctx->lock);
  3210. ctx->state = CAM_CTX_FLUSHED;
  3211. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3212. spin_unlock_bh(&ctx->lock);
  3213. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3214. flush_req->req_id, ctx->ctx_id);
  3215. ctx->last_flush_req = flush_req->req_id;
  3216. memset(&hw_cmd_args, 0, sizeof(hw_cmd_args));
  3217. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  3218. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH;
  3219. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3220. &hw_cmd_args);
  3221. if (rc) {
  3222. CAM_ERR(CAM_ISP, "Reg dump on flush failed rc: %d", rc);
  3223. rc = 0;
  3224. }
  3225. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3226. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3227. stop_isp.stop_only = true;
  3228. stop_args.args = (void *)&stop_isp;
  3229. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3230. &stop_args);
  3231. if (rc)
  3232. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3233. rc);
  3234. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3235. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3236. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3237. timer.link_hdl = ctx->link_hdl;
  3238. timer.dev_hdl = ctx->dev_hdl;
  3239. timer.state = false;
  3240. ctx->ctx_crm_intf->notify_timer(&timer);
  3241. }
  3242. spin_lock_bh(&ctx->lock);
  3243. if (!list_empty(&ctx->wait_req_list))
  3244. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3245. flush_req);
  3246. if (!list_empty(&ctx->active_req_list))
  3247. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3248. flush_req);
  3249. ctx_isp->active_req_cnt = 0;
  3250. spin_unlock_bh(&ctx->lock);
  3251. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3252. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3253. &reset_args);
  3254. if (rc)
  3255. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3256. ctx_isp->init_received = false;
  3257. }
  3258. end:
  3259. ctx_isp->bubble_frame_cnt = 0;
  3260. atomic_set(&ctx_isp->process_bubble, 0);
  3261. atomic_set(&ctx_isp->rxd_epoch, 0);
  3262. return rc;
  3263. }
  3264. static int __cam_isp_ctx_flush_req_in_ready(
  3265. struct cam_context *ctx,
  3266. struct cam_req_mgr_flush_request *flush_req)
  3267. {
  3268. int rc = 0;
  3269. CAM_DBG(CAM_ISP, "try to flush pending list");
  3270. spin_lock_bh(&ctx->lock);
  3271. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3272. /* if nothing is in pending req list, change state to acquire */
  3273. if (list_empty(&ctx->pending_req_list))
  3274. ctx->state = CAM_CTX_ACQUIRED;
  3275. spin_unlock_bh(&ctx->lock);
  3276. trace_cam_context_state("ISP", ctx);
  3277. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3278. ctx->state);
  3279. return rc;
  3280. }
  3281. static struct cam_ctx_ops
  3282. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3283. /* SOF */
  3284. {
  3285. .ioctl_ops = {},
  3286. .crm_ops = {
  3287. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3288. .notify_frame_skip =
  3289. __cam_isp_ctx_apply_default_req_settings,
  3290. },
  3291. .irq_ops = NULL,
  3292. },
  3293. /* APPLIED */
  3294. {
  3295. .ioctl_ops = {},
  3296. .crm_ops = {},
  3297. .irq_ops = NULL,
  3298. },
  3299. /* EPOCH */
  3300. {
  3301. .ioctl_ops = {},
  3302. .crm_ops = {
  3303. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3304. .notify_frame_skip =
  3305. __cam_isp_ctx_apply_default_req_settings,
  3306. },
  3307. .irq_ops = NULL,
  3308. },
  3309. /* BUBBLE */
  3310. {
  3311. .ioctl_ops = {},
  3312. .crm_ops = {
  3313. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3314. .notify_frame_skip =
  3315. __cam_isp_ctx_apply_default_req_settings,
  3316. },
  3317. .irq_ops = NULL,
  3318. },
  3319. /* Bubble Applied */
  3320. {
  3321. .ioctl_ops = {},
  3322. .crm_ops = {},
  3323. .irq_ops = NULL,
  3324. },
  3325. /* HW ERROR */
  3326. {
  3327. .ioctl_ops = {},
  3328. .crm_ops = {},
  3329. .irq_ops = NULL,
  3330. },
  3331. /* HALT */
  3332. {
  3333. .ioctl_ops = {},
  3334. .crm_ops = {},
  3335. .irq_ops = NULL,
  3336. },
  3337. };
  3338. static struct cam_ctx_ops
  3339. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3340. /* SOF */
  3341. {
  3342. .ioctl_ops = {},
  3343. .crm_ops = {
  3344. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3345. },
  3346. .irq_ops = NULL,
  3347. },
  3348. /* APPLIED */
  3349. {
  3350. .ioctl_ops = {},
  3351. .crm_ops = {},
  3352. .irq_ops = NULL,
  3353. },
  3354. /* EPOCH */
  3355. {
  3356. .ioctl_ops = {},
  3357. .crm_ops = {
  3358. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3359. },
  3360. .irq_ops = NULL,
  3361. },
  3362. /* BUBBLE */
  3363. {
  3364. .ioctl_ops = {},
  3365. .crm_ops = {
  3366. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3367. },
  3368. .irq_ops = NULL,
  3369. },
  3370. /* Bubble Applied */
  3371. {
  3372. .ioctl_ops = {},
  3373. .crm_ops = {},
  3374. .irq_ops = NULL,
  3375. },
  3376. /* HW ERROR */
  3377. {
  3378. .ioctl_ops = {},
  3379. .crm_ops = {},
  3380. .irq_ops = NULL,
  3381. },
  3382. /* HALT */
  3383. {
  3384. .ioctl_ops = {},
  3385. .crm_ops = {},
  3386. .irq_ops = NULL,
  3387. },
  3388. };
  3389. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3390. struct cam_isp_context *ctx_isp, void *evt_data)
  3391. {
  3392. int rc = 0;
  3393. struct cam_context *ctx = ctx_isp->base;
  3394. struct cam_req_mgr_trigger_notify notify;
  3395. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3396. uint64_t request_id = 0;
  3397. if (!evt_data) {
  3398. CAM_ERR(CAM_ISP, "in valid sof event data");
  3399. return -EINVAL;
  3400. }
  3401. ctx_isp->frame_id++;
  3402. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3403. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3404. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3405. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3406. /*
  3407. * notify reqmgr with sof signal. Note, due to scheduling delay
  3408. * we can run into situation that two active requests has already
  3409. * be in the active queue while we try to do the notification.
  3410. * In this case, we need to skip the current notification. This
  3411. * helps the state machine to catch up the delay.
  3412. */
  3413. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger &&
  3414. ctx_isp->active_req_cnt <= 2) {
  3415. notify.link_hdl = ctx->link_hdl;
  3416. notify.dev_hdl = ctx->dev_hdl;
  3417. notify.frame_id = ctx_isp->frame_id;
  3418. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3419. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3420. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3421. ctx->ctx_crm_intf->notify_trigger(&notify);
  3422. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3423. ctx_isp->frame_id);
  3424. /*
  3425. * It is idle frame with out any applied request id, send
  3426. * request id as zero
  3427. */
  3428. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3429. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3430. } else {
  3431. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3432. }
  3433. if (list_empty(&ctx->active_req_list))
  3434. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3435. else
  3436. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3437. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3438. __cam_isp_ctx_substate_val_to_type(
  3439. ctx_isp->substate_activated));
  3440. return rc;
  3441. }
  3442. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3443. struct cam_isp_context *ctx_isp, void *evt_data)
  3444. {
  3445. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3446. if (!evt_data) {
  3447. CAM_ERR(CAM_ISP, "in valid sof event data");
  3448. return -EINVAL;
  3449. }
  3450. ctx_isp->frame_id++;
  3451. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3452. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3453. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3454. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3455. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3456. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3457. __cam_isp_ctx_substate_val_to_type(
  3458. ctx_isp->substate_activated));
  3459. return 0;
  3460. }
  3461. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3462. struct cam_isp_context *ctx_isp, void *evt_data)
  3463. {
  3464. struct cam_ctx_request *req;
  3465. struct cam_isp_ctx_req *req_isp;
  3466. struct cam_context *ctx = ctx_isp->base;
  3467. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3468. uint64_t request_id = 0;
  3469. /*
  3470. * Sof in bubble applied state means, reg update not received.
  3471. * before increment frame id and override time stamp value, send
  3472. * the previous sof time stamp that got captured in the
  3473. * sof in applied state.
  3474. */
  3475. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3476. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3477. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3478. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3479. ctx_isp->frame_id++;
  3480. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3481. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3482. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3483. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3484. if (list_empty(&ctx->wait_req_list)) {
  3485. /*
  3486. * If no pending req in epoch, this is an error case.
  3487. * The recovery is to go back to sof state
  3488. */
  3489. CAM_ERR(CAM_ISP, "No wait request");
  3490. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3491. /* Send SOF event as empty frame*/
  3492. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3493. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3494. goto end;
  3495. }
  3496. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3497. list);
  3498. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3499. req_isp->bubble_detected = true;
  3500. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3501. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3502. req_isp->reapply = true;
  3503. req_isp->cdm_reset_before_apply = false;
  3504. if (req_isp->bubble_report && ctx->ctx_crm_intf &&
  3505. ctx->ctx_crm_intf->notify_err) {
  3506. struct cam_req_mgr_error_notify notify;
  3507. notify.link_hdl = ctx->link_hdl;
  3508. notify.dev_hdl = ctx->dev_hdl;
  3509. notify.req_id = req->request_id;
  3510. notify.error = CRM_KMD_ERR_BUBBLE;
  3511. notify.trigger = 0;
  3512. if (ctx_isp->subscribe_event & CAM_TRIGGER_POINT_SOF)
  3513. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3514. notify.frame_id = ctx_isp->frame_id;
  3515. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3516. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3517. "Notify CRM about Bubble req %lld frame %lld ctx %u",
  3518. req->request_id,
  3519. ctx_isp->frame_id,
  3520. ctx->ctx_id);
  3521. ctx->ctx_crm_intf->notify_err(&notify);
  3522. } else {
  3523. req_isp->bubble_report = 0;
  3524. }
  3525. /*
  3526. * Always move the request to active list. Let buf done
  3527. * function handles the rest.
  3528. */
  3529. list_del_init(&req->list);
  3530. list_add_tail(&req->list, &ctx->active_req_list);
  3531. ctx_isp->active_req_cnt++;
  3532. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  3533. req->request_id, ctx_isp->active_req_cnt);
  3534. if (!req_isp->bubble_report) {
  3535. if (req->request_id > ctx_isp->reported_req_id) {
  3536. request_id = req->request_id;
  3537. ctx_isp->reported_req_id = request_id;
  3538. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3539. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3540. } else
  3541. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3542. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3543. } else
  3544. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3545. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3546. /* change the state to bubble, as reg update has not come */
  3547. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3548. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3549. __cam_isp_ctx_substate_val_to_type(
  3550. ctx_isp->substate_activated));
  3551. end:
  3552. return 0;
  3553. }
  3554. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  3555. struct cam_isp_context *ctx_isp, void *evt_data)
  3556. {
  3557. uint32_t i;
  3558. struct cam_ctx_request *req;
  3559. struct cam_context *ctx = ctx_isp->base;
  3560. struct cam_req_mgr_trigger_notify notify;
  3561. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3562. struct cam_isp_ctx_req *req_isp;
  3563. uint64_t request_id = 0;
  3564. if (!evt_data) {
  3565. CAM_ERR(CAM_ISP, "in valid sof event data");
  3566. return -EINVAL;
  3567. }
  3568. ctx_isp->frame_id++;
  3569. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3570. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3571. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3572. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3573. /*
  3574. * Signal all active requests with error and move the all the active
  3575. * requests to free list
  3576. */
  3577. while (!list_empty(&ctx->active_req_list)) {
  3578. req = list_first_entry(&ctx->active_req_list,
  3579. struct cam_ctx_request, list);
  3580. list_del_init(&req->list);
  3581. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3582. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  3583. req_isp->num_fence_map_out);
  3584. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3585. if (req_isp->fence_map_out[i].sync_id != -1) {
  3586. cam_sync_signal(
  3587. req_isp->fence_map_out[i].sync_id,
  3588. CAM_SYNC_STATE_SIGNALED_ERROR,
  3589. CAM_SYNC_ISP_EVENT_BUBBLE);
  3590. }
  3591. list_add_tail(&req->list, &ctx->free_req_list);
  3592. ctx_isp->active_req_cnt--;
  3593. }
  3594. /* notify reqmgr with sof signal */
  3595. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3596. notify.link_hdl = ctx->link_hdl;
  3597. notify.dev_hdl = ctx->dev_hdl;
  3598. notify.frame_id = ctx_isp->frame_id;
  3599. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3600. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3601. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3602. ctx->ctx_crm_intf->notify_trigger(&notify);
  3603. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3604. ctx_isp->frame_id);
  3605. } else {
  3606. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3607. }
  3608. /*
  3609. * It is idle frame with out any applied request id, send
  3610. * request id as zero
  3611. */
  3612. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3613. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3614. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3615. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3616. __cam_isp_ctx_substate_val_to_type(
  3617. ctx_isp->substate_activated));
  3618. return 0;
  3619. }
  3620. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  3621. struct cam_isp_context *ctx_isp, void *evt_data)
  3622. {
  3623. struct cam_ctx_request *req = NULL;
  3624. struct cam_context *ctx = ctx_isp->base;
  3625. req = list_first_entry(&ctx->active_req_list,
  3626. struct cam_ctx_request, list);
  3627. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  3628. return 0;
  3629. }
  3630. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  3631. struct cam_isp_context *ctx_isp, void *evt_data)
  3632. {
  3633. struct cam_ctx_request *req = NULL;
  3634. struct cam_context *ctx = ctx_isp->base;
  3635. struct cam_isp_ctx_req *req_isp;
  3636. struct cam_req_mgr_trigger_notify notify;
  3637. uint64_t request_id = 0;
  3638. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3639. /* notify reqmgr with sof signal*/
  3640. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_trigger) {
  3641. if (list_empty(&ctx->wait_req_list)) {
  3642. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  3643. goto error;
  3644. }
  3645. req = list_first_entry(&ctx->wait_req_list,
  3646. struct cam_ctx_request, list);
  3647. list_del_init(&req->list);
  3648. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3649. request_id =
  3650. (req_isp->hw_update_data.packet_opcode_type ==
  3651. CAM_ISP_PACKET_INIT_DEV) ?
  3652. 0 : req->request_id;
  3653. if (req_isp->num_fence_map_out != 0) {
  3654. list_add_tail(&req->list, &ctx->active_req_list);
  3655. ctx_isp->active_req_cnt++;
  3656. CAM_DBG(CAM_ISP,
  3657. "move request %lld to active list(cnt = %d)",
  3658. req->request_id, ctx_isp->active_req_cnt);
  3659. /* if packet has buffers, set correct request id */
  3660. request_id = req->request_id;
  3661. } else {
  3662. /* no io config, so the request is completed. */
  3663. list_add_tail(&req->list, &ctx->free_req_list);
  3664. CAM_DBG(CAM_ISP,
  3665. "move active req %lld to free list(cnt=%d)",
  3666. req->request_id, ctx_isp->active_req_cnt);
  3667. }
  3668. notify.link_hdl = ctx->link_hdl;
  3669. notify.dev_hdl = ctx->dev_hdl;
  3670. notify.frame_id = ctx_isp->frame_id;
  3671. notify.trigger = CAM_TRIGGER_POINT_SOF;
  3672. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  3673. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  3674. ctx->ctx_crm_intf->notify_trigger(&notify);
  3675. CAM_DBG(CAM_ISP, "Notify CRM SOF frame %lld",
  3676. ctx_isp->frame_id);
  3677. } else {
  3678. CAM_ERR(CAM_ISP, "Can not notify SOF to CRM");
  3679. }
  3680. if (request_id)
  3681. ctx_isp->reported_req_id = request_id;
  3682. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3683. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3684. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3685. __cam_isp_ctx_substate_val_to_type(
  3686. ctx_isp->substate_activated));
  3687. __cam_isp_ctx_update_event_record(ctx_isp,
  3688. CAM_ISP_CTX_EVENT_RUP, req);
  3689. return 0;
  3690. error:
  3691. /* Send SOF event as idle frame*/
  3692. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3693. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3694. __cam_isp_ctx_update_event_record(ctx_isp,
  3695. CAM_ISP_CTX_EVENT_RUP, NULL);
  3696. /*
  3697. * There is no request in the pending list, move the sub state machine
  3698. * to SOF sub state
  3699. */
  3700. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3701. return 0;
  3702. }
  3703. static struct cam_isp_ctx_irq_ops
  3704. cam_isp_ctx_rdi_only_activated_state_machine_irq
  3705. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3706. /* SOF */
  3707. {
  3708. .irq_ops = {
  3709. NULL,
  3710. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3711. __cam_isp_ctx_reg_upd_in_sof,
  3712. NULL,
  3713. NULL,
  3714. NULL,
  3715. },
  3716. },
  3717. /* APPLIED */
  3718. {
  3719. .irq_ops = {
  3720. __cam_isp_ctx_handle_error,
  3721. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  3722. __cam_isp_ctx_reg_upd_in_applied_state,
  3723. NULL,
  3724. NULL,
  3725. __cam_isp_ctx_buf_done_in_applied,
  3726. },
  3727. },
  3728. /* EPOCH */
  3729. {
  3730. .irq_ops = {
  3731. __cam_isp_ctx_handle_error,
  3732. __cam_isp_ctx_rdi_only_sof_in_top_state,
  3733. NULL,
  3734. NULL,
  3735. NULL,
  3736. __cam_isp_ctx_buf_done_in_epoch,
  3737. },
  3738. },
  3739. /* BUBBLE*/
  3740. {
  3741. .irq_ops = {
  3742. __cam_isp_ctx_handle_error,
  3743. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  3744. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  3745. NULL,
  3746. NULL,
  3747. __cam_isp_ctx_buf_done_in_bubble,
  3748. },
  3749. },
  3750. /* BUBBLE APPLIED ie PRE_BUBBLE */
  3751. {
  3752. .irq_ops = {
  3753. __cam_isp_ctx_handle_error,
  3754. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  3755. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  3756. NULL,
  3757. NULL,
  3758. __cam_isp_ctx_buf_done_in_bubble_applied,
  3759. },
  3760. },
  3761. /* HW ERROR */
  3762. {
  3763. },
  3764. /* HALT */
  3765. {
  3766. },
  3767. };
  3768. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  3769. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3770. {
  3771. int rc = 0;
  3772. struct cam_isp_context *ctx_isp =
  3773. (struct cam_isp_context *) ctx->ctx_priv;
  3774. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3775. __cam_isp_ctx_substate_val_to_type(
  3776. ctx_isp->substate_activated));
  3777. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3778. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3779. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3780. __cam_isp_ctx_substate_val_to_type(
  3781. ctx_isp->substate_activated));
  3782. if (rc)
  3783. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3784. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  3785. ctx->ctx_id,
  3786. __cam_isp_ctx_substate_val_to_type(
  3787. ctx_isp->substate_activated), rc);
  3788. return rc;
  3789. }
  3790. static struct cam_ctx_ops
  3791. cam_isp_ctx_rdi_only_activated_state_machine
  3792. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  3793. /* SOF */
  3794. {
  3795. .ioctl_ops = {},
  3796. .crm_ops = {
  3797. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3798. },
  3799. .irq_ops = NULL,
  3800. },
  3801. /* APPLIED */
  3802. {
  3803. .ioctl_ops = {},
  3804. .crm_ops = {},
  3805. .irq_ops = NULL,
  3806. },
  3807. /* EPOCH */
  3808. {
  3809. .ioctl_ops = {},
  3810. .crm_ops = {
  3811. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  3812. },
  3813. .irq_ops = NULL,
  3814. },
  3815. /* PRE BUBBLE */
  3816. {
  3817. .ioctl_ops = {},
  3818. .crm_ops = {},
  3819. .irq_ops = NULL,
  3820. },
  3821. /* BUBBLE */
  3822. {
  3823. .ioctl_ops = {},
  3824. .crm_ops = {},
  3825. .irq_ops = NULL,
  3826. },
  3827. /* HW ERROR */
  3828. {
  3829. .ioctl_ops = {},
  3830. .crm_ops = {},
  3831. .irq_ops = NULL,
  3832. },
  3833. /* HALT */
  3834. {
  3835. .ioctl_ops = {},
  3836. .crm_ops = {},
  3837. .irq_ops = NULL,
  3838. },
  3839. };
  3840. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  3841. {
  3842. kfree(ctx->out_map_entries);
  3843. kfree(ctx->in_map_entries);
  3844. kfree(ctx->hw_update_entry);
  3845. ctx->out_map_entries = NULL;
  3846. ctx->in_map_entries = NULL;
  3847. ctx->hw_update_entry = NULL;
  3848. ctx->max_out_map_entries = 0;
  3849. ctx->max_in_map_entries = 0;
  3850. ctx->max_hw_update_entries = 0;
  3851. }
  3852. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  3853. void *cmd)
  3854. {
  3855. int rc = 0;
  3856. struct cam_hw_release_args rel_arg;
  3857. struct cam_isp_context *ctx_isp =
  3858. (struct cam_isp_context *) ctx->ctx_priv;
  3859. struct cam_req_mgr_flush_request flush_req;
  3860. int i;
  3861. if (ctx_isp->hw_ctx) {
  3862. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3863. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3864. &rel_arg);
  3865. ctx_isp->hw_ctx = NULL;
  3866. } else {
  3867. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  3868. }
  3869. ctx->last_flush_req = 0;
  3870. ctx_isp->custom_enabled = false;
  3871. ctx_isp->use_frame_header_ts = false;
  3872. ctx_isp->use_default_apply = false;
  3873. ctx_isp->frame_id = 0;
  3874. ctx_isp->active_req_cnt = 0;
  3875. ctx_isp->reported_req_id = 0;
  3876. ctx_isp->hw_acquired = false;
  3877. ctx_isp->init_received = false;
  3878. ctx_isp->support_consumed_addr = false;
  3879. ctx_isp->req_info.last_bufdone_req_id = 0;
  3880. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3881. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3882. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3883. /*
  3884. * Ideally, we should never have any active request here.
  3885. * But we still add some sanity check code here to help the debug
  3886. */
  3887. if (!list_empty(&ctx->active_req_list))
  3888. CAM_WARN(CAM_ISP, "Active list is not empty");
  3889. /* Flush all the pending request list */
  3890. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3891. flush_req.link_hdl = ctx->link_hdl;
  3892. flush_req.dev_hdl = ctx->dev_hdl;
  3893. flush_req.req_id = 0;
  3894. CAM_DBG(CAM_ISP, "try to flush pending list");
  3895. spin_lock_bh(&ctx->lock);
  3896. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3897. spin_unlock_bh(&ctx->lock);
  3898. __cam_isp_ctx_free_mem_hw_entries(ctx);
  3899. ctx->state = CAM_CTX_ACQUIRED;
  3900. trace_cam_context_state("ISP", ctx);
  3901. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3902. ctx->ctx_id, ctx->state);
  3903. return rc;
  3904. }
  3905. /* top level state machine */
  3906. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  3907. struct cam_release_dev_cmd *cmd)
  3908. {
  3909. int rc = 0;
  3910. int i;
  3911. struct cam_hw_release_args rel_arg;
  3912. struct cam_isp_context *ctx_isp =
  3913. (struct cam_isp_context *) ctx->ctx_priv;
  3914. struct cam_req_mgr_flush_request flush_req;
  3915. if (cmd && ctx_isp->hw_ctx) {
  3916. CAM_ERR(CAM_ISP, "releasing hw");
  3917. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  3918. }
  3919. if (ctx_isp->hw_ctx) {
  3920. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3921. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  3922. &rel_arg);
  3923. ctx_isp->hw_ctx = NULL;
  3924. }
  3925. ctx->session_hdl = -1;
  3926. ctx->dev_hdl = -1;
  3927. ctx->link_hdl = -1;
  3928. ctx->ctx_crm_intf = NULL;
  3929. ctx->last_flush_req = 0;
  3930. ctx_isp->frame_id = 0;
  3931. ctx_isp->active_req_cnt = 0;
  3932. ctx_isp->reported_req_id = 0;
  3933. ctx_isp->hw_acquired = false;
  3934. ctx_isp->init_received = false;
  3935. ctx_isp->offline_context = false;
  3936. ctx_isp->rdi_only_context = false;
  3937. ctx_isp->req_info.last_bufdone_req_id = 0;
  3938. atomic64_set(&ctx_isp->state_monitor_head, -1);
  3939. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  3940. atomic64_set(&ctx_isp->event_record_head[i], -1);
  3941. /*
  3942. * Ideally, we should never have any active request here.
  3943. * But we still add some sanity check code here to help the debug
  3944. */
  3945. if (!list_empty(&ctx->active_req_list))
  3946. CAM_ERR(CAM_ISP, "Active list is not empty");
  3947. /* Flush all the pending request list */
  3948. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  3949. flush_req.link_hdl = ctx->link_hdl;
  3950. flush_req.dev_hdl = ctx->dev_hdl;
  3951. flush_req.req_id = 0;
  3952. CAM_DBG(CAM_ISP, "try to flush pending list");
  3953. spin_lock_bh(&ctx->lock);
  3954. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  3955. spin_unlock_bh(&ctx->lock);
  3956. __cam_isp_ctx_free_mem_hw_entries(ctx);
  3957. ctx->state = CAM_CTX_AVAILABLE;
  3958. trace_cam_context_state("ISP", ctx);
  3959. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  3960. ctx->ctx_id, ctx->state);
  3961. return rc;
  3962. }
  3963. static int __cam_isp_ctx_config_dev_in_top_state(
  3964. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  3965. {
  3966. int rc = 0, i;
  3967. struct cam_ctx_request *req = NULL;
  3968. struct cam_isp_ctx_req *req_isp;
  3969. uintptr_t packet_addr;
  3970. struct cam_packet *packet;
  3971. size_t len = 0;
  3972. size_t remain_len = 0;
  3973. struct cam_hw_prepare_update_args cfg = {0};
  3974. struct cam_req_mgr_add_request add_req;
  3975. struct cam_isp_context *ctx_isp =
  3976. (struct cam_isp_context *) ctx->ctx_priv;
  3977. struct cam_hw_cmd_args hw_cmd_args;
  3978. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3979. uint32_t packet_opcode = 0;
  3980. CAM_DBG(CAM_ISP, "get free request object......");
  3981. /* get free request */
  3982. spin_lock_bh(&ctx->lock);
  3983. if (!list_empty(&ctx->free_req_list)) {
  3984. req = list_first_entry(&ctx->free_req_list,
  3985. struct cam_ctx_request, list);
  3986. list_del_init(&req->list);
  3987. }
  3988. spin_unlock_bh(&ctx->lock);
  3989. if (!req) {
  3990. CAM_ERR(CAM_ISP, "No more request obj free");
  3991. return -ENOMEM;
  3992. }
  3993. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3994. /* for config dev, only memory handle is supported */
  3995. /* map packet from the memhandle */
  3996. rc = cam_mem_get_cpu_buf((int32_t) cmd->packet_handle,
  3997. &packet_addr, &len);
  3998. if (rc != 0) {
  3999. CAM_ERR(CAM_ISP, "Can not get packet address");
  4000. rc = -EINVAL;
  4001. goto free_req;
  4002. }
  4003. remain_len = len;
  4004. if ((len < sizeof(struct cam_packet)) ||
  4005. ((size_t)cmd->offset >= len - sizeof(struct cam_packet))) {
  4006. CAM_ERR(CAM_ISP, "invalid buff length: %zu or offset", len);
  4007. rc = -EINVAL;
  4008. goto free_req;
  4009. }
  4010. remain_len -= (size_t)cmd->offset;
  4011. packet = (struct cam_packet *)(packet_addr + (uint32_t)cmd->offset);
  4012. CAM_DBG(CAM_ISP, "pack_handle %llx", cmd->packet_handle);
  4013. CAM_DBG(CAM_ISP, "packet address is 0x%zx", packet_addr);
  4014. CAM_DBG(CAM_ISP, "packet with length %zu, offset 0x%llx",
  4015. len, cmd->offset);
  4016. CAM_DBG(CAM_ISP, "Packet request id %lld",
  4017. packet->header.request_id);
  4018. CAM_DBG(CAM_ISP, "Packet size 0x%x", packet->header.size);
  4019. CAM_DBG(CAM_ISP, "packet op %d", packet->header.op_code);
  4020. /* Query the packet opcode */
  4021. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4022. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4023. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  4024. isp_hw_cmd_args.cmd_data = (void *)packet;
  4025. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4026. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4027. &hw_cmd_args);
  4028. if (rc) {
  4029. CAM_ERR(CAM_ISP, "HW command failed");
  4030. goto free_req;
  4031. }
  4032. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  4033. CAM_DBG(CAM_ISP, "packet op %d", packet_opcode);
  4034. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  4035. && (packet->header.request_id <= ctx->last_flush_req)) {
  4036. CAM_INFO(CAM_ISP,
  4037. "request %lld has been flushed, reject packet",
  4038. packet->header.request_id);
  4039. rc = -EBADR;
  4040. goto free_req;
  4041. }
  4042. cfg.packet = packet;
  4043. cfg.remain_len = remain_len;
  4044. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4045. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  4046. cfg.hw_update_entries = req_isp->cfg;
  4047. cfg.max_out_map_entries = ctx->max_out_map_entries;
  4048. cfg.max_in_map_entries = ctx->max_in_map_entries;
  4049. cfg.out_map_entries = req_isp->fence_map_out;
  4050. cfg.in_map_entries = req_isp->fence_map_in;
  4051. cfg.priv = &req_isp->hw_update_data;
  4052. cfg.pf_data = &(req->pf_data);
  4053. cfg.num_out_map_entries = 0;
  4054. cfg.num_in_map_entries = 0;
  4055. CAM_DBG(CAM_ISP, "try to prepare config packet......");
  4056. rc = ctx->hw_mgr_intf->hw_prepare_update(
  4057. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4058. if (rc != 0) {
  4059. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4060. rc = -EFAULT;
  4061. goto free_req;
  4062. }
  4063. req_isp->num_cfg = cfg.num_hw_update_entries;
  4064. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4065. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4066. req_isp->num_acked = 0;
  4067. req_isp->num_deferred_acks = 0;
  4068. req_isp->bubble_detected = false;
  4069. req_isp->cdm_reset_before_apply = false;
  4070. req_isp->hw_update_data.packet = packet;
  4071. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4072. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4073. if (rc) {
  4074. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4075. req_isp->fence_map_out[i].sync_id);
  4076. goto put_ref;
  4077. }
  4078. }
  4079. CAM_DBG(CAM_ISP, "num_entry: %d, num fence out: %d, num fence in: %d",
  4080. req_isp->num_cfg, req_isp->num_fence_map_out,
  4081. req_isp->num_fence_map_in);
  4082. req->request_id = packet->header.request_id;
  4083. req->status = 1;
  4084. CAM_DBG(CAM_ISP, "Packet request id %lld packet opcode:%d",
  4085. packet->header.request_id,
  4086. req_isp->hw_update_data.packet_opcode_type);
  4087. if (req_isp->hw_update_data.packet_opcode_type ==
  4088. CAM_ISP_PACKET_INIT_DEV) {
  4089. if (ctx->state < CAM_CTX_ACTIVATED) {
  4090. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4091. if (rc)
  4092. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4093. ctx_isp->init_received = true;
  4094. } else {
  4095. rc = -EINVAL;
  4096. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4097. ctx->state);
  4098. }
  4099. } else {
  4100. if (ctx_isp->offline_context) {
  4101. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4102. } else if ((ctx->state != CAM_CTX_FLUSHED) &&
  4103. (ctx->state >= CAM_CTX_READY) &&
  4104. ctx->ctx_crm_intf->add_req) {
  4105. memset(&add_req, 0, sizeof(add_req));
  4106. add_req.link_hdl = ctx->link_hdl;
  4107. add_req.dev_hdl = ctx->dev_hdl;
  4108. add_req.req_id = req->request_id;
  4109. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4110. if (rc) {
  4111. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4112. req->request_id);
  4113. } else {
  4114. __cam_isp_ctx_enqueue_request_in_order(
  4115. ctx, req);
  4116. }
  4117. } else {
  4118. rc = -EINVAL;
  4119. CAM_ERR(CAM_ISP,
  4120. "Recevied update req %lld in wrong state:%d",
  4121. req->request_id, ctx->state);
  4122. }
  4123. }
  4124. if (rc)
  4125. goto put_ref;
  4126. CAM_DBG(CAM_REQ,
  4127. "Preprocessing Config req_id %lld successful on ctx %u",
  4128. req->request_id, ctx->ctx_id);
  4129. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4130. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4131. }
  4132. return rc;
  4133. put_ref:
  4134. for (--i; i >= 0; i--) {
  4135. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4136. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4137. req_isp->fence_map_out[i].sync_id);
  4138. }
  4139. free_req:
  4140. spin_lock_bh(&ctx->lock);
  4141. list_add_tail(&req->list, &ctx->free_req_list);
  4142. spin_unlock_bh(&ctx->lock);
  4143. return rc;
  4144. }
  4145. static int __cam_isp_ctx_allocate_mem_hw_entries(
  4146. struct cam_context *ctx,
  4147. struct cam_hw_acquire_args *param)
  4148. {
  4149. int rc = 0;
  4150. uint32_t max_res = 0;
  4151. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  4152. struct cam_ctx_request *req;
  4153. struct cam_ctx_request *temp_req;
  4154. struct cam_isp_ctx_req *req_isp;
  4155. size_t num_entries = 0;
  4156. if (!param->op_params.param_list[0])
  4157. max_res = CAM_ISP_CTX_RES_MAX;
  4158. else {
  4159. max_res = param->op_params.param_list[0];
  4160. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  4161. max_res += param->op_params.param_list[1];
  4162. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  4163. }
  4164. }
  4165. ctx->max_in_map_entries = max_res;
  4166. ctx->max_out_map_entries = max_res;
  4167. ctx->max_hw_update_entries = max_hw_upd_entries;
  4168. CAM_DBG(CAM_ISP,
  4169. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  4170. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  4171. num_entries = ctx->max_hw_update_entries * CAM_ISP_CTX_REQ_MAX;
  4172. ctx->hw_update_entry = kcalloc(num_entries,
  4173. sizeof(struct cam_hw_update_entry),
  4174. GFP_KERNEL);
  4175. if (!ctx->hw_update_entry) {
  4176. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4177. return -ENOMEM;
  4178. }
  4179. num_entries = ctx->max_in_map_entries * CAM_ISP_CTX_REQ_MAX;
  4180. ctx->in_map_entries = kcalloc(num_entries,
  4181. sizeof(struct cam_hw_fence_map_entry),
  4182. GFP_KERNEL);
  4183. if (!ctx->in_map_entries) {
  4184. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4185. rc = -ENOMEM;
  4186. goto end;
  4187. }
  4188. num_entries = ctx->max_out_map_entries * CAM_ISP_CTX_REQ_MAX;
  4189. ctx->out_map_entries = kcalloc(num_entries,
  4190. sizeof(struct cam_hw_fence_map_entry),
  4191. GFP_KERNEL);
  4192. if (!ctx->out_map_entries) {
  4193. CAM_ERR(CAM_CTXT, "%s[%d] no memory", ctx->dev_name, ctx->ctx_id);
  4194. rc = -ENOMEM;
  4195. goto end;
  4196. }
  4197. list_for_each_entry_safe(req, temp_req,
  4198. &ctx->free_req_list, list) {
  4199. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4200. req_isp->cfg =
  4201. &ctx->hw_update_entry[req->index * ctx->max_hw_update_entries];
  4202. req_isp->fence_map_in =
  4203. &ctx->in_map_entries[req->index * ctx->max_in_map_entries];
  4204. req_isp->fence_map_out =
  4205. &ctx->out_map_entries[req->index * ctx->max_out_map_entries];
  4206. }
  4207. return rc;
  4208. end:
  4209. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4210. return rc;
  4211. }
  4212. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4213. struct cam_acquire_dev_cmd *cmd)
  4214. {
  4215. int rc = 0;
  4216. int i;
  4217. struct cam_hw_acquire_args param;
  4218. struct cam_isp_resource *isp_res = NULL;
  4219. struct cam_create_dev_hdl req_hdl_param;
  4220. struct cam_hw_release_args release;
  4221. struct cam_isp_context *ctx_isp =
  4222. (struct cam_isp_context *) ctx->ctx_priv;
  4223. struct cam_hw_cmd_args hw_cmd_args;
  4224. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4225. if (!ctx->hw_mgr_intf) {
  4226. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4227. rc = -EFAULT;
  4228. goto end;
  4229. }
  4230. CAM_DBG(CAM_ISP,
  4231. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4232. cmd->session_handle, cmd->num_resources,
  4233. cmd->handle_type, cmd->resource_hdl);
  4234. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4235. ctx_isp->split_acquire = true;
  4236. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4237. goto get_dev_handle;
  4238. }
  4239. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4240. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4241. rc = -ENOMEM;
  4242. goto end;
  4243. }
  4244. /* for now we only support user pointer */
  4245. if (cmd->handle_type != 1) {
  4246. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4247. rc = -EINVAL;
  4248. goto end;
  4249. }
  4250. isp_res = kzalloc(
  4251. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4252. if (!isp_res) {
  4253. rc = -ENOMEM;
  4254. goto end;
  4255. }
  4256. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4257. cmd->num_resources);
  4258. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4259. sizeof(*isp_res)*cmd->num_resources)) {
  4260. rc = -EFAULT;
  4261. goto free_res;
  4262. }
  4263. memset(&param, 0, sizeof(param));
  4264. param.context_data = ctx;
  4265. param.event_cb = ctx->irq_cb_intf;
  4266. param.num_acq = cmd->num_resources;
  4267. param.acquire_info = (uintptr_t) isp_res;
  4268. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4269. if (rc) {
  4270. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4271. ctx->ctx_id);
  4272. goto free_res;
  4273. }
  4274. /* call HW manager to reserve the resource */
  4275. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4276. &param);
  4277. if (rc != 0) {
  4278. CAM_ERR(CAM_ISP, "Acquire device failed");
  4279. goto free_res;
  4280. }
  4281. /* Query the context has rdi only resource */
  4282. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4283. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4284. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4285. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4286. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4287. &hw_cmd_args);
  4288. if (rc) {
  4289. CAM_ERR(CAM_ISP, "HW command failed");
  4290. goto free_hw;
  4291. }
  4292. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4293. /*
  4294. * this context has rdi only resource assign rdi only
  4295. * state machine
  4296. */
  4297. CAM_DBG(CAM_ISP, "RDI only session Context");
  4298. ctx_isp->substate_machine_irq =
  4299. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4300. ctx_isp->substate_machine =
  4301. cam_isp_ctx_rdi_only_activated_state_machine;
  4302. ctx_isp->rdi_only_context = true;
  4303. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4304. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4305. ctx_isp->substate_machine_irq =
  4306. cam_isp_ctx_fs2_state_machine_irq;
  4307. ctx_isp->substate_machine =
  4308. cam_isp_ctx_fs2_state_machine;
  4309. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4310. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4311. ctx_isp->substate_machine_irq =
  4312. cam_isp_ctx_offline_state_machine_irq;
  4313. } else {
  4314. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4315. ctx_isp->substate_machine_irq =
  4316. cam_isp_ctx_activated_state_machine_irq;
  4317. ctx_isp->substate_machine =
  4318. cam_isp_ctx_activated_state_machine;
  4319. }
  4320. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4321. ctx_isp->hw_acquired = true;
  4322. ctx_isp->split_acquire = false;
  4323. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4324. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4325. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4326. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4327. kfree(isp_res);
  4328. isp_res = NULL;
  4329. get_dev_handle:
  4330. req_hdl_param.session_hdl = cmd->session_handle;
  4331. /* bridge is not ready for these flags. so false for now */
  4332. req_hdl_param.v4l2_sub_dev_flag = 0;
  4333. req_hdl_param.media_entity_flag = 0;
  4334. req_hdl_param.ops = ctx->crm_ctx_intf;
  4335. req_hdl_param.priv = ctx;
  4336. req_hdl_param.dev_id = CAM_ISP;
  4337. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4338. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4339. if (ctx->dev_hdl <= 0) {
  4340. rc = -EFAULT;
  4341. CAM_ERR(CAM_ISP, "Can not create device handle");
  4342. goto free_hw;
  4343. }
  4344. cmd->dev_handle = ctx->dev_hdl;
  4345. /* store session information */
  4346. ctx->session_hdl = cmd->session_handle;
  4347. ctx->state = CAM_CTX_ACQUIRED;
  4348. trace_cam_context_state("ISP", ctx);
  4349. CAM_DBG(CAM_ISP,
  4350. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4351. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4352. return rc;
  4353. free_hw:
  4354. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4355. if (ctx_isp->hw_acquired)
  4356. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4357. &release);
  4358. ctx_isp->hw_ctx = NULL;
  4359. ctx_isp->hw_acquired = false;
  4360. free_res:
  4361. kfree(isp_res);
  4362. end:
  4363. return rc;
  4364. }
  4365. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4366. void *args)
  4367. {
  4368. int rc = 0;
  4369. int i;
  4370. struct cam_acquire_hw_cmd_v1 *cmd =
  4371. (struct cam_acquire_hw_cmd_v1 *)args;
  4372. struct cam_hw_acquire_args param;
  4373. struct cam_hw_release_args release;
  4374. struct cam_isp_context *ctx_isp =
  4375. (struct cam_isp_context *) ctx->ctx_priv;
  4376. struct cam_hw_cmd_args hw_cmd_args;
  4377. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4378. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4379. if (!ctx->hw_mgr_intf) {
  4380. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4381. rc = -EFAULT;
  4382. goto end;
  4383. }
  4384. CAM_DBG(CAM_ISP,
  4385. "session_hdl 0x%x, hdl type %d, res %lld",
  4386. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4387. /* for now we only support user pointer */
  4388. if (cmd->handle_type != 1) {
  4389. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4390. rc = -EINVAL;
  4391. goto end;
  4392. }
  4393. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4394. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4395. goto end;
  4396. }
  4397. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4398. if (!acquire_hw_info) {
  4399. rc = -ENOMEM;
  4400. goto end;
  4401. }
  4402. CAM_DBG(CAM_ISP, "start copy resources from user");
  4403. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4404. cmd->data_size)) {
  4405. rc = -EFAULT;
  4406. goto free_res;
  4407. }
  4408. memset(&param, 0, sizeof(param));
  4409. param.context_data = ctx;
  4410. param.event_cb = ctx->irq_cb_intf;
  4411. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4412. param.acquire_info_size = cmd->data_size;
  4413. param.acquire_info = (uint64_t) acquire_hw_info;
  4414. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  4415. &param);
  4416. if (rc) {
  4417. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4418. ctx->ctx_id);
  4419. goto free_res;
  4420. }
  4421. /* call HW manager to reserve the resource */
  4422. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4423. &param);
  4424. if (rc != 0) {
  4425. CAM_ERR(CAM_ISP, "Acquire device failed");
  4426. goto free_res;
  4427. }
  4428. ctx_isp->support_consumed_addr =
  4429. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  4430. /* Query the context has rdi only resource */
  4431. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4432. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4433. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4434. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4435. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4436. &hw_cmd_args);
  4437. if (rc) {
  4438. CAM_ERR(CAM_ISP, "HW command failed");
  4439. goto free_hw;
  4440. }
  4441. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4442. /*
  4443. * this context has rdi only resource assign rdi only
  4444. * state machine
  4445. */
  4446. CAM_DBG(CAM_ISP, "RDI only session Context");
  4447. ctx_isp->substate_machine_irq =
  4448. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4449. ctx_isp->substate_machine =
  4450. cam_isp_ctx_rdi_only_activated_state_machine;
  4451. ctx_isp->rdi_only_context = true;
  4452. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4453. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4454. ctx_isp->substate_machine_irq =
  4455. cam_isp_ctx_fs2_state_machine_irq;
  4456. ctx_isp->substate_machine =
  4457. cam_isp_ctx_fs2_state_machine;
  4458. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4459. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  4460. ctx_isp->substate_machine_irq =
  4461. cam_isp_ctx_offline_state_machine_irq;
  4462. ctx_isp->substate_machine = NULL;
  4463. } else {
  4464. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4465. ctx_isp->substate_machine_irq =
  4466. cam_isp_ctx_activated_state_machine_irq;
  4467. ctx_isp->substate_machine =
  4468. cam_isp_ctx_activated_state_machine;
  4469. }
  4470. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4471. ctx_isp->hw_acquired = true;
  4472. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4473. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4474. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4475. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4476. trace_cam_context_state("ISP", ctx);
  4477. CAM_DBG(CAM_ISP,
  4478. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4479. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4480. kfree(acquire_hw_info);
  4481. return rc;
  4482. free_hw:
  4483. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4484. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4485. ctx_isp->hw_ctx = NULL;
  4486. ctx_isp->hw_acquired = false;
  4487. free_res:
  4488. kfree(acquire_hw_info);
  4489. end:
  4490. return rc;
  4491. }
  4492. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  4493. {
  4494. cam_req_mgr_process_workq(w);
  4495. }
  4496. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  4497. void *args)
  4498. {
  4499. int rc = 0, i, j;
  4500. struct cam_acquire_hw_cmd_v2 *cmd =
  4501. (struct cam_acquire_hw_cmd_v2 *)args;
  4502. struct cam_hw_acquire_args param;
  4503. struct cam_hw_release_args release;
  4504. struct cam_isp_context *ctx_isp =
  4505. (struct cam_isp_context *) ctx->ctx_priv;
  4506. struct cam_hw_cmd_args hw_cmd_args;
  4507. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4508. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4509. if (!ctx->hw_mgr_intf) {
  4510. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4511. rc = -EFAULT;
  4512. goto end;
  4513. }
  4514. CAM_DBG(CAM_ISP,
  4515. "session_hdl 0x%x, hdl type %d, res %lld",
  4516. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4517. /* for now we only support user pointer */
  4518. if (cmd->handle_type != 1) {
  4519. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4520. rc = -EINVAL;
  4521. goto end;
  4522. }
  4523. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4524. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4525. goto end;
  4526. }
  4527. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4528. if (!acquire_hw_info) {
  4529. rc = -ENOMEM;
  4530. goto end;
  4531. }
  4532. CAM_DBG(CAM_ISP, "start copy resources from user");
  4533. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4534. cmd->data_size)) {
  4535. rc = -EFAULT;
  4536. goto free_res;
  4537. }
  4538. memset(&param, 0, sizeof(param));
  4539. param.context_data = ctx;
  4540. param.event_cb = ctx->irq_cb_intf;
  4541. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4542. param.acquire_info_size = cmd->data_size;
  4543. param.acquire_info = (uint64_t) acquire_hw_info;
  4544. /* call HW manager to reserve the resource */
  4545. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4546. &param);
  4547. if (rc != 0) {
  4548. CAM_ERR(CAM_ISP, "Acquire device failed");
  4549. goto free_res;
  4550. }
  4551. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4552. if (rc) {
  4553. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4554. ctx->ctx_id);
  4555. goto free_hw;
  4556. }
  4557. /*
  4558. * Set feature flag if applicable
  4559. * custom hw is supported only on v2
  4560. */
  4561. ctx_isp->custom_enabled =
  4562. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  4563. ctx_isp->use_frame_header_ts =
  4564. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  4565. ctx_isp->use_default_apply =
  4566. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  4567. ctx_isp->support_consumed_addr =
  4568. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  4569. /* Query the context has rdi only resource */
  4570. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4571. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4572. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4573. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4574. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4575. &hw_cmd_args);
  4576. if (rc) {
  4577. CAM_ERR(CAM_ISP, "HW command failed");
  4578. goto free_hw;
  4579. }
  4580. if (param.valid_acquired_hw) {
  4581. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4582. cmd->hw_info.acquired_hw_id[i] =
  4583. param.acquired_hw_id[i];
  4584. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  4585. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  4586. cmd->hw_info.acquired_hw_path[i][j] =
  4587. param.acquired_hw_path[i][j];
  4588. }
  4589. cmd->hw_info.valid_acquired_hw =
  4590. param.valid_acquired_hw;
  4591. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  4592. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4593. /*
  4594. * this context has rdi only resource assign rdi only
  4595. * state machine
  4596. */
  4597. CAM_DBG(CAM_ISP, "RDI only session Context");
  4598. ctx_isp->substate_machine_irq =
  4599. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4600. ctx_isp->substate_machine =
  4601. cam_isp_ctx_rdi_only_activated_state_machine;
  4602. ctx_isp->rdi_only_context = true;
  4603. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4604. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4605. ctx_isp->substate_machine_irq =
  4606. cam_isp_ctx_fs2_state_machine_irq;
  4607. ctx_isp->substate_machine =
  4608. cam_isp_ctx_fs2_state_machine;
  4609. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4610. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  4611. ctx_isp->substate_machine_irq =
  4612. cam_isp_ctx_offline_state_machine_irq;
  4613. ctx_isp->substate_machine = NULL;
  4614. ctx_isp->offline_context = true;
  4615. rc = cam_req_mgr_workq_create("offline_ife", 20,
  4616. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  4617. cam_req_mgr_process_workq_offline_ife_worker);
  4618. if (rc)
  4619. CAM_ERR(CAM_ISP,
  4620. "Failed to create workq for offline IFE rc:%d",
  4621. rc);
  4622. } else {
  4623. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4624. ctx_isp->substate_machine_irq =
  4625. cam_isp_ctx_activated_state_machine_irq;
  4626. ctx_isp->substate_machine =
  4627. cam_isp_ctx_activated_state_machine;
  4628. }
  4629. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4630. ctx_isp->hw_acquired = true;
  4631. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4632. trace_cam_context_state("ISP", ctx);
  4633. CAM_DBG(CAM_ISP,
  4634. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  4635. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  4636. kfree(acquire_hw_info);
  4637. return rc;
  4638. free_hw:
  4639. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4640. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  4641. ctx_isp->hw_ctx = NULL;
  4642. ctx_isp->hw_acquired = false;
  4643. free_res:
  4644. kfree(acquire_hw_info);
  4645. end:
  4646. return rc;
  4647. }
  4648. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  4649. void *args)
  4650. {
  4651. int rc = -EINVAL;
  4652. uint32_t api_version;
  4653. if (!ctx || !args) {
  4654. CAM_ERR(CAM_ISP, "Invalid input pointer");
  4655. return rc;
  4656. }
  4657. api_version = *((uint32_t *)args);
  4658. if (api_version == 1)
  4659. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  4660. else if (api_version == 2)
  4661. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  4662. else
  4663. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  4664. return rc;
  4665. }
  4666. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  4667. struct cam_config_dev_cmd *cmd)
  4668. {
  4669. int rc = 0;
  4670. struct cam_isp_context *ctx_isp =
  4671. (struct cam_isp_context *) ctx->ctx_priv;
  4672. if (!ctx_isp->hw_acquired) {
  4673. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4674. return -EINVAL;
  4675. }
  4676. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4677. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  4678. ctx->state = CAM_CTX_READY;
  4679. trace_cam_context_state("ISP", ctx);
  4680. }
  4681. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  4682. return rc;
  4683. }
  4684. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  4685. struct cam_config_dev_cmd *cmd)
  4686. {
  4687. int rc = 0;
  4688. struct cam_start_stop_dev_cmd start_cmd;
  4689. struct cam_hw_cmd_args hw_cmd_args;
  4690. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4691. struct cam_isp_context *ctx_isp =
  4692. (struct cam_isp_context *) ctx->ctx_priv;
  4693. if (!ctx_isp->hw_acquired) {
  4694. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  4695. rc = -EINVAL;
  4696. goto end;
  4697. }
  4698. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  4699. if (rc)
  4700. goto end;
  4701. if (!ctx_isp->init_received) {
  4702. CAM_WARN(CAM_ISP,
  4703. "Received update packet in flushed state, skip start");
  4704. goto end;
  4705. }
  4706. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4707. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4708. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  4709. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4710. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4711. &hw_cmd_args);
  4712. if (rc) {
  4713. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  4714. goto end;
  4715. }
  4716. start_cmd.dev_handle = cmd->dev_handle;
  4717. start_cmd.session_handle = cmd->session_handle;
  4718. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  4719. if (rc)
  4720. CAM_ERR(CAM_ISP,
  4721. "Failed to re-start HW after flush rc: %d", rc);
  4722. else
  4723. CAM_INFO(CAM_ISP,
  4724. "Received init after flush. Re-start HW complete in ctx:%d",
  4725. ctx->ctx_id);
  4726. end:
  4727. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  4728. ctx_isp->substate_activated);
  4729. return rc;
  4730. }
  4731. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  4732. struct cam_req_mgr_core_dev_link_setup *link)
  4733. {
  4734. int rc = 0;
  4735. struct cam_isp_context *ctx_isp =
  4736. (struct cam_isp_context *) ctx->ctx_priv;
  4737. CAM_DBG(CAM_ISP, "Enter.........");
  4738. ctx->link_hdl = link->link_hdl;
  4739. ctx->ctx_crm_intf = link->crm_cb;
  4740. ctx_isp->subscribe_event =
  4741. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  4742. ctx_isp->trigger_id = link->trigger_id;
  4743. /* change state only if we had the init config */
  4744. if (ctx_isp->init_received) {
  4745. ctx->state = CAM_CTX_READY;
  4746. trace_cam_context_state("ISP", ctx);
  4747. }
  4748. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  4749. return rc;
  4750. }
  4751. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  4752. struct cam_req_mgr_core_dev_link_setup *unlink)
  4753. {
  4754. int rc = 0;
  4755. struct cam_isp_context *ctx_isp =
  4756. (struct cam_isp_context *) ctx->ctx_priv;
  4757. ctx->link_hdl = -1;
  4758. ctx->ctx_crm_intf = NULL;
  4759. ctx_isp->trigger_id = -1;
  4760. return rc;
  4761. }
  4762. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  4763. struct cam_req_mgr_device_info *dev_info)
  4764. {
  4765. int rc = 0;
  4766. dev_info->dev_hdl = ctx->dev_hdl;
  4767. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  4768. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  4769. dev_info->p_delay = 1;
  4770. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  4771. dev_info->trigger_on = true;
  4772. return rc;
  4773. }
  4774. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  4775. struct cam_start_stop_dev_cmd *cmd)
  4776. {
  4777. int rc = 0;
  4778. int i;
  4779. struct cam_isp_start_args start_isp;
  4780. struct cam_ctx_request *req;
  4781. struct cam_isp_ctx_req *req_isp;
  4782. struct cam_isp_context *ctx_isp =
  4783. (struct cam_isp_context *) ctx->ctx_priv;
  4784. if (cmd->session_handle != ctx->session_hdl ||
  4785. cmd->dev_handle != ctx->dev_hdl) {
  4786. rc = -EPERM;
  4787. goto end;
  4788. }
  4789. if (list_empty(&ctx->pending_req_list)) {
  4790. /* should never happen */
  4791. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  4792. rc = -EFAULT;
  4793. goto end;
  4794. } else {
  4795. req = list_first_entry(&ctx->pending_req_list,
  4796. struct cam_ctx_request, list);
  4797. }
  4798. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4799. if (!ctx_isp->hw_ctx) {
  4800. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  4801. rc = -EFAULT;
  4802. goto end;
  4803. }
  4804. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4805. start_isp.hw_config.request_id = req->request_id;
  4806. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  4807. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  4808. start_isp.hw_config.priv = &req_isp->hw_update_data;
  4809. start_isp.hw_config.init_packet = 1;
  4810. start_isp.hw_config.reapply = 0;
  4811. start_isp.hw_config.cdm_reset_before_apply = false;
  4812. ctx_isp->last_applied_req_id = req->request_id;
  4813. if (ctx->state == CAM_CTX_FLUSHED)
  4814. start_isp.start_only = true;
  4815. else
  4816. start_isp.start_only = false;
  4817. atomic_set(&ctx_isp->process_bubble, 0);
  4818. atomic_set(&ctx_isp->rxd_epoch, 0);
  4819. ctx_isp->frame_id = 0;
  4820. ctx_isp->active_req_cnt = 0;
  4821. ctx_isp->reported_req_id = 0;
  4822. ctx_isp->bubble_frame_cnt = 0;
  4823. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  4824. CAM_ISP_CTX_ACTIVATED_APPLIED :
  4825. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  4826. CAM_ISP_CTX_ACTIVATED_SOF;
  4827. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4828. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4829. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4830. /*
  4831. * In case of CSID TPG we might receive SOF and RUP IRQs
  4832. * before hw_mgr_intf->hw_start has returned. So move
  4833. * req out of pending list before hw_start and add it
  4834. * back to pending list if hw_start fails.
  4835. */
  4836. list_del_init(&req->list);
  4837. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  4838. list_add_tail(&req->list, &ctx->free_req_list);
  4839. atomic_set(&ctx_isp->rxd_epoch, 1);
  4840. CAM_DBG(CAM_REQ,
  4841. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  4842. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4843. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  4844. list_add_tail(&req->list, &ctx->wait_req_list);
  4845. CAM_DBG(CAM_REQ,
  4846. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  4847. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  4848. } else {
  4849. list_add_tail(&req->list, &ctx->active_req_list);
  4850. ctx_isp->active_req_cnt++;
  4851. CAM_DBG(CAM_REQ,
  4852. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  4853. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  4854. ctx_isp->offline_context);
  4855. }
  4856. /*
  4857. * Only place to change state before calling the hw due to
  4858. * hardware tasklet has higher priority that can cause the
  4859. * irq handling comes early
  4860. */
  4861. ctx->state = CAM_CTX_ACTIVATED;
  4862. trace_cam_context_state("ISP", ctx);
  4863. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  4864. &start_isp);
  4865. if (rc) {
  4866. /* HW failure. user need to clean up the resource */
  4867. CAM_ERR(CAM_ISP, "Start HW failed");
  4868. ctx->state = CAM_CTX_READY;
  4869. if ((rc == -ETIMEDOUT) &&
  4870. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  4871. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  4872. trace_cam_context_state("ISP", ctx);
  4873. list_del_init(&req->list);
  4874. list_add(&req->list, &ctx->pending_req_list);
  4875. goto end;
  4876. }
  4877. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  4878. end:
  4879. return rc;
  4880. }
  4881. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  4882. struct cam_req_mgr_core_dev_link_setup *unlink)
  4883. {
  4884. int rc = 0;
  4885. ctx->link_hdl = -1;
  4886. ctx->ctx_crm_intf = NULL;
  4887. ctx->state = CAM_CTX_ACQUIRED;
  4888. trace_cam_context_state("ISP", ctx);
  4889. return rc;
  4890. }
  4891. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  4892. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  4893. {
  4894. int rc = 0;
  4895. uint32_t i;
  4896. struct cam_hw_stop_args stop;
  4897. struct cam_ctx_request *req;
  4898. struct cam_isp_ctx_req *req_isp;
  4899. struct cam_isp_context *ctx_isp =
  4900. (struct cam_isp_context *) ctx->ctx_priv;
  4901. struct cam_isp_stop_args stop_isp;
  4902. /* Mask off all the incoming hardware events */
  4903. spin_lock_bh(&ctx->lock);
  4904. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  4905. spin_unlock_bh(&ctx->lock);
  4906. /* stop hw first */
  4907. if (ctx_isp->hw_ctx) {
  4908. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4909. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  4910. stop_isp.stop_only = false;
  4911. stop.args = (void *) &stop_isp;
  4912. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  4913. &stop);
  4914. }
  4915. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4916. __cam_isp_ctx_substate_val_to_type(
  4917. ctx_isp->substate_activated));
  4918. if (ctx->ctx_crm_intf &&
  4919. ctx->ctx_crm_intf->notify_stop) {
  4920. struct cam_req_mgr_notify_stop notify;
  4921. notify.link_hdl = ctx->link_hdl;
  4922. CAM_DBG(CAM_ISP,
  4923. "Notify CRM about device stop ctx %u link 0x%x",
  4924. ctx->ctx_id, ctx->link_hdl);
  4925. ctx->ctx_crm_intf->notify_stop(&notify);
  4926. } else
  4927. CAM_ERR(CAM_ISP, "cb not present");
  4928. while (!list_empty(&ctx->pending_req_list)) {
  4929. req = list_first_entry(&ctx->pending_req_list,
  4930. struct cam_ctx_request, list);
  4931. list_del_init(&req->list);
  4932. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4933. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  4934. req_isp->num_fence_map_out);
  4935. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4936. if (req_isp->fence_map_out[i].sync_id != -1) {
  4937. cam_sync_signal(
  4938. req_isp->fence_map_out[i].sync_id,
  4939. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4940. CAM_SYNC_ISP_EVENT_HW_STOP);
  4941. }
  4942. list_add_tail(&req->list, &ctx->free_req_list);
  4943. }
  4944. while (!list_empty(&ctx->wait_req_list)) {
  4945. req = list_first_entry(&ctx->wait_req_list,
  4946. struct cam_ctx_request, list);
  4947. list_del_init(&req->list);
  4948. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4949. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  4950. req_isp->num_fence_map_out);
  4951. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4952. if (req_isp->fence_map_out[i].sync_id != -1) {
  4953. cam_sync_signal(
  4954. req_isp->fence_map_out[i].sync_id,
  4955. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4956. CAM_SYNC_ISP_EVENT_HW_STOP);
  4957. }
  4958. list_add_tail(&req->list, &ctx->free_req_list);
  4959. }
  4960. while (!list_empty(&ctx->active_req_list)) {
  4961. req = list_first_entry(&ctx->active_req_list,
  4962. struct cam_ctx_request, list);
  4963. list_del_init(&req->list);
  4964. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4965. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4966. req_isp->num_fence_map_out);
  4967. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4968. if (req_isp->fence_map_out[i].sync_id != -1) {
  4969. cam_sync_signal(
  4970. req_isp->fence_map_out[i].sync_id,
  4971. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4972. CAM_SYNC_ISP_EVENT_HW_STOP);
  4973. }
  4974. list_add_tail(&req->list, &ctx->free_req_list);
  4975. }
  4976. ctx_isp->frame_id = 0;
  4977. ctx_isp->active_req_cnt = 0;
  4978. ctx_isp->reported_req_id = 0;
  4979. ctx_isp->last_applied_req_id = 0;
  4980. ctx_isp->req_info.last_bufdone_req_id = 0;
  4981. ctx_isp->bubble_frame_cnt = 0;
  4982. atomic_set(&ctx_isp->process_bubble, 0);
  4983. atomic_set(&ctx_isp->rxd_epoch, 0);
  4984. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4985. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4986. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4987. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  4988. ctx->state, ctx->ctx_id);
  4989. if (!stop_cmd) {
  4990. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  4991. if (rc)
  4992. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  4993. }
  4994. return rc;
  4995. }
  4996. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  4997. struct cam_start_stop_dev_cmd *cmd)
  4998. {
  4999. int rc = 0;
  5000. struct cam_isp_context *ctx_isp =
  5001. (struct cam_isp_context *)ctx->ctx_priv;
  5002. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  5003. ctx_isp->init_received = false;
  5004. ctx->state = CAM_CTX_ACQUIRED;
  5005. trace_cam_context_state("ISP", ctx);
  5006. return rc;
  5007. }
  5008. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  5009. struct cam_release_dev_cmd *cmd)
  5010. {
  5011. int rc = 0;
  5012. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5013. if (rc)
  5014. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5015. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  5016. if (rc)
  5017. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  5018. return rc;
  5019. }
  5020. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  5021. void *cmd)
  5022. {
  5023. int rc = 0;
  5024. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5025. if (rc)
  5026. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5027. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  5028. if (rc)
  5029. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  5030. return rc;
  5031. }
  5032. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  5033. {
  5034. int rc = 0;
  5035. struct cam_hw_cmd_args hw_cmd_args;
  5036. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5037. struct cam_isp_context *ctx_isp =
  5038. (struct cam_isp_context *) ctx->ctx_priv;
  5039. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5040. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5041. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  5042. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5043. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5044. &hw_cmd_args);
  5045. return rc;
  5046. }
  5047. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  5048. {
  5049. int rc = 0;
  5050. struct cam_hw_cmd_args hw_cmd_args;
  5051. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5052. struct cam_isp_context *ctx_isp =
  5053. (struct cam_isp_context *) ctx->ctx_priv;
  5054. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5055. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5056. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5057. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5058. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5059. &hw_cmd_args);
  5060. return rc;
  5061. }
  5062. static int __cam_isp_ctx_handle_sof_freeze_evt(
  5063. struct cam_context *ctx)
  5064. {
  5065. int rc = 0;
  5066. struct cam_hw_cmd_args hw_cmd_args;
  5067. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5068. struct cam_isp_context *ctx_isp =
  5069. (struct cam_isp_context *) ctx->ctx_priv;
  5070. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5071. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5072. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  5073. isp_hw_cmd_args.u.sof_irq_enable = 1;
  5074. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5075. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5076. &hw_cmd_args);
  5077. return rc;
  5078. }
  5079. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  5080. struct cam_req_mgr_link_evt_data *link_evt_data)
  5081. {
  5082. int rc = 0;
  5083. switch (link_evt_data->evt_type) {
  5084. case CAM_REQ_MGR_LINK_EVT_ERR:
  5085. /* No need to handle this message now */
  5086. break;
  5087. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  5088. __cam_isp_ctx_link_pause(ctx);
  5089. break;
  5090. case CAM_REQ_MGR_LINK_EVT_RESUME:
  5091. __cam_isp_ctx_link_resume(ctx);
  5092. break;
  5093. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  5094. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  5095. break;
  5096. default:
  5097. CAM_WARN(CAM_ISP, "Unknown event from CRM");
  5098. break;
  5099. }
  5100. return rc;
  5101. }
  5102. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  5103. struct cam_req_mgr_core_dev_link_setup *unlink)
  5104. {
  5105. int rc = 0;
  5106. CAM_WARN(CAM_ISP,
  5107. "Received unlink in activated state. It's unexpected");
  5108. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5109. if (rc)
  5110. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  5111. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  5112. if (rc)
  5113. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5114. return rc;
  5115. }
  5116. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  5117. struct cam_req_mgr_apply_request *apply)
  5118. {
  5119. int rc = 0;
  5120. struct cam_ctx_ops *ctx_ops = NULL;
  5121. struct cam_isp_context *ctx_isp =
  5122. (struct cam_isp_context *) ctx->ctx_priv;
  5123. trace_cam_apply_req("ISP", apply->request_id);
  5124. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  5125. __cam_isp_ctx_substate_val_to_type(
  5126. ctx_isp->substate_activated), apply->request_id);
  5127. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5128. if (ctx_ops->crm_ops.apply_req) {
  5129. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  5130. } else {
  5131. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5132. "No handle function in activated Substate[%s]",
  5133. __cam_isp_ctx_substate_val_to_type(
  5134. ctx_isp->substate_activated));
  5135. rc = -EFAULT;
  5136. }
  5137. if (rc)
  5138. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5139. "Apply failed in active Substate[%s] rc %d",
  5140. __cam_isp_ctx_substate_val_to_type(
  5141. ctx_isp->substate_activated), rc);
  5142. return rc;
  5143. }
  5144. static int __cam_isp_ctx_apply_default_settings(
  5145. struct cam_context *ctx,
  5146. struct cam_req_mgr_apply_request *apply)
  5147. {
  5148. int rc = 0;
  5149. struct cam_ctx_ops *ctx_ops = NULL;
  5150. struct cam_isp_context *ctx_isp =
  5151. (struct cam_isp_context *) ctx->ctx_priv;
  5152. if (!ctx_isp->use_default_apply)
  5153. return 0;
  5154. CAM_DBG(CAM_ISP,
  5155. "Enter: apply req in Substate %d request _id:%lld",
  5156. ctx_isp->substate_activated, apply->request_id);
  5157. ctx_ops = &ctx_isp->substate_machine[
  5158. ctx_isp->substate_activated];
  5159. if (ctx_ops->crm_ops.notify_frame_skip) {
  5160. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  5161. } else {
  5162. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5163. "No handle function in activated substate %d",
  5164. ctx_isp->substate_activated);
  5165. rc = -EFAULT;
  5166. }
  5167. if (rc)
  5168. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5169. "Apply default failed in active substate %d rc %d",
  5170. ctx_isp->substate_activated, rc);
  5171. return rc;
  5172. }
  5173. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  5174. uint32_t evt_id, void *evt_data)
  5175. {
  5176. int rc = 0;
  5177. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  5178. struct cam_context *ctx = (struct cam_context *)context;
  5179. struct cam_isp_context *ctx_isp =
  5180. (struct cam_isp_context *)ctx->ctx_priv;
  5181. spin_lock(&ctx->lock);
  5182. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5183. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5184. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5185. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5186. ctx_isp->substate_activated), evt_id);
  5187. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5188. if (irq_ops->irq_ops[evt_id]) {
  5189. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5190. } else {
  5191. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5192. __cam_isp_ctx_substate_val_to_type(
  5193. ctx_isp->substate_activated));
  5194. if (isp_ctx_debug.enable_state_monitor_dump)
  5195. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5196. }
  5197. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5198. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5199. ctx_isp->substate_activated));
  5200. spin_unlock(&ctx->lock);
  5201. return rc;
  5202. }
  5203. /* top state machine */
  5204. static struct cam_ctx_ops
  5205. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5206. /* Uninit */
  5207. {
  5208. .ioctl_ops = {},
  5209. .crm_ops = {},
  5210. .irq_ops = NULL,
  5211. },
  5212. /* Available */
  5213. {
  5214. .ioctl_ops = {
  5215. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5216. },
  5217. .crm_ops = {},
  5218. .irq_ops = NULL,
  5219. },
  5220. /* Acquired */
  5221. {
  5222. .ioctl_ops = {
  5223. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5224. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5225. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5226. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5227. },
  5228. .crm_ops = {
  5229. .link = __cam_isp_ctx_link_in_acquired,
  5230. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5231. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5232. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5233. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5234. },
  5235. .irq_ops = NULL,
  5236. .pagefault_ops = cam_isp_context_dump_requests,
  5237. .dumpinfo_ops = cam_isp_context_info_dump,
  5238. },
  5239. /* Ready */
  5240. {
  5241. .ioctl_ops = {
  5242. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5243. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5244. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5245. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5246. },
  5247. .crm_ops = {
  5248. .unlink = __cam_isp_ctx_unlink_in_ready,
  5249. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5250. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5251. },
  5252. .irq_ops = NULL,
  5253. .pagefault_ops = cam_isp_context_dump_requests,
  5254. .dumpinfo_ops = cam_isp_context_info_dump,
  5255. },
  5256. /* Flushed */
  5257. {
  5258. .ioctl_ops = {
  5259. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5260. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5261. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  5262. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5263. },
  5264. .crm_ops = {
  5265. .unlink = __cam_isp_ctx_unlink_in_ready,
  5266. .process_evt = __cam_isp_ctx_process_evt,
  5267. },
  5268. .irq_ops = NULL,
  5269. .pagefault_ops = cam_isp_context_dump_requests,
  5270. .dumpinfo_ops = cam_isp_context_info_dump,
  5271. },
  5272. /* Activated */
  5273. {
  5274. .ioctl_ops = {
  5275. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5276. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5277. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5278. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5279. },
  5280. .crm_ops = {
  5281. .unlink = __cam_isp_ctx_unlink_in_activated,
  5282. .apply_req = __cam_isp_ctx_apply_req,
  5283. .notify_frame_skip =
  5284. __cam_isp_ctx_apply_default_settings,
  5285. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5286. .process_evt = __cam_isp_ctx_process_evt,
  5287. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5288. },
  5289. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  5290. .pagefault_ops = cam_isp_context_dump_requests,
  5291. .dumpinfo_ops = cam_isp_context_info_dump,
  5292. },
  5293. };
  5294. static int cam_isp_context_dump_requests(void *data,
  5295. struct cam_smmu_pf_info *pf_info)
  5296. {
  5297. struct cam_context *ctx = (struct cam_context *)data;
  5298. struct cam_ctx_request *req = NULL;
  5299. struct cam_ctx_request *req_temp = NULL;
  5300. struct cam_isp_ctx_req *req_isp = NULL;
  5301. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  5302. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  5303. struct cam_req_mgr_message req_msg;
  5304. struct cam_isp_context *ctx_isp;
  5305. uint32_t resource_type = 0;
  5306. bool mem_found = false, ctx_found = false, send_error = false;
  5307. int rc = 0;
  5308. struct cam_isp_context *isp_ctx =
  5309. (struct cam_isp_context *)ctx->ctx_priv;
  5310. if (!isp_ctx) {
  5311. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  5312. return -EINVAL;
  5313. }
  5314. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  5315. ctx->ctx_id, ctx->state);
  5316. list_for_each_entry_safe(req, req_temp,
  5317. &ctx->active_req_list, list) {
  5318. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5319. hw_update_data = &req_isp->hw_update_data;
  5320. pf_dbg_entry = &(req->pf_data);
  5321. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  5322. req->request_id);
  5323. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5324. &mem_found, &ctx_found, &resource_type, pf_info);
  5325. if (rc)
  5326. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5327. if (ctx_found)
  5328. send_error = true;
  5329. }
  5330. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  5331. ctx->ctx_id, ctx->state);
  5332. list_for_each_entry_safe(req, req_temp,
  5333. &ctx->wait_req_list, list) {
  5334. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5335. hw_update_data = &req_isp->hw_update_data;
  5336. pf_dbg_entry = &(req->pf_data);
  5337. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  5338. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5339. &mem_found, &ctx_found, &resource_type, pf_info);
  5340. if (rc)
  5341. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5342. if (ctx_found)
  5343. send_error = true;
  5344. }
  5345. /*
  5346. * In certain scenarios we observe both overflow and SMMU pagefault
  5347. * for a particular request. If overflow is handled before page fault
  5348. * we need to traverse through pending request list because if
  5349. * bubble recovery is enabled on any request we move that request
  5350. * and all the subsequent requests to the pending list while handling
  5351. * overflow error.
  5352. */
  5353. CAM_INFO(CAM_ISP,
  5354. "Iterating over pending req list of isp ctx %d state %d",
  5355. ctx->ctx_id, ctx->state);
  5356. list_for_each_entry_safe(req, req_temp,
  5357. &ctx->pending_req_list, list) {
  5358. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5359. hw_update_data = &req_isp->hw_update_data;
  5360. pf_dbg_entry = &(req->pf_data);
  5361. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  5362. req->request_id);
  5363. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  5364. &mem_found, &ctx_found, &resource_type, pf_info);
  5365. if (rc)
  5366. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  5367. if (ctx_found)
  5368. send_error = true;
  5369. }
  5370. if (resource_type) {
  5371. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5372. if (ctx_isp->isp_device_type == CAM_IFE_DEVICE_TYPE)
  5373. CAM_ERR(CAM_ISP,
  5374. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5375. __cam_isp_resource_handle_id_to_type(
  5376. resource_type),
  5377. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5378. ctx_isp->reported_req_id,
  5379. ctx_isp->last_applied_req_id);
  5380. else
  5381. CAM_ERR(CAM_ISP,
  5382. "Page fault on resource id:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  5383. __cam_isp_tfe_resource_handle_id_to_type(
  5384. resource_type),
  5385. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  5386. ctx_isp->reported_req_id,
  5387. ctx_isp->last_applied_req_id);
  5388. }
  5389. if (send_error) {
  5390. CAM_INFO(CAM_ISP,
  5391. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5392. ctx->ctx_id, ctx->session_hdl,
  5393. ctx->dev_hdl, ctx->link_hdl);
  5394. req_msg.session_hdl = ctx->session_hdl;
  5395. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  5396. req_msg.u.err_msg.error_type =
  5397. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  5398. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  5399. req_msg.u.err_msg.request_id = 0;
  5400. req_msg.u.err_msg.resource_size = 0x0;
  5401. if (cam_req_mgr_notify_message(&req_msg,
  5402. V4L_EVENT_CAM_REQ_MGR_ERROR,
  5403. V4L_EVENT_CAM_REQ_MGR_EVENT))
  5404. CAM_ERR(CAM_ISP,
  5405. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  5406. ctx->ctx_id, ctx->session_hdl,
  5407. ctx->dev_hdl, ctx->link_hdl);
  5408. }
  5409. return rc;
  5410. }
  5411. static int cam_isp_context_debug_register(void)
  5412. {
  5413. int rc = 0;
  5414. struct dentry *dbgfileptr = NULL;
  5415. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  5416. if (!dbgfileptr) {
  5417. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  5418. rc = -ENOENT;
  5419. goto end;
  5420. }
  5421. /* Store parent inode for cleanup in caller */
  5422. isp_ctx_debug.dentry = dbgfileptr;
  5423. debugfs_create_u32("enable_state_monitor_dump", 0644,
  5424. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  5425. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  5426. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  5427. if (IS_ERR(dbgfileptr)) {
  5428. if (PTR_ERR(dbgfileptr) == -ENODEV)
  5429. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  5430. else
  5431. rc = PTR_ERR(dbgfileptr);
  5432. }
  5433. end:
  5434. return rc;
  5435. }
  5436. int cam_isp_context_init(struct cam_isp_context *ctx,
  5437. struct cam_context *ctx_base,
  5438. struct cam_req_mgr_kmd_ops *crm_node_intf,
  5439. struct cam_hw_mgr_intf *hw_intf,
  5440. uint32_t ctx_id,
  5441. uint32_t isp_device_type)
  5442. {
  5443. int rc = -1;
  5444. int i;
  5445. if (!ctx || !ctx_base) {
  5446. CAM_ERR(CAM_ISP, "Invalid Context");
  5447. goto err;
  5448. }
  5449. /* ISP context setup */
  5450. memset(ctx, 0, sizeof(*ctx));
  5451. ctx->base = ctx_base;
  5452. ctx->frame_id = 0;
  5453. ctx->custom_enabled = false;
  5454. ctx->use_frame_header_ts = false;
  5455. ctx->use_default_apply = false;
  5456. ctx->active_req_cnt = 0;
  5457. ctx->reported_req_id = 0;
  5458. ctx->bubble_frame_cnt = 0;
  5459. ctx->req_info.last_bufdone_req_id = 0;
  5460. ctx->hw_ctx = NULL;
  5461. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5462. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  5463. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  5464. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  5465. ctx->isp_device_type = isp_device_type;
  5466. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5467. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  5468. ctx->req_isp[i].base = &ctx->req_base[i];
  5469. }
  5470. /* camera context setup */
  5471. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  5472. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX);
  5473. if (rc) {
  5474. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  5475. goto err;
  5476. }
  5477. /* link camera context with isp context */
  5478. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  5479. ctx_base->ctx_priv = ctx;
  5480. /* initializing current state for error logging */
  5481. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  5482. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  5483. CAM_ISP_CTX_ACTIVATED_MAX;
  5484. }
  5485. atomic64_set(&ctx->state_monitor_head, -1);
  5486. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5487. atomic64_set(&ctx->event_record_head[i], -1);
  5488. if (!isp_ctx_debug.dentry)
  5489. cam_isp_context_debug_register();
  5490. err:
  5491. return rc;
  5492. }
  5493. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  5494. {
  5495. if (ctx->base)
  5496. cam_context_deinit(ctx->base);
  5497. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  5498. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  5499. __cam_isp_ctx_substate_val_to_type(
  5500. ctx->substate_activated));
  5501. debugfs_remove_recursive(isp_ctx_debug.dentry);
  5502. isp_ctx_debug.dentry = NULL;
  5503. memset(ctx, 0, sizeof(*ctx));
  5504. return 0;
  5505. }