cam_isp_context.c 239 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160
  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. "Unexpected Buf done for res=0x%x on ctx[%d] for Req %llu, status=%d, possible bh delays",
  1565. req_isp->fence_map_out[j].resource_handle, ctx->ctx_id,
  1566. req->request_id, status);
  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 void __cam_isp_ctx_try_buf_done_process_for_active_request(
  1908. uint32_t deferred_ack_start_idx, struct cam_isp_context *ctx_isp,
  1909. struct cam_ctx_request *deferred_req)
  1910. {
  1911. int i, j, deferred_map_idx, rc;
  1912. struct cam_context *ctx = ctx_isp->base;
  1913. struct cam_ctx_request *curr_active_req;
  1914. struct cam_isp_ctx_req *curr_active_isp_req;
  1915. struct cam_isp_ctx_req *deferred_isp_req;
  1916. if (list_empty(&ctx->active_req_list))
  1917. return;
  1918. curr_active_req = list_first_entry(&ctx->active_req_list,
  1919. struct cam_ctx_request, list);
  1920. curr_active_isp_req = (struct cam_isp_ctx_req *)curr_active_req->req_priv;
  1921. deferred_isp_req = (struct cam_isp_ctx_req *)deferred_req->req_priv;
  1922. /* Check from newly updated deferred acks */
  1923. for (i = deferred_ack_start_idx; i < deferred_isp_req->num_deferred_acks; i++) {
  1924. deferred_map_idx = deferred_isp_req->deferred_fence_map_index[i];
  1925. for (j = 0; j < curr_active_isp_req->num_fence_map_out; j++) {
  1926. /* resource needs to match */
  1927. if (curr_active_isp_req->fence_map_out[j].resource_handle !=
  1928. deferred_isp_req->fence_map_out[deferred_map_idx].resource_handle)
  1929. continue;
  1930. /* Check if fence is valid */
  1931. if (curr_active_isp_req->fence_map_out[j].sync_id == -1)
  1932. break;
  1933. CAM_WARN(CAM_ISP,
  1934. "Processing delayed buf done req: %llu bubble_detected: %s res: 0x%x fd: 0x%x, ctx: %u [deferred req: %llu last applied: %llu]",
  1935. curr_active_req->request_id,
  1936. CAM_BOOL_TO_YESNO(curr_active_isp_req->bubble_detected),
  1937. curr_active_isp_req->fence_map_out[j].resource_handle,
  1938. curr_active_isp_req->fence_map_out[j].sync_id, ctx->ctx_id,
  1939. deferred_req->request_id, ctx_isp->last_applied_req_id);
  1940. /* Signal only if bubble is not detected for this request */
  1941. if (!curr_active_isp_req->bubble_detected) {
  1942. rc = cam_sync_signal(curr_active_isp_req->fence_map_out[j].sync_id,
  1943. CAM_SYNC_STATE_SIGNALED_SUCCESS,
  1944. CAM_SYNC_COMMON_EVENT_SUCCESS);
  1945. if (rc)
  1946. CAM_ERR(CAM_ISP,
  1947. "Sync: %d for req: %llu failed with rc: %d",
  1948. curr_active_isp_req->fence_map_out[j].sync_id,
  1949. curr_active_req->request_id, rc);
  1950. curr_active_isp_req->fence_map_out[j].sync_id = -1;
  1951. }
  1952. curr_active_isp_req->num_acked++;
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. static int __cam_isp_ctx_check_deferred_buf_done(
  1958. struct cam_isp_context *ctx_isp,
  1959. struct cam_isp_hw_done_event_data *done,
  1960. uint32_t bubble_state)
  1961. {
  1962. int rc = 0;
  1963. uint32_t curr_num_deferred = 0;
  1964. struct cam_ctx_request *req;
  1965. struct cam_context *ctx = ctx_isp->base;
  1966. struct cam_isp_ctx_req *req_isp;
  1967. bool req_in_pending_wait_list = false;
  1968. if (!list_empty(&ctx->wait_req_list)) {
  1969. req = list_first_entry(&ctx->wait_req_list,
  1970. struct cam_ctx_request, list);
  1971. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1972. curr_num_deferred = req_isp->num_deferred_acks;
  1973. req_in_pending_wait_list = true;
  1974. if (ctx_isp->last_applied_req_id !=
  1975. ctx_isp->last_bufdone_err_apply_req_id) {
  1976. CAM_DBG(CAM_ISP,
  1977. "Trying to find buf done with req in wait list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u",
  1978. req->request_id, ctx_isp->last_applied_req_id,
  1979. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred);
  1980. ctx_isp->last_bufdone_err_apply_req_id =
  1981. ctx_isp->last_applied_req_id;
  1982. }
  1983. /*
  1984. * Verify consumed address for this request to make sure
  1985. * we are handling the buf_done for the correct
  1986. * buffer. Also defer actual buf_done handling, i.e
  1987. * do not signal the fence as this request may go into
  1988. * Bubble state eventully.
  1989. */
  1990. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1991. ctx_isp, req, done, bubble_state, true, true);
  1992. /* Check for active req if any deferred is processed */
  1993. if (req_isp->num_deferred_acks > curr_num_deferred)
  1994. __cam_isp_ctx_try_buf_done_process_for_active_request(
  1995. curr_num_deferred, ctx_isp, req);
  1996. } else if (!list_empty(&ctx->pending_req_list)) {
  1997. /*
  1998. * We saw the case that the hw config is blocked due to
  1999. * some reason, the we get the reg upd and buf done before
  2000. * the req is added to wait req list.
  2001. */
  2002. req = list_first_entry(&ctx->pending_req_list,
  2003. struct cam_ctx_request, list);
  2004. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2005. curr_num_deferred = req_isp->num_deferred_acks;
  2006. req_in_pending_wait_list = true;
  2007. if (ctx_isp->last_applied_req_id !=
  2008. ctx_isp->last_bufdone_err_apply_req_id) {
  2009. CAM_DBG(CAM_ISP,
  2010. "Trying to find buf done with req in pending list, req %llu last apply id:%lld last err id:%lld curr_num_deferred: %u",
  2011. req->request_id, ctx_isp->last_applied_req_id,
  2012. ctx_isp->last_bufdone_err_apply_req_id, curr_num_deferred);
  2013. ctx_isp->last_bufdone_err_apply_req_id =
  2014. ctx_isp->last_applied_req_id;
  2015. }
  2016. /*
  2017. * Verify consumed address for this request to make sure
  2018. * we are handling the buf_done for the correct
  2019. * buffer. Also defer actual buf_done handling, i.e
  2020. * do not signal the fence as this request may go into
  2021. * Bubble state eventully.
  2022. */
  2023. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2024. ctx_isp, req, done, bubble_state, true, true);
  2025. /* Check for active req if any deferred is processed */
  2026. if (req_isp->num_deferred_acks > curr_num_deferred)
  2027. __cam_isp_ctx_try_buf_done_process_for_active_request(
  2028. curr_num_deferred, ctx_isp, req);
  2029. }
  2030. if (!req_in_pending_wait_list && (ctx_isp->last_applied_req_id !=
  2031. ctx_isp->last_bufdone_err_apply_req_id)) {
  2032. CAM_DBG(CAM_ISP,
  2033. "Buf done with no active request bubble_state=%d last_applied_req_id:%lld",
  2034. bubble_state, ctx_isp->last_applied_req_id);
  2035. ctx_isp->last_bufdone_err_apply_req_id =
  2036. ctx_isp->last_applied_req_id;
  2037. }
  2038. return rc;
  2039. }
  2040. static int __cam_isp_ctx_handle_buf_done_verify_addr(
  2041. struct cam_isp_context *ctx_isp,
  2042. struct cam_isp_hw_done_event_data *done,
  2043. uint32_t bubble_state)
  2044. {
  2045. int rc = 0;
  2046. bool irq_delay_detected = false;
  2047. struct cam_ctx_request *req;
  2048. struct cam_ctx_request *next_req = NULL;
  2049. struct cam_context *ctx = ctx_isp->base;
  2050. if (list_empty(&ctx->active_req_list)) {
  2051. return __cam_isp_ctx_check_deferred_buf_done(
  2052. ctx_isp, done, bubble_state);
  2053. }
  2054. req = list_first_entry(&ctx->active_req_list,
  2055. struct cam_ctx_request, list);
  2056. if (ctx_isp->active_req_cnt > 1) {
  2057. next_req = list_last_entry(
  2058. &ctx->active_req_list,
  2059. struct cam_ctx_request, list);
  2060. if (next_req->request_id != req->request_id)
  2061. __cam_isp_ctx_buf_done_match_req(next_req, done,
  2062. &irq_delay_detected);
  2063. else
  2064. CAM_WARN(CAM_ISP,
  2065. "Req %lld only active request, spurious buf_done rxd",
  2066. req->request_id);
  2067. }
  2068. /*
  2069. * If irq delay isn't detected, then we need to verify
  2070. * the consumed address for current req, otherwise, we
  2071. * can't verify the consumed address.
  2072. */
  2073. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2074. ctx_isp, req, done, bubble_state,
  2075. !irq_delay_detected, false);
  2076. /*
  2077. * Verify the consumed address for next req all the time,
  2078. * since the reported buf done event may belong to current
  2079. * req, then we can't signal this event for next req.
  2080. */
  2081. if (!rc && irq_delay_detected)
  2082. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  2083. ctx_isp, next_req, done,
  2084. bubble_state, true, false);
  2085. return rc;
  2086. }
  2087. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  2088. struct cam_isp_context *ctx_isp,
  2089. struct cam_isp_hw_done_event_data *done,
  2090. uint32_t bubble_state)
  2091. {
  2092. int rc = 0;
  2093. if (ctx_isp->support_consumed_addr)
  2094. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  2095. ctx_isp, done, bubble_state);
  2096. else
  2097. rc = __cam_isp_ctx_handle_buf_done(
  2098. ctx_isp, done, bubble_state);
  2099. return rc;
  2100. }
  2101. static int __cam_isp_ctx_apply_pending_req(
  2102. void *priv, void *data)
  2103. {
  2104. int rc = 0;
  2105. int64_t prev_applied_req;
  2106. struct cam_context *ctx = NULL;
  2107. struct cam_isp_context *ctx_isp = priv;
  2108. struct cam_ctx_request *req;
  2109. struct cam_isp_ctx_req *req_isp;
  2110. struct cam_hw_config_args cfg = {0};
  2111. if (!ctx_isp) {
  2112. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  2113. rc = -EINVAL;
  2114. goto end;
  2115. }
  2116. ctx = ctx_isp->base;
  2117. if (list_empty(&ctx->pending_req_list)) {
  2118. CAM_DBG(CAM_ISP, "No pending requests to apply");
  2119. rc = -EFAULT;
  2120. goto end;
  2121. }
  2122. if (ctx_isp->vfps_aux_context) {
  2123. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED)
  2124. goto end;
  2125. if (ctx_isp->active_req_cnt >= 1)
  2126. goto end;
  2127. } else {
  2128. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  2129. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  2130. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  2131. goto end;
  2132. if (ctx_isp->active_req_cnt >= 2)
  2133. goto end;
  2134. }
  2135. spin_lock_bh(&ctx->lock);
  2136. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  2137. list);
  2138. spin_unlock_bh(&ctx->lock);
  2139. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  2140. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  2141. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2142. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2143. cfg.request_id = req->request_id;
  2144. cfg.hw_update_entries = req_isp->cfg;
  2145. cfg.num_hw_update_entries = req_isp->num_cfg;
  2146. cfg.priv = &req_isp->hw_update_data;
  2147. /*
  2148. * Offline mode may receive the SOF and REG_UPD earlier than
  2149. * CDM processing return back, so we set the substate before
  2150. * apply setting.
  2151. */
  2152. spin_lock_bh(&ctx->lock);
  2153. atomic_set(&ctx_isp->rxd_epoch, 0);
  2154. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  2155. prev_applied_req = ctx_isp->last_applied_req_id;
  2156. ctx_isp->last_applied_req_id = req->request_id;
  2157. atomic_set(&ctx_isp->apply_in_progress, 1);
  2158. list_del_init(&req->list);
  2159. list_add_tail(&req->list, &ctx->wait_req_list);
  2160. spin_unlock_bh(&ctx->lock);
  2161. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  2162. if (rc) {
  2163. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  2164. spin_lock_bh(&ctx->lock);
  2165. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2166. ctx_isp->last_applied_req_id = prev_applied_req;
  2167. atomic_set(&ctx_isp->apply_in_progress, 0);
  2168. list_del_init(&req->list);
  2169. list_add(&req->list, &ctx->pending_req_list);
  2170. spin_unlock_bh(&ctx->lock);
  2171. } else {
  2172. atomic_set(&ctx_isp->apply_in_progress, 0);
  2173. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld",
  2174. CAM_ISP_CTX_ACTIVATED_APPLIED,
  2175. ctx_isp->last_applied_req_id);
  2176. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2177. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  2178. req->request_id);
  2179. }
  2180. end:
  2181. return rc;
  2182. }
  2183. static int __cam_isp_ctx_schedule_apply_req(
  2184. struct cam_isp_context *ctx_isp)
  2185. {
  2186. int rc = 0;
  2187. struct crm_workq_task *task;
  2188. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  2189. if (!task) {
  2190. CAM_ERR(CAM_ISP, "No task for worker");
  2191. return -ENOMEM;
  2192. }
  2193. task->process_cb = __cam_isp_ctx_apply_pending_req;
  2194. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  2195. if (rc)
  2196. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  2197. return rc;
  2198. }
  2199. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  2200. struct cam_isp_context *ctx_isp, void *evt_data)
  2201. {
  2202. struct cam_context *ctx = ctx_isp->base;
  2203. struct cam_ctx_request *req, *req_temp;
  2204. uint64_t request_id = 0;
  2205. atomic_set(&ctx_isp->rxd_epoch, 1);
  2206. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u", ctx_isp->frame_id,
  2207. ctx->ctx_id);
  2208. /*
  2209. * For offline it is not possible for epoch to be generated without
  2210. * RUP done. IRQ scheduling delays can possibly cause this.
  2211. */
  2212. if (list_empty(&ctx->active_req_list)) {
  2213. CAM_WARN(CAM_ISP, "Active list empty on ctx: %u - EPOCH serviced before RUP",
  2214. ctx->ctx_id);
  2215. } else {
  2216. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  2217. if (req->request_id > ctx_isp->reported_req_id) {
  2218. request_id = req->request_id;
  2219. ctx_isp->reported_req_id = request_id;
  2220. break;
  2221. }
  2222. }
  2223. }
  2224. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  2225. /*
  2226. * If no valid request, wait for RUP shutter posted after buf done
  2227. */
  2228. if (request_id)
  2229. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2230. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2231. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2232. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2233. request_id);
  2234. return 0;
  2235. }
  2236. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  2237. struct cam_isp_context *ctx_isp, void *evt_data)
  2238. {
  2239. if (ctx_isp->frame_id == 1)
  2240. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  2241. __cam_isp_ctx_substate_val_to_type(
  2242. ctx_isp->substate_activated));
  2243. else
  2244. CAM_WARN_RATE_LIMIT(CAM_ISP,
  2245. "ctx_id:%d Unexpected reg update in activated Substate[%s] for frame_id:%lld",
  2246. ctx_isp->base->ctx_id,
  2247. __cam_isp_ctx_substate_val_to_type(
  2248. ctx_isp->substate_activated),
  2249. ctx_isp->frame_id);
  2250. return 0;
  2251. }
  2252. static int __cam_isp_ctx_reg_upd_in_applied_state(
  2253. struct cam_isp_context *ctx_isp, void *evt_data)
  2254. {
  2255. int rc = 0;
  2256. struct cam_ctx_request *req;
  2257. struct cam_context *ctx = ctx_isp->base;
  2258. struct cam_isp_ctx_req *req_isp;
  2259. uint64_t request_id = 0;
  2260. if (list_empty(&ctx->wait_req_list)) {
  2261. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2262. goto end;
  2263. }
  2264. req = list_first_entry(&ctx->wait_req_list,
  2265. struct cam_ctx_request, list);
  2266. list_del_init(&req->list);
  2267. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2268. if (req_isp->num_fence_map_out != 0) {
  2269. list_add_tail(&req->list, &ctx->active_req_list);
  2270. ctx_isp->active_req_cnt++;
  2271. request_id = req->request_id;
  2272. CAM_DBG(CAM_REQ,
  2273. "move request %lld to active list(cnt = %d), ctx %u",
  2274. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2275. __cam_isp_ctx_update_event_record(ctx_isp,
  2276. CAM_ISP_CTX_EVENT_RUP, req);
  2277. } else {
  2278. /* no io config, so the request is completed. */
  2279. list_add_tail(&req->list, &ctx->free_req_list);
  2280. CAM_DBG(CAM_ISP,
  2281. "move active request %lld to free list(cnt = %d), ctx %u",
  2282. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2283. }
  2284. /*
  2285. * This function only called directly from applied and bubble applied
  2286. * state so change substate here.
  2287. */
  2288. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2289. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2290. __cam_isp_ctx_substate_val_to_type(
  2291. ctx_isp->substate_activated));
  2292. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2293. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  2294. end:
  2295. return rc;
  2296. }
  2297. static int __cam_isp_ctx_notify_sof_in_activated_state(
  2298. struct cam_isp_context *ctx_isp, void *evt_data)
  2299. {
  2300. int rc = 0;
  2301. uint64_t request_id = 0;
  2302. struct cam_context *ctx = ctx_isp->base;
  2303. struct cam_ctx_request *req;
  2304. struct cam_isp_ctx_req *req_isp;
  2305. struct cam_hw_cmd_args hw_cmd_args;
  2306. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  2307. uint64_t last_cdm_done_req = 0;
  2308. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2309. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2310. if (!evt_data) {
  2311. CAM_ERR(CAM_ISP, "invalid event data");
  2312. return -EINVAL;
  2313. }
  2314. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2315. if (atomic_read(&ctx_isp->process_bubble)) {
  2316. if (list_empty(&ctx->active_req_list)) {
  2317. CAM_ERR(CAM_ISP,
  2318. "No available active req in bubble");
  2319. atomic_set(&ctx_isp->process_bubble, 0);
  2320. ctx_isp->bubble_frame_cnt = 0;
  2321. rc = -EINVAL;
  2322. return rc;
  2323. }
  2324. if (ctx_isp->last_sof_timestamp ==
  2325. ctx_isp->sof_timestamp_val) {
  2326. CAM_DBG(CAM_ISP,
  2327. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld",
  2328. ctx_isp->sof_timestamp_val);
  2329. goto notify_only;
  2330. }
  2331. req = list_first_entry(&ctx->active_req_list,
  2332. struct cam_ctx_request, list);
  2333. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2334. if (ctx_isp->bubble_frame_cnt >= 1 &&
  2335. req_isp->bubble_detected) {
  2336. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2337. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2338. isp_hw_cmd_args.cmd_type =
  2339. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  2340. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2341. rc = ctx->hw_mgr_intf->hw_cmd(
  2342. ctx->hw_mgr_intf->hw_mgr_priv,
  2343. &hw_cmd_args);
  2344. if (rc) {
  2345. CAM_ERR(CAM_ISP, "HW command failed");
  2346. return rc;
  2347. }
  2348. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  2349. CAM_DBG(CAM_ISP, "last_cdm_done req: %d",
  2350. last_cdm_done_req);
  2351. if (last_cdm_done_req >= req->request_id) {
  2352. CAM_DBG(CAM_ISP,
  2353. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  2354. req->request_id);
  2355. ctx_isp->bubble_frame_cnt = 0;
  2356. } else {
  2357. CAM_DBG(CAM_ISP,
  2358. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  2359. req->request_id);
  2360. req_isp->num_acked = 0;
  2361. req_isp->num_deferred_acks = 0;
  2362. ctx_isp->bubble_frame_cnt = 0;
  2363. req_isp->bubble_detected = false;
  2364. req_isp->cdm_reset_before_apply = true;
  2365. list_del_init(&req->list);
  2366. list_add(&req->list, &ctx->pending_req_list);
  2367. atomic_set(&ctx_isp->process_bubble, 0);
  2368. ctx_isp->active_req_cnt--;
  2369. CAM_DBG(CAM_REQ,
  2370. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  2371. req->request_id,
  2372. ctx_isp->active_req_cnt, ctx->ctx_id);
  2373. }
  2374. } else if (req_isp->bubble_detected) {
  2375. ctx_isp->bubble_frame_cnt++;
  2376. CAM_DBG(CAM_ISP,
  2377. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld",
  2378. req->request_id,
  2379. ctx_isp->bubble_frame_cnt);
  2380. } else {
  2381. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld",
  2382. req->request_id);
  2383. }
  2384. }
  2385. notify_only:
  2386. /*
  2387. * notify reqmgr with sof signal. Note, due to scheduling delay
  2388. * we can run into situation that two active requests has already
  2389. * be in the active queue while we try to do the notification.
  2390. * In this case, we need to skip the current notification. This
  2391. * helps the state machine to catch up the delay.
  2392. */
  2393. if (ctx_isp->active_req_cnt <= 2) {
  2394. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2395. list_for_each_entry(req, &ctx->active_req_list, list) {
  2396. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2397. if ((!req_isp->bubble_detected) &&
  2398. (req->request_id > ctx_isp->reported_req_id)) {
  2399. request_id = req->request_id;
  2400. __cam_isp_ctx_update_event_record(ctx_isp,
  2401. CAM_ISP_CTX_EVENT_EPOCH, req);
  2402. break;
  2403. }
  2404. }
  2405. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  2406. request_id = 0;
  2407. if (request_id != 0)
  2408. ctx_isp->reported_req_id = request_id;
  2409. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2410. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2411. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2412. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2413. request_id);
  2414. }
  2415. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  2416. return 0;
  2417. }
  2418. static int __cam_isp_ctx_notify_eof_in_activated_state(
  2419. struct cam_isp_context *ctx_isp, void *evt_data)
  2420. {
  2421. int rc = 0;
  2422. /* notify reqmgr with eof signal */
  2423. rc = __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_EOF, ctx_isp);
  2424. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2425. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  2426. return rc;
  2427. }
  2428. static int __cam_isp_ctx_reg_upd_in_hw_error(
  2429. struct cam_isp_context *ctx_isp, void *evt_data)
  2430. {
  2431. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2432. return 0;
  2433. }
  2434. static int __cam_isp_ctx_sof_in_activated_state(
  2435. struct cam_isp_context *ctx_isp, void *evt_data)
  2436. {
  2437. int rc = 0;
  2438. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2439. struct cam_ctx_request *req = NULL;
  2440. struct cam_context *ctx = ctx_isp->base;
  2441. uint64_t request_id = 0;
  2442. /* First check if there is a valid request in active list */
  2443. list_for_each_entry(req, &ctx->active_req_list, list) {
  2444. if (req->request_id > ctx_isp->reported_req_id) {
  2445. request_id = req->request_id;
  2446. break;
  2447. }
  2448. }
  2449. /*
  2450. * If nothing in active list, current request might have not moved
  2451. * from wait to active list. This could happen if REG_UPDATE to sw
  2452. * is coming immediately after SOF
  2453. */
  2454. if (request_id == 0) {
  2455. req = list_first_entry(&ctx->wait_req_list,
  2456. struct cam_ctx_request, list);
  2457. if (req)
  2458. request_id = req->request_id;
  2459. }
  2460. if (!evt_data) {
  2461. CAM_ERR(CAM_ISP, "in valid sof event data");
  2462. return -EINVAL;
  2463. }
  2464. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  2465. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2466. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2467. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  2468. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  2469. return rc;
  2470. }
  2471. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2472. void *evt_data)
  2473. {
  2474. int rc = 0;
  2475. struct cam_ctx_request *req = NULL;
  2476. struct cam_isp_ctx_req *req_isp;
  2477. struct cam_context *ctx = ctx_isp->base;
  2478. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2479. CAM_DBG(CAM_ISP, "invalid RUP");
  2480. goto end;
  2481. }
  2482. /*
  2483. * This is for the first update. The initial setting will
  2484. * cause the reg_upd in the first frame.
  2485. */
  2486. if (!list_empty(&ctx->wait_req_list)) {
  2487. req = list_first_entry(&ctx->wait_req_list,
  2488. struct cam_ctx_request, list);
  2489. list_del_init(&req->list);
  2490. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2491. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2492. list_add_tail(&req->list, &ctx->free_req_list);
  2493. else
  2494. CAM_ERR(CAM_ISP,
  2495. "receive rup in unexpected state");
  2496. }
  2497. if (req != NULL) {
  2498. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2499. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2500. req->request_id);
  2501. }
  2502. end:
  2503. return rc;
  2504. }
  2505. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  2506. void *evt_data)
  2507. {
  2508. uint64_t request_id = 0;
  2509. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  2510. struct cam_ctx_request *req;
  2511. struct cam_isp_ctx_req *req_isp;
  2512. struct cam_context *ctx = ctx_isp->base;
  2513. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2514. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2515. if (!evt_data) {
  2516. CAM_ERR(CAM_ISP, "invalid event data");
  2517. return -EINVAL;
  2518. }
  2519. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2520. if (list_empty(&ctx->wait_req_list)) {
  2521. /*
  2522. * If no wait req in epoch, this is an error case.
  2523. * The recovery is to go back to sof state
  2524. */
  2525. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  2526. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2527. /* Send SOF event as empty frame*/
  2528. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2529. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2530. __cam_isp_ctx_update_event_record(ctx_isp,
  2531. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2532. goto end;
  2533. }
  2534. /* Update state prior to notifying CRM */
  2535. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2536. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2537. list);
  2538. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2539. req_isp->bubble_detected = true;
  2540. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  2541. req_isp->cdm_reset_before_apply = false;
  2542. atomic_set(&ctx_isp->process_bubble, 1);
  2543. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%d Report Bubble flag %d req id:%lld",
  2544. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  2545. if (req_isp->bubble_report) {
  2546. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  2547. req->request_id, ctx_isp);
  2548. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  2549. req->request_id, ctx->ctx_id);
  2550. } else {
  2551. req_isp->bubble_report = 0;
  2552. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  2553. req->request_id, ctx->ctx_id);
  2554. if (ctx_isp->active_req_cnt <= 1)
  2555. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2556. }
  2557. /*
  2558. * Always move the request to active list. Let buf done
  2559. * function handles the rest.
  2560. */
  2561. list_del_init(&req->list);
  2562. list_add_tail(&req->list, &ctx->active_req_list);
  2563. ctx_isp->active_req_cnt++;
  2564. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  2565. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2566. /*
  2567. * Handle the deferred buf done after moving
  2568. * the bubble req to active req list.
  2569. */
  2570. __cam_isp_ctx_handle_deferred_buf_done_in_bubble(
  2571. ctx_isp, req);
  2572. /*
  2573. * Update the record before req pointer to
  2574. * other invalid req.
  2575. */
  2576. __cam_isp_ctx_update_event_record(ctx_isp,
  2577. CAM_ISP_CTX_EVENT_EPOCH, req);
  2578. /*
  2579. * Get the req again from active_req_list in case
  2580. * the active req cnt is 2.
  2581. */
  2582. list_for_each_entry(req, &ctx->active_req_list, list) {
  2583. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2584. if ((!req_isp->bubble_report) &&
  2585. (req->request_id > ctx_isp->reported_req_id)) {
  2586. request_id = req->request_id;
  2587. ctx_isp->reported_req_id = request_id;
  2588. CAM_DBG(CAM_ISP,
  2589. "ctx %d reported_req_id update to %lld",
  2590. ctx->ctx_id, ctx_isp->reported_req_id);
  2591. break;
  2592. }
  2593. }
  2594. if ((request_id != 0) && req_isp->bubble_detected)
  2595. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  2596. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2597. sof_event_status);
  2598. cam_req_mgr_debug_delay_detect();
  2599. trace_cam_delay_detect("ISP",
  2600. "bubble epoch_in_applied", req->request_id,
  2601. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  2602. CAM_DEFAULT_VALUE);
  2603. end:
  2604. if (request_id == 0) {
  2605. req = list_last_entry(&ctx->active_req_list,
  2606. struct cam_ctx_request, list);
  2607. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2608. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2609. } else {
  2610. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2611. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  2612. }
  2613. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2614. __cam_isp_ctx_substate_val_to_type(
  2615. ctx_isp->substate_activated));
  2616. return 0;
  2617. }
  2618. static int __cam_isp_ctx_buf_done_in_sof(struct cam_isp_context *ctx_isp,
  2619. void *evt_data)
  2620. {
  2621. int rc = 0;
  2622. struct cam_isp_hw_done_event_data *done =
  2623. (struct cam_isp_hw_done_event_data *) evt_data;
  2624. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2625. return rc;
  2626. }
  2627. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  2628. void *evt_data)
  2629. {
  2630. int rc = 0;
  2631. struct cam_isp_hw_done_event_data *done =
  2632. (struct cam_isp_hw_done_event_data *) evt_data;
  2633. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2634. return rc;
  2635. }
  2636. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  2637. void *evt_data)
  2638. {
  2639. int rc = 0;
  2640. struct cam_context *ctx = ctx_isp->base;
  2641. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2642. struct cam_ctx_request *req;
  2643. if (!evt_data) {
  2644. CAM_ERR(CAM_ISP, "in valid sof event data");
  2645. return -EINVAL;
  2646. }
  2647. if (atomic_read(&ctx_isp->apply_in_progress))
  2648. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF");
  2649. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  2650. if (list_empty(&ctx->active_req_list))
  2651. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2652. else
  2653. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  2654. req = list_last_entry(&ctx->active_req_list,
  2655. struct cam_ctx_request, list);
  2656. if (req)
  2657. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2658. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  2659. req->request_id);
  2660. if (ctx_isp->frame_id == 1)
  2661. CAM_INFO(CAM_ISP,
  2662. "First SOF in EPCR ctx:%d frame_id:%lld next substate %s",
  2663. ctx->ctx_id, ctx_isp->frame_id,
  2664. __cam_isp_ctx_substate_val_to_type(
  2665. ctx_isp->substate_activated));
  2666. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%d frame_id:%lld next substate:%s",
  2667. ctx->ctx_id, ctx_isp->frame_id,
  2668. __cam_isp_ctx_substate_val_to_type(
  2669. ctx_isp->substate_activated));
  2670. return rc;
  2671. }
  2672. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2673. void *evt_data)
  2674. {
  2675. int rc = 0;
  2676. struct cam_isp_hw_done_event_data *done =
  2677. (struct cam_isp_hw_done_event_data *) evt_data;
  2678. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2679. return rc;
  2680. }
  2681. static int __cam_isp_ctx_buf_done_in_bubble(
  2682. struct cam_isp_context *ctx_isp, void *evt_data)
  2683. {
  2684. int rc = 0;
  2685. struct cam_isp_hw_done_event_data *done =
  2686. (struct cam_isp_hw_done_event_data *) evt_data;
  2687. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2688. return rc;
  2689. }
  2690. static int __cam_isp_ctx_epoch_in_bubble_applied(
  2691. struct cam_isp_context *ctx_isp, void *evt_data)
  2692. {
  2693. uint64_t request_id = 0;
  2694. struct cam_ctx_request *req;
  2695. struct cam_isp_ctx_req *req_isp;
  2696. struct cam_context *ctx = ctx_isp->base;
  2697. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2698. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2699. if (!evt_data) {
  2700. CAM_ERR(CAM_ISP, "invalid event data");
  2701. return -EINVAL;
  2702. }
  2703. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2704. /*
  2705. * This means we missed the reg upd ack. So we need to
  2706. * transition to BUBBLE state again.
  2707. */
  2708. if (list_empty(&ctx->wait_req_list)) {
  2709. /*
  2710. * If no pending req in epoch, this is an error case.
  2711. * Just go back to the bubble state.
  2712. */
  2713. CAM_ERR(CAM_ISP, "ctx:%d No pending request.", ctx->ctx_id);
  2714. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2715. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2716. __cam_isp_ctx_update_event_record(ctx_isp,
  2717. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2718. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2719. goto end;
  2720. }
  2721. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2722. list);
  2723. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2724. req_isp->bubble_detected = true;
  2725. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  2726. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  2727. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  2728. req_isp->cdm_reset_before_apply = false;
  2729. if (req_isp->bubble_report) {
  2730. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  2731. req->request_id, ctx_isp);
  2732. atomic_set(&ctx_isp->process_bubble, 1);
  2733. } else {
  2734. req_isp->bubble_report = 0;
  2735. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  2736. req->request_id, ctx->ctx_id);
  2737. if (ctx_isp->active_req_cnt <= 1)
  2738. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2739. atomic_set(&ctx_isp->process_bubble, 1);
  2740. }
  2741. /*
  2742. * Always move the request to active list. Let buf done
  2743. * function handles the rest.
  2744. */
  2745. list_del_init(&req->list);
  2746. list_add_tail(&req->list, &ctx->active_req_list);
  2747. ctx_isp->active_req_cnt++;
  2748. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  2749. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2750. if (!req_isp->bubble_report) {
  2751. if (req->request_id > ctx_isp->reported_req_id) {
  2752. request_id = req->request_id;
  2753. ctx_isp->reported_req_id = request_id;
  2754. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2755. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2756. __cam_isp_ctx_update_event_record(ctx_isp,
  2757. CAM_ISP_CTX_EVENT_EPOCH, req);
  2758. } else {
  2759. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2760. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2761. __cam_isp_ctx_update_event_record(ctx_isp,
  2762. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2763. }
  2764. } else {
  2765. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2766. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2767. __cam_isp_ctx_update_event_record(ctx_isp,
  2768. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2769. }
  2770. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2771. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2772. __cam_isp_ctx_substate_val_to_type(
  2773. ctx_isp->substate_activated));
  2774. cam_req_mgr_debug_delay_detect();
  2775. trace_cam_delay_detect("ISP",
  2776. "bubble epoch_in_bubble_applied",
  2777. req->request_id, ctx->ctx_id,
  2778. ctx->link_hdl, ctx->session_hdl,
  2779. CAM_DEFAULT_VALUE);
  2780. end:
  2781. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  2782. list);
  2783. if (req)
  2784. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2785. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2786. return 0;
  2787. }
  2788. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  2789. struct cam_isp_context *ctx_isp, void *evt_data)
  2790. {
  2791. int rc = 0;
  2792. struct cam_isp_hw_done_event_data *done =
  2793. (struct cam_isp_hw_done_event_data *) evt_data;
  2794. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2795. return rc;
  2796. }
  2797. static void get_notification_evt_params(uint32_t hw_error, uint32_t *fence_evt_cause,
  2798. uint32_t *req_mgr_err_code, uint32_t *recovery_type)
  2799. {
  2800. uint32_t err_type, err_code = 0, recovery_type_temp;
  2801. err_type = CAM_SYNC_ISP_EVENT_UNKNOWN;
  2802. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2803. if (hw_error & CAM_ISP_HW_ERROR_OVERFLOW) {
  2804. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  2805. err_type = CAM_SYNC_ISP_EVENT_OVERFLOW;
  2806. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2807. }
  2808. if (hw_error & CAM_ISP_HW_ERROR_CSID_OUTPUT_FIFO_OVERFLOW) {
  2809. err_code |= CAM_REQ_MGR_CSID_FIFO_OVERFLOW_ERROR;
  2810. err_type = CAM_SYNC_ISP_EVENT_CSID_OUTPUT_FIFO_OVERFLOW;
  2811. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2812. }
  2813. if (hw_error & CAM_ISP_HW_ERROR_RECOVERY_OVERFLOW) {
  2814. err_code |= CAM_REQ_MGR_CSID_RECOVERY_OVERFLOW_ERROR;
  2815. err_type = CAM_SYNC_ISP_EVENT_RECOVERY_OVERFLOW;
  2816. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2817. }
  2818. if (hw_error & CAM_ISP_HW_ERROR_P2I_ERROR) {
  2819. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  2820. err_type = CAM_SYNC_ISP_EVENT_P2I_ERROR;
  2821. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2822. }
  2823. if (hw_error & CAM_ISP_HW_ERROR_VIOLATION) {
  2824. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  2825. err_type = CAM_SYNC_ISP_EVENT_VIOLATION;
  2826. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2827. }
  2828. if (hw_error & CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) {
  2829. err_code |= CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  2830. err_type = CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  2831. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2832. }
  2833. if (hw_error & CAM_ISP_HW_ERROR_CSID_SENSOR_SWITCH_ERROR) {
  2834. err_code |= CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING;
  2835. err_type = CAM_SYNC_ISP_EVENT_CSID_SENSOR_SWITCH_ERROR;
  2836. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2837. }
  2838. if (hw_error & CAM_ISP_HW_ERROR_CSID_LANE_FIFO_OVERFLOW) {
  2839. err_code |= CAM_REQ_MGR_CSID_LANE_FIFO_OVERFLOW_ERROR;
  2840. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2841. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2842. }
  2843. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_HDR_CORRUPTED) {
  2844. err_code |= CAM_REQ_MGR_CSID_RX_PKT_HDR_CORRUPTION;
  2845. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2846. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2847. }
  2848. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_PKT_HDR_DATA) {
  2849. err_code |= CAM_REQ_MGR_CSID_MISSING_PKT_HDR_DATA;
  2850. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2851. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2852. }
  2853. if (hw_error & CAM_ISP_HW_ERROR_CSID_UNBOUNDED_FRAME) {
  2854. err_code |= CAM_REQ_MGR_CSID_UNBOUNDED_FRAME;
  2855. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2856. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2857. }
  2858. if (hw_error & CAM_ISP_HW_ERROR_CSID_FRAME_SIZE) {
  2859. err_code |= CAM_REQ_MGR_CSID_PIXEL_COUNT_MISMATCH;
  2860. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2861. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2862. }
  2863. if (hw_error & CAM_ISP_HW_ERROR_CSID_MISSING_EOT) {
  2864. err_code |= CAM_REQ_MGR_CSID_MISSING_EOT;
  2865. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2866. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2867. }
  2868. if (hw_error & CAM_ISP_HW_ERROR_CSID_PKT_PAYLOAD_CORRUPTED) {
  2869. err_code |= CAM_REQ_MGR_CSID_RX_PKT_PAYLOAD_CORRUPTION;
  2870. err_type = CAM_SYNC_ISP_EVENT_CSID_RX_ERROR;
  2871. recovery_type_temp |= CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2872. }
  2873. if (recovery_type_temp == (CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY |
  2874. CAM_REQ_MGR_ERROR_TYPE_RECOVERY))
  2875. recovery_type_temp = CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2876. if (!err_code)
  2877. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  2878. *req_mgr_err_code = err_code;
  2879. *fence_evt_cause = err_type;
  2880. *recovery_type = recovery_type_temp;
  2881. }
  2882. static bool __cam_isp_ctx_request_can_reapply(
  2883. struct cam_isp_ctx_req *req_isp)
  2884. {
  2885. int i;
  2886. for (i = 0; i < req_isp->num_fence_map_out; i++)
  2887. if (req_isp->fence_map_out[i].sync_id == -1)
  2888. return false;
  2889. return true;
  2890. }
  2891. static int __cam_isp_ctx_validate_for_req_reapply_util(
  2892. struct cam_isp_context *ctx_isp)
  2893. {
  2894. int rc = 0;
  2895. struct cam_ctx_request *req_temp;
  2896. struct cam_ctx_request *req = NULL;
  2897. struct cam_isp_ctx_req *req_isp = NULL;
  2898. struct cam_context *ctx = ctx_isp->base;
  2899. /* Check for req in active/wait lists */
  2900. if (list_empty(&ctx->active_req_list)) {
  2901. CAM_DBG(CAM_ISP,
  2902. "Active request list empty for ctx: %u on link: 0x%x",
  2903. ctx->ctx_id, ctx->link_hdl);
  2904. if (list_empty(&ctx->wait_req_list)) {
  2905. CAM_WARN(CAM_ISP,
  2906. "No active/wait req for ctx: %u on link: 0x%x",
  2907. ctx->ctx_id, ctx->link_hdl);
  2908. rc = -EINVAL;
  2909. goto end;
  2910. }
  2911. }
  2912. /* Validate if all fences for active requests are not signaled */
  2913. if (!list_empty(&ctx->active_req_list)) {
  2914. list_for_each_entry_safe_reverse(req, req_temp,
  2915. &ctx->active_req_list, list) {
  2916. /*
  2917. * If some fences of the active request are already
  2918. * signaled, we should not do recovery for the buffer
  2919. * and timestamp consistency.
  2920. */
  2921. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2922. if (!__cam_isp_ctx_request_can_reapply(req_isp)) {
  2923. CAM_WARN(CAM_ISP,
  2924. "Req: %llu in ctx:%u on link: 0x%x fence has partially signaled, cannot do recovery",
  2925. req->request_id, ctx->ctx_id, ctx->link_hdl);
  2926. rc = -EINVAL;
  2927. goto end;
  2928. }
  2929. }
  2930. }
  2931. /* Move active requests to pending list */
  2932. if (!list_empty(&ctx->active_req_list)) {
  2933. list_for_each_entry_safe_reverse(req, req_temp,
  2934. &ctx->active_req_list, list) {
  2935. list_del_init(&req->list);
  2936. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  2937. ctx_isp->active_req_cnt--;
  2938. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move active req %llu to pending",
  2939. ctx->ctx_id, ctx->link_hdl, req->request_id);
  2940. }
  2941. }
  2942. /* Move wait requests to pending list */
  2943. if (!list_empty(&ctx->wait_req_list)) {
  2944. list_for_each_entry_safe_reverse(req, req_temp, &ctx->wait_req_list, list) {
  2945. list_del_init(&req->list);
  2946. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  2947. CAM_DBG(CAM_ISP, "ctx:%u link:0x%x move wait req %llu to pending",
  2948. ctx->ctx_id, ctx->link_hdl, req->request_id);
  2949. }
  2950. }
  2951. end:
  2952. return rc;
  2953. }
  2954. static int __cam_isp_ctx_handle_recovery_req_util(
  2955. struct cam_isp_context *ctx_isp)
  2956. {
  2957. int rc = 0;
  2958. struct cam_context *ctx = ctx_isp->base;
  2959. struct cam_ctx_request *req_to_reapply = NULL;
  2960. struct cam_isp_ctx_req *req_isp = NULL;
  2961. req_to_reapply = list_first_entry(&ctx->pending_req_list,
  2962. struct cam_ctx_request, list);
  2963. req_isp = (struct cam_isp_ctx_req *)req_to_reapply->req_priv;
  2964. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2965. ctx_isp->recovery_req_id = req_to_reapply->request_id;
  2966. atomic_set(&ctx_isp->internal_recovery_set, 1);
  2967. CAM_INFO(CAM_ISP, "Notify CRM to reapply req:%llu for ctx:%u link:0x%x",
  2968. req_to_reapply->request_id, ctx->ctx_id, ctx->link_hdl);
  2969. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF,
  2970. CRM_KMD_WARN_INTERNAL_RECOVERY, req_to_reapply->request_id,
  2971. ctx_isp);
  2972. if (rc) {
  2973. /* Unable to notify CRM to do reapply back to normal */
  2974. CAM_WARN(CAM_ISP,
  2975. "ctx:%u unable to notify CRM for req %llu",
  2976. ctx->ctx_id, ctx_isp->recovery_req_id);
  2977. ctx_isp->recovery_req_id = 0;
  2978. atomic_set(&ctx_isp->internal_recovery_set, 0);
  2979. }
  2980. return rc;
  2981. }
  2982. static int __cam_isp_ctx_trigger_error_req_reapply(
  2983. struct cam_isp_context *ctx_isp)
  2984. {
  2985. int rc = 0;
  2986. struct cam_context *ctx = ctx_isp->base;
  2987. /*
  2988. * For errors that can be recoverable within kmd, we
  2989. * try to do internal hw stop, restart and notify CRM
  2990. * to do reapply with the help of bubble control flow.
  2991. */
  2992. rc = __cam_isp_ctx_validate_for_req_reapply_util(ctx_isp);
  2993. if (rc)
  2994. goto end;
  2995. rc = __cam_isp_ctx_handle_recovery_req_util(ctx_isp);
  2996. if (rc)
  2997. goto end;
  2998. CAM_DBG(CAM_ISP, "Triggered internal recovery for req:%llu ctx:%u on link 0x%x",
  2999. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3000. end:
  3001. return rc;
  3002. }
  3003. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  3004. void *evt_data)
  3005. {
  3006. int rc = 0;
  3007. enum cam_req_mgr_device_error error;
  3008. uint32_t i = 0;
  3009. bool found = 0;
  3010. struct cam_ctx_request *req = NULL;
  3011. struct cam_ctx_request *req_to_report = NULL;
  3012. struct cam_ctx_request *req_to_dump = NULL;
  3013. struct cam_ctx_request *req_temp;
  3014. struct cam_isp_ctx_req *req_isp = NULL;
  3015. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  3016. uint64_t error_request_id;
  3017. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  3018. uint32_t recovery_type, fence_evt_cause;
  3019. uint32_t req_mgr_err_code;
  3020. struct cam_context *ctx = ctx_isp->base;
  3021. struct cam_isp_hw_error_event_data *error_event_data =
  3022. (struct cam_isp_hw_error_event_data *)evt_data;
  3023. CAM_DBG(CAM_ISP, "Enter HW error_type = %d", error_event_data->error_type);
  3024. if (error_event_data->try_internal_recovery) {
  3025. rc = __cam_isp_ctx_trigger_error_req_reapply(ctx_isp);
  3026. if (!rc)
  3027. goto exit;
  3028. }
  3029. if (!ctx_isp->offline_context)
  3030. __cam_isp_ctx_pause_crm_timer(ctx);
  3031. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3032. get_notification_evt_params(error_event_data->error_type, &fence_evt_cause,
  3033. &req_mgr_err_code, &recovery_type);
  3034. /*
  3035. * The error is likely caused by first request on the active list.
  3036. * If active list is empty check wait list (maybe error hit as soon
  3037. * as RUP and we handle error before RUP.
  3038. */
  3039. if (list_empty(&ctx->active_req_list)) {
  3040. CAM_DBG(CAM_ISP,
  3041. "handling error with no active request");
  3042. if (list_empty(&ctx->wait_req_list)) {
  3043. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3044. "Error with no active/wait request");
  3045. goto end;
  3046. } else {
  3047. req_to_dump = list_first_entry(&ctx->wait_req_list,
  3048. struct cam_ctx_request, list);
  3049. }
  3050. } else {
  3051. req_to_dump = list_first_entry(&ctx->active_req_list,
  3052. struct cam_ctx_request, list);
  3053. }
  3054. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  3055. if (error_event_data->enable_req_dump)
  3056. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  3057. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3058. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  3059. list_for_each_entry_safe(req, req_temp,
  3060. &ctx->active_req_list, list) {
  3061. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3062. if (!req_isp->bubble_report) {
  3063. CAM_ERR(CAM_ISP, "signalled error for req %llu",
  3064. req->request_id);
  3065. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3066. fence_map_out =
  3067. &req_isp->fence_map_out[i];
  3068. if (req_isp->fence_map_out[i].sync_id != -1) {
  3069. CAM_DBG(CAM_ISP,
  3070. "req %llu, Sync fd 0x%x ctx %u",
  3071. req->request_id,
  3072. req_isp->fence_map_out[i].sync_id,
  3073. ctx->ctx_id);
  3074. rc = cam_sync_signal(
  3075. fence_map_out->sync_id,
  3076. CAM_SYNC_STATE_SIGNALED_ERROR,
  3077. fence_evt_cause);
  3078. fence_map_out->sync_id = -1;
  3079. }
  3080. }
  3081. list_del_init(&req->list);
  3082. list_add_tail(&req->list, &ctx->free_req_list);
  3083. ctx_isp->active_req_cnt--;
  3084. } else {
  3085. found = 1;
  3086. break;
  3087. }
  3088. }
  3089. if (found)
  3090. goto move_to_pending;
  3091. list_for_each_entry_safe(req, req_temp,
  3092. &ctx->wait_req_list, list) {
  3093. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3094. if (!req_isp->bubble_report) {
  3095. CAM_ERR(CAM_ISP, "signalled error for req %llu",
  3096. req->request_id);
  3097. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3098. fence_map_out =
  3099. &req_isp->fence_map_out[i];
  3100. if (req_isp->fence_map_out[i].sync_id != -1) {
  3101. CAM_DBG(CAM_ISP,
  3102. "req %llu, Sync fd 0x%x ctx %u",
  3103. req->request_id,
  3104. req_isp->fence_map_out[i].sync_id,
  3105. ctx->ctx_id);
  3106. rc = cam_sync_signal(
  3107. fence_map_out->sync_id,
  3108. CAM_SYNC_STATE_SIGNALED_ERROR,
  3109. fence_evt_cause);
  3110. fence_map_out->sync_id = -1;
  3111. }
  3112. }
  3113. list_del_init(&req->list);
  3114. list_add_tail(&req->list, &ctx->free_req_list);
  3115. } else {
  3116. found = 1;
  3117. break;
  3118. }
  3119. }
  3120. move_to_pending:
  3121. /*
  3122. * If bubble recovery is enabled on any request we need to move that
  3123. * request and all the subsequent requests to the pending list.
  3124. * Note:
  3125. * We need to traverse the active list in reverse order and add
  3126. * to head of pending list.
  3127. * e.g. pending current state: 10, 11 | active current state: 8, 9
  3128. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  3129. * final state - pending: 8, 9, 10, 11 | active: NULL
  3130. */
  3131. if (found) {
  3132. list_for_each_entry_safe_reverse(req, req_temp,
  3133. &ctx->active_req_list, list) {
  3134. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3135. list_del_init(&req->list);
  3136. list_add(&req->list, &ctx->pending_req_list);
  3137. ctx_isp->active_req_cnt--;
  3138. }
  3139. list_for_each_entry_safe_reverse(req, req_temp,
  3140. &ctx->wait_req_list, list) {
  3141. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3142. list_del_init(&req->list);
  3143. list_add(&req->list, &ctx->pending_req_list);
  3144. }
  3145. }
  3146. end:
  3147. do {
  3148. if (list_empty(&ctx->pending_req_list)) {
  3149. error_request_id = ctx_isp->last_applied_req_id;
  3150. req_isp = NULL;
  3151. break;
  3152. }
  3153. req = list_first_entry(&ctx->pending_req_list,
  3154. struct cam_ctx_request, list);
  3155. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3156. error_request_id = ctx_isp->last_applied_req_id;
  3157. if (req_isp->bubble_report) {
  3158. req_to_report = req;
  3159. req_isp_to_report = req_to_report->req_priv;
  3160. break;
  3161. }
  3162. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3163. if (req_isp->fence_map_out[i].sync_id != -1)
  3164. rc = cam_sync_signal(
  3165. req_isp->fence_map_out[i].sync_id,
  3166. CAM_SYNC_STATE_SIGNALED_ERROR,
  3167. fence_evt_cause);
  3168. req_isp->fence_map_out[i].sync_id = -1;
  3169. }
  3170. list_del_init(&req->list);
  3171. list_add_tail(&req->list, &ctx->free_req_list);
  3172. } while (req->request_id < ctx_isp->last_applied_req_id);
  3173. if (ctx_isp->offline_context)
  3174. goto exit;
  3175. error = CRM_KMD_ERR_FATAL;
  3176. if (req_isp_to_report && req_isp_to_report->bubble_report)
  3177. if (error_event_data->recovery_enabled)
  3178. error = CRM_KMD_ERR_BUBBLE;
  3179. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, error,
  3180. error_request_id, ctx_isp);
  3181. /*
  3182. * Need to send error occurred in KMD
  3183. * This will help UMD to take necessary action
  3184. * and to dump relevant info
  3185. */
  3186. if (error == CRM_KMD_ERR_FATAL)
  3187. __cam_isp_ctx_notify_v4l2_error_event(recovery_type,
  3188. req_mgr_err_code, error_request_id, ctx);
  3189. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  3190. CAM_DBG(CAM_ISP, "Handling error done on ctx: %u", ctx->ctx_id);
  3191. exit:
  3192. return rc;
  3193. }
  3194. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  3195. struct cam_isp_context *ctx_isp, void *evt_data)
  3196. {
  3197. int rc = 0;
  3198. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3199. struct cam_ctx_request *req;
  3200. struct cam_context *ctx = ctx_isp->base;
  3201. uint64_t request_id = 0;
  3202. if (!evt_data) {
  3203. CAM_ERR(CAM_ISP, "in valid sof event data");
  3204. return -EINVAL;
  3205. }
  3206. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  3207. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3208. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3209. if (!(list_empty(&ctx->wait_req_list)))
  3210. goto end;
  3211. if (ctx_isp->active_req_cnt <= 2) {
  3212. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3213. list_for_each_entry(req, &ctx->active_req_list, list) {
  3214. if (req->request_id > ctx_isp->reported_req_id) {
  3215. request_id = req->request_id;
  3216. ctx_isp->reported_req_id = request_id;
  3217. break;
  3218. }
  3219. }
  3220. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3221. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3222. }
  3223. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3224. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  3225. end:
  3226. return rc;
  3227. }
  3228. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  3229. void *evt_data)
  3230. {
  3231. int rc = 0;
  3232. struct cam_isp_hw_done_event_data *done =
  3233. (struct cam_isp_hw_done_event_data *) evt_data;
  3234. struct cam_context *ctx = ctx_isp->base;
  3235. int prev_active_req_cnt = 0;
  3236. int curr_req_id = 0;
  3237. struct cam_ctx_request *req;
  3238. prev_active_req_cnt = ctx_isp->active_req_cnt;
  3239. req = list_first_entry(&ctx->active_req_list,
  3240. struct cam_ctx_request, list);
  3241. if (req)
  3242. curr_req_id = req->request_id;
  3243. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  3244. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  3245. if (list_empty(&ctx->wait_req_list) &&
  3246. list_empty(&ctx->active_req_list)) {
  3247. CAM_DBG(CAM_ISP, "No request, move to SOF");
  3248. ctx_isp->substate_activated =
  3249. CAM_ISP_CTX_ACTIVATED_SOF;
  3250. if (ctx_isp->reported_req_id < curr_req_id) {
  3251. ctx_isp->reported_req_id = curr_req_id;
  3252. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  3253. curr_req_id,
  3254. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3255. }
  3256. }
  3257. }
  3258. return rc;
  3259. }
  3260. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  3261. void *evt_data)
  3262. {
  3263. int rc = 0;
  3264. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3265. return rc;
  3266. }
  3267. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  3268. struct cam_isp_context *ctx_isp,
  3269. void *evt_data)
  3270. {
  3271. int rc = 0;
  3272. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  3273. return rc;
  3274. }
  3275. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  3276. void *evt_data)
  3277. {
  3278. int rc = 0;
  3279. struct cam_ctx_request *req = NULL;
  3280. struct cam_isp_ctx_req *req_isp;
  3281. struct cam_context *ctx = ctx_isp->base;
  3282. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  3283. CAM_DBG(CAM_ISP, "invalid RUP");
  3284. goto end;
  3285. }
  3286. /*
  3287. * This is for the first update. The initial setting will
  3288. * cause the reg_upd in the first frame.
  3289. */
  3290. if (!list_empty(&ctx->wait_req_list)) {
  3291. req = list_first_entry(&ctx->wait_req_list,
  3292. struct cam_ctx_request, list);
  3293. list_del_init(&req->list);
  3294. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3295. if (req_isp->num_fence_map_out == req_isp->num_acked)
  3296. list_add_tail(&req->list, &ctx->free_req_list);
  3297. else
  3298. CAM_ERR(CAM_ISP,
  3299. "receive rup in unexpected state");
  3300. }
  3301. if (req != NULL) {
  3302. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3303. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3304. req->request_id);
  3305. }
  3306. end:
  3307. return rc;
  3308. }
  3309. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  3310. struct cam_isp_context *ctx_isp, void *evt_data)
  3311. {
  3312. int rc = 0;
  3313. struct cam_ctx_request *req = NULL;
  3314. struct cam_context *ctx = ctx_isp->base;
  3315. struct cam_isp_ctx_req *req_isp;
  3316. uint64_t request_id = 0;
  3317. if (list_empty(&ctx->wait_req_list)) {
  3318. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  3319. goto end;
  3320. }
  3321. req = list_first_entry(&ctx->wait_req_list,
  3322. struct cam_ctx_request, list);
  3323. list_del_init(&req->list);
  3324. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3325. if (req_isp->num_fence_map_out != 0) {
  3326. list_add_tail(&req->list, &ctx->active_req_list);
  3327. ctx_isp->active_req_cnt++;
  3328. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  3329. req->request_id, ctx_isp->active_req_cnt);
  3330. } else {
  3331. /* no io config, so the request is completed. */
  3332. list_add_tail(&req->list, &ctx->free_req_list);
  3333. }
  3334. /*
  3335. * This function only called directly from applied and bubble applied
  3336. * state so change substate here.
  3337. */
  3338. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  3339. if (req_isp->num_fence_map_out != 1)
  3340. goto end;
  3341. if (ctx_isp->active_req_cnt <= 2) {
  3342. list_for_each_entry(req, &ctx->active_req_list, list) {
  3343. if (req->request_id > ctx_isp->reported_req_id) {
  3344. request_id = req->request_id;
  3345. ctx_isp->reported_req_id = request_id;
  3346. break;
  3347. }
  3348. }
  3349. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3350. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3351. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3352. }
  3353. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3354. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated));
  3355. end:
  3356. if (req != NULL && !rc) {
  3357. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3358. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  3359. req->request_id);
  3360. }
  3361. return rc;
  3362. }
  3363. static void __cam_isp_ctx_notify_aeb_error_for_sec_event(
  3364. struct cam_isp_context *ctx_isp)
  3365. {
  3366. struct cam_context *ctx = ctx_isp->base;
  3367. if ((++ctx_isp->aeb_error_cnt) <= CAM_ISP_CONTEXT_AEB_ERROR_CNT_MAX) {
  3368. CAM_WARN(CAM_ISP,
  3369. "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",
  3370. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id, ctx_isp->aeb_error_cnt);
  3371. return;
  3372. }
  3373. CAM_ERR(CAM_ISP,
  3374. "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",
  3375. ctx->ctx_id, ctx->link_hdl, ctx_isp->aeb_error_cnt);
  3376. /* Pause CRM timer */
  3377. if (!ctx_isp->offline_context)
  3378. __cam_isp_ctx_pause_crm_timer(ctx);
  3379. /* Trigger reg dump */
  3380. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  3381. /* Notify CRM on fatal error */
  3382. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_FATAL,
  3383. ctx_isp->last_applied_req_id, ctx_isp);
  3384. /* Notify userland on error */
  3385. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_ERROR_TYPE_RECOVERY,
  3386. CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING, ctx_isp->last_applied_req_id, ctx);
  3387. /* Change state to HALT, stop further processing of HW events */
  3388. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3389. }
  3390. static int __cam_isp_ctx_trigger_internal_recovery(
  3391. bool sync_frame_drop, struct cam_isp_context *ctx_isp)
  3392. {
  3393. int rc = 0;
  3394. bool do_recovery = true;
  3395. struct cam_context *ctx = ctx_isp->base;
  3396. struct cam_ctx_request *req = NULL;
  3397. struct cam_isp_ctx_req *req_isp = NULL;
  3398. if (list_empty(&ctx->wait_req_list)) {
  3399. /*
  3400. * If the wait list is empty, and we encounter a "silent" frame drop
  3401. * then the settings applied on the previous frame, did not reflect
  3402. * at the next frame boundary, it's expected to latch a frame after.
  3403. * No need to recover. If it's an out of sync drop use pending req
  3404. */
  3405. if (sync_frame_drop && !list_empty(&ctx->pending_req_list))
  3406. req = list_first_entry(&ctx->pending_req_list,
  3407. struct cam_ctx_request, list);
  3408. else
  3409. do_recovery = false;
  3410. }
  3411. /* If both wait and pending list have no request to recover on */
  3412. if (!do_recovery) {
  3413. CAM_WARN(CAM_ISP,
  3414. "No request to perform recovery - ctx: %u on link: 0x%x last_applied: %lld last_buf_done: %lld",
  3415. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  3416. ctx_isp->req_info.last_bufdone_req_id);
  3417. goto end;
  3418. }
  3419. if (!req) {
  3420. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  3421. if (req->request_id != ctx_isp->last_applied_req_id)
  3422. CAM_WARN(CAM_ISP,
  3423. "Top of wait list req: %llu does not match with last applied: %llu in ctx: %u on link: 0x%x",
  3424. req->request_id, ctx_isp->last_applied_req_id,
  3425. ctx->ctx_id, ctx->link_hdl);
  3426. }
  3427. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3428. /*
  3429. * Treat this as bubble, after recovery re-start from appropriate sub-state
  3430. * This will block servicing any further apply calls from CRM
  3431. */
  3432. atomic_set(&ctx_isp->internal_recovery_set, 1);
  3433. atomic_set(&ctx_isp->process_bubble, 1);
  3434. ctx_isp->recovery_req_id = req->request_id;
  3435. /* Wait for active request's to finish before issuing recovery */
  3436. if (ctx_isp->active_req_cnt) {
  3437. req_isp->bubble_detected = true;
  3438. CAM_WARN(CAM_ISP,
  3439. "Active req cnt: %u wait for all buf dones before kicking in recovery on req: %lld ctx: %u on link: 0x%x",
  3440. ctx_isp->active_req_cnt, ctx_isp->recovery_req_id,
  3441. ctx->ctx_id, ctx->link_hdl);
  3442. } else {
  3443. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3444. ctx_isp->recovery_req_id, ctx_isp);
  3445. if (rc) {
  3446. /* Unable to do bubble recovery reset back to normal */
  3447. CAM_WARN(CAM_ISP,
  3448. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  3449. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  3450. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  3451. goto end;
  3452. }
  3453. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  3454. list_del_init(&req->list);
  3455. list_add(&req->list, &ctx->pending_req_list);
  3456. }
  3457. end:
  3458. return rc;
  3459. }
  3460. static int __cam_isp_ctx_handle_secondary_events(
  3461. struct cam_isp_context *ctx_isp, void *evt_data)
  3462. {
  3463. int rc = 0;
  3464. bool recover = false, sync_frame_drop = false;
  3465. struct cam_context *ctx = ctx_isp->base;
  3466. struct cam_isp_hw_secondary_event_data *sec_evt_data =
  3467. (struct cam_isp_hw_secondary_event_data *)evt_data;
  3468. /* Current scheme to handle only for custom AEB */
  3469. if (!ctx_isp->aeb_enabled) {
  3470. CAM_WARN(CAM_ISP,
  3471. "Recovery not supported for non-AEB ctx: %u on link: 0x%x reject sec evt: %u",
  3472. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  3473. goto end;
  3474. }
  3475. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  3476. CAM_WARN(CAM_ISP,
  3477. "Internal recovery in progress in ctx: %u on link: 0x%x reject sec evt: %u",
  3478. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  3479. goto end;
  3480. }
  3481. /*
  3482. * In case of custom AEB ensure first exposure frame has
  3483. * not moved forward with its settings without second/third
  3484. * expoure frame coming in. Also track for bubble, in case of system
  3485. * delays it's possible for the IFE settings to be not written to
  3486. * HW on a given frame. If these scenarios occurs flag as error,
  3487. * and recover.
  3488. */
  3489. switch (sec_evt_data->evt_type) {
  3490. case CAM_ISP_HW_SEC_EVENT_SOF:
  3491. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3492. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  3493. ctx_isp->last_applied_req_id);
  3494. /* Slave RDI's frame starting post IFE EPOCH - Fatal */
  3495. if ((ctx_isp->substate_activated ==
  3496. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  3497. (ctx_isp->substate_activated ==
  3498. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  3499. __cam_isp_ctx_notify_aeb_error_for_sec_event(ctx_isp);
  3500. else
  3501. /* Reset error count */
  3502. ctx_isp->aeb_error_cnt = 0;
  3503. break;
  3504. case CAM_ISP_HW_SEC_EVENT_EPOCH:
  3505. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3506. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  3507. ctx_isp->last_applied_req_id);
  3508. /*
  3509. * Master RDI frame dropped in CSID, due to programming delay no RUP/AUP
  3510. * On such occasions use CSID CAMIF EPOCH for bubble detection, flag
  3511. * on detection and perform necessary bubble recovery
  3512. */
  3513. if ((ctx_isp->substate_activated ==
  3514. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  3515. (ctx_isp->substate_activated ==
  3516. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED)) {
  3517. recover = true;
  3518. CAM_WARN(CAM_ISP,
  3519. "Programming delay input frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  3520. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  3521. }
  3522. break;
  3523. case CAM_ISP_HW_SEC_EVENT_OUT_OF_SYNC_FRAME_DROP:
  3524. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3525. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  3526. ctx_isp->last_applied_req_id);
  3527. /* Avoid recovery loop if frame is dropped at stream on */
  3528. if (!ctx_isp->frame_id) {
  3529. CAM_ERR(CAM_ISP,
  3530. "Sensor sync [vc mismatch] frame dropped at stream on ctx: %u link: 0x%x frame_id: %u last_applied_req: %lld",
  3531. ctx->ctx_id, ctx->link_hdl,
  3532. ctx_isp->frame_id, ctx_isp->last_applied_req_id);
  3533. rc = -EPERM;
  3534. break;
  3535. }
  3536. recover = true;
  3537. sync_frame_drop = true;
  3538. CAM_WARN(CAM_ISP,
  3539. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  3540. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  3541. break;
  3542. default:
  3543. break;
  3544. }
  3545. if (recover && ctx_isp->do_internal_recovery)
  3546. rc = __cam_isp_ctx_trigger_internal_recovery(sync_frame_drop, ctx_isp);
  3547. end:
  3548. return rc;
  3549. }
  3550. static struct cam_isp_ctx_irq_ops
  3551. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3552. /* SOF */
  3553. {
  3554. .irq_ops = {
  3555. __cam_isp_ctx_handle_error,
  3556. __cam_isp_ctx_sof_in_activated_state,
  3557. __cam_isp_ctx_reg_upd_in_sof,
  3558. __cam_isp_ctx_notify_sof_in_activated_state,
  3559. __cam_isp_ctx_notify_eof_in_activated_state,
  3560. __cam_isp_ctx_buf_done_in_sof,
  3561. __cam_isp_ctx_handle_secondary_events,
  3562. },
  3563. },
  3564. /* APPLIED */
  3565. {
  3566. .irq_ops = {
  3567. __cam_isp_ctx_handle_error,
  3568. __cam_isp_ctx_sof_in_activated_state,
  3569. __cam_isp_ctx_reg_upd_in_applied_state,
  3570. __cam_isp_ctx_epoch_in_applied,
  3571. __cam_isp_ctx_notify_eof_in_activated_state,
  3572. __cam_isp_ctx_buf_done_in_applied,
  3573. __cam_isp_ctx_handle_secondary_events,
  3574. },
  3575. },
  3576. /* EPOCH */
  3577. {
  3578. .irq_ops = {
  3579. __cam_isp_ctx_handle_error,
  3580. __cam_isp_ctx_sof_in_epoch,
  3581. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3582. __cam_isp_ctx_notify_sof_in_activated_state,
  3583. __cam_isp_ctx_notify_eof_in_activated_state,
  3584. __cam_isp_ctx_buf_done_in_epoch,
  3585. __cam_isp_ctx_handle_secondary_events,
  3586. },
  3587. },
  3588. /* BUBBLE */
  3589. {
  3590. .irq_ops = {
  3591. __cam_isp_ctx_handle_error,
  3592. __cam_isp_ctx_sof_in_activated_state,
  3593. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3594. __cam_isp_ctx_notify_sof_in_activated_state,
  3595. __cam_isp_ctx_notify_eof_in_activated_state,
  3596. __cam_isp_ctx_buf_done_in_bubble,
  3597. __cam_isp_ctx_handle_secondary_events,
  3598. },
  3599. },
  3600. /* Bubble Applied */
  3601. {
  3602. .irq_ops = {
  3603. __cam_isp_ctx_handle_error,
  3604. __cam_isp_ctx_sof_in_activated_state,
  3605. __cam_isp_ctx_reg_upd_in_applied_state,
  3606. __cam_isp_ctx_epoch_in_bubble_applied,
  3607. NULL,
  3608. __cam_isp_ctx_buf_done_in_bubble_applied,
  3609. __cam_isp_ctx_handle_secondary_events,
  3610. },
  3611. },
  3612. /* HW ERROR */
  3613. {
  3614. .irq_ops = {
  3615. NULL,
  3616. __cam_isp_ctx_sof_in_activated_state,
  3617. __cam_isp_ctx_reg_upd_in_hw_error,
  3618. NULL,
  3619. NULL,
  3620. NULL,
  3621. },
  3622. },
  3623. /* HALT */
  3624. {
  3625. },
  3626. };
  3627. static struct cam_isp_ctx_irq_ops
  3628. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3629. /* SOF */
  3630. {
  3631. .irq_ops = {
  3632. __cam_isp_ctx_handle_error,
  3633. __cam_isp_ctx_fs2_sof_in_sof_state,
  3634. __cam_isp_ctx_fs2_reg_upd_in_sof,
  3635. __cam_isp_ctx_fs2_sof_in_sof_state,
  3636. __cam_isp_ctx_notify_eof_in_activated_state,
  3637. NULL,
  3638. },
  3639. },
  3640. /* APPLIED */
  3641. {
  3642. .irq_ops = {
  3643. __cam_isp_ctx_handle_error,
  3644. __cam_isp_ctx_sof_in_activated_state,
  3645. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  3646. __cam_isp_ctx_epoch_in_applied,
  3647. __cam_isp_ctx_notify_eof_in_activated_state,
  3648. __cam_isp_ctx_fs2_buf_done_in_applied,
  3649. },
  3650. },
  3651. /* EPOCH */
  3652. {
  3653. .irq_ops = {
  3654. __cam_isp_ctx_handle_error,
  3655. __cam_isp_ctx_sof_in_epoch,
  3656. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3657. __cam_isp_ctx_notify_sof_in_activated_state,
  3658. __cam_isp_ctx_notify_eof_in_activated_state,
  3659. __cam_isp_ctx_fs2_buf_done_in_epoch,
  3660. },
  3661. },
  3662. /* BUBBLE */
  3663. {
  3664. .irq_ops = {
  3665. __cam_isp_ctx_handle_error,
  3666. __cam_isp_ctx_sof_in_activated_state,
  3667. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3668. __cam_isp_ctx_notify_sof_in_activated_state,
  3669. __cam_isp_ctx_notify_eof_in_activated_state,
  3670. __cam_isp_ctx_buf_done_in_bubble,
  3671. },
  3672. },
  3673. /* Bubble Applied */
  3674. {
  3675. .irq_ops = {
  3676. __cam_isp_ctx_handle_error,
  3677. __cam_isp_ctx_sof_in_activated_state,
  3678. __cam_isp_ctx_reg_upd_in_applied_state,
  3679. __cam_isp_ctx_epoch_in_bubble_applied,
  3680. NULL,
  3681. __cam_isp_ctx_buf_done_in_bubble_applied,
  3682. },
  3683. },
  3684. /* HW ERROR */
  3685. {
  3686. .irq_ops = {
  3687. NULL,
  3688. __cam_isp_ctx_sof_in_activated_state,
  3689. __cam_isp_ctx_reg_upd_in_hw_error,
  3690. NULL,
  3691. NULL,
  3692. NULL,
  3693. },
  3694. },
  3695. /* HALT */
  3696. {
  3697. },
  3698. };
  3699. static struct cam_isp_ctx_irq_ops
  3700. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3701. /* SOF */
  3702. {
  3703. .irq_ops = {
  3704. __cam_isp_ctx_handle_error,
  3705. NULL,
  3706. NULL,
  3707. NULL,
  3708. NULL,
  3709. NULL,
  3710. },
  3711. },
  3712. /* APPLIED */
  3713. {
  3714. .irq_ops = {
  3715. __cam_isp_ctx_handle_error,
  3716. __cam_isp_ctx_sof_in_activated_state,
  3717. __cam_isp_ctx_reg_upd_in_applied_state,
  3718. __cam_isp_ctx_offline_epoch_in_activated_state,
  3719. NULL,
  3720. __cam_isp_ctx_buf_done_in_applied,
  3721. },
  3722. },
  3723. /* EPOCH */
  3724. {
  3725. .irq_ops = {
  3726. __cam_isp_ctx_handle_error,
  3727. __cam_isp_ctx_sof_in_activated_state,
  3728. NULL,
  3729. __cam_isp_ctx_offline_epoch_in_activated_state,
  3730. NULL,
  3731. __cam_isp_ctx_buf_done_in_epoch,
  3732. },
  3733. },
  3734. /* BUBBLE */
  3735. {
  3736. },
  3737. /* Bubble Applied */
  3738. {
  3739. },
  3740. /* HW ERROR */
  3741. {
  3742. .irq_ops = {
  3743. NULL,
  3744. __cam_isp_ctx_sof_in_activated_state,
  3745. __cam_isp_ctx_reg_upd_in_hw_error,
  3746. NULL,
  3747. NULL,
  3748. NULL,
  3749. },
  3750. },
  3751. /* HALT */
  3752. {
  3753. },
  3754. };
  3755. static inline int cam_isp_context_apply_error(struct cam_hw_err_param *err_params,
  3756. uint64_t current_req_id, struct cam_context *ctx)
  3757. {
  3758. int rc = 0;
  3759. if (err_params->err_req_id == current_req_id) {
  3760. CAM_INFO(CAM_ISP,
  3761. "Err inject for req: %llu, err_code: %d, err_type: %d ctx id: %d",
  3762. current_req_id, err_params->err_code, err_params->err_type, ctx->ctx_id);
  3763. __cam_isp_ctx_notify_v4l2_error_event(err_params->err_type, err_params->err_code,
  3764. current_req_id, ctx);
  3765. memset(err_params, 0, sizeof(struct cam_hw_err_param));
  3766. return rc;
  3767. }
  3768. return -EINVAL;
  3769. }
  3770. static int __cam_isp_ctx_apply_req_in_activated_state(
  3771. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  3772. enum cam_isp_ctx_activated_substate next_state)
  3773. {
  3774. int rc = 0;
  3775. struct cam_ctx_request *req;
  3776. struct cam_ctx_request *active_req = NULL;
  3777. struct cam_isp_ctx_req *req_isp;
  3778. struct cam_isp_ctx_req *active_req_isp;
  3779. struct cam_isp_context *ctx_isp = NULL;
  3780. struct cam_hw_config_args cfg = {0};
  3781. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3782. if (apply->re_apply)
  3783. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  3784. CAM_INFO_RATE_LIMIT(CAM_ISP,
  3785. "ctx_id:%d Trying to reapply the same request %llu again",
  3786. ctx->ctx_id,
  3787. apply->request_id);
  3788. return 0;
  3789. }
  3790. if (list_empty(&ctx->pending_req_list)) {
  3791. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3792. "ctx_id:%d No available request for Apply id %lld",
  3793. ctx->ctx_id,
  3794. apply->request_id);
  3795. rc = -EFAULT;
  3796. goto end;
  3797. }
  3798. /*
  3799. * When the pipeline has issue, the requests can be queued up in the
  3800. * pipeline. In this case, we should reject the additional request.
  3801. * The maximum number of request allowed to be outstanding is 2.
  3802. *
  3803. */
  3804. if (atomic_read(&ctx_isp->process_bubble)) {
  3805. CAM_INFO_RATE_LIMIT(CAM_ISP,
  3806. "ctx_id:%d Processing bubble cannot apply Request Id %llu",
  3807. ctx->ctx_id,
  3808. apply->request_id);
  3809. rc = -EFAULT;
  3810. goto end;
  3811. }
  3812. /*
  3813. * When isp processing internal recovery, the crm may still apply
  3814. * req to isp ctx. In this case, we should reject this req apply.
  3815. */
  3816. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  3817. CAM_INFO_RATE_LIMIT(CAM_ISP,
  3818. "ctx_id:%d Processing recovery cannot apply Request Id %lld",
  3819. ctx->ctx_id,
  3820. apply->request_id);
  3821. rc = -EAGAIN;
  3822. goto end;
  3823. }
  3824. spin_lock_bh(&ctx->lock);
  3825. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  3826. list);
  3827. spin_unlock_bh(&ctx->lock);
  3828. /*
  3829. * Check whether the request id is matching the tip, if not, this means
  3830. * we are in the middle of the error handling. Need to reject this apply
  3831. */
  3832. if (req->request_id != apply->request_id) {
  3833. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3834. "ctx_id:%d Invalid Request Id asking %llu existing %llu",
  3835. ctx->ctx_id,
  3836. apply->request_id, req->request_id);
  3837. rc = -EFAULT;
  3838. goto end;
  3839. }
  3840. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u",
  3841. req->request_id,
  3842. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  3843. ctx->ctx_id);
  3844. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3845. if (ctx_isp->active_req_cnt >= 2) {
  3846. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3847. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  3848. req->request_id,
  3849. ctx_isp->active_req_cnt,
  3850. ctx->ctx_id);
  3851. spin_lock_bh(&ctx->lock);
  3852. if (!list_empty(&ctx->active_req_list))
  3853. active_req = list_first_entry(&ctx->active_req_list,
  3854. struct cam_ctx_request, list);
  3855. else
  3856. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3857. "WARNING: should not happen (cnt = %d) but active_list empty",
  3858. ctx_isp->active_req_cnt);
  3859. spin_unlock_bh(&ctx->lock);
  3860. if (active_req) {
  3861. active_req_isp =
  3862. (struct cam_isp_ctx_req *) active_req->req_priv;
  3863. __cam_isp_ctx_handle_buf_done_fail_log(
  3864. active_req->request_id, active_req_isp,
  3865. ctx_isp->isp_device_type);
  3866. }
  3867. rc = -EFAULT;
  3868. goto end;
  3869. }
  3870. req_isp->bubble_report = apply->report_if_bubble;
  3871. /*
  3872. * Reset all buf done/bubble flags for the req being applied
  3873. * If internal recovery has led to re-apply of same
  3874. * request, clear all stale entities
  3875. */
  3876. req_isp->num_acked = 0;
  3877. req_isp->num_deferred_acks = 0;
  3878. req_isp->cdm_reset_before_apply = false;
  3879. req_isp->bubble_detected = false;
  3880. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3881. cfg.request_id = req->request_id;
  3882. cfg.hw_update_entries = req_isp->cfg;
  3883. cfg.num_hw_update_entries = req_isp->num_cfg;
  3884. cfg.priv = &req_isp->hw_update_data;
  3885. cfg.init_packet = 0;
  3886. cfg.reapply_type = req_isp->reapply_type;
  3887. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  3888. if (ctx_isp->err_inject_params.is_valid) {
  3889. rc = cam_isp_context_apply_error(&(ctx_isp->err_inject_params),
  3890. req->request_id, ctx_isp->base);
  3891. if (!rc)
  3892. goto end;
  3893. }
  3894. atomic_set(&ctx_isp->apply_in_progress, 1);
  3895. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  3896. if (!rc) {
  3897. spin_lock_bh(&ctx->lock);
  3898. ctx_isp->substate_activated = next_state;
  3899. ctx_isp->last_applied_req_id = apply->request_id;
  3900. list_del_init(&req->list);
  3901. if (atomic_read(&ctx_isp->internal_recovery_set))
  3902. __cam_isp_ctx_enqueue_request_in_order(ctx, req, false);
  3903. else
  3904. list_add_tail(&req->list, &ctx->wait_req_list);
  3905. CAM_DBG(CAM_ISP, "new substate Substate[%s], applied req %lld",
  3906. __cam_isp_ctx_substate_val_to_type(next_state),
  3907. ctx_isp->last_applied_req_id);
  3908. spin_unlock_bh(&ctx->lock);
  3909. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3910. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  3911. req->request_id);
  3912. __cam_isp_ctx_update_event_record(ctx_isp,
  3913. CAM_ISP_CTX_EVENT_APPLY, req);
  3914. } else if (rc == -EALREADY) {
  3915. spin_lock_bh(&ctx->lock);
  3916. req_isp->bubble_detected = true;
  3917. req_isp->cdm_reset_before_apply = false;
  3918. atomic_set(&ctx_isp->process_bubble, 1);
  3919. list_del_init(&req->list);
  3920. list_add(&req->list, &ctx->active_req_list);
  3921. ctx_isp->active_req_cnt++;
  3922. spin_unlock_bh(&ctx->lock);
  3923. CAM_DBG(CAM_REQ,
  3924. "move request %lld to active list(cnt = %d), ctx %u",
  3925. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  3926. } else {
  3927. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3928. "ctx_id:%d ,Can not apply (req %lld) the configuration, rc %d",
  3929. ctx->ctx_id, apply->request_id, rc);
  3930. }
  3931. atomic_set(&ctx_isp->apply_in_progress, 0);
  3932. end:
  3933. return rc;
  3934. }
  3935. static int __cam_isp_ctx_apply_req_in_sof(
  3936. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3937. {
  3938. int rc = 0;
  3939. struct cam_isp_context *ctx_isp =
  3940. (struct cam_isp_context *) ctx->ctx_priv;
  3941. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3942. __cam_isp_ctx_substate_val_to_type(
  3943. ctx_isp->substate_activated));
  3944. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3945. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3946. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3947. __cam_isp_ctx_substate_val_to_type(
  3948. ctx_isp->substate_activated));
  3949. if (rc)
  3950. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3951. __cam_isp_ctx_substate_val_to_type(
  3952. ctx_isp->substate_activated), rc);
  3953. return rc;
  3954. }
  3955. static int __cam_isp_ctx_apply_req_in_epoch(
  3956. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3957. {
  3958. int rc = 0;
  3959. struct cam_isp_context *ctx_isp =
  3960. (struct cam_isp_context *) ctx->ctx_priv;
  3961. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3962. __cam_isp_ctx_substate_val_to_type(
  3963. ctx_isp->substate_activated));
  3964. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3965. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3966. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3967. __cam_isp_ctx_substate_val_to_type(
  3968. ctx_isp->substate_activated));
  3969. if (rc)
  3970. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3971. __cam_isp_ctx_substate_val_to_type(
  3972. ctx_isp->substate_activated), rc);
  3973. return rc;
  3974. }
  3975. static int __cam_isp_ctx_apply_req_in_bubble(
  3976. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3977. {
  3978. int rc = 0;
  3979. struct cam_isp_context *ctx_isp =
  3980. (struct cam_isp_context *) ctx->ctx_priv;
  3981. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3982. __cam_isp_ctx_substate_val_to_type(
  3983. ctx_isp->substate_activated));
  3984. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3985. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  3986. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3987. __cam_isp_ctx_substate_val_to_type(
  3988. ctx_isp->substate_activated));
  3989. if (rc)
  3990. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3991. __cam_isp_ctx_substate_val_to_type(
  3992. ctx_isp->substate_activated), rc);
  3993. return rc;
  3994. }
  3995. static int __cam_isp_ctx_apply_default_req_settings(
  3996. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3997. {
  3998. int rc = 0;
  3999. struct cam_isp_context *isp_ctx =
  4000. (struct cam_isp_context *) ctx->ctx_priv;
  4001. struct cam_hw_cmd_args hw_cmd_args;
  4002. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4003. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  4004. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4005. isp_hw_cmd_args.cmd_type =
  4006. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  4007. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4008. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4009. &hw_cmd_args);
  4010. if (rc)
  4011. CAM_ERR(CAM_ISP,
  4012. "Failed to apply default settings rc %d", rc);
  4013. else
  4014. CAM_DBG(CAM_ISP, "Applied default settings rc %d", rc);
  4015. return rc;
  4016. }
  4017. static void *cam_isp_ctx_user_dump_req_list(
  4018. void *dump_struct, uint8_t *addr_ptr)
  4019. {
  4020. struct list_head *head = NULL;
  4021. uint64_t *addr;
  4022. struct cam_ctx_request *req, *req_temp;
  4023. head = (struct list_head *)dump_struct;
  4024. addr = (uint64_t *)addr_ptr;
  4025. if (!list_empty(head)) {
  4026. list_for_each_entry_safe(req, req_temp, head, list) {
  4027. *addr++ = req->request_id;
  4028. }
  4029. }
  4030. return addr;
  4031. }
  4032. static void *cam_isp_ctx_user_dump_active_requests(
  4033. void *dump_struct, uint8_t *addr_ptr)
  4034. {
  4035. uint64_t *addr;
  4036. struct cam_ctx_request *req;
  4037. req = (struct cam_ctx_request *)dump_struct;
  4038. addr = (uint64_t *)addr_ptr;
  4039. *addr++ = req->request_id;
  4040. return addr;
  4041. }
  4042. static int __cam_isp_ctx_dump_req_info(
  4043. struct cam_context *ctx,
  4044. struct cam_ctx_request *req,
  4045. struct cam_common_hw_dump_args *dump_args)
  4046. {
  4047. int i, rc = 0;
  4048. uint32_t min_len;
  4049. size_t remain_len;
  4050. struct cam_isp_ctx_req *req_isp;
  4051. struct cam_isp_context *ctx_isp;
  4052. struct cam_ctx_request *req_temp;
  4053. if (!req || !ctx || !dump_args) {
  4054. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK",
  4055. req, ctx, dump_args);
  4056. return -EINVAL;
  4057. }
  4058. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4059. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  4060. if (dump_args->buf_len <= dump_args->offset) {
  4061. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  4062. dump_args->buf_len, dump_args->offset);
  4063. return -ENOSPC;
  4064. }
  4065. remain_len = dump_args->buf_len - dump_args->offset;
  4066. min_len = sizeof(struct cam_isp_context_dump_header) +
  4067. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  4068. req_isp->num_fence_map_out *
  4069. sizeof(uint64_t));
  4070. if (remain_len < min_len) {
  4071. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  4072. remain_len, min_len);
  4073. return -ENOSPC;
  4074. }
  4075. /* Dump pending request list */
  4076. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4077. &ctx->pending_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_PENDING_REQUESTS:");
  4078. if (rc) {
  4079. CAM_ERR(CAM_ISP, "CAM_ISP_CONTEXT: Pending request dump failed, rc: %d",
  4080. rc);
  4081. return rc;
  4082. }
  4083. /* Dump applied request list */
  4084. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4085. &ctx->wait_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_APPLIED_REQUESTS:");
  4086. if (rc) {
  4087. CAM_ERR(CAM_ISP, "CAM_ISP_CONTEXT: Applied request dump failed, rc: %d",
  4088. rc);
  4089. return rc;
  4090. }
  4091. /* Dump active request list */
  4092. rc = cam_common_user_dump_helper(dump_args, cam_isp_ctx_user_dump_req_list,
  4093. &ctx->active_req_list, sizeof(uint64_t), "ISP_OUT_FENCE_ACTIVE_REQUESTS:");
  4094. if (rc) {
  4095. CAM_ERR(CAM_ISP, "CAM_ISP_CONTEXT: Active request dump failed, rc: %d",
  4096. rc);
  4097. return rc;
  4098. }
  4099. /* Dump active request fences */
  4100. if (!list_empty(&ctx->active_req_list)) {
  4101. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  4102. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4103. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4104. rc = cam_common_user_dump_helper(dump_args,
  4105. cam_isp_ctx_user_dump_active_requests,
  4106. req, sizeof(uint64_t),
  4107. "ISP_OUT_FENCE_REQUEST_ACTIVE.%s.%u.%d:",
  4108. __cam_isp_ife_sfe_resource_handle_id_to_type(
  4109. req_isp->fence_map_out[i].resource_handle),
  4110. req_isp->fence_map_out[i].image_buf_addr[0],
  4111. req_isp->fence_map_out[i].sync_id);
  4112. if (rc) {
  4113. CAM_ERR(CAM_ISP,
  4114. "CAM_ISP_CONTEXT DUMP_REQ_INFO: Dump failed, rc: %d",
  4115. rc);
  4116. return rc;
  4117. }
  4118. }
  4119. }
  4120. }
  4121. return rc;
  4122. }
  4123. static void *cam_isp_ctx_user_dump_timer(
  4124. void *dump_struct, uint8_t *addr_ptr)
  4125. {
  4126. struct cam_ctx_request *req = NULL;
  4127. struct cam_isp_ctx_req *req_isp = NULL;
  4128. uint64_t *addr;
  4129. ktime_t cur_time;
  4130. req = (struct cam_ctx_request *)dump_struct;
  4131. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4132. cur_time = ktime_get();
  4133. addr = (uint64_t *)addr_ptr;
  4134. *addr++ = req->request_id;
  4135. *addr++ = ktime_to_timespec64(
  4136. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_sec;
  4137. *addr++ = ktime_to_timespec64(
  4138. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]).tv_nsec / NSEC_PER_USEC;
  4139. *addr++ = ktime_to_timespec64(cur_time).tv_sec;
  4140. *addr++ = ktime_to_timespec64(cur_time).tv_nsec / NSEC_PER_USEC;
  4141. return addr;
  4142. }
  4143. static void *cam_isp_ctx_user_dump_stream_info(
  4144. void *dump_struct, uint8_t *addr_ptr)
  4145. {
  4146. struct cam_context *ctx = NULL;
  4147. int32_t *addr;
  4148. ctx = (struct cam_context *)dump_struct;
  4149. addr = (int32_t *)addr_ptr;
  4150. *addr++ = ctx->ctx_id;
  4151. *addr++ = ctx->dev_hdl;
  4152. *addr++ = ctx->link_hdl;
  4153. return addr;
  4154. }
  4155. static int __cam_isp_ctx_dump_in_top_state(
  4156. struct cam_context *ctx,
  4157. struct cam_req_mgr_dump_info *dump_info)
  4158. {
  4159. int rc = 0;
  4160. bool dump_only_event_record = false;
  4161. size_t buf_len;
  4162. size_t remain_len;
  4163. ktime_t cur_time;
  4164. uint32_t min_len;
  4165. uint64_t diff;
  4166. uintptr_t cpu_addr;
  4167. struct cam_isp_context *ctx_isp;
  4168. struct cam_ctx_request *req = NULL;
  4169. struct cam_isp_ctx_req *req_isp;
  4170. struct cam_ctx_request *req_temp;
  4171. struct cam_hw_dump_args ife_dump_args;
  4172. struct cam_common_hw_dump_args dump_args;
  4173. struct cam_hw_cmd_args hw_cmd_args;
  4174. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4175. spin_lock_bh(&ctx->lock);
  4176. list_for_each_entry_safe(req, req_temp,
  4177. &ctx->active_req_list, list) {
  4178. if (req->request_id == dump_info->req_id) {
  4179. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  4180. dump_info->req_id);
  4181. goto hw_dump;
  4182. }
  4183. }
  4184. list_for_each_entry_safe(req, req_temp,
  4185. &ctx->wait_req_list, list) {
  4186. if (req->request_id == dump_info->req_id) {
  4187. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  4188. dump_info->req_id);
  4189. goto hw_dump;
  4190. }
  4191. }
  4192. goto end;
  4193. hw_dump:
  4194. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  4195. &cpu_addr, &buf_len);
  4196. if (rc) {
  4197. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  4198. dump_info->buf_handle, rc);
  4199. goto end;
  4200. }
  4201. if (buf_len <= dump_info->offset) {
  4202. spin_unlock_bh(&ctx->lock);
  4203. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  4204. buf_len, dump_info->offset);
  4205. return -ENOSPC;
  4206. }
  4207. remain_len = buf_len - dump_info->offset;
  4208. min_len = sizeof(struct cam_isp_context_dump_header) +
  4209. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  4210. if (remain_len < min_len) {
  4211. spin_unlock_bh(&ctx->lock);
  4212. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  4213. remain_len, min_len);
  4214. return -ENOSPC;
  4215. }
  4216. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4217. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4218. cur_time = ktime_get();
  4219. diff = ktime_us_delta(
  4220. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  4221. cur_time);
  4222. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  4223. CAM_INFO(CAM_ISP, "req %lld found no error",
  4224. req->request_id);
  4225. dump_only_event_record = true;
  4226. }
  4227. dump_args.req_id = dump_info->req_id;
  4228. dump_args.cpu_addr = cpu_addr;
  4229. dump_args.buf_len = buf_len;
  4230. dump_args.offset = dump_info->offset;
  4231. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4232. /* Dump time info */
  4233. rc = cam_common_user_dump_helper(&dump_args, cam_isp_ctx_user_dump_timer,
  4234. req, sizeof(uint64_t), "ISP_CTX_DUMP:");
  4235. if (rc) {
  4236. CAM_ERR(CAM_ISP, "Time dump fail %lld, rc: %d",
  4237. req->request_id, rc);
  4238. goto end;
  4239. }
  4240. dump_info->offset = dump_args.offset;
  4241. /* Dump stream info */
  4242. ctx->ctxt_to_hw_map = ctx_isp->hw_ctx;
  4243. if (ctx->hw_mgr_intf->hw_dump) {
  4244. /* Dump first part of stream info from isp context */
  4245. rc = cam_common_user_dump_helper(&dump_args,
  4246. cam_isp_ctx_user_dump_stream_info, ctx,
  4247. sizeof(int32_t), "ISP_STREAM_INFO_FROM_CTX:");
  4248. if (rc) {
  4249. CAM_ERR(CAM_ISP, "ISP CTX stream info dump fail %lld, rc: %d",
  4250. req->request_id, rc);
  4251. goto end;
  4252. }
  4253. /* Dump second part of stream info from ife hw manager */
  4254. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  4255. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4256. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_DUMP_STREAM_INFO;
  4257. isp_hw_cmd_args.cmd_data = &dump_args;
  4258. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4259. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv, &hw_cmd_args);
  4260. if (rc) {
  4261. CAM_ERR(CAM_ISP, "IFE HW MGR stream info dump fail %lld, rc: %d",
  4262. req->request_id, rc);
  4263. goto end;
  4264. }
  4265. dump_info->offset = dump_args.offset;
  4266. }
  4267. /* Dump event record */
  4268. rc = __cam_isp_ctx_dump_event_record(ctx_isp, &dump_args);
  4269. if (rc) {
  4270. CAM_ERR(CAM_ISP, "Event record dump fail %lld, rc: %d",
  4271. req->request_id, rc);
  4272. goto end;
  4273. }
  4274. dump_info->offset = dump_args.offset;
  4275. if (dump_only_event_record) {
  4276. goto end;
  4277. }
  4278. /* Dump state monitor array */
  4279. rc = __cam_isp_ctx_user_dump_state_monitor_array(ctx_isp, &dump_args);
  4280. if (rc) {
  4281. CAM_ERR(CAM_ISP, "Dump event fail %lld, rc: %d",
  4282. req->request_id, rc);
  4283. goto end;
  4284. }
  4285. /* Dump request info */
  4286. rc = __cam_isp_ctx_dump_req_info(ctx, req, &dump_args);
  4287. if (rc) {
  4288. CAM_ERR(CAM_ISP, "Dump Req info fail %lld, rc: %d",
  4289. req->request_id, rc);
  4290. goto end;
  4291. }
  4292. spin_unlock_bh(&ctx->lock);
  4293. /* Dump CSID, VFE, and SFE info */
  4294. dump_info->offset = dump_args.offset;
  4295. if (ctx->hw_mgr_intf->hw_dump) {
  4296. ife_dump_args.offset = dump_args.offset;
  4297. ife_dump_args.request_id = dump_info->req_id;
  4298. ife_dump_args.buf_handle = dump_info->buf_handle;
  4299. ife_dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4300. rc = ctx->hw_mgr_intf->hw_dump(
  4301. ctx->hw_mgr_intf->hw_mgr_priv,
  4302. &ife_dump_args);
  4303. dump_info->offset = ife_dump_args.offset;
  4304. }
  4305. return rc;
  4306. end:
  4307. spin_unlock_bh(&ctx->lock);
  4308. return rc;
  4309. }
  4310. static int __cam_isp_ctx_flush_req_in_flushed_state(
  4311. struct cam_context *ctx,
  4312. struct cam_req_mgr_flush_request *flush_req)
  4313. {
  4314. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%d",
  4315. flush_req->type, flush_req->req_id, ctx->ctx_id);
  4316. if (flush_req->req_id > ctx->last_flush_req)
  4317. ctx->last_flush_req = flush_req->req_id;
  4318. return 0;
  4319. }
  4320. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  4321. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  4322. {
  4323. int i, rc, tmp = 0;
  4324. uint32_t cancel_req_id_found = 0;
  4325. struct cam_ctx_request *req;
  4326. struct cam_ctx_request *req_temp;
  4327. struct cam_isp_ctx_req *req_isp;
  4328. struct list_head flush_list;
  4329. struct cam_isp_context *ctx_isp = NULL;
  4330. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4331. INIT_LIST_HEAD(&flush_list);
  4332. if (list_empty(req_list)) {
  4333. CAM_DBG(CAM_ISP, "request list is empty");
  4334. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  4335. CAM_INFO(CAM_ISP, "no request to cancel (last applied:%lld cancel:%lld)",
  4336. ctx_isp->last_applied_req_id, flush_req->req_id);
  4337. return -EINVAL;
  4338. } else
  4339. return 0;
  4340. }
  4341. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  4342. flush_req->type, flush_req->req_id);
  4343. list_for_each_entry_safe(req, req_temp, req_list, list) {
  4344. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  4345. if (req->request_id != flush_req->req_id) {
  4346. continue;
  4347. } else {
  4348. list_del_init(&req->list);
  4349. list_add_tail(&req->list, &flush_list);
  4350. cancel_req_id_found = 1;
  4351. __cam_isp_ctx_update_state_monitor_array(
  4352. ctx_isp,
  4353. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  4354. req->request_id);
  4355. break;
  4356. }
  4357. }
  4358. list_del_init(&req->list);
  4359. list_add_tail(&req->list, &flush_list);
  4360. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  4361. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  4362. }
  4363. if (list_empty(&flush_list)) {
  4364. /*
  4365. * Maybe the req isn't sent to KMD since UMD already skip
  4366. * req in CSL layer.
  4367. */
  4368. CAM_INFO(CAM_ISP,
  4369. "flush list is empty, flush type %d for req %llu",
  4370. flush_req->type, flush_req->req_id);
  4371. return 0;
  4372. }
  4373. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  4374. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4375. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4376. if (req_isp->fence_map_out[i].sync_id != -1) {
  4377. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  4378. req->request_id,
  4379. req_isp->fence_map_out[i].sync_id);
  4380. rc = cam_sync_signal(
  4381. req_isp->fence_map_out[i].sync_id,
  4382. CAM_SYNC_STATE_SIGNALED_CANCEL,
  4383. CAM_SYNC_ISP_EVENT_FLUSH);
  4384. if (rc) {
  4385. tmp = req_isp->fence_map_out[i].sync_id;
  4386. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4387. "signal fence %d failed", tmp);
  4388. }
  4389. req_isp->fence_map_out[i].sync_id = -1;
  4390. }
  4391. }
  4392. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  4393. req_isp->cdm_reset_before_apply = false;
  4394. list_del_init(&req->list);
  4395. list_add_tail(&req->list, &ctx->free_req_list);
  4396. }
  4397. return 0;
  4398. }
  4399. static int __cam_isp_ctx_flush_req_in_top_state(
  4400. struct cam_context *ctx,
  4401. struct cam_req_mgr_flush_request *flush_req)
  4402. {
  4403. int rc = 0;
  4404. struct cam_isp_context *ctx_isp;
  4405. struct cam_isp_stop_args stop_isp;
  4406. struct cam_hw_stop_args stop_args;
  4407. struct cam_hw_reset_args reset_args;
  4408. struct cam_req_mgr_timer_notify timer;
  4409. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  4410. CAM_DBG(CAM_ISP, "Flush pending list");
  4411. spin_lock_bh(&ctx->lock);
  4412. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  4413. spin_unlock_bh(&ctx->lock);
  4414. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  4415. if (ctx->state <= CAM_CTX_READY) {
  4416. ctx->state = CAM_CTX_ACQUIRED;
  4417. goto end;
  4418. }
  4419. spin_lock_bh(&ctx->lock);
  4420. ctx->state = CAM_CTX_FLUSHED;
  4421. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  4422. spin_unlock_bh(&ctx->lock);
  4423. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  4424. flush_req->req_id, ctx->ctx_id);
  4425. ctx->last_flush_req = flush_req->req_id;
  4426. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  4427. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4428. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  4429. stop_isp.stop_only = true;
  4430. stop_isp.is_internal_stop = false;
  4431. stop_args.args = (void *)&stop_isp;
  4432. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  4433. &stop_args);
  4434. if (rc)
  4435. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  4436. rc);
  4437. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  4438. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  4439. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  4440. timer.link_hdl = ctx->link_hdl;
  4441. timer.dev_hdl = ctx->dev_hdl;
  4442. timer.state = false;
  4443. ctx->ctx_crm_intf->notify_timer(&timer);
  4444. }
  4445. spin_lock_bh(&ctx->lock);
  4446. if (!list_empty(&ctx->wait_req_list))
  4447. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  4448. flush_req);
  4449. if (!list_empty(&ctx->active_req_list))
  4450. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  4451. flush_req);
  4452. ctx_isp->active_req_cnt = 0;
  4453. spin_unlock_bh(&ctx->lock);
  4454. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4455. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  4456. &reset_args);
  4457. if (rc)
  4458. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  4459. ctx_isp->init_received = false;
  4460. }
  4461. end:
  4462. ctx_isp->bubble_frame_cnt = 0;
  4463. atomic_set(&ctx_isp->process_bubble, 0);
  4464. atomic_set(&ctx_isp->rxd_epoch, 0);
  4465. atomic_set(&ctx_isp->internal_recovery_set, 0);
  4466. return rc;
  4467. }
  4468. static int __cam_isp_ctx_flush_req_in_ready(
  4469. struct cam_context *ctx,
  4470. struct cam_req_mgr_flush_request *flush_req)
  4471. {
  4472. int rc = 0;
  4473. CAM_DBG(CAM_ISP, "try to flush pending list");
  4474. spin_lock_bh(&ctx->lock);
  4475. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  4476. /* if nothing is in pending req list, change state to acquire */
  4477. if (list_empty(&ctx->pending_req_list))
  4478. ctx->state = CAM_CTX_ACQUIRED;
  4479. spin_unlock_bh(&ctx->lock);
  4480. trace_cam_context_state("ISP", ctx);
  4481. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  4482. ctx->state);
  4483. return rc;
  4484. }
  4485. static struct cam_ctx_ops
  4486. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4487. /* SOF */
  4488. {
  4489. .ioctl_ops = {},
  4490. .crm_ops = {
  4491. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  4492. .notify_frame_skip =
  4493. __cam_isp_ctx_apply_default_req_settings,
  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. .notify_frame_skip =
  4509. __cam_isp_ctx_apply_default_req_settings,
  4510. },
  4511. .irq_ops = NULL,
  4512. },
  4513. /* BUBBLE */
  4514. {
  4515. .ioctl_ops = {},
  4516. .crm_ops = {
  4517. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  4518. .notify_frame_skip =
  4519. __cam_isp_ctx_apply_default_req_settings,
  4520. },
  4521. .irq_ops = NULL,
  4522. },
  4523. /* Bubble Applied */
  4524. {
  4525. .ioctl_ops = {},
  4526. .crm_ops = {},
  4527. .irq_ops = NULL,
  4528. },
  4529. /* HW ERROR */
  4530. {
  4531. .ioctl_ops = {},
  4532. .crm_ops = {},
  4533. .irq_ops = NULL,
  4534. },
  4535. /* HALT */
  4536. {
  4537. .ioctl_ops = {},
  4538. .crm_ops = {},
  4539. .irq_ops = NULL,
  4540. },
  4541. };
  4542. static struct cam_ctx_ops
  4543. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  4544. /* SOF */
  4545. {
  4546. .ioctl_ops = {},
  4547. .crm_ops = {
  4548. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  4549. },
  4550. .irq_ops = NULL,
  4551. },
  4552. /* APPLIED */
  4553. {
  4554. .ioctl_ops = {},
  4555. .crm_ops = {},
  4556. .irq_ops = NULL,
  4557. },
  4558. /* EPOCH */
  4559. {
  4560. .ioctl_ops = {},
  4561. .crm_ops = {
  4562. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  4563. },
  4564. .irq_ops = NULL,
  4565. },
  4566. /* BUBBLE */
  4567. {
  4568. .ioctl_ops = {},
  4569. .crm_ops = {
  4570. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  4571. },
  4572. .irq_ops = NULL,
  4573. },
  4574. /* Bubble Applied */
  4575. {
  4576. .ioctl_ops = {},
  4577. .crm_ops = {},
  4578. .irq_ops = NULL,
  4579. },
  4580. /* HW ERROR */
  4581. {
  4582. .ioctl_ops = {},
  4583. .crm_ops = {},
  4584. .irq_ops = NULL,
  4585. },
  4586. /* HALT */
  4587. {
  4588. .ioctl_ops = {},
  4589. .crm_ops = {},
  4590. .irq_ops = NULL,
  4591. },
  4592. };
  4593. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  4594. struct cam_isp_context *ctx_isp, void *evt_data)
  4595. {
  4596. int rc = 0;
  4597. struct cam_context *ctx = ctx_isp->base;
  4598. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4599. uint64_t request_id = 0;
  4600. if (!evt_data) {
  4601. CAM_ERR(CAM_ISP, "in valid sof event data");
  4602. return -EINVAL;
  4603. }
  4604. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  4605. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4606. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4607. /*
  4608. * notify reqmgr with sof signal. Note, due to scheduling delay
  4609. * we can run into situation that two active requests has already
  4610. * be in the active queue while we try to do the notification.
  4611. * In this case, we need to skip the current notification. This
  4612. * helps the state machine to catch up the delay.
  4613. */
  4614. if (ctx_isp->active_req_cnt <= 2) {
  4615. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4616. /*
  4617. * It's possible for rup done to be processed before
  4618. * SOF, check for first active request shutter here
  4619. */
  4620. if (!list_empty(&ctx->active_req_list)) {
  4621. struct cam_ctx_request *req = NULL;
  4622. req = list_first_entry(&ctx->active_req_list,
  4623. struct cam_ctx_request, list);
  4624. if (req->request_id > ctx_isp->reported_req_id) {
  4625. request_id = req->request_id;
  4626. ctx_isp->reported_req_id = request_id;
  4627. }
  4628. }
  4629. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4630. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4631. } else {
  4632. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  4633. }
  4634. if (list_empty(&ctx->active_req_list))
  4635. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4636. else
  4637. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  4638. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4639. __cam_isp_ctx_substate_val_to_type(
  4640. ctx_isp->substate_activated));
  4641. return rc;
  4642. }
  4643. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  4644. struct cam_isp_context *ctx_isp, void *evt_data)
  4645. {
  4646. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4647. if (!evt_data) {
  4648. CAM_ERR(CAM_ISP, "in valid sof event data");
  4649. return -EINVAL;
  4650. }
  4651. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  4652. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4653. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4654. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  4655. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4656. __cam_isp_ctx_substate_val_to_type(
  4657. ctx_isp->substate_activated));
  4658. return 0;
  4659. }
  4660. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  4661. struct cam_isp_context *ctx_isp, void *evt_data)
  4662. {
  4663. struct cam_ctx_request *req;
  4664. struct cam_isp_ctx_req *req_isp;
  4665. struct cam_context *ctx = ctx_isp->base;
  4666. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4667. uint64_t request_id = 0;
  4668. /*
  4669. * Sof in bubble applied state means, reg update not received.
  4670. * before increment frame id and override time stamp value, send
  4671. * the previous sof time stamp that got captured in the
  4672. * sof in applied state.
  4673. */
  4674. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4675. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4676. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4677. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4678. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  4679. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4680. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4681. if (list_empty(&ctx->wait_req_list)) {
  4682. /*
  4683. * If no pending req in epoch, this is an error case.
  4684. * The recovery is to go back to sof state
  4685. */
  4686. CAM_ERR(CAM_ISP, "No wait request");
  4687. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4688. /* Send SOF event as empty frame*/
  4689. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4690. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4691. goto end;
  4692. }
  4693. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  4694. list);
  4695. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4696. req_isp->bubble_detected = true;
  4697. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  4698. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  4699. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  4700. req_isp->cdm_reset_before_apply = false;
  4701. if (req_isp->bubble_report) {
  4702. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  4703. req->request_id, ctx_isp);
  4704. atomic_set(&ctx_isp->process_bubble, 1);
  4705. } else {
  4706. req_isp->bubble_report = 0;
  4707. }
  4708. /*
  4709. * Always move the request to active list. Let buf done
  4710. * function handles the rest.
  4711. */
  4712. list_del_init(&req->list);
  4713. list_add_tail(&req->list, &ctx->active_req_list);
  4714. ctx_isp->active_req_cnt++;
  4715. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  4716. req->request_id, ctx_isp->active_req_cnt);
  4717. if (!req_isp->bubble_report) {
  4718. if (req->request_id > ctx_isp->reported_req_id) {
  4719. request_id = req->request_id;
  4720. ctx_isp->reported_req_id = request_id;
  4721. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4722. CAM_REQ_MGR_SOF_EVENT_ERROR);
  4723. } else
  4724. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4725. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4726. } else
  4727. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4728. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4729. /* change the state to bubble, as reg update has not come */
  4730. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  4731. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4732. __cam_isp_ctx_substate_val_to_type(
  4733. ctx_isp->substate_activated));
  4734. end:
  4735. return 0;
  4736. }
  4737. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  4738. struct cam_isp_context *ctx_isp, void *evt_data)
  4739. {
  4740. uint32_t i;
  4741. struct cam_ctx_request *req;
  4742. struct cam_context *ctx = ctx_isp->base;
  4743. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4744. struct cam_isp_ctx_req *req_isp;
  4745. struct cam_hw_cmd_args hw_cmd_args;
  4746. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4747. uint64_t request_id = 0;
  4748. uint64_t last_cdm_done_req = 0;
  4749. int rc = 0;
  4750. if (!evt_data) {
  4751. CAM_ERR(CAM_ISP, "in valid sof event data");
  4752. return -EINVAL;
  4753. }
  4754. __cam_isp_ctx_update_sof_ts_util(sof_event_data, ctx_isp);
  4755. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4756. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4757. if (atomic_read(&ctx_isp->process_bubble)) {
  4758. if (list_empty(&ctx->active_req_list)) {
  4759. CAM_ERR(CAM_ISP, "No available active req in bubble");
  4760. atomic_set(&ctx_isp->process_bubble, 0);
  4761. return -EINVAL;
  4762. }
  4763. if (ctx_isp->last_sof_timestamp ==
  4764. ctx_isp->sof_timestamp_val) {
  4765. CAM_DBG(CAM_ISP,
  4766. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %d",
  4767. ctx_isp->frame_id,
  4768. ctx_isp->sof_timestamp_val,
  4769. ctx->ctx_id);
  4770. goto end;
  4771. }
  4772. req = list_first_entry(&ctx->active_req_list,
  4773. struct cam_ctx_request, list);
  4774. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4775. if (req_isp->bubble_detected) {
  4776. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4777. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4778. isp_hw_cmd_args.cmd_type =
  4779. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  4780. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4781. rc = ctx->hw_mgr_intf->hw_cmd(
  4782. ctx->hw_mgr_intf->hw_mgr_priv,
  4783. &hw_cmd_args);
  4784. if (rc) {
  4785. CAM_ERR(CAM_ISP, "HW command failed");
  4786. return rc;
  4787. }
  4788. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  4789. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %d",
  4790. last_cdm_done_req, ctx->ctx_id);
  4791. if (last_cdm_done_req >= req->request_id) {
  4792. CAM_DBG(CAM_ISP,
  4793. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  4794. req->request_id);
  4795. if (req_isp->num_fence_map_out ==
  4796. req_isp->num_deferred_acks) {
  4797. __cam_isp_handle_deferred_buf_done(ctx_isp, req,
  4798. true,
  4799. CAM_SYNC_STATE_SIGNALED_ERROR,
  4800. CAM_SYNC_ISP_EVENT_BUBBLE);
  4801. __cam_isp_ctx_handle_buf_done_for_req_list(
  4802. ctx_isp, req);
  4803. }
  4804. goto end;
  4805. } else {
  4806. CAM_WARN(CAM_ISP,
  4807. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  4808. req->request_id);
  4809. req_isp->num_acked = 0;
  4810. req_isp->num_deferred_acks = 0;
  4811. req_isp->bubble_detected = false;
  4812. req_isp->cdm_reset_before_apply = true;
  4813. list_del_init(&req->list);
  4814. list_add(&req->list, &ctx->pending_req_list);
  4815. atomic_set(&ctx_isp->process_bubble, 0);
  4816. ctx_isp->active_req_cnt--;
  4817. CAM_DBG(CAM_REQ,
  4818. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  4819. req->request_id,
  4820. ctx_isp->active_req_cnt, ctx->ctx_id);
  4821. }
  4822. goto end;
  4823. }
  4824. }
  4825. /*
  4826. * Signal all active requests with error and move the all the active
  4827. * requests to free list
  4828. */
  4829. while (!list_empty(&ctx->active_req_list)) {
  4830. req = list_first_entry(&ctx->active_req_list,
  4831. struct cam_ctx_request, list);
  4832. list_del_init(&req->list);
  4833. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4834. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4835. req_isp->num_fence_map_out);
  4836. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4837. if (req_isp->fence_map_out[i].sync_id != -1) {
  4838. cam_sync_signal(
  4839. req_isp->fence_map_out[i].sync_id,
  4840. CAM_SYNC_STATE_SIGNALED_ERROR,
  4841. CAM_SYNC_ISP_EVENT_BUBBLE);
  4842. }
  4843. list_add_tail(&req->list, &ctx->free_req_list);
  4844. ctx_isp->active_req_cnt--;
  4845. }
  4846. end:
  4847. /* notify reqmgr with sof signal */
  4848. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4849. /*
  4850. * It is idle frame with out any applied request id, send
  4851. * request id as zero
  4852. */
  4853. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4854. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4855. /*
  4856. * Can't move the substate to SOF if we are processing bubble,
  4857. * since the SOF substate can't receive REG_UPD and buf done,
  4858. * then the processing of bubble req can't be finished
  4859. */
  4860. if (!atomic_read(&ctx_isp->process_bubble))
  4861. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4862. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4863. __cam_isp_ctx_substate_val_to_type(
  4864. ctx_isp->substate_activated));
  4865. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  4866. return 0;
  4867. }
  4868. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  4869. struct cam_isp_context *ctx_isp, void *evt_data)
  4870. {
  4871. struct cam_ctx_request *req = NULL;
  4872. struct cam_context *ctx = ctx_isp->base;
  4873. req = list_first_entry(&ctx->active_req_list,
  4874. struct cam_ctx_request, list);
  4875. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  4876. return 0;
  4877. }
  4878. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  4879. struct cam_isp_context *ctx_isp, void *evt_data)
  4880. {
  4881. struct cam_ctx_request *req = NULL;
  4882. struct cam_context *ctx = ctx_isp->base;
  4883. struct cam_isp_ctx_req *req_isp;
  4884. uint64_t request_id = 0;
  4885. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  4886. /* notify reqmgr with sof signal*/
  4887. if (list_empty(&ctx->wait_req_list)) {
  4888. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  4889. goto error;
  4890. }
  4891. req = list_first_entry(&ctx->wait_req_list,
  4892. struct cam_ctx_request, list);
  4893. list_del_init(&req->list);
  4894. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4895. request_id =
  4896. (req_isp->hw_update_data.packet_opcode_type ==
  4897. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  4898. if (req_isp->num_fence_map_out != 0) {
  4899. list_add_tail(&req->list, &ctx->active_req_list);
  4900. ctx_isp->active_req_cnt++;
  4901. CAM_DBG(CAM_ISP,
  4902. "move request %lld to active list(cnt = %d)",
  4903. req->request_id, ctx_isp->active_req_cnt);
  4904. /* if packet has buffers, set correct request id */
  4905. request_id = req->request_id;
  4906. } else {
  4907. /* no io config, so the request is completed. */
  4908. list_add_tail(&req->list, &ctx->free_req_list);
  4909. CAM_DBG(CAM_ISP,
  4910. "move active req %lld to free list(cnt=%d)",
  4911. req->request_id, ctx_isp->active_req_cnt);
  4912. }
  4913. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4914. if (request_id)
  4915. ctx_isp->reported_req_id = request_id;
  4916. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4917. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4918. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4919. __cam_isp_ctx_substate_val_to_type(
  4920. ctx_isp->substate_activated));
  4921. __cam_isp_ctx_update_event_record(ctx_isp,
  4922. CAM_ISP_CTX_EVENT_RUP, req);
  4923. return 0;
  4924. error:
  4925. /* Send SOF event as idle frame*/
  4926. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4927. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4928. __cam_isp_ctx_update_event_record(ctx_isp,
  4929. CAM_ISP_CTX_EVENT_RUP, NULL);
  4930. /*
  4931. * There is no request in the pending list, move the sub state machine
  4932. * to SOF sub state
  4933. */
  4934. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4935. return 0;
  4936. }
  4937. static struct cam_isp_ctx_irq_ops
  4938. cam_isp_ctx_rdi_only_activated_state_machine_irq
  4939. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4940. /* SOF */
  4941. {
  4942. .irq_ops = {
  4943. NULL,
  4944. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4945. __cam_isp_ctx_reg_upd_in_sof,
  4946. NULL,
  4947. __cam_isp_ctx_notify_eof_in_activated_state,
  4948. NULL,
  4949. },
  4950. },
  4951. /* APPLIED */
  4952. {
  4953. .irq_ops = {
  4954. __cam_isp_ctx_handle_error,
  4955. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  4956. __cam_isp_ctx_reg_upd_in_applied_state,
  4957. NULL,
  4958. __cam_isp_ctx_notify_eof_in_activated_state,
  4959. __cam_isp_ctx_buf_done_in_applied,
  4960. },
  4961. },
  4962. /* EPOCH */
  4963. {
  4964. .irq_ops = {
  4965. __cam_isp_ctx_handle_error,
  4966. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4967. NULL,
  4968. NULL,
  4969. __cam_isp_ctx_notify_eof_in_activated_state,
  4970. __cam_isp_ctx_buf_done_in_epoch,
  4971. },
  4972. },
  4973. /* BUBBLE*/
  4974. {
  4975. .irq_ops = {
  4976. __cam_isp_ctx_handle_error,
  4977. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  4978. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  4979. NULL,
  4980. __cam_isp_ctx_notify_eof_in_activated_state,
  4981. __cam_isp_ctx_buf_done_in_bubble,
  4982. },
  4983. },
  4984. /* BUBBLE APPLIED ie PRE_BUBBLE */
  4985. {
  4986. .irq_ops = {
  4987. __cam_isp_ctx_handle_error,
  4988. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  4989. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  4990. NULL,
  4991. __cam_isp_ctx_notify_eof_in_activated_state,
  4992. __cam_isp_ctx_buf_done_in_bubble_applied,
  4993. },
  4994. },
  4995. /* HW ERROR */
  4996. {
  4997. },
  4998. /* HALT */
  4999. {
  5000. },
  5001. };
  5002. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  5003. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  5004. {
  5005. int rc = 0;
  5006. struct cam_isp_context *ctx_isp =
  5007. (struct cam_isp_context *) ctx->ctx_priv;
  5008. CAM_DBG(CAM_ISP, "current Substate[%s]",
  5009. __cam_isp_ctx_substate_val_to_type(
  5010. ctx_isp->substate_activated));
  5011. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  5012. CAM_ISP_CTX_ACTIVATED_APPLIED);
  5013. CAM_DBG(CAM_ISP, "new Substate[%s]",
  5014. __cam_isp_ctx_substate_val_to_type(
  5015. ctx_isp->substate_activated));
  5016. if (rc)
  5017. CAM_ERR_RATE_LIMIT(CAM_ISP,
  5018. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  5019. ctx->ctx_id,
  5020. __cam_isp_ctx_substate_val_to_type(
  5021. ctx_isp->substate_activated), rc);
  5022. return rc;
  5023. }
  5024. static struct cam_ctx_ops
  5025. cam_isp_ctx_rdi_only_activated_state_machine
  5026. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  5027. /* SOF */
  5028. {
  5029. .ioctl_ops = {},
  5030. .crm_ops = {
  5031. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5032. },
  5033. .irq_ops = NULL,
  5034. },
  5035. /* APPLIED */
  5036. {
  5037. .ioctl_ops = {},
  5038. .crm_ops = {},
  5039. .irq_ops = NULL,
  5040. },
  5041. /* EPOCH */
  5042. {
  5043. .ioctl_ops = {},
  5044. .crm_ops = {
  5045. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  5046. },
  5047. .irq_ops = NULL,
  5048. },
  5049. /* PRE BUBBLE */
  5050. {
  5051. .ioctl_ops = {},
  5052. .crm_ops = {},
  5053. .irq_ops = NULL,
  5054. },
  5055. /* BUBBLE */
  5056. {
  5057. .ioctl_ops = {},
  5058. .crm_ops = {},
  5059. .irq_ops = NULL,
  5060. },
  5061. /* HW ERROR */
  5062. {
  5063. .ioctl_ops = {},
  5064. .crm_ops = {},
  5065. .irq_ops = NULL,
  5066. },
  5067. /* HALT */
  5068. {
  5069. .ioctl_ops = {},
  5070. .crm_ops = {},
  5071. .irq_ops = NULL,
  5072. },
  5073. };
  5074. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  5075. struct cam_flush_dev_cmd *cmd)
  5076. {
  5077. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  5078. struct cam_req_mgr_flush_request flush_req;
  5079. if (!ctx_isp->offline_context) {
  5080. CAM_ERR(CAM_ISP, "flush dev only supported in offline context");
  5081. return -EINVAL;
  5082. }
  5083. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  5084. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  5085. flush_req.req_id = cmd->req_id;
  5086. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu)", flush_req.type, flush_req.req_id);
  5087. switch (ctx->state) {
  5088. case CAM_CTX_ACQUIRED:
  5089. case CAM_CTX_ACTIVATED:
  5090. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  5091. case CAM_CTX_READY:
  5092. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  5093. default:
  5094. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d", ctx->state);
  5095. return -EINVAL;
  5096. }
  5097. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  5098. cam_req_mgr_workq_flush(ctx_isp->workq);
  5099. }
  5100. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  5101. {
  5102. int i;
  5103. if (ctx->out_map_entries) {
  5104. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5105. kfree(ctx->out_map_entries[i]);
  5106. ctx->out_map_entries[i] = NULL;
  5107. }
  5108. kfree(ctx->out_map_entries);
  5109. ctx->out_map_entries = NULL;
  5110. }
  5111. if (ctx->in_map_entries) {
  5112. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5113. kfree(ctx->in_map_entries[i]);
  5114. ctx->in_map_entries[i] = NULL;
  5115. }
  5116. kfree(ctx->in_map_entries);
  5117. ctx->in_map_entries = NULL;
  5118. }
  5119. if (ctx->hw_update_entry) {
  5120. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5121. kfree(ctx->hw_update_entry[i]);
  5122. ctx->hw_update_entry[i] = NULL;
  5123. }
  5124. kfree(ctx->hw_update_entry);
  5125. ctx->hw_update_entry = NULL;
  5126. }
  5127. ctx->max_out_map_entries = 0;
  5128. ctx->max_in_map_entries = 0;
  5129. ctx->max_hw_update_entries = 0;
  5130. }
  5131. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  5132. void *cmd)
  5133. {
  5134. int rc = 0;
  5135. struct cam_hw_release_args rel_arg;
  5136. struct cam_isp_context *ctx_isp =
  5137. (struct cam_isp_context *) ctx->ctx_priv;
  5138. struct cam_req_mgr_flush_request flush_req;
  5139. int i;
  5140. if (ctx_isp->hw_ctx) {
  5141. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5142. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5143. &rel_arg);
  5144. ctx_isp->hw_ctx = NULL;
  5145. } else {
  5146. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  5147. }
  5148. ctx->last_flush_req = 0;
  5149. ctx_isp->custom_enabled = false;
  5150. ctx_isp->use_frame_header_ts = false;
  5151. ctx_isp->use_default_apply = false;
  5152. ctx_isp->frame_id = 0;
  5153. ctx_isp->active_req_cnt = 0;
  5154. ctx_isp->reported_req_id = 0;
  5155. ctx_isp->reported_frame_id = 0;
  5156. ctx_isp->hw_acquired = false;
  5157. ctx_isp->init_received = false;
  5158. ctx_isp->support_consumed_addr = false;
  5159. ctx_isp->aeb_enabled = false;
  5160. ctx_isp->do_internal_recovery = false;
  5161. ctx_isp->req_info.last_bufdone_req_id = 0;
  5162. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5163. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5164. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5165. /*
  5166. * Ideally, we should never have any active request here.
  5167. * But we still add some sanity check code here to help the debug
  5168. */
  5169. if (!list_empty(&ctx->active_req_list))
  5170. CAM_WARN(CAM_ISP, "Active list is not empty");
  5171. /* Flush all the pending request list */
  5172. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5173. flush_req.link_hdl = ctx->link_hdl;
  5174. flush_req.dev_hdl = ctx->dev_hdl;
  5175. flush_req.req_id = 0;
  5176. CAM_DBG(CAM_ISP, "try to flush pending list");
  5177. spin_lock_bh(&ctx->lock);
  5178. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  5179. spin_unlock_bh(&ctx->lock);
  5180. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5181. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  5182. ctx->state = CAM_CTX_ACQUIRED;
  5183. trace_cam_context_state("ISP", ctx);
  5184. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  5185. ctx->ctx_id, ctx->state);
  5186. return rc;
  5187. }
  5188. /* top level state machine */
  5189. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  5190. struct cam_release_dev_cmd *cmd)
  5191. {
  5192. int rc = 0;
  5193. int i;
  5194. struct cam_hw_release_args rel_arg;
  5195. struct cam_isp_context *ctx_isp =
  5196. (struct cam_isp_context *) ctx->ctx_priv;
  5197. struct cam_req_mgr_flush_request flush_req;
  5198. if (cmd && ctx_isp->hw_ctx) {
  5199. CAM_ERR(CAM_ISP, "releasing hw");
  5200. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  5201. }
  5202. if (ctx_isp->hw_ctx) {
  5203. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5204. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5205. &rel_arg);
  5206. ctx_isp->hw_ctx = NULL;
  5207. }
  5208. cam_common_release_err_params(ctx->dev_hdl);
  5209. memset(&(ctx_isp->err_inject_params), 0, sizeof(struct cam_hw_err_param));
  5210. ctx->session_hdl = -1;
  5211. ctx->dev_hdl = -1;
  5212. ctx->link_hdl = -1;
  5213. ctx->ctx_crm_intf = NULL;
  5214. ctx->last_flush_req = 0;
  5215. ctx_isp->frame_id = 0;
  5216. ctx_isp->active_req_cnt = 0;
  5217. ctx_isp->reported_req_id = 0;
  5218. ctx_isp->reported_frame_id = 0;
  5219. ctx_isp->hw_acquired = false;
  5220. ctx_isp->init_received = false;
  5221. ctx_isp->offline_context = false;
  5222. ctx_isp->vfps_aux_context = false;
  5223. ctx_isp->rdi_only_context = false;
  5224. ctx_isp->req_info.last_bufdone_req_id = 0;
  5225. ctx_isp->v4l2_event_sub_ids = 0;
  5226. ctx_isp->resume_hw_in_flushed = false;
  5227. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5228. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5229. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5230. /*
  5231. * Ideally, we should never have any active request here.
  5232. * But we still add some sanity check code here to help the debug
  5233. */
  5234. if (!list_empty(&ctx->active_req_list))
  5235. CAM_ERR(CAM_ISP, "Active list is not empty");
  5236. /* Flush all the pending request list */
  5237. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  5238. flush_req.link_hdl = ctx->link_hdl;
  5239. flush_req.dev_hdl = ctx->dev_hdl;
  5240. flush_req.req_id = 0;
  5241. CAM_DBG(CAM_ISP, "try to flush pending list");
  5242. spin_lock_bh(&ctx->lock);
  5243. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  5244. spin_unlock_bh(&ctx->lock);
  5245. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5246. ctx->state = CAM_CTX_AVAILABLE;
  5247. trace_cam_context_state("ISP", ctx);
  5248. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  5249. ctx->ctx_id, ctx->state);
  5250. return rc;
  5251. }
  5252. static int __cam_isp_ctx_config_dev_in_top_state(
  5253. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  5254. {
  5255. int rc = 0, i;
  5256. struct cam_ctx_request *req = NULL;
  5257. struct cam_isp_ctx_req *req_isp;
  5258. struct cam_packet *packet;
  5259. size_t remain_len = 0;
  5260. struct cam_hw_prepare_update_args cfg = {0};
  5261. struct cam_req_mgr_add_request add_req;
  5262. struct cam_isp_context *ctx_isp =
  5263. (struct cam_isp_context *) ctx->ctx_priv;
  5264. struct cam_hw_cmd_args hw_cmd_args;
  5265. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5266. uint32_t packet_opcode = 0;
  5267. CAM_DBG(CAM_ISP, "get free request object......");
  5268. /* get free request */
  5269. spin_lock_bh(&ctx->lock);
  5270. if (!list_empty(&ctx->free_req_list)) {
  5271. req = list_first_entry(&ctx->free_req_list,
  5272. struct cam_ctx_request, list);
  5273. list_del_init(&req->list);
  5274. }
  5275. spin_unlock_bh(&ctx->lock);
  5276. if (!req) {
  5277. CAM_ERR(CAM_ISP, "No more request obj free");
  5278. return -ENOMEM;
  5279. }
  5280. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5281. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  5282. if (IS_ERR(packet)) {
  5283. rc = PTR_ERR(packet);
  5284. goto free_req;
  5285. }
  5286. /* Query the packet opcode */
  5287. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5288. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5289. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  5290. isp_hw_cmd_args.cmd_data = (void *)packet;
  5291. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5292. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5293. &hw_cmd_args);
  5294. if (rc) {
  5295. CAM_ERR(CAM_ISP, "HW command failed");
  5296. goto free_req;
  5297. }
  5298. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  5299. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  5300. && (packet->header.request_id <= ctx->last_flush_req)) {
  5301. CAM_INFO(CAM_ISP,
  5302. "request %lld has been flushed, reject packet",
  5303. packet->header.request_id);
  5304. rc = -EBADR;
  5305. goto free_req;
  5306. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  5307. && (packet->header.request_id <= ctx->last_flush_req)
  5308. && ctx->last_flush_req && packet->header.request_id) {
  5309. CAM_WARN(CAM_ISP,
  5310. "last flushed req is %lld, config dev(init) for req %lld",
  5311. ctx->last_flush_req, packet->header.request_id);
  5312. rc = -EBADR;
  5313. goto free_req;
  5314. }
  5315. cfg.packet = packet;
  5316. cfg.remain_len = remain_len;
  5317. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5318. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  5319. cfg.hw_update_entries = req_isp->cfg;
  5320. cfg.max_out_map_entries = ctx->max_out_map_entries;
  5321. cfg.max_in_map_entries = ctx->max_in_map_entries;
  5322. cfg.out_map_entries = req_isp->fence_map_out;
  5323. cfg.in_map_entries = req_isp->fence_map_in;
  5324. cfg.priv = &req_isp->hw_update_data;
  5325. cfg.pf_data = &(req->pf_data);
  5326. cfg.num_out_map_entries = 0;
  5327. cfg.num_in_map_entries = 0;
  5328. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  5329. rc = ctx->hw_mgr_intf->hw_prepare_update(
  5330. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  5331. if (rc != 0) {
  5332. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  5333. rc = -EFAULT;
  5334. goto free_req;
  5335. }
  5336. req_isp->num_cfg = cfg.num_hw_update_entries;
  5337. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  5338. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  5339. req_isp->num_acked = 0;
  5340. req_isp->num_deferred_acks = 0;
  5341. req_isp->bubble_detected = false;
  5342. req_isp->cdm_reset_before_apply = false;
  5343. req_isp->hw_update_data.packet = packet;
  5344. req->pf_data.packet_handle = cmd->packet_handle;
  5345. req->pf_data.packet_offset = cmd->offset;
  5346. req->pf_data.req = req;
  5347. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  5348. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  5349. if (rc) {
  5350. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  5351. req_isp->fence_map_out[i].sync_id);
  5352. goto put_ref;
  5353. }
  5354. }
  5355. CAM_DBG(CAM_ISP,
  5356. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d",
  5357. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  5358. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in);
  5359. req->request_id = packet->header.request_id;
  5360. req->status = 1;
  5361. if (req_isp->hw_update_data.packet_opcode_type ==
  5362. CAM_ISP_PACKET_INIT_DEV) {
  5363. if (ctx->state < CAM_CTX_ACTIVATED) {
  5364. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  5365. if (rc)
  5366. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  5367. ctx_isp->init_received = true;
  5368. if ((ctx_isp->vfps_aux_context) && (req->request_id > 0))
  5369. ctx_isp->resume_hw_in_flushed = true;
  5370. else
  5371. ctx_isp->resume_hw_in_flushed = false;
  5372. } else {
  5373. rc = -EINVAL;
  5374. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  5375. ctx->state);
  5376. }
  5377. } else {
  5378. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  5379. rc = -EINVAL;
  5380. CAM_ERR(CAM_ISP, "Received update req %lld in wrong state:%d",
  5381. req->request_id, ctx->state);
  5382. goto put_ref;
  5383. }
  5384. if ((ctx_isp->offline_context) || (ctx_isp->vfps_aux_context)) {
  5385. __cam_isp_ctx_enqueue_request_in_order(ctx, req, true);
  5386. } else if (ctx->ctx_crm_intf->add_req) {
  5387. memset(&add_req, 0, sizeof(add_req));
  5388. add_req.link_hdl = ctx->link_hdl;
  5389. add_req.dev_hdl = ctx->dev_hdl;
  5390. add_req.req_id = req->request_id;
  5391. rc = ctx->ctx_crm_intf->add_req(&add_req);
  5392. if (rc) {
  5393. if (rc == -EBADR)
  5394. CAM_INFO(CAM_ISP,
  5395. "Add req failed: req id=%llu, it has been flushed",
  5396. req->request_id);
  5397. else
  5398. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  5399. req->request_id);
  5400. } else {
  5401. __cam_isp_ctx_enqueue_request_in_order(
  5402. ctx, req, true);
  5403. }
  5404. } else {
  5405. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu", req->request_id);
  5406. rc = -ENODEV;
  5407. }
  5408. }
  5409. if (rc)
  5410. goto put_ref;
  5411. CAM_DBG(CAM_REQ,
  5412. "Preprocessing Config req_id %lld successful on ctx %u",
  5413. req->request_id, ctx->ctx_id);
  5414. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch))
  5415. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  5416. else if (ctx_isp->vfps_aux_context &&
  5417. (req_isp->hw_update_data.packet_opcode_type != CAM_ISP_PACKET_INIT_DEV))
  5418. __cam_isp_ctx_schedule_apply_req(ctx_isp);
  5419. return rc;
  5420. put_ref:
  5421. for (--i; i >= 0; i--) {
  5422. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  5423. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  5424. req_isp->fence_map_out[i].sync_id);
  5425. }
  5426. free_req:
  5427. spin_lock_bh(&ctx->lock);
  5428. list_add_tail(&req->list, &ctx->free_req_list);
  5429. spin_unlock_bh(&ctx->lock);
  5430. return rc;
  5431. }
  5432. static int __cam_isp_ctx_allocate_mem_hw_entries(
  5433. struct cam_context *ctx,
  5434. struct cam_hw_acquire_args *param)
  5435. {
  5436. int rc = 0, i;
  5437. uint32_t max_res = 0;
  5438. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  5439. struct cam_ctx_request *req;
  5440. struct cam_ctx_request *temp_req;
  5441. struct cam_isp_ctx_req *req_isp;
  5442. if (!param->op_params.param_list[0])
  5443. max_res = CAM_ISP_CTX_RES_MAX;
  5444. else {
  5445. max_res = param->op_params.param_list[0];
  5446. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  5447. max_res += param->op_params.param_list[1];
  5448. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  5449. }
  5450. }
  5451. ctx->max_in_map_entries = max_res;
  5452. ctx->max_out_map_entries = max_res;
  5453. ctx->max_hw_update_entries = max_hw_upd_entries;
  5454. CAM_DBG(CAM_ISP,
  5455. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  5456. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  5457. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  5458. GFP_KERNEL);
  5459. if (!ctx->hw_update_entry) {
  5460. CAM_ERR(CAM_CTXT, "%s[%d] no memory ",
  5461. ctx->dev_name, ctx->ctx_id);
  5462. return -ENOMEM;
  5463. }
  5464. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5465. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  5466. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  5467. if (!ctx->hw_update_entry[i]) {
  5468. CAM_ERR(CAM_CTXT, "%s[%d] no memory for hw_update_entry: %u",
  5469. ctx->dev_name, ctx->ctx_id, i);
  5470. return -ENOMEM;
  5471. }
  5472. }
  5473. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  5474. GFP_KERNEL);
  5475. if (!ctx->in_map_entries) {
  5476. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries",
  5477. ctx->dev_name, ctx->ctx_id);
  5478. rc = -ENOMEM;
  5479. goto end;
  5480. }
  5481. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5482. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  5483. sizeof(struct cam_hw_fence_map_entry),
  5484. GFP_KERNEL);
  5485. if (!ctx->in_map_entries[i]) {
  5486. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries: %u",
  5487. ctx->dev_name, ctx->ctx_id, i);
  5488. rc = -ENOMEM;
  5489. goto end;
  5490. }
  5491. }
  5492. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  5493. GFP_KERNEL);
  5494. if (!ctx->out_map_entries) {
  5495. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries",
  5496. ctx->dev_name, ctx->ctx_id);
  5497. rc = -ENOMEM;
  5498. goto end;
  5499. }
  5500. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  5501. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  5502. sizeof(struct cam_hw_fence_map_entry),
  5503. GFP_KERNEL);
  5504. if (!ctx->out_map_entries[i]) {
  5505. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries: %u",
  5506. ctx->dev_name, ctx->ctx_id, i);
  5507. rc = -ENOMEM;
  5508. goto end;
  5509. }
  5510. }
  5511. list_for_each_entry_safe(req, temp_req,
  5512. &ctx->free_req_list, list) {
  5513. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5514. req_isp->cfg = ctx->hw_update_entry[req->index];
  5515. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  5516. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  5517. }
  5518. return rc;
  5519. end:
  5520. __cam_isp_ctx_free_mem_hw_entries(ctx);
  5521. return rc;
  5522. }
  5523. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  5524. struct cam_acquire_dev_cmd *cmd)
  5525. {
  5526. int rc = 0;
  5527. int i;
  5528. struct cam_hw_acquire_args param;
  5529. struct cam_isp_resource *isp_res = NULL;
  5530. struct cam_create_dev_hdl req_hdl_param;
  5531. struct cam_hw_release_args release;
  5532. struct cam_isp_context *ctx_isp =
  5533. (struct cam_isp_context *) ctx->ctx_priv;
  5534. struct cam_hw_cmd_args hw_cmd_args;
  5535. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5536. if (!ctx->hw_mgr_intf) {
  5537. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5538. rc = -EFAULT;
  5539. goto end;
  5540. }
  5541. CAM_DBG(CAM_ISP,
  5542. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  5543. cmd->session_handle, cmd->num_resources,
  5544. cmd->handle_type, cmd->resource_hdl);
  5545. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  5546. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  5547. ctx_isp->split_acquire = true;
  5548. CAM_DBG(CAM_ISP, "Acquire dev handle");
  5549. goto get_dev_handle;
  5550. }
  5551. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  5552. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  5553. rc = -ENOMEM;
  5554. goto end;
  5555. }
  5556. /* for now we only support user pointer */
  5557. if (cmd->handle_type != 1) {
  5558. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5559. rc = -EINVAL;
  5560. goto end;
  5561. }
  5562. isp_res = kzalloc(
  5563. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  5564. if (!isp_res) {
  5565. rc = -ENOMEM;
  5566. goto end;
  5567. }
  5568. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  5569. cmd->num_resources);
  5570. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  5571. sizeof(*isp_res)*cmd->num_resources)) {
  5572. rc = -EFAULT;
  5573. goto free_res;
  5574. }
  5575. memset(&param, 0, sizeof(param));
  5576. param.context_data = ctx;
  5577. param.event_cb = ctx->irq_cb_intf;
  5578. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  5579. param.num_acq = cmd->num_resources;
  5580. param.acquire_info = (uintptr_t) isp_res;
  5581. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  5582. if (rc) {
  5583. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5584. ctx->ctx_id);
  5585. goto free_res;
  5586. }
  5587. /* call HW manager to reserve the resource */
  5588. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5589. &param);
  5590. if (rc != 0) {
  5591. CAM_ERR(CAM_ISP, "Acquire device failed");
  5592. goto free_res;
  5593. }
  5594. /* Query the context has rdi only resource */
  5595. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5596. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5597. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5598. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5599. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5600. &hw_cmd_args);
  5601. if (rc) {
  5602. CAM_ERR(CAM_ISP, "HW command failed");
  5603. goto free_hw;
  5604. }
  5605. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5606. /*
  5607. * this context has rdi only resource assign rdi only
  5608. * state machine
  5609. */
  5610. CAM_DBG(CAM_ISP, "RDI only session Context");
  5611. ctx_isp->substate_machine_irq =
  5612. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5613. ctx_isp->substate_machine =
  5614. cam_isp_ctx_rdi_only_activated_state_machine;
  5615. ctx_isp->rdi_only_context = true;
  5616. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5617. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5618. ctx_isp->substate_machine_irq =
  5619. cam_isp_ctx_fs2_state_machine_irq;
  5620. ctx_isp->substate_machine =
  5621. cam_isp_ctx_fs2_state_machine;
  5622. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5623. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  5624. ctx_isp->substate_machine_irq =
  5625. cam_isp_ctx_offline_state_machine_irq;
  5626. } else {
  5627. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5628. ctx_isp->substate_machine_irq =
  5629. cam_isp_ctx_activated_state_machine_irq;
  5630. ctx_isp->substate_machine =
  5631. cam_isp_ctx_activated_state_machine;
  5632. }
  5633. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5634. ctx_isp->hw_acquired = true;
  5635. ctx_isp->split_acquire = false;
  5636. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5637. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5638. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5639. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5640. kfree(isp_res);
  5641. isp_res = NULL;
  5642. get_dev_handle:
  5643. req_hdl_param.session_hdl = cmd->session_handle;
  5644. /* bridge is not ready for these flags. so false for now */
  5645. req_hdl_param.v4l2_sub_dev_flag = 0;
  5646. req_hdl_param.media_entity_flag = 0;
  5647. req_hdl_param.ops = ctx->crm_ctx_intf;
  5648. req_hdl_param.priv = ctx;
  5649. req_hdl_param.dev_id = CAM_ISP;
  5650. CAM_DBG(CAM_ISP, "get device handle form bridge");
  5651. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  5652. if (ctx->dev_hdl <= 0) {
  5653. rc = -EFAULT;
  5654. CAM_ERR(CAM_ISP, "Can not create device handle");
  5655. goto free_hw;
  5656. }
  5657. cmd->dev_handle = ctx->dev_hdl;
  5658. /* store session information */
  5659. ctx->session_hdl = cmd->session_handle;
  5660. ctx->state = CAM_CTX_ACQUIRED;
  5661. trace_cam_context_state("ISP", ctx);
  5662. CAM_DBG(CAM_ISP,
  5663. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  5664. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  5665. return rc;
  5666. free_hw:
  5667. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5668. if (ctx_isp->hw_acquired)
  5669. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  5670. &release);
  5671. ctx_isp->hw_ctx = NULL;
  5672. ctx_isp->hw_acquired = false;
  5673. free_res:
  5674. kfree(isp_res);
  5675. end:
  5676. return rc;
  5677. }
  5678. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  5679. void *args)
  5680. {
  5681. int rc = 0;
  5682. int i;
  5683. struct cam_acquire_hw_cmd_v1 *cmd =
  5684. (struct cam_acquire_hw_cmd_v1 *)args;
  5685. struct cam_hw_acquire_args param;
  5686. struct cam_hw_release_args release;
  5687. struct cam_isp_context *ctx_isp =
  5688. (struct cam_isp_context *) ctx->ctx_priv;
  5689. struct cam_hw_cmd_args hw_cmd_args;
  5690. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5691. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  5692. if (!ctx->hw_mgr_intf) {
  5693. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5694. rc = -EFAULT;
  5695. goto end;
  5696. }
  5697. CAM_DBG(CAM_ISP,
  5698. "session_hdl 0x%x, hdl type %d, res %lld",
  5699. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  5700. /* for now we only support user pointer */
  5701. if (cmd->handle_type != 1) {
  5702. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5703. rc = -EINVAL;
  5704. goto end;
  5705. }
  5706. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  5707. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  5708. goto end;
  5709. }
  5710. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  5711. if (!acquire_hw_info) {
  5712. rc = -ENOMEM;
  5713. goto end;
  5714. }
  5715. CAM_DBG(CAM_ISP, "start copy resources from user");
  5716. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  5717. cmd->data_size)) {
  5718. rc = -EFAULT;
  5719. goto free_res;
  5720. }
  5721. memset(&param, 0, sizeof(param));
  5722. param.context_data = ctx;
  5723. param.event_cb = ctx->irq_cb_intf;
  5724. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  5725. param.num_acq = CAM_API_COMPAT_CONSTANT;
  5726. param.acquire_info_size = cmd->data_size;
  5727. param.acquire_info = (uint64_t) acquire_hw_info;
  5728. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5729. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  5730. &param);
  5731. if (rc) {
  5732. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5733. ctx->ctx_id);
  5734. goto free_res;
  5735. }
  5736. /* call HW manager to reserve the resource */
  5737. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5738. &param);
  5739. if (rc != 0) {
  5740. CAM_ERR(CAM_ISP, "Acquire device failed");
  5741. goto free_res;
  5742. }
  5743. ctx_isp->support_consumed_addr =
  5744. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5745. /* Query the context has rdi only resource */
  5746. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5747. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5748. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5749. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5750. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5751. &hw_cmd_args);
  5752. if (rc) {
  5753. CAM_ERR(CAM_ISP, "HW command failed");
  5754. goto free_hw;
  5755. }
  5756. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5757. /*
  5758. * this context has rdi only resource assign rdi only
  5759. * state machine
  5760. */
  5761. CAM_DBG(CAM_ISP, "RDI only session Context");
  5762. ctx_isp->substate_machine_irq =
  5763. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5764. ctx_isp->substate_machine =
  5765. cam_isp_ctx_rdi_only_activated_state_machine;
  5766. ctx_isp->rdi_only_context = true;
  5767. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5768. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5769. ctx_isp->substate_machine_irq =
  5770. cam_isp_ctx_fs2_state_machine_irq;
  5771. ctx_isp->substate_machine =
  5772. cam_isp_ctx_fs2_state_machine;
  5773. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5774. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  5775. ctx_isp->substate_machine_irq =
  5776. cam_isp_ctx_offline_state_machine_irq;
  5777. ctx_isp->substate_machine = NULL;
  5778. } else {
  5779. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5780. ctx_isp->substate_machine_irq =
  5781. cam_isp_ctx_activated_state_machine_irq;
  5782. ctx_isp->substate_machine =
  5783. cam_isp_ctx_activated_state_machine;
  5784. }
  5785. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5786. ctx_isp->hw_acquired = true;
  5787. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5788. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5789. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5790. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5791. trace_cam_context_state("ISP", ctx);
  5792. CAM_DBG(CAM_ISP,
  5793. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5794. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5795. kfree(acquire_hw_info);
  5796. return rc;
  5797. free_hw:
  5798. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5799. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5800. ctx_isp->hw_ctx = NULL;
  5801. ctx_isp->hw_acquired = false;
  5802. free_res:
  5803. kfree(acquire_hw_info);
  5804. end:
  5805. return rc;
  5806. }
  5807. static void cam_req_mgr_process_workq_apply_req_worker(struct work_struct *w)
  5808. {
  5809. cam_req_mgr_process_workq(w);
  5810. }
  5811. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  5812. void *args)
  5813. {
  5814. int rc = 0, i, j;
  5815. struct cam_acquire_hw_cmd_v2 *cmd =
  5816. (struct cam_acquire_hw_cmd_v2 *)args;
  5817. struct cam_hw_acquire_args param;
  5818. struct cam_hw_release_args release;
  5819. struct cam_isp_context *ctx_isp =
  5820. (struct cam_isp_context *) ctx->ctx_priv;
  5821. struct cam_hw_cmd_args hw_cmd_args;
  5822. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5823. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  5824. if (!ctx->hw_mgr_intf) {
  5825. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5826. rc = -EFAULT;
  5827. goto end;
  5828. }
  5829. CAM_DBG(CAM_ISP,
  5830. "session_hdl 0x%x, hdl type %d, res %lld",
  5831. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  5832. /* for now we only support user pointer */
  5833. if (cmd->handle_type != 1) {
  5834. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5835. rc = -EINVAL;
  5836. goto end;
  5837. }
  5838. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  5839. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  5840. goto end;
  5841. }
  5842. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  5843. if (!acquire_hw_info) {
  5844. rc = -ENOMEM;
  5845. goto end;
  5846. }
  5847. CAM_DBG(CAM_ISP, "start copy resources from user");
  5848. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  5849. cmd->data_size)) {
  5850. rc = -EFAULT;
  5851. goto free_res;
  5852. }
  5853. memset(&param, 0, sizeof(param));
  5854. param.context_data = ctx;
  5855. param.event_cb = ctx->irq_cb_intf;
  5856. param.sec_pf_evt_cb = cam_context_dump_pf_info;
  5857. param.num_acq = CAM_API_COMPAT_CONSTANT;
  5858. param.acquire_info_size = cmd->data_size;
  5859. param.acquire_info = (uint64_t) acquire_hw_info;
  5860. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5861. /* call HW manager to reserve the resource */
  5862. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5863. &param);
  5864. if (rc != 0) {
  5865. CAM_ERR(CAM_ISP, "Acquire device failed");
  5866. goto free_res;
  5867. }
  5868. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  5869. if (rc) {
  5870. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5871. ctx->ctx_id);
  5872. goto free_hw;
  5873. }
  5874. /*
  5875. * Set feature flag if applicable
  5876. * custom hw is supported only on v2
  5877. */
  5878. ctx_isp->custom_enabled =
  5879. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  5880. ctx_isp->use_frame_header_ts =
  5881. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5882. ctx_isp->use_default_apply =
  5883. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  5884. ctx_isp->support_consumed_addr =
  5885. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  5886. ctx_isp->aeb_enabled =
  5887. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  5888. if ((ctx_isp->aeb_enabled) && (!isp_ctx_debug.disable_internal_recovery))
  5889. ctx_isp->do_internal_recovery = true;
  5890. /* Query the context has rdi only resource */
  5891. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5892. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5893. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5894. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5895. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5896. &hw_cmd_args);
  5897. if (rc) {
  5898. CAM_ERR(CAM_ISP, "HW command failed");
  5899. goto free_hw;
  5900. }
  5901. if (param.valid_acquired_hw) {
  5902. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5903. cmd->hw_info.acquired_hw_id[i] =
  5904. param.acquired_hw_id[i];
  5905. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5906. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  5907. cmd->hw_info.acquired_hw_path[i][j] =
  5908. param.acquired_hw_path[i][j];
  5909. }
  5910. cmd->hw_info.valid_acquired_hw =
  5911. param.valid_acquired_hw;
  5912. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  5913. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5914. /*
  5915. * this context has rdi only resource assign rdi only
  5916. * state machine
  5917. */
  5918. CAM_DBG(CAM_ISP, "RDI only session Context");
  5919. ctx_isp->substate_machine_irq =
  5920. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5921. ctx_isp->substate_machine =
  5922. cam_isp_ctx_rdi_only_activated_state_machine;
  5923. ctx_isp->rdi_only_context = true;
  5924. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5925. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5926. ctx_isp->substate_machine_irq =
  5927. cam_isp_ctx_fs2_state_machine_irq;
  5928. ctx_isp->substate_machine =
  5929. cam_isp_ctx_fs2_state_machine;
  5930. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5931. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  5932. ctx_isp->substate_machine_irq =
  5933. cam_isp_ctx_offline_state_machine_irq;
  5934. ctx_isp->substate_machine = NULL;
  5935. ctx_isp->offline_context = true;
  5936. } else {
  5937. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5938. ctx_isp->substate_machine_irq =
  5939. cam_isp_ctx_activated_state_machine_irq;
  5940. ctx_isp->substate_machine =
  5941. cam_isp_ctx_activated_state_machine;
  5942. }
  5943. if (ctx_isp->offline_context || ctx_isp->vfps_aux_context) {
  5944. rc = cam_req_mgr_workq_create("ife_apply_req", 20,
  5945. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  5946. cam_req_mgr_process_workq_apply_req_worker);
  5947. if (rc)
  5948. CAM_ERR(CAM_ISP,
  5949. "Failed to create workq for IFE rc:%d offline: %s vfps: %s",
  5950. rc, CAM_BOOL_TO_YESNO(ctx_isp->offline_context),
  5951. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context));
  5952. }
  5953. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5954. ctx_isp->hw_acquired = true;
  5955. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5956. trace_cam_context_state("ISP", ctx);
  5957. CAM_DBG(CAM_ISP,
  5958. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5959. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5960. kfree(acquire_hw_info);
  5961. return rc;
  5962. free_hw:
  5963. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5964. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5965. ctx_isp->hw_ctx = NULL;
  5966. ctx_isp->hw_acquired = false;
  5967. free_res:
  5968. kfree(acquire_hw_info);
  5969. end:
  5970. return rc;
  5971. }
  5972. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  5973. void *args)
  5974. {
  5975. int rc = -EINVAL;
  5976. uint32_t api_version;
  5977. if (!ctx || !args) {
  5978. CAM_ERR(CAM_ISP, "Invalid input pointer");
  5979. return rc;
  5980. }
  5981. api_version = *((uint32_t *)args);
  5982. if (api_version == 1)
  5983. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  5984. else if (api_version == 2)
  5985. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  5986. else
  5987. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  5988. return rc;
  5989. }
  5990. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  5991. struct cam_config_dev_cmd *cmd)
  5992. {
  5993. int rc = 0;
  5994. struct cam_isp_context *ctx_isp =
  5995. (struct cam_isp_context *) ctx->ctx_priv;
  5996. if (!ctx_isp->hw_acquired) {
  5997. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5998. return -EINVAL;
  5999. }
  6000. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6001. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  6002. ctx->state = CAM_CTX_READY;
  6003. trace_cam_context_state("ISP", ctx);
  6004. }
  6005. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  6006. return rc;
  6007. }
  6008. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  6009. struct cam_config_dev_cmd *cmd)
  6010. {
  6011. int rc = 0;
  6012. struct cam_start_stop_dev_cmd start_cmd;
  6013. struct cam_hw_cmd_args hw_cmd_args;
  6014. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6015. struct cam_isp_context *ctx_isp =
  6016. (struct cam_isp_context *) ctx->ctx_priv;
  6017. if (!ctx_isp->hw_acquired) {
  6018. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  6019. rc = -EINVAL;
  6020. goto end;
  6021. }
  6022. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  6023. if (rc)
  6024. goto end;
  6025. if (!ctx_isp->init_received) {
  6026. CAM_WARN(CAM_ISP,
  6027. "Received update packet in flushed state, skip start");
  6028. goto end;
  6029. }
  6030. CAM_DBG(CAM_ISP, "vfps_ctx:%s resume_hw_in_flushed:%d ctx:%d",
  6031. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context),
  6032. ctx_isp->resume_hw_in_flushed,
  6033. ctx->ctx_id);
  6034. if (ctx_isp->vfps_aux_context) {
  6035. /* Resume the HW only when we get first valid req */
  6036. if (!ctx_isp->resume_hw_in_flushed)
  6037. goto end;
  6038. else
  6039. ctx_isp->resume_hw_in_flushed = false;
  6040. }
  6041. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6042. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6043. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6044. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6045. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6046. &hw_cmd_args);
  6047. if (rc) {
  6048. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  6049. goto end;
  6050. }
  6051. start_cmd.dev_handle = cmd->dev_handle;
  6052. start_cmd.session_handle = cmd->session_handle;
  6053. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  6054. if (rc)
  6055. CAM_ERR(CAM_ISP,
  6056. "Failed to re-start HW after flush rc: %d", rc);
  6057. else
  6058. CAM_INFO(CAM_ISP,
  6059. "Received init after flush. Re-start HW complete in ctx:%d",
  6060. ctx->ctx_id);
  6061. end:
  6062. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  6063. ctx_isp->substate_activated);
  6064. return rc;
  6065. }
  6066. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  6067. struct cam_req_mgr_core_dev_link_setup *link)
  6068. {
  6069. int rc = 0;
  6070. struct cam_isp_context *ctx_isp =
  6071. (struct cam_isp_context *) ctx->ctx_priv;
  6072. if (!link) {
  6073. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u",
  6074. link, ctx->ctx_id);
  6075. return -EINVAL;
  6076. }
  6077. if (!link->crm_cb) {
  6078. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u",
  6079. link->crm_cb, ctx->ctx_id);
  6080. return -EINVAL;
  6081. }
  6082. CAM_DBG(CAM_ISP, "Enter.........");
  6083. ctx->link_hdl = link->link_hdl;
  6084. ctx->ctx_crm_intf = link->crm_cb;
  6085. ctx_isp->subscribe_event =
  6086. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  6087. ctx_isp->trigger_id = link->trigger_id;
  6088. /* change state only if we had the init config */
  6089. if (ctx_isp->init_received) {
  6090. ctx->state = CAM_CTX_READY;
  6091. trace_cam_context_state("ISP", ctx);
  6092. }
  6093. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  6094. return rc;
  6095. }
  6096. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  6097. struct cam_req_mgr_core_dev_link_setup *unlink)
  6098. {
  6099. int rc = 0;
  6100. struct cam_isp_context *ctx_isp =
  6101. (struct cam_isp_context *) ctx->ctx_priv;
  6102. ctx->link_hdl = -1;
  6103. ctx->ctx_crm_intf = NULL;
  6104. ctx_isp->trigger_id = -1;
  6105. return rc;
  6106. }
  6107. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  6108. struct cam_req_mgr_device_info *dev_info)
  6109. {
  6110. int rc = 0;
  6111. dev_info->dev_hdl = ctx->dev_hdl;
  6112. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  6113. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  6114. dev_info->p_delay = 1;
  6115. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  6116. dev_info->trigger_on = true;
  6117. return rc;
  6118. }
  6119. static inline void __cam_isp_context_reset_ctx_params(
  6120. struct cam_isp_context *ctx_isp)
  6121. {
  6122. atomic_set(&ctx_isp->process_bubble, 0);
  6123. atomic_set(&ctx_isp->rxd_epoch, 0);
  6124. atomic_set(&ctx_isp->internal_recovery_set, 0);
  6125. ctx_isp->frame_id = 0;
  6126. ctx_isp->sof_timestamp_val = 0;
  6127. ctx_isp->boot_timestamp = 0;
  6128. ctx_isp->active_req_cnt = 0;
  6129. ctx_isp->reported_req_id = 0;
  6130. ctx_isp->reported_frame_id = 0;
  6131. ctx_isp->bubble_frame_cnt = 0;
  6132. ctx_isp->recovery_req_id = 0;
  6133. ctx_isp->aeb_error_cnt = 0;
  6134. }
  6135. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  6136. struct cam_start_stop_dev_cmd *cmd)
  6137. {
  6138. int rc = 0;
  6139. int i;
  6140. struct cam_isp_start_args start_isp;
  6141. struct cam_ctx_request *req;
  6142. struct cam_isp_ctx_req *req_isp;
  6143. struct cam_isp_context *ctx_isp =
  6144. (struct cam_isp_context *) ctx->ctx_priv;
  6145. if (cmd->session_handle != ctx->session_hdl ||
  6146. cmd->dev_handle != ctx->dev_hdl) {
  6147. rc = -EPERM;
  6148. goto end;
  6149. }
  6150. if (list_empty(&ctx->pending_req_list)) {
  6151. /* should never happen */
  6152. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  6153. rc = -EFAULT;
  6154. goto end;
  6155. } else {
  6156. req = list_first_entry(&ctx->pending_req_list,
  6157. struct cam_ctx_request, list);
  6158. }
  6159. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6160. if (!ctx_isp->hw_ctx) {
  6161. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  6162. rc = -EFAULT;
  6163. goto end;
  6164. }
  6165. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6166. start_isp.hw_config.request_id = req->request_id;
  6167. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  6168. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  6169. start_isp.hw_config.priv = &req_isp->hw_update_data;
  6170. start_isp.hw_config.init_packet = 1;
  6171. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  6172. start_isp.hw_config.cdm_reset_before_apply = false;
  6173. start_isp.is_internal_start = false;
  6174. ctx_isp->last_applied_req_id = req->request_id;
  6175. if (ctx->state == CAM_CTX_FLUSHED)
  6176. start_isp.start_only = true;
  6177. else
  6178. start_isp.start_only = false;
  6179. __cam_isp_context_reset_ctx_params(ctx_isp);
  6180. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  6181. CAM_ISP_CTX_ACTIVATED_APPLIED :
  6182. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  6183. CAM_ISP_CTX_ACTIVATED_SOF;
  6184. atomic64_set(&ctx_isp->state_monitor_head, -1);
  6185. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6186. atomic64_set(&ctx_isp->event_record_head[i], -1);
  6187. /*
  6188. * In case of CSID TPG we might receive SOF and RUP IRQs
  6189. * before hw_mgr_intf->hw_start has returned. So move
  6190. * req out of pending list before hw_start and add it
  6191. * back to pending list if hw_start fails.
  6192. */
  6193. list_del_init(&req->list);
  6194. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  6195. list_add_tail(&req->list, &ctx->free_req_list);
  6196. atomic_set(&ctx_isp->rxd_epoch, 1);
  6197. CAM_DBG(CAM_REQ,
  6198. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  6199. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  6200. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  6201. list_add_tail(&req->list, &ctx->wait_req_list);
  6202. CAM_DBG(CAM_REQ,
  6203. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  6204. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  6205. } else {
  6206. list_add_tail(&req->list, &ctx->active_req_list);
  6207. ctx_isp->active_req_cnt++;
  6208. CAM_DBG(CAM_REQ,
  6209. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  6210. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  6211. ctx_isp->offline_context);
  6212. }
  6213. /*
  6214. * Only place to change state before calling the hw due to
  6215. * hardware tasklet has higher priority that can cause the
  6216. * irq handling comes early
  6217. */
  6218. ctx->state = CAM_CTX_ACTIVATED;
  6219. trace_cam_context_state("ISP", ctx);
  6220. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  6221. &start_isp);
  6222. if (rc) {
  6223. /* HW failure. user need to clean up the resource */
  6224. CAM_ERR(CAM_ISP, "Start HW failed");
  6225. ctx->state = CAM_CTX_READY;
  6226. if ((rc == -ETIMEDOUT) &&
  6227. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  6228. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  6229. trace_cam_context_state("ISP", ctx);
  6230. list_del_init(&req->list);
  6231. list_add(&req->list, &ctx->pending_req_list);
  6232. goto end;
  6233. }
  6234. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  6235. end:
  6236. return rc;
  6237. }
  6238. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  6239. struct cam_req_mgr_core_dev_link_setup *unlink)
  6240. {
  6241. int rc = 0;
  6242. ctx->link_hdl = -1;
  6243. ctx->ctx_crm_intf = NULL;
  6244. ctx->state = CAM_CTX_ACQUIRED;
  6245. trace_cam_context_state("ISP", ctx);
  6246. return rc;
  6247. }
  6248. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  6249. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  6250. {
  6251. int rc = 0;
  6252. uint32_t i;
  6253. struct cam_hw_stop_args stop;
  6254. struct cam_ctx_request *req;
  6255. struct cam_isp_ctx_req *req_isp;
  6256. struct cam_isp_context *ctx_isp =
  6257. (struct cam_isp_context *) ctx->ctx_priv;
  6258. struct cam_isp_stop_args stop_isp;
  6259. /* Mask off all the incoming hardware events */
  6260. spin_lock_bh(&ctx->lock);
  6261. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  6262. spin_unlock_bh(&ctx->lock);
  6263. /* stop hw first */
  6264. if (ctx_isp->hw_ctx) {
  6265. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6266. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  6267. stop_isp.stop_only = false;
  6268. stop_isp.is_internal_stop = false;
  6269. stop.args = (void *) &stop_isp;
  6270. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  6271. &stop);
  6272. }
  6273. CAM_DBG(CAM_ISP, "next Substate[%s]",
  6274. __cam_isp_ctx_substate_val_to_type(
  6275. ctx_isp->substate_activated));
  6276. if (ctx->ctx_crm_intf &&
  6277. ctx->ctx_crm_intf->notify_stop) {
  6278. struct cam_req_mgr_notify_stop notify;
  6279. notify.link_hdl = ctx->link_hdl;
  6280. CAM_DBG(CAM_ISP,
  6281. "Notify CRM about device stop ctx %u link 0x%x",
  6282. ctx->ctx_id, ctx->link_hdl);
  6283. ctx->ctx_crm_intf->notify_stop(&notify);
  6284. } else if (!ctx_isp->offline_context)
  6285. CAM_ERR(CAM_ISP, "cb not present");
  6286. while (!list_empty(&ctx->pending_req_list)) {
  6287. req = list_first_entry(&ctx->pending_req_list,
  6288. struct cam_ctx_request, list);
  6289. list_del_init(&req->list);
  6290. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6291. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  6292. req_isp->num_fence_map_out);
  6293. for (i = 0; i < req_isp->num_fence_map_out; i++)
  6294. if (req_isp->fence_map_out[i].sync_id != -1) {
  6295. cam_sync_signal(
  6296. req_isp->fence_map_out[i].sync_id,
  6297. CAM_SYNC_STATE_SIGNALED_CANCEL,
  6298. CAM_SYNC_ISP_EVENT_HW_STOP);
  6299. }
  6300. list_add_tail(&req->list, &ctx->free_req_list);
  6301. }
  6302. while (!list_empty(&ctx->wait_req_list)) {
  6303. req = list_first_entry(&ctx->wait_req_list,
  6304. struct cam_ctx_request, list);
  6305. list_del_init(&req->list);
  6306. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6307. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  6308. req_isp->num_fence_map_out);
  6309. for (i = 0; i < req_isp->num_fence_map_out; i++)
  6310. if (req_isp->fence_map_out[i].sync_id != -1) {
  6311. cam_sync_signal(
  6312. req_isp->fence_map_out[i].sync_id,
  6313. CAM_SYNC_STATE_SIGNALED_CANCEL,
  6314. CAM_SYNC_ISP_EVENT_HW_STOP);
  6315. }
  6316. list_add_tail(&req->list, &ctx->free_req_list);
  6317. }
  6318. while (!list_empty(&ctx->active_req_list)) {
  6319. req = list_first_entry(&ctx->active_req_list,
  6320. struct cam_ctx_request, list);
  6321. list_del_init(&req->list);
  6322. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6323. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  6324. req_isp->num_fence_map_out);
  6325. for (i = 0; i < req_isp->num_fence_map_out; i++)
  6326. if (req_isp->fence_map_out[i].sync_id != -1) {
  6327. cam_sync_signal(
  6328. req_isp->fence_map_out[i].sync_id,
  6329. CAM_SYNC_STATE_SIGNALED_CANCEL,
  6330. CAM_SYNC_ISP_EVENT_HW_STOP);
  6331. }
  6332. list_add_tail(&req->list, &ctx->free_req_list);
  6333. }
  6334. ctx_isp->frame_id = 0;
  6335. ctx_isp->active_req_cnt = 0;
  6336. ctx_isp->reported_req_id = 0;
  6337. ctx_isp->reported_frame_id = 0;
  6338. ctx_isp->last_applied_req_id = 0;
  6339. ctx_isp->req_info.last_bufdone_req_id = 0;
  6340. ctx_isp->bubble_frame_cnt = 0;
  6341. atomic_set(&ctx_isp->process_bubble, 0);
  6342. atomic_set(&ctx_isp->internal_recovery_set, 0);
  6343. atomic_set(&ctx_isp->rxd_epoch, 0);
  6344. atomic64_set(&ctx_isp->state_monitor_head, -1);
  6345. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6346. atomic64_set(&ctx_isp->event_record_head[i], -1);
  6347. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  6348. ctx->state, ctx->ctx_id);
  6349. if (!stop_cmd) {
  6350. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  6351. if (rc)
  6352. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  6353. }
  6354. return rc;
  6355. }
  6356. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  6357. struct cam_start_stop_dev_cmd *cmd)
  6358. {
  6359. int rc = 0;
  6360. struct cam_isp_context *ctx_isp =
  6361. (struct cam_isp_context *)ctx->ctx_priv;
  6362. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  6363. ctx_isp->init_received = false;
  6364. ctx->state = CAM_CTX_ACQUIRED;
  6365. trace_cam_context_state("ISP", ctx);
  6366. return rc;
  6367. }
  6368. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  6369. struct cam_release_dev_cmd *cmd)
  6370. {
  6371. int rc = 0;
  6372. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  6373. if (rc)
  6374. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  6375. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  6376. if (rc)
  6377. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  6378. return rc;
  6379. }
  6380. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  6381. void *cmd)
  6382. {
  6383. int rc = 0;
  6384. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  6385. if (rc)
  6386. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  6387. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  6388. if (rc)
  6389. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  6390. return rc;
  6391. }
  6392. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  6393. {
  6394. int rc = 0;
  6395. struct cam_hw_cmd_args hw_cmd_args;
  6396. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6397. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  6398. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6399. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  6400. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6401. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6402. &hw_cmd_args);
  6403. return rc;
  6404. }
  6405. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  6406. {
  6407. int rc = 0;
  6408. struct cam_hw_cmd_args hw_cmd_args;
  6409. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6410. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  6411. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6412. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6413. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6414. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6415. &hw_cmd_args);
  6416. return rc;
  6417. }
  6418. static int __cam_isp_ctx_handle_sof_freeze_evt(
  6419. struct cam_context *ctx)
  6420. {
  6421. int rc = 0;
  6422. struct cam_hw_cmd_args hw_cmd_args;
  6423. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6424. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  6425. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6426. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  6427. isp_hw_cmd_args.u.sof_irq_enable = 1;
  6428. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6429. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6430. &hw_cmd_args);
  6431. return rc;
  6432. }
  6433. static int __cam_isp_ctx_reset_and_recover(
  6434. bool skip_resume, struct cam_context *ctx)
  6435. {
  6436. int rc = 0;
  6437. struct cam_isp_context *ctx_isp =
  6438. (struct cam_isp_context *)ctx->ctx_priv;
  6439. struct cam_isp_stop_args stop_isp;
  6440. struct cam_hw_stop_args stop_args;
  6441. struct cam_isp_start_args start_isp;
  6442. struct cam_hw_cmd_args hw_cmd_args;
  6443. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  6444. struct cam_ctx_request *req;
  6445. struct cam_isp_ctx_req *req_isp;
  6446. spin_lock_bh(&ctx->lock);
  6447. if (ctx_isp->active_req_cnt) {
  6448. spin_unlock_bh(&ctx->lock);
  6449. CAM_WARN(CAM_ISP,
  6450. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  6451. ctx_isp->active_req_cnt, ctx->ctx_id,
  6452. ctx->link_hdl, ctx_isp->recovery_req_id);
  6453. goto end;
  6454. }
  6455. if (ctx->state != CAM_CTX_ACTIVATED) {
  6456. spin_unlock_bh(&ctx->lock);
  6457. CAM_ERR(CAM_ISP,
  6458. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  6459. ctx->state, ctx->ctx_id,
  6460. ctx->link_hdl, ctx_isp->recovery_req_id);
  6461. rc = -EINVAL;
  6462. goto end;
  6463. }
  6464. if (list_empty(&ctx->pending_req_list)) {
  6465. /* Cannot start with no request */
  6466. spin_unlock_bh(&ctx->lock);
  6467. CAM_ERR(CAM_ISP,
  6468. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  6469. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  6470. rc = -EFAULT;
  6471. goto end;
  6472. }
  6473. if (!ctx_isp->hw_ctx) {
  6474. spin_unlock_bh(&ctx->lock);
  6475. CAM_ERR(CAM_ISP,
  6476. "Invalid hw context pointer ctx: %u on link: 0x%x",
  6477. ctx->ctx_id, ctx->link_hdl);
  6478. rc = -EFAULT;
  6479. goto end;
  6480. }
  6481. /* Block all events till HW is resumed */
  6482. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  6483. req = list_first_entry(&ctx->pending_req_list,
  6484. struct cam_ctx_request, list);
  6485. spin_unlock_bh(&ctx->lock);
  6486. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6487. CAM_INFO(CAM_ISP,
  6488. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  6489. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  6490. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6491. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  6492. stop_isp.stop_only = true;
  6493. stop_isp.is_internal_stop = true;
  6494. stop_args.args = (void *)&stop_isp;
  6495. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  6496. &stop_args);
  6497. if (rc) {
  6498. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u",
  6499. rc, ctx->ctx_id);
  6500. goto end;
  6501. }
  6502. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  6503. ctx->ctx_id, ctx->link_hdl);
  6504. /* API provides provision to stream off and not resume as well in case of fatal errors */
  6505. if (skip_resume) {
  6506. atomic_set(&ctx_isp->internal_recovery_set, 0);
  6507. CAM_INFO(CAM_ISP,
  6508. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  6509. ctx->ctx_id, ctx_isp->last_applied_req_id,
  6510. ctx_isp->recovery_req_id, ctx->link_hdl);
  6511. goto end;
  6512. }
  6513. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6514. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  6515. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  6516. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  6517. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  6518. &hw_cmd_args);
  6519. if (rc) {
  6520. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u", rc, ctx->ctx_id);
  6521. goto end;
  6522. }
  6523. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  6524. ctx->ctx_id, ctx->link_hdl);
  6525. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  6526. start_isp.hw_config.request_id = req->request_id;
  6527. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  6528. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  6529. start_isp.hw_config.priv = &req_isp->hw_update_data;
  6530. start_isp.hw_config.init_packet = 1;
  6531. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  6532. start_isp.hw_config.cdm_reset_before_apply = false;
  6533. start_isp.start_only = true;
  6534. start_isp.is_internal_start = true;
  6535. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  6536. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  6537. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  6538. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  6539. &start_isp);
  6540. if (rc) {
  6541. CAM_ERR(CAM_ISP, "Start HW failed");
  6542. ctx->state = CAM_CTX_READY;
  6543. goto end;
  6544. }
  6545. /* IQ applied for this request, on next trigger skip IQ cfg */
  6546. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  6547. /* Notify userland that KMD has done internal recovery */
  6548. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_WARN_TYPE_KMD_RECOVERY,
  6549. 0, req->request_id, ctx);
  6550. CAM_INFO(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x for req: %llu",
  6551. ctx->ctx_id, ctx->link_hdl, req->request_id);
  6552. end:
  6553. return rc;
  6554. }
  6555. static bool __cam_isp_ctx_try_internal_recovery_for_bubble(
  6556. int64_t error_req_id, struct cam_context *ctx)
  6557. {
  6558. int rc;
  6559. struct cam_isp_context *ctx_isp =
  6560. (struct cam_isp_context *)ctx->ctx_priv;
  6561. /* Perform recovery if bubble recovery is stalled */
  6562. if (!atomic_read(&ctx_isp->process_bubble))
  6563. return false;
  6564. /* Validate if errored request has been applied */
  6565. if (ctx_isp->last_applied_req_id < error_req_id) {
  6566. CAM_WARN(CAM_ISP,
  6567. "Skip trying for internal recovery last applied: %lld error_req: %lld for ctx: %u on link: 0x%x",
  6568. ctx_isp->last_applied_req_id, error_req_id,
  6569. ctx->ctx_id, ctx->link_hdl);
  6570. return false;
  6571. }
  6572. if (__cam_isp_ctx_validate_for_req_reapply_util(ctx_isp)) {
  6573. CAM_WARN(CAM_ISP,
  6574. "Internal recovery not possible for ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  6575. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  6576. return false;
  6577. }
  6578. /* Trigger reset and recover */
  6579. atomic_set(&ctx_isp->internal_recovery_set, 1);
  6580. rc = __cam_isp_ctx_reset_and_recover(false, ctx);
  6581. if (rc) {
  6582. CAM_WARN(CAM_ISP,
  6583. "Internal recovery failed in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  6584. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  6585. atomic_set(&ctx_isp->internal_recovery_set, 0);
  6586. goto error;
  6587. }
  6588. CAM_DBG(CAM_ISP,
  6589. "Internal recovery done in ctx: %u on link: 0x%x req: %lld [last_applied: %lld]",
  6590. ctx->ctx_id, ctx->link_hdl, error_req_id, ctx_isp->last_applied_req_id);
  6591. return true;
  6592. error:
  6593. return false;
  6594. }
  6595. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  6596. struct cam_req_mgr_link_evt_data *link_evt_data)
  6597. {
  6598. int rc = 0;
  6599. struct cam_isp_context *ctx_isp =
  6600. (struct cam_isp_context *) ctx->ctx_priv;
  6601. if ((ctx->state == CAM_CTX_ACQUIRED) &&
  6602. (link_evt_data->evt_type != CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES)) {
  6603. CAM_WARN(CAM_ISP,
  6604. "Get unexpect evt:%d in acquired state",
  6605. link_evt_data->evt_type);
  6606. return -EINVAL;
  6607. }
  6608. switch (link_evt_data->evt_type) {
  6609. case CAM_REQ_MGR_LINK_EVT_ERR:
  6610. case CAM_REQ_MGR_LINK_EVT_EOF:
  6611. /* No handling */
  6612. break;
  6613. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  6614. rc = __cam_isp_ctx_link_pause(ctx);
  6615. break;
  6616. case CAM_REQ_MGR_LINK_EVT_RESUME:
  6617. rc = __cam_isp_ctx_link_resume(ctx);
  6618. break;
  6619. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  6620. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  6621. break;
  6622. case CAM_REQ_MGR_LINK_EVT_STALLED: {
  6623. bool internal_recovery_skipped = false;
  6624. if (ctx->state == CAM_CTX_ACTIVATED) {
  6625. if (link_evt_data->try_for_recovery)
  6626. internal_recovery_skipped =
  6627. __cam_isp_ctx_try_internal_recovery_for_bubble(
  6628. link_evt_data->req_id, ctx);
  6629. if (!internal_recovery_skipped)
  6630. rc = __cam_isp_ctx_trigger_reg_dump(
  6631. CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  6632. }
  6633. link_evt_data->try_for_recovery = internal_recovery_skipped;
  6634. }
  6635. break;
  6636. case CAM_REQ_MGR_LINK_EVT_UPDATE_PROPERTIES:
  6637. if (link_evt_data->u.properties_mask &
  6638. CAM_LINK_PROPERTY_SENSOR_STANDBY_AFTER_EOF)
  6639. ctx_isp->vfps_aux_context = true;
  6640. else
  6641. ctx_isp->vfps_aux_context = false;
  6642. CAM_DBG(CAM_ISP, "vfps_aux_context:%s on ctx: %u",
  6643. CAM_BOOL_TO_YESNO(ctx_isp->vfps_aux_context), ctx->ctx_id);
  6644. break;
  6645. default:
  6646. CAM_WARN(CAM_ISP,
  6647. "Unsupported event type: 0x%x on ctx: %u",
  6648. link_evt_data->evt_type, ctx->ctx_id);
  6649. rc = -EINVAL;
  6650. break;
  6651. }
  6652. return rc;
  6653. }
  6654. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  6655. struct cam_req_mgr_core_dev_link_setup *unlink)
  6656. {
  6657. int rc = 0;
  6658. CAM_WARN(CAM_ISP,
  6659. "Received unlink in activated state. It's unexpected");
  6660. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  6661. if (rc)
  6662. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  6663. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  6664. if (rc)
  6665. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  6666. return rc;
  6667. }
  6668. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  6669. struct cam_req_mgr_apply_request *apply)
  6670. {
  6671. int rc = 0;
  6672. struct cam_ctx_ops *ctx_ops = NULL;
  6673. struct cam_isp_context *ctx_isp =
  6674. (struct cam_isp_context *) ctx->ctx_priv;
  6675. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  6676. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  6677. __cam_isp_ctx_substate_val_to_type(
  6678. ctx_isp->substate_activated), apply->request_id);
  6679. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  6680. if (ctx_ops->crm_ops.apply_req) {
  6681. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  6682. } else {
  6683. CAM_WARN_RATE_LIMIT(CAM_ISP,
  6684. "No handle function in activated Substate[%s]",
  6685. __cam_isp_ctx_substate_val_to_type(
  6686. ctx_isp->substate_activated));
  6687. rc = -EFAULT;
  6688. }
  6689. if (rc)
  6690. CAM_WARN_RATE_LIMIT(CAM_ISP,
  6691. "Apply failed in active Substate[%s] rc %d",
  6692. __cam_isp_ctx_substate_val_to_type(
  6693. ctx_isp->substate_activated), rc);
  6694. return rc;
  6695. }
  6696. static int __cam_isp_ctx_apply_default_settings(
  6697. struct cam_context *ctx,
  6698. struct cam_req_mgr_apply_request *apply)
  6699. {
  6700. int rc = 0;
  6701. struct cam_ctx_ops *ctx_ops = NULL;
  6702. struct cam_isp_context *ctx_isp =
  6703. (struct cam_isp_context *) ctx->ctx_priv;
  6704. if ((!ctx_isp->use_default_apply) && !(atomic_read(&ctx_isp->internal_recovery_set)))
  6705. return 0;
  6706. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  6707. CAM_WARN(CAM_ISP,
  6708. "Trigger: %u not subscribed for: %u",
  6709. apply->trigger_point, ctx_isp->subscribe_event);
  6710. return 0;
  6711. }
  6712. /* Allow apply default settings for IFE only at SOF */
  6713. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  6714. return 0;
  6715. if (atomic_read(&ctx_isp->internal_recovery_set))
  6716. return __cam_isp_ctx_reset_and_recover(false, ctx);
  6717. CAM_DBG(CAM_ISP,
  6718. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  6719. ctx_isp->substate_activated, apply->request_id,
  6720. ctx->ctx_id, ctx->link_hdl);
  6721. ctx_ops = &ctx_isp->substate_machine[
  6722. ctx_isp->substate_activated];
  6723. if (ctx_ops->crm_ops.notify_frame_skip) {
  6724. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  6725. } else {
  6726. CAM_WARN_RATE_LIMIT(CAM_ISP,
  6727. "No handle function in activated substate %d",
  6728. ctx_isp->substate_activated);
  6729. rc = -EFAULT;
  6730. }
  6731. if (rc)
  6732. CAM_WARN_RATE_LIMIT(CAM_ISP,
  6733. "Apply default failed in active substate %d rc %d",
  6734. ctx_isp->substate_activated, rc);
  6735. return rc;
  6736. }
  6737. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  6738. uint32_t evt_id, void *evt_data)
  6739. {
  6740. int rc = 0;
  6741. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  6742. struct cam_context *ctx = (struct cam_context *)context;
  6743. struct cam_isp_context *ctx_isp =
  6744. (struct cam_isp_context *)ctx->ctx_priv;
  6745. spin_lock(&ctx->lock);
  6746. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  6747. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  6748. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d, ctx:%d",
  6749. ctx->state, __cam_isp_ctx_substate_val_to_type(
  6750. ctx_isp->substate_activated), evt_id,
  6751. ctx->ctx_id);
  6752. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  6753. if (irq_ops->irq_ops[evt_id]) {
  6754. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  6755. } else {
  6756. CAM_DBG(CAM_ISP,
  6757. "No handle function for Substate[%s], evt id %d, ctx:%d",
  6758. __cam_isp_ctx_substate_val_to_type(
  6759. ctx_isp->substate_activated), evt_id,
  6760. ctx->ctx_id);
  6761. if (isp_ctx_debug.enable_state_monitor_dump)
  6762. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  6763. }
  6764. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s], ctx:%d",
  6765. ctx->state, __cam_isp_ctx_substate_val_to_type(
  6766. ctx_isp->substate_activated), ctx->ctx_id);
  6767. spin_unlock(&ctx->lock);
  6768. return rc;
  6769. }
  6770. static int cam_isp_context_inject_error(void *context, void *err_param)
  6771. {
  6772. int rc = 0;
  6773. struct cam_context *ctx = (struct cam_context *)context;
  6774. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  6775. uint64_t req_id;
  6776. uint32_t err_code;
  6777. uint32_t err_type;
  6778. if (!err_param) {
  6779. CAM_ERR(CAM_ISP, "err_param not valid");
  6780. return -EINVAL;
  6781. }
  6782. req_id = ((struct cam_err_inject_param *)err_param)->req_id;
  6783. err_type = ((struct cam_err_inject_param *)err_param)->err_type;
  6784. err_code = ((struct cam_err_inject_param *)err_param)->err_code;
  6785. switch (err_type) {
  6786. case CAM_REQ_MGR_ERROR_TYPE_RECOVERY:
  6787. switch (err_code) {
  6788. case CAM_REQ_MGR_LINK_STALLED_ERROR:
  6789. case CAM_REQ_MGR_CSID_FIFO_OVERFLOW_ERROR:
  6790. case CAM_REQ_MGR_CSID_RECOVERY_OVERFLOW_ERROR:
  6791. case CAM_REQ_MGR_CSID_PIXEL_COUNT_MISMATCH:
  6792. case CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING:
  6793. break;
  6794. default:
  6795. CAM_ERR(CAM_ISP, "err code not supported %d", err_code);
  6796. return -EINVAL;
  6797. }
  6798. break;
  6799. case CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY:
  6800. switch (err_code) {
  6801. case CAM_REQ_MGR_CSID_FATAL_ERROR:
  6802. break;
  6803. default:
  6804. CAM_ERR(CAM_ISP, "err code not supported %d", err_code);
  6805. return -EINVAL;
  6806. }
  6807. break;
  6808. case CAM_REQ_MGR_ERROR_TYPE_SOF_FREEZE:
  6809. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  6810. break;
  6811. case CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT:
  6812. err_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  6813. break;
  6814. default:
  6815. CAM_ERR(CAM_ISP, "err type not supported %d", err_type);
  6816. return -EINVAL;
  6817. }
  6818. CAM_DBG(CAM_ISP, "inject err isp code: %d type: %d ctx id: %d",
  6819. err_code, err_type, ctx->ctx_id);
  6820. ctx_isp->err_inject_params.err_code = err_code;
  6821. ctx_isp->err_inject_params.err_type = err_type;
  6822. ctx_isp->err_inject_params.err_req_id = req_id;
  6823. ctx_isp->err_inject_params.is_valid = true;
  6824. return rc;
  6825. }
  6826. /* top state machine */
  6827. static struct cam_ctx_ops
  6828. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  6829. /* Uninit */
  6830. {
  6831. .ioctl_ops = {},
  6832. .crm_ops = {},
  6833. .irq_ops = NULL,
  6834. },
  6835. /* Available */
  6836. {
  6837. .ioctl_ops = {
  6838. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  6839. },
  6840. .crm_ops = {},
  6841. .irq_ops = NULL,
  6842. },
  6843. /* Acquired */
  6844. {
  6845. .ioctl_ops = {
  6846. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  6847. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  6848. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  6849. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6850. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  6851. },
  6852. .crm_ops = {
  6853. .link = __cam_isp_ctx_link_in_acquired,
  6854. .unlink = __cam_isp_ctx_unlink_in_acquired,
  6855. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  6856. .process_evt = __cam_isp_ctx_process_evt,
  6857. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  6858. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6859. },
  6860. .irq_ops = NULL,
  6861. .pagefault_ops = cam_isp_context_dump_requests,
  6862. .dumpinfo_ops = cam_isp_context_info_dump,
  6863. .err_inject_ops = cam_isp_context_inject_error,
  6864. },
  6865. /* Ready */
  6866. {
  6867. .ioctl_ops = {
  6868. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  6869. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  6870. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  6871. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6872. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  6873. },
  6874. .crm_ops = {
  6875. .unlink = __cam_isp_ctx_unlink_in_ready,
  6876. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  6877. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6878. },
  6879. .irq_ops = NULL,
  6880. .pagefault_ops = cam_isp_context_dump_requests,
  6881. .dumpinfo_ops = cam_isp_context_info_dump,
  6882. .err_inject_ops = cam_isp_context_inject_error,
  6883. },
  6884. /* Flushed */
  6885. {
  6886. .ioctl_ops = {
  6887. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6888. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6889. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  6890. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6891. },
  6892. .crm_ops = {
  6893. .unlink = __cam_isp_ctx_unlink_in_ready,
  6894. .process_evt = __cam_isp_ctx_process_evt,
  6895. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  6896. },
  6897. .irq_ops = NULL,
  6898. .pagefault_ops = cam_isp_context_dump_requests,
  6899. .dumpinfo_ops = cam_isp_context_info_dump,
  6900. .err_inject_ops = cam_isp_context_inject_error,
  6901. },
  6902. /* Activated */
  6903. {
  6904. .ioctl_ops = {
  6905. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6906. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6907. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  6908. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6909. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6910. },
  6911. .crm_ops = {
  6912. .unlink = __cam_isp_ctx_unlink_in_activated,
  6913. .apply_req = __cam_isp_ctx_apply_req,
  6914. .notify_frame_skip =
  6915. __cam_isp_ctx_apply_default_settings,
  6916. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  6917. .process_evt = __cam_isp_ctx_process_evt,
  6918. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6919. },
  6920. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  6921. .pagefault_ops = cam_isp_context_dump_requests,
  6922. .dumpinfo_ops = cam_isp_context_info_dump,
  6923. .recovery_ops = cam_isp_context_hw_recovery,
  6924. .err_inject_ops = cam_isp_context_inject_error,
  6925. },
  6926. };
  6927. static int cam_isp_context_hw_recovery(void *priv, void *data)
  6928. {
  6929. struct cam_context *ctx = priv;
  6930. int rc = -EPERM;
  6931. if (ctx->hw_mgr_intf->hw_recovery)
  6932. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  6933. else
  6934. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery");
  6935. return rc;
  6936. }
  6937. static void cam_isp_context_find_faulted_context(struct cam_context *ctx,
  6938. struct list_head *req_list, struct cam_hw_dump_pf_args *pf_args, bool *found)
  6939. {
  6940. struct cam_ctx_request *req = NULL;
  6941. struct cam_ctx_request *req_temp = NULL;
  6942. int rc;
  6943. *found = false;
  6944. list_for_each_entry_safe(req, req_temp, req_list, list) {
  6945. CAM_INFO(CAM_ISP, "List req_id: %llu ctx id: %u",
  6946. req->request_id, ctx->ctx_id);
  6947. rc = cam_context_dump_pf_info_to_hw(ctx, pf_args, &req->pf_data);
  6948. if (rc)
  6949. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6950. /*
  6951. * Found faulted buffer. Even if faulted ctx is found, but
  6952. * continue to search for faulted buffer
  6953. */
  6954. if (pf_args->pf_context_info.mem_type != CAM_FAULT_BUF_NOT_FOUND) {
  6955. *found = true;
  6956. break;
  6957. }
  6958. }
  6959. }
  6960. static int cam_isp_context_dump_requests(void *data, void *args)
  6961. {
  6962. struct cam_context *ctx = (struct cam_context *)data;
  6963. struct cam_isp_context *ctx_isp;
  6964. struct cam_hw_dump_pf_args *pf_args = (struct cam_hw_dump_pf_args *)args;
  6965. int rc = 0;
  6966. bool found;
  6967. if (!ctx || !pf_args) {
  6968. CAM_ERR(CAM_ISP, "Invalid ctx %pK or pf args %pK",
  6969. ctx, pf_args);
  6970. return -EINVAL;
  6971. }
  6972. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  6973. if (!ctx_isp) {
  6974. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  6975. return -EINVAL;
  6976. }
  6977. if (pf_args->handle_sec_pf)
  6978. goto end;
  6979. CAM_INFO(CAM_ISP,
  6980. "Iterating over active list for isp ctx %d state %d",
  6981. ctx->ctx_id, ctx->state);
  6982. cam_isp_context_find_faulted_context(ctx, &ctx->active_req_list,
  6983. pf_args, &found);
  6984. if (found)
  6985. goto end;
  6986. CAM_INFO(CAM_ISP,
  6987. "Iterating over waiting list of isp ctx %d state %d",
  6988. ctx->ctx_id, ctx->state);
  6989. cam_isp_context_find_faulted_context(ctx, &ctx->wait_req_list,
  6990. pf_args, &found);
  6991. if (found)
  6992. goto end;
  6993. /*
  6994. * In certain scenarios we observe both overflow and SMMU pagefault
  6995. * for a particular request. If overflow is handled before page fault
  6996. * we need to traverse through pending request list because if
  6997. * bubble recovery is enabled on any request we move that request
  6998. * and all the subsequent requests to the pending list while handling
  6999. * overflow error.
  7000. */
  7001. CAM_INFO(CAM_ISP,
  7002. "Iterating over pending req list of isp ctx %d state %d",
  7003. ctx->ctx_id, ctx->state);
  7004. cam_isp_context_find_faulted_context(ctx, &ctx->pending_req_list,
  7005. pf_args, &found);
  7006. if (found)
  7007. goto end;
  7008. end:
  7009. if (pf_args->pf_context_info.resource_type) {
  7010. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  7011. CAM_INFO(CAM_ISP,
  7012. "Page fault on resource:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  7013. __cam_isp_resource_handle_id_to_type(ctx_isp->isp_device_type,
  7014. pf_args->pf_context_info.resource_type),
  7015. pf_args->pf_context_info.resource_type, ctx->ctx_id, ctx_isp->frame_id,
  7016. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  7017. }
  7018. /*
  7019. * Send PF notification to UMD if PF found on current CTX
  7020. * or it is forced to send PF notification to UMD even if no
  7021. * faulted context found
  7022. */
  7023. if (pf_args->pf_context_info.ctx_found ||
  7024. pf_args->pf_context_info.force_send_pf_evt)
  7025. rc = cam_context_send_pf_evt(ctx, pf_args);
  7026. if (rc)
  7027. CAM_ERR(CAM_ISP,
  7028. "Failed to notify PF event to userspace rc: %d", rc);
  7029. return rc;
  7030. }
  7031. static int cam_isp_context_debug_register(void)
  7032. {
  7033. int rc = 0;
  7034. struct dentry *dbgfileptr = NULL;
  7035. if (!cam_debugfs_available())
  7036. return 0;
  7037. rc = cam_debugfs_create_subdir("isp_ctx", &dbgfileptr);
  7038. if (rc) {
  7039. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  7040. return rc;
  7041. }
  7042. /* Store parent inode for cleanup in caller */
  7043. isp_ctx_debug.dentry = dbgfileptr;
  7044. debugfs_create_u32("enable_state_monitor_dump", 0644,
  7045. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  7046. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  7047. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  7048. debugfs_create_bool("disable_internal_recovery", 0644,
  7049. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery);
  7050. return 0;
  7051. }
  7052. int cam_isp_context_init(struct cam_isp_context *ctx,
  7053. struct cam_context *ctx_base,
  7054. struct cam_req_mgr_kmd_ops *crm_node_intf,
  7055. struct cam_hw_mgr_intf *hw_intf,
  7056. uint32_t ctx_id,
  7057. uint32_t isp_device_type,
  7058. int img_iommu_hdl)
  7059. {
  7060. int rc = -1;
  7061. int i;
  7062. if (!ctx || !ctx_base) {
  7063. CAM_ERR(CAM_ISP, "Invalid Context");
  7064. goto err;
  7065. }
  7066. /* ISP context setup */
  7067. memset(ctx, 0, sizeof(*ctx));
  7068. ctx->base = ctx_base;
  7069. ctx->frame_id = 0;
  7070. ctx->custom_enabled = false;
  7071. ctx->use_frame_header_ts = false;
  7072. ctx->use_default_apply = false;
  7073. ctx->active_req_cnt = 0;
  7074. ctx->reported_req_id = 0;
  7075. ctx->bubble_frame_cnt = 0;
  7076. ctx->req_info.last_bufdone_req_id = 0;
  7077. ctx->v4l2_event_sub_ids = 0;
  7078. ctx->hw_ctx = NULL;
  7079. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  7080. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  7081. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  7082. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  7083. ctx->isp_device_type = isp_device_type;
  7084. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  7085. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  7086. ctx->req_isp[i].base = &ctx->req_base[i];
  7087. }
  7088. /* camera context setup */
  7089. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  7090. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  7091. if (rc) {
  7092. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  7093. goto err;
  7094. }
  7095. /* link camera context with isp context */
  7096. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  7097. ctx_base->ctx_priv = ctx;
  7098. /* initializing current state for error logging */
  7099. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  7100. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  7101. CAM_ISP_CTX_ACTIVATED_MAX;
  7102. }
  7103. atomic64_set(&ctx->state_monitor_head, -1);
  7104. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  7105. atomic64_set(&ctx->event_record_head[i], -1);
  7106. if (!isp_ctx_debug.dentry)
  7107. cam_isp_context_debug_register();
  7108. err:
  7109. return rc;
  7110. }
  7111. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  7112. {
  7113. if (ctx->base)
  7114. cam_context_deinit(ctx->base);
  7115. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  7116. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  7117. __cam_isp_ctx_substate_val_to_type(
  7118. ctx->substate_activated));
  7119. isp_ctx_debug.dentry = NULL;
  7120. memset(ctx, 0, sizeof(*ctx));
  7121. return 0;
  7122. }