cam_isp_context.c 242 KB

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