cam_isp_context.c 283 KB

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