cam_isp_context.c 275 KB

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