cam_isp_context.c 267 KB

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