cam_isp_context.c 247 KB

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