cam_isp_context.c 281 KB

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