cam_isp_context.c 290 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/videodev2.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/ratelimit.h>
  11. #include "cam_mem_mgr.h"
  12. #include "cam_sync_api.h"
  13. #include "cam_req_mgr_dev.h"
  14. #include "cam_trace.h"
  15. #include "cam_debug_util.h"
  16. #include "cam_packet_util.h"
  17. #include "cam_context_utils.h"
  18. #include "cam_cdm_util.h"
  19. #include "cam_isp_context.h"
  20. #include "cam_common_util.h"
  21. #include "cam_req_mgr_debug.h"
  22. #include "cam_cpas_api.h"
  23. #include "cam_ife_hw_mgr.h"
  24. #include "cam_subdev.h"
  25. static const char isp_dev_name[] = "cam-isp";
  26. static struct cam_isp_ctx_debug isp_ctx_debug;
  27. #define INC_HEAD(head, max_entries, ret) \
  28. div_u64_rem(atomic64_add_return(1, head),\
  29. max_entries, (ret))
  30. static int cam_isp_context_dump_requests(void *data,
  31. void *pf_args);
  32. static int cam_isp_context_hw_recovery(void *priv, void *data);
  33. static int cam_isp_context_handle_message(void *context,
  34. uint32_t msg_type, void *data);
  35. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  36. struct cam_start_stop_dev_cmd *cmd);
  37. static void __cam_isp_ctx_dump_state_monitor_array(
  38. struct cam_isp_context *ctx_isp);
  39. static const char *__cam_isp_hw_evt_val_to_type(
  40. uint32_t evt_id);
  41. static const char *__cam_isp_ctx_substate_val_to_type(
  42. enum cam_isp_ctx_activated_substate type);
  43. static int __cam_isp_ctx_check_deferred_buf_done(
  44. struct cam_isp_context *ctx_isp,
  45. struct cam_isp_hw_done_event_data *done,
  46. uint32_t bubble_state);
  47. static const char *__cam_isp_evt_val_to_type(
  48. uint32_t evt_id)
  49. {
  50. switch (evt_id) {
  51. case CAM_ISP_CTX_EVENT_SUBMIT:
  52. return "SUBMIT";
  53. case CAM_ISP_CTX_EVENT_APPLY:
  54. return "APPLY";
  55. case CAM_ISP_CTX_EVENT_EPOCH:
  56. return "EPOCH";
  57. case CAM_ISP_CTX_EVENT_RUP:
  58. return "RUP";
  59. case CAM_ISP_CTX_EVENT_BUFDONE:
  60. return "BUFDONE";
  61. case CAM_ISP_CTX_EVENT_SHUTTER:
  62. return "SHUTTER_NOTIFICATION";
  63. default:
  64. return "CAM_ISP_EVENT_INVALID";
  65. }
  66. }
  67. static void __cam_isp_ctx_update_event_record(
  68. struct cam_isp_context *ctx_isp,
  69. enum cam_isp_ctx_event event,
  70. struct cam_ctx_request *req,
  71. void *event_data)
  72. {
  73. int iterator = 0;
  74. ktime_t cur_time;
  75. struct cam_isp_ctx_req *req_isp;
  76. if (!ctx_isp) {
  77. CAM_ERR(CAM_ISP, "Invalid Args");
  78. return;
  79. }
  80. switch (event) {
  81. case CAM_ISP_CTX_EVENT_EPOCH:
  82. case CAM_ISP_CTX_EVENT_RUP:
  83. case CAM_ISP_CTX_EVENT_BUFDONE:
  84. case CAM_ISP_CTX_EVENT_SHUTTER:
  85. break;
  86. case CAM_ISP_CTX_EVENT_SUBMIT:
  87. case CAM_ISP_CTX_EVENT_APPLY:
  88. if (!req) {
  89. CAM_ERR(CAM_ISP, "Invalid arg for event %d", event);
  90. return;
  91. }
  92. break;
  93. default:
  94. break;
  95. }
  96. INC_HEAD(&ctx_isp->dbg_monitors.event_record_head[event],
  97. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES, &iterator);
  98. cur_time = ktime_get();
  99. if (req) {
  100. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  101. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  102. req->request_id;
  103. req_isp->event_timestamp[event] = cur_time;
  104. } else {
  105. ctx_isp->dbg_monitors.event_record[event][iterator].req_id = 0;
  106. }
  107. ctx_isp->dbg_monitors.event_record[event][iterator].timestamp = cur_time;
  108. if (event_data == NULL)
  109. return;
  110. /* Update event specific data */
  111. ctx_isp->dbg_monitors.event_record[event][iterator].event_type = event;
  112. if (event == CAM_ISP_CTX_EVENT_SHUTTER) {
  113. ctx_isp->dbg_monitors.event_record[event][iterator].req_id =
  114. ((struct shutter_event *)event_data)->req_id;
  115. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.req_id =
  116. ((struct shutter_event *)event_data)->req_id;
  117. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.status =
  118. ((struct shutter_event *)event_data)->status;
  119. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.frame_id =
  120. ((struct shutter_event *)event_data)->frame_id;
  121. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.boot_ts =
  122. ((struct shutter_event *)event_data)->boot_ts;
  123. ctx_isp->dbg_monitors.event_record[event][iterator].event.shutter_event.sof_ts =
  124. ((struct shutter_event *)event_data)->sof_ts;
  125. }
  126. }
  127. static int __cam_isp_ctx_handle_sof_freeze_evt(
  128. struct cam_context *ctx)
  129. {
  130. int rc = 0;
  131. struct cam_isp_context *ctx_isp;
  132. struct cam_hw_cmd_args hw_cmd_args;
  133. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  134. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  135. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  136. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  137. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  138. isp_hw_cmd_args.u.sof_irq_enable = 1;
  139. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  140. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  141. &hw_cmd_args);
  142. ctx_isp->sof_dbg_irq_en = true;
  143. return rc;
  144. }
  145. static void *cam_isp_ctx_user_dump_events(
  146. void *dump_struct, uint8_t *addr_ptr)
  147. {
  148. uint64_t *addr;
  149. struct cam_isp_context_event_record *record;
  150. struct timespec64 ts;
  151. record = (struct cam_isp_context_event_record *)dump_struct;
  152. addr = (uint64_t *)addr_ptr;
  153. ts = ktime_to_timespec64(record->timestamp);
  154. *addr++ = record->req_id;
  155. *addr++ = ts.tv_sec;
  156. *addr++ = ts.tv_nsec / NSEC_PER_USEC;
  157. return addr;
  158. }
  159. static int __cam_isp_ctx_print_event_record(struct cam_isp_context *ctx_isp)
  160. {
  161. int i, j, rc = 0;
  162. int index;
  163. uint32_t oldest_entry, num_entries;
  164. uint64_t state_head;
  165. struct cam_isp_context_event_record *record;
  166. uint32_t len;
  167. uint8_t buf[CAM_ISP_CONTEXT_DBG_BUF_LEN];
  168. struct timespec64 ts;
  169. struct cam_context *ctx = ctx_isp->base;
  170. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  171. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  172. if (state_head == -1) {
  173. return 0;
  174. } else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  175. num_entries = state_head + 1;
  176. oldest_entry = 0;
  177. } else {
  178. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  179. div_u64_rem(state_head + 1,
  180. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  181. &oldest_entry);
  182. }
  183. index = oldest_entry;
  184. len = 0;
  185. memset(buf, 0, CAM_ISP_CONTEXT_DBG_BUF_LEN);
  186. for (j = 0; j < num_entries; j++) {
  187. record = &ctx_isp->dbg_monitors.event_record[i][index];
  188. ts = ktime_to_timespec64(record->timestamp);
  189. if (len >= CAM_ISP_CONTEXT_DBG_BUF_LEN) {
  190. CAM_WARN(CAM_ISP, "Overshooting buffer length %llu", len);
  191. break;
  192. }
  193. if (record->event_type != CAM_ISP_CTX_EVENT_SHUTTER)
  194. len += scnprintf(buf + len, CAM_ISP_CONTEXT_DBG_BUF_LEN - len,
  195. "%llu[%lld:%06lld] ", record->req_id, ts.tv_sec,
  196. ts.tv_nsec / NSEC_PER_USEC);
  197. else
  198. /*
  199. * Output format
  200. * req Id[timestamp] status frmId softs bootts
  201. */
  202. len += scnprintf(buf + len, (CAM_ISP_CONTEXT_DBG_BUF_LEN) - len,
  203. "%llu[%lld:%06lld] [%d %lld %llu %llu] | ",
  204. record->req_id, ts.tv_sec,
  205. ts.tv_nsec / NSEC_PER_USEC,
  206. record->event.shutter_event.status,
  207. record->event.shutter_event.frame_id,
  208. record->event.shutter_event.sof_ts,
  209. record->event.shutter_event.boot_ts);
  210. index = (index + 1) %
  211. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  212. }
  213. if (len)
  214. CAM_INFO(CAM_ISP, "Ctx:%d %s: %s",
  215. ctx->ctx_id, __cam_isp_evt_val_to_type(i), buf);
  216. }
  217. return rc;
  218. }
  219. static int __cam_isp_ctx_dump_event_record(
  220. struct cam_isp_context *ctx_isp,
  221. struct cam_common_hw_dump_args *dump_args)
  222. {
  223. int i, j, rc = 0;
  224. int index;
  225. size_t remain_len;
  226. uint32_t oldest_entry, num_entries;
  227. uint32_t min_len;
  228. uint64_t state_head;
  229. struct cam_isp_context_event_record *record;
  230. if (!dump_args || !ctx_isp) {
  231. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  232. dump_args, ctx_isp);
  233. return -EINVAL;
  234. }
  235. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  236. state_head = atomic64_read(&ctx_isp->dbg_monitors.event_record_head[i]);
  237. if (state_head == -1)
  238. return 0;
  239. else if (state_head < CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES) {
  240. num_entries = state_head + 1;
  241. oldest_entry = 0;
  242. } else {
  243. num_entries = CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  244. div_u64_rem(state_head + 1, CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES,
  245. &oldest_entry);
  246. }
  247. index = oldest_entry;
  248. if (dump_args->buf_len <= dump_args->offset) {
  249. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  250. dump_args->buf_len, dump_args->offset);
  251. return -ENOSPC;
  252. }
  253. min_len = sizeof(struct cam_isp_context_dump_header) +
  254. ((num_entries * CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS) *
  255. sizeof(uint64_t));
  256. remain_len = dump_args->buf_len - dump_args->offset;
  257. if (remain_len < min_len) {
  258. CAM_WARN(CAM_ISP,
  259. "Dump buffer exhaust remain %zu min %u",
  260. remain_len, min_len);
  261. return -ENOSPC;
  262. }
  263. for (j = 0; j < num_entries; j++) {
  264. record = &ctx_isp->dbg_monitors.event_record[i][index];
  265. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_events,
  266. record, sizeof(uint64_t), "ISP_EVT_%s:",
  267. __cam_isp_evt_val_to_type(i));
  268. if (rc) {
  269. CAM_ERR(CAM_ISP,
  270. "CAM_ISP_CONTEXT DUMP_EVENT_RECORD: Dump failed, rc: %d",
  271. rc);
  272. return rc;
  273. }
  274. index = (index + 1) % CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES;
  275. }
  276. }
  277. return rc;
  278. }
  279. static void __cam_isp_ctx_req_mini_dump(struct cam_ctx_request *req,
  280. uint8_t *start_addr, uint8_t *end_addr,
  281. unsigned long *bytes_updated)
  282. {
  283. struct cam_isp_ctx_req_mini_dump *req_md;
  284. struct cam_buf_io_cfg *io_cfg;
  285. struct cam_isp_ctx_req *req_isp;
  286. struct cam_packet *packet = NULL;
  287. unsigned long bytes_required = 0;
  288. bytes_required = sizeof(*req_md);
  289. *bytes_updated = 0;
  290. if (start_addr + bytes_required > end_addr)
  291. return;
  292. req_md = (struct cam_isp_ctx_req_mini_dump *)start_addr;
  293. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  294. req_md->num_acked = req_isp->num_acked;
  295. req_md->num_deferred_acks = req_isp->num_deferred_acks;
  296. req_md->bubble_report = req_isp->bubble_report;
  297. req_md->bubble_detected = req_isp->bubble_detected;
  298. req_md->reapply_type = req_isp->reapply_type;
  299. req_md->request_id = req->request_id;
  300. *bytes_updated += bytes_required;
  301. if (req_isp->num_fence_map_out) {
  302. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  303. req_isp->num_fence_map_out;
  304. if (start_addr + *bytes_updated + bytes_required > end_addr)
  305. return;
  306. req_md->map_out = (struct cam_hw_fence_map_entry *)
  307. ((uint8_t *)start_addr + *bytes_updated);
  308. memcpy(req_md->map_out, req_isp->fence_map_out, bytes_required);
  309. req_md->num_fence_map_out = req_isp->num_fence_map_out;
  310. *bytes_updated += bytes_required;
  311. }
  312. if (req_isp->num_fence_map_in) {
  313. bytes_required = sizeof(struct cam_hw_fence_map_entry) *
  314. req_isp->num_fence_map_in;
  315. if (start_addr + *bytes_updated + bytes_required > end_addr)
  316. return;
  317. req_md->map_in = (struct cam_hw_fence_map_entry *)
  318. ((uint8_t *)start_addr + *bytes_updated);
  319. memcpy(req_md->map_in, req_isp->fence_map_in, bytes_required);
  320. req_md->num_fence_map_in = req_isp->num_fence_map_in;
  321. *bytes_updated += bytes_required;
  322. }
  323. packet = req_isp->hw_update_data.packet;
  324. if (packet && packet->num_io_configs) {
  325. bytes_required = packet->num_io_configs * sizeof(struct cam_buf_io_cfg);
  326. if (start_addr + *bytes_updated + bytes_required > end_addr)
  327. return;
  328. io_cfg = (struct cam_buf_io_cfg *)((uint32_t *)&packet->payload +
  329. packet->io_configs_offset / 4);
  330. req_md->io_cfg = (struct cam_buf_io_cfg *)((uint8_t *)start_addr + *bytes_updated);
  331. memcpy(req_md->io_cfg, io_cfg, bytes_required);
  332. *bytes_updated += bytes_required;
  333. req_md->num_io_cfg = packet->num_io_configs;
  334. }
  335. }
  336. static int __cam_isp_ctx_minidump_cb(void *priv, void *args)
  337. {
  338. struct cam_isp_ctx_mini_dump_info *md;
  339. struct cam_isp_context *ctx_isp;
  340. struct cam_context *ctx;
  341. struct cam_ctx_request *req, *req_temp;
  342. struct cam_hw_mini_dump_args *dump_args;
  343. uint8_t *start_addr;
  344. uint8_t *end_addr;
  345. unsigned long total_bytes = 0;
  346. unsigned long bytes_updated = 0;
  347. uint32_t i;
  348. if (!priv || !args) {
  349. CAM_ERR(CAM_ISP, "invalid params");
  350. return 0;
  351. }
  352. dump_args = (struct cam_hw_mini_dump_args *)args;
  353. if (dump_args->len < sizeof(*md)) {
  354. CAM_ERR(CAM_ISP,
  355. "In sufficient size received %lu required size: %zu",
  356. dump_args->len, sizeof(*md));
  357. return 0;
  358. }
  359. ctx = (struct cam_context *)priv;
  360. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  361. start_addr = (uint8_t *)dump_args->start_addr;
  362. end_addr = start_addr + dump_args->len;
  363. md = (struct cam_isp_ctx_mini_dump_info *)dump_args->start_addr;
  364. md->sof_timestamp_val = ctx_isp->sof_timestamp_val;
  365. md->boot_timestamp = ctx_isp->boot_timestamp;
  366. md->last_sof_timestamp = ctx_isp->last_sof_timestamp;
  367. md->init_timestamp = ctx_isp->init_timestamp;
  368. md->frame_id = ctx_isp->frame_id;
  369. md->reported_req_id = ctx_isp->reported_req_id;
  370. md->last_applied_req_id = ctx_isp->last_applied_req_id;
  371. md->last_bufdone_err_apply_req_id =
  372. ctx_isp->last_bufdone_err_apply_req_id;
  373. md->frame_id_meta = ctx_isp->frame_id_meta;
  374. md->substate_activated = ctx_isp->substate_activated;
  375. md->ctx_id = ctx->ctx_id;
  376. md->subscribe_event = ctx_isp->subscribe_event;
  377. md->bubble_frame_cnt = ctx_isp->bubble_frame_cnt;
  378. md->isp_device_type = ctx_isp->isp_device_type;
  379. md->active_req_cnt = ctx_isp->active_req_cnt;
  380. md->trigger_id = ctx_isp->trigger_id;
  381. md->rdi_only_context = ctx_isp->rdi_only_context;
  382. md->offline_context = ctx_isp->offline_context;
  383. md->hw_acquired = ctx_isp->hw_acquired;
  384. md->init_received = ctx_isp->init_received;
  385. md->split_acquire = ctx_isp->split_acquire;
  386. md->use_frame_header_ts = ctx_isp->use_frame_header_ts;
  387. md->support_consumed_addr = ctx_isp->support_consumed_addr;
  388. md->use_default_apply = ctx_isp->use_default_apply;
  389. md->apply_in_progress = atomic_read(&ctx_isp->apply_in_progress);
  390. md->process_bubble = atomic_read(&ctx_isp->process_bubble);
  391. md->rxd_epoch = atomic_read(&ctx_isp->rxd_epoch);
  392. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++) {
  393. memcpy(md->event_record[i], ctx_isp->dbg_monitors.event_record[i],
  394. sizeof(struct cam_isp_context_event_record) *
  395. CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES);
  396. }
  397. total_bytes += sizeof(*md);
  398. if (start_addr + total_bytes >= end_addr)
  399. goto end;
  400. if (!list_empty(&ctx->active_req_list)) {
  401. md->active_list = (struct cam_isp_ctx_req_mini_dump *)
  402. (start_addr + total_bytes);
  403. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  404. bytes_updated = 0;
  405. __cam_isp_ctx_req_mini_dump(req,
  406. (uint8_t *)&md->active_list[md->active_cnt++],
  407. end_addr, &bytes_updated);
  408. total_bytes += bytes_updated;
  409. if ((start_addr + total_bytes >= end_addr))
  410. goto end;
  411. }
  412. }
  413. if (!list_empty(&ctx->wait_req_list)) {
  414. md->wait_list = (struct cam_isp_ctx_req_mini_dump *)
  415. (start_addr + total_bytes);
  416. list_for_each_entry_safe(req, req_temp, &ctx->wait_req_list, list) {
  417. bytes_updated = 0;
  418. __cam_isp_ctx_req_mini_dump(req,
  419. (uint8_t *)&md->wait_list[md->wait_cnt++],
  420. end_addr, &bytes_updated);
  421. total_bytes += bytes_updated;
  422. if ((start_addr + total_bytes >= end_addr))
  423. goto end;
  424. }
  425. }
  426. if (!list_empty(&ctx->pending_req_list)) {
  427. md->pending_list = (struct cam_isp_ctx_req_mini_dump *)
  428. (start_addr + total_bytes);
  429. list_for_each_entry_safe(req, req_temp, &ctx->pending_req_list, list) {
  430. bytes_updated = 0;
  431. __cam_isp_ctx_req_mini_dump(req,
  432. (uint8_t *)&md->pending_list[md->pending_cnt++],
  433. end_addr, &bytes_updated);
  434. total_bytes += bytes_updated;
  435. if ((start_addr + total_bytes >= end_addr))
  436. goto end;
  437. }
  438. }
  439. end:
  440. dump_args->bytes_written = total_bytes;
  441. return 0;
  442. }
  443. static void __cam_isp_ctx_update_state_monitor_array(
  444. struct cam_isp_context *ctx_isp,
  445. enum cam_isp_state_change_trigger trigger_type,
  446. uint64_t req_id)
  447. {
  448. int iterator;
  449. struct cam_context *ctx = ctx_isp->base;
  450. INC_HEAD(&ctx_isp->dbg_monitors.state_monitor_head,
  451. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &iterator);
  452. ctx_isp->dbg_monitors.state_monitor[iterator].curr_state =
  453. ctx_isp->substate_activated;
  454. ctx_isp->dbg_monitors.state_monitor[iterator].frame_id =
  455. ctx_isp->frame_id;
  456. ctx_isp->dbg_monitors.state_monitor[iterator].trigger =
  457. trigger_type;
  458. ctx_isp->dbg_monitors.state_monitor[iterator].req_id =
  459. req_id;
  460. if (trigger_type == CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE)
  461. ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp =
  462. ctx->cdm_done_ts;
  463. else
  464. ktime_get_clocktai_ts64(
  465. &ctx_isp->dbg_monitors.state_monitor[iterator].evt_time_stamp);
  466. }
  467. static int __cam_isp_ctx_update_frame_timing_record(
  468. enum cam_isp_hw_event_type hw_evt, struct cam_isp_context *ctx_isp)
  469. {
  470. uint32_t index = 0;
  471. /* Update event index on IFE SOF - primary event */
  472. if (hw_evt == CAM_ISP_HW_EVENT_SOF)
  473. INC_HEAD(&ctx_isp->dbg_monitors.frame_monitor_head,
  474. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  475. else
  476. div_u64_rem(atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head),
  477. CAM_ISP_CTX_MAX_FRAME_RECORDS, &index);
  478. switch (hw_evt) {
  479. case CAM_ISP_HW_EVENT_SOF:
  480. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].sof_ts);
  481. break;
  482. case CAM_ISP_HW_EVENT_EOF:
  483. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].eof_ts);
  484. break;
  485. case CAM_ISP_HW_EVENT_EPOCH:
  486. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts);
  487. break;
  488. case CAM_ISP_HW_SECONDARY_EVENT:
  489. CAM_GET_BOOT_TIMESTAMP(ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts);
  490. break;
  491. default:
  492. break;
  493. }
  494. return 0;
  495. }
  496. static void __cam_isp_ctx_dump_frame_timing_record(
  497. struct cam_isp_context *ctx_isp)
  498. {
  499. int i = 0;
  500. int64_t state_head = 0;
  501. uint32_t index, num_entries, oldest_entry;
  502. state_head = atomic64_read(&ctx_isp->dbg_monitors.frame_monitor_head);
  503. if (state_head == -1)
  504. return;
  505. if (state_head < CAM_ISP_CTX_MAX_FRAME_RECORDS) {
  506. num_entries = state_head + 1;
  507. oldest_entry = 0;
  508. } else {
  509. num_entries = CAM_ISP_CTX_MAX_FRAME_RECORDS;
  510. div_u64_rem(state_head + 1, CAM_ISP_CTX_MAX_FRAME_RECORDS,
  511. &oldest_entry);
  512. }
  513. index = oldest_entry;
  514. for (i = 0; i < num_entries; i++) {
  515. CAM_INFO(CAM_ISP,
  516. "Index: %u SOF_TS: %llu.%llu EPOCH_TS: %llu.%llu EOF_TS: %llu.%llu SEC_SOF: %llu.%llu",
  517. index,
  518. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_sec,
  519. ctx_isp->dbg_monitors.frame_monitor[index].sof_ts.tv_nsec / NSEC_PER_USEC,
  520. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_sec,
  521. ctx_isp->dbg_monitors.frame_monitor[index].epoch_ts.tv_nsec / NSEC_PER_USEC,
  522. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_sec,
  523. ctx_isp->dbg_monitors.frame_monitor[index].eof_ts.tv_nsec / NSEC_PER_USEC,
  524. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_sec,
  525. ctx_isp->dbg_monitors.frame_monitor[index].secondary_sof_ts.tv_nsec /
  526. NSEC_PER_USEC);
  527. index = (index + 1) % CAM_ISP_CTX_MAX_FRAME_RECORDS;
  528. }
  529. }
  530. static const char *__cam_isp_ctx_substate_val_to_type(
  531. enum cam_isp_ctx_activated_substate type)
  532. {
  533. switch (type) {
  534. case CAM_ISP_CTX_ACTIVATED_SOF:
  535. return "SOF";
  536. case CAM_ISP_CTX_ACTIVATED_APPLIED:
  537. return "APPLIED";
  538. case CAM_ISP_CTX_ACTIVATED_EPOCH:
  539. return "EPOCH";
  540. case CAM_ISP_CTX_ACTIVATED_BUBBLE:
  541. return "BUBBLE";
  542. case CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED:
  543. return "BUBBLE_APPLIED";
  544. case CAM_ISP_CTX_ACTIVATED_HW_ERROR:
  545. return "HW_ERROR";
  546. case CAM_ISP_CTX_ACTIVATED_HALT:
  547. return "HALT";
  548. default:
  549. return "INVALID";
  550. }
  551. }
  552. static const char *__cam_isp_hw_evt_val_to_type(
  553. uint32_t evt_id)
  554. {
  555. switch (evt_id) {
  556. case CAM_ISP_STATE_CHANGE_TRIGGER_ERROR:
  557. return "ERROR";
  558. case CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED:
  559. return "APPLIED";
  560. case CAM_ISP_STATE_CHANGE_TRIGGER_SOF:
  561. return "SOF";
  562. case CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE:
  563. return "REG_UPDATE";
  564. case CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH:
  565. return "EPOCH";
  566. case CAM_ISP_STATE_CHANGE_TRIGGER_EOF:
  567. return "EOF";
  568. case CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE:
  569. return "CDM_DONE";
  570. case CAM_ISP_STATE_CHANGE_TRIGGER_DONE:
  571. return "DONE";
  572. case CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH:
  573. return "FLUSH";
  574. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF:
  575. return "SEC_EVT_SOF";
  576. case CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH:
  577. return "SEC_EVT_EPOCH";
  578. case CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP:
  579. return "OUT_OF_SYNC_FRAME_DROP";
  580. default:
  581. return "CAM_ISP_EVENT_INVALID";
  582. }
  583. }
  584. static void __cam_isp_ctx_dump_state_monitor_array(
  585. struct cam_isp_context *ctx_isp)
  586. {
  587. int i = 0;
  588. int64_t state_head = 0;
  589. uint32_t index, num_entries, oldest_entry;
  590. struct tm ts;
  591. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  592. if (state_head == -1)
  593. return;
  594. if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  595. num_entries = state_head + 1;
  596. oldest_entry = 0;
  597. } else {
  598. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  599. div_u64_rem(state_head + 1, CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES,
  600. &oldest_entry);
  601. }
  602. CAM_ERR(CAM_ISP,
  603. "Dumping state information for preceding requests");
  604. index = oldest_entry;
  605. for (i = 0; i < num_entries; i++) {
  606. time64_to_tm(ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_sec,
  607. 0, &ts);
  608. CAM_ERR(CAM_ISP,
  609. "Idx[%d] time[%d-%d %d:%d:%d.%lld]:Substate[%s] Frame[%lld] Req[%llu] evt[%s]",
  610. index, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  611. ctx_isp->dbg_monitors.state_monitor[index].evt_time_stamp.tv_nsec / 1000000,
  612. __cam_isp_ctx_substate_val_to_type(
  613. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  614. ctx_isp->dbg_monitors.state_monitor[index].frame_id,
  615. ctx_isp->dbg_monitors.state_monitor[index].req_id,
  616. __cam_isp_hw_evt_val_to_type(
  617. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  618. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  619. }
  620. }
  621. static void *cam_isp_ctx_user_dump_state_monitor_array_info(
  622. void *dump_struct, uint8_t *addr_ptr)
  623. {
  624. struct cam_isp_context_state_monitor *evt = NULL;
  625. uint64_t *addr;
  626. evt = (struct cam_isp_context_state_monitor *)dump_struct;
  627. addr = (uint64_t *)addr_ptr;
  628. *addr++ = evt->evt_time_stamp.tv_sec;
  629. *addr++ = evt->evt_time_stamp.tv_nsec / NSEC_PER_USEC;
  630. *addr++ = evt->frame_id;
  631. *addr++ = evt->req_id;
  632. return addr;
  633. }
  634. static int __cam_isp_ctx_user_dump_state_monitor_array(
  635. struct cam_isp_context *ctx_isp,
  636. struct cam_common_hw_dump_args *dump_args)
  637. {
  638. int i, rc = 0;
  639. int index;
  640. uint32_t oldest_entry;
  641. uint32_t num_entries;
  642. uint64_t state_head = 0;
  643. if (!dump_args || !ctx_isp) {
  644. CAM_ERR(CAM_ISP, "Invalid args %pK %pK",
  645. dump_args, ctx_isp);
  646. return -EINVAL;
  647. }
  648. state_head = atomic64_read(&ctx_isp->dbg_monitors.state_monitor_head);
  649. if (state_head == -1) {
  650. return 0;
  651. } else if (state_head < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES) {
  652. num_entries = state_head;
  653. oldest_entry = 0;
  654. } else {
  655. num_entries = CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  656. div_u64_rem(state_head + 1,
  657. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES, &oldest_entry);
  658. }
  659. CAM_ERR(CAM_ISP,
  660. "Dumping state information for preceding requests");
  661. index = oldest_entry;
  662. for (i = 0; i < num_entries; i++) {
  663. rc = cam_common_user_dump_helper(dump_args,
  664. cam_isp_ctx_user_dump_state_monitor_array_info,
  665. &ctx_isp->dbg_monitors.state_monitor[index],
  666. sizeof(uint64_t), "ISP_STATE_MONITOR.%s.%s:",
  667. __cam_isp_ctx_substate_val_to_type(
  668. ctx_isp->dbg_monitors.state_monitor[index].curr_state),
  669. __cam_isp_hw_evt_val_to_type(
  670. ctx_isp->dbg_monitors.state_monitor[index].trigger));
  671. if (rc) {
  672. CAM_ERR(CAM_ISP, "CAM ISP CONTEXT: Event record dump failed, rc: %d", rc);
  673. return rc;
  674. }
  675. index = (index + 1) % CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES;
  676. }
  677. return rc;
  678. }
  679. static int cam_isp_context_info_dump(void *context,
  680. enum cam_context_dump_id id)
  681. {
  682. struct cam_context *ctx = (struct cam_context *)context;
  683. switch (id) {
  684. case CAM_CTX_DUMP_ACQ_INFO: {
  685. cam_context_dump_hw_acq_info(ctx);
  686. break;
  687. }
  688. default:
  689. CAM_DBG(CAM_ISP, "DUMP id not valid %u, ctx_idx: %u, link: 0x%x",
  690. id, ctx->ctx_id, ctx->link_hdl);
  691. break;
  692. }
  693. return 0;
  694. }
  695. static const char *__cam_isp_ctx_crm_trigger_point_to_string(
  696. int trigger_point)
  697. {
  698. switch (trigger_point) {
  699. case CAM_TRIGGER_POINT_SOF:
  700. return "SOF";
  701. case CAM_TRIGGER_POINT_EOF:
  702. return "EOF";
  703. default:
  704. return "Invalid";
  705. }
  706. }
  707. static int __cam_isp_ctx_notify_trigger_util(
  708. int trigger_type, struct cam_isp_context *ctx_isp)
  709. {
  710. int rc = -EINVAL;
  711. struct cam_context *ctx = ctx_isp->base;
  712. struct cam_req_mgr_trigger_notify notify;
  713. /* Trigger type not supported, return */
  714. if (!(ctx_isp->subscribe_event & trigger_type)) {
  715. CAM_DBG(CAM_ISP,
  716. "%s trigger point not subscribed for in mask: %u in ctx: %u on link: 0x%x last_bufdone: %lld",
  717. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  718. ctx_isp->subscribe_event, ctx->ctx_id, ctx->link_hdl,
  719. ctx_isp->req_info.last_bufdone_req_id);
  720. return 0;
  721. }
  722. /* Skip CRM notify when recovery is in progress */
  723. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  724. CAM_DBG(CAM_ISP,
  725. "Internal recovery in progress skip notifying %s trigger point in ctx: %u on link: 0x%x",
  726. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  727. ctx->ctx_id, ctx->link_hdl);
  728. return 0;
  729. }
  730. notify.link_hdl = ctx->link_hdl;
  731. notify.dev_hdl = ctx->dev_hdl;
  732. notify.frame_id = ctx_isp->frame_id;
  733. notify.trigger = trigger_type;
  734. notify.req_id = ctx_isp->req_info.last_bufdone_req_id;
  735. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  736. notify.trigger_id = ctx_isp->trigger_id;
  737. CAM_DBG(CAM_ISP,
  738. "Notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld",
  739. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  740. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  741. ctx_isp->req_info.last_bufdone_req_id);
  742. rc = ctx->ctx_crm_intf->notify_trigger(&notify);
  743. if (rc)
  744. CAM_ERR_RATE_LIMIT(CAM_ISP,
  745. "Failed to notify CRM %s on frame: %llu ctx: %u link: 0x%x last_buf_done_req: %lld rc: %d",
  746. __cam_isp_ctx_crm_trigger_point_to_string(trigger_type),
  747. ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl,
  748. ctx_isp->req_info.last_bufdone_req_id, rc);
  749. return rc;
  750. }
  751. static int __cam_isp_ctx_notify_v4l2_error_event(
  752. uint32_t error_type, uint32_t error_code,
  753. uint64_t error_request_id, struct cam_context *ctx)
  754. {
  755. int rc = 0;
  756. struct cam_req_mgr_message req_msg;
  757. req_msg.session_hdl = ctx->session_hdl;
  758. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  759. req_msg.u.err_msg.error_type = error_type;
  760. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  761. req_msg.u.err_msg.request_id = error_request_id;
  762. req_msg.u.err_msg.resource_size = 0x0;
  763. req_msg.u.err_msg.error_code = error_code;
  764. CAM_DBG(CAM_ISP,
  765. "v4l2 error event [type: %u code: %u] for req: %llu in ctx: %u on link: 0x%x notified successfully",
  766. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  767. rc = cam_req_mgr_notify_message(&req_msg,
  768. V4L_EVENT_CAM_REQ_MGR_ERROR,
  769. V4L_EVENT_CAM_REQ_MGR_EVENT);
  770. if (rc)
  771. CAM_ERR(CAM_ISP,
  772. "Notifying v4l2 error [type: %u code: %u] failed for req id:%llu in ctx %u on link: 0x%x",
  773. error_type, error_code, error_request_id, ctx->ctx_id, ctx->link_hdl);
  774. return rc;
  775. }
  776. static int __cam_isp_ctx_notify_error_util(
  777. uint32_t trigger_type, enum cam_req_mgr_device_error error,
  778. uint64_t req_id, struct cam_isp_context *ctx_isp)
  779. {
  780. int rc = -EINVAL;
  781. struct cam_context *ctx = ctx_isp->base;
  782. struct cam_req_mgr_error_notify notify;
  783. notify.link_hdl = ctx->link_hdl;
  784. notify.dev_hdl = ctx->dev_hdl;
  785. notify.req_id = req_id;
  786. notify.error = error;
  787. notify.trigger = trigger_type;
  788. notify.frame_id = ctx_isp->frame_id;
  789. notify.sof_timestamp_val = ctx_isp->sof_timestamp_val;
  790. if (error == CRM_KMD_ERR_BUBBLE)
  791. CAM_WARN(CAM_ISP,
  792. "Notify CRM about bubble req: %llu frame: %llu in ctx: %u on link: 0x%x",
  793. req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  794. else
  795. CAM_ERR(CAM_ISP,
  796. "Notify CRM about fatal error: %u req: %llu frame: %llu in ctx: %u on link: 0x%x",
  797. error, req_id, ctx_isp->frame_id, ctx->ctx_id, ctx->link_hdl);
  798. rc = ctx->ctx_crm_intf->notify_err(&notify);
  799. if (rc)
  800. CAM_ERR(CAM_ISP,
  801. "Failed to notify error: %u for req: %lu on ctx: %u in link: 0x%x",
  802. error, req_id, ctx->ctx_id, ctx->link_hdl);
  803. return rc;
  804. }
  805. static int __cam_isp_ctx_trigger_reg_dump(
  806. enum cam_hw_mgr_command cmd,
  807. struct cam_context *ctx)
  808. {
  809. int rc = 0;
  810. struct cam_hw_cmd_args hw_cmd_args;
  811. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  812. hw_cmd_args.cmd_type = cmd;
  813. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  814. &hw_cmd_args);
  815. if (rc) {
  816. CAM_ERR(CAM_ISP, "Reg dump on error failed ctx: %u link: 0x%x rc: %d",
  817. ctx->ctx_id, ctx->link_hdl, rc);
  818. goto end;
  819. }
  820. CAM_DBG(CAM_ISP,
  821. "Reg dump type: %u successful in ctx: %u on link: 0x%x",
  822. cmd, ctx->ctx_id, ctx->link_hdl);
  823. end:
  824. return rc;
  825. }
  826. static int __cam_isp_ctx_pause_crm_timer(
  827. struct cam_context *ctx)
  828. {
  829. int rc = -EINVAL;
  830. struct cam_req_mgr_timer_notify timer;
  831. if (!ctx || !ctx->ctx_crm_intf)
  832. goto end;
  833. timer.link_hdl = ctx->link_hdl;
  834. timer.dev_hdl = ctx->dev_hdl;
  835. timer.state = false;
  836. rc = ctx->ctx_crm_intf->notify_timer(&timer);
  837. if (rc) {
  838. CAM_ERR(CAM_ISP, "Failed to pause sof timer in ctx: %u on link: 0x%x",
  839. ctx->ctx_id, ctx->link_hdl);
  840. goto end;
  841. }
  842. CAM_DBG(CAM_ISP, "Notify CRM to pause timer for ctx: %u link: 0x%x success",
  843. ctx->ctx_id, ctx->link_hdl);
  844. end:
  845. return rc;
  846. }
  847. static inline void __cam_isp_ctx_update_sof_ts_util(
  848. struct cam_isp_hw_sof_event_data *sof_event_data,
  849. struct cam_isp_context *ctx_isp)
  850. {
  851. /* Delayed update, skip if ts is already updated */
  852. if (ctx_isp->sof_timestamp_val == sof_event_data->timestamp)
  853. return;
  854. ctx_isp->frame_id++;
  855. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  856. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  857. }
  858. static int cam_isp_ctx_dump_req(
  859. struct cam_isp_ctx_req *req_isp,
  860. uintptr_t cpu_addr,
  861. size_t buf_len,
  862. size_t *offset,
  863. bool dump_to_buff)
  864. {
  865. int i, rc = 0;
  866. size_t len = 0;
  867. uint32_t *buf_addr;
  868. uint32_t *buf_start, *buf_end;
  869. size_t remain_len = 0;
  870. struct cam_cdm_cmd_buf_dump_info dump_info;
  871. for (i = 0; i < req_isp->num_cfg; i++) {
  872. rc = cam_packet_util_get_cmd_mem_addr(
  873. req_isp->cfg[i].handle, &buf_addr, &len);
  874. if (rc) {
  875. CAM_ERR_RATE_LIMIT(CAM_ISP,
  876. "Failed to get_cmd_mem_addr, rc=%d",
  877. rc);
  878. } else {
  879. if (req_isp->cfg[i].offset >= ((uint32_t)len)) {
  880. CAM_ERR(CAM_ISP,
  881. "Invalid offset exp %u actual %u",
  882. req_isp->cfg[i].offset, (uint32_t)len);
  883. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  884. return -EINVAL;
  885. }
  886. remain_len = len - req_isp->cfg[i].offset;
  887. if (req_isp->cfg[i].len >
  888. ((uint32_t)remain_len)) {
  889. CAM_ERR(CAM_ISP,
  890. "Invalid len exp %u remain_len %u",
  891. req_isp->cfg[i].len,
  892. (uint32_t)remain_len);
  893. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  894. return -EINVAL;
  895. }
  896. buf_start = (uint32_t *)((uint8_t *) buf_addr +
  897. req_isp->cfg[i].offset);
  898. buf_end = (uint32_t *)((uint8_t *) buf_start +
  899. req_isp->cfg[i].len - 1);
  900. if (dump_to_buff) {
  901. if (!cpu_addr || !offset || !buf_len) {
  902. CAM_ERR(CAM_ISP, "Invalid args");
  903. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  904. break;
  905. }
  906. dump_info.src_start = buf_start;
  907. dump_info.src_end = buf_end;
  908. dump_info.dst_start = cpu_addr;
  909. dump_info.dst_offset = *offset;
  910. dump_info.dst_max_size = buf_len;
  911. rc = cam_cdm_util_dump_cmd_bufs_v2(
  912. &dump_info);
  913. *offset = dump_info.dst_offset;
  914. if (rc) {
  915. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  916. return rc;
  917. }
  918. } else
  919. cam_cdm_util_dump_cmd_buf(buf_start, buf_end);
  920. cam_mem_put_cpu_buf(req_isp->cfg[i].handle);
  921. }
  922. }
  923. return rc;
  924. }
  925. static int __cam_isp_ctx_enqueue_request_in_order(
  926. struct cam_context *ctx, struct cam_ctx_request *req, bool lock)
  927. {
  928. struct cam_ctx_request *req_current;
  929. struct cam_ctx_request *req_prev;
  930. struct list_head temp_list;
  931. struct cam_isp_context *ctx_isp;
  932. INIT_LIST_HEAD(&temp_list);
  933. if (lock)
  934. spin_lock_bh(&ctx->lock);
  935. if (list_empty(&ctx->pending_req_list)) {
  936. list_add_tail(&req->list, &ctx->pending_req_list);
  937. } else {
  938. list_for_each_entry_safe_reverse(
  939. req_current, req_prev, &ctx->pending_req_list, list) {
  940. if (req->request_id < req_current->request_id) {
  941. list_del_init(&req_current->list);
  942. list_add(&req_current->list, &temp_list);
  943. continue;
  944. } else if (req->request_id == req_current->request_id) {
  945. CAM_WARN(CAM_ISP,
  946. "Received duplicated request %lld, ctx_idx: %u link: 0x%x",
  947. req->request_id, ctx->ctx_id, ctx->link_hdl);
  948. }
  949. break;
  950. }
  951. list_add_tail(&req->list, &ctx->pending_req_list);
  952. if (!list_empty(&temp_list)) {
  953. list_for_each_entry_safe(
  954. req_current, req_prev, &temp_list, list) {
  955. list_del_init(&req_current->list);
  956. list_add_tail(&req_current->list,
  957. &ctx->pending_req_list);
  958. }
  959. }
  960. }
  961. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  962. __cam_isp_ctx_update_event_record(ctx_isp,
  963. CAM_ISP_CTX_EVENT_SUBMIT, req, NULL);
  964. if (lock)
  965. spin_unlock_bh(&ctx->lock);
  966. return 0;
  967. }
  968. static int __cam_isp_ctx_enqueue_init_request(
  969. struct cam_context *ctx, struct cam_ctx_request *req)
  970. {
  971. int rc = 0;
  972. struct cam_ctx_request *req_old;
  973. struct cam_isp_ctx_req *req_isp_old;
  974. struct cam_isp_ctx_req *req_isp_new;
  975. struct cam_isp_prepare_hw_update_data *req_update_old;
  976. struct cam_isp_prepare_hw_update_data *req_update_new;
  977. struct cam_isp_prepare_hw_update_data *hw_update_data;
  978. struct cam_isp_fcg_config_info *fcg_info_old;
  979. struct cam_isp_fcg_config_info *fcg_info_new;
  980. spin_lock_bh(&ctx->lock);
  981. if (list_empty(&ctx->pending_req_list)) {
  982. list_add_tail(&req->list, &ctx->pending_req_list);
  983. CAM_DBG(CAM_ISP, "INIT packet added req id= %d, ctx_idx: %u, link: 0x%x",
  984. req->request_id, ctx->ctx_id, ctx->link_hdl);
  985. goto end;
  986. }
  987. req_old = list_first_entry(&ctx->pending_req_list,
  988. struct cam_ctx_request, list);
  989. req_isp_old = (struct cam_isp_ctx_req *) req_old->req_priv;
  990. req_isp_new = (struct cam_isp_ctx_req *) req->req_priv;
  991. if (req_isp_old->hw_update_data.packet_opcode_type ==
  992. CAM_ISP_PACKET_INIT_DEV) {
  993. if ((req_isp_old->num_cfg + req_isp_new->num_cfg) >=
  994. ctx->max_hw_update_entries) {
  995. CAM_WARN(CAM_ISP,
  996. "Can not merge INIT pkt num_cfgs = %d, ctx_idx: %u, link: 0x%x",
  997. (req_isp_old->num_cfg +
  998. req_isp_new->num_cfg), ctx->ctx_id, ctx->link_hdl);
  999. rc = -ENOMEM;
  1000. }
  1001. if (req_isp_old->num_fence_map_out != 0 ||
  1002. req_isp_old->num_fence_map_in != 0) {
  1003. CAM_WARN(CAM_ISP, "Invalid INIT pkt sequence, ctx_idx: %u, link: 0x%x",
  1004. ctx->ctx_id, ctx->link_hdl);
  1005. rc = -EINVAL;
  1006. }
  1007. if (!rc) {
  1008. memcpy(req_isp_old->fence_map_out,
  1009. req_isp_new->fence_map_out,
  1010. sizeof(req_isp_new->fence_map_out[0])*
  1011. req_isp_new->num_fence_map_out);
  1012. req_isp_old->num_fence_map_out =
  1013. req_isp_new->num_fence_map_out;
  1014. memcpy(req_isp_old->fence_map_in,
  1015. req_isp_new->fence_map_in,
  1016. sizeof(req_isp_new->fence_map_in[0])*
  1017. req_isp_new->num_fence_map_in);
  1018. req_isp_old->num_fence_map_in =
  1019. req_isp_new->num_fence_map_in;
  1020. /* Copy hw update entries, num_cfg is updated later */
  1021. memcpy(&req_isp_old->cfg[req_isp_old->num_cfg],
  1022. req_isp_new->cfg,
  1023. sizeof(req_isp_new->cfg[0]) *
  1024. req_isp_new->num_cfg);
  1025. memcpy(&req_old->pf_data, &req->pf_data,
  1026. sizeof(struct cam_hw_mgr_pf_request_info));
  1027. if (req_isp_new->hw_update_data.num_reg_dump_buf) {
  1028. req_update_new = &req_isp_new->hw_update_data;
  1029. req_update_old = &req_isp_old->hw_update_data;
  1030. memcpy(&req_update_old->reg_dump_buf_desc,
  1031. &req_update_new->reg_dump_buf_desc,
  1032. sizeof(struct cam_cmd_buf_desc) *
  1033. req_update_new->num_reg_dump_buf);
  1034. req_update_old->num_reg_dump_buf =
  1035. req_update_new->num_reg_dump_buf;
  1036. }
  1037. /* Update HW update params for ePCR */
  1038. hw_update_data = &req_isp_new->hw_update_data;
  1039. req_isp_old->hw_update_data.frame_header_res_id =
  1040. req_isp_new->hw_update_data.frame_header_res_id;
  1041. req_isp_old->hw_update_data.frame_header_cpu_addr =
  1042. hw_update_data->frame_header_cpu_addr;
  1043. if (req_isp_new->hw_update_data.mup_en) {
  1044. req_isp_old->hw_update_data.mup_en =
  1045. req_isp_new->hw_update_data.mup_en;
  1046. req_isp_old->hw_update_data.mup_val =
  1047. req_isp_new->hw_update_data.mup_val;
  1048. req_isp_old->hw_update_data.num_exp =
  1049. req_isp_new->hw_update_data.num_exp;
  1050. }
  1051. /* Copy FCG HW update params */
  1052. fcg_info_new = &hw_update_data->fcg_info;
  1053. fcg_info_old = &req_isp_old->hw_update_data.fcg_info;
  1054. fcg_info_old->use_current_cfg = true;
  1055. if (fcg_info_new->ife_fcg_online) {
  1056. fcg_info_old->ife_fcg_online = true;
  1057. fcg_info_old->ife_fcg_entry_idx =
  1058. req_isp_old->num_cfg +
  1059. fcg_info_new->ife_fcg_entry_idx;
  1060. memcpy(&fcg_info_old->ife_fcg_config,
  1061. &fcg_info_new->ife_fcg_config,
  1062. sizeof(struct cam_isp_fcg_config_internal));
  1063. }
  1064. if (fcg_info_new->sfe_fcg_online) {
  1065. fcg_info_old->sfe_fcg_online = true;
  1066. fcg_info_old->sfe_fcg_entry_idx =
  1067. req_isp_old->num_cfg +
  1068. fcg_info_new->sfe_fcg_entry_idx;
  1069. memcpy(&fcg_info_old->sfe_fcg_config,
  1070. &fcg_info_new->sfe_fcg_config,
  1071. sizeof(struct cam_isp_fcg_config_internal));
  1072. }
  1073. req_isp_old->num_cfg += req_isp_new->num_cfg;
  1074. req_old->request_id = req->request_id;
  1075. list_splice_init(&req->buf_tracker, &req_old->buf_tracker);
  1076. list_add_tail(&req->list, &ctx->free_req_list);
  1077. }
  1078. } else {
  1079. CAM_WARN(CAM_ISP,
  1080. "Received Update pkt before INIT pkt. req_id= %lld, ctx_idx: %u, link: 0x%x",
  1081. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1082. rc = -EINVAL;
  1083. }
  1084. end:
  1085. spin_unlock_bh(&ctx->lock);
  1086. return rc;
  1087. }
  1088. static char *__cam_isp_ife_sfe_resource_handle_id_to_type(
  1089. uint32_t resource_handle)
  1090. {
  1091. switch (resource_handle) {
  1092. /* IFE output ports */
  1093. case CAM_ISP_IFE_OUT_RES_FULL: return "IFE_FULL";
  1094. case CAM_ISP_IFE_OUT_RES_DS4: return "IFE_DS4";
  1095. case CAM_ISP_IFE_OUT_RES_DS16: return "IFE_DS16";
  1096. case CAM_ISP_IFE_OUT_RES_RAW_DUMP: return "IFE_RAW_DUMP";
  1097. case CAM_ISP_IFE_OUT_RES_FD: return "IFE_FD";
  1098. case CAM_ISP_IFE_OUT_RES_PDAF: return "IFE_PDAF";
  1099. case CAM_ISP_IFE_OUT_RES_RDI_0: return "IFE_RDI_0";
  1100. case CAM_ISP_IFE_OUT_RES_RDI_1: return "IFE_RDI_1";
  1101. case CAM_ISP_IFE_OUT_RES_RDI_2: return "IFE_RDI_2";
  1102. case CAM_ISP_IFE_OUT_RES_RDI_3: return "IFE_RDI_3";
  1103. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BE: return "IFE_STATS_HDR_BE";
  1104. case CAM_ISP_IFE_OUT_RES_STATS_HDR_BHIST: return "IFE_STATS_HDR_BHIST";
  1105. case CAM_ISP_IFE_OUT_RES_STATS_TL_BG: return "IFE_STATS_TL_BG";
  1106. case CAM_ISP_IFE_OUT_RES_STATS_BF: return "IFE_STATS_BF";
  1107. case CAM_ISP_IFE_OUT_RES_STATS_AWB_BG: return "IFE_STATS_AWB_BG";
  1108. case CAM_ISP_IFE_OUT_RES_STATS_BHIST: return "IFE_STATS_BHIST";
  1109. case CAM_ISP_IFE_OUT_RES_STATS_RS: return "IFE_STATS_RS";
  1110. case CAM_ISP_IFE_OUT_RES_STATS_CS: return "IFE_STATS_CS";
  1111. case CAM_ISP_IFE_OUT_RES_STATS_IHIST: return "IFE_STATS_IHIST";
  1112. case CAM_ISP_IFE_OUT_RES_FULL_DISP: return "IFE_FULL_DISP";
  1113. case CAM_ISP_IFE_OUT_RES_DS4_DISP: return "IFE_DS4_DISP";
  1114. case CAM_ISP_IFE_OUT_RES_DS16_DISP: return "IFE_DS16_DISP";
  1115. case CAM_ISP_IFE_OUT_RES_2PD: return "IFE_2PD";
  1116. case CAM_ISP_IFE_OUT_RES_LCR: return "IFE_LCR";
  1117. case CAM_ISP_IFE_OUT_RES_AWB_BFW: return "IFE_AWB_BFW";
  1118. case CAM_ISP_IFE_OUT_RES_PREPROCESS_2PD: return "IFE_PREPROCESS_2PD";
  1119. case CAM_ISP_IFE_OUT_RES_STATS_AEC_BE: return "IFE_STATS_AEC_BE";
  1120. case CAM_ISP_IFE_OUT_RES_LTM_STATS: return "IFE_LTM_STATS";
  1121. case CAM_ISP_IFE_OUT_RES_STATS_GTM_BHIST: return "IFE_STATS_GTM_BHIST";
  1122. case CAM_ISP_IFE_LITE_OUT_RES_STATS_BG: return "IFE_STATS_BG";
  1123. case CAM_ISP_IFE_LITE_OUT_RES_PREPROCESS_RAW: return "IFE_PREPROCESS_RAW";
  1124. case CAM_ISP_IFE_OUT_RES_SPARSE_PD: return "IFE_SPARSE_PD";
  1125. case CAM_ISP_IFE_OUT_RES_STATS_CAF: return "IFE_STATS_CAF";
  1126. case CAM_ISP_IFE_OUT_RES_STATS_BAYER_RS: return "IFE_STATS_BAYER_RS";
  1127. case CAM_ISP_IFE_OUT_RES_PDAF_PARSED_DATA: return "IFE_PDAF_PARSED_DATA";
  1128. case CAM_ISP_IFE_OUT_RES_STATS_ALSC: return "IFE_STATS_ALSC";
  1129. /* SFE output ports */
  1130. case CAM_ISP_SFE_OUT_RES_RDI_0: return "SFE_RDI_0";
  1131. case CAM_ISP_SFE_OUT_RES_RDI_1: return "SFE_RDI_1";
  1132. case CAM_ISP_SFE_OUT_RES_RDI_2: return "SFE_RDI_2";
  1133. case CAM_ISP_SFE_OUT_RES_RDI_3: return "SFE_RDI_3";
  1134. case CAM_ISP_SFE_OUT_RES_RDI_4: return "SFE_RDI_4";
  1135. case CAM_ISP_SFE_OUT_BE_STATS_0: return "SFE_BE_STATS_0";
  1136. case CAM_ISP_SFE_OUT_BE_STATS_1: return "SFE_BE_STATS_1";
  1137. case CAM_ISP_SFE_OUT_BE_STATS_2: return "SFE_BE_STATS_2";
  1138. case CAM_ISP_SFE_OUT_BHIST_STATS_0: return "SFE_BHIST_STATS_0";
  1139. case CAM_ISP_SFE_OUT_BHIST_STATS_1: return "SFE_BHIST_STATS_1";
  1140. case CAM_ISP_SFE_OUT_BHIST_STATS_2: return "SFE_BHIST_STATS_2";
  1141. case CAM_ISP_SFE_OUT_RES_LCR: return "SFE_LCR";
  1142. case CAM_ISP_SFE_OUT_RES_RAW_DUMP: return "SFE_PROCESSED_RAW";
  1143. case CAM_ISP_SFE_OUT_RES_IR: return "SFE_IR";
  1144. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_0: return "SFE_RS_STATS_0";
  1145. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_1: return "SFE_RS_STATS_1";
  1146. case CAM_ISP_SFE_OUT_BAYER_RS_STATS_2: return "SFE_RS_STATS_2";
  1147. case CAM_ISP_SFE_OUT_HDR_STATS: return "HDR_STATS";
  1148. /* SFE input ports */
  1149. case CAM_ISP_SFE_IN_RD_0: return "SFE_RD_0";
  1150. case CAM_ISP_SFE_IN_RD_1: return "SFE_RD_1";
  1151. case CAM_ISP_SFE_IN_RD_2: return "SFE_RD_2";
  1152. /* Handle invalid type */
  1153. default: return "Invalid_Resource_Type";
  1154. }
  1155. }
  1156. static const char *__cam_isp_tfe_resource_handle_id_to_type(
  1157. uint32_t resource_handle)
  1158. {
  1159. switch (resource_handle) {
  1160. /* TFE output ports */
  1161. case CAM_ISP_TFE_OUT_RES_FULL: return "TFE_FULL";
  1162. case CAM_ISP_TFE_OUT_RES_RAW_DUMP: return "TFE_RAW_DUMP";
  1163. case CAM_ISP_TFE_OUT_RES_PDAF: return "TFE_PDAF";
  1164. case CAM_ISP_TFE_OUT_RES_RDI_0: return "TFE_RDI_0";
  1165. case CAM_ISP_TFE_OUT_RES_RDI_1: return "TFE_RDI_1";
  1166. case CAM_ISP_TFE_OUT_RES_RDI_2: return "TFE_RDI_2";
  1167. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BE: return "TFE_STATS_HDR_BE";
  1168. case CAM_ISP_TFE_OUT_RES_STATS_HDR_BHIST: return "TFE_STATS_HDR_BHIST";
  1169. case CAM_ISP_TFE_OUT_RES_STATS_TL_BG: return "TFE_STATS_TL_BG";
  1170. case CAM_ISP_TFE_OUT_RES_STATS_BF: return "TFE_STATS_BF";
  1171. case CAM_ISP_TFE_OUT_RES_STATS_AWB_BG: return "TFE_STATS_AWB_BG";
  1172. case CAM_ISP_TFE_OUT_RES_STATS_RS: return "TFE_STATS_RS";
  1173. case CAM_ISP_TFE_OUT_RES_DS4: return "TFE_DS_4";
  1174. case CAM_ISP_TFE_OUT_RES_DS16: return "TFE_DS_16";
  1175. case CAM_ISP_TFE_OUT_RES_AI: return "TFE_AI";
  1176. case CAM_ISP_TFE_OUT_RES_PD_LCR_STATS: return "TFE_LCR_STATS";
  1177. case CAM_ISP_TFE_OUT_RES_PD_PREPROCESSED: return "TFE_PD_PREPROCESSED";
  1178. case CAM_ISP_TFE_OUT_RES_PD_PARSED: return "TFE_PD_PARSED";
  1179. /* Handle invalid type */
  1180. default: return "Invalid_Resource_Type";
  1181. }
  1182. }
  1183. static const char *__cam_isp_resource_handle_id_to_type(
  1184. uint32_t device_type, uint32_t resource_handle)
  1185. {
  1186. switch (device_type) {
  1187. case CAM_IFE_DEVICE_TYPE:
  1188. case CAM_TFE_MC_DEVICE_TYPE:
  1189. return __cam_isp_ife_sfe_resource_handle_id_to_type(resource_handle);
  1190. case CAM_TFE_DEVICE_TYPE:
  1191. return __cam_isp_tfe_resource_handle_id_to_type(resource_handle);
  1192. default:
  1193. return "INVALID_DEV_TYPE";
  1194. }
  1195. }
  1196. static uint64_t __cam_isp_ctx_get_event_ts(uint32_t evt_id, void *evt_data)
  1197. {
  1198. uint64_t ts = 0;
  1199. if (!evt_data)
  1200. return 0;
  1201. switch (evt_id) {
  1202. case CAM_ISP_HW_EVENT_ERROR:
  1203. ts = ((struct cam_isp_hw_error_event_data *)evt_data)->
  1204. timestamp;
  1205. break;
  1206. case CAM_ISP_HW_EVENT_SOF:
  1207. ts = ((struct cam_isp_hw_sof_event_data *)evt_data)->
  1208. timestamp;
  1209. break;
  1210. case CAM_ISP_HW_EVENT_REG_UPDATE:
  1211. ts = ((struct cam_isp_hw_reg_update_event_data *)evt_data)->
  1212. timestamp;
  1213. break;
  1214. case CAM_ISP_HW_EVENT_EPOCH:
  1215. ts = ((struct cam_isp_hw_epoch_event_data *)evt_data)->
  1216. timestamp;
  1217. break;
  1218. case CAM_ISP_HW_EVENT_EOF:
  1219. ts = ((struct cam_isp_hw_eof_event_data *)evt_data)->
  1220. timestamp;
  1221. break;
  1222. case CAM_ISP_HW_EVENT_DONE:
  1223. case CAM_ISP_HW_SECONDARY_EVENT:
  1224. break;
  1225. default:
  1226. CAM_DBG(CAM_ISP, "Invalid Event Type %d", evt_id);
  1227. }
  1228. return ts;
  1229. }
  1230. static int __cam_isp_ctx_get_hw_timestamp(struct cam_context *ctx, uint64_t *prev_ts,
  1231. uint64_t *curr_ts, uint64_t *boot_ts)
  1232. {
  1233. struct cam_hw_cmd_args hw_cmd_args;
  1234. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1235. int rc;
  1236. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1237. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1238. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1239. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_SOF_TS;
  1240. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1241. if (rc)
  1242. return rc;
  1243. if (isp_hw_cmd_args.u.sof_ts.prev >= isp_hw_cmd_args.u.sof_ts.curr) {
  1244. CAM_ERR(CAM_ISP, "ctx:%u link:0x%x prev timestamp greater than curr timestamp",
  1245. ctx->ctx_id, ctx->link_hdl);
  1246. return -EINVAL;
  1247. }
  1248. *prev_ts = isp_hw_cmd_args.u.sof_ts.prev;
  1249. *curr_ts = isp_hw_cmd_args.u.sof_ts.curr;
  1250. *boot_ts = isp_hw_cmd_args.u.sof_ts.boot;
  1251. return 0;
  1252. }
  1253. static int __cam_isp_ctx_get_cdm_done_timestamp(struct cam_context *ctx,
  1254. uint64_t *last_cdm_done_req)
  1255. {
  1256. struct cam_hw_cmd_args hw_cmd_args;
  1257. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1258. struct tm ts;
  1259. int rc;
  1260. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  1261. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  1262. hw_cmd_args.u.internal_args = &isp_hw_cmd_args;
  1263. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  1264. rc = ctx->hw_mgr_intf->hw_cmd(ctx->ctxt_to_hw_map, &hw_cmd_args);
  1265. if (rc)
  1266. return rc;
  1267. *last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  1268. ctx->cdm_done_ts = isp_hw_cmd_args.cdm_done_ts;
  1269. time64_to_tm(isp_hw_cmd_args.cdm_done_ts.tv_sec, 0, &ts);
  1270. CAM_DBG(CAM_ISP,
  1271. "last_cdm_done req: %llu ctx: %u link: 0x%x time[%d-%d %d:%d:%d.%lld]",
  1272. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl,
  1273. ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec,
  1274. isp_hw_cmd_args.cdm_done_ts.tv_nsec / 1000000);
  1275. return 0;
  1276. }
  1277. static int __cam_isp_ctx_recover_sof_timestamp(struct cam_context *ctx, uint64_t request_id)
  1278. {
  1279. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  1280. uint64_t prev_ts, curr_ts, boot_ts;
  1281. uint64_t a, b, c;
  1282. int rc;
  1283. rc = __cam_isp_ctx_get_hw_timestamp(ctx, &prev_ts, &curr_ts, &boot_ts);
  1284. if (rc) {
  1285. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  1286. ctx->ctx_id, ctx->link_hdl);
  1287. return rc;
  1288. }
  1289. /**
  1290. * If the last received SOF was for frame A and we have missed the SOF for frame B,
  1291. * then we need to find out if the hardware is at frame B or C.
  1292. * +-----+-----+-----+
  1293. * | A | B | C |
  1294. * +-----+-----+-----+
  1295. */
  1296. a = ctx_isp->sof_timestamp_val;
  1297. if (a == prev_ts) {
  1298. /* Hardware is at frame B */
  1299. b = curr_ts;
  1300. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x recover time(last:0x%llx,curr:0x%llx)req:%llu",
  1301. ctx->ctx_id, ctx->link_hdl, a, b, request_id);
  1302. } else if (a < prev_ts) {
  1303. /* Hardware is at frame C */
  1304. b = prev_ts;
  1305. c = curr_ts;
  1306. CAM_DBG(CAM_ISP,
  1307. "ctx:%u link:0x%x recover time(last:0x%llx,prev:0x%llx,curr:0x%llx)req:%llu",
  1308. ctx->ctx_id, ctx->link_hdl, a, b, c, request_id);
  1309. } else {
  1310. /* Hardware is at frame A (which we supposedly missed) */
  1311. CAM_ERR_RATE_LIMIT(CAM_ISP,
  1312. "ctx:%u link: 0x%x erroneous call to SOF recovery(last:0x%llx, prev:0x%llx, curr:0x%llx) req: %llu",
  1313. ctx->ctx_id, ctx->link_hdl, a, prev_ts, curr_ts, request_id);
  1314. return 0;
  1315. }
  1316. ctx_isp->boot_timestamp = boot_ts + (b - curr_ts);
  1317. ctx_isp->sof_timestamp_val = b;
  1318. ctx_isp->frame_id++;
  1319. return 0;
  1320. }
  1321. static void __cam_isp_ctx_send_sof_boot_timestamp(
  1322. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1323. uint32_t sof_event_status, struct shutter_event *shutter_event)
  1324. {
  1325. struct cam_req_mgr_message req_msg;
  1326. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1327. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1328. req_msg.u.frame_msg.request_id = request_id;
  1329. req_msg.u.frame_msg.timestamp = ctx_isp->boot_timestamp;
  1330. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1331. req_msg.u.frame_msg.sof_status = sof_event_status;
  1332. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1333. CAM_DBG(CAM_ISP,
  1334. "request id:%lld frame number:%lld boot time stamp:0x%llx status:%u",
  1335. request_id, ctx_isp->frame_id,
  1336. ctx_isp->boot_timestamp, sof_event_status);
  1337. shutter_event->frame_id = ctx_isp->frame_id;
  1338. shutter_event->req_id = request_id;
  1339. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1340. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1341. if (cam_req_mgr_notify_message(&req_msg,
  1342. V4L_EVENT_CAM_REQ_MGR_SOF_BOOT_TS,
  1343. V4L_EVENT_CAM_REQ_MGR_EVENT))
  1344. CAM_ERR(CAM_ISP,
  1345. "Error in notifying the boot time for req id:%lld",
  1346. request_id);
  1347. __cam_isp_ctx_update_event_record(ctx_isp,
  1348. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1349. }
  1350. static void __cam_isp_ctx_send_unified_timestamp(
  1351. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1352. struct shutter_event *shutter_event)
  1353. {
  1354. struct cam_req_mgr_message req_msg;
  1355. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1356. req_msg.u.frame_msg_v2.frame_id = ctx_isp->frame_id;
  1357. req_msg.u.frame_msg_v2.request_id = request_id;
  1358. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_SOF_QTIMER_TIMESTAMP] =
  1359. (request_id == 0) ? 0 : ctx_isp->sof_timestamp_val;
  1360. req_msg.u.frame_msg_v2.timestamps[CAM_REQ_BOOT_TIMESTAMP] = ctx_isp->boot_timestamp;
  1361. req_msg.u.frame_msg_v2.link_hdl = ctx_isp->base->link_hdl;
  1362. req_msg.u.frame_msg_v2.frame_id_meta = ctx_isp->frame_id_meta;
  1363. CAM_DBG(CAM_ISP,
  1364. "link hdl 0x%x request id:%lld frame number:%lld SOF time stamp:0x%llx ctx %d\
  1365. boot time stamp:0x%llx", ctx_isp->base->link_hdl, request_id,
  1366. ctx_isp->frame_id, ctx_isp->sof_timestamp_val,ctx_isp->base->ctx_id,
  1367. ctx_isp->boot_timestamp);
  1368. shutter_event->frame_id = ctx_isp->frame_id;
  1369. shutter_event->req_id = request_id;
  1370. shutter_event->boot_ts = ctx_isp->boot_timestamp;
  1371. shutter_event->sof_ts = ctx_isp->sof_timestamp_val;
  1372. if (cam_req_mgr_notify_message(&req_msg,
  1373. V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1374. CAM_ERR(CAM_ISP,
  1375. "Error in notifying the sof and boot time for req id:%lld",
  1376. request_id);
  1377. __cam_isp_ctx_update_event_record(ctx_isp,
  1378. CAM_ISP_CTX_EVENT_SHUTTER, NULL, shutter_event);
  1379. }
  1380. static void __cam_isp_ctx_send_sof_timestamp_frame_header(
  1381. struct cam_isp_context *ctx_isp, uint32_t *frame_header_cpu_addr,
  1382. uint64_t request_id, uint32_t sof_event_status)
  1383. {
  1384. uint32_t *time32 = NULL;
  1385. uint64_t timestamp = 0;
  1386. struct cam_req_mgr_message req_msg;
  1387. time32 = frame_header_cpu_addr;
  1388. timestamp = (uint64_t) time32[1];
  1389. timestamp = timestamp << 24;
  1390. timestamp |= (uint64_t)(time32[0] >> 8);
  1391. timestamp = mul_u64_u32_div(timestamp,
  1392. CAM_IFE_QTIMER_MUL_FACTOR,
  1393. CAM_IFE_QTIMER_DIV_FACTOR);
  1394. ctx_isp->sof_timestamp_val = timestamp;
  1395. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1396. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1397. req_msg.u.frame_msg.request_id = request_id;
  1398. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1399. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1400. req_msg.u.frame_msg.sof_status = sof_event_status;
  1401. CAM_DBG(CAM_ISP,
  1402. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u",
  1403. request_id, ctx_isp->frame_id,
  1404. ctx_isp->sof_timestamp_val, sof_event_status);
  1405. if (cam_req_mgr_notify_message(&req_msg,
  1406. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1407. CAM_ERR(CAM_ISP,
  1408. "Error in notifying the sof time for req id:%lld",
  1409. request_id);
  1410. }
  1411. static void __cam_isp_ctx_send_sof_timestamp(
  1412. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1413. uint32_t sof_event_status)
  1414. {
  1415. struct cam_req_mgr_message req_msg;
  1416. struct cam_context *ctx = ctx_isp->base;
  1417. struct shutter_event shutter_event = {0};
  1418. if (ctx_isp->reported_frame_id == ctx_isp->frame_id) {
  1419. if (__cam_isp_ctx_recover_sof_timestamp(ctx_isp->base, request_id))
  1420. CAM_WARN(CAM_ISP, "Missed SOF.No SOF timestamp recovery,ctx:%u,link:0x%x",
  1421. ctx->ctx_id, ctx->link_hdl);
  1422. }
  1423. if (request_id == 0 && (ctx_isp->reported_frame_id == ctx_isp->frame_id)) {
  1424. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1425. "Missed SOF Recovery for invalid req, Skip notificaiton to userspace Ctx: %u link: 0x%x frame_id %u",
  1426. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id);
  1427. return;
  1428. }
  1429. ctx_isp->reported_frame_id = ctx_isp->frame_id;
  1430. shutter_event.status = sof_event_status;
  1431. if ((ctx_isp->v4l2_event_sub_ids & (1 << V4L_EVENT_CAM_REQ_MGR_SOF_UNIFIED_TS))
  1432. && !ctx_isp->use_frame_header_ts) {
  1433. __cam_isp_ctx_send_unified_timestamp(ctx_isp, request_id, &shutter_event);
  1434. return;
  1435. }
  1436. if ((ctx_isp->use_frame_header_ts) || (request_id == 0))
  1437. goto end;
  1438. req_msg.session_hdl = ctx_isp->base->session_hdl;
  1439. req_msg.u.frame_msg.frame_id = ctx_isp->frame_id;
  1440. req_msg.u.frame_msg.request_id = request_id;
  1441. req_msg.u.frame_msg.timestamp = ctx_isp->sof_timestamp_val;
  1442. req_msg.u.frame_msg.link_hdl = ctx_isp->base->link_hdl;
  1443. req_msg.u.frame_msg.sof_status = sof_event_status;
  1444. req_msg.u.frame_msg.frame_id_meta = ctx_isp->frame_id_meta;
  1445. CAM_DBG(CAM_ISP,
  1446. "request id:%lld frame number:%lld SOF time stamp:0x%llx status:%u ctx_idx: %u, link: 0x%x",
  1447. request_id, ctx_isp->frame_id,
  1448. ctx_isp->sof_timestamp_val, sof_event_status, ctx->ctx_id, ctx->link_hdl);
  1449. if (cam_req_mgr_notify_message(&req_msg,
  1450. V4L_EVENT_CAM_REQ_MGR_SOF, V4L_EVENT_CAM_REQ_MGR_EVENT))
  1451. CAM_ERR(CAM_ISP,
  1452. "Error in notifying the sof time for req id:%lld, ctx_idx: %u, link: 0x%x",
  1453. request_id, ctx->ctx_id, ctx->link_hdl);
  1454. end:
  1455. __cam_isp_ctx_send_sof_boot_timestamp(ctx_isp,
  1456. request_id, sof_event_status, &shutter_event);
  1457. }
  1458. static void __cam_isp_ctx_handle_buf_done_fail_log(
  1459. struct cam_isp_context *ctx_isp, uint64_t request_id,
  1460. struct cam_isp_ctx_req *req_isp)
  1461. {
  1462. int i;
  1463. struct cam_context *ctx = ctx_isp->base;
  1464. const char *handle_type;
  1465. if (req_isp->num_fence_map_out >= CAM_ISP_CTX_RES_MAX) {
  1466. CAM_ERR(CAM_ISP,
  1467. "Num Resources exceed mMAX %d >= %d ",
  1468. req_isp->num_fence_map_out, CAM_ISP_CTX_RES_MAX);
  1469. return;
  1470. }
  1471. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1472. "Prev Req[%lld] : num_out=%d, num_acked=%d, bubble : report=%d, detected=%d",
  1473. request_id, req_isp->num_fence_map_out, req_isp->num_acked,
  1474. req_isp->bubble_report, req_isp->bubble_detected);
  1475. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1476. "Resource Handles that fail to generate buf_done in prev frame");
  1477. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1478. if (req_isp->fence_map_out[i].sync_id != -1) {
  1479. handle_type = __cam_isp_resource_handle_id_to_type(
  1480. ctx_isp->isp_device_type,
  1481. req_isp->fence_map_out[i].resource_handle);
  1482. trace_cam_log_event("Buf_done Congestion",
  1483. handle_type, request_id, req_isp->fence_map_out[i].sync_id);
  1484. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1485. "Resource_Handle: [%s][0x%x] Sync_ID: [0x%x]",
  1486. handle_type,
  1487. req_isp->fence_map_out[i].resource_handle,
  1488. req_isp->fence_map_out[i].sync_id);
  1489. }
  1490. }
  1491. ctx_isp->congestion_cnt++;
  1492. /* Trigger SOF freeze debug dump on 3 or greater instances of congestion */
  1493. if ((ctx_isp->congestion_cnt >= CAM_ISP_CONTEXT_CONGESTION_CNT_MAX) &&
  1494. (!ctx_isp->sof_dbg_irq_en))
  1495. __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  1496. }
  1497. static void __cam_isp_context_reset_internal_recovery_params(
  1498. struct cam_isp_context *ctx_isp)
  1499. {
  1500. atomic_set(&ctx_isp->internal_recovery_set, 0);
  1501. atomic_set(&ctx_isp->process_bubble, 0);
  1502. ctx_isp->aeb_error_cnt = 0;
  1503. ctx_isp->bubble_frame_cnt = 0;
  1504. ctx_isp->congestion_cnt = 0;
  1505. ctx_isp->sof_dbg_irq_en = false;
  1506. }
  1507. static int __cam_isp_context_try_internal_recovery(
  1508. struct cam_isp_context *ctx_isp)
  1509. {
  1510. int rc = 0;
  1511. struct cam_context *ctx = ctx_isp->base;
  1512. struct cam_ctx_request *req;
  1513. struct cam_isp_ctx_req *req_isp;
  1514. /*
  1515. * Start with wait list, if recovery is stil set
  1516. * errored request has not been moved to pending yet.
  1517. * Buf done for errored request has not occurred recover
  1518. * from here
  1519. */
  1520. if (!list_empty(&ctx->wait_req_list)) {
  1521. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  1522. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1523. if (req->request_id == ctx_isp->recovery_req_id) {
  1524. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1525. CRM_KMD_ERR_BUBBLE, ctx_isp->recovery_req_id, ctx_isp);
  1526. if (rc) {
  1527. /* Unable to do bubble recovery reset back to normal */
  1528. CAM_WARN(CAM_ISP,
  1529. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1530. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1531. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1532. req_isp->bubble_detected = false;
  1533. goto end;
  1534. }
  1535. list_del_init(&req->list);
  1536. list_add(&req->list, &ctx->pending_req_list);
  1537. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1538. CAM_INFO(CAM_ISP,
  1539. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1540. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1541. goto end;
  1542. }
  1543. }
  1544. /*
  1545. * If not in wait list only other possibility is request is in pending list
  1546. * on error detection, bubble detect is set assuming new frame after detection
  1547. * comes in, there is an rup it's moved to active list and it finishes with
  1548. * it's buf done's
  1549. */
  1550. if (!list_empty(&ctx->pending_req_list)) {
  1551. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request, list);
  1552. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1553. if (req->request_id == ctx_isp->recovery_req_id) {
  1554. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  1555. CRM_KMD_ERR_BUBBLE, ctx_isp->recovery_req_id, ctx_isp);
  1556. if (rc) {
  1557. /* Unable to do bubble recovery reset back to normal */
  1558. CAM_WARN(CAM_ISP,
  1559. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  1560. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1561. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1562. req_isp->bubble_detected = false;
  1563. goto end;
  1564. }
  1565. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  1566. CAM_INFO(CAM_ISP,
  1567. "Internal recovery for req: %llu in ctx: %u on link: 0x%x triggered",
  1568. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  1569. goto end;
  1570. }
  1571. }
  1572. /* If request is not found in either of the lists skip recovery */
  1573. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  1574. end:
  1575. return rc;
  1576. }
  1577. static int __cam_isp_ctx_handle_buf_done_for_req_list(
  1578. struct cam_isp_context *ctx_isp,
  1579. struct cam_ctx_request *req)
  1580. {
  1581. int rc = 0, i;
  1582. uint64_t buf_done_req_id;
  1583. struct cam_isp_ctx_req *req_isp;
  1584. struct cam_context *ctx = ctx_isp->base;
  1585. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1586. ctx_isp->active_req_cnt--;
  1587. buf_done_req_id = req->request_id;
  1588. if (req_isp->bubble_detected && req_isp->bubble_report) {
  1589. req_isp->num_acked = 0;
  1590. req_isp->num_deferred_acks = 0;
  1591. req_isp->bubble_detected = false;
  1592. list_del_init(&req->list);
  1593. atomic_set(&ctx_isp->process_bubble, 0);
  1594. req_isp->cdm_reset_before_apply = false;
  1595. ctx_isp->bubble_frame_cnt = 0;
  1596. if (buf_done_req_id <= ctx->last_flush_req) {
  1597. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1598. for (i = 0; i < req_isp->num_fence_map_out; i++)
  1599. rc = cam_sync_signal(
  1600. req_isp->fence_map_out[i].sync_id,
  1601. CAM_SYNC_STATE_SIGNALED_ERROR,
  1602. CAM_SYNC_ISP_EVENT_BUBBLE);
  1603. list_add_tail(&req->list, &ctx->free_req_list);
  1604. CAM_DBG(CAM_REQ,
  1605. "Move active request %lld to free list(cnt = %d) [flushed], ctx %u, link: 0x%x",
  1606. buf_done_req_id, ctx_isp->active_req_cnt,
  1607. ctx->ctx_id, ctx->link_hdl);
  1608. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1609. } else {
  1610. list_add(&req->list, &ctx->pending_req_list);
  1611. CAM_DBG(CAM_REQ,
  1612. "Move active request %lld to pending list(cnt = %d) [bubble recovery], ctx %u, link: 0x%x",
  1613. req->request_id, ctx_isp->active_req_cnt,
  1614. ctx->ctx_id, ctx->link_hdl);
  1615. }
  1616. } else {
  1617. if (!ctx_isp->use_frame_header_ts) {
  1618. if (ctx_isp->reported_req_id < buf_done_req_id) {
  1619. ctx_isp->reported_req_id = buf_done_req_id;
  1620. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  1621. buf_done_req_id,
  1622. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1623. }
  1624. }
  1625. list_del_init(&req->list);
  1626. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  1627. list_add_tail(&req->list, &ctx->free_req_list);
  1628. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  1629. req_isp->cdm_reset_before_apply = false;
  1630. req_isp->num_acked = 0;
  1631. req_isp->num_deferred_acks = 0;
  1632. /*
  1633. * Only update the process_bubble and bubble_frame_cnt
  1634. * when bubble is detected on this req, in case the other
  1635. * request is processing bubble.
  1636. */
  1637. if (req_isp->bubble_detected) {
  1638. atomic_set(&ctx_isp->process_bubble, 0);
  1639. ctx_isp->bubble_frame_cnt = 0;
  1640. req_isp->bubble_detected = false;
  1641. }
  1642. CAM_DBG(CAM_REQ,
  1643. "Move active request %lld to free list(cnt = %d) [all fences done], ctx %u link: 0x%x",
  1644. buf_done_req_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  1645. ctx_isp->req_info.last_bufdone_req_id = req->request_id;
  1646. ctx_isp->last_bufdone_err_apply_req_id = 0;
  1647. }
  1648. if (atomic_read(&ctx_isp->internal_recovery_set) && !ctx_isp->active_req_cnt)
  1649. __cam_isp_context_try_internal_recovery(ctx_isp);
  1650. cam_cpas_notify_event("IFE BufDone", buf_done_req_id);
  1651. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1652. CAM_ISP_STATE_CHANGE_TRIGGER_DONE, buf_done_req_id);
  1653. __cam_isp_ctx_update_event_record(ctx_isp,
  1654. CAM_ISP_CTX_EVENT_BUFDONE, req, NULL);
  1655. return rc;
  1656. }
  1657. static int __cam_isp_ctx_handle_buf_done_for_request(
  1658. struct cam_isp_context *ctx_isp,
  1659. struct cam_ctx_request *req,
  1660. struct cam_isp_hw_done_event_data *done,
  1661. uint32_t bubble_state,
  1662. struct cam_isp_hw_done_event_data *done_next_req)
  1663. {
  1664. int rc = 0;
  1665. int i, j, k;
  1666. bool not_found = false;
  1667. struct cam_isp_ctx_req *req_isp;
  1668. struct cam_context *ctx = ctx_isp->base;
  1669. const char *handle_type;
  1670. struct cam_isp_context_comp_record *comp_grp = NULL;
  1671. trace_cam_buf_done("ISP", ctx, req);
  1672. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1673. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u link: 0x%x",
  1674. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1675. done_next_req->resource_handle = 0;
  1676. done_next_req->timestamp = done->timestamp;
  1677. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1678. if (done->resource_handle ==
  1679. req_isp->fence_map_out[i].resource_handle)
  1680. break;
  1681. }
  1682. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1683. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1684. else
  1685. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1686. if (!comp_grp) {
  1687. CAM_ERR(CAM_ISP, "comp_grp is NULL");
  1688. rc = -EINVAL;
  1689. return rc;
  1690. }
  1691. if (i == req_isp->num_fence_map_out) {
  1692. for (j = 0; j < comp_grp->num_res; j++) {
  1693. not_found = false;
  1694. if (comp_grp->res_id[j] == done->resource_handle)
  1695. continue;
  1696. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1697. if (comp_grp->res_id[j] ==
  1698. req_isp->fence_map_out[k].resource_handle)
  1699. break;
  1700. if ((k == req_isp->num_fence_map_out) && (j != comp_grp->num_res - 1))
  1701. continue;
  1702. else if (k != req_isp->num_fence_map_out)
  1703. break;
  1704. else
  1705. not_found = true;
  1706. }
  1707. }
  1708. if (not_found) {
  1709. /*
  1710. * If not found in current request, it could be
  1711. * belonging to next request, this can happen if
  1712. * IRQ delay happens. It is only valid when the
  1713. * platform doesn't have last consumed address.
  1714. */
  1715. CAM_WARN(CAM_ISP,
  1716. "BUF_DONE for res %s not found in Req %lld ",
  1717. __cam_isp_resource_handle_id_to_type(
  1718. ctx_isp->isp_device_type,
  1719. done->resource_handle),
  1720. req->request_id);
  1721. done_next_req->hw_type = done->hw_type;
  1722. done_next_req->resource_handle = done->resource_handle;
  1723. done_next_req->comp_group_id = done->comp_group_id;
  1724. goto check_deferred;
  1725. }
  1726. for (i = 0; i < comp_grp->num_res; i++) {
  1727. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  1728. if (comp_grp->res_id[i] ==
  1729. req_isp->fence_map_out[j].resource_handle)
  1730. break;
  1731. }
  1732. if (j == req_isp->num_fence_map_out) {
  1733. /*
  1734. * If not found in current request, it could be
  1735. * belonging to an active port with no valid fence
  1736. * bound to it, we needn't process it.
  1737. */
  1738. CAM_DBG(CAM_ISP,
  1739. "BUF_DONE for res %s not active in Req %lld ctx %u link: 0x%x",
  1740. __cam_isp_resource_handle_id_to_type(
  1741. ctx_isp->isp_device_type,
  1742. comp_grp->res_id[i]),
  1743. req->request_id, ctx->ctx_id, ctx->link_hdl);
  1744. continue;
  1745. }
  1746. if (req_isp->fence_map_out[j].sync_id == -1) {
  1747. handle_type =
  1748. __cam_isp_resource_handle_id_to_type(
  1749. ctx_isp->isp_device_type,
  1750. req_isp->fence_map_out[j].resource_handle);
  1751. CAM_WARN(CAM_ISP,
  1752. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u link: 0x%x",
  1753. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  1754. trace_cam_log_event("Duplicate BufDone",
  1755. handle_type, req->request_id, ctx->ctx_id);
  1756. continue;
  1757. }
  1758. /* Get buf handles from packet and retrieve them from presil framework */
  1759. if (cam_presil_mode_enabled()) {
  1760. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  1761. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  1762. if (rc) {
  1763. CAM_ERR(CAM_ISP,
  1764. "Failed to retrieve image buffers req_id:%d ctx_id:%u link: 0x%x bubble detected:%d rc:%d",
  1765. req->request_id, ctx->ctx_id, ctx->link_hdl,
  1766. req_isp->bubble_detected, rc);
  1767. return rc;
  1768. }
  1769. }
  1770. if (!req_isp->bubble_detected) {
  1771. handle_type = __cam_isp_resource_handle_id_to_type(
  1772. ctx_isp->isp_device_type,
  1773. req_isp->fence_map_out[j].resource_handle);
  1774. CAM_DBG(CAM_ISP,
  1775. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x port %s",
  1776. req->request_id,
  1777. req_isp->fence_map_out[j].resource_handle,
  1778. req_isp->fence_map_out[j].sync_id,
  1779. ctx->ctx_id, ctx->link_hdl, handle_type);
  1780. cam_smmu_buffer_tracker_buffer_putref(
  1781. req_isp->fence_map_out[j].buffer_tracker);
  1782. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1783. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1784. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1785. if (rc)
  1786. CAM_DBG(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1787. rc, ctx->ctx_id, ctx->link_hdl);
  1788. } else if (!req_isp->bubble_report) {
  1789. CAM_DBG(CAM_ISP,
  1790. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx %u link: 0x%x",
  1791. req->request_id,
  1792. req_isp->fence_map_out[j].resource_handle,
  1793. req_isp->fence_map_out[j].sync_id,
  1794. ctx->ctx_id, ctx->link_hdl);
  1795. cam_smmu_buffer_tracker_buffer_putref(
  1796. req_isp->fence_map_out[j].buffer_tracker);
  1797. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1798. CAM_SYNC_STATE_SIGNALED_ERROR,
  1799. CAM_SYNC_ISP_EVENT_BUBBLE);
  1800. if (rc)
  1801. CAM_ERR(CAM_ISP, "Sync failed with rc = %d, ctx %u link: 0x%x",
  1802. rc, ctx->ctx_id, ctx->link_hdl);
  1803. } else {
  1804. /*
  1805. * Ignore the buffer done if bubble detect is on
  1806. * Increment the ack number here, and queue the
  1807. * request back to pending list whenever all the
  1808. * buffers are done.
  1809. */
  1810. req_isp->num_acked++;
  1811. CAM_DBG(CAM_ISP,
  1812. "buf done with bubble state %d recovery %d for req %lld, ctx %u link: 0x%x",
  1813. bubble_state,
  1814. req_isp->bubble_report,
  1815. req->request_id,
  1816. ctx->ctx_id, ctx->link_hdl);
  1817. continue;
  1818. }
  1819. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx %u link: 0x%x",
  1820. req->request_id,
  1821. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  1822. if (!rc) {
  1823. req_isp->num_acked++;
  1824. req_isp->fence_map_out[j].sync_id = -1;
  1825. }
  1826. if ((ctx_isp->use_frame_header_ts) &&
  1827. (req_isp->hw_update_data.frame_header_res_id ==
  1828. req_isp->fence_map_out[j].resource_handle))
  1829. __cam_isp_ctx_send_sof_timestamp_frame_header(
  1830. ctx_isp,
  1831. req_isp->hw_update_data.frame_header_cpu_addr,
  1832. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1833. }
  1834. check_deferred:
  1835. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1836. /* Should not happen */
  1837. CAM_ERR(CAM_ISP,
  1838. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u link: 0x%x",
  1839. req->request_id, req_isp->num_acked,
  1840. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1841. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1842. }
  1843. if (req_isp->num_acked != req_isp->num_fence_map_out)
  1844. return rc;
  1845. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1846. return rc;
  1847. }
  1848. static int __cam_isp_handle_deferred_buf_done(
  1849. struct cam_isp_context *ctx_isp,
  1850. struct cam_ctx_request *req,
  1851. bool bubble_handling,
  1852. uint32_t status, uint32_t event_cause)
  1853. {
  1854. int i, j;
  1855. int rc = 0;
  1856. struct cam_isp_ctx_req *req_isp =
  1857. (struct cam_isp_ctx_req *) req->req_priv;
  1858. struct cam_context *ctx = ctx_isp->base;
  1859. CAM_DBG(CAM_ISP,
  1860. "ctx[%u] link[0x%x] : Req %llu : Handling %d deferred buf_dones num_acked=%d, bubble_handling=%d",
  1861. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1862. req_isp->num_acked, bubble_handling);
  1863. for (i = 0; i < req_isp->num_deferred_acks; i++) {
  1864. j = req_isp->deferred_fence_map_index[i];
  1865. CAM_DBG(CAM_ISP,
  1866. "ctx[%u] link[0x%x] : Sync with status=%d, event_cause=%d: req %lld res 0x%x sync_id 0x%x",
  1867. ctx->ctx_id, ctx->link_hdl, status, event_cause,
  1868. req->request_id,
  1869. req_isp->fence_map_out[j].resource_handle,
  1870. req_isp->fence_map_out[j].sync_id);
  1871. if (req_isp->fence_map_out[j].sync_id == -1) {
  1872. CAM_WARN(CAM_ISP,
  1873. "ctx[%u] link[0x%x] : Deferred buf_done already signalled, req_id=%llu, j=%d, res=0x%x",
  1874. ctx->ctx_id, ctx->link_hdl, req->request_id, j,
  1875. req_isp->fence_map_out[j].resource_handle);
  1876. continue;
  1877. }
  1878. if (!bubble_handling) {
  1879. CAM_WARN(CAM_ISP,
  1880. "Unexpected Buf done for res=0x%x on ctx[%u] link[0x%x] for Req %llu, status=%d, possible bh delays",
  1881. req_isp->fence_map_out[j].resource_handle, ctx->ctx_id,
  1882. ctx->link_hdl, req->request_id, status);
  1883. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  1884. status, event_cause);
  1885. if (rc) {
  1886. CAM_ERR(CAM_ISP,
  1887. "ctx[%u] link[0x%x] : Sync signal for Req %llu, sync_id %d status=%d failed with rc = %d",
  1888. ctx->ctx_id, ctx->link_hdl, req->request_id,
  1889. req_isp->fence_map_out[j].sync_id,
  1890. status, rc);
  1891. } else {
  1892. req_isp->num_acked++;
  1893. req_isp->fence_map_out[j].sync_id = -1;
  1894. }
  1895. } else {
  1896. req_isp->num_acked++;
  1897. }
  1898. }
  1899. CAM_DBG(CAM_ISP,
  1900. "ctx[%u] link[0x%x] : Req %llu : Handled %d deferred buf_dones num_acked=%d, num_fence_map_out=%d",
  1901. ctx->ctx_id, ctx->link_hdl, req->request_id, req_isp->num_deferred_acks,
  1902. req_isp->num_acked, req_isp->num_fence_map_out);
  1903. req_isp->num_deferred_acks = 0;
  1904. return rc;
  1905. }
  1906. static int __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  1907. struct cam_isp_context *ctx_isp,
  1908. struct cam_ctx_request *req)
  1909. {
  1910. int rc = 0;
  1911. struct cam_context *ctx = ctx_isp->base;
  1912. struct cam_isp_ctx_req *req_isp;
  1913. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  1914. if (req_isp->num_deferred_acks)
  1915. rc = __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  1916. req_isp->bubble_report,
  1917. CAM_SYNC_STATE_SIGNALED_ERROR,
  1918. CAM_SYNC_ISP_EVENT_BUBBLE);
  1919. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  1920. /* Should not happen */
  1921. CAM_ERR(CAM_ISP,
  1922. "WARNING: req_id %lld num_acked %d > map_out %d, ctx %u, link[0x%x]",
  1923. req->request_id, req_isp->num_acked,
  1924. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  1925. WARN_ON(req_isp->num_acked > req_isp->num_fence_map_out);
  1926. }
  1927. if (req_isp->num_acked == req_isp->num_fence_map_out)
  1928. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  1929. return rc;
  1930. }
  1931. static int __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1932. struct cam_isp_context *ctx_isp,
  1933. struct cam_ctx_request *req,
  1934. struct cam_isp_hw_done_event_data *done,
  1935. uint32_t bubble_state,
  1936. bool verify_consumed_addr,
  1937. bool defer_buf_done)
  1938. {
  1939. int rc = 0;
  1940. int i, j, k, def_idx;
  1941. bool not_found = false;
  1942. bool duplicate_defer_buf_done = false;
  1943. struct cam_isp_ctx_req *req_isp;
  1944. struct cam_context *ctx = ctx_isp->base;
  1945. const char *handle_type;
  1946. uint32_t cmp_addr = 0;
  1947. struct cam_isp_hw_done_event_data unhandled_done = {0};
  1948. struct cam_isp_context_comp_record *comp_grp = NULL;
  1949. struct cam_hw_cmd_args hw_cmd_args;
  1950. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1951. trace_cam_buf_done("ISP", ctx, req);
  1952. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1953. CAM_DBG(CAM_ISP, "Enter with bubble_state %d, req_bubble_detected %d, ctx %u, link[0x%x]",
  1954. bubble_state, req_isp->bubble_detected, ctx->ctx_id, ctx->link_hdl);
  1955. unhandled_done.timestamp = done->timestamp;
  1956. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  1957. if (done->resource_handle ==
  1958. req_isp->fence_map_out[i].resource_handle) {
  1959. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  1960. req_isp->fence_map_out[i].image_buf_addr[0]) :
  1961. req_isp->fence_map_out[i].image_buf_addr[0];
  1962. if (!verify_consumed_addr ||
  1963. (verify_consumed_addr && (done->last_consumed_addr == cmp_addr))) {
  1964. break;
  1965. }
  1966. }
  1967. }
  1968. if (done->hw_type == CAM_ISP_HW_TYPE_SFE)
  1969. comp_grp = &ctx_isp->sfe_bus_comp_grp[done->comp_group_id];
  1970. else
  1971. comp_grp = &ctx_isp->vfe_bus_comp_grp[done->comp_group_id];
  1972. if (!comp_grp) {
  1973. CAM_ERR(CAM_ISP, "comp_grp is NULL");
  1974. rc = -EINVAL;
  1975. return rc;
  1976. }
  1977. if (i == req_isp->num_fence_map_out) {
  1978. not_found = true;
  1979. for (j = 0; j < comp_grp->num_res; j++) {
  1980. /* If the res is same with original res, we don't need to read again */
  1981. if (comp_grp->res_id[j] == done->resource_handle)
  1982. continue;
  1983. /* Check if the res in the requested list */
  1984. for (k = 0; k < req_isp->num_fence_map_out; k++)
  1985. if (comp_grp->res_id[j] ==
  1986. req_isp->fence_map_out[k].resource_handle)
  1987. break;
  1988. /* If res_id[j] isn't in requested list, then try next res in the group */
  1989. if (k == req_isp->num_fence_map_out) {
  1990. if (j != comp_grp->num_res - 1)
  1991. continue;
  1992. else
  1993. break;
  1994. }
  1995. if (!verify_consumed_addr) {
  1996. not_found = false;
  1997. break;
  1998. }
  1999. /*
  2000. * Find out the res from the requested list,
  2001. * then we can get last consumed address from this port.
  2002. */
  2003. done->resource_handle = comp_grp->res_id[j];
  2004. done->last_consumed_addr = 0;
  2005. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2006. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2007. isp_hw_cmd_args.cmd_type =
  2008. CAM_ISP_HW_MGR_GET_LAST_CONSUMED_ADDR;
  2009. isp_hw_cmd_args.cmd_data = done;
  2010. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2011. rc = ctx->hw_mgr_intf->hw_cmd(
  2012. ctx->hw_mgr_intf->hw_mgr_priv,
  2013. &hw_cmd_args);
  2014. if (rc) {
  2015. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2016. ctx->ctx_id, ctx->link_hdl);
  2017. return rc;
  2018. }
  2019. cmp_addr = cam_smmu_is_expanded_memory() ?
  2020. CAM_36BIT_INTF_GET_IOVA_BASE(
  2021. req_isp->fence_map_out[k].image_buf_addr[0]) :
  2022. req_isp->fence_map_out[k].image_buf_addr[0];
  2023. CAM_DBG(CAM_ISP, "Get res %s last_consumed_addr:0x%x cmp_addr:0x%x",
  2024. __cam_isp_resource_handle_id_to_type(
  2025. ctx_isp->isp_device_type, done->resource_handle),
  2026. done->last_consumed_addr, cmp_addr);
  2027. if (done->last_consumed_addr == cmp_addr) {
  2028. CAM_INFO(CAM_ISP, "found...");
  2029. not_found = false;
  2030. break;
  2031. }
  2032. }
  2033. }
  2034. if (not_found) {
  2035. /*
  2036. * If not found in current request, it could be
  2037. * belonging to next request, this can happen if
  2038. * IRQ delay happens. It is only valid when the
  2039. * platform doesn't have last consumed address.
  2040. */
  2041. CAM_WARN(CAM_ISP,
  2042. "BUF_DONE for res %s last_consumed_addr:0x%x not found in Req %lld ",
  2043. __cam_isp_resource_handle_id_to_type(
  2044. ctx_isp->isp_device_type, done->resource_handle),
  2045. done->last_consumed_addr,
  2046. req->request_id);
  2047. unhandled_done.hw_type = done->hw_type;
  2048. unhandled_done.resource_handle = done->resource_handle;
  2049. unhandled_done.comp_group_id = done->comp_group_id;
  2050. unhandled_done.last_consumed_addr = done->last_consumed_addr;
  2051. goto check_deferred;
  2052. }
  2053. for (i = 0; i < comp_grp->num_res; i++) {
  2054. for (j = 0; j < req_isp->num_fence_map_out; j++) {
  2055. if (comp_grp->res_id[i] ==
  2056. req_isp->fence_map_out[j].resource_handle)
  2057. break;
  2058. }
  2059. if (j == req_isp->num_fence_map_out) {
  2060. /*
  2061. * If not found in current request, it could be
  2062. * belonging to an active port with no valid fence
  2063. * bound to it, we needn't process it.
  2064. */
  2065. CAM_DBG(CAM_ISP,
  2066. "BUF_DONE for res %s not active in Req %lld ctx %u, link[0x%x]",
  2067. __cam_isp_resource_handle_id_to_type(
  2068. ctx_isp->isp_device_type, comp_grp->res_id[i]),
  2069. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2070. continue;
  2071. }
  2072. if (req_isp->fence_map_out[j].sync_id == -1) {
  2073. handle_type = __cam_isp_resource_handle_id_to_type(
  2074. ctx_isp->isp_device_type,
  2075. req_isp->fence_map_out[j].resource_handle);
  2076. CAM_WARN(CAM_ISP,
  2077. "Duplicate BUF_DONE for req %lld : i=%d, j=%d, res=%s, ctx %u, link[0x%x]",
  2078. req->request_id, i, j, handle_type, ctx->ctx_id, ctx->link_hdl);
  2079. trace_cam_log_event("Duplicate BufDone",
  2080. handle_type, req->request_id, ctx->ctx_id);
  2081. continue;
  2082. }
  2083. /* Get buf handles from packet and retrieve them from presil framework */
  2084. if (cam_presil_mode_enabled()) {
  2085. rc = cam_presil_retrieve_buffers_from_packet(req_isp->hw_update_data.packet,
  2086. ctx->img_iommu_hdl, req_isp->fence_map_out[j].resource_handle);
  2087. if (rc) {
  2088. CAM_ERR(CAM_ISP,
  2089. "Failed to retrieve image buffers req_id:%d ctx_id:%u link[0x%x] bubble detected:%d rc:%d",
  2090. req->request_id, ctx->ctx_id, ctx->link_hdl,
  2091. req_isp->bubble_detected, rc);
  2092. return rc;
  2093. }
  2094. }
  2095. if (defer_buf_done) {
  2096. uint32_t deferred_indx = req_isp->num_deferred_acks;
  2097. duplicate_defer_buf_done = false;
  2098. for (k = 0; k < req_isp->num_deferred_acks; k++) {
  2099. def_idx = req_isp->deferred_fence_map_index[k];
  2100. if (def_idx == j) {
  2101. CAM_WARN(CAM_ISP,
  2102. "duplicate deferred ack for ctx[%u] link[0x%x] req %lld res 0x%x sync_id 0x%x",
  2103. ctx->ctx_id, ctx->link_hdl,
  2104. req->request_id,
  2105. req_isp->fence_map_out[j].resource_handle,
  2106. req_isp->fence_map_out[j].sync_id);
  2107. duplicate_defer_buf_done = true;
  2108. break;
  2109. }
  2110. }
  2111. if (duplicate_defer_buf_done)
  2112. continue;
  2113. if (req_isp->num_deferred_acks == req_isp->num_fence_map_out) {
  2114. CAM_WARN(CAM_ISP,
  2115. "WARNING: req_id %lld num_deferred_acks %d > map_out %d, ctx_idx:%u link[0x%x]",
  2116. req->request_id, req_isp->num_deferred_acks,
  2117. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2118. continue;
  2119. }
  2120. /*
  2121. * If we are handling this BUF_DONE event for a request
  2122. * that is still in wait_list, do not signal now,
  2123. * instead mark it as done and handle it later -
  2124. * if this request is going into BUBBLE state later
  2125. * it will automatically be re-applied. If this is not
  2126. * going into BUBBLE, signal fences later.
  2127. * Note - we will come here only if the last consumed
  2128. * address matches with this ports buffer.
  2129. */
  2130. req_isp->deferred_fence_map_index[deferred_indx] = j;
  2131. req_isp->num_deferred_acks++;
  2132. CAM_DBG(CAM_ISP,
  2133. "ctx[%u] link[0x%x]:Deferred buf done for %llu with bubble state %d recovery %d",
  2134. ctx->ctx_id, ctx->link_hdl, req->request_id, bubble_state,
  2135. req_isp->bubble_report);
  2136. CAM_DBG(CAM_ISP,
  2137. "ctx[%u] link[0x%x]:Deferred info:num_acks=%d,fence_map_index=%d,resource_handle=0x%x,sync_id=%d",
  2138. ctx->ctx_id, ctx->link_hdl, req_isp->num_deferred_acks, j,
  2139. req_isp->fence_map_out[j].resource_handle,
  2140. req_isp->fence_map_out[j].sync_id);
  2141. continue;
  2142. } else if (!req_isp->bubble_detected) {
  2143. CAM_DBG(CAM_ISP,
  2144. "Sync with success: req %lld res 0x%x fd 0x%x, ctx %u res %s",
  2145. req->request_id,
  2146. req_isp->fence_map_out[j].resource_handle,
  2147. req_isp->fence_map_out[j].sync_id,
  2148. ctx->ctx_id,
  2149. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  2150. req_isp->fence_map_out[j].resource_handle));
  2151. cam_smmu_buffer_tracker_buffer_putref(
  2152. req_isp->fence_map_out[j].buffer_tracker);
  2153. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2154. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2155. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2156. if (rc) {
  2157. CAM_ERR(CAM_ISP,
  2158. "Sync=%u for req=%llu failed with rc=%d ctx:%u link[0x%x]",
  2159. req_isp->fence_map_out[j].sync_id, req->request_id,
  2160. rc, ctx->ctx_id, ctx->link_hdl);
  2161. } else if (req_isp->num_deferred_acks) {
  2162. /* Process deferred buf_done acks */
  2163. __cam_isp_handle_deferred_buf_done(ctx_isp,
  2164. req, false,
  2165. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2166. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2167. }
  2168. /* Reset fence */
  2169. req_isp->fence_map_out[j].sync_id = -1;
  2170. } else if (!req_isp->bubble_report) {
  2171. CAM_DBG(CAM_ISP,
  2172. "Sync with failure: req %lld res 0x%x fd 0x%x, ctx:%u link[0x%x]",
  2173. req->request_id,
  2174. req_isp->fence_map_out[j].resource_handle,
  2175. req_isp->fence_map_out[j].sync_id,
  2176. ctx->ctx_id, ctx->link_hdl);
  2177. cam_smmu_buffer_tracker_buffer_putref(
  2178. req_isp->fence_map_out[j].buffer_tracker);
  2179. rc = cam_sync_signal(req_isp->fence_map_out[j].sync_id,
  2180. CAM_SYNC_STATE_SIGNALED_ERROR,
  2181. CAM_SYNC_ISP_EVENT_BUBBLE);
  2182. if (rc) {
  2183. CAM_ERR(CAM_ISP,
  2184. "Sync:%u for req:%llu failed with rc:%d,ctx:%u,link[0x%x]",
  2185. req_isp->fence_map_out[j].sync_id, req->request_id,
  2186. rc, ctx->ctx_id, ctx->link_hdl);
  2187. } else if (req_isp->num_deferred_acks) {
  2188. /* Process deferred buf_done acks */
  2189. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2190. false,
  2191. CAM_SYNC_STATE_SIGNALED_ERROR,
  2192. CAM_SYNC_ISP_EVENT_BUBBLE);
  2193. }
  2194. /* Reset fence */
  2195. req_isp->fence_map_out[j].sync_id = -1;
  2196. } else {
  2197. /*
  2198. * Ignore the buffer done if bubble detect is on
  2199. * Increment the ack number here, and queue the
  2200. * request back to pending list whenever all the
  2201. * buffers are done.
  2202. */
  2203. req_isp->num_acked++;
  2204. CAM_DBG(CAM_ISP,
  2205. "buf done with bubble state %d recovery %d for req %lld, ctx_idx:%u link[0x%x]",
  2206. bubble_state,
  2207. req_isp->bubble_report,
  2208. req->request_id,
  2209. ctx->ctx_id, ctx->link_hdl);
  2210. /* Process deferred buf_done acks */
  2211. if (req_isp->num_deferred_acks)
  2212. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  2213. true,
  2214. CAM_SYNC_STATE_SIGNALED_ERROR,
  2215. CAM_SYNC_ISP_EVENT_BUBBLE);
  2216. if (req_isp->num_acked == req_isp->num_fence_map_out) {
  2217. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2218. if (rc)
  2219. CAM_ERR(CAM_ISP,
  2220. "Error in buf done for req = %llu with rc = %d, ctx_idx:%u link[0x%x]",
  2221. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  2222. return rc;
  2223. }
  2224. continue;
  2225. }
  2226. CAM_DBG(CAM_ISP, "req %lld, reset sync id 0x%x ctx_idx:%u link[0x%x]",
  2227. req->request_id,
  2228. req_isp->fence_map_out[j].sync_id, ctx->ctx_id, ctx->link_hdl);
  2229. if (!rc) {
  2230. req_isp->num_acked++;
  2231. }
  2232. if ((ctx_isp->use_frame_header_ts) &&
  2233. (req_isp->hw_update_data.frame_header_res_id ==
  2234. req_isp->fence_map_out[j].resource_handle))
  2235. __cam_isp_ctx_send_sof_timestamp_frame_header(
  2236. ctx_isp,
  2237. req_isp->hw_update_data.frame_header_cpu_addr,
  2238. req->request_id, CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2239. }
  2240. check_deferred:
  2241. if ((unhandled_done.resource_handle > 0) && (!defer_buf_done))
  2242. __cam_isp_ctx_check_deferred_buf_done(
  2243. ctx_isp, &unhandled_done, bubble_state);
  2244. if (req_isp->num_acked > req_isp->num_fence_map_out) {
  2245. /* Should not happen */
  2246. CAM_ERR(CAM_ISP,
  2247. "WARNING: req_id %lld num_acked %d > map_out %d, ctx_idx:%u link[0x%x]",
  2248. req->request_id, req_isp->num_acked,
  2249. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  2250. }
  2251. if (req_isp->num_acked != req_isp->num_fence_map_out)
  2252. return rc;
  2253. rc = __cam_isp_ctx_handle_buf_done_for_req_list(ctx_isp, req);
  2254. return rc;
  2255. }
  2256. static int __cam_isp_ctx_handle_buf_done(
  2257. struct cam_isp_context *ctx_isp,
  2258. struct cam_isp_hw_done_event_data *done,
  2259. uint32_t bubble_state)
  2260. {
  2261. int rc = 0;
  2262. struct cam_ctx_request *req;
  2263. struct cam_context *ctx = ctx_isp->base;
  2264. struct cam_isp_hw_done_event_data done_next_req = {0};
  2265. if (list_empty(&ctx->active_req_list)) {
  2266. CAM_WARN(CAM_ISP, "Buf done with no active request, ctx_idx:%u link[0x%x]",
  2267. ctx->ctx_id, ctx->link_hdl);
  2268. return 0;
  2269. }
  2270. req = list_first_entry(&ctx->active_req_list,
  2271. struct cam_ctx_request, list);
  2272. rc = __cam_isp_ctx_handle_buf_done_for_request(ctx_isp, req, done,
  2273. bubble_state, &done_next_req);
  2274. if (done_next_req.resource_handle) {
  2275. struct cam_isp_hw_done_event_data unhandled_res = {0};
  2276. struct cam_ctx_request *next_req = list_last_entry(
  2277. &ctx->active_req_list, struct cam_ctx_request, list);
  2278. if (next_req->request_id != req->request_id) {
  2279. /*
  2280. * Few resource handles are already signalled in the
  2281. * current request, lets check if there is another
  2282. * request waiting for these resources. This can
  2283. * happen if handling some of next request's buf done
  2284. * events are happening first before handling current
  2285. * request's remaining buf dones due to IRQ scheduling.
  2286. * Lets check only one more request as we will have
  2287. * maximum of 2 requests in active_list at any time.
  2288. */
  2289. CAM_WARN(CAM_ISP,
  2290. "Unhandled bufdone resources for req %lld,trying next request %lld,ctx:%u link[0x%x]",
  2291. req->request_id, next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2292. __cam_isp_ctx_handle_buf_done_for_request(ctx_isp,
  2293. next_req, &done_next_req,
  2294. bubble_state, &unhandled_res);
  2295. if (unhandled_res.resource_handle == 0)
  2296. CAM_INFO(CAM_ISP,
  2297. "BUF Done event handed for next request %lld, ctx_idx:%u link[0x%x]",
  2298. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2299. else
  2300. CAM_ERR(CAM_ISP,
  2301. "BUF Done not handled for next request %lld, ctx_idx:%u link[0x%x]",
  2302. next_req->request_id, ctx->ctx_id, ctx->link_hdl);
  2303. } else {
  2304. CAM_WARN(CAM_ISP,
  2305. "Req %lld only active request, spurious buf_done rxd, ctx_idx:%u link[0x%x]",
  2306. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2307. }
  2308. }
  2309. return rc;
  2310. }
  2311. static void __cam_isp_ctx_buf_done_match_req(
  2312. struct cam_ctx_request *req,
  2313. struct cam_isp_hw_done_event_data *done,
  2314. bool *irq_delay_detected)
  2315. {
  2316. int i;
  2317. uint32_t match_count = 0;
  2318. struct cam_isp_ctx_req *req_isp;
  2319. uint32_t cmp_addr = 0;
  2320. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2321. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2322. cmp_addr = cam_smmu_is_expanded_memory() ? CAM_36BIT_INTF_GET_IOVA_BASE(
  2323. req_isp->fence_map_out[i].image_buf_addr[0]) :
  2324. req_isp->fence_map_out[i].image_buf_addr[0];
  2325. if ((done->resource_handle ==
  2326. req_isp->fence_map_out[i].resource_handle) &&
  2327. (done->last_consumed_addr == cmp_addr)) {
  2328. match_count++;
  2329. break;
  2330. }
  2331. }
  2332. if (match_count > 0)
  2333. *irq_delay_detected = true;
  2334. else
  2335. *irq_delay_detected = false;
  2336. CAM_DBG(CAM_ISP,
  2337. "buf done num handles %d match count %d for next req:%lld",
  2338. done->resource_handle, match_count, req->request_id);
  2339. CAM_DBG(CAM_ISP,
  2340. "irq_delay_detected %d", *irq_delay_detected);
  2341. }
  2342. static void __cam_isp_ctx_try_buf_done_process_for_active_request(
  2343. uint32_t deferred_ack_start_idx, struct cam_isp_context *ctx_isp,
  2344. struct cam_ctx_request *deferred_req)
  2345. {
  2346. int i, j, deferred_map_idx, rc;
  2347. struct cam_context *ctx = ctx_isp->base;
  2348. struct cam_ctx_request *curr_active_req;
  2349. struct cam_isp_ctx_req *curr_active_isp_req;
  2350. struct cam_isp_ctx_req *deferred_isp_req;
  2351. if (list_empty(&ctx->active_req_list))
  2352. return;
  2353. curr_active_req = list_first_entry(&ctx->active_req_list,
  2354. struct cam_ctx_request, list);
  2355. curr_active_isp_req = (struct cam_isp_ctx_req *)curr_active_req->req_priv;
  2356. deferred_isp_req = (struct cam_isp_ctx_req *)deferred_req->req_priv;
  2357. /* Check from newly updated deferred acks */
  2358. for (i = deferred_ack_start_idx; i < deferred_isp_req->num_deferred_acks; i++) {
  2359. deferred_map_idx = deferred_isp_req->deferred_fence_map_index[i];
  2360. for (j = 0; j < curr_active_isp_req->num_fence_map_out; j++) {
  2361. /* resource needs to match */
  2362. if (curr_active_isp_req->fence_map_out[j].resource_handle !=
  2363. deferred_isp_req->fence_map_out[deferred_map_idx].resource_handle)
  2364. continue;
  2365. /* Check if fence is valid */
  2366. if (curr_active_isp_req->fence_map_out[j].sync_id == -1)
  2367. break;
  2368. CAM_WARN(CAM_ISP,
  2369. "Processing delayed buf done req: %llu bubble_detected: %s res: 0x%x fd: 0x%x, ctx: %u link: 0x%x [deferred req: %llu last applied: %llu]",
  2370. curr_active_req->request_id,
  2371. CAM_BOOL_TO_YESNO(curr_active_isp_req->bubble_detected),
  2372. curr_active_isp_req->fence_map_out[j].resource_handle,
  2373. curr_active_isp_req->fence_map_out[j].sync_id,
  2374. ctx->ctx_id, ctx->link_hdl,
  2375. deferred_req->request_id, ctx_isp->last_applied_req_id);
  2376. /* Signal only if bubble is not detected for this request */
  2377. if (!curr_active_isp_req->bubble_detected) {
  2378. rc = cam_sync_signal(curr_active_isp_req->fence_map_out[j].sync_id,
  2379. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  2380. CAM_SYNC_COMMON_EVENT_SUCCESS);
  2381. if (rc)
  2382. CAM_ERR(CAM_ISP,
  2383. "Sync: %d for req: %llu failed with rc: %d, ctx: %u link: 0x%x",
  2384. curr_active_isp_req->fence_map_out[j].sync_id,
  2385. curr_active_req->request_id, rc,
  2386. ctx->ctx_id, ctx->link_hdl);
  2387. curr_active_isp_req->fence_map_out[j].sync_id = -1;
  2388. }
  2389. curr_active_isp_req->num_acked++;
  2390. break;
  2391. }
  2392. }
  2393. }
  2394. static int __cam_isp_ctx_check_deferred_buf_done(
  2395. struct cam_isp_context *ctx_isp,
  2396. struct cam_isp_hw_done_event_data *done,
  2397. uint32_t bubble_state)
  2398. {
  2399. int rc = 0;
  2400. uint32_t curr_num_deferred = 0;
  2401. struct cam_ctx_request *req;
  2402. struct cam_context *ctx = ctx_isp->base;
  2403. struct cam_isp_ctx_req *req_isp;
  2404. bool req_in_pending_wait_list = false;
  2405. if (!list_empty(&ctx->wait_req_list)) {
  2406. req = list_first_entry(&ctx->wait_req_list,
  2407. struct cam_ctx_request, list);
  2408. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2409. curr_num_deferred = req_isp->num_deferred_acks;
  2410. req_in_pending_wait_list = true;
  2411. if (ctx_isp->last_applied_req_id !=
  2412. ctx_isp->last_bufdone_err_apply_req_id) {
  2413. CAM_DBG(CAM_ISP,
  2414. "Trying to find buf done with req in wait list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2415. req->request_id, ctx_isp->last_applied_req_id,
  2416. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2417. ctx->ctx_id, ctx->link_hdl);
  2418. ctx_isp->last_bufdone_err_apply_req_id =
  2419. ctx_isp->last_applied_req_id;
  2420. }
  2421. /*
  2422. * Verify consumed address for this request to make sure
  2423. * we are handling the buf_done for the correct
  2424. * buffer. Also defer actual buf_done handling, i.e
  2425. * do not signal the fence as this request may go into
  2426. * Bubble state eventully.
  2427. */
  2428. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2429. ctx_isp, req, done, bubble_state, true, true);
  2430. /* Check for active req if any deferred is processed */
  2431. if (req_isp->num_deferred_acks > curr_num_deferred)
  2432. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2433. curr_num_deferred, ctx_isp, req);
  2434. } else if (!list_empty(&ctx->pending_req_list)) {
  2435. /*
  2436. * We saw the case that the hw config is blocked due to
  2437. * some reason, the we get the reg upd and buf done before
  2438. * the req is added to wait req list.
  2439. */
  2440. req = list_first_entry(&ctx->pending_req_list,
  2441. struct cam_ctx_request, list);
  2442. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2443. curr_num_deferred = req_isp->num_deferred_acks;
  2444. req_in_pending_wait_list = true;
  2445. if (ctx_isp->last_applied_req_id !=
  2446. ctx_isp->last_bufdone_err_apply_req_id) {
  2447. CAM_DBG(CAM_ISP,
  2448. "Trying to find buf done with req in pending list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u, ctx: %u link: 0x%x",
  2449. req->request_id, ctx_isp->last_applied_req_id,
  2450. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred,
  2451. ctx->ctx_id, ctx->link_hdl);
  2452. ctx_isp->last_bufdone_err_apply_req_id =
  2453. ctx_isp->last_applied_req_id;
  2454. }
  2455. /*
  2456. * Verify consumed address for this request to make sure
  2457. * we are handling the buf_done for the correct
  2458. * buffer. Also defer actual buf_done handling, i.e
  2459. * do not signal the fence as this request may go into
  2460. * Bubble state eventully.
  2461. */
  2462. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2463. ctx_isp, req, done, bubble_state, true, true);
  2464. /* Check for active req if any deferred is processed */
  2465. if (req_isp->num_deferred_acks > curr_num_deferred)
  2466. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2467. curr_num_deferred, ctx_isp, req);
  2468. }
  2469. if (!req_in_pending_wait_list && (ctx_isp->last_applied_req_id !=
  2470. ctx_isp->last_bufdone_err_apply_req_id)) {
  2471. CAM_DBG(CAM_ISP,
  2472. "Bufdone without active request bubble_state=%d last_applied_req_id:%lld,ctx:%u link:0x%x",
  2473. bubble_state, ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2474. ctx_isp->last_bufdone_err_apply_req_id =
  2475. ctx_isp->last_applied_req_id;
  2476. }
  2477. return rc;
  2478. }
  2479. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  2480. struct cam_isp_context *ctx_isp,
  2481. struct cam_isp_hw_done_event_data *done,
  2482. uint32_t bubble_state)
  2483. {
  2484. int rc = 0;
  2485. bool irq_delay_detected = false;
  2486. struct cam_ctx_request *req;
  2487. struct cam_ctx_request *next_req = NULL;
  2488. struct cam_context *ctx = ctx_isp->base;
  2489. if (list_empty(&ctx->active_req_list)) {
  2490. return __cam_isp_ctx_check_deferred_buf_done(
  2491. ctx_isp, done, bubble_state);
  2492. }
  2493. req = list_first_entry(&ctx->active_req_list,
  2494. struct cam_ctx_request, list);
  2495. if (ctx_isp->active_req_cnt > 1) {
  2496. next_req = list_last_entry(
  2497. &ctx->active_req_list,
  2498. struct cam_ctx_request, list);
  2499. if (next_req->request_id != req->request_id)
  2500. __cam_isp_ctx_buf_done_match_req(next_req, done,
  2501. &irq_delay_detected);
  2502. else
  2503. CAM_WARN(CAM_ISP,
  2504. "Req %lld only active request, spurious buf_done rxd, ctx: %u link: 0x%x",
  2505. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2506. }
  2507. /*
  2508. * If irq delay isn't detected, then we need to verify
  2509. * the consumed address for current req, otherwise, we
  2510. * can't verify the consumed address.
  2511. */
  2512. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2513. ctx_isp, req, done, bubble_state,
  2514. !irq_delay_detected, false);
  2515. /*
  2516. * Verify the consumed address for next req all the time,
  2517. * since the reported buf done event may belong to current
  2518. * req, then we can't signal this event for next req.
  2519. */
  2520. if (!rc && irq_delay_detected)
  2521. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2522. ctx_isp, next_req, done,
  2523. bubble_state, true, false);
  2524. return rc;
  2525. }
  2526. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  2527. struct cam_isp_context *ctx_isp,
  2528. struct cam_isp_hw_done_event_data *done,
  2529. uint32_t bubble_state)
  2530. {
  2531. int rc = 0;
  2532. if (ctx_isp->support_consumed_addr)
  2533. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  2534. ctx_isp, done, bubble_state);
  2535. else
  2536. rc = __cam_isp_ctx_handle_buf_done(
  2537. ctx_isp, done, bubble_state);
  2538. return rc;
  2539. }
  2540. static int __cam_isp_ctx_apply_pending_req(
  2541. void *priv, void *data)
  2542. {
  2543. int rc = 0;
  2544. int64_t prev_applied_req;
  2545. struct cam_context *ctx = NULL;
  2546. struct cam_isp_context *ctx_isp = priv;
  2547. struct cam_ctx_request *req;
  2548. struct cam_isp_ctx_req *req_isp;
  2549. struct cam_hw_config_args cfg = {0};
  2550. if (!ctx_isp) {
  2551. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  2552. rc = -EINVAL;
  2553. goto end;
  2554. }
  2555. ctx = ctx_isp->base;
  2556. if (list_empty(&ctx->pending_req_list)) {
  2557. CAM_DBG(CAM_ISP,
  2558. "No pending requests to apply, ctx_idx: %u, link: 0x%x",
  2559. ctx->ctx_id, ctx->link_hdl);
  2560. rc = -EFAULT;
  2561. goto end;
  2562. }
  2563. if (ctx_isp->vfps_aux_context) {
  2564. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED)
  2565. goto end;
  2566. if (ctx_isp->active_req_cnt >= 1)
  2567. goto end;
  2568. } else {
  2569. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  2570. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  2571. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  2572. goto end;
  2573. if (ctx_isp->active_req_cnt >= 2)
  2574. goto end;
  2575. }
  2576. spin_lock_bh(&ctx->lock);
  2577. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2578. list);
  2579. spin_unlock_bh(&ctx->lock);
  2580. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx_idx: %u, link: 0x%x",
  2581. req->request_id, ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  2582. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2583. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2584. cfg.request_id = req->request_id;
  2585. cfg.hw_update_entries = req_isp->cfg;
  2586. cfg.num_hw_update_entries = req_isp->num_cfg;
  2587. cfg.priv = &req_isp->hw_update_data;
  2588. /*
  2589. * Offline mode may receive the SOF and REG_UPD earlier than
  2590. * CDM processing return back, so we set the substate before
  2591. * apply setting.
  2592. */
  2593. spin_lock_bh(&ctx->lock);
  2594. atomic_set(&ctx_isp->rxd_epoch, 0);
  2595. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  2596. prev_applied_req = ctx_isp->last_applied_req_id;
  2597. ctx_isp->last_applied_req_id = req->request_id;
  2598. atomic_set(&ctx_isp->apply_in_progress, 1);
  2599. list_del_init(&req->list);
  2600. list_add_tail(&req->list, &ctx->wait_req_list);
  2601. spin_unlock_bh(&ctx->lock);
  2602. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2603. if (rc) {
  2604. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2605. "Can not apply the configuration,ctx: %u,link: 0x%x",
  2606. ctx->ctx_id, ctx->link_hdl);
  2607. spin_lock_bh(&ctx->lock);
  2608. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2609. ctx_isp->last_applied_req_id = prev_applied_req;
  2610. atomic_set(&ctx_isp->apply_in_progress, 0);
  2611. list_del_init(&req->list);
  2612. list_add(&req->list, &ctx->pending_req_list);
  2613. spin_unlock_bh(&ctx->lock);
  2614. } else {
  2615. atomic_set(&ctx_isp->apply_in_progress, 0);
  2616. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld, ctx: %u, link: 0x%x",
  2617. CAM_ISP_CTX_ACTIVATED_APPLIED,
  2618. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  2619. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2620. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2621. req->request_id);
  2622. }
  2623. end:
  2624. return rc;
  2625. }
  2626. static int __cam_isp_ctx_schedule_apply_req(
  2627. struct cam_isp_context *ctx_isp)
  2628. {
  2629. int rc = 0;
  2630. struct crm_workq_task *task;
  2631. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  2632. if (!task) {
  2633. CAM_ERR(CAM_ISP, "No task for worker");
  2634. return -ENOMEM;
  2635. }
  2636. task->process_cb = __cam_isp_ctx_apply_pending_req;
  2637. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  2638. if (rc)
  2639. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  2640. return rc;
  2641. }
  2642. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  2643. struct cam_isp_context *ctx_isp, void *evt_data)
  2644. {
  2645. struct cam_context *ctx = ctx_isp->base;
  2646. struct cam_ctx_request *req, *req_temp;
  2647. uint64_t request_id = 0;
  2648. atomic_set(&ctx_isp->rxd_epoch, 1);
  2649. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u link: 0x%x", ctx_isp->frame_id,
  2650. ctx->ctx_id, ctx->link_hdl);
  2651. /*
  2652. * For offline it is not possible for epoch to be generated without
  2653. * RUP done. IRQ scheduling delays can possibly cause this.
  2654. */
  2655. if (list_empty(&ctx->active_req_list)) {
  2656. CAM_WARN(CAM_ISP,
  2657. "Active list empty on ctx:%u link:0x%x - EPOCH serviced before RUP",
  2658. ctx->ctx_id, ctx->link_hdl);
  2659. } else {
  2660. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  2661. if (req->request_id > ctx_isp->reported_req_id) {
  2662. request_id = req->request_id;
  2663. ctx_isp->reported_req_id = request_id;
  2664. break;
  2665. }
  2666. }
  2667. }
  2668. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  2669. /*
  2670. * If no valid request, wait for RUP shutter posted after buf done
  2671. */
  2672. if (request_id)
  2673. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2674. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2675. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2676. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2677. request_id);
  2678. return 0;
  2679. }
  2680. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  2681. struct cam_isp_context *ctx_isp, void *evt_data)
  2682. {
  2683. if (ctx_isp->frame_id == 1)
  2684. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  2685. __cam_isp_ctx_substate_val_to_type(
  2686. ctx_isp->substate_activated));
  2687. else
  2688. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2689. "ctx:%u Unexpected regupdate in activated Substate[%s] for frame_id:%lld",
  2690. ctx_isp->base->ctx_id,
  2691. __cam_isp_ctx_substate_val_to_type(
  2692. ctx_isp->substate_activated),
  2693. ctx_isp->frame_id);
  2694. return 0;
  2695. }
  2696. static int __cam_isp_ctx_reg_upd_in_applied_state(
  2697. struct cam_isp_context *ctx_isp, void *evt_data)
  2698. {
  2699. int rc = 0;
  2700. struct cam_ctx_request *req;
  2701. struct cam_context *ctx = ctx_isp->base;
  2702. struct cam_isp_ctx_req *req_isp;
  2703. uint64_t request_id = 0;
  2704. if (list_empty(&ctx->wait_req_list)) {
  2705. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  2706. ctx->ctx_id, ctx->link_hdl);
  2707. goto end;
  2708. }
  2709. req = list_first_entry(&ctx->wait_req_list,
  2710. struct cam_ctx_request, list);
  2711. list_del_init(&req->list);
  2712. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2713. if (req_isp->num_fence_map_out != 0) {
  2714. list_add_tail(&req->list, &ctx->active_req_list);
  2715. ctx_isp->active_req_cnt++;
  2716. request_id = req->request_id;
  2717. CAM_DBG(CAM_REQ,
  2718. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  2719. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2720. __cam_isp_ctx_update_event_record(ctx_isp,
  2721. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  2722. } else {
  2723. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2724. /* no io config, so the request is completed. */
  2725. list_add_tail(&req->list, &ctx->free_req_list);
  2726. CAM_DBG(CAM_ISP,
  2727. "move active request %lld to free list(cnt = %d), ctx %u, link: 0x%x",
  2728. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2729. }
  2730. /*
  2731. * This function only called directly from applied and bubble applied
  2732. * state so change substate here.
  2733. */
  2734. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2735. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u, link: 0x%x",
  2736. __cam_isp_ctx_substate_val_to_type(
  2737. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  2738. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2739. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  2740. end:
  2741. return rc;
  2742. }
  2743. static int __cam_isp_ctx_notify_sof_in_activated_state(
  2744. struct cam_isp_context *ctx_isp, void *evt_data)
  2745. {
  2746. int rc = 0;
  2747. uint64_t request_id = 0;
  2748. struct cam_context *ctx = ctx_isp->base;
  2749. struct cam_ctx_request *req;
  2750. struct cam_isp_ctx_req *req_isp;
  2751. struct cam_hw_cmd_args hw_cmd_args;
  2752. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2753. uint64_t last_cdm_done_req = 0;
  2754. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2755. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2756. if (!evt_data) {
  2757. CAM_ERR(CAM_ISP, "invalid event data");
  2758. return -EINVAL;
  2759. }
  2760. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2761. if (atomic_read(&ctx_isp->process_bubble)) {
  2762. if (list_empty(&ctx->active_req_list)) {
  2763. CAM_ERR(CAM_ISP,
  2764. "No available active req in bubble, ctx %u, link: 0x%x",
  2765. ctx->ctx_id, ctx->link_hdl);
  2766. atomic_set(&ctx_isp->process_bubble, 0);
  2767. ctx_isp->bubble_frame_cnt = 0;
  2768. rc = -EINVAL;
  2769. return rc;
  2770. }
  2771. if (ctx_isp->last_sof_timestamp ==
  2772. ctx_isp->sof_timestamp_val) {
  2773. CAM_DBG(CAM_ISP,
  2774. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld, ctx %u, link: 0x%x",
  2775. ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  2776. goto notify_only;
  2777. }
  2778. req = list_first_entry(&ctx->active_req_list,
  2779. struct cam_ctx_request, list);
  2780. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2781. if (ctx_isp->bubble_frame_cnt >= 1 &&
  2782. req_isp->bubble_detected) {
  2783. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2784. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2785. isp_hw_cmd_args.cmd_type =
  2786. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  2787. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2788. rc = ctx->hw_mgr_intf->hw_cmd(
  2789. ctx->hw_mgr_intf->hw_mgr_priv,
  2790. &hw_cmd_args);
  2791. if (rc) {
  2792. CAM_ERR(CAM_ISP, "HW command failed, ctx %u, link: 0x%x",
  2793. ctx->ctx_id, ctx->link_hdl);
  2794. return rc;
  2795. }
  2796. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  2797. CAM_DBG(CAM_ISP, "last_cdm_done req: %d, ctx %u, link: 0x%x",
  2798. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  2799. if (last_cdm_done_req >= req->request_id) {
  2800. CAM_DBG(CAM_ISP,
  2801. "invalid sof event data CDM cb detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx %u, link: 0x%x",
  2802. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2803. ctx_isp->bubble_frame_cnt = 0;
  2804. } else {
  2805. CAM_DBG(CAM_ISP,
  2806. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx %u, link: 0x%x",
  2807. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2808. req_isp->num_acked = 0;
  2809. req_isp->num_deferred_acks = 0;
  2810. ctx_isp->bubble_frame_cnt = 0;
  2811. req_isp->bubble_detected = false;
  2812. req_isp->cdm_reset_before_apply = true;
  2813. list_del_init(&req->list);
  2814. list_add(&req->list, &ctx->pending_req_list);
  2815. atomic_set(&ctx_isp->process_bubble, 0);
  2816. ctx_isp->active_req_cnt--;
  2817. CAM_DBG(CAM_REQ,
  2818. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply], ctx %u link: 0x%x",
  2819. req->request_id,
  2820. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  2821. }
  2822. } else if (req_isp->bubble_detected) {
  2823. ctx_isp->bubble_frame_cnt++;
  2824. CAM_DBG(CAM_ISP,
  2825. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld, ctx %u link: 0x%x",
  2826. req->request_id,
  2827. ctx_isp->bubble_frame_cnt, ctx->ctx_id, ctx->link_hdl);
  2828. } else {
  2829. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld, ctx %u link: 0x%x",
  2830. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2831. }
  2832. }
  2833. notify_only:
  2834. /*
  2835. * notify reqmgr with sof signal. Note, due to scheduling delay
  2836. * we can run into situation that two active requests has already
  2837. * be in the active queue while we try to do the notification.
  2838. * In this case, we need to skip the current notification. This
  2839. * helps the state machine to catch up the delay.
  2840. */
  2841. if (ctx_isp->active_req_cnt <= 2) {
  2842. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2843. list_for_each_entry(req, &ctx->active_req_list, list) {
  2844. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2845. if ((!req_isp->bubble_detected) &&
  2846. (req->request_id > ctx_isp->reported_req_id)) {
  2847. request_id = req->request_id;
  2848. __cam_isp_ctx_update_event_record(ctx_isp,
  2849. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  2850. break;
  2851. }
  2852. }
  2853. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  2854. request_id = 0;
  2855. if (request_id != 0)
  2856. ctx_isp->reported_req_id = request_id;
  2857. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2858. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2859. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2860. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2861. request_id);
  2862. }
  2863. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  2864. return 0;
  2865. }
  2866. static int __cam_isp_ctx_notify_eof_in_activated_state(
  2867. struct cam_isp_context *ctx_isp, void *evt_data)
  2868. {
  2869. int rc = 0;
  2870. struct cam_context *ctx = ctx_isp->base;
  2871. uint64_t last_cdm_done_req = 0;
  2872. /* update last cdm done timestamp */
  2873. rc = __cam_isp_ctx_get_cdm_done_timestamp(ctx, &last_cdm_done_req);
  2874. if (rc)
  2875. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x Failed to get timestamp from HW",
  2876. ctx->ctx_id, ctx->link_hdl);
  2877. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2878. CAM_ISP_STATE_CHANGE_TRIGGER_CDM_DONE, last_cdm_done_req);
  2879. /* notify reqmgr with eof signal */
  2880. rc = __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_EOF, ctx_isp);
  2881. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2882. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  2883. return rc;
  2884. }
  2885. static int __cam_isp_ctx_reg_upd_in_hw_error(
  2886. struct cam_isp_context *ctx_isp, void *evt_data)
  2887. {
  2888. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2889. return 0;
  2890. }
  2891. static int __cam_isp_ctx_sof_in_activated_state(
  2892. struct cam_isp_context *ctx_isp, void *evt_data)
  2893. {
  2894. int rc = 0;
  2895. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2896. struct cam_ctx_request *req = NULL;
  2897. struct cam_context *ctx = ctx_isp->base;
  2898. uint64_t request_id = 0;
  2899. ctx_isp->last_sof_jiffies = jiffies;
  2900. /* First check if there is a valid request in active list */
  2901. list_for_each_entry(req, &ctx->active_req_list, list) {
  2902. if (req->request_id > ctx_isp->reported_req_id) {
  2903. request_id = req->request_id;
  2904. break;
  2905. }
  2906. }
  2907. /*
  2908. * If nothing in active list, current request might have not moved
  2909. * from wait to active list. This could happen if REG_UPDATE to sw
  2910. * is coming immediately after SOF
  2911. */
  2912. if (request_id == 0) {
  2913. req = list_first_entry(&ctx->wait_req_list,
  2914. struct cam_ctx_request, list);
  2915. if (req)
  2916. request_id = req->request_id;
  2917. }
  2918. if (!evt_data) {
  2919. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  2920. ctx->ctx_id, ctx->link_hdl);
  2921. return -EINVAL;
  2922. }
  2923. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  2924. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2925. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2926. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u request %llu, link: 0x%x",
  2927. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, request_id,
  2928. ctx->link_hdl);
  2929. return rc;
  2930. }
  2931. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2932. void *evt_data)
  2933. {
  2934. int rc = 0;
  2935. struct cam_ctx_request *req = NULL;
  2936. struct cam_isp_ctx_req *req_isp;
  2937. struct cam_context *ctx = ctx_isp->base;
  2938. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2939. CAM_DBG(CAM_ISP, "invalid RUP");
  2940. goto end;
  2941. }
  2942. /*
  2943. * This is for the first update. The initial setting will
  2944. * cause the reg_upd in the first frame.
  2945. */
  2946. if (!list_empty(&ctx->wait_req_list)) {
  2947. req = list_first_entry(&ctx->wait_req_list,
  2948. struct cam_ctx_request, list);
  2949. list_del_init(&req->list);
  2950. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  2951. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2952. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2953. list_add_tail(&req->list, &ctx->free_req_list);
  2954. else
  2955. CAM_ERR(CAM_ISP,
  2956. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  2957. ctx->ctx_id, ctx->link_hdl);
  2958. }
  2959. if (req != NULL) {
  2960. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2961. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2962. req->request_id);
  2963. }
  2964. end:
  2965. return rc;
  2966. }
  2967. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  2968. void *evt_data)
  2969. {
  2970. uint64_t request_id = 0;
  2971. uint32_t wait_req_cnt = 0;
  2972. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  2973. struct cam_ctx_request *req;
  2974. struct cam_isp_ctx_req *req_isp;
  2975. struct cam_context *ctx = ctx_isp->base;
  2976. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2977. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2978. if (!evt_data) {
  2979. CAM_ERR(CAM_ISP, "invalid event data");
  2980. return -EINVAL;
  2981. }
  2982. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2983. if (list_empty(&ctx->wait_req_list)) {
  2984. /*
  2985. * If no wait req in epoch, this is an error case.
  2986. * The recovery is to go back to sof state
  2987. */
  2988. CAM_ERR(CAM_ISP, "Ctx:%u link: 0x%x No wait request", ctx->ctx_id, ctx->link_hdl);
  2989. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2990. /* Send SOF event as empty frame*/
  2991. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2992. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2993. __cam_isp_ctx_update_event_record(ctx_isp,
  2994. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  2995. goto end;
  2996. }
  2997. if (ctx_isp->last_applied_jiffies >= ctx_isp->last_sof_jiffies) {
  2998. list_for_each_entry(req, &ctx->wait_req_list, list) {
  2999. wait_req_cnt++;
  3000. }
  3001. /*
  3002. * The previous req is applied after SOF and there is only
  3003. * one applied req, we don't need to report bubble for this case.
  3004. */
  3005. if (wait_req_cnt == 1 && !ctx_isp->is_tfe_shdr) {
  3006. req = list_first_entry(&ctx->wait_req_list,
  3007. struct cam_ctx_request, list);
  3008. request_id = req->request_id;
  3009. CAM_INFO(CAM_ISP,
  3010. "ctx:%d Don't report the bubble for req:%lld",
  3011. ctx->ctx_id, request_id);
  3012. goto end;
  3013. }
  3014. }
  3015. /* Update state prior to notifying CRM */
  3016. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3017. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3018. list);
  3019. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3020. req_isp->bubble_detected = true;
  3021. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3022. req_isp->cdm_reset_before_apply = false;
  3023. atomic_set(&ctx_isp->process_bubble, 1);
  3024. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3025. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3026. if (req_isp->bubble_report) {
  3027. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3028. req->request_id, ctx_isp);
  3029. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  3030. req->request_id, ctx->ctx_id);
  3031. } else {
  3032. req_isp->bubble_report = 0;
  3033. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u, link: 0x%x",
  3034. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3035. if (ctx_isp->active_req_cnt <= 1)
  3036. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3037. }
  3038. /*
  3039. * Always move the request to active list. Let buf done
  3040. * function handles the rest.
  3041. */
  3042. list_del_init(&req->list);
  3043. list_add_tail(&req->list, &ctx->active_req_list);
  3044. ctx_isp->active_req_cnt++;
  3045. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  3046. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3047. /*
  3048. * Handle the deferred buf done after moving
  3049. * the bubble req to active req list.
  3050. */
  3051. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3052. ctx_isp, req);
  3053. /*
  3054. * Update the record before req pointer to
  3055. * other invalid req.
  3056. */
  3057. __cam_isp_ctx_update_event_record(ctx_isp,
  3058. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3059. /*
  3060. * Get the req again from active_req_list in case
  3061. * the active req cnt is 2.
  3062. */
  3063. list_for_each_entry(req, &ctx->active_req_list, list) {
  3064. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3065. if ((!req_isp->bubble_report) &&
  3066. (req->request_id > ctx_isp->reported_req_id)) {
  3067. request_id = req->request_id;
  3068. ctx_isp->reported_req_id = request_id;
  3069. CAM_DBG(CAM_ISP,
  3070. "ctx %u link: 0x%x reported_req_id update to %lld",
  3071. ctx->ctx_id, ctx->link_hdl, ctx_isp->reported_req_id);
  3072. break;
  3073. }
  3074. }
  3075. if ((request_id != 0) && req_isp->bubble_detected)
  3076. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  3077. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3078. sof_event_status);
  3079. cam_req_mgr_debug_delay_detect();
  3080. trace_cam_delay_detect("ISP",
  3081. "bubble epoch_in_applied", req->request_id,
  3082. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  3083. CAM_DEFAULT_VALUE);
  3084. end:
  3085. if (request_id == 0) {
  3086. req = list_last_entry(&ctx->active_req_list,
  3087. struct cam_ctx_request, list);
  3088. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3089. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3090. } else {
  3091. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3092. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  3093. }
  3094. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u link: 0x%x",
  3095. __cam_isp_ctx_substate_val_to_type(
  3096. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3097. return 0;
  3098. }
  3099. static int __cam_isp_ctx_buf_done_in_sof(struct cam_isp_context *ctx_isp,
  3100. void *evt_data)
  3101. {
  3102. int rc = 0;
  3103. struct cam_isp_hw_done_event_data *done =
  3104. (struct cam_isp_hw_done_event_data *) evt_data;
  3105. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3106. return rc;
  3107. }
  3108. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  3109. void *evt_data)
  3110. {
  3111. int rc = 0;
  3112. struct cam_isp_hw_done_event_data *done =
  3113. (struct cam_isp_hw_done_event_data *) evt_data;
  3114. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3115. return rc;
  3116. }
  3117. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  3118. void *evt_data)
  3119. {
  3120. int rc = 0;
  3121. struct cam_context *ctx = ctx_isp->base;
  3122. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3123. struct cam_ctx_request *req;
  3124. if (!evt_data) {
  3125. CAM_ERR(CAM_ISP, "in valid sof event data");
  3126. return -EINVAL;
  3127. }
  3128. ctx_isp->last_sof_jiffies = jiffies;
  3129. if (atomic_read(&ctx_isp->apply_in_progress))
  3130. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF, ctx: %u, link: 0x%x",
  3131. ctx->ctx_id, ctx->link_hdl);
  3132. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3133. if (list_empty(&ctx->active_req_list))
  3134. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3135. else
  3136. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx: %u, link: 0x%x",
  3137. ctx->ctx_id, ctx->link_hdl);
  3138. req = list_last_entry(&ctx->active_req_list,
  3139. struct cam_ctx_request, list);
  3140. if (req)
  3141. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3142. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  3143. req->request_id);
  3144. if (ctx_isp->frame_id == 1)
  3145. CAM_INFO(CAM_ISP,
  3146. "First SOF in EPCR ctx:%u link: 0x%x frame_id:%lld next substate %s",
  3147. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3148. __cam_isp_ctx_substate_val_to_type(
  3149. ctx_isp->substate_activated));
  3150. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%u link: 0x%x frame_id:%lld next substate:%s",
  3151. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  3152. __cam_isp_ctx_substate_val_to_type(
  3153. ctx_isp->substate_activated));
  3154. return rc;
  3155. }
  3156. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3157. void *evt_data)
  3158. {
  3159. int rc = 0;
  3160. struct cam_isp_hw_done_event_data *done =
  3161. (struct cam_isp_hw_done_event_data *) evt_data;
  3162. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3163. return rc;
  3164. }
  3165. static int __cam_isp_ctx_buf_done_in_bubble(
  3166. struct cam_isp_context *ctx_isp, void *evt_data)
  3167. {
  3168. int rc = 0;
  3169. struct cam_isp_hw_done_event_data *done =
  3170. (struct cam_isp_hw_done_event_data *) evt_data;
  3171. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3172. return rc;
  3173. }
  3174. static int __cam_isp_ctx_epoch_in_bubble_applied(
  3175. struct cam_isp_context *ctx_isp, void *evt_data)
  3176. {
  3177. uint64_t request_id = 0;
  3178. struct cam_ctx_request *req;
  3179. struct cam_isp_ctx_req *req_isp;
  3180. struct cam_context *ctx = ctx_isp->base;
  3181. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  3182. (struct cam_isp_hw_epoch_event_data *)evt_data;
  3183. if (!evt_data) {
  3184. CAM_ERR(CAM_ISP, "invalid event data, ctx_idx: %u, link: 0x%x",
  3185. ctx->ctx_id, ctx->link_hdl);
  3186. return -EINVAL;
  3187. }
  3188. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  3189. /*
  3190. * This means we missed the reg upd ack. So we need to
  3191. * transition to BUBBLE state again.
  3192. */
  3193. if (list_empty(&ctx->wait_req_list)) {
  3194. /*
  3195. * If no pending req in epoch, this is an error case.
  3196. * Just go back to the bubble state.
  3197. */
  3198. CAM_ERR(CAM_ISP, "ctx:%u link: 0x%x No pending request.",
  3199. ctx->ctx_id, ctx->link_hdl);
  3200. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3201. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3202. __cam_isp_ctx_update_event_record(ctx_isp,
  3203. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3204. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3205. goto end;
  3206. }
  3207. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3208. list);
  3209. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3210. req_isp->bubble_detected = true;
  3211. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  3212. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  3213. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3214. req_isp->cdm_reset_before_apply = false;
  3215. if (req_isp->bubble_report) {
  3216. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3217. req->request_id, ctx_isp);
  3218. atomic_set(&ctx_isp->process_bubble, 1);
  3219. } else {
  3220. req_isp->bubble_report = 0;
  3221. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u link: 0x%x",
  3222. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3223. if (ctx_isp->active_req_cnt <= 1)
  3224. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3225. atomic_set(&ctx_isp->process_bubble, 1);
  3226. }
  3227. /*
  3228. * Always move the request to active list. Let buf done
  3229. * function handles the rest.
  3230. */
  3231. list_del_init(&req->list);
  3232. list_add_tail(&req->list, &ctx->active_req_list);
  3233. ctx_isp->active_req_cnt++;
  3234. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u, link: 0x%x",
  3235. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3236. /*
  3237. * Handle the deferred buf done after moving
  3238. * the bubble req to active req list.
  3239. */
  3240. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  3241. ctx_isp, req);
  3242. if (!req_isp->bubble_detected) {
  3243. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  3244. list);
  3245. req_isp->bubble_detected = true;
  3246. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3247. req_isp->cdm_reset_before_apply = false;
  3248. atomic_set(&ctx_isp->process_bubble, 1);
  3249. list_del_init(&req->list);
  3250. list_add_tail(&req->list, &ctx->active_req_list);
  3251. ctx_isp->active_req_cnt++;
  3252. }
  3253. if (!req_isp->bubble_report) {
  3254. if (req->request_id > ctx_isp->reported_req_id) {
  3255. request_id = req->request_id;
  3256. ctx_isp->reported_req_id = request_id;
  3257. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3258. CAM_REQ_MGR_SOF_EVENT_ERROR);
  3259. __cam_isp_ctx_update_event_record(ctx_isp,
  3260. CAM_ISP_CTX_EVENT_EPOCH, req, NULL);
  3261. } else {
  3262. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3263. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3264. __cam_isp_ctx_update_event_record(ctx_isp,
  3265. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3266. }
  3267. } else {
  3268. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3269. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3270. __cam_isp_ctx_update_event_record(ctx_isp,
  3271. CAM_ISP_CTX_EVENT_EPOCH, NULL, NULL);
  3272. }
  3273. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3274. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3275. __cam_isp_ctx_substate_val_to_type(
  3276. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  3277. cam_req_mgr_debug_delay_detect();
  3278. trace_cam_delay_detect("ISP",
  3279. "bubble epoch_in_bubble_applied",
  3280. req->request_id, ctx->ctx_id, ctx->link_hdl,
  3281. ctx->session_hdl,
  3282. CAM_DEFAULT_VALUE);
  3283. end:
  3284. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  3285. list);
  3286. if (req)
  3287. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3288. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  3289. return 0;
  3290. }
  3291. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  3292. struct cam_isp_context *ctx_isp, void *evt_data)
  3293. {
  3294. int rc = 0;
  3295. struct cam_isp_hw_done_event_data *done =
  3296. (struct cam_isp_hw_done_event_data *) evt_data;
  3297. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  3298. return rc;
  3299. }
  3300. static void __cam_isp_get_notification_evt_params(
  3301. uint32_t hw_error, uint32_t *fence_evt_cause,
  3302. uint32_t *req_mgr_err_code, uint32_t *recovery_type)
  3303. {
  3304. uint32_t err_type, err_code = 0, recovery_type_temp;
  3305. err_type = CAM_SYNC_ISP_EVENT_UNKNOWN;
  3306. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3307. if (hw_error & CAM_ISP_HW_ERROR_OVERFLOW) {
  3308. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3309. err_type = CAM_SYNC_ISP_EVENT_OVERFLOW;
  3310. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3311. }
  3312. if (hw_error & CAM_ISP_HW_ERROR_CSID_OUTPUT_FIFO_OVERFLOW) {
  3313. err_code |= CAM_REQ_MGR_CSID_FIFO_OVERFLOW_ERROR;
  3314. err_type = CAM_SYNC_ISP_EVENT_CSID_OUTPUT_FIFO_OVERFLOW;
  3315. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3316. }
  3317. if (hw_error & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) {
  3318. err_code |= CAM_REQ_MGR_CSID_RECOVERY_OVERFLOW_ERROR;
  3319. err_type = CAM_SYNC_ISP_EVENT_RECOVERY_OVERFLOW;
  3320. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3321. }
  3322. if (hw_error & CAM_ISP_HW_ERROR_P2I_ERROR) {
  3323. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3324. err_type = CAM_SYNC_ISP_EVENT_P2I_ERROR;
  3325. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3326. }
  3327. if (hw_error & CAM_ISP_HW_ERROR_VIOLATION) {
  3328. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3329. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3330. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3331. }
  3332. if (hw_error & CAM_ISP_HW_ERROR_HWPD_VIOLATION) {
  3333. err_code |= CAM_REQ_MGR_ISP_ERR_HWPD_VIOLATION;
  3334. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  3335. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3336. }
  3337. if (hw_error & CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) {
  3338. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3339. err_type = CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  3340. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3341. }
  3342. if (hw_error & CAM_ISP_HW_ERROR_CSID_SENSOR_SWITCH_ERROR) {
  3343. err_code |= CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING;
  3344. err_type = CAM_SYNC_ISP_EVENT_CSID_SENSOR_SWITCH_ERROR;
  3345. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3346. }
  3347. if (hw_error & CAM_ISP_HW_ERROR_CSID_LANE_FIFO_OVERFLOW) {
  3348. err_code |= CAM_REQ_MGR_CSID_LANE_FIFO_OVERFLOW_ERROR;
  3349. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3350. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3351. }
  3352. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_HDR_CORRUPTED) {
  3353. err_code |= CAM_REQ_MGR_CSID_RX_PKT_HDR_CORRUPTION;
  3354. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3355. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3356. }
  3357. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_PKT_HDR_DATA) {
  3358. err_code |= CAM_REQ_MGR_CSID_MISSING_PKT_HDR_DATA;
  3359. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3360. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3361. }
  3362. if (hw_error & CAM_ISP_HW_ERROR_CSID_UNBOUNDED_FRAME) {
  3363. err_code |= CAM_REQ_MGR_CSID_UNBOUNDED_FRAME;
  3364. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3365. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3366. }
  3367. if (hw_error & CAM_ISP_HW_ERROR_CSID_FRAME_SIZE) {
  3368. err_code |= CAM_REQ_MGR_CSID_PIXEL_COUNT_MISMATCH;
  3369. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3370. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  3371. }
  3372. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_EOT) {
  3373. err_code |= CAM_REQ_MGR_CSID_MISSING_EOT;
  3374. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3375. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3376. }
  3377. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_PAYLOAD_CORRUPTED) {
  3378. err_code |= CAM_REQ_MGR_CSID_RX_PKT_PAYLOAD_CORRUPTION;
  3379. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  3380. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3381. }
  3382. if (recovery_type_temp == (CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY |
  3383. CAM_REQ_MGR_ERROR_TYPE_RECOVERY))
  3384. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  3385. if (!err_code)
  3386. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  3387. *req_mgr_err_code = err_code;
  3388. *fence_evt_cause = err_type;
  3389. *recovery_type = recovery_type_temp;
  3390. }
  3391. static bool __cam_isp_ctx_request_can_reapply(
  3392. struct cam_isp_ctx_req *req_isp)
  3393. {
  3394. int i;
  3395. for (i = 0; i < req_isp->num_fence_map_out; i++)
  3396. if (req_isp->fence_map_out[i].sync_id == -1)
  3397. return false;
  3398. return true;
  3399. }
  3400. static int __cam_isp_ctx_validate_for_req_reapply_util(
  3401. struct cam_isp_context *ctx_isp)
  3402. {
  3403. int rc = 0;
  3404. struct cam_ctx_request *req_temp;
  3405. struct cam_ctx_request *req = NULL;
  3406. struct cam_isp_ctx_req *req_isp = NULL;
  3407. struct cam_context *ctx = ctx_isp->base;
  3408. if (in_task())
  3409. spin_lock_bh(&ctx->lock);
  3410. /* Check for req in active/wait lists */
  3411. if (list_empty(&ctx->active_req_list)) {
  3412. CAM_DBG(CAM_ISP,
  3413. "Active request list empty for ctx: %u on link: 0x%x",
  3414. ctx->ctx_id, ctx->link_hdl);
  3415. if (list_empty(&ctx->wait_req_list)) {
  3416. CAM_WARN(CAM_ISP,
  3417. "No active/wait req for ctx: %u on link: 0x%x start from pending",
  3418. ctx->ctx_id, ctx->link_hdl);
  3419. rc = 0;
  3420. goto end;
  3421. }
  3422. }
  3423. /* Validate if all fences for active requests are not signaled */
  3424. if (!list_empty(&ctx->active_req_list)) {
  3425. list_for_each_entry_safe_reverse(req, req_temp,
  3426. &ctx->active_req_list, list) {
  3427. /*
  3428. * If some fences of the active request are already
  3429. * signaled, we should not do recovery for the buffer
  3430. * and timestamp consistency.
  3431. */
  3432. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3433. if (!__cam_isp_ctx_request_can_reapply(req_isp)) {
  3434. CAM_WARN(CAM_ISP,
  3435. "Req: %llu in ctx:%u on link: 0x%x fence has partially signaled, cannot do recovery",
  3436. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3437. rc = -EINVAL;
  3438. goto end;
  3439. }
  3440. }
  3441. }
  3442. /* Move active requests to pending list */
  3443. if (!list_empty(&ctx->active_req_list)) {
  3444. list_for_each_entry_safe_reverse(req, req_temp,
  3445. &ctx->active_req_list, list) {
  3446. list_del_init(&req->list);
  3447. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3448. ctx_isp->active_req_cnt--;
  3449. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move active req %llu to pending",
  3450. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3451. }
  3452. }
  3453. /* Move wait requests to pending list */
  3454. if (!list_empty(&ctx->wait_req_list)) {
  3455. list_for_each_entry_safe_reverse(req, req_temp, &ctx->wait_req_list, list) {
  3456. list_del_init(&req->list);
  3457. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3458. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move wait req %llu to pending",
  3459. ctx->ctx_id, ctx->link_hdl, req->request_id);
  3460. }
  3461. }
  3462. end:
  3463. if (in_task())
  3464. spin_unlock_bh(&ctx->lock);
  3465. return rc;
  3466. }
  3467. static int __cam_isp_ctx_handle_recovery_req_util(
  3468. struct cam_isp_context *ctx_isp)
  3469. {
  3470. int rc = 0;
  3471. struct cam_context *ctx = ctx_isp->base;
  3472. struct cam_ctx_request *req_to_reapply = NULL;
  3473. if (list_empty(&ctx->pending_req_list)) {
  3474. CAM_WARN(CAM_ISP,
  3475. "No pending request to recover from on ctx: %u", ctx->ctx_id);
  3476. return -EINVAL;
  3477. }
  3478. req_to_reapply = list_first_entry(&ctx->pending_req_list,
  3479. struct cam_ctx_request, list);
  3480. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3481. ctx_isp->recovery_req_id = req_to_reapply->request_id;
  3482. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3483. CAM_INFO(CAM_ISP, "Notify CRM to reapply req:%llu for ctx:%u link:0x%x",
  3484. req_to_reapply->request_id, ctx->ctx_id, ctx->link_hdl);
  3485. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  3486. CRM_KMD_WARN_INTERNAL_RECOVERY, req_to_reapply->request_id,
  3487. ctx_isp);
  3488. if (rc) {
  3489. /* Unable to notify CRM to do reapply back to normal */
  3490. CAM_WARN(CAM_ISP,
  3491. "ctx:%u link:0x%x unable to notify CRM for req %llu",
  3492. ctx->ctx_id, ctx->link_hdl, ctx_isp->recovery_req_id);
  3493. ctx_isp->recovery_req_id = 0;
  3494. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3495. }
  3496. return rc;
  3497. }
  3498. static int __cam_isp_ctx_trigger_error_req_reapply(
  3499. uint32_t err_type, struct cam_isp_context *ctx_isp)
  3500. {
  3501. int rc = 0;
  3502. struct cam_context *ctx = ctx_isp->base;
  3503. if ((err_type & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) &&
  3504. (isp_ctx_debug.disable_internal_recovery_mask &
  3505. CAM_ISP_CTX_DISABLE_RECOVERY_BUS_OVERFLOW))
  3506. return -EINVAL;
  3507. /*
  3508. * For errors that can be recoverable within kmd, we
  3509. * try to do internal hw stop, restart and notify CRM
  3510. * to do reapply with the help of bubble control flow.
  3511. */
  3512. rc = __cam_isp_ctx_validate_for_req_reapply_util(ctx_isp);
  3513. if (rc)
  3514. goto end;
  3515. rc = __cam_isp_ctx_handle_recovery_req_util(ctx_isp);
  3516. if (rc)
  3517. goto end;
  3518. CAM_DBG(CAM_ISP, "Triggered internal recovery for req:%llu ctx:%u on link 0x%x",
  3519. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3520. end:
  3521. return rc;
  3522. }
  3523. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  3524. void *evt_data)
  3525. {
  3526. int rc = 0;
  3527. enum cam_req_mgr_device_error error;
  3528. uint32_t i = 0;
  3529. bool found = 0;
  3530. struct cam_ctx_request *req = NULL;
  3531. struct cam_ctx_request *req_to_report = NULL;
  3532. struct cam_ctx_request *req_to_dump = NULL;
  3533. struct cam_ctx_request *req_temp;
  3534. struct cam_isp_ctx_req *req_isp = NULL;
  3535. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  3536. uint64_t error_request_id;
  3537. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  3538. uint32_t recovery_type, fence_evt_cause;
  3539. uint32_t req_mgr_err_code;
  3540. struct cam_context *ctx = ctx_isp->base;
  3541. struct cam_isp_hw_error_event_data *error_event_data =
  3542. (struct cam_isp_hw_error_event_data *)evt_data;
  3543. CAM_DBG(CAM_ISP, "Enter HW error_type = %d, ctx:%u on link 0x%x",
  3544. error_event_data->error_type, ctx->ctx_id, ctx->link_hdl);
  3545. if (error_event_data->try_internal_recovery) {
  3546. rc = __cam_isp_ctx_trigger_error_req_reapply(error_event_data->error_type, ctx_isp);
  3547. if (!rc)
  3548. goto exit;
  3549. }
  3550. if (!ctx_isp->offline_context)
  3551. __cam_isp_ctx_pause_crm_timer(ctx);
  3552. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3553. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3554. __cam_isp_get_notification_evt_params(error_event_data->error_type,
  3555. &fence_evt_cause, &req_mgr_err_code, &recovery_type);
  3556. /*
  3557. * The error is likely caused by first request on the active list.
  3558. * If active list is empty check wait list (maybe error hit as soon
  3559. * as RUP and we handle error before RUP.
  3560. */
  3561. if (list_empty(&ctx->active_req_list)) {
  3562. CAM_DBG(CAM_ISP,
  3563. "handling error with no active request, ctx:%u on link 0x%x",
  3564. ctx->ctx_id, ctx->link_hdl);
  3565. if (list_empty(&ctx->wait_req_list)) {
  3566. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3567. "Error with no active/wait request, ctx:%u on link 0x%x",
  3568. ctx->ctx_id, ctx->link_hdl);
  3569. goto end;
  3570. } else {
  3571. req_to_dump = list_first_entry(&ctx->wait_req_list,
  3572. struct cam_ctx_request, list);
  3573. }
  3574. } else {
  3575. req_to_dump = list_first_entry(&ctx->active_req_list,
  3576. struct cam_ctx_request, list);
  3577. }
  3578. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  3579. if (error_event_data->enable_req_dump)
  3580. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  3581. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3582. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  3583. list_for_each_entry_safe(req, req_temp,
  3584. &ctx->active_req_list, list) {
  3585. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3586. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3587. if (!req_isp->bubble_report) {
  3588. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx:%u on link 0x%x",
  3589. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3590. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3591. fence_map_out =
  3592. &req_isp->fence_map_out[i];
  3593. if (req_isp->fence_map_out[i].sync_id != -1) {
  3594. CAM_DBG(CAM_ISP,
  3595. "req %llu, Sync fd 0x%x ctx %u, link 0x%x",
  3596. req->request_id,
  3597. req_isp->fence_map_out[i].sync_id,
  3598. ctx->ctx_id, ctx->link_hdl);
  3599. rc = cam_sync_signal(
  3600. fence_map_out->sync_id,
  3601. CAM_SYNC_STATE_SIGNALED_ERROR,
  3602. fence_evt_cause);
  3603. fence_map_out->sync_id = -1;
  3604. }
  3605. }
  3606. list_del_init(&req->list);
  3607. list_add_tail(&req->list, &ctx->free_req_list);
  3608. ctx_isp->active_req_cnt--;
  3609. } else {
  3610. found = 1;
  3611. break;
  3612. }
  3613. }
  3614. if (found)
  3615. goto move_to_pending;
  3616. list_for_each_entry_safe(req, req_temp,
  3617. &ctx->wait_req_list, list) {
  3618. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3619. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3620. if (!req_isp->bubble_report) {
  3621. CAM_ERR(CAM_ISP, "signalled error for req %llu, ctx %u, link 0x%x",
  3622. req->request_id, ctx->ctx_id, ctx->link_hdl);
  3623. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3624. fence_map_out =
  3625. &req_isp->fence_map_out[i];
  3626. if (req_isp->fence_map_out[i].sync_id != -1) {
  3627. CAM_DBG(CAM_ISP,
  3628. "req %llu, Sync fd 0x%x ctx %u link 0x%x",
  3629. req->request_id,
  3630. req_isp->fence_map_out[i].sync_id,
  3631. ctx->ctx_id, ctx->link_hdl);
  3632. rc = cam_sync_signal(
  3633. fence_map_out->sync_id,
  3634. CAM_SYNC_STATE_SIGNALED_ERROR,
  3635. fence_evt_cause);
  3636. fence_map_out->sync_id = -1;
  3637. }
  3638. }
  3639. list_del_init(&req->list);
  3640. list_add_tail(&req->list, &ctx->free_req_list);
  3641. } else {
  3642. found = 1;
  3643. break;
  3644. }
  3645. }
  3646. move_to_pending:
  3647. /*
  3648. * If bubble recovery is enabled on any request we need to move that
  3649. * request and all the subsequent requests to the pending list.
  3650. * Note:
  3651. * We need to traverse the active list in reverse order and add
  3652. * to head of pending list.
  3653. * e.g. pending current state: 10, 11 | active current state: 8, 9
  3654. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  3655. * final state - pending: 8, 9, 10, 11 | active: NULL
  3656. */
  3657. if (found) {
  3658. list_for_each_entry_safe_reverse(req, req_temp,
  3659. &ctx->active_req_list, list) {
  3660. list_del_init(&req->list);
  3661. list_add(&req->list, &ctx->pending_req_list);
  3662. ctx_isp->active_req_cnt--;
  3663. }
  3664. list_for_each_entry_safe_reverse(req, req_temp,
  3665. &ctx->wait_req_list, list) {
  3666. list_del_init(&req->list);
  3667. list_add(&req->list, &ctx->pending_req_list);
  3668. }
  3669. }
  3670. end:
  3671. do {
  3672. if (list_empty(&ctx->pending_req_list)) {
  3673. error_request_id = ctx_isp->last_applied_req_id;
  3674. break;
  3675. }
  3676. req = list_first_entry(&ctx->pending_req_list,
  3677. struct cam_ctx_request, list);
  3678. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3679. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3680. error_request_id = ctx_isp->last_applied_req_id;
  3681. if (req_isp->bubble_report) {
  3682. req_to_report = req;
  3683. req_isp_to_report = req_to_report->req_priv;
  3684. break;
  3685. }
  3686. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3687. if (req_isp->fence_map_out[i].sync_id != -1)
  3688. rc = cam_sync_signal(
  3689. req_isp->fence_map_out[i].sync_id,
  3690. CAM_SYNC_STATE_SIGNALED_ERROR,
  3691. fence_evt_cause);
  3692. req_isp->fence_map_out[i].sync_id = -1;
  3693. }
  3694. list_del_init(&req->list);
  3695. list_add_tail(&req->list, &ctx->free_req_list);
  3696. } while (req->request_id < ctx_isp->last_applied_req_id);
  3697. if (ctx_isp->offline_context)
  3698. goto exit;
  3699. error = CRM_KMD_ERR_FATAL;
  3700. if (req_isp_to_report && req_isp_to_report->bubble_report)
  3701. if (error_event_data->recovery_enabled)
  3702. error = CRM_KMD_ERR_BUBBLE;
  3703. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, error,
  3704. error_request_id, ctx_isp);
  3705. /*
  3706. * Need to send error occurred in KMD
  3707. * This will help UMD to take necessary action
  3708. * and to dump relevant info
  3709. */
  3710. if (error == CRM_KMD_ERR_FATAL)
  3711. __cam_isp_ctx_notify_v4l2_error_event(recovery_type,
  3712. req_mgr_err_code, error_request_id, ctx);
  3713. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  3714. CAM_DBG(CAM_ISP, "Handling error done on ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  3715. exit:
  3716. return rc;
  3717. }
  3718. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  3719. struct cam_isp_context *ctx_isp, void *evt_data)
  3720. {
  3721. int rc = 0;
  3722. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3723. struct cam_ctx_request *req;
  3724. struct cam_context *ctx = ctx_isp->base;
  3725. uint64_t request_id = 0;
  3726. if (!evt_data) {
  3727. CAM_ERR(CAM_ISP, "in valid sof event data, ctx: %u, link: 0x%x",
  3728. ctx->ctx_id, ctx->link_hdl);
  3729. return -EINVAL;
  3730. }
  3731. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3732. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx: %u, link: 0x%x",
  3733. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  3734. if (!(list_empty(&ctx->wait_req_list)))
  3735. goto end;
  3736. if (ctx_isp->active_req_cnt <= 2) {
  3737. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3738. list_for_each_entry(req, &ctx->active_req_list, list) {
  3739. if (req->request_id > ctx_isp->reported_req_id) {
  3740. request_id = req->request_id;
  3741. ctx_isp->reported_req_id = request_id;
  3742. break;
  3743. }
  3744. }
  3745. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3746. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3747. }
  3748. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3749. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  3750. end:
  3751. return rc;
  3752. }
  3753. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  3754. void *evt_data)
  3755. {
  3756. int rc = 0;
  3757. struct cam_isp_hw_done_event_data *done =
  3758. (struct cam_isp_hw_done_event_data *) evt_data;
  3759. struct cam_context *ctx = ctx_isp->base;
  3760. int prev_active_req_cnt = 0;
  3761. int curr_req_id = 0;
  3762. struct cam_ctx_request *req;
  3763. prev_active_req_cnt = ctx_isp->active_req_cnt;
  3764. req = list_first_entry(&ctx->active_req_list,
  3765. struct cam_ctx_request, list);
  3766. if (req)
  3767. curr_req_id = req->request_id;
  3768. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3769. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  3770. if (list_empty(&ctx->wait_req_list) &&
  3771. list_empty(&ctx->active_req_list)) {
  3772. CAM_DBG(CAM_ISP, "No request, move to SOF, ctx_idx: %u, link: 0x%x",
  3773. ctx->ctx_id, ctx->link_hdl);
  3774. ctx_isp->substate_activated =
  3775. CAM_ISP_CTX_ACTIVATED_SOF;
  3776. if (ctx_isp->reported_req_id < curr_req_id) {
  3777. ctx_isp->reported_req_id = curr_req_id;
  3778. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  3779. curr_req_id,
  3780. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3781. }
  3782. }
  3783. }
  3784. return rc;
  3785. }
  3786. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3787. void *evt_data)
  3788. {
  3789. int rc = 0;
  3790. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3791. return rc;
  3792. }
  3793. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  3794. struct cam_isp_context *ctx_isp,
  3795. void *evt_data)
  3796. {
  3797. int rc = 0;
  3798. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3799. return rc;
  3800. }
  3801. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  3802. void *evt_data)
  3803. {
  3804. int rc = 0;
  3805. struct cam_ctx_request *req = NULL;
  3806. struct cam_isp_ctx_req *req_isp;
  3807. struct cam_context *ctx = ctx_isp->base;
  3808. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  3809. CAM_DBG(CAM_ISP, "invalid RUP");
  3810. goto end;
  3811. }
  3812. /*
  3813. * This is for the first update. The initial setting will
  3814. * cause the reg_upd in the first frame.
  3815. */
  3816. if (!list_empty(&ctx->wait_req_list)) {
  3817. req = list_first_entry(&ctx->wait_req_list,
  3818. struct cam_ctx_request, list);
  3819. list_del_init(&req->list);
  3820. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3821. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3822. if (req_isp->num_fence_map_out == req_isp->num_acked)
  3823. list_add_tail(&req->list, &ctx->free_req_list);
  3824. else
  3825. CAM_ERR(CAM_ISP,
  3826. "receive rup in unexpected state, ctx_idx: %u, link: 0x%x",
  3827. ctx->ctx_id, ctx->link_hdl);
  3828. }
  3829. if (req != NULL) {
  3830. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3831. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3832. req->request_id);
  3833. }
  3834. end:
  3835. return rc;
  3836. }
  3837. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  3838. struct cam_isp_context *ctx_isp, void *evt_data)
  3839. {
  3840. int rc = 0;
  3841. struct cam_ctx_request *req = NULL;
  3842. struct cam_context *ctx = ctx_isp->base;
  3843. struct cam_isp_ctx_req *req_isp;
  3844. uint64_t request_id = 0;
  3845. if (list_empty(&ctx->wait_req_list)) {
  3846. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx_idx: %u, link: 0x%x",
  3847. ctx->ctx_id, ctx->link_hdl);
  3848. goto end;
  3849. }
  3850. req = list_first_entry(&ctx->wait_req_list,
  3851. struct cam_ctx_request, list);
  3852. list_del_init(&req->list);
  3853. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3854. if (req_isp->num_fence_map_out != 0) {
  3855. list_add_tail(&req->list, &ctx->active_req_list);
  3856. ctx_isp->active_req_cnt++;
  3857. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx:%u,link:0x%x",
  3858. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  3859. } else {
  3860. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  3861. /* no io config, so the request is completed. */
  3862. list_add_tail(&req->list, &ctx->free_req_list);
  3863. }
  3864. /*
  3865. * This function only called directly from applied and bubble applied
  3866. * state so change substate here.
  3867. */
  3868. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3869. if (req_isp->num_fence_map_out != 1)
  3870. goto end;
  3871. if (ctx_isp->active_req_cnt <= 2) {
  3872. list_for_each_entry(req, &ctx->active_req_list, list) {
  3873. if (req->request_id > ctx_isp->reported_req_id) {
  3874. request_id = req->request_id;
  3875. ctx_isp->reported_req_id = request_id;
  3876. break;
  3877. }
  3878. }
  3879. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3880. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3881. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3882. }
  3883. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  3884. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  3885. ctx->ctx_id, ctx->link_hdl);
  3886. end:
  3887. if (req != NULL && !rc) {
  3888. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3889. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3890. req->request_id);
  3891. }
  3892. return rc;
  3893. }
  3894. static void __cam_isp_ctx_notify_aeb_error_for_sec_event(
  3895. struct cam_isp_context *ctx_isp)
  3896. {
  3897. struct cam_context *ctx = ctx_isp->base;
  3898. if ((++ctx_isp->aeb_error_cnt) <= CAM_ISP_CONTEXT_AEB_ERROR_CNT_MAX) {
  3899. CAM_WARN(CAM_ISP,
  3900. "AEB slave RDI's current request's SOF seen after next req is applied for ctx: %u on link: 0x%x last_applied_req: %llu err_cnt: %u",
  3901. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id, ctx_isp->aeb_error_cnt);
  3902. return;
  3903. }
  3904. CAM_ERR(CAM_ISP,
  3905. "Fatal - AEB slave RDI's current request's SOF seen after next req is applied, EPOCH height need to be re-configured for ctx: %u on link: 0x%x err_cnt: %u",
  3906. ctx->ctx_id, ctx->link_hdl, ctx_isp->aeb_error_cnt);
  3907. /* Pause CRM timer */
  3908. if (!ctx_isp->offline_context)
  3909. __cam_isp_ctx_pause_crm_timer(ctx);
  3910. /* Trigger reg dump */
  3911. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3912. /* Notify CRM on fatal error */
  3913. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_FATAL,
  3914. ctx_isp->last_applied_req_id, ctx_isp);
  3915. /* Notify userland on error */
  3916. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_ERROR_TYPE_RECOVERY,
  3917. CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING, ctx_isp->last_applied_req_id, ctx);
  3918. /* Change state to HALT, stop further processing of HW events */
  3919. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3920. /* Dump AEB debug info */
  3921. __cam_isp_ctx_dump_frame_timing_record(ctx_isp);
  3922. }
  3923. static int __cam_isp_ctx_trigger_internal_recovery(
  3924. bool sync_frame_drop, struct cam_isp_context *ctx_isp)
  3925. {
  3926. int rc = 0;
  3927. bool do_recovery = true;
  3928. struct cam_context *ctx = ctx_isp->base;
  3929. struct cam_ctx_request *req = NULL;
  3930. struct cam_isp_ctx_req *req_isp = NULL;
  3931. if (list_empty(&ctx->wait_req_list)) {
  3932. /*
  3933. * If the wait list is empty, and we encounter a "silent" frame drop
  3934. * then the settings applied on the previous frame, did not reflect
  3935. * at the next frame boundary, it's expected to latch a frame after.
  3936. * No need to recover. If it's an out of sync drop use pending req
  3937. */
  3938. if (sync_frame_drop && !list_empty(&ctx->pending_req_list))
  3939. req = list_first_entry(&ctx->pending_req_list,
  3940. struct cam_ctx_request, list);
  3941. else
  3942. do_recovery = false;
  3943. }
  3944. /* If both wait and pending list have no request to recover on */
  3945. if (!do_recovery) {
  3946. CAM_WARN(CAM_ISP,
  3947. "No request to perform recovery - ctx: %u on link: 0x%x last_applied: %lld last_buf_done: %lld",
  3948. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  3949. ctx_isp->req_info.last_bufdone_req_id);
  3950. goto end;
  3951. }
  3952. if (!req) {
  3953. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  3954. if (req->request_id != ctx_isp->last_applied_req_id)
  3955. CAM_WARN(CAM_ISP,
  3956. "Top of wait list req: %llu does not match with last applied: %llu in ctx: %u on link: 0x%x",
  3957. req->request_id, ctx_isp->last_applied_req_id,
  3958. ctx->ctx_id, ctx->link_hdl);
  3959. }
  3960. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3961. /*
  3962. * Treat this as bubble, after recovery re-start from appropriate sub-state
  3963. * This will block servicing any further apply calls from CRM
  3964. */
  3965. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3966. atomic_set(&ctx_isp->process_bubble, 1);
  3967. ctx_isp->recovery_req_id = req->request_id;
  3968. /* Wait for active request's to finish before issuing recovery */
  3969. if (ctx_isp->active_req_cnt) {
  3970. req_isp->bubble_detected = true;
  3971. CAM_WARN(CAM_ISP,
  3972. "Active req cnt: %u wait for all buf dones before kicking in recovery on req: %lld ctx: %u on link: 0x%x",
  3973. ctx_isp->active_req_cnt, ctx_isp->recovery_req_id,
  3974. ctx->ctx_id, ctx->link_hdl);
  3975. } else {
  3976. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3977. ctx_isp->recovery_req_id, ctx_isp);
  3978. if (rc) {
  3979. /* Unable to do bubble recovery reset back to normal */
  3980. CAM_WARN(CAM_ISP,
  3981. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  3982. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3983. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  3984. goto end;
  3985. }
  3986. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3987. list_del_init(&req->list);
  3988. list_add(&req->list, &ctx->pending_req_list);
  3989. }
  3990. end:
  3991. return rc;
  3992. }
  3993. static int __cam_isp_ctx_handle_secondary_events(
  3994. struct cam_isp_context *ctx_isp, void *evt_data)
  3995. {
  3996. int rc = 0;
  3997. bool recover = false, sync_frame_drop = false;
  3998. struct cam_context *ctx = ctx_isp->base;
  3999. struct cam_isp_hw_secondary_event_data *sec_evt_data =
  4000. (struct cam_isp_hw_secondary_event_data *)evt_data;
  4001. /* Current scheme to handle only for custom AEB */
  4002. if (!ctx_isp->aeb_enabled) {
  4003. CAM_WARN(CAM_ISP,
  4004. "Recovery not supported for non-AEB ctx: %u on link: 0x%x reject sec evt: %u",
  4005. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4006. goto end;
  4007. }
  4008. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4009. CAM_WARN(CAM_ISP,
  4010. "Internal recovery in progress in ctx: %u on link: 0x%x reject sec evt: %u",
  4011. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  4012. goto end;
  4013. }
  4014. /*
  4015. * In case of custom AEB ensure first exposure frame has
  4016. * not moved forward with its settings without second/third
  4017. * expoure frame coming in. Also track for bubble, in case of system
  4018. * delays it's possible for the IFE settings to be not written to
  4019. * HW on a given frame. If these scenarios occurs flag as error,
  4020. * and recover.
  4021. */
  4022. switch (sec_evt_data->evt_type) {
  4023. case CAM_ISP_HW_SEC_EVENT_SOF:
  4024. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4025. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  4026. ctx_isp->last_applied_req_id);
  4027. __cam_isp_ctx_update_frame_timing_record(CAM_ISP_HW_SECONDARY_EVENT, ctx_isp);
  4028. /* Slave RDI's frame starting post IFE EPOCH - Fatal */
  4029. if ((ctx_isp->substate_activated ==
  4030. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4031. (ctx_isp->substate_activated ==
  4032. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  4033. __cam_isp_ctx_notify_aeb_error_for_sec_event(ctx_isp);
  4034. else
  4035. /* Reset error count */
  4036. ctx_isp->aeb_error_cnt = 0;
  4037. break;
  4038. case CAM_ISP_HW_SEC_EVENT_EPOCH:
  4039. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4040. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  4041. ctx_isp->last_applied_req_id);
  4042. ctx_isp->out_of_sync_cnt = 0;
  4043. /*
  4044. * Master RDI frame dropped in CSID, due to programming delay no RUP/AUP
  4045. * On such occasions use CSID CAMIF EPOCH for bubble detection, flag
  4046. * on detection and perform necessary bubble recovery
  4047. */
  4048. if ((ctx_isp->substate_activated ==
  4049. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  4050. (ctx_isp->substate_activated ==
  4051. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED)) {
  4052. recover = true;
  4053. CAM_WARN(CAM_ISP,
  4054. "Programming delay input frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  4055. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  4056. }
  4057. break;
  4058. case CAM_ISP_HW_SEC_EVENT_OUT_OF_SYNC_FRAME_DROP:
  4059. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4060. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  4061. ctx_isp->last_applied_req_id);
  4062. /* Avoid recovery loop if frame is dropped at stream on */
  4063. if (!ctx_isp->frame_id) {
  4064. CAM_ERR(CAM_ISP,
  4065. "Sensor sync [vc mismatch] frame dropped at stream on ctx: %u link: 0x%x frame_id: %u last_applied_req: %lld",
  4066. ctx->ctx_id, ctx->link_hdl,
  4067. ctx_isp->frame_id, ctx_isp->last_applied_req_id);
  4068. rc = -EPERM;
  4069. break;
  4070. }
  4071. if (!(ctx_isp->out_of_sync_cnt++) &&
  4072. (ctx_isp->recovery_req_id == ctx_isp->last_applied_req_id)) {
  4073. CAM_WARN(CAM_ISP,
  4074. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, recovery maybe in progress...",
  4075. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4076. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4077. break;
  4078. }
  4079. recover = true;
  4080. sync_frame_drop = true;
  4081. ctx_isp->out_of_sync_cnt = 0;
  4082. CAM_WARN(CAM_ISP,
  4083. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu last_recovered_req: %llu out_of_sync_cnt: %u, kicking in internal recovery....",
  4084. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  4085. ctx_isp->recovery_req_id, ctx_isp->out_of_sync_cnt);
  4086. break;
  4087. default:
  4088. break;
  4089. }
  4090. if (recover &&
  4091. !(isp_ctx_debug.disable_internal_recovery_mask & CAM_ISP_CTX_DISABLE_RECOVERY_AEB))
  4092. rc = __cam_isp_ctx_trigger_internal_recovery(sync_frame_drop, ctx_isp);
  4093. end:
  4094. return rc;
  4095. }
  4096. static struct cam_isp_ctx_irq_ops
  4097. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4098. /* SOF */
  4099. {
  4100. .irq_ops = {
  4101. __cam_isp_ctx_handle_error,
  4102. __cam_isp_ctx_sof_in_activated_state,
  4103. __cam_isp_ctx_reg_upd_in_sof,
  4104. __cam_isp_ctx_notify_sof_in_activated_state,
  4105. __cam_isp_ctx_notify_eof_in_activated_state,
  4106. __cam_isp_ctx_buf_done_in_sof,
  4107. __cam_isp_ctx_handle_secondary_events,
  4108. },
  4109. },
  4110. /* APPLIED */
  4111. {
  4112. .irq_ops = {
  4113. __cam_isp_ctx_handle_error,
  4114. __cam_isp_ctx_sof_in_activated_state,
  4115. __cam_isp_ctx_reg_upd_in_applied_state,
  4116. __cam_isp_ctx_epoch_in_applied,
  4117. __cam_isp_ctx_notify_eof_in_activated_state,
  4118. __cam_isp_ctx_buf_done_in_applied,
  4119. __cam_isp_ctx_handle_secondary_events,
  4120. },
  4121. },
  4122. /* EPOCH */
  4123. {
  4124. .irq_ops = {
  4125. __cam_isp_ctx_handle_error,
  4126. __cam_isp_ctx_sof_in_epoch,
  4127. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4128. __cam_isp_ctx_notify_sof_in_activated_state,
  4129. __cam_isp_ctx_notify_eof_in_activated_state,
  4130. __cam_isp_ctx_buf_done_in_epoch,
  4131. __cam_isp_ctx_handle_secondary_events,
  4132. },
  4133. },
  4134. /* BUBBLE */
  4135. {
  4136. .irq_ops = {
  4137. __cam_isp_ctx_handle_error,
  4138. __cam_isp_ctx_sof_in_activated_state,
  4139. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4140. __cam_isp_ctx_notify_sof_in_activated_state,
  4141. __cam_isp_ctx_notify_eof_in_activated_state,
  4142. __cam_isp_ctx_buf_done_in_bubble,
  4143. __cam_isp_ctx_handle_secondary_events,
  4144. },
  4145. },
  4146. /* Bubble Applied */
  4147. {
  4148. .irq_ops = {
  4149. __cam_isp_ctx_handle_error,
  4150. __cam_isp_ctx_sof_in_activated_state,
  4151. __cam_isp_ctx_reg_upd_in_applied_state,
  4152. __cam_isp_ctx_epoch_in_bubble_applied,
  4153. NULL,
  4154. __cam_isp_ctx_buf_done_in_bubble_applied,
  4155. __cam_isp_ctx_handle_secondary_events,
  4156. },
  4157. },
  4158. /* HW ERROR */
  4159. {
  4160. .irq_ops = {
  4161. NULL,
  4162. __cam_isp_ctx_sof_in_activated_state,
  4163. __cam_isp_ctx_reg_upd_in_hw_error,
  4164. NULL,
  4165. NULL,
  4166. NULL,
  4167. },
  4168. },
  4169. /* HALT */
  4170. {
  4171. },
  4172. };
  4173. static struct cam_isp_ctx_irq_ops
  4174. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4175. /* SOF */
  4176. {
  4177. .irq_ops = {
  4178. __cam_isp_ctx_handle_error,
  4179. __cam_isp_ctx_fs2_sof_in_sof_state,
  4180. __cam_isp_ctx_fs2_reg_upd_in_sof,
  4181. __cam_isp_ctx_fs2_sof_in_sof_state,
  4182. __cam_isp_ctx_notify_eof_in_activated_state,
  4183. NULL,
  4184. },
  4185. },
  4186. /* APPLIED */
  4187. {
  4188. .irq_ops = {
  4189. __cam_isp_ctx_handle_error,
  4190. __cam_isp_ctx_sof_in_activated_state,
  4191. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  4192. __cam_isp_ctx_epoch_in_applied,
  4193. __cam_isp_ctx_notify_eof_in_activated_state,
  4194. __cam_isp_ctx_fs2_buf_done_in_applied,
  4195. },
  4196. },
  4197. /* EPOCH */
  4198. {
  4199. .irq_ops = {
  4200. __cam_isp_ctx_handle_error,
  4201. __cam_isp_ctx_sof_in_epoch,
  4202. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4203. __cam_isp_ctx_notify_sof_in_activated_state,
  4204. __cam_isp_ctx_notify_eof_in_activated_state,
  4205. __cam_isp_ctx_fs2_buf_done_in_epoch,
  4206. },
  4207. },
  4208. /* BUBBLE */
  4209. {
  4210. .irq_ops = {
  4211. __cam_isp_ctx_handle_error,
  4212. __cam_isp_ctx_sof_in_activated_state,
  4213. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  4214. __cam_isp_ctx_notify_sof_in_activated_state,
  4215. __cam_isp_ctx_notify_eof_in_activated_state,
  4216. __cam_isp_ctx_buf_done_in_bubble,
  4217. },
  4218. },
  4219. /* Bubble Applied */
  4220. {
  4221. .irq_ops = {
  4222. __cam_isp_ctx_handle_error,
  4223. __cam_isp_ctx_sof_in_activated_state,
  4224. __cam_isp_ctx_reg_upd_in_applied_state,
  4225. __cam_isp_ctx_epoch_in_bubble_applied,
  4226. NULL,
  4227. __cam_isp_ctx_buf_done_in_bubble_applied,
  4228. },
  4229. },
  4230. /* HW ERROR */
  4231. {
  4232. .irq_ops = {
  4233. NULL,
  4234. __cam_isp_ctx_sof_in_activated_state,
  4235. __cam_isp_ctx_reg_upd_in_hw_error,
  4236. NULL,
  4237. NULL,
  4238. NULL,
  4239. },
  4240. },
  4241. /* HALT */
  4242. {
  4243. },
  4244. };
  4245. static struct cam_isp_ctx_irq_ops
  4246. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4247. /* SOF */
  4248. {
  4249. .irq_ops = {
  4250. __cam_isp_ctx_handle_error,
  4251. NULL,
  4252. NULL,
  4253. NULL,
  4254. NULL,
  4255. NULL,
  4256. },
  4257. },
  4258. /* APPLIED */
  4259. {
  4260. .irq_ops = {
  4261. __cam_isp_ctx_handle_error,
  4262. __cam_isp_ctx_sof_in_activated_state,
  4263. __cam_isp_ctx_reg_upd_in_applied_state,
  4264. __cam_isp_ctx_offline_epoch_in_activated_state,
  4265. NULL,
  4266. __cam_isp_ctx_buf_done_in_applied,
  4267. },
  4268. },
  4269. /* EPOCH */
  4270. {
  4271. .irq_ops = {
  4272. __cam_isp_ctx_handle_error,
  4273. __cam_isp_ctx_sof_in_activated_state,
  4274. NULL,
  4275. __cam_isp_ctx_offline_epoch_in_activated_state,
  4276. NULL,
  4277. __cam_isp_ctx_buf_done_in_epoch,
  4278. },
  4279. },
  4280. /* BUBBLE */
  4281. {
  4282. },
  4283. /* Bubble Applied */
  4284. {
  4285. },
  4286. /* HW ERROR */
  4287. {
  4288. .irq_ops = {
  4289. NULL,
  4290. __cam_isp_ctx_sof_in_activated_state,
  4291. __cam_isp_ctx_reg_upd_in_hw_error,
  4292. NULL,
  4293. NULL,
  4294. NULL,
  4295. },
  4296. },
  4297. /* HALT */
  4298. {
  4299. },
  4300. };
  4301. static inline int cam_isp_context_apply_evt_injection(struct cam_context *ctx)
  4302. {
  4303. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4304. struct cam_hw_inject_evt_param *evt_inject_params = &ctx_isp->evt_inject_params;
  4305. struct cam_common_evt_inject_data inject_evt = {0};
  4306. int rc;
  4307. inject_evt.evt_params = evt_inject_params;
  4308. rc = cam_context_apply_evt_injection(ctx, &inject_evt);
  4309. if (rc)
  4310. CAM_ERR(CAM_ISP, "Fail to apply event injection ctx_id: %u link: 0x%x req_id: %u",
  4311. ctx->ctx_id, ctx->link_hdl, evt_inject_params->req_id);
  4312. evt_inject_params->is_valid = false;
  4313. return rc;
  4314. }
  4315. static inline void __cam_isp_ctx_update_fcg_prediction_idx(
  4316. struct cam_context *ctx,
  4317. uint64_t request_id,
  4318. struct cam_isp_fcg_prediction_tracker *fcg_tracker,
  4319. struct cam_isp_fcg_config_info *fcg_info)
  4320. {
  4321. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4322. if ((fcg_tracker->sum_skipped == 0) ||
  4323. (fcg_tracker->sum_skipped > CAM_ISP_MAX_FCG_PREDICTIONS)) {
  4324. fcg_info->use_current_cfg = true;
  4325. CAM_DBG(CAM_ISP,
  4326. "Apply req: %llu, Use current FCG value, frame_id: %llu, ctx_id: %u",
  4327. request_id, ctx_isp->frame_id, ctx->ctx_id);
  4328. } else {
  4329. fcg_info->prediction_idx = fcg_tracker->sum_skipped;
  4330. CAM_DBG(CAM_ISP,
  4331. "Apply req: %llu, FCG prediction: %u, frame_id: %llu, ctx_id: %u",
  4332. request_id, fcg_tracker->sum_skipped,
  4333. ctx_isp->frame_id, ctx->ctx_id);
  4334. }
  4335. }
  4336. static inline void __cam_isp_ctx_print_fcg_tracker(
  4337. struct cam_isp_fcg_prediction_tracker *fcg_tracker)
  4338. {
  4339. uint32_t skipped_list[CAM_ISP_AFD_PIPELINE_DELAY];
  4340. struct cam_isp_skip_frame_info *skip_info;
  4341. int i = 0;
  4342. list_for_each_entry(skip_info,
  4343. &fcg_tracker->skipped_list, list) {
  4344. skipped_list[i] = skip_info->num_frame_skipped;
  4345. i += 1;
  4346. }
  4347. CAM_DBG(CAM_ISP,
  4348. "FCG tracker num_skipped: %u, sum_skipped: %u, skipped list: [%u, %u, %u]",
  4349. fcg_tracker->num_skipped, fcg_tracker->sum_skipped,
  4350. skipped_list[0], skipped_list[1], skipped_list[2]);
  4351. }
  4352. static int __cam_isp_ctx_apply_req_in_activated_state(
  4353. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  4354. enum cam_isp_ctx_activated_substate next_state)
  4355. {
  4356. int rc = 0;
  4357. struct cam_ctx_request *req;
  4358. struct cam_ctx_request *active_req = NULL;
  4359. struct cam_isp_ctx_req *req_isp;
  4360. struct cam_isp_ctx_req *active_req_isp;
  4361. struct cam_isp_context *ctx_isp = NULL;
  4362. struct cam_hw_config_args cfg = {0};
  4363. struct cam_isp_skip_frame_info *skip_info;
  4364. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  4365. struct cam_isp_fcg_config_info *fcg_info;
  4366. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4367. /* Reset mswitch ref cnt */
  4368. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  4369. ctx_isp->mswitch_default_apply_delay_max_cnt);
  4370. if (apply->re_apply)
  4371. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  4372. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4373. "ctx_id:%u link: 0x%x Trying to reapply the same request %llu again",
  4374. ctx->ctx_id, ctx->link_hdl,
  4375. apply->request_id);
  4376. return 0;
  4377. }
  4378. if (list_empty(&ctx->pending_req_list)) {
  4379. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4380. "ctx_id:%u link: 0x%x No available request for Apply id %lld",
  4381. ctx->ctx_id, ctx->link_hdl,
  4382. apply->request_id);
  4383. rc = -EFAULT;
  4384. goto end;
  4385. }
  4386. /*
  4387. * When the pipeline has issue, the requests can be queued up in the
  4388. * pipeline. In this case, we should reject the additional request.
  4389. * The maximum number of request allowed to be outstanding is 2.
  4390. *
  4391. */
  4392. if (atomic_read(&ctx_isp->process_bubble)) {
  4393. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4394. "ctx_id:%u link: 0x%x Processing bubble cannot apply Request Id %llu",
  4395. ctx->ctx_id, ctx->link_hdl,
  4396. apply->request_id);
  4397. rc = -EFAULT;
  4398. goto end;
  4399. }
  4400. /*
  4401. * When isp processing internal recovery, the crm may still apply
  4402. * req to isp ctx. In this case, we should reject this req apply.
  4403. */
  4404. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  4405. CAM_INFO_RATE_LIMIT(CAM_ISP,
  4406. "ctx_id:%u link: 0x%x Processing recovery cannot apply Request Id %lld",
  4407. ctx->ctx_id, ctx->link_hdl,
  4408. apply->request_id);
  4409. rc = -EAGAIN;
  4410. goto end;
  4411. }
  4412. spin_lock_bh(&ctx->lock);
  4413. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  4414. list);
  4415. spin_unlock_bh(&ctx->lock);
  4416. /*
  4417. * Check whether the request id is matching the tip, if not, this means
  4418. * we are in the middle of the error handling. Need to reject this apply
  4419. */
  4420. if (req->request_id != apply->request_id) {
  4421. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4422. "ctx_id:%u link: 0x%x Invalid Request Id asking %llu existing %llu",
  4423. ctx->ctx_id, ctx->link_hdl,
  4424. apply->request_id, req->request_id);
  4425. rc = -EFAULT;
  4426. goto end;
  4427. }
  4428. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u, link: 0x%x",
  4429. req->request_id,
  4430. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  4431. ctx->ctx_id, ctx->link_hdl);
  4432. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4433. if (ctx_isp->active_req_cnt >= 2) {
  4434. CAM_WARN_RATE_LIMIT(CAM_ISP,
  4435. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u, link: 0x%x",
  4436. req->request_id,
  4437. ctx_isp->active_req_cnt,
  4438. ctx->ctx_id, ctx->link_hdl);
  4439. spin_lock_bh(&ctx->lock);
  4440. if (!list_empty(&ctx->active_req_list))
  4441. active_req = list_first_entry(&ctx->active_req_list,
  4442. struct cam_ctx_request, list);
  4443. else
  4444. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4445. "WARNING: should not happen (cnt = %d) but active_list empty, ctx %u, link: 0x%x",
  4446. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4447. spin_unlock_bh(&ctx->lock);
  4448. if (active_req) {
  4449. active_req_isp =
  4450. (struct cam_isp_ctx_req *) active_req->req_priv;
  4451. __cam_isp_ctx_handle_buf_done_fail_log(ctx_isp,
  4452. active_req->request_id, active_req_isp);
  4453. }
  4454. rc = -EFAULT;
  4455. goto end;
  4456. }
  4457. /* Reset congestion counter */
  4458. ctx_isp->congestion_cnt = 0;
  4459. req_isp->bubble_report = apply->report_if_bubble;
  4460. /*
  4461. * Reset all buf done/bubble flags for the req being applied
  4462. * If internal recovery has led to re-apply of same
  4463. * request, clear all stale entities
  4464. */
  4465. req_isp->num_acked = 0;
  4466. req_isp->num_deferred_acks = 0;
  4467. req_isp->cdm_reset_before_apply = false;
  4468. req_isp->bubble_detected = false;
  4469. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4470. cfg.request_id = req->request_id;
  4471. cfg.hw_update_entries = req_isp->cfg;
  4472. cfg.num_hw_update_entries = req_isp->num_cfg;
  4473. cfg.priv = &req_isp->hw_update_data;
  4474. cfg.init_packet = 0;
  4475. cfg.reapply_type = req_isp->reapply_type;
  4476. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4477. if ((ctx_isp->evt_inject_params.is_valid) &&
  4478. (req->request_id == ctx_isp->evt_inject_params.req_id)) {
  4479. rc = cam_isp_context_apply_evt_injection(ctx_isp->base);
  4480. if (!rc)
  4481. goto end;
  4482. }
  4483. /* Decide the exact FCG prediction */
  4484. fcg_tracker = &ctx_isp->fcg_tracker;
  4485. fcg_info = &req_isp->hw_update_data.fcg_info;
  4486. if (!list_empty(&fcg_tracker->skipped_list)) {
  4487. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4488. skip_info = list_first_entry(&fcg_tracker->skipped_list,
  4489. struct cam_isp_skip_frame_info, list);
  4490. fcg_tracker->sum_skipped -= skip_info->num_frame_skipped;
  4491. if (unlikely((uint32_t)UINT_MAX - fcg_tracker->sum_skipped <
  4492. fcg_tracker->num_skipped))
  4493. fcg_tracker->num_skipped =
  4494. (uint32_t)UINT_MAX - fcg_tracker->sum_skipped;
  4495. fcg_tracker->sum_skipped += fcg_tracker->num_skipped;
  4496. skip_info->num_frame_skipped = fcg_tracker->num_skipped;
  4497. fcg_tracker->num_skipped = 0;
  4498. list_rotate_left(&fcg_tracker->skipped_list);
  4499. __cam_isp_ctx_print_fcg_tracker(fcg_tracker);
  4500. __cam_isp_ctx_update_fcg_prediction_idx(ctx,
  4501. apply->request_id, fcg_tracker, fcg_info);
  4502. }
  4503. atomic_set(&ctx_isp->apply_in_progress, 1);
  4504. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4505. if (!rc) {
  4506. spin_lock_bh(&ctx->lock);
  4507. ctx_isp->substate_activated = next_state;
  4508. ctx_isp->last_applied_req_id = apply->request_id;
  4509. ctx_isp->last_applied_jiffies = jiffies;
  4510. if (ctx_isp->is_tfe_shdr) {
  4511. if (ctx_isp->is_shdr_master && req_isp->hw_update_data.mup_en)
  4512. apply->dual_trigger_status = req_isp->hw_update_data.num_exp;
  4513. else
  4514. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4515. } else {
  4516. apply->dual_trigger_status = CAM_REQ_DUAL_TRIGGER_NONE;
  4517. }
  4518. list_del_init(&req->list);
  4519. if (atomic_read(&ctx_isp->internal_recovery_set))
  4520. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  4521. else
  4522. list_add_tail(&req->list, &ctx->wait_req_list);
  4523. CAM_DBG(CAM_ISP, "new Substate[%s], applied req %lld, ctx_idx: %u, link: 0x%x",
  4524. __cam_isp_ctx_substate_val_to_type(next_state),
  4525. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  4526. spin_unlock_bh(&ctx->lock);
  4527. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4528. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  4529. req->request_id);
  4530. __cam_isp_ctx_update_event_record(ctx_isp,
  4531. CAM_ISP_CTX_EVENT_APPLY, req, NULL);
  4532. } else if (rc == -EALREADY) {
  4533. spin_lock_bh(&ctx->lock);
  4534. req_isp->bubble_detected = true;
  4535. req_isp->cdm_reset_before_apply = false;
  4536. atomic_set(&ctx_isp->process_bubble, 1);
  4537. list_del_init(&req->list);
  4538. list_add(&req->list, &ctx->active_req_list);
  4539. ctx_isp->active_req_cnt++;
  4540. spin_unlock_bh(&ctx->lock);
  4541. CAM_DBG(CAM_REQ,
  4542. "move request %lld to active list(cnt = %d), ctx %u, link: 0x%x",
  4543. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  4544. } else {
  4545. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4546. "ctx_id:%u link: 0x%x,Can not apply (req %lld) the configuration, rc %d",
  4547. ctx->ctx_id, ctx->link_hdl, apply->request_id, rc);
  4548. }
  4549. atomic_set(&ctx_isp->apply_in_progress, 0);
  4550. end:
  4551. return rc;
  4552. }
  4553. static int __cam_isp_ctx_apply_req_in_sof(
  4554. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4555. {
  4556. int rc = 0;
  4557. struct cam_isp_context *ctx_isp =
  4558. (struct cam_isp_context *) ctx->ctx_priv;
  4559. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4560. __cam_isp_ctx_substate_val_to_type(
  4561. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4562. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4563. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4564. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4565. __cam_isp_ctx_substate_val_to_type(
  4566. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4567. if (rc)
  4568. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4569. __cam_isp_ctx_substate_val_to_type(
  4570. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4571. return rc;
  4572. }
  4573. static int __cam_isp_ctx_apply_req_in_epoch(
  4574. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4575. {
  4576. int rc = 0;
  4577. struct cam_isp_context *ctx_isp =
  4578. (struct cam_isp_context *) ctx->ctx_priv;
  4579. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4580. __cam_isp_ctx_substate_val_to_type(
  4581. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4582. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4583. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4584. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4585. __cam_isp_ctx_substate_val_to_type(
  4586. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4587. if (rc)
  4588. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4589. __cam_isp_ctx_substate_val_to_type(
  4590. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4591. return rc;
  4592. }
  4593. static int __cam_isp_ctx_apply_req_in_bubble(
  4594. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4595. {
  4596. int rc = 0;
  4597. struct cam_isp_context *ctx_isp =
  4598. (struct cam_isp_context *) ctx->ctx_priv;
  4599. CAM_DBG(CAM_ISP, "current Substate[%s], ctx %u, link: 0x%x",
  4600. __cam_isp_ctx_substate_val_to_type(
  4601. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4602. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4603. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  4604. CAM_DBG(CAM_ISP, "new Substate[%s], ctx %u, link: 0x%x",
  4605. __cam_isp_ctx_substate_val_to_type(
  4606. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  4607. if (rc)
  4608. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d, ctx %u, link: 0x%x",
  4609. __cam_isp_ctx_substate_val_to_type(
  4610. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  4611. return rc;
  4612. }
  4613. static void __cam_isp_ctx_find_mup_for_default_settings(
  4614. int64_t req_id, struct cam_context *ctx,
  4615. struct cam_ctx_request **switch_req)
  4616. {
  4617. struct cam_ctx_request *req, *temp_req;
  4618. if (list_empty(&ctx->pending_req_list)) {
  4619. CAM_DBG(CAM_ISP,
  4620. "Pending list empty, unable to find mup for req: %lld ctx: %u",
  4621. req_id, ctx->ctx_id);
  4622. return;
  4623. }
  4624. list_for_each_entry_safe(
  4625. req, temp_req, &ctx->pending_req_list, list) {
  4626. if (req->request_id == req_id) {
  4627. struct cam_isp_ctx_req *req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4628. if (req_isp->hw_update_data.mup_en) {
  4629. *switch_req = req;
  4630. CAM_DBG(CAM_ISP,
  4631. "Found mup for last applied max pd req: %lld in ctx: %u",
  4632. req_id, ctx->ctx_id);
  4633. }
  4634. }
  4635. }
  4636. }
  4637. static int __cam_isp_ctx_apply_default_req_settings(
  4638. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4639. {
  4640. int rc = 0;
  4641. bool skip_rup_aup = false;
  4642. struct cam_ctx_request *req = NULL;
  4643. struct cam_isp_ctx_req *req_isp = NULL;
  4644. struct cam_isp_context *isp_ctx =
  4645. (struct cam_isp_context *) ctx->ctx_priv;
  4646. struct cam_hw_cmd_args hw_cmd_args;
  4647. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4648. struct cam_hw_config_args cfg = {0};
  4649. if (isp_ctx->mode_switch_en && isp_ctx->handle_mswitch) {
  4650. if ((apply->last_applied_max_pd_req > 0) &&
  4651. (atomic_dec_and_test(&isp_ctx->mswitch_default_apply_delay_ref_cnt))) {
  4652. __cam_isp_ctx_find_mup_for_default_settings(
  4653. apply->last_applied_max_pd_req, ctx, &req);
  4654. }
  4655. if (req) {
  4656. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4657. CAM_DBG(CAM_ISP,
  4658. "Applying IQ for mode switch req: %lld ctx: %u",
  4659. req->request_id, ctx->ctx_id);
  4660. cfg.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4661. cfg.request_id = req->request_id;
  4662. cfg.hw_update_entries = req_isp->cfg;
  4663. cfg.num_hw_update_entries = req_isp->num_cfg;
  4664. cfg.priv = &req_isp->hw_update_data;
  4665. cfg.init_packet = 0;
  4666. cfg.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  4667. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  4668. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4669. if (rc) {
  4670. CAM_ERR(CAM_ISP,
  4671. "Failed to apply req: %lld IQ settings in ctx: %u",
  4672. req->request_id, ctx->ctx_id);
  4673. goto end;
  4674. }
  4675. skip_rup_aup = true;
  4676. }
  4677. }
  4678. if (isp_ctx->use_default_apply) {
  4679. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4680. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4681. isp_hw_cmd_args.cmd_type =
  4682. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  4683. isp_hw_cmd_args.cmd_data = (void *)&skip_rup_aup;
  4684. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4685. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4686. &hw_cmd_args);
  4687. if (rc)
  4688. CAM_ERR(CAM_ISP,
  4689. "Failed to apply default settings rc %d ctx %u, link: 0x%x",
  4690. rc, ctx->ctx_id, ctx->link_hdl);
  4691. else
  4692. CAM_DBG(CAM_ISP, "Applied default settings rc %d ctx: %u link: 0x%x",
  4693. rc, ctx->ctx_id, ctx->link_hdl);
  4694. }
  4695. end:
  4696. return rc;
  4697. }
  4698. static void *cam_isp_ctx_user_dump_req_list(
  4699. void *dump_struct, uint8_t *addr_ptr)
  4700. {
  4701. struct list_head *head = NULL;
  4702. uint64_t *addr;
  4703. struct cam_ctx_request *req, *req_temp;
  4704. head = (struct list_head *)dump_struct;
  4705. addr = (uint64_t *)addr_ptr;
  4706. if (!list_empty(head)) {
  4707. list_for_each_entry_safe(req, req_temp, head, list) {
  4708. *addr++ = req->request_id;
  4709. }
  4710. }
  4711. return addr;
  4712. }
  4713. static void *cam_isp_ctx_user_dump_active_requests(
  4714. void *dump_struct, uint8_t *addr_ptr)
  4715. {
  4716. uint64_t *addr;
  4717. struct cam_ctx_request *req;
  4718. req = (struct cam_ctx_request *)dump_struct;
  4719. addr = (uint64_t *)addr_ptr;
  4720. *addr++ = req->request_id;
  4721. return addr;
  4722. }
  4723. static int __cam_isp_ctx_dump_req_info(
  4724. struct cam_context *ctx,
  4725. struct cam_ctx_request *req,
  4726. struct cam_common_hw_dump_args *dump_args)
  4727. {
  4728. int i, rc = 0;
  4729. uint32_t min_len;
  4730. size_t remain_len;
  4731. struct cam_isp_ctx_req *req_isp;
  4732. struct cam_ctx_request *req_temp;
  4733. if (!req || !ctx || !dump_args) {
  4734. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK",
  4735. req, ctx, dump_args);
  4736. return -EINVAL;
  4737. }
  4738. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4739. if (dump_args->buf_len <= dump_args->offset) {
  4740. CAM_WARN(CAM_ISP,
  4741. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4742. dump_args->buf_len, dump_args->offset, ctx->ctx_id, ctx->link_hdl);
  4743. return -ENOSPC;
  4744. }
  4745. remain_len = dump_args->buf_len - dump_args->offset;
  4746. min_len = sizeof(struct cam_isp_context_dump_header) +
  4747. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  4748. req_isp->num_fence_map_out *
  4749. sizeof(uint64_t));
  4750. if (remain_len < min_len) {
  4751. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4752. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4753. return -ENOSPC;
  4754. }
  4755. /* Dump pending request list */
  4756. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4757. &ctx->pending_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_PENDING_REQUESTS:");
  4758. if (rc) {
  4759. CAM_ERR(CAM_ISP,
  4760. "CAM_ISP_CONTEXT:Pending request dump failed, rc:%d, ctx:%u, link:0x%x",
  4761. rc, ctx->ctx_id, ctx->link_hdl);
  4762. return rc;
  4763. }
  4764. /* Dump applied request list */
  4765. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4766. &ctx->wait_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_APPLIED_REQUESTS:");
  4767. if (rc) {
  4768. CAM_ERR(CAM_ISP,
  4769. "CAM_ISP_CONTEXT: Applied request dump failed, rc:%d, ctx:%u, link:0x%x",
  4770. rc, ctx->ctx_id, ctx->link_hdl);
  4771. return rc;
  4772. }
  4773. /* Dump active request list */
  4774. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4775. &ctx->active_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_ACTIVE_REQUESTS:");
  4776. if (rc) {
  4777. CAM_ERR(CAM_ISP,
  4778. "CAM_ISP_CONTEXT: Active request dump failed, rc:%d, ctx:%u, link:0x%x",
  4779. rc, ctx->ctx_id, ctx->link_hdl);
  4780. return rc;
  4781. }
  4782. /* Dump active request fences */
  4783. if (!list_empty(&ctx->active_req_list)) {
  4784. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  4785. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4786. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4787. rc = cam_common_user_dump_helper(dump_args,
  4788. cam_isp_ctx_user_dump_active_requests,
  4789. req, sizeof(uint64_t),
  4790. "ISP_OUT_FENCE_REQUEST_ACTIVE.%s.%u.%d:",
  4791. __cam_isp_ife_sfe_resource_handle_id_to_type(
  4792. req_isp->fence_map_out[i].resource_handle),
  4793. req_isp->fence_map_out[i].image_buf_addr[0],
  4794. req_isp->fence_map_out[i].sync_id);
  4795. if (rc) {
  4796. CAM_ERR(CAM_ISP,
  4797. "CAM_ISP_CONTEXT DUMP_REQ_INFO: Dump failed, rc: %d, ctx_idx: %u, link: 0x%x",
  4798. rc, ctx->ctx_id, ctx->link_hdl);
  4799. return rc;
  4800. }
  4801. }
  4802. }
  4803. }
  4804. return rc;
  4805. }
  4806. static void *cam_isp_ctx_user_dump_timer(
  4807. void *dump_struct, uint8_t *addr_ptr)
  4808. {
  4809. struct cam_ctx_request *req = NULL;
  4810. struct cam_isp_ctx_req *req_isp = NULL;
  4811. uint64_t *addr;
  4812. ktime_t cur_time;
  4813. req = (struct cam_ctx_request *)dump_struct;
  4814. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4815. cur_time = ktime_get();
  4816. addr = (uint64_t *)addr_ptr;
  4817. *addr++ = req->request_id;
  4818. *addr++ = ktime_to_timespec64(
  4819. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_sec;
  4820. *addr++ = ktime_to_timespec64(
  4821. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_nsec / NSEC_PER_USEC;
  4822. *addr++ = ktime_to_timespec64(cur_time).tv_sec;
  4823. *addr++ = ktime_to_timespec64(cur_time).tv_nsec / NSEC_PER_USEC;
  4824. return addr;
  4825. }
  4826. static void *cam_isp_ctx_user_dump_stream_info(
  4827. void *dump_struct, uint8_t *addr_ptr)
  4828. {
  4829. struct cam_context *ctx = NULL;
  4830. int32_t *addr;
  4831. ctx = (struct cam_context *)dump_struct;
  4832. addr = (int32_t *)addr_ptr;
  4833. *addr++ = ctx->ctx_id;
  4834. *addr++ = ctx->dev_hdl;
  4835. *addr++ = ctx->link_hdl;
  4836. return addr;
  4837. }
  4838. static int __cam_isp_ctx_dump_in_top_state(
  4839. struct cam_context *ctx,
  4840. struct cam_req_mgr_dump_info *dump_info)
  4841. {
  4842. int rc = 0;
  4843. bool dump_only_event_record = false;
  4844. size_t buf_len;
  4845. size_t remain_len;
  4846. ktime_t cur_time;
  4847. uint32_t min_len;
  4848. uint64_t diff;
  4849. uintptr_t cpu_addr;
  4850. uint8_t req_type;
  4851. struct cam_isp_context *ctx_isp;
  4852. struct cam_ctx_request *req = NULL;
  4853. struct cam_isp_ctx_req *req_isp;
  4854. struct cam_ctx_request *req_temp;
  4855. struct cam_hw_dump_args ife_dump_args;
  4856. struct cam_common_hw_dump_args dump_args;
  4857. struct cam_hw_cmd_args hw_cmd_args;
  4858. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4859. spin_lock_bh(&ctx->lock);
  4860. list_for_each_entry_safe(req, req_temp,
  4861. &ctx->active_req_list, list) {
  4862. if (req->request_id == dump_info->req_id) {
  4863. CAM_INFO(CAM_ISP, "isp dump active list req: %lld, ctx_idx: %u, link: 0x%x",
  4864. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4865. req_type = 'a';
  4866. goto hw_dump;
  4867. }
  4868. }
  4869. list_for_each_entry_safe(req, req_temp,
  4870. &ctx->wait_req_list, list) {
  4871. if (req->request_id == dump_info->req_id) {
  4872. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld, ctx_idx: %u, link: 0x%x",
  4873. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4874. req_type = 'w';
  4875. goto hw_dump;
  4876. }
  4877. }
  4878. list_for_each_entry_safe(req, req_temp,
  4879. &ctx->pending_req_list, list) {
  4880. if (req->request_id == dump_info->req_id) {
  4881. CAM_INFO(CAM_ISP,
  4882. "isp dump pending list req: %lld, ctx_idx: %u, link: 0x%x",
  4883. dump_info->req_id, ctx->ctx_id, ctx->link_hdl);
  4884. req_type = 'p';
  4885. goto hw_dump;
  4886. }
  4887. }
  4888. goto end;
  4889. hw_dump:
  4890. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  4891. &cpu_addr, &buf_len);
  4892. if (rc) {
  4893. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d, ctx_idx: %u, link: 0x%x",
  4894. dump_info->buf_handle, rc, ctx->ctx_id, ctx->link_hdl);
  4895. spin_unlock_bh(&ctx->lock);
  4896. return rc;
  4897. }
  4898. if (buf_len <= dump_info->offset) {
  4899. spin_unlock_bh(&ctx->lock);
  4900. CAM_WARN(CAM_ISP,
  4901. "Dump buffer overshoot len %zu offset %zu, ctx_idx: %u, link: 0x%x",
  4902. buf_len, dump_info->offset, ctx->ctx_id, ctx->link_hdl);
  4903. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4904. return -ENOSPC;
  4905. }
  4906. remain_len = buf_len - dump_info->offset;
  4907. min_len = sizeof(struct cam_isp_context_dump_header) +
  4908. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  4909. if (remain_len < min_len) {
  4910. spin_unlock_bh(&ctx->lock);
  4911. CAM_WARN(CAM_ISP,
  4912. "Dump buffer exhaust remain %zu min %u, ctx_idx: %u, link: 0x%x",
  4913. remain_len, min_len, ctx->ctx_id, ctx->link_hdl);
  4914. cam_mem_put_cpu_buf(dump_info->buf_handle);
  4915. return -ENOSPC;
  4916. }
  4917. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4918. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4919. cur_time = ktime_get();
  4920. diff = ktime_us_delta(
  4921. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  4922. cur_time);
  4923. __cam_isp_ctx_print_event_record(ctx_isp);
  4924. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  4925. CAM_INFO(CAM_ISP, "req %lld found no error, ctx_idx: %u, link: 0x%x",
  4926. req->request_id, ctx->ctx_id, ctx->link_hdl);
  4927. dump_only_event_record = true;
  4928. }
  4929. dump_args.req_id = dump_info->req_id;
  4930. dump_args.cpu_addr = cpu_addr;
  4931. dump_args.buf_len = buf_len;
  4932. dump_args.offset = dump_info->offset;
  4933. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4934. /* Dump time info */
  4935. rc = cam_common_user_dump_helper(&dump_args, cam_isp_ctx_user_dump_timer,
  4936. req, sizeof(uint64_t), "ISP_CTX_DUMP.%c:", req_type);
  4937. if (rc) {
  4938. CAM_ERR(CAM_ISP, "Time dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4939. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4940. goto end;
  4941. }
  4942. dump_info->offset = dump_args.offset;
  4943. /* Dump stream info */
  4944. ctx->ctxt_to_hw_map = ctx_isp->hw_ctx;
  4945. if (ctx->hw_mgr_intf->hw_dump) {
  4946. /* Dump first part of stream info from isp context */
  4947. rc = cam_common_user_dump_helper(&dump_args,
  4948. cam_isp_ctx_user_dump_stream_info, ctx,
  4949. sizeof(int32_t), "ISP_STREAM_INFO_FROM_CTX:");
  4950. if (rc) {
  4951. CAM_ERR(CAM_ISP,
  4952. "ISP CTX stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4953. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4954. goto end;
  4955. }
  4956. /* Dump second part of stream info from ife hw manager */
  4957. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  4958. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4959. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_DUMP_STREAM_INFO;
  4960. isp_hw_cmd_args.cmd_data = &dump_args;
  4961. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4962. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv, &hw_cmd_args);
  4963. if (rc) {
  4964. CAM_ERR(CAM_ISP,
  4965. "IFE HW MGR stream info dump fail %lld, rc: %d, ctx: %u, link: 0x%x",
  4966. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4967. goto end;
  4968. }
  4969. dump_info->offset = dump_args.offset;
  4970. }
  4971. /* Dump event record */
  4972. rc = __cam_isp_ctx_dump_event_record(ctx_isp, &dump_args);
  4973. if (rc) {
  4974. CAM_ERR(CAM_ISP, "Event record dump fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4975. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4976. goto end;
  4977. }
  4978. dump_info->offset = dump_args.offset;
  4979. if (dump_only_event_record) {
  4980. goto end;
  4981. }
  4982. /* Dump state monitor array */
  4983. rc = __cam_isp_ctx_user_dump_state_monitor_array(ctx_isp, &dump_args);
  4984. if (rc) {
  4985. CAM_ERR(CAM_ISP, "Dump event fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4986. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4987. goto end;
  4988. }
  4989. /* Dump request info */
  4990. rc = __cam_isp_ctx_dump_req_info(ctx, req, &dump_args);
  4991. if (rc) {
  4992. CAM_ERR(CAM_ISP, "Dump Req info fail %lld, rc: %d, ctx_idx: %u, link: 0x%x",
  4993. req->request_id, rc, ctx->ctx_id, ctx->link_hdl);
  4994. goto end;
  4995. }
  4996. spin_unlock_bh(&ctx->lock);
  4997. /* Dump CSID, VFE, and SFE info */
  4998. dump_info->offset = dump_args.offset;
  4999. if (ctx->hw_mgr_intf->hw_dump) {
  5000. ife_dump_args.offset = dump_args.offset;
  5001. ife_dump_args.request_id = dump_info->req_id;
  5002. ife_dump_args.buf_handle = dump_info->buf_handle;
  5003. ife_dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5004. rc = ctx->hw_mgr_intf->hw_dump(
  5005. ctx->hw_mgr_intf->hw_mgr_priv,
  5006. &ife_dump_args);
  5007. dump_info->offset = ife_dump_args.offset;
  5008. }
  5009. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5010. return rc;
  5011. end:
  5012. spin_unlock_bh(&ctx->lock);
  5013. cam_mem_put_cpu_buf(dump_info->buf_handle);
  5014. return rc;
  5015. }
  5016. static int __cam_isp_ctx_flush_req_in_flushed_state(
  5017. struct cam_context *ctx,
  5018. struct cam_req_mgr_flush_request *flush_req)
  5019. {
  5020. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%u link: 0x%x",
  5021. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5022. if (flush_req->req_id > ctx->last_flush_req)
  5023. ctx->last_flush_req = flush_req->req_id;
  5024. return 0;
  5025. }
  5026. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  5027. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  5028. {
  5029. int i, rc, tmp = 0;
  5030. struct cam_ctx_request *req;
  5031. struct cam_ctx_request *req_temp;
  5032. struct cam_isp_ctx_req *req_isp;
  5033. struct list_head flush_list;
  5034. struct cam_isp_context *ctx_isp = NULL;
  5035. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5036. INIT_LIST_HEAD(&flush_list);
  5037. if (list_empty(req_list)) {
  5038. CAM_DBG(CAM_ISP, "request list is empty, ctx_id:%u link: 0x%x",
  5039. ctx->ctx_id, ctx->link_hdl);
  5040. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5041. CAM_INFO(CAM_ISP,
  5042. "no request to cancel(lastapplied:%lld cancel:%lld),ctx:%u link:0x%x",
  5043. ctx_isp->last_applied_req_id, flush_req->req_id,
  5044. ctx->ctx_id, ctx->link_hdl);
  5045. return -EINVAL;
  5046. } else
  5047. return 0;
  5048. }
  5049. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu, ctx_id:%u link: 0x%x",
  5050. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5051. list_for_each_entry_safe(req, req_temp, req_list, list) {
  5052. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  5053. if (req->request_id != flush_req->req_id) {
  5054. continue;
  5055. } else {
  5056. list_del_init(&req->list);
  5057. list_add_tail(&req->list, &flush_list);
  5058. __cam_isp_ctx_update_state_monitor_array(
  5059. ctx_isp,
  5060. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  5061. req->request_id);
  5062. break;
  5063. }
  5064. }
  5065. list_del_init(&req->list);
  5066. list_add_tail(&req->list, &flush_list);
  5067. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  5068. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  5069. }
  5070. if (list_empty(&flush_list)) {
  5071. /*
  5072. * Maybe the req isn't sent to KMD since UMD already skip
  5073. * req in CSL layer.
  5074. */
  5075. CAM_INFO(CAM_ISP,
  5076. "flush list is empty, flush type %d for req %llu, ctx_id:%u link: 0x%x",
  5077. flush_req->type, flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5078. return 0;
  5079. }
  5080. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  5081. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5082. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5083. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  5084. if (req_isp->fence_map_out[i].sync_id != -1) {
  5085. CAM_DBG(CAM_ISP,
  5086. "Flush req 0x%llx, fence %d, ctx_id:%u link: 0x%x",
  5087. req->request_id,
  5088. req_isp->fence_map_out[i].sync_id,
  5089. ctx->ctx_id, ctx->link_hdl);
  5090. rc = cam_sync_signal(
  5091. req_isp->fence_map_out[i].sync_id,
  5092. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5093. CAM_SYNC_ISP_EVENT_FLUSH);
  5094. if (rc) {
  5095. tmp = req_isp->fence_map_out[i].sync_id;
  5096. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5097. "signal fence %d failed, ctx_id:%u link: 0x%x",
  5098. tmp, ctx->ctx_id, ctx->link_hdl);
  5099. }
  5100. req_isp->fence_map_out[i].sync_id = -1;
  5101. }
  5102. }
  5103. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5104. req_isp->cdm_reset_before_apply = false;
  5105. list_del_init(&req->list);
  5106. list_add_tail(&req->list, &ctx->free_req_list);
  5107. }
  5108. return 0;
  5109. }
  5110. static inline void __cam_isp_ctx_reset_fcg_tracker(
  5111. struct cam_context *ctx)
  5112. {
  5113. struct cam_isp_context *ctx_isp;
  5114. struct cam_isp_skip_frame_info *skip_info;
  5115. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5116. /* Reset skipped_list for FCG config */
  5117. ctx_isp->fcg_tracker.sum_skipped = 0;
  5118. ctx_isp->fcg_tracker.num_skipped = 0;
  5119. list_for_each_entry(skip_info, &ctx_isp->fcg_tracker.skipped_list, list)
  5120. skip_info->num_frame_skipped = 0;
  5121. CAM_DBG(CAM_ISP, "Reset FCG skip info on ctx %u link: %x",
  5122. ctx->ctx_id, ctx->link_hdl);
  5123. }
  5124. static int __cam_isp_ctx_flush_req_in_top_state(
  5125. struct cam_context *ctx,
  5126. struct cam_req_mgr_flush_request *flush_req)
  5127. {
  5128. int rc = 0;
  5129. struct cam_isp_context *ctx_isp;
  5130. struct cam_isp_stop_args stop_isp;
  5131. struct cam_hw_stop_args stop_args;
  5132. struct cam_hw_reset_args reset_args;
  5133. struct cam_req_mgr_timer_notify timer;
  5134. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  5135. /* Reset skipped_list for FCG config */
  5136. __cam_isp_ctx_reset_fcg_tracker(ctx);
  5137. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  5138. if (ctx->state <= CAM_CTX_READY) {
  5139. ctx->state = CAM_CTX_ACQUIRED;
  5140. goto end;
  5141. }
  5142. spin_lock_bh(&ctx->lock);
  5143. ctx->state = CAM_CTX_FLUSHED;
  5144. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5145. spin_unlock_bh(&ctx->lock);
  5146. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%u link: 0x%x",
  5147. flush_req->req_id, ctx->ctx_id, ctx->link_hdl);
  5148. ctx->last_flush_req = flush_req->req_id;
  5149. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  5150. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5151. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5152. stop_isp.stop_only = true;
  5153. stop_isp.is_internal_stop = false;
  5154. stop_args.args = (void *)&stop_isp;
  5155. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5156. &stop_args);
  5157. if (rc)
  5158. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d, ctx_id:%u link: 0x%x",
  5159. rc, ctx->ctx_id, ctx->link_hdl);
  5160. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.Ctx_id:%u link: 0x%x",
  5161. ctx->ctx_id, ctx->link_hdl);
  5162. CAM_DBG(CAM_ISP, "Flush wait and active lists, ctx_id:%u link: 0x%x",
  5163. ctx->ctx_id, ctx->link_hdl);
  5164. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  5165. timer.link_hdl = ctx->link_hdl;
  5166. timer.dev_hdl = ctx->dev_hdl;
  5167. timer.state = false;
  5168. ctx->ctx_crm_intf->notify_timer(&timer);
  5169. }
  5170. spin_lock_bh(&ctx->lock);
  5171. if (!list_empty(&ctx->wait_req_list))
  5172. __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  5173. flush_req);
  5174. if (!list_empty(&ctx->active_req_list))
  5175. __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  5176. flush_req);
  5177. ctx_isp->active_req_cnt = 0;
  5178. spin_unlock_bh(&ctx->lock);
  5179. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5180. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  5181. &reset_args);
  5182. if (rc)
  5183. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d, ctx_id:%u link: 0x%x",
  5184. rc, ctx->ctx_id, ctx->link_hdl);
  5185. ctx_isp->init_received = false;
  5186. }
  5187. CAM_DBG(CAM_ISP, "Flush pending list, ctx_idx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  5188. /*
  5189. * On occasions when we are doing a flush all, HW would get reset
  5190. * shutting down any th/bh in the pipeline. If internal recovery
  5191. * is triggered prior to flush, by clearing the pending list post
  5192. * HW reset will ensure no stale request entities are left behind
  5193. */
  5194. spin_lock_bh(&ctx->lock);
  5195. __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5196. spin_unlock_bh(&ctx->lock);
  5197. end:
  5198. ctx_isp->bubble_frame_cnt = 0;
  5199. ctx_isp->congestion_cnt = 0;
  5200. ctx_isp->sof_dbg_irq_en = false;
  5201. atomic_set(&ctx_isp->process_bubble, 0);
  5202. atomic_set(&ctx_isp->rxd_epoch, 0);
  5203. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5204. return rc;
  5205. }
  5206. static int __cam_isp_ctx_flush_req_in_ready(
  5207. struct cam_context *ctx,
  5208. struct cam_req_mgr_flush_request *flush_req)
  5209. {
  5210. int rc = 0;
  5211. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_id:%u link: 0x%x",
  5212. ctx->ctx_id, ctx->link_hdl);
  5213. spin_lock_bh(&ctx->lock);
  5214. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  5215. /* if nothing is in pending req list, change state to acquire */
  5216. if (list_empty(&ctx->pending_req_list))
  5217. ctx->state = CAM_CTX_ACQUIRED;
  5218. spin_unlock_bh(&ctx->lock);
  5219. trace_cam_context_state("ISP", ctx);
  5220. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d, ctx_id:%u link: 0x%x",
  5221. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5222. return rc;
  5223. }
  5224. static struct cam_ctx_ops
  5225. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5226. /* SOF */
  5227. {
  5228. .ioctl_ops = {},
  5229. .crm_ops = {
  5230. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5231. .notify_frame_skip =
  5232. __cam_isp_ctx_apply_default_req_settings,
  5233. },
  5234. .irq_ops = NULL,
  5235. },
  5236. /* APPLIED */
  5237. {
  5238. .ioctl_ops = {},
  5239. .crm_ops = {},
  5240. .irq_ops = NULL,
  5241. },
  5242. /* EPOCH */
  5243. {
  5244. .ioctl_ops = {},
  5245. .crm_ops = {
  5246. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5247. .notify_frame_skip =
  5248. __cam_isp_ctx_apply_default_req_settings,
  5249. },
  5250. .irq_ops = NULL,
  5251. },
  5252. /* BUBBLE */
  5253. {
  5254. .ioctl_ops = {},
  5255. .crm_ops = {
  5256. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5257. .notify_frame_skip =
  5258. __cam_isp_ctx_apply_default_req_settings,
  5259. },
  5260. .irq_ops = NULL,
  5261. },
  5262. /* Bubble Applied */
  5263. {
  5264. .ioctl_ops = {},
  5265. .crm_ops = {
  5266. .notify_frame_skip =
  5267. __cam_isp_ctx_apply_default_req_settings,
  5268. },
  5269. .irq_ops = NULL,
  5270. },
  5271. /* HW ERROR */
  5272. {
  5273. .ioctl_ops = {},
  5274. .crm_ops = {},
  5275. .irq_ops = NULL,
  5276. },
  5277. /* HALT */
  5278. {
  5279. .ioctl_ops = {},
  5280. .crm_ops = {},
  5281. .irq_ops = NULL,
  5282. },
  5283. };
  5284. static struct cam_ctx_ops
  5285. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  5286. /* SOF */
  5287. {
  5288. .ioctl_ops = {},
  5289. .crm_ops = {
  5290. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  5291. },
  5292. .irq_ops = NULL,
  5293. },
  5294. /* APPLIED */
  5295. {
  5296. .ioctl_ops = {},
  5297. .crm_ops = {},
  5298. .irq_ops = NULL,
  5299. },
  5300. /* EPOCH */
  5301. {
  5302. .ioctl_ops = {},
  5303. .crm_ops = {
  5304. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  5305. },
  5306. .irq_ops = NULL,
  5307. },
  5308. /* BUBBLE */
  5309. {
  5310. .ioctl_ops = {},
  5311. .crm_ops = {
  5312. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  5313. },
  5314. .irq_ops = NULL,
  5315. },
  5316. /* Bubble Applied */
  5317. {
  5318. .ioctl_ops = {},
  5319. .crm_ops = {},
  5320. .irq_ops = NULL,
  5321. },
  5322. /* HW ERROR */
  5323. {
  5324. .ioctl_ops = {},
  5325. .crm_ops = {},
  5326. .irq_ops = NULL,
  5327. },
  5328. /* HALT */
  5329. {
  5330. .ioctl_ops = {},
  5331. .crm_ops = {},
  5332. .irq_ops = NULL,
  5333. },
  5334. };
  5335. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  5336. struct cam_isp_context *ctx_isp, void *evt_data)
  5337. {
  5338. int rc = 0;
  5339. struct cam_context *ctx = ctx_isp->base;
  5340. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5341. uint64_t request_id = 0;
  5342. if (!evt_data) {
  5343. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5344. ctx->ctx_id, ctx->link_hdl);
  5345. return -EINVAL;
  5346. }
  5347. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5348. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5349. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5350. /*
  5351. * notify reqmgr with sof signal. Note, due to scheduling delay
  5352. * we can run into situation that two active requests has already
  5353. * be in the active queue while we try to do the notification.
  5354. * In this case, we need to skip the current notification. This
  5355. * helps the state machine to catch up the delay.
  5356. */
  5357. if (ctx_isp->active_req_cnt <= 2) {
  5358. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5359. /*
  5360. * It's possible for rup done to be processed before
  5361. * SOF, check for first active request shutter here
  5362. */
  5363. if (!list_empty(&ctx->active_req_list)) {
  5364. struct cam_ctx_request *req = NULL;
  5365. req = list_first_entry(&ctx->active_req_list,
  5366. struct cam_ctx_request, list);
  5367. if (req->request_id > ctx_isp->reported_req_id) {
  5368. request_id = req->request_id;
  5369. ctx_isp->reported_req_id = request_id;
  5370. }
  5371. }
  5372. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5373. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5374. } else {
  5375. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM, ctx_idx: %u, link: 0x%x",
  5376. ctx->ctx_id, ctx->link_hdl);
  5377. }
  5378. if (list_empty(&ctx->active_req_list))
  5379. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5380. else
  5381. CAM_DBG(CAM_ISP, "Still need to wait for the buf done, ctx_idx: %u, link: 0x%x",
  5382. ctx->ctx_id, ctx->link_hdl);
  5383. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5384. __cam_isp_ctx_substate_val_to_type(
  5385. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5386. return rc;
  5387. }
  5388. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  5389. struct cam_isp_context *ctx_isp, void *evt_data)
  5390. {
  5391. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5392. if (!evt_data) {
  5393. CAM_ERR(CAM_ISP, "in valid sof event data");
  5394. return -EINVAL;
  5395. }
  5396. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5397. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  5398. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  5399. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  5400. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5401. __cam_isp_ctx_substate_val_to_type(
  5402. ctx_isp->substate_activated));
  5403. return 0;
  5404. }
  5405. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  5406. struct cam_isp_context *ctx_isp, void *evt_data)
  5407. {
  5408. struct cam_ctx_request *req;
  5409. struct cam_isp_ctx_req *req_isp;
  5410. struct cam_context *ctx = ctx_isp->base;
  5411. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5412. uint64_t request_id = 0;
  5413. /*
  5414. * Sof in bubble applied state means, reg update not received.
  5415. * before increment frame id and override time stamp value, send
  5416. * the previous sof time stamp that got captured in the
  5417. * sof in applied state.
  5418. */
  5419. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5420. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5421. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5422. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5423. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5424. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5425. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5426. if (list_empty(&ctx->wait_req_list)) {
  5427. /*
  5428. * If no pending req in epoch, this is an error case.
  5429. * The recovery is to go back to sof state
  5430. */
  5431. CAM_ERR(CAM_ISP, "No wait request, ctx_idx: %u, link: 0x%x",
  5432. ctx->ctx_id, ctx->link_hdl);
  5433. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5434. /* Send SOF event as empty frame*/
  5435. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5436. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5437. goto end;
  5438. }
  5439. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  5440. list);
  5441. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  5442. req_isp->bubble_detected = true;
  5443. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%u link: 0x%x Report Bubble flag %d req id:%lld",
  5444. ctx->ctx_id, ctx->link_hdl, req_isp->bubble_report, req->request_id);
  5445. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5446. req_isp->cdm_reset_before_apply = false;
  5447. if (req_isp->bubble_report) {
  5448. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  5449. req->request_id, ctx_isp);
  5450. atomic_set(&ctx_isp->process_bubble, 1);
  5451. } else {
  5452. req_isp->bubble_report = 0;
  5453. }
  5454. /*
  5455. * Always move the request to active list. Let buf done
  5456. * function handles the rest.
  5457. */
  5458. list_del_init(&req->list);
  5459. list_add_tail(&req->list, &ctx->active_req_list);
  5460. ctx_isp->active_req_cnt++;
  5461. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d), ctx_idx: %u, link: 0x%x",
  5462. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5463. if (!req_isp->bubble_report) {
  5464. if (req->request_id > ctx_isp->reported_req_id) {
  5465. request_id = req->request_id;
  5466. ctx_isp->reported_req_id = request_id;
  5467. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5468. CAM_REQ_MGR_SOF_EVENT_ERROR);
  5469. } else
  5470. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5471. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5472. } else
  5473. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5474. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5475. /* change the state to bubble, as reg update has not come */
  5476. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  5477. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  5478. __cam_isp_ctx_substate_val_to_type(
  5479. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5480. end:
  5481. return 0;
  5482. }
  5483. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  5484. struct cam_isp_context *ctx_isp, void *evt_data)
  5485. {
  5486. uint32_t i;
  5487. struct cam_ctx_request *req;
  5488. struct cam_context *ctx = ctx_isp->base;
  5489. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  5490. struct cam_isp_ctx_req *req_isp;
  5491. struct cam_hw_cmd_args hw_cmd_args;
  5492. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5493. uint64_t request_id = 0;
  5494. uint64_t last_cdm_done_req = 0;
  5495. int rc = 0;
  5496. if (!evt_data) {
  5497. CAM_ERR(CAM_ISP, "in valid sof event data, ctx_idx: %u, link: 0x%x",
  5498. ctx->ctx_id, ctx->link_hdl);
  5499. return -EINVAL;
  5500. }
  5501. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  5502. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx_idx: %u, link: 0x%x",
  5503. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id, ctx->link_hdl);
  5504. if (atomic_read(&ctx_isp->process_bubble)) {
  5505. if (list_empty(&ctx->active_req_list)) {
  5506. CAM_ERR(CAM_ISP, "No available active req in bubble, ctx: %u, link: 0x%x",
  5507. ctx->ctx_id, ctx->link_hdl);
  5508. atomic_set(&ctx_isp->process_bubble, 0);
  5509. return -EINVAL;
  5510. }
  5511. if (ctx_isp->last_sof_timestamp ==
  5512. ctx_isp->sof_timestamp_val) {
  5513. CAM_DBG(CAM_ISP,
  5514. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %u, link: 0x%x",
  5515. ctx_isp->frame_id,
  5516. ctx_isp->sof_timestamp_val,
  5517. ctx->ctx_id, ctx->link_hdl);
  5518. goto end;
  5519. }
  5520. req = list_first_entry(&ctx->active_req_list,
  5521. struct cam_ctx_request, list);
  5522. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5523. if (req_isp->bubble_detected) {
  5524. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5525. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5526. isp_hw_cmd_args.cmd_type =
  5527. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  5528. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5529. rc = ctx->hw_mgr_intf->hw_cmd(
  5530. ctx->hw_mgr_intf->hw_mgr_priv,
  5531. &hw_cmd_args);
  5532. if (rc) {
  5533. CAM_ERR(CAM_ISP, "HW command failed, ctx_id: %u, link: 0x%x",
  5534. ctx->ctx_id, ctx->link_hdl);
  5535. return rc;
  5536. }
  5537. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  5538. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %u, link: 0x%x",
  5539. last_cdm_done_req, ctx->ctx_id, ctx->link_hdl);
  5540. if (last_cdm_done_req >= req->request_id) {
  5541. CAM_DBG(CAM_ISP,
  5542. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done, ctx_id: %u, link: 0x%x",
  5543. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5544. if (req_isp->num_fence_map_out ==
  5545. req_isp->num_deferred_acks) {
  5546. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  5547. true,
  5548. CAM_SYNC_STATE_SIGNALED_ERROR,
  5549. CAM_SYNC_ISP_EVENT_BUBBLE);
  5550. __cam_isp_ctx_handle_buf_done_for_req_list(
  5551. ctx_isp, req);
  5552. }
  5553. goto end;
  5554. } else {
  5555. CAM_WARN(CAM_ISP,
  5556. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay, ctx_id: %u, link: 0x%x",
  5557. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5558. req_isp->num_acked = 0;
  5559. req_isp->num_deferred_acks = 0;
  5560. req_isp->bubble_detected = false;
  5561. req_isp->cdm_reset_before_apply = true;
  5562. list_del_init(&req->list);
  5563. list_add(&req->list, &ctx->pending_req_list);
  5564. atomic_set(&ctx_isp->process_bubble, 0);
  5565. ctx_isp->active_req_cnt--;
  5566. CAM_DBG(CAM_REQ,
  5567. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u link: 0x%x",
  5568. req->request_id,
  5569. ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5570. }
  5571. goto end;
  5572. }
  5573. }
  5574. /*
  5575. * Signal all active requests with error and move the all the active
  5576. * requests to free list
  5577. */
  5578. while (!list_empty(&ctx->active_req_list)) {
  5579. req = list_first_entry(&ctx->active_req_list,
  5580. struct cam_ctx_request, list);
  5581. list_del_init(&req->list);
  5582. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5583. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5584. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d, ctx %u link: 0x%x",
  5585. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  5586. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5587. if (req_isp->fence_map_out[i].sync_id != -1) {
  5588. cam_sync_signal(
  5589. req_isp->fence_map_out[i].sync_id,
  5590. CAM_SYNC_STATE_SIGNALED_ERROR,
  5591. CAM_SYNC_ISP_EVENT_BUBBLE);
  5592. }
  5593. list_add_tail(&req->list, &ctx->free_req_list);
  5594. ctx_isp->active_req_cnt--;
  5595. }
  5596. end:
  5597. /* notify reqmgr with sof signal */
  5598. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5599. /*
  5600. * It is idle frame with out any applied request id, send
  5601. * request id as zero
  5602. */
  5603. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5604. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5605. /*
  5606. * Can't move the substate to SOF if we are processing bubble,
  5607. * since the SOF substate can't receive REG_UPD and buf done,
  5608. * then the processing of bubble req can't be finished
  5609. */
  5610. if (!atomic_read(&ctx_isp->process_bubble))
  5611. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5612. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5613. __cam_isp_ctx_substate_val_to_type(
  5614. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5615. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  5616. return 0;
  5617. }
  5618. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  5619. struct cam_isp_context *ctx_isp, void *evt_data)
  5620. {
  5621. struct cam_ctx_request *req = NULL;
  5622. struct cam_context *ctx = ctx_isp->base;
  5623. req = list_first_entry(&ctx->active_req_list,
  5624. struct cam_ctx_request, list);
  5625. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request, ctx %u link: 0x%x",
  5626. req->request_id, ctx->ctx_id, ctx->link_hdl);
  5627. return 0;
  5628. }
  5629. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  5630. struct cam_isp_context *ctx_isp, void *evt_data)
  5631. {
  5632. struct cam_ctx_request *req = NULL;
  5633. struct cam_context *ctx = ctx_isp->base;
  5634. struct cam_isp_ctx_req *req_isp;
  5635. uint64_t request_id = 0;
  5636. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  5637. /* notify reqmgr with sof signal*/
  5638. if (list_empty(&ctx->wait_req_list)) {
  5639. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request, ctx %u link: 0x%x",
  5640. ctx->ctx_id, ctx->link_hdl);
  5641. goto error;
  5642. }
  5643. req = list_first_entry(&ctx->wait_req_list,
  5644. struct cam_ctx_request, list);
  5645. list_del_init(&req->list);
  5646. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5647. request_id =
  5648. (req_isp->hw_update_data.packet_opcode_type ==
  5649. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  5650. if (req_isp->num_fence_map_out != 0) {
  5651. list_add_tail(&req->list, &ctx->active_req_list);
  5652. ctx_isp->active_req_cnt++;
  5653. CAM_DBG(CAM_ISP,
  5654. "move request %lld to active list(cnt = %d), ctx %u link: 0x%x",
  5655. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5656. /* if packet has buffers, set correct request id */
  5657. request_id = req->request_id;
  5658. } else {
  5659. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  5660. /* no io config, so the request is completed. */
  5661. list_add_tail(&req->list, &ctx->free_req_list);
  5662. CAM_DBG(CAM_ISP,
  5663. "move active req %lld to free list(cnt=%d), ctx %u link: 0x%x",
  5664. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  5665. }
  5666. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  5667. if (request_id)
  5668. ctx_isp->reported_req_id = request_id;
  5669. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5670. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5671. CAM_DBG(CAM_ISP, "next Substate[%s], ctx %u link: 0x%x",
  5672. __cam_isp_ctx_substate_val_to_type(
  5673. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5674. __cam_isp_ctx_update_event_record(ctx_isp,
  5675. CAM_ISP_CTX_EVENT_RUP, req, NULL);
  5676. return 0;
  5677. error:
  5678. /* Send SOF event as idle frame*/
  5679. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  5680. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  5681. __cam_isp_ctx_update_event_record(ctx_isp,
  5682. CAM_ISP_CTX_EVENT_RUP, NULL, NULL);
  5683. /*
  5684. * There is no request in the pending list, move the sub state machine
  5685. * to SOF sub state
  5686. */
  5687. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  5688. return 0;
  5689. }
  5690. static struct cam_isp_ctx_irq_ops
  5691. cam_isp_ctx_rdi_only_activated_state_machine_irq
  5692. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5693. /* SOF */
  5694. {
  5695. .irq_ops = {
  5696. NULL,
  5697. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5698. __cam_isp_ctx_reg_upd_in_sof,
  5699. NULL,
  5700. __cam_isp_ctx_notify_eof_in_activated_state,
  5701. NULL,
  5702. },
  5703. },
  5704. /* APPLIED */
  5705. {
  5706. .irq_ops = {
  5707. __cam_isp_ctx_handle_error,
  5708. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  5709. __cam_isp_ctx_reg_upd_in_applied_state,
  5710. NULL,
  5711. __cam_isp_ctx_notify_eof_in_activated_state,
  5712. __cam_isp_ctx_buf_done_in_applied,
  5713. },
  5714. },
  5715. /* EPOCH */
  5716. {
  5717. .irq_ops = {
  5718. __cam_isp_ctx_handle_error,
  5719. __cam_isp_ctx_rdi_only_sof_in_top_state,
  5720. NULL,
  5721. NULL,
  5722. __cam_isp_ctx_notify_eof_in_activated_state,
  5723. __cam_isp_ctx_buf_done_in_epoch,
  5724. },
  5725. },
  5726. /* BUBBLE*/
  5727. {
  5728. .irq_ops = {
  5729. __cam_isp_ctx_handle_error,
  5730. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  5731. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  5732. NULL,
  5733. __cam_isp_ctx_notify_eof_in_activated_state,
  5734. __cam_isp_ctx_buf_done_in_bubble,
  5735. },
  5736. },
  5737. /* BUBBLE APPLIED ie PRE_BUBBLE */
  5738. {
  5739. .irq_ops = {
  5740. __cam_isp_ctx_handle_error,
  5741. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  5742. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  5743. NULL,
  5744. __cam_isp_ctx_notify_eof_in_activated_state,
  5745. __cam_isp_ctx_buf_done_in_bubble_applied,
  5746. },
  5747. },
  5748. /* HW ERROR */
  5749. {
  5750. },
  5751. /* HALT */
  5752. {
  5753. },
  5754. };
  5755. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  5756. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  5757. {
  5758. int rc = 0;
  5759. struct cam_isp_context *ctx_isp =
  5760. (struct cam_isp_context *) ctx->ctx_priv;
  5761. CAM_DBG(CAM_ISP, "current Substate[%s], ctx_idx: %u, link: 0x%x",
  5762. __cam_isp_ctx_substate_val_to_type(
  5763. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5764. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  5765. CAM_ISP_CTX_ACTIVATED_APPLIED);
  5766. CAM_DBG(CAM_ISP, "new Substate[%s], ctx_idx: %u, link: 0x%x",
  5767. __cam_isp_ctx_substate_val_to_type(
  5768. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  5769. if (rc)
  5770. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5771. "ctx_id:%u link: 0x%x Apply failed in Substate[%s], rc %d",
  5772. ctx->ctx_id, ctx->link_hdl,
  5773. __cam_isp_ctx_substate_val_to_type(
  5774. ctx_isp->substate_activated), rc);
  5775. return rc;
  5776. }
  5777. static struct cam_ctx_ops
  5778. cam_isp_ctx_rdi_only_activated_state_machine
  5779. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5780. /* SOF */
  5781. {
  5782. .ioctl_ops = {},
  5783. .crm_ops = {
  5784. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5785. },
  5786. .irq_ops = NULL,
  5787. },
  5788. /* APPLIED */
  5789. {
  5790. .ioctl_ops = {},
  5791. .crm_ops = {},
  5792. .irq_ops = NULL,
  5793. },
  5794. /* EPOCH */
  5795. {
  5796. .ioctl_ops = {},
  5797. .crm_ops = {
  5798. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5799. },
  5800. .irq_ops = NULL,
  5801. },
  5802. /* PRE BUBBLE */
  5803. {
  5804. .ioctl_ops = {},
  5805. .crm_ops = {},
  5806. .irq_ops = NULL,
  5807. },
  5808. /* BUBBLE */
  5809. {
  5810. .ioctl_ops = {},
  5811. .crm_ops = {},
  5812. .irq_ops = NULL,
  5813. },
  5814. /* HW ERROR */
  5815. {
  5816. .ioctl_ops = {},
  5817. .crm_ops = {},
  5818. .irq_ops = NULL,
  5819. },
  5820. /* HALT */
  5821. {
  5822. .ioctl_ops = {},
  5823. .crm_ops = {},
  5824. .irq_ops = NULL,
  5825. },
  5826. };
  5827. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  5828. struct cam_flush_dev_cmd *cmd)
  5829. {
  5830. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  5831. struct cam_req_mgr_flush_request flush_req;
  5832. if (!ctx_isp->offline_context) {
  5833. CAM_ERR(CAM_ISP, "flush dev only supported in offline context,ctx: %u, link:0x%x",
  5834. ctx->ctx_id, ctx->link_hdl);
  5835. return -EINVAL;
  5836. }
  5837. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  5838. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  5839. flush_req.req_id = cmd->req_id;
  5840. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu), ctx_idx: %u, link: 0x%x",
  5841. flush_req.type, flush_req.req_id, ctx->ctx_id, ctx->link_hdl);
  5842. switch (ctx->state) {
  5843. case CAM_CTX_ACQUIRED:
  5844. case CAM_CTX_ACTIVATED:
  5845. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  5846. case CAM_CTX_READY:
  5847. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  5848. default:
  5849. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d, ctx_idx: %u, link: 0x%x",
  5850. ctx->state, ctx->ctx_id, ctx->link_hdl);
  5851. return -EINVAL;
  5852. }
  5853. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  5854. cam_req_mgr_workq_flush(ctx_isp->workq);
  5855. }
  5856. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  5857. {
  5858. int i;
  5859. if (ctx->out_map_entries) {
  5860. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5861. kfree(ctx->out_map_entries[i]);
  5862. ctx->out_map_entries[i] = NULL;
  5863. }
  5864. kfree(ctx->out_map_entries);
  5865. ctx->out_map_entries = NULL;
  5866. }
  5867. if (ctx->in_map_entries) {
  5868. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5869. kfree(ctx->in_map_entries[i]);
  5870. ctx->in_map_entries[i] = NULL;
  5871. }
  5872. kfree(ctx->in_map_entries);
  5873. ctx->in_map_entries = NULL;
  5874. }
  5875. if (ctx->hw_update_entry) {
  5876. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5877. kfree(ctx->hw_update_entry[i]);
  5878. ctx->hw_update_entry[i] = NULL;
  5879. }
  5880. kfree(ctx->hw_update_entry);
  5881. ctx->hw_update_entry = NULL;
  5882. }
  5883. ctx->max_out_map_entries = 0;
  5884. ctx->max_in_map_entries = 0;
  5885. ctx->max_hw_update_entries = 0;
  5886. }
  5887. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  5888. void *cmd)
  5889. {
  5890. int rc = 0;
  5891. struct cam_hw_release_args rel_arg;
  5892. struct cam_isp_context *ctx_isp =
  5893. (struct cam_isp_context *) ctx->ctx_priv;
  5894. struct cam_req_mgr_flush_request flush_req;
  5895. int i;
  5896. if (ctx_isp->hw_ctx) {
  5897. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5898. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5899. &rel_arg);
  5900. ctx_isp->hw_ctx = NULL;
  5901. } else {
  5902. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u], link: 0x%x",
  5903. ctx->ctx_id, ctx->link_hdl);
  5904. }
  5905. ctx->last_flush_req = 0;
  5906. ctx_isp->custom_enabled = false;
  5907. ctx_isp->use_frame_header_ts = false;
  5908. ctx_isp->use_default_apply = false;
  5909. ctx_isp->frame_id = 0;
  5910. ctx_isp->active_req_cnt = 0;
  5911. ctx_isp->reported_req_id = 0;
  5912. ctx_isp->reported_frame_id = 0;
  5913. ctx_isp->hw_acquired = false;
  5914. ctx_isp->init_received = false;
  5915. ctx_isp->support_consumed_addr = false;
  5916. ctx_isp->aeb_enabled = false;
  5917. ctx_isp->req_info.last_bufdone_req_id = 0;
  5918. kfree(ctx_isp->vfe_bus_comp_grp);
  5919. kfree(ctx_isp->sfe_bus_comp_grp);
  5920. ctx_isp->vfe_bus_comp_grp = NULL;
  5921. ctx_isp->sfe_bus_comp_grp = NULL;
  5922. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  5923. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  5924. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5925. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  5926. /*
  5927. * Ideally, we should never have any active request here.
  5928. * But we still add some sanity check code here to help the debug
  5929. */
  5930. if (!list_empty(&ctx->active_req_list))
  5931. CAM_WARN(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  5932. ctx->ctx_id, ctx->link_hdl);
  5933. /* Flush all the pending request list */
  5934. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5935. flush_req.link_hdl = ctx->link_hdl;
  5936. flush_req.dev_hdl = ctx->dev_hdl;
  5937. flush_req.req_id = 0;
  5938. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  5939. ctx->ctx_id, ctx->link_hdl);
  5940. spin_lock_bh(&ctx->lock);
  5941. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  5942. spin_unlock_bh(&ctx->lock);
  5943. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5944. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  5945. ctx->state = CAM_CTX_ACQUIRED;
  5946. trace_cam_context_state("ISP", ctx);
  5947. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  5948. ctx->ctx_id, ctx->link_hdl, ctx->state);
  5949. return rc;
  5950. }
  5951. /* top level state machine */
  5952. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  5953. struct cam_release_dev_cmd *cmd)
  5954. {
  5955. int rc = 0;
  5956. int i;
  5957. struct cam_hw_release_args rel_arg;
  5958. struct cam_isp_context *ctx_isp =
  5959. (struct cam_isp_context *) ctx->ctx_priv;
  5960. struct cam_req_mgr_flush_request flush_req;
  5961. if (cmd && ctx_isp->hw_ctx) {
  5962. CAM_ERR(CAM_ISP, "releasing hw, ctx_idx: %u, link: 0x%x",
  5963. ctx->ctx_id, ctx->link_hdl);
  5964. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  5965. }
  5966. if (ctx_isp->hw_ctx) {
  5967. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5968. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5969. &rel_arg);
  5970. ctx_isp->hw_ctx = NULL;
  5971. }
  5972. cam_common_release_evt_params(ctx->dev_hdl);
  5973. memset(&ctx_isp->evt_inject_params, 0, sizeof(struct cam_hw_inject_evt_param));
  5974. ctx->session_hdl = -1;
  5975. ctx->dev_hdl = -1;
  5976. ctx->link_hdl = -1;
  5977. ctx->ctx_crm_intf = NULL;
  5978. ctx->last_flush_req = 0;
  5979. ctx_isp->frame_id = 0;
  5980. ctx_isp->active_req_cnt = 0;
  5981. ctx_isp->reported_req_id = 0;
  5982. ctx_isp->reported_frame_id = 0;
  5983. ctx_isp->hw_acquired = false;
  5984. ctx_isp->init_received = false;
  5985. ctx_isp->offline_context = false;
  5986. ctx_isp->vfps_aux_context = false;
  5987. ctx_isp->rdi_only_context = false;
  5988. ctx_isp->req_info.last_bufdone_req_id = 0;
  5989. ctx_isp->v4l2_event_sub_ids = 0;
  5990. ctx_isp->resume_hw_in_flushed = false;
  5991. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  5992. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  5993. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5994. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  5995. /*
  5996. * Ideally, we should never have any active request here.
  5997. * But we still add some sanity check code here to help the debug
  5998. */
  5999. if (!list_empty(&ctx->active_req_list))
  6000. CAM_ERR(CAM_ISP, "Active list is not empty, ctx_idx: %u, link: 0x%x",
  6001. ctx->ctx_id, ctx->link_hdl);
  6002. /* Flush all the pending request list */
  6003. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  6004. flush_req.link_hdl = ctx->link_hdl;
  6005. flush_req.dev_hdl = ctx->dev_hdl;
  6006. flush_req.req_id = 0;
  6007. CAM_DBG(CAM_ISP, "try to flush pending list, ctx_idx: %u, link: 0x%x",
  6008. ctx->ctx_id, ctx->link_hdl);
  6009. spin_lock_bh(&ctx->lock);
  6010. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  6011. spin_unlock_bh(&ctx->lock);
  6012. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6013. ctx->state = CAM_CTX_AVAILABLE;
  6014. trace_cam_context_state("ISP", ctx);
  6015. CAM_DBG(CAM_ISP, "Release device success[%u] link: 0x%x next state %d",
  6016. ctx->ctx_id, ctx->link_hdl, ctx->state);
  6017. return rc;
  6018. }
  6019. static int __cam_isp_ctx_config_dev_in_top_state(
  6020. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  6021. {
  6022. int rc = 0, i;
  6023. struct cam_ctx_request *req = NULL;
  6024. struct cam_isp_ctx_req *req_isp;
  6025. struct cam_packet *packet;
  6026. size_t remain_len = 0;
  6027. struct cam_hw_prepare_update_args cfg = {0};
  6028. struct cam_isp_prepare_hw_update_data *hw_update_data;
  6029. struct cam_req_mgr_add_request add_req;
  6030. struct cam_isp_context *ctx_isp =
  6031. (struct cam_isp_context *) ctx->ctx_priv;
  6032. struct cam_hw_cmd_args hw_cmd_args;
  6033. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6034. uint32_t packet_opcode = 0;
  6035. CAM_DBG(CAM_ISP, "get free request object......ctx_idx: %u, link: 0x%x",
  6036. ctx->ctx_id, ctx->link_hdl);
  6037. /* get free request */
  6038. spin_lock_bh(&ctx->lock);
  6039. if (!list_empty(&ctx->free_req_list)) {
  6040. req = list_first_entry(&ctx->free_req_list,
  6041. struct cam_ctx_request, list);
  6042. list_del_init(&req->list);
  6043. }
  6044. spin_unlock_bh(&ctx->lock);
  6045. if (!req) {
  6046. CAM_ERR(CAM_ISP, "No more request obj free, ctx_idx: %u, link: 0x%x",
  6047. ctx->ctx_id, ctx->link_hdl);
  6048. return -ENOMEM;
  6049. }
  6050. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6051. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  6052. if (IS_ERR(packet)) {
  6053. rc = PTR_ERR(packet);
  6054. goto free_req;
  6055. }
  6056. /* Query the packet opcode */
  6057. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6058. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6059. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  6060. isp_hw_cmd_args.cmd_data = (void *)packet;
  6061. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6062. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6063. &hw_cmd_args);
  6064. if (rc) {
  6065. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6066. ctx->ctx_id, ctx->link_hdl);
  6067. goto free_req;
  6068. }
  6069. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  6070. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  6071. && (packet->header.request_id <= ctx->last_flush_req)) {
  6072. CAM_INFO(CAM_ISP,
  6073. "request %lld has been flushed, reject packet, ctx_idx: %u, link: 0x%x",
  6074. packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6075. rc = -EBADR;
  6076. goto free_req;
  6077. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  6078. && (packet->header.request_id <= ctx->last_flush_req)
  6079. && ctx->last_flush_req && packet->header.request_id) {
  6080. CAM_WARN(CAM_ISP,
  6081. "last flushed req is %lld, config dev(init) for req %lld, ctx_idx: %u, link: 0x%x",
  6082. ctx->last_flush_req, packet->header.request_id, ctx->ctx_id, ctx->link_hdl);
  6083. rc = -EBADR;
  6084. goto free_req;
  6085. }
  6086. cfg.packet = packet;
  6087. cfg.remain_len = remain_len;
  6088. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6089. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  6090. cfg.hw_update_entries = req_isp->cfg;
  6091. cfg.max_out_map_entries = ctx->max_out_map_entries;
  6092. cfg.max_in_map_entries = ctx->max_in_map_entries;
  6093. cfg.out_map_entries = req_isp->fence_map_out;
  6094. cfg.in_map_entries = req_isp->fence_map_in;
  6095. cfg.priv = &req_isp->hw_update_data;
  6096. cfg.pf_data = &(req->pf_data);
  6097. cfg.num_out_map_entries = 0;
  6098. cfg.num_in_map_entries = 0;
  6099. cfg.buf_tracker = &req->buf_tracker;
  6100. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  6101. memset(req_isp->fence_map_out, 0, sizeof(struct cam_hw_fence_map_entry)
  6102. * ctx->max_out_map_entries);
  6103. INIT_LIST_HEAD(cfg.buf_tracker);
  6104. rc = ctx->hw_mgr_intf->hw_prepare_update(
  6105. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  6106. if (rc != 0) {
  6107. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer, ctx: %u, link: 0x%x",
  6108. ctx->ctx_id, ctx->link_hdl);
  6109. rc = -EFAULT;
  6110. goto free_req_and_buf_tracker_list;
  6111. }
  6112. hw_update_data = cfg.priv;
  6113. req_isp->num_cfg = cfg.num_hw_update_entries;
  6114. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  6115. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  6116. req_isp->num_acked = 0;
  6117. req_isp->num_deferred_acks = 0;
  6118. req_isp->bubble_detected = false;
  6119. req_isp->cdm_reset_before_apply = false;
  6120. req_isp->hw_update_data.packet = packet;
  6121. req_isp->hw_update_data.num_exp = hw_update_data->num_exp;
  6122. req_isp->hw_update_data.mup_en = hw_update_data->mup_en;
  6123. req->pf_data.packet_handle = cmd->packet_handle;
  6124. req->pf_data.packet_offset = cmd->offset;
  6125. req->pf_data.req = req;
  6126. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  6127. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  6128. if (rc) {
  6129. CAM_ERR(CAM_ISP, "Can't get ref for fence %d, ctx_idx: %u, link: 0x%x",
  6130. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6131. goto put_ref;
  6132. }
  6133. }
  6134. CAM_DBG(CAM_ISP,
  6135. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d, ctx: %u, link: 0x%x",
  6136. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  6137. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in,
  6138. ctx->ctx_id, ctx->link_hdl);
  6139. req->request_id = packet->header.request_id;
  6140. req->status = 1;
  6141. if (req_isp->hw_update_data.packet_opcode_type ==
  6142. CAM_ISP_PACKET_INIT_DEV) {
  6143. if (ctx->state < CAM_CTX_ACTIVATED) {
  6144. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  6145. if (rc)
  6146. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed, ctx: %u, link: 0x%x",
  6147. ctx->ctx_id, ctx->link_hdl);
  6148. ctx_isp->init_received = true;
  6149. if ((ctx_isp->vfps_aux_context) && (req->request_id > 0))
  6150. ctx_isp->resume_hw_in_flushed = true;
  6151. else
  6152. ctx_isp->resume_hw_in_flushed = false;
  6153. } else {
  6154. rc = -EINVAL;
  6155. CAM_ERR(CAM_ISP, "Received INIT pkt in wrong state:%d, ctx:%u, link:0x%x",
  6156. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6157. }
  6158. } else {
  6159. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  6160. rc = -EINVAL;
  6161. CAM_ERR(CAM_ISP,
  6162. "Received update req %lld in wrong state:%d, ctx_idx: %u, link: 0x%x",
  6163. req->request_id, ctx->state, ctx->ctx_id, ctx->link_hdl);
  6164. goto put_ref;
  6165. }
  6166. if ((ctx_isp->offline_context) || (ctx_isp->vfps_aux_context)) {
  6167. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6168. } else if (ctx->ctx_crm_intf->add_req) {
  6169. memset(&add_req, 0, sizeof(add_req));
  6170. add_req.link_hdl = ctx->link_hdl;
  6171. add_req.dev_hdl = ctx->dev_hdl;
  6172. add_req.req_id = req->request_id;
  6173. add_req.num_exp = ctx_isp->last_num_exp;
  6174. if (req_isp->hw_update_data.mup_en) {
  6175. add_req.num_exp = req_isp->hw_update_data.num_exp;
  6176. ctx_isp->last_num_exp = add_req.num_exp;
  6177. }
  6178. rc = ctx->ctx_crm_intf->add_req(&add_req);
  6179. if (rc) {
  6180. if (rc == -EBADR)
  6181. CAM_INFO(CAM_ISP,
  6182. "Add req failed: req id=%llu, it has been flushed on link 0x%x ctx %u",
  6183. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6184. else
  6185. CAM_ERR(CAM_ISP,
  6186. "Add req failed: req id=%llu on link 0x%x ctx %u",
  6187. req->request_id, ctx->link_hdl, ctx->ctx_id);
  6188. } else {
  6189. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  6190. }
  6191. } else {
  6192. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu,ctx: %u,link: 0x%x",
  6193. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6194. rc = -ENODEV;
  6195. }
  6196. }
  6197. if (rc)
  6198. goto put_ref;
  6199. CAM_DBG(CAM_REQ,
  6200. "Preprocessing Config req_id %lld successful on ctx %u, link: 0x%x",
  6201. req->request_id, ctx->ctx_id, ctx->link_hdl);
  6202. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch))
  6203. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6204. else if (ctx_isp->vfps_aux_context &&
  6205. (req_isp->hw_update_data.packet_opcode_type != CAM_ISP_PACKET_INIT_DEV))
  6206. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  6207. return rc;
  6208. put_ref:
  6209. for (--i; i >= 0; i--) {
  6210. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  6211. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d, ctx_idx: %u, link: 0x%x",
  6212. req_isp->fence_map_out[i].sync_id, ctx->ctx_id, ctx->link_hdl);
  6213. }
  6214. free_req_and_buf_tracker_list:
  6215. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  6216. free_req:
  6217. spin_lock_bh(&ctx->lock);
  6218. list_add_tail(&req->list, &ctx->free_req_list);
  6219. spin_unlock_bh(&ctx->lock);
  6220. return rc;
  6221. }
  6222. static int __cam_isp_ctx_allocate_mem_hw_entries(
  6223. struct cam_context *ctx,
  6224. struct cam_hw_acquire_args *param)
  6225. {
  6226. int rc = 0, i;
  6227. uint32_t max_res = 0;
  6228. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  6229. struct cam_ctx_request *req;
  6230. struct cam_ctx_request *temp_req;
  6231. struct cam_isp_ctx_req *req_isp;
  6232. if (!param->op_params.param_list[0])
  6233. max_res = CAM_ISP_CTX_RES_MAX;
  6234. else {
  6235. max_res = param->op_params.param_list[0];
  6236. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  6237. max_res += param->op_params.param_list[1];
  6238. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  6239. }
  6240. }
  6241. ctx->max_in_map_entries = max_res;
  6242. ctx->max_out_map_entries = max_res;
  6243. ctx->max_hw_update_entries = max_hw_upd_entries;
  6244. CAM_DBG(CAM_ISP,
  6245. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d, ctx: %u, link: 0x%x",
  6246. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN),
  6247. ctx->ctx_id, ctx->link_hdl);
  6248. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  6249. GFP_KERNEL);
  6250. if (!ctx->hw_update_entry) {
  6251. CAM_ERR(CAM_CTXT, "%s[%u] no memory, link: 0x%x",
  6252. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6253. return -ENOMEM;
  6254. }
  6255. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6256. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  6257. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  6258. if (!ctx->hw_update_entry[i]) {
  6259. CAM_ERR(CAM_CTXT, "%s[%u] no memory for hw_update_entry: %u, link: 0x%x",
  6260. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6261. return -ENOMEM;
  6262. }
  6263. }
  6264. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6265. GFP_KERNEL);
  6266. if (!ctx->in_map_entries) {
  6267. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries, link: 0x%x",
  6268. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6269. rc = -ENOMEM;
  6270. goto end;
  6271. }
  6272. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6273. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  6274. sizeof(struct cam_hw_fence_map_entry),
  6275. GFP_KERNEL);
  6276. if (!ctx->in_map_entries[i]) {
  6277. CAM_ERR(CAM_CTXT, "%s[%u] no memory for in_map_entries: %u, link: 0x%x",
  6278. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6279. rc = -ENOMEM;
  6280. goto end;
  6281. }
  6282. }
  6283. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  6284. GFP_KERNEL);
  6285. if (!ctx->out_map_entries) {
  6286. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries, link: 0x%x",
  6287. ctx->dev_name, ctx->ctx_id, ctx->link_hdl);
  6288. rc = -ENOMEM;
  6289. goto end;
  6290. }
  6291. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6292. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  6293. sizeof(struct cam_hw_fence_map_entry),
  6294. GFP_KERNEL);
  6295. if (!ctx->out_map_entries[i]) {
  6296. CAM_ERR(CAM_CTXT, "%s[%u] no memory for out_map_entries: %u, link: 0x%x",
  6297. ctx->dev_name, ctx->ctx_id, i, ctx->link_hdl);
  6298. rc = -ENOMEM;
  6299. goto end;
  6300. }
  6301. }
  6302. list_for_each_entry_safe(req, temp_req,
  6303. &ctx->free_req_list, list) {
  6304. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6305. req_isp->cfg = ctx->hw_update_entry[req->index];
  6306. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  6307. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  6308. }
  6309. return rc;
  6310. end:
  6311. __cam_isp_ctx_free_mem_hw_entries(ctx);
  6312. return rc;
  6313. }
  6314. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  6315. struct cam_acquire_dev_cmd *cmd)
  6316. {
  6317. int rc = 0;
  6318. int i;
  6319. struct cam_hw_acquire_args param;
  6320. struct cam_isp_resource *isp_res = NULL;
  6321. struct cam_create_dev_hdl req_hdl_param;
  6322. struct cam_hw_release_args release;
  6323. struct cam_isp_context *ctx_isp =
  6324. (struct cam_isp_context *) ctx->ctx_priv;
  6325. struct cam_hw_cmd_args hw_cmd_args;
  6326. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6327. if (!ctx->hw_mgr_intf) {
  6328. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_idx: %u, link: 0x%x",
  6329. ctx->ctx_id, ctx->link_hdl);
  6330. rc = -EFAULT;
  6331. goto end;
  6332. }
  6333. CAM_DBG(CAM_ISP,
  6334. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld, ctx_idx: %u, link: 0x%x",
  6335. cmd->session_handle, cmd->num_resources,
  6336. cmd->handle_type, cmd->resource_hdl, ctx->ctx_id, ctx->link_hdl);
  6337. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  6338. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  6339. ctx_isp->split_acquire = true;
  6340. CAM_DBG(CAM_ISP, "Acquire dev handle, ctx_idx: %u, link: 0x%x",
  6341. ctx->ctx_id, ctx->link_hdl);
  6342. goto get_dev_handle;
  6343. }
  6344. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  6345. CAM_ERR(CAM_ISP, "Too much resources in the acquire, ctx_idx: %u, link: 0x%x",
  6346. ctx->ctx_id, ctx->link_hdl);
  6347. rc = -ENOMEM;
  6348. goto end;
  6349. }
  6350. /* for now we only support user pointer */
  6351. if (cmd->handle_type != 1) {
  6352. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_idx: %u, link: 0x%x",
  6353. ctx->ctx_id, ctx->link_hdl);
  6354. rc = -EINVAL;
  6355. goto end;
  6356. }
  6357. isp_res = kzalloc(
  6358. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  6359. if (!isp_res) {
  6360. rc = -ENOMEM;
  6361. goto end;
  6362. }
  6363. CAM_DBG(CAM_ISP, "start copy %d resources from user, ctx_idx: %u, link: 0x%x",
  6364. cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6365. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  6366. sizeof(*isp_res)*cmd->num_resources)) {
  6367. rc = -EFAULT;
  6368. goto free_res;
  6369. }
  6370. memset(&param, 0, sizeof(param));
  6371. param.context_data = ctx;
  6372. param.event_cb = ctx->irq_cb_intf;
  6373. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6374. param.num_acq = cmd->num_resources;
  6375. param.acquire_info = (uintptr_t) isp_res;
  6376. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6377. if (rc) {
  6378. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6379. ctx->ctx_id, ctx->link_hdl);
  6380. goto free_res;
  6381. }
  6382. /* call HW manager to reserve the resource */
  6383. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6384. &param);
  6385. if (rc != 0) {
  6386. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6387. ctx->ctx_id, ctx->link_hdl);
  6388. goto free_res;
  6389. }
  6390. /* Query the context has rdi only resource */
  6391. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6392. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6393. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6394. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6395. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6396. &hw_cmd_args);
  6397. if (rc) {
  6398. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6399. ctx->ctx_id, ctx->link_hdl);
  6400. goto free_hw;
  6401. }
  6402. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6403. /*
  6404. * this context has rdi only resource assign rdi only
  6405. * state machine
  6406. */
  6407. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6408. ctx->ctx_id, ctx->link_hdl);
  6409. ctx_isp->substate_machine_irq =
  6410. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6411. ctx_isp->substate_machine =
  6412. cam_isp_ctx_rdi_only_activated_state_machine;
  6413. ctx_isp->rdi_only_context = true;
  6414. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6415. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6416. ctx->ctx_id, ctx->link_hdl);
  6417. ctx_isp->substate_machine_irq =
  6418. cam_isp_ctx_fs2_state_machine_irq;
  6419. ctx_isp->substate_machine =
  6420. cam_isp_ctx_fs2_state_machine;
  6421. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6422. CAM_DBG(CAM_ISP,
  6423. "offline Session has PIX and RD resources, ctx_idx: %u, link: 0x%x",
  6424. ctx->ctx_id, ctx->link_hdl);
  6425. ctx_isp->substate_machine_irq =
  6426. cam_isp_ctx_offline_state_machine_irq;
  6427. } else {
  6428. CAM_DBG(CAM_ISP,
  6429. "Session has PIX or PIX and RDI resources, ctx_idx: %u, link: 0x%x",
  6430. ctx->ctx_id, ctx->link_hdl);
  6431. ctx_isp->substate_machine_irq =
  6432. cam_isp_ctx_activated_state_machine_irq;
  6433. ctx_isp->substate_machine =
  6434. cam_isp_ctx_activated_state_machine;
  6435. }
  6436. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6437. ctx_isp->hw_acquired = true;
  6438. ctx_isp->split_acquire = false;
  6439. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6440. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6441. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6442. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6443. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6444. CAM_INFO(CAM_ISP, "Ctx_type: %u, ctx_id: %u, hw_mgr_ctx: %u", isp_hw_cmd_args.u.ctx_type,
  6445. ctx->ctx_id, param.hw_mgr_ctx_id);
  6446. kfree(isp_res);
  6447. isp_res = NULL;
  6448. get_dev_handle:
  6449. req_hdl_param.session_hdl = cmd->session_handle;
  6450. /* bridge is not ready for these flags. so false for now */
  6451. req_hdl_param.v4l2_sub_dev_flag = 0;
  6452. req_hdl_param.media_entity_flag = 0;
  6453. req_hdl_param.ops = ctx->crm_ctx_intf;
  6454. req_hdl_param.priv = ctx;
  6455. req_hdl_param.dev_id = CAM_ISP;
  6456. CAM_DBG(CAM_ISP, "get device handle form bridge, ctx_idx: %u, link: 0x%x",
  6457. ctx->ctx_id, ctx->link_hdl);
  6458. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  6459. if (ctx->dev_hdl <= 0) {
  6460. rc = -EFAULT;
  6461. CAM_ERR(CAM_ISP, "Can not create device handle, ctx_idx: %u, link: 0x%x",
  6462. ctx->ctx_id, ctx->link_hdl);
  6463. goto free_hw;
  6464. }
  6465. cmd->dev_handle = ctx->dev_hdl;
  6466. /* store session information */
  6467. ctx->session_hdl = cmd->session_handle;
  6468. ctx->state = CAM_CTX_ACQUIRED;
  6469. trace_cam_context_state("ISP", ctx);
  6470. CAM_INFO(CAM_ISP,
  6471. "Acquire success: session_hdl 0x%x num_rsrces %d ctx %u link: 0x%x",
  6472. cmd->session_handle, cmd->num_resources, ctx->ctx_id, ctx->link_hdl);
  6473. return rc;
  6474. free_hw:
  6475. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6476. if (ctx_isp->hw_acquired)
  6477. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  6478. &release);
  6479. ctx_isp->hw_ctx = NULL;
  6480. ctx_isp->hw_acquired = false;
  6481. free_res:
  6482. kfree(isp_res);
  6483. end:
  6484. return rc;
  6485. }
  6486. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  6487. void *args)
  6488. {
  6489. int rc = 0;
  6490. int i;
  6491. struct cam_acquire_hw_cmd_v1 *cmd =
  6492. (struct cam_acquire_hw_cmd_v1 *)args;
  6493. struct cam_hw_acquire_args param;
  6494. struct cam_hw_release_args release;
  6495. struct cam_isp_context *ctx_isp =
  6496. (struct cam_isp_context *) ctx->ctx_priv;
  6497. struct cam_hw_cmd_args hw_cmd_args;
  6498. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6499. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6500. if (!ctx->hw_mgr_intf) {
  6501. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx %u link: 0x%x",
  6502. ctx->ctx_id, ctx->link_hdl);
  6503. rc = -EFAULT;
  6504. goto end;
  6505. }
  6506. CAM_DBG(CAM_ISP,
  6507. "session_hdl 0x%x, hdl type %d, res %lld ctx %u link: 0x%x",
  6508. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6509. ctx->ctx_id, ctx->link_hdl);
  6510. /* for now we only support user pointer */
  6511. if (cmd->handle_type != 1) {
  6512. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx %u link: 0x%x",
  6513. ctx->ctx_id, ctx->link_hdl);
  6514. rc = -EINVAL;
  6515. goto end;
  6516. }
  6517. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6518. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx %u link: 0x%x",
  6519. ctx->ctx_id, ctx->link_hdl);
  6520. goto end;
  6521. }
  6522. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6523. if (!acquire_hw_info) {
  6524. rc = -ENOMEM;
  6525. goto end;
  6526. }
  6527. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_idx: %u, link: 0x%x",
  6528. ctx->ctx_id, ctx->link_hdl);
  6529. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6530. cmd->data_size)) {
  6531. rc = -EFAULT;
  6532. goto free_res;
  6533. }
  6534. memset(&param, 0, sizeof(param));
  6535. param.context_data = ctx;
  6536. param.event_cb = ctx->irq_cb_intf;
  6537. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6538. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6539. param.acquire_info_size = cmd->data_size;
  6540. param.acquire_info = (uint64_t) acquire_hw_info;
  6541. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6542. param.link_hdl = ctx->link_hdl;
  6543. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  6544. &param);
  6545. if (rc) {
  6546. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6547. ctx->ctx_id, ctx->link_hdl);
  6548. goto free_res;
  6549. }
  6550. /* call HW manager to reserve the resource */
  6551. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6552. &param);
  6553. if (rc != 0) {
  6554. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_idx: %u, link: 0x%x",
  6555. ctx->ctx_id, ctx->link_hdl);
  6556. goto free_res;
  6557. }
  6558. ctx_isp->last_num_exp = 0;
  6559. ctx_isp->support_consumed_addr =
  6560. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6561. ctx_isp->is_tfe_shdr = (param.op_flags & CAM_IFE_CTX_SHDR_EN);
  6562. ctx_isp->is_shdr_master = (param.op_flags & CAM_IFE_CTX_SHDR_IS_MASTER);
  6563. /* Query the context has rdi only resource */
  6564. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6565. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6566. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6567. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6568. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6569. &hw_cmd_args);
  6570. if (rc) {
  6571. CAM_ERR(CAM_ISP, "HW command failed, ctx_idx: %u, link: 0x%x",
  6572. ctx->ctx_id, ctx->link_hdl);
  6573. goto free_hw;
  6574. }
  6575. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6576. /*
  6577. * this context has rdi only resource assign rdi only
  6578. * state machine
  6579. */
  6580. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_idx: %u, link: 0x%x",
  6581. ctx->ctx_id, ctx->link_hdl);
  6582. ctx_isp->substate_machine_irq =
  6583. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6584. ctx_isp->substate_machine =
  6585. cam_isp_ctx_rdi_only_activated_state_machine;
  6586. ctx_isp->rdi_only_context = true;
  6587. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6588. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_idx: %u, link: 0x%x",
  6589. ctx->ctx_id, ctx->link_hdl);
  6590. ctx_isp->substate_machine_irq =
  6591. cam_isp_ctx_fs2_state_machine_irq;
  6592. ctx_isp->substate_machine =
  6593. cam_isp_ctx_fs2_state_machine;
  6594. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6595. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources, ctx: %u, link: 0x%x",
  6596. ctx->ctx_id, ctx->link_hdl);
  6597. ctx_isp->substate_machine_irq =
  6598. cam_isp_ctx_offline_state_machine_irq;
  6599. ctx_isp->substate_machine = NULL;
  6600. } else {
  6601. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx: %u, link: 0x%x",
  6602. ctx->ctx_id, ctx->link_hdl);
  6603. ctx_isp->substate_machine_irq =
  6604. cam_isp_ctx_activated_state_machine_irq;
  6605. ctx_isp->substate_machine =
  6606. cam_isp_ctx_activated_state_machine;
  6607. }
  6608. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6609. ctx_isp->hw_acquired = true;
  6610. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6611. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  6612. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  6613. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6614. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  6615. trace_cam_context_state("ISP", ctx);
  6616. CAM_INFO(CAM_ISP,
  6617. "Acquire success:session_hdl 0x%xs ctx_type %d ctx %u link: 0x%x hw_mgr_ctx: %u is_shdr %d is_shdr_master %d",
  6618. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6619. param.hw_mgr_ctx_id, ctx_isp->is_tfe_shdr, ctx_isp->is_shdr_master);
  6620. kfree(acquire_hw_info);
  6621. return rc;
  6622. free_hw:
  6623. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6624. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6625. ctx_isp->hw_ctx = NULL;
  6626. ctx_isp->hw_acquired = false;
  6627. free_res:
  6628. kfree(acquire_hw_info);
  6629. end:
  6630. return rc;
  6631. }
  6632. static void cam_req_mgr_process_workq_apply_req_worker(struct work_struct *w)
  6633. {
  6634. cam_req_mgr_process_workq(w);
  6635. }
  6636. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  6637. void *args)
  6638. {
  6639. int rc = 0, i, j;
  6640. struct cam_acquire_hw_cmd_v2 *cmd =
  6641. (struct cam_acquire_hw_cmd_v2 *)args;
  6642. struct cam_hw_acquire_args param;
  6643. struct cam_hw_release_args release;
  6644. struct cam_isp_context *ctx_isp =
  6645. (struct cam_isp_context *) ctx->ctx_priv;
  6646. struct cam_hw_cmd_args hw_cmd_args;
  6647. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6648. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  6649. struct cam_isp_comp_record_query query_cmd;
  6650. if (!ctx->hw_mgr_intf) {
  6651. CAM_ERR(CAM_ISP, "HW interface is not ready, ctx_id %u link: 0x%x",
  6652. ctx->ctx_id, ctx->link_hdl);
  6653. rc = -EFAULT;
  6654. goto end;
  6655. }
  6656. CAM_DBG(CAM_ISP,
  6657. "session_hdl 0x%x, hdl type %d, res %lld, ctx_id %u link: 0x%x",
  6658. cmd->session_handle, cmd->handle_type, cmd->resource_hdl,
  6659. ctx->ctx_id, ctx->link_hdl);
  6660. /* for now we only support user pointer */
  6661. if (cmd->handle_type != 1) {
  6662. CAM_ERR(CAM_ISP, "Only user pointer is supported, ctx_id %u link: 0x%x",
  6663. ctx->ctx_id, ctx->link_hdl);
  6664. rc = -EINVAL;
  6665. goto end;
  6666. }
  6667. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  6668. CAM_ERR(CAM_ISP, "data_size is not a valid value, ctx_id %u link: 0x%x",
  6669. ctx->ctx_id, ctx->link_hdl);
  6670. goto end;
  6671. }
  6672. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  6673. if (!acquire_hw_info) {
  6674. rc = -ENOMEM;
  6675. goto end;
  6676. }
  6677. CAM_DBG(CAM_ISP, "start copy resources from user, ctx_id %u link: 0x%x",
  6678. ctx->ctx_id, ctx->link_hdl);
  6679. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  6680. cmd->data_size)) {
  6681. rc = -EFAULT;
  6682. goto free_res;
  6683. }
  6684. memset(&param, 0, sizeof(param));
  6685. param.context_data = ctx;
  6686. param.event_cb = ctx->irq_cb_intf;
  6687. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  6688. param.num_acq = CAM_API_COMPAT_CONSTANT;
  6689. param.acquire_info_size = cmd->data_size;
  6690. param.acquire_info = (uint64_t) acquire_hw_info;
  6691. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  6692. param.link_hdl = ctx->link_hdl;
  6693. /* call HW manager to reserve the resource */
  6694. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  6695. &param);
  6696. if (rc != 0) {
  6697. CAM_ERR(CAM_ISP, "Acquire device failed, ctx_id %u link: 0x%x",
  6698. ctx->ctx_id, ctx->link_hdl);
  6699. goto free_res;
  6700. }
  6701. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  6702. if (rc) {
  6703. CAM_ERR(CAM_ISP, "Ctx[%u] link: 0x%x allocate hw entry fail",
  6704. ctx->ctx_id, ctx->link_hdl);
  6705. goto free_hw;
  6706. }
  6707. /*
  6708. * Set feature flag if applicable
  6709. * custom hw is supported only on v2
  6710. */
  6711. ctx_isp->last_num_exp = 0;
  6712. ctx_isp->custom_enabled =
  6713. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  6714. ctx_isp->use_frame_header_ts =
  6715. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  6716. ctx_isp->use_default_apply =
  6717. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  6718. ctx_isp->support_consumed_addr =
  6719. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  6720. ctx_isp->aeb_enabled =
  6721. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  6722. ctx_isp->mode_switch_en =
  6723. (param.op_flags & CAM_IFE_CTX_DYNAMIC_SWITCH_EN);
  6724. ctx_isp->is_tfe_shdr = (param.op_flags & CAM_IFE_CTX_SHDR_EN);
  6725. ctx_isp->is_shdr_master = (param.op_flags & CAM_IFE_CTX_SHDR_IS_MASTER);
  6726. /* Query the context bus comp group information */
  6727. ctx_isp->vfe_bus_comp_grp = kcalloc(CAM_IFE_BUS_COMP_NUM_MAX,
  6728. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6729. if (!ctx_isp->vfe_bus_comp_grp) {
  6730. CAM_ERR(CAM_CTXT, "%s[%d] no memory for vfe_bus_comp_grp",
  6731. ctx->dev_name, ctx->ctx_id);
  6732. rc = -ENOMEM;
  6733. goto end;
  6734. }
  6735. if (param.op_flags & CAM_IFE_CTX_SFE_EN) {
  6736. ctx_isp->sfe_bus_comp_grp = kcalloc(CAM_SFE_BUS_COMP_NUM_MAX,
  6737. sizeof(struct cam_isp_context_comp_record), GFP_KERNEL);
  6738. if (!ctx_isp->sfe_bus_comp_grp) {
  6739. CAM_ERR(CAM_CTXT, "%s[%d] no memory for sfe_bus_comp_grp",
  6740. ctx->dev_name, ctx->ctx_id);
  6741. rc = -ENOMEM;
  6742. goto end;
  6743. }
  6744. }
  6745. query_cmd.vfe_bus_comp_grp = ctx_isp->vfe_bus_comp_grp;
  6746. if (ctx_isp->sfe_bus_comp_grp)
  6747. query_cmd.sfe_bus_comp_grp = ctx_isp->sfe_bus_comp_grp;
  6748. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6749. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6750. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_BUS_COMP_GROUP;
  6751. isp_hw_cmd_args.cmd_data = &query_cmd;
  6752. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6753. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6754. &hw_cmd_args);
  6755. if (rc) {
  6756. CAM_ERR(CAM_ISP, "HW command failed");
  6757. goto free_hw;
  6758. }
  6759. /* Query the context has rdi only resource */
  6760. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  6761. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6762. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  6763. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6764. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6765. &hw_cmd_args);
  6766. if (rc) {
  6767. CAM_ERR(CAM_ISP, "HW command failed, ctx_id %u link: 0x%x",
  6768. ctx->ctx_id, ctx->link_hdl);
  6769. goto free_hw;
  6770. }
  6771. if (param.valid_acquired_hw) {
  6772. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6773. cmd->hw_info.acquired_hw_id[i] =
  6774. param.acquired_hw_id[i];
  6775. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  6776. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  6777. cmd->hw_info.acquired_hw_path[i][j] =
  6778. param.acquired_hw_path[i][j];
  6779. ctx_isp->hw_idx = param.acquired_hw_id[0];
  6780. }
  6781. cmd->hw_info.valid_acquired_hw =
  6782. param.valid_acquired_hw;
  6783. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  6784. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  6785. /*
  6786. * this context has rdi only resource assign rdi only
  6787. * state machine
  6788. */
  6789. CAM_DBG(CAM_ISP, "RDI only session Context, ctx_id %u link: 0x%x",
  6790. ctx->ctx_id, ctx->link_hdl);
  6791. ctx_isp->substate_machine_irq =
  6792. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  6793. ctx_isp->substate_machine =
  6794. cam_isp_ctx_rdi_only_activated_state_machine;
  6795. ctx_isp->rdi_only_context = true;
  6796. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  6797. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI, ctx_id %u link: 0x%x",
  6798. ctx->ctx_id, ctx->link_hdl);
  6799. ctx_isp->substate_machine_irq =
  6800. cam_isp_ctx_fs2_state_machine_irq;
  6801. ctx_isp->substate_machine =
  6802. cam_isp_ctx_fs2_state_machine;
  6803. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  6804. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources, ctx_id %u link: 0x%x",
  6805. ctx->ctx_id, ctx->link_hdl);
  6806. ctx_isp->substate_machine_irq =
  6807. cam_isp_ctx_offline_state_machine_irq;
  6808. ctx_isp->substate_machine = NULL;
  6809. ctx_isp->offline_context = true;
  6810. } else {
  6811. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources, ctx_id %u link: 0x%x",
  6812. ctx->ctx_id, ctx->link_hdl);
  6813. ctx_isp->substate_machine_irq =
  6814. cam_isp_ctx_activated_state_machine_irq;
  6815. ctx_isp->substate_machine =
  6816. cam_isp_ctx_activated_state_machine;
  6817. }
  6818. if (ctx_isp->offline_context || ctx_isp->vfps_aux_context) {
  6819. rc = cam_req_mgr_workq_create("ife_apply_req", 20,
  6820. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  6821. cam_req_mgr_process_workq_apply_req_worker);
  6822. if (rc)
  6823. CAM_ERR(CAM_ISP,
  6824. "Failed to create workq for IFE rc:%d offline: %s vfps: %s ctx_id %u link: 0x%x",
  6825. rc, CAM_BOOL_TO_YESNO(ctx_isp->offline_context),
  6826. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6827. ctx->ctx_id, ctx->link_hdl);
  6828. }
  6829. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  6830. ctx_isp->hw_acquired = true;
  6831. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  6832. ctx->hw_mgr_ctx_id = param.hw_mgr_ctx_id;
  6833. snprintf(ctx->ctx_id_string, sizeof(ctx->ctx_id_string),
  6834. "%s_ctx[%d]_hwmgrctx[%d]_hwidx[0x%x]",
  6835. ctx->dev_name,
  6836. ctx->ctx_id,
  6837. ctx->hw_mgr_ctx_id,
  6838. ctx_isp->hw_idx);
  6839. trace_cam_context_state("ISP", ctx);
  6840. CAM_INFO(CAM_ISP,
  6841. "Acquire success: session_hdl 0x%xs ctx_type %d ctx %u link 0x%x hw_mgr_ctx %u is_shdr %d is_shdr_master %d",
  6842. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id, ctx->link_hdl,
  6843. param.hw_mgr_ctx_id, ctx_isp->is_tfe_shdr, ctx_isp->is_shdr_master);
  6844. kfree(acquire_hw_info);
  6845. return rc;
  6846. free_hw:
  6847. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6848. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  6849. ctx_isp->hw_ctx = NULL;
  6850. ctx_isp->hw_acquired = false;
  6851. free_res:
  6852. kfree(acquire_hw_info);
  6853. end:
  6854. return rc;
  6855. }
  6856. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  6857. void *args)
  6858. {
  6859. int rc = -EINVAL;
  6860. uint32_t api_version;
  6861. if (!ctx || !args) {
  6862. CAM_ERR(CAM_ISP, "Invalid input pointer");
  6863. return rc;
  6864. }
  6865. api_version = *((uint32_t *)args);
  6866. if (api_version == 1)
  6867. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  6868. else if (api_version == 2)
  6869. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  6870. else
  6871. CAM_ERR(CAM_ISP, "Unsupported api version %d, ctx_id %u link: 0x%x",
  6872. api_version, ctx->ctx_id, ctx->link_hdl);
  6873. return rc;
  6874. }
  6875. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  6876. struct cam_config_dev_cmd *cmd)
  6877. {
  6878. int rc = 0;
  6879. struct cam_isp_context *ctx_isp =
  6880. (struct cam_isp_context *) ctx->ctx_priv;
  6881. if (!ctx_isp->hw_acquired) {
  6882. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6883. ctx->ctx_id, ctx->link_hdl);
  6884. return -EINVAL;
  6885. }
  6886. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6887. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  6888. ctx->state = CAM_CTX_READY;
  6889. trace_cam_context_state("ISP", ctx);
  6890. }
  6891. CAM_DBG(CAM_ISP, "next state %d, ctx %u link: 0x%x",
  6892. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6893. return rc;
  6894. }
  6895. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  6896. struct cam_config_dev_cmd *cmd)
  6897. {
  6898. int rc = 0;
  6899. struct cam_start_stop_dev_cmd start_cmd;
  6900. struct cam_hw_cmd_args hw_cmd_args;
  6901. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6902. struct cam_isp_context *ctx_isp =
  6903. (struct cam_isp_context *) ctx->ctx_priv;
  6904. if (!ctx_isp->hw_acquired) {
  6905. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet, ctx_id %u link: 0x%x",
  6906. ctx->ctx_id, ctx->link_hdl);
  6907. rc = -EINVAL;
  6908. goto end;
  6909. }
  6910. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6911. if (rc)
  6912. goto end;
  6913. if (!ctx_isp->init_received) {
  6914. CAM_WARN(CAM_ISP,
  6915. "Received update pckt in flushed state, skip start, ctx %u link: 0x%x",
  6916. ctx->ctx_id, ctx->link_hdl);
  6917. goto end;
  6918. }
  6919. CAM_DBG(CAM_ISP, "vfps_ctx:%s resume_hw_in_flushed:%d ctx:%u link: 0x%x",
  6920. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6921. ctx_isp->resume_hw_in_flushed,
  6922. ctx->ctx_id, ctx->link_hdl);
  6923. if (ctx_isp->vfps_aux_context) {
  6924. /* Resume the HW only when we get first valid req */
  6925. if (!ctx_isp->resume_hw_in_flushed)
  6926. goto end;
  6927. else
  6928. ctx_isp->resume_hw_in_flushed = false;
  6929. }
  6930. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6931. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6932. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6933. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6934. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6935. &hw_cmd_args);
  6936. if (rc) {
  6937. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d, ctx_id %u link: 0x%x",
  6938. rc, ctx->ctx_id, ctx->link_hdl);
  6939. goto end;
  6940. }
  6941. start_cmd.dev_handle = cmd->dev_handle;
  6942. start_cmd.session_handle = cmd->session_handle;
  6943. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  6944. if (rc)
  6945. CAM_ERR(CAM_ISP,
  6946. "Failed to re-start HW after flush rc: %d, ctx_id %u link: 0x%x",
  6947. rc, ctx->ctx_id, ctx->link_hdl);
  6948. else
  6949. CAM_INFO(CAM_ISP,
  6950. "Received init after flush. Re-start HW complete in ctx:%d, link: 0x%x",
  6951. ctx->ctx_id, ctx->link_hdl);
  6952. end:
  6953. CAM_DBG(CAM_ISP, "next state %d sub_state:%d ctx_id %u link: 0x%x", ctx->state,
  6954. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  6955. return rc;
  6956. }
  6957. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  6958. struct cam_req_mgr_core_dev_link_setup *link)
  6959. {
  6960. int rc = 0;
  6961. struct cam_isp_context *ctx_isp =
  6962. (struct cam_isp_context *) ctx->ctx_priv;
  6963. if (!link) {
  6964. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u link: 0x%x",
  6965. link, ctx->ctx_id, ctx->link_hdl);
  6966. return -EINVAL;
  6967. }
  6968. if (!link->crm_cb) {
  6969. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u, link: 0x%x",
  6970. link->crm_cb, ctx->ctx_id, ctx->link_hdl);
  6971. return -EINVAL;
  6972. }
  6973. CAM_DBG(CAM_ISP, "Enter.........ctx: %u, link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  6974. ctx->link_hdl = link->link_hdl;
  6975. ctx->ctx_crm_intf = link->crm_cb;
  6976. ctx_isp->subscribe_event =
  6977. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  6978. ctx_isp->trigger_id = link->trigger_id;
  6979. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  6980. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  6981. if ((link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1) > 0) {
  6982. ctx_isp->handle_mswitch = true;
  6983. ctx_isp->mswitch_default_apply_delay_max_cnt =
  6984. link->mode_switch_max_delay - CAM_MODESWITCH_DELAY_1;
  6985. CAM_DBG(CAM_ISP,
  6986. "Enabled mode switch handling on ctx: %u max delay cnt: %u",
  6987. ctx->ctx_id, ctx_isp->mswitch_default_apply_delay_max_cnt);
  6988. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt,
  6989. ctx_isp->mswitch_default_apply_delay_max_cnt);
  6990. }
  6991. /* change state only if we had the init config */
  6992. if (ctx_isp->init_received) {
  6993. ctx->state = CAM_CTX_READY;
  6994. trace_cam_context_state("ISP", ctx);
  6995. }
  6996. CAM_DBG(CAM_ISP, "next state %d, ctx: %u, link: 0x%x",
  6997. ctx->state, ctx->ctx_id, ctx->link_hdl);
  6998. return rc;
  6999. }
  7000. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  7001. struct cam_req_mgr_core_dev_link_setup *unlink)
  7002. {
  7003. int rc = 0;
  7004. struct cam_isp_context *ctx_isp =
  7005. (struct cam_isp_context *) ctx->ctx_priv;
  7006. ctx->link_hdl = -1;
  7007. ctx->ctx_crm_intf = NULL;
  7008. ctx_isp->trigger_id = -1;
  7009. ctx_isp->mswitch_default_apply_delay_max_cnt = 0;
  7010. atomic_set(&ctx_isp->mswitch_default_apply_delay_ref_cnt, 0);
  7011. return rc;
  7012. }
  7013. static int __cam_isp_ctx_get_dev_info(struct cam_context *ctx,
  7014. struct cam_req_mgr_device_info *dev_info)
  7015. {
  7016. int rc = 0;
  7017. struct cam_isp_context *ctx_isp =
  7018. (struct cam_isp_context *) ctx->ctx_priv;
  7019. dev_info->dev_hdl = ctx->dev_hdl;
  7020. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  7021. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  7022. dev_info->p_delay = CAM_PIPELINE_DELAY_1;
  7023. dev_info->m_delay = CAM_MODESWITCH_DELAY_1;
  7024. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  7025. dev_info->trigger_on = true;
  7026. dev_info->is_shdr = ctx_isp->is_tfe_shdr;
  7027. dev_info->is_shdr_master = ctx_isp->is_shdr_master;
  7028. return rc;
  7029. }
  7030. static inline void __cam_isp_context_reset_ctx_params(
  7031. struct cam_isp_context *ctx_isp)
  7032. {
  7033. atomic_set(&ctx_isp->process_bubble, 0);
  7034. atomic_set(&ctx_isp->rxd_epoch, 0);
  7035. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7036. ctx_isp->frame_id = 0;
  7037. ctx_isp->sof_timestamp_val = 0;
  7038. ctx_isp->boot_timestamp = 0;
  7039. ctx_isp->active_req_cnt = 0;
  7040. ctx_isp->reported_req_id = 0;
  7041. ctx_isp->reported_frame_id = 0;
  7042. ctx_isp->bubble_frame_cnt = 0;
  7043. ctx_isp->congestion_cnt = 0;
  7044. ctx_isp->recovery_req_id = 0;
  7045. ctx_isp->aeb_error_cnt = 0;
  7046. ctx_isp->out_of_sync_cnt = 0;
  7047. ctx_isp->sof_dbg_irq_en = false;
  7048. ctx_isp->last_sof_jiffies = 0;
  7049. ctx_isp->last_applied_jiffies = 0;
  7050. }
  7051. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  7052. struct cam_start_stop_dev_cmd *cmd)
  7053. {
  7054. int rc = 0;
  7055. int i;
  7056. struct cam_isp_start_args start_isp;
  7057. struct cam_ctx_request *req;
  7058. struct cam_isp_ctx_req *req_isp;
  7059. struct cam_isp_context *ctx_isp =
  7060. (struct cam_isp_context *) ctx->ctx_priv;
  7061. if (cmd->session_handle != ctx->session_hdl ||
  7062. cmd->dev_handle != ctx->dev_hdl) {
  7063. rc = -EPERM;
  7064. goto end;
  7065. }
  7066. if (list_empty(&ctx->pending_req_list)) {
  7067. /* should never happen */
  7068. CAM_ERR(CAM_ISP, "Start device with empty configuration, ctx: %u, link: 0x%x",
  7069. ctx->ctx_id, ctx->link_hdl);
  7070. rc = -EFAULT;
  7071. goto end;
  7072. } else {
  7073. req = list_first_entry(&ctx->pending_req_list,
  7074. struct cam_ctx_request, list);
  7075. }
  7076. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7077. if (!ctx_isp->hw_ctx) {
  7078. CAM_ERR(CAM_ISP, "Wrong hw context pointer.ctx_idx: %u, link: 0x%x",
  7079. ctx->ctx_id, ctx->link_hdl);
  7080. rc = -EFAULT;
  7081. goto end;
  7082. }
  7083. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7084. start_isp.hw_config.request_id = req->request_id;
  7085. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7086. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7087. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7088. start_isp.hw_config.init_packet = 1;
  7089. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  7090. start_isp.hw_config.cdm_reset_before_apply = false;
  7091. start_isp.is_internal_start = false;
  7092. ctx_isp->last_applied_req_id = req->request_id;
  7093. if (ctx->state == CAM_CTX_FLUSHED)
  7094. start_isp.start_only = true;
  7095. else
  7096. start_isp.start_only = false;
  7097. __cam_isp_context_reset_ctx_params(ctx_isp);
  7098. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7099. CAM_ISP_CTX_ACTIVATED_APPLIED :
  7100. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  7101. CAM_ISP_CTX_ACTIVATED_SOF;
  7102. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7103. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7104. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7105. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7106. /*
  7107. * In case of CSID TPG we might receive SOF and RUP IRQs
  7108. * before hw_mgr_intf->hw_start has returned. So move
  7109. * req out of pending list before hw_start and add it
  7110. * back to pending list if hw_start fails.
  7111. */
  7112. list_del_init(&req->list);
  7113. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  7114. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7115. list_add_tail(&req->list, &ctx->free_req_list);
  7116. atomic_set(&ctx_isp->rxd_epoch, 1);
  7117. CAM_DBG(CAM_REQ,
  7118. "Move pending req: %lld to free list(cnt: %d) offline ctx %u link: 0x%x",
  7119. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7120. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  7121. list_add_tail(&req->list, &ctx->wait_req_list);
  7122. CAM_DBG(CAM_REQ,
  7123. "Move pending req: %lld to wait list(cnt: %d) ctx %u link: 0x%x",
  7124. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl);
  7125. } else {
  7126. list_add_tail(&req->list, &ctx->active_req_list);
  7127. ctx_isp->active_req_cnt++;
  7128. CAM_DBG(CAM_REQ,
  7129. "Move pending req: %lld to active list(cnt: %d) ctx %u link: 0x%x offline %d",
  7130. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id, ctx->link_hdl,
  7131. ctx_isp->offline_context);
  7132. }
  7133. /*
  7134. * Only place to change state before calling the hw due to
  7135. * hardware tasklet has higher priority that can cause the
  7136. * irq handling comes early
  7137. */
  7138. ctx->state = CAM_CTX_ACTIVATED;
  7139. trace_cam_context_state("ISP", ctx);
  7140. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7141. &start_isp);
  7142. if (rc) {
  7143. /* HW failure. user need to clean up the resource */
  7144. CAM_ERR(CAM_ISP, "Start HW failed, ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7145. ctx->state = CAM_CTX_READY;
  7146. if ((rc == -ETIMEDOUT) &&
  7147. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  7148. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  7149. trace_cam_context_state("ISP", ctx);
  7150. list_del_init(&req->list);
  7151. list_add(&req->list, &ctx->pending_req_list);
  7152. goto end;
  7153. }
  7154. CAM_DBG(CAM_ISP, "start device success ctx %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7155. end:
  7156. return rc;
  7157. }
  7158. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  7159. struct cam_req_mgr_core_dev_link_setup *unlink)
  7160. {
  7161. int rc = 0;
  7162. ctx->link_hdl = -1;
  7163. ctx->ctx_crm_intf = NULL;
  7164. ctx->state = CAM_CTX_ACQUIRED;
  7165. trace_cam_context_state("ISP", ctx);
  7166. return rc;
  7167. }
  7168. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  7169. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  7170. {
  7171. int rc = 0;
  7172. uint32_t i;
  7173. struct cam_hw_stop_args stop;
  7174. struct cam_ctx_request *req;
  7175. struct cam_isp_ctx_req *req_isp;
  7176. struct cam_isp_context *ctx_isp =
  7177. (struct cam_isp_context *) ctx->ctx_priv;
  7178. struct cam_isp_stop_args stop_isp;
  7179. /* Mask off all the incoming hardware events */
  7180. spin_lock_bh(&ctx->lock);
  7181. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7182. spin_unlock_bh(&ctx->lock);
  7183. /* stop hw first */
  7184. if (ctx_isp->hw_ctx) {
  7185. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7186. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7187. stop_isp.stop_only = false;
  7188. stop_isp.is_internal_stop = false;
  7189. stop.args = (void *) &stop_isp;
  7190. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7191. &stop);
  7192. }
  7193. CAM_DBG(CAM_ISP, "next Substate[%s], ctx_idx: %u, link: 0x%x",
  7194. __cam_isp_ctx_substate_val_to_type(
  7195. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7196. if (ctx->ctx_crm_intf &&
  7197. ctx->ctx_crm_intf->notify_stop) {
  7198. struct cam_req_mgr_notify_stop notify;
  7199. notify.link_hdl = ctx->link_hdl;
  7200. CAM_DBG(CAM_ISP,
  7201. "Notify CRM about device stop ctx %u link 0x%x",
  7202. ctx->ctx_id, ctx->link_hdl);
  7203. ctx->ctx_crm_intf->notify_stop(&notify);
  7204. } else if (!ctx_isp->offline_context)
  7205. CAM_ERR(CAM_ISP, "cb not present, ctx_idx: %u, link: 0x%x",
  7206. ctx->ctx_id, ctx->link_hdl);
  7207. while (!list_empty(&ctx->pending_req_list)) {
  7208. req = list_first_entry(&ctx->pending_req_list,
  7209. struct cam_ctx_request, list);
  7210. list_del_init(&req->list);
  7211. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7212. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7213. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d ctx:%u, link: 0x%x",
  7214. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7215. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7216. if (req_isp->fence_map_out[i].sync_id != -1) {
  7217. cam_sync_signal(
  7218. req_isp->fence_map_out[i].sync_id,
  7219. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7220. CAM_SYNC_ISP_EVENT_HW_STOP);
  7221. }
  7222. list_add_tail(&req->list, &ctx->free_req_list);
  7223. }
  7224. while (!list_empty(&ctx->wait_req_list)) {
  7225. req = list_first_entry(&ctx->wait_req_list,
  7226. struct cam_ctx_request, list);
  7227. list_del_init(&req->list);
  7228. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7229. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7230. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d ctx: %u, link: 0x%x",
  7231. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7232. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7233. if (req_isp->fence_map_out[i].sync_id != -1) {
  7234. cam_sync_signal(
  7235. req_isp->fence_map_out[i].sync_id,
  7236. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7237. CAM_SYNC_ISP_EVENT_HW_STOP);
  7238. }
  7239. list_add_tail(&req->list, &ctx->free_req_list);
  7240. }
  7241. while (!list_empty(&ctx->active_req_list)) {
  7242. req = list_first_entry(&ctx->active_req_list,
  7243. struct cam_ctx_request, list);
  7244. list_del_init(&req->list);
  7245. cam_smmu_buffer_tracker_putref(&req->buf_tracker);
  7246. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7247. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d ctx: %u, link: 0x%x",
  7248. req_isp->num_fence_map_out, ctx->ctx_id, ctx->link_hdl);
  7249. for (i = 0; i < req_isp->num_fence_map_out; i++)
  7250. if (req_isp->fence_map_out[i].sync_id != -1) {
  7251. cam_sync_signal(
  7252. req_isp->fence_map_out[i].sync_id,
  7253. CAM_SYNC_STATE_SIGNALED_CANCEL,
  7254. CAM_SYNC_ISP_EVENT_HW_STOP);
  7255. }
  7256. list_add_tail(&req->list, &ctx->free_req_list);
  7257. }
  7258. ctx_isp->frame_id = 0;
  7259. ctx_isp->active_req_cnt = 0;
  7260. ctx_isp->reported_req_id = 0;
  7261. ctx_isp->reported_frame_id = 0;
  7262. ctx_isp->last_applied_req_id = 0;
  7263. ctx_isp->req_info.last_bufdone_req_id = 0;
  7264. ctx_isp->bubble_frame_cnt = 0;
  7265. ctx_isp->congestion_cnt = 0;
  7266. ctx_isp->sof_dbg_irq_en = false;
  7267. atomic_set(&ctx_isp->process_bubble, 0);
  7268. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7269. atomic_set(&ctx_isp->rxd_epoch, 0);
  7270. atomic64_set(&ctx_isp->dbg_monitors.state_monitor_head, -1);
  7271. atomic64_set(&ctx_isp->dbg_monitors.frame_monitor_head, -1);
  7272. /* Reset skipped_list for FCG config */
  7273. __cam_isp_ctx_reset_fcg_tracker(ctx);
  7274. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7275. atomic64_set(&ctx_isp->dbg_monitors.event_record_head[i], -1);
  7276. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u link: 0x%x",
  7277. ctx->state, ctx->ctx_id, ctx->link_hdl);
  7278. if (!stop_cmd) {
  7279. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  7280. if (rc)
  7281. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx %u link: 0x%x",
  7282. rc, ctx->ctx_id, ctx->link_hdl);
  7283. }
  7284. return rc;
  7285. }
  7286. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  7287. struct cam_start_stop_dev_cmd *cmd)
  7288. {
  7289. int rc = 0;
  7290. struct cam_isp_context *ctx_isp =
  7291. (struct cam_isp_context *)ctx->ctx_priv;
  7292. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  7293. ctx_isp->init_received = false;
  7294. ctx->state = CAM_CTX_ACQUIRED;
  7295. trace_cam_context_state("ISP", ctx);
  7296. return rc;
  7297. }
  7298. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  7299. struct cam_release_dev_cmd *cmd)
  7300. {
  7301. int rc = 0;
  7302. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7303. if (rc)
  7304. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7305. rc, ctx->ctx_id, ctx->link_hdl);
  7306. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  7307. if (rc)
  7308. CAM_ERR(CAM_ISP, "Release device failed rc=%d ctx %u link: 0x%x",
  7309. rc, ctx->ctx_id, ctx->link_hdl);
  7310. return rc;
  7311. }
  7312. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  7313. void *cmd)
  7314. {
  7315. int rc = 0;
  7316. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7317. if (rc)
  7318. CAM_ERR(CAM_ISP, "Stop device failed rc=%d, ctx %u link: 0x%x",
  7319. rc, ctx->ctx_id, ctx->link_hdl);
  7320. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  7321. if (rc)
  7322. CAM_ERR(CAM_ISP, "Release hw failed rc=%d, ctx %u link: 0x%x",
  7323. rc, ctx->ctx_id, ctx->link_hdl);
  7324. return rc;
  7325. }
  7326. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  7327. {
  7328. int rc = 0;
  7329. struct cam_hw_cmd_args hw_cmd_args;
  7330. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7331. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7332. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7333. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  7334. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7335. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7336. &hw_cmd_args);
  7337. return rc;
  7338. }
  7339. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  7340. {
  7341. int rc = 0;
  7342. struct cam_hw_cmd_args hw_cmd_args;
  7343. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7344. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  7345. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7346. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7347. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7348. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7349. &hw_cmd_args);
  7350. return rc;
  7351. }
  7352. static int __cam_isp_ctx_reset_and_recover(
  7353. bool skip_resume, struct cam_context *ctx)
  7354. {
  7355. int rc = 0;
  7356. struct cam_isp_context *ctx_isp =
  7357. (struct cam_isp_context *)ctx->ctx_priv;
  7358. struct cam_isp_stop_args stop_isp;
  7359. struct cam_hw_stop_args stop_args;
  7360. struct cam_isp_start_args start_isp;
  7361. struct cam_hw_cmd_args hw_cmd_args;
  7362. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  7363. struct cam_ctx_request *req;
  7364. struct cam_isp_ctx_req *req_isp;
  7365. spin_lock_bh(&ctx->lock);
  7366. if (ctx_isp->active_req_cnt) {
  7367. spin_unlock_bh(&ctx->lock);
  7368. CAM_WARN(CAM_ISP,
  7369. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  7370. ctx_isp->active_req_cnt, ctx->ctx_id,
  7371. ctx->link_hdl, ctx_isp->recovery_req_id);
  7372. goto end;
  7373. }
  7374. if (ctx->state != CAM_CTX_ACTIVATED) {
  7375. spin_unlock_bh(&ctx->lock);
  7376. CAM_ERR(CAM_ISP,
  7377. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  7378. ctx->state, ctx->ctx_id,
  7379. ctx->link_hdl, ctx_isp->recovery_req_id);
  7380. rc = -EINVAL;
  7381. goto end;
  7382. }
  7383. if (list_empty(&ctx->pending_req_list)) {
  7384. /* Cannot start with no request */
  7385. spin_unlock_bh(&ctx->lock);
  7386. CAM_ERR(CAM_ISP,
  7387. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  7388. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  7389. rc = -EFAULT;
  7390. goto end;
  7391. }
  7392. if (!ctx_isp->hw_ctx) {
  7393. spin_unlock_bh(&ctx->lock);
  7394. CAM_ERR(CAM_ISP,
  7395. "Invalid hw context pointer ctx: %u on link: 0x%x",
  7396. ctx->ctx_id, ctx->link_hdl);
  7397. rc = -EFAULT;
  7398. goto end;
  7399. }
  7400. /* Block all events till HW is resumed */
  7401. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  7402. req = list_first_entry(&ctx->pending_req_list,
  7403. struct cam_ctx_request, list);
  7404. spin_unlock_bh(&ctx->lock);
  7405. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  7406. CAM_INFO(CAM_ISP,
  7407. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  7408. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  7409. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7410. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  7411. stop_isp.stop_only = true;
  7412. stop_isp.is_internal_stop = true;
  7413. stop_args.args = (void *)&stop_isp;
  7414. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  7415. &stop_args);
  7416. if (rc) {
  7417. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u link: 0x%x",
  7418. rc, ctx->ctx_id, ctx->link_hdl);
  7419. goto end;
  7420. }
  7421. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  7422. ctx->ctx_id, ctx->link_hdl);
  7423. /* API provides provision to stream off and not resume as well in case of fatal errors */
  7424. if (skip_resume) {
  7425. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7426. CAM_INFO(CAM_ISP,
  7427. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  7428. ctx->ctx_id, ctx_isp->last_applied_req_id,
  7429. ctx_isp->recovery_req_id, ctx->link_hdl);
  7430. goto end;
  7431. }
  7432. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7433. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  7434. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  7435. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  7436. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  7437. &hw_cmd_args);
  7438. if (rc) {
  7439. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u link: 0x%x",
  7440. rc, ctx->ctx_id, ctx->link_hdl);
  7441. goto end;
  7442. }
  7443. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  7444. ctx->ctx_id, ctx->link_hdl);
  7445. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  7446. start_isp.hw_config.request_id = req->request_id;
  7447. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  7448. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  7449. start_isp.hw_config.priv = &req_isp->hw_update_data;
  7450. start_isp.hw_config.init_packet = 1;
  7451. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  7452. start_isp.hw_config.cdm_reset_before_apply = false;
  7453. start_isp.start_only = true;
  7454. start_isp.is_internal_start = true;
  7455. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  7456. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  7457. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  7458. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  7459. &start_isp);
  7460. if (rc) {
  7461. CAM_ERR(CAM_ISP, "Start HW failed, ctx: %u link: 0x%x", ctx->ctx_id, ctx->link_hdl);
  7462. ctx->state = CAM_CTX_READY;
  7463. goto end;
  7464. }
  7465. /* IQ applied for this request, on next trigger skip IQ cfg */
  7466. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  7467. /* Notify userland that KMD has done internal recovery */
  7468. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY,
  7469. 0, req->request_id, ctx);
  7470. CAM_INFO(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x for req: %llu",
  7471. ctx->ctx_id, ctx->link_hdl, req->request_id);
  7472. end:
  7473. return rc;
  7474. }
  7475. static bool __cam_isp_ctx_try_internal_recovery_for_bubble(
  7476. int64_t error_req_id, struct cam_context *ctx)
  7477. {
  7478. int rc;
  7479. struct cam_isp_context *ctx_isp =
  7480. (struct cam_isp_context *)ctx->ctx_priv;
  7481. if (isp_ctx_debug.disable_internal_recovery_mask &
  7482. CAM_ISP_CTX_DISABLE_RECOVERY_BUBBLE)
  7483. return false;
  7484. /* Perform recovery if bubble recovery is stalled */
  7485. if (!atomic_read(&ctx_isp->process_bubble))
  7486. return false;
  7487. /* Validate if errored request has been applied */
  7488. if (ctx_isp->last_applied_req_id < error_req_id) {
  7489. CAM_WARN(CAM_ISP,
  7490. "Skip trying for internal recovery last applied: %lld error_req: %lld for ctx: %u on link: 0x%x",
  7491. ctx_isp->last_applied_req_id, error_req_id,
  7492. ctx->ctx_id, ctx->link_hdl);
  7493. return false;
  7494. }
  7495. if (__cam_isp_ctx_validate_for_req_reapply_util(ctx_isp)) {
  7496. CAM_WARN(CAM_ISP,
  7497. "Internal recovery not possible for ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7498. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7499. return false;
  7500. }
  7501. /* Trigger reset and recover */
  7502. atomic_set(&ctx_isp->internal_recovery_set, 1);
  7503. rc = __cam_isp_ctx_reset_and_recover(false, ctx);
  7504. if (rc) {
  7505. CAM_WARN(CAM_ISP,
  7506. "Internal recovery failed in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7507. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7508. atomic_set(&ctx_isp->internal_recovery_set, 0);
  7509. goto error;
  7510. }
  7511. CAM_DBG(CAM_ISP,
  7512. "Internal recovery done in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  7513. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  7514. return true;
  7515. error:
  7516. return false;
  7517. }
  7518. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  7519. struct cam_req_mgr_link_evt_data *link_evt_data)
  7520. {
  7521. int rc = 0;
  7522. struct cam_isp_context *ctx_isp =
  7523. (struct cam_isp_context *) ctx->ctx_priv;
  7524. if ((ctx->state == CAM_CTX_ACQUIRED) &&
  7525. (link_evt_data->evt_type != CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES)) {
  7526. CAM_WARN(CAM_ISP,
  7527. "Get unexpect evt:%d in acquired state, ctx: %u on link: 0x%x",
  7528. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7529. return -EINVAL;
  7530. }
  7531. switch (link_evt_data->evt_type) {
  7532. case CAM_REQ_MGR_LINK_EVT_ERR:
  7533. case CAM_REQ_MGR_LINK_EVT_EOF:
  7534. /* No handling */
  7535. break;
  7536. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  7537. rc = __cam_isp_ctx_link_pause(ctx);
  7538. break;
  7539. case CAM_REQ_MGR_LINK_EVT_RESUME:
  7540. rc = __cam_isp_ctx_link_resume(ctx);
  7541. break;
  7542. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  7543. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  7544. break;
  7545. case CAM_REQ_MGR_LINK_EVT_STALLED: {
  7546. bool internal_recovery_skipped = false;
  7547. if (ctx->state == CAM_CTX_ACTIVATED) {
  7548. if (link_evt_data->try_for_recovery)
  7549. internal_recovery_skipped =
  7550. __cam_isp_ctx_try_internal_recovery_for_bubble(
  7551. link_evt_data->req_id, ctx);
  7552. if (!internal_recovery_skipped)
  7553. rc = __cam_isp_ctx_trigger_reg_dump(
  7554. CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  7555. }
  7556. link_evt_data->try_for_recovery = internal_recovery_skipped;
  7557. }
  7558. break;
  7559. case CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES:
  7560. if (link_evt_data->u.properties_mask &
  7561. CAM_LINK_PROPERTY_SENSOR_STANDBY_AFTER_EOF)
  7562. ctx_isp->vfps_aux_context = true;
  7563. else
  7564. ctx_isp->vfps_aux_context = false;
  7565. CAM_DBG(CAM_ISP, "vfps_aux_context:%s on ctx: %u link: 0x%x",
  7566. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context), ctx->ctx_id, ctx->link_hdl);
  7567. break;
  7568. default:
  7569. CAM_WARN(CAM_ISP,
  7570. "Unsupported event type: 0x%x on ctx: %u link: 0x%x",
  7571. link_evt_data->evt_type, ctx->ctx_id, ctx->link_hdl);
  7572. rc = -EINVAL;
  7573. break;
  7574. }
  7575. return rc;
  7576. }
  7577. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  7578. struct cam_req_mgr_core_dev_link_setup *unlink)
  7579. {
  7580. int rc = 0;
  7581. CAM_WARN(CAM_ISP,
  7582. "Received unlink in activated state. It's unexpected, ctx: %u link: 0x%x",
  7583. ctx->ctx_id, ctx->link_hdl);
  7584. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  7585. if (rc)
  7586. CAM_WARN(CAM_ISP, "Stop device failed rc=%d, ctx: %u link: 0x%x",
  7587. rc, ctx->ctx_id, ctx->link_hdl);
  7588. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  7589. if (rc)
  7590. CAM_ERR(CAM_ISP, "Unlink failed rc=%d, ctx: %u link: 0x%x",
  7591. rc, ctx->ctx_id, ctx->link_hdl);
  7592. return rc;
  7593. }
  7594. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  7595. struct cam_req_mgr_apply_request *apply)
  7596. {
  7597. int rc = 0;
  7598. struct cam_ctx_ops *ctx_ops = NULL;
  7599. struct cam_isp_context *ctx_isp =
  7600. (struct cam_isp_context *) ctx->ctx_priv;
  7601. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  7602. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld, ctx: %u link: 0x%x",
  7603. __cam_isp_ctx_substate_val_to_type(
  7604. ctx_isp->substate_activated), apply->request_id, ctx->ctx_id, ctx->link_hdl);
  7605. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  7606. if (ctx_ops->crm_ops.apply_req) {
  7607. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  7608. } else {
  7609. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7610. "No handle function in activated Substate[%s], ctx: %u link: 0x%x",
  7611. __cam_isp_ctx_substate_val_to_type(
  7612. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7613. rc = -EFAULT;
  7614. }
  7615. if (rc)
  7616. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7617. "Apply failed in active Substate[%s] rc %d, ctx: %u link: 0x%x",
  7618. __cam_isp_ctx_substate_val_to_type(
  7619. ctx_isp->substate_activated), rc, ctx->ctx_id, ctx->link_hdl);
  7620. return rc;
  7621. }
  7622. static int __cam_isp_ctx_apply_default_settings(
  7623. struct cam_context *ctx,
  7624. struct cam_req_mgr_apply_request *apply)
  7625. {
  7626. int rc = 0;
  7627. struct cam_ctx_ops *ctx_ops = NULL;
  7628. struct cam_isp_context *ctx_isp =
  7629. (struct cam_isp_context *) ctx->ctx_priv;
  7630. struct cam_isp_fcg_prediction_tracker *fcg_tracker;
  7631. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  7632. CAM_WARN(CAM_ISP,
  7633. "Trigger: %u not subscribed for: %u, ctx: %u link: 0x%x",
  7634. apply->trigger_point, ctx_isp->subscribe_event, ctx->ctx_id,
  7635. ctx->link_hdl);
  7636. return 0;
  7637. }
  7638. /* Allow apply default settings for IFE only at SOF */
  7639. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  7640. return 0;
  7641. if (atomic_read(&ctx_isp->internal_recovery_set))
  7642. return __cam_isp_ctx_reset_and_recover(false, ctx);
  7643. /* FCG handling */
  7644. fcg_tracker = &ctx_isp->fcg_tracker;
  7645. if (ctx_isp->frame_id != 1)
  7646. fcg_tracker->num_skipped += 1;
  7647. CAM_DBG(CAM_ISP,
  7648. "Apply default settings, number of previous continuous skipped frames: %d, ctx_id: %d",
  7649. fcg_tracker->num_skipped, ctx->ctx_id);
  7650. /*
  7651. * Call notify frame skip for static offline cases or
  7652. * mode switch cases where IFE mode switch delay differs
  7653. * from other devices on the link
  7654. */
  7655. if ((ctx_isp->use_default_apply) ||
  7656. (ctx_isp->mode_switch_en && ctx_isp->handle_mswitch)) {
  7657. CAM_DBG(CAM_ISP,
  7658. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  7659. ctx_isp->substate_activated, apply->request_id,
  7660. ctx->ctx_id, ctx->link_hdl);
  7661. ctx_ops = &ctx_isp->substate_machine[
  7662. ctx_isp->substate_activated];
  7663. if (ctx_ops->crm_ops.notify_frame_skip) {
  7664. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  7665. } else {
  7666. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7667. "No handle function in activated substate %d, ctx:%u on link:0x%x",
  7668. ctx_isp->substate_activated, ctx->ctx_id, ctx->link_hdl);
  7669. rc = -EFAULT;
  7670. }
  7671. if (rc)
  7672. CAM_WARN_RATE_LIMIT(CAM_ISP,
  7673. "Apply default failed in active substate %d rc %d ctx: %u link: 0x%x",
  7674. ctx_isp->substate_activated, rc, ctx->ctx_id, ctx->link_hdl);
  7675. }
  7676. return rc;
  7677. }
  7678. void __cam_isp_ctx_notify_cpas(struct cam_context *ctx, uint32_t evt_id)
  7679. {
  7680. uint64_t request_id = 0;
  7681. struct cam_isp_context *ctx_isp =
  7682. (struct cam_isp_context *)ctx->ctx_priv;
  7683. struct cam_ctx_request *req = NULL;
  7684. char ctx_evt_id_string[128];
  7685. switch (evt_id) {
  7686. case CAM_ISP_HW_EVENT_SOF:
  7687. if (!list_empty(&ctx->wait_req_list)) {
  7688. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7689. request_id = req->request_id;
  7690. } else
  7691. request_id = 0;
  7692. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_EPOCH &&
  7693. !list_empty(&ctx->active_req_list)) {
  7694. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7695. request_id = req->request_id;
  7696. CAM_DBG(CAM_ISP, "EPCR notify cpas");
  7697. }
  7698. break;
  7699. case CAM_ISP_HW_EVENT_EOF:
  7700. if (!list_empty(&ctx->active_req_list)) {
  7701. req = list_first_entry(&ctx->active_req_list, struct cam_ctx_request, list);
  7702. request_id = req->request_id;
  7703. } else
  7704. request_id = 0;
  7705. break;
  7706. case CAM_ISP_HW_EVENT_EPOCH:
  7707. if (list_empty(&ctx->wait_req_list)) {
  7708. if (!list_empty(&ctx->active_req_list)) {
  7709. req = list_last_entry(&ctx->active_req_list,
  7710. struct cam_ctx_request, list);
  7711. request_id = req->request_id;
  7712. } else
  7713. request_id = 0;
  7714. } else {
  7715. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  7716. request_id = req->request_id;
  7717. }
  7718. break;
  7719. default:
  7720. return;
  7721. }
  7722. snprintf(ctx_evt_id_string, sizeof(ctx_evt_id_string),
  7723. "%s_frame[%llu]_%s",
  7724. ctx->ctx_id_string,
  7725. ctx_isp->frame_id,
  7726. cam_isp_hw_evt_type_to_string(evt_id));
  7727. CAM_DBG(CAM_ISP, "Substate[%s] ctx: %s frame: %llu evt: %s req: %llu",
  7728. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  7729. ctx->ctx_id_string,
  7730. ctx_isp->frame_id,
  7731. cam_isp_hw_evt_type_to_string(evt_id),
  7732. request_id);
  7733. cam_cpas_notify_event(ctx_evt_id_string, request_id);
  7734. }
  7735. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  7736. uint32_t evt_id, void *evt_data)
  7737. {
  7738. int rc = 0;
  7739. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  7740. struct cam_context *ctx = (struct cam_context *)context;
  7741. struct cam_isp_context *ctx_isp =
  7742. (struct cam_isp_context *)ctx->ctx_priv;
  7743. spin_lock(&ctx->lock);
  7744. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  7745. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  7746. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7747. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7748. ctx_isp->substate_activated), evt_id,
  7749. ctx->ctx_id, ctx->link_hdl);
  7750. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  7751. if (irq_ops->irq_ops[evt_id]) {
  7752. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  7753. } else {
  7754. CAM_DBG(CAM_ISP,
  7755. "No handle function for Substate[%s], evt id %d, ctx:%u link: 0x%x",
  7756. __cam_isp_ctx_substate_val_to_type(
  7757. ctx_isp->substate_activated), evt_id,
  7758. ctx->ctx_id, ctx->link_hdl);
  7759. if (isp_ctx_debug.enable_state_monitor_dump)
  7760. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  7761. }
  7762. if ((evt_id == CAM_ISP_HW_EVENT_SOF) ||
  7763. (evt_id == CAM_ISP_HW_EVENT_EOF) ||
  7764. (evt_id == CAM_ISP_HW_EVENT_EPOCH))
  7765. __cam_isp_ctx_update_frame_timing_record(evt_id, ctx_isp);
  7766. __cam_isp_ctx_notify_cpas(ctx, evt_id);
  7767. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s], ctx: %u link: 0x%x",
  7768. ctx->state, __cam_isp_ctx_substate_val_to_type(
  7769. ctx_isp->substate_activated), ctx->ctx_id, ctx->link_hdl);
  7770. spin_unlock(&ctx->lock);
  7771. return rc;
  7772. }
  7773. static int cam_isp_context_validate_event_notify_injection(struct cam_context *ctx,
  7774. struct cam_hw_inject_evt_param *evt_params)
  7775. {
  7776. int rc = 0;
  7777. uint32_t evt_type;
  7778. uint64_t req_id;
  7779. req_id = evt_params->req_id;
  7780. evt_type = evt_params->u.evt_notify.evt_notify_type;
  7781. switch (evt_type) {
  7782. case V4L_EVENT_CAM_REQ_MGR_ERROR: {
  7783. struct cam_hw_inject_err_evt_param *err_evt_params =
  7784. &evt_params->u.evt_notify.u.err_evt_params;
  7785. switch (err_evt_params->err_type) {
  7786. case CAM_REQ_MGR_ERROR_TYPE_RECOVERY:
  7787. case CAM_REQ_MGR_ERROR_TYPE_SOF_FREEZE:
  7788. case CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY:
  7789. case CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY:
  7790. break;
  7791. default:
  7792. CAM_ERR(CAM_ISP,
  7793. "Invalid error type: %u for error event injection err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7794. err_evt_params->err_type, err_evt_params->err_code,
  7795. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7796. return -EINVAL;
  7797. }
  7798. CAM_INFO(CAM_ISP,
  7799. "Inject ERR evt: err code: %u err type: %u req id: %llu ctx id: %u link: 0x%x dev hdl: %d",
  7800. err_evt_params->err_code, err_evt_params->err_type,
  7801. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl);
  7802. break;
  7803. }
  7804. case V4L_EVENT_CAM_REQ_MGR_PF_ERROR: {
  7805. struct cam_hw_inject_pf_evt_param *pf_evt_params =
  7806. &evt_params->u.evt_notify.u.pf_evt_params;
  7807. bool non_fatal_en;
  7808. rc = cam_smmu_is_cb_non_fatal_fault_en(ctx->img_iommu_hdl, &non_fatal_en);
  7809. if (rc) {
  7810. CAM_ERR(CAM_ISP,
  7811. "Fail to query whether device's cb has non-fatal enabled rc:%d, ctx id: %u link: 0x%x",
  7812. rc, ctx->ctx_id, ctx->link_hdl);
  7813. return rc;
  7814. }
  7815. if (!non_fatal_en) {
  7816. CAM_ERR(CAM_ISP,
  7817. "Fail to inject pagefault event notif. Pagefault fatal for ISP,ctx:%u link:0x%x",
  7818. ctx->ctx_id, ctx->link_hdl);
  7819. return -EINVAL;
  7820. }
  7821. CAM_INFO(CAM_ISP,
  7822. "Inject PF evt: req_id:%llu ctx:%u link:0x%x dev hdl:%d ctx found:%hhu",
  7823. req_id, ctx->ctx_id, ctx->link_hdl, ctx->dev_hdl,
  7824. pf_evt_params->ctx_found);
  7825. break;
  7826. }
  7827. default:
  7828. CAM_ERR(CAM_ISP, "Event notification type not supported: %u, ctx: %u link: 0x%x",
  7829. evt_type, ctx->ctx_id, ctx->link_hdl);
  7830. rc = -EINVAL;
  7831. }
  7832. return rc;
  7833. }
  7834. static int cam_isp_context_inject_evt(void *context, void *evt_args)
  7835. {
  7836. struct cam_context *ctx = context;
  7837. struct cam_isp_context *ctx_isp = NULL;
  7838. struct cam_hw_inject_evt_param *evt_params = evt_args;
  7839. int rc = 0;
  7840. if (!ctx || !evt_args) {
  7841. CAM_ERR(CAM_ISP,
  7842. "Invalid params ctx %s event args %s",
  7843. CAM_IS_NULL_TO_STR(ctx), CAM_IS_NULL_TO_STR(evt_args));
  7844. return -EINVAL;
  7845. }
  7846. ctx_isp = ctx->ctx_priv;
  7847. if (evt_params->inject_id == CAM_COMMON_EVT_INJECT_NOTIFY_EVENT_TYPE) {
  7848. rc = cam_isp_context_validate_event_notify_injection(ctx, evt_params);
  7849. if (rc) {
  7850. CAM_ERR(CAM_ISP,
  7851. "Event notif injection failed validation rc:%d, ctx:%u link:0x%x",
  7852. rc, ctx->ctx_id, ctx->link_hdl);
  7853. return rc;
  7854. }
  7855. } else {
  7856. CAM_ERR(CAM_ISP, "Bufdone err injection %u not supported by ISP,ctx:%u link:0x%x",
  7857. evt_params->inject_id, ctx->ctx_id, ctx->link_hdl);
  7858. return -EINVAL;
  7859. }
  7860. memcpy(&ctx_isp->evt_inject_params, evt_params,
  7861. sizeof(struct cam_hw_inject_evt_param));
  7862. ctx_isp->evt_inject_params.is_valid = true;
  7863. return rc;
  7864. }
  7865. /* top state machine */
  7866. static struct cam_ctx_ops
  7867. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  7868. /* Uninit */
  7869. {
  7870. .ioctl_ops = {},
  7871. .crm_ops = {},
  7872. .irq_ops = NULL,
  7873. },
  7874. /* Available */
  7875. {
  7876. .ioctl_ops = {
  7877. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  7878. },
  7879. .crm_ops = {},
  7880. .irq_ops = NULL,
  7881. },
  7882. /* Acquired */
  7883. {
  7884. .ioctl_ops = {
  7885. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  7886. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7887. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  7888. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7889. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7890. },
  7891. .crm_ops = {
  7892. .link = __cam_isp_ctx_link_in_acquired,
  7893. .unlink = __cam_isp_ctx_unlink_in_acquired,
  7894. .get_dev_info = __cam_isp_ctx_get_dev_info,
  7895. .process_evt = __cam_isp_ctx_process_evt,
  7896. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  7897. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7898. },
  7899. .irq_ops = NULL,
  7900. .pagefault_ops = cam_isp_context_dump_requests,
  7901. .dumpinfo_ops = cam_isp_context_info_dump,
  7902. .evt_inject_ops = cam_isp_context_inject_evt,
  7903. },
  7904. /* Ready */
  7905. {
  7906. .ioctl_ops = {
  7907. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  7908. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  7909. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  7910. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7911. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  7912. },
  7913. .crm_ops = {
  7914. .unlink = __cam_isp_ctx_unlink_in_ready,
  7915. .get_dev_info = __cam_isp_ctx_get_dev_info,
  7916. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  7917. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7918. },
  7919. .irq_ops = NULL,
  7920. .pagefault_ops = cam_isp_context_dump_requests,
  7921. .dumpinfo_ops = cam_isp_context_info_dump,
  7922. .evt_inject_ops = cam_isp_context_inject_evt,
  7923. },
  7924. /* Flushed */
  7925. {
  7926. .ioctl_ops = {
  7927. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  7928. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  7929. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  7930. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  7931. },
  7932. .crm_ops = {
  7933. .unlink = __cam_isp_ctx_unlink_in_ready,
  7934. .process_evt = __cam_isp_ctx_process_evt,
  7935. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  7936. },
  7937. .irq_ops = NULL,
  7938. .pagefault_ops = cam_isp_context_dump_requests,
  7939. .dumpinfo_ops = cam_isp_context_info_dump,
  7940. .evt_inject_ops = cam_isp_context_inject_evt,
  7941. .msg_cb_ops = cam_isp_context_handle_message,
  7942. },
  7943. /* Activated */
  7944. {
  7945. .ioctl_ops = {
  7946. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  7947. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  7948. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  7949. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  7950. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  7951. },
  7952. .crm_ops = {
  7953. .unlink = __cam_isp_ctx_unlink_in_activated,
  7954. .apply_req = __cam_isp_ctx_apply_req,
  7955. .notify_frame_skip =
  7956. __cam_isp_ctx_apply_default_settings,
  7957. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  7958. .process_evt = __cam_isp_ctx_process_evt,
  7959. .dump_req = __cam_isp_ctx_dump_in_top_state,
  7960. },
  7961. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  7962. .pagefault_ops = cam_isp_context_dump_requests,
  7963. .dumpinfo_ops = cam_isp_context_info_dump,
  7964. .recovery_ops = cam_isp_context_hw_recovery,
  7965. .evt_inject_ops = cam_isp_context_inject_evt,
  7966. .msg_cb_ops = cam_isp_context_handle_message,
  7967. },
  7968. };
  7969. static int cam_isp_context_hw_recovery(void *priv, void *data)
  7970. {
  7971. struct cam_context *ctx = priv;
  7972. int rc = -EPERM;
  7973. if (ctx->hw_mgr_intf->hw_recovery)
  7974. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  7975. else
  7976. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery, ctx_idx: %u, link: 0x%x",
  7977. ctx->ctx_id, ctx->link_hdl);
  7978. return rc;
  7979. }
  7980. static void cam_isp_context_find_faulted_context(struct cam_context *ctx,
  7981. struct list_head *req_list, struct cam_hw_dump_pf_args *pf_args, bool *found)
  7982. {
  7983. struct cam_ctx_request *req = NULL;
  7984. struct cam_ctx_request *req_temp = NULL;
  7985. int rc;
  7986. *found = false;
  7987. list_for_each_entry_safe(req, req_temp, req_list, list) {
  7988. CAM_INFO(CAM_ISP, "List req_id: %llu ctx id: %u link: 0x%x",
  7989. req->request_id, ctx->ctx_id, ctx->link_hdl);
  7990. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  7991. if (rc)
  7992. CAM_ERR(CAM_ISP, "Failed to dump pf info, ctx_idx: %u, link: 0x%x",
  7993. ctx->ctx_id, ctx->link_hdl);
  7994. /*
  7995. * Found faulted buffer. Even if faulted ctx is found, but
  7996. * continue to search for faulted buffer
  7997. */
  7998. if (pf_args->pf_context_info.mem_type != CAM_FAULT_BUF_NOT_FOUND) {
  7999. *found = true;
  8000. break;
  8001. }
  8002. }
  8003. }
  8004. static int cam_isp_context_dump_requests(void *data, void *args)
  8005. {
  8006. struct cam_context *ctx = (struct cam_context *)data;
  8007. struct cam_isp_context *ctx_isp;
  8008. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  8009. int rc = 0;
  8010. bool found;
  8011. if (!ctx || !pf_args) {
  8012. CAM_ERR(CAM_ISP, "Invalid ctx %pK or pf args %pK",
  8013. ctx, pf_args);
  8014. return -EINVAL;
  8015. }
  8016. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  8017. if (!ctx_isp) {
  8018. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  8019. return -EINVAL;
  8020. }
  8021. if (pf_args->handle_sec_pf)
  8022. goto end;
  8023. CAM_INFO(CAM_ISP,
  8024. "Iterating over active list for isp ctx %u link: 0x%x state %d",
  8025. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8026. cam_isp_context_find_faulted_context(ctx, &ctx->active_req_list,
  8027. pf_args, &found);
  8028. if (found)
  8029. goto end;
  8030. CAM_INFO(CAM_ISP,
  8031. "Iterating over waiting list of isp ctx %u link: 0x%x state %d",
  8032. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8033. cam_isp_context_find_faulted_context(ctx, &ctx->wait_req_list,
  8034. pf_args, &found);
  8035. if (found)
  8036. goto end;
  8037. /*
  8038. * In certain scenarios we observe both overflow and SMMU pagefault
  8039. * for a particular request. If overflow is handled before page fault
  8040. * we need to traverse through pending request list because if
  8041. * bubble recovery is enabled on any request we move that request
  8042. * and all the subsequent requests to the pending list while handling
  8043. * overflow error.
  8044. */
  8045. CAM_INFO(CAM_ISP,
  8046. "Iterating over pending req list of isp ctx %u link: 0x%x state %d",
  8047. ctx->ctx_id, ctx->link_hdl, ctx->state);
  8048. cam_isp_context_find_faulted_context(ctx, &ctx->pending_req_list,
  8049. pf_args, &found);
  8050. if (found)
  8051. goto end;
  8052. end:
  8053. if (pf_args->pf_context_info.resource_type) {
  8054. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  8055. CAM_INFO(CAM_ISP,
  8056. "Page fault on resource:%s (0x%x) ctx id:%u link: 0x%x frame id:%d reported id:%lld applied id:%lld",
  8057. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  8058. pf_args->pf_context_info.resource_type),
  8059. pf_args->pf_context_info.resource_type,
  8060. ctx->ctx_id, ctx->link_hdl, ctx_isp->frame_id,
  8061. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  8062. }
  8063. /*
  8064. * Send PF notification to UMD if PF found on current CTX
  8065. * or it is forced to send PF notification to UMD even if no
  8066. * faulted context found
  8067. */
  8068. if (pf_args->pf_context_info.ctx_found ||
  8069. pf_args->pf_context_info.force_send_pf_evt)
  8070. rc = cam_context_send_pf_evt(ctx, pf_args);
  8071. if (rc)
  8072. CAM_ERR(CAM_ISP,
  8073. "Failed to notify PF event to userspace rc: %d, ctx id:%u link: 0x%x",
  8074. rc, ctx->ctx_id, ctx->link_hdl);
  8075. return rc;
  8076. }
  8077. static int cam_isp_context_handle_message(void *context,
  8078. uint32_t msg_type, void *data)
  8079. {
  8080. int rc = -EINVAL;
  8081. struct cam_hw_cmd_args hw_cmd_args = {0};
  8082. struct cam_isp_hw_cmd_args isp_hw_cmd_args = {0};
  8083. struct cam_context *ctx = (struct cam_context *)context;
  8084. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  8085. switch (msg_type) {
  8086. case CAM_SUBDEV_MESSAGE_CLOCK_UPDATE:
  8087. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  8088. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_UPDATE_CLOCK;
  8089. isp_hw_cmd_args.cmd_data = data;
  8090. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  8091. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  8092. &hw_cmd_args);
  8093. if (rc)
  8094. CAM_ERR(CAM_ISP, "Update clock rate failed rc: %d", rc);
  8095. break;
  8096. default:
  8097. CAM_ERR(CAM_ISP, "Invalid message type %d", msg_type);
  8098. }
  8099. return rc;
  8100. }
  8101. static int cam_isp_context_debug_register(void)
  8102. {
  8103. int rc = 0;
  8104. struct dentry *dbgfileptr = NULL;
  8105. if (!cam_debugfs_available())
  8106. return 0;
  8107. rc = cam_debugfs_create_subdir("isp_ctx", &dbgfileptr);
  8108. if (rc) {
  8109. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  8110. return rc;
  8111. }
  8112. /* Store parent inode for cleanup in caller */
  8113. isp_ctx_debug.dentry = dbgfileptr;
  8114. debugfs_create_u32("enable_state_monitor_dump", 0644,
  8115. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  8116. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  8117. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  8118. debugfs_create_u32("disable_internal_recovery_mask", 0644,
  8119. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery_mask);
  8120. return 0;
  8121. }
  8122. int cam_isp_context_init(struct cam_isp_context *ctx,
  8123. struct cam_context *ctx_base,
  8124. struct cam_req_mgr_kmd_ops *crm_node_intf,
  8125. struct cam_hw_mgr_intf *hw_intf,
  8126. uint32_t ctx_id,
  8127. uint32_t isp_device_type,
  8128. int img_iommu_hdl)
  8129. {
  8130. int rc = -1;
  8131. int i;
  8132. struct cam_isp_skip_frame_info *skip_info, *temp;
  8133. if (!ctx || !ctx_base) {
  8134. CAM_ERR(CAM_ISP, "Invalid Context");
  8135. goto err;
  8136. }
  8137. /* ISP context setup */
  8138. memset(ctx, 0, sizeof(*ctx));
  8139. ctx->base = ctx_base;
  8140. ctx->frame_id = 0;
  8141. ctx->custom_enabled = false;
  8142. ctx->use_frame_header_ts = false;
  8143. ctx->use_default_apply = false;
  8144. ctx->active_req_cnt = 0;
  8145. ctx->reported_req_id = 0;
  8146. ctx->bubble_frame_cnt = 0;
  8147. ctx->congestion_cnt = 0;
  8148. ctx->req_info.last_bufdone_req_id = 0;
  8149. ctx->v4l2_event_sub_ids = 0;
  8150. ctx->hw_ctx = NULL;
  8151. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  8152. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  8153. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  8154. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  8155. ctx->isp_device_type = isp_device_type;
  8156. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  8157. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  8158. ctx->req_isp[i].base = &ctx->req_base[i];
  8159. }
  8160. /* camera context setup */
  8161. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  8162. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  8163. if (rc) {
  8164. CAM_ERR(CAM_ISP, "Camera Context Base init failed, ctx_idx: %u, link: 0x%x",
  8165. ctx_base->ctx_id, ctx_base->link_hdl);
  8166. goto free_mem;
  8167. }
  8168. /* FCG related struct setup */
  8169. INIT_LIST_HEAD(&ctx->fcg_tracker.skipped_list);
  8170. for (i = 0; i < CAM_ISP_AFD_PIPELINE_DELAY; i++) {
  8171. skip_info = kzalloc(sizeof(struct cam_isp_skip_frame_info), GFP_KERNEL);
  8172. if (!skip_info) {
  8173. CAM_ERR(CAM_ISP,
  8174. "Failed to allocate memory for FCG struct, ctx_idx: %u, link: %x",
  8175. ctx_base->ctx_id, ctx_base->link_hdl);
  8176. rc = -ENOMEM;
  8177. goto free_mem;
  8178. }
  8179. list_add_tail(&skip_info->list, &ctx->fcg_tracker.skipped_list);
  8180. }
  8181. /* link camera context with isp context */
  8182. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  8183. ctx_base->ctx_priv = ctx;
  8184. /* initializing current state for error logging */
  8185. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  8186. ctx->dbg_monitors.state_monitor[i].curr_state =
  8187. CAM_ISP_CTX_ACTIVATED_MAX;
  8188. }
  8189. atomic64_set(&ctx->dbg_monitors.state_monitor_head, -1);
  8190. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  8191. atomic64_set(&ctx->dbg_monitors.event_record_head[i], -1);
  8192. atomic64_set(&ctx->dbg_monitors.frame_monitor_head, -1);
  8193. if (!isp_ctx_debug.dentry)
  8194. cam_isp_context_debug_register();
  8195. return rc;
  8196. free_mem:
  8197. list_for_each_entry_safe(skip_info, temp,
  8198. &ctx->fcg_tracker.skipped_list, list) {
  8199. list_del(&skip_info->list);
  8200. kfree(skip_info);
  8201. skip_info = NULL;
  8202. }
  8203. err:
  8204. return rc;
  8205. }
  8206. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  8207. {
  8208. struct cam_isp_skip_frame_info *skip_info, *temp;
  8209. list_for_each_entry_safe(skip_info, temp,
  8210. &ctx->fcg_tracker.skipped_list, list) {
  8211. list_del(&skip_info->list);
  8212. kfree(skip_info);
  8213. skip_info = NULL;
  8214. }
  8215. if (ctx->base)
  8216. cam_context_deinit(ctx->base);
  8217. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  8218. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  8219. __cam_isp_ctx_substate_val_to_type(
  8220. ctx->substate_activated));
  8221. isp_ctx_debug.dentry = NULL;
  8222. memset(ctx, 0, sizeof(*ctx));
  8223. return 0;
  8224. }