cam_isp_context.c 244 KB

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