cam_isp_context.c 236 KB

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