cam_isp_context.c 289 KB

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