cam_isp_context.c 206 KB

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