cam_isp_context.c 175 KB

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