cam_isp_context.c 221 KB

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