cam_isp_context.c 273 KB

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