cam_isp_context.c 247 KB

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