cam_isp_context.c 207 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122
  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_in_flushed_state(
  3592. struct cam_context *ctx,
  3593. struct cam_req_mgr_flush_request *flush_req)
  3594. {
  3595. CAM_INFO(CAM_ISP, "Flush (type %d) in flushed state req id %lld ctx_id:%d",
  3596. flush_req->type, flush_req->req_id, ctx->ctx_id);
  3597. if (flush_req->req_id > ctx->last_flush_req)
  3598. ctx->last_flush_req = flush_req->req_id;
  3599. return 0;
  3600. }
  3601. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3602. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3603. {
  3604. int i, rc, tmp = 0;
  3605. uint32_t cancel_req_id_found = 0;
  3606. struct cam_ctx_request *req;
  3607. struct cam_ctx_request *req_temp;
  3608. struct cam_isp_ctx_req *req_isp;
  3609. struct list_head flush_list;
  3610. struct cam_isp_context *ctx_isp = NULL;
  3611. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3612. INIT_LIST_HEAD(&flush_list);
  3613. if (list_empty(req_list)) {
  3614. CAM_DBG(CAM_ISP, "request list is empty");
  3615. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3616. CAM_INFO(CAM_ISP, "no request to cancel (last applied:%lld cancel:%lld)",
  3617. ctx_isp->last_applied_req_id, flush_req->req_id);
  3618. return -EINVAL;
  3619. } else
  3620. return 0;
  3621. }
  3622. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3623. flush_req->type, flush_req->req_id);
  3624. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3625. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3626. if (req->request_id != flush_req->req_id) {
  3627. continue;
  3628. } else {
  3629. list_del_init(&req->list);
  3630. list_add_tail(&req->list, &flush_list);
  3631. cancel_req_id_found = 1;
  3632. __cam_isp_ctx_update_state_monitor_array(
  3633. ctx_isp,
  3634. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3635. req->request_id);
  3636. break;
  3637. }
  3638. }
  3639. list_del_init(&req->list);
  3640. list_add_tail(&req->list, &flush_list);
  3641. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3642. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3643. }
  3644. if (list_empty(&flush_list)) {
  3645. /*
  3646. * Maybe the req isn't sent to KMD since UMD already skip
  3647. * req in CSL layer.
  3648. */
  3649. CAM_INFO(CAM_ISP,
  3650. "flush list is empty, flush type %d for req %llu",
  3651. flush_req->type, flush_req->req_id);
  3652. return 0;
  3653. }
  3654. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3655. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3656. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3657. if (req_isp->fence_map_out[i].sync_id != -1) {
  3658. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3659. req->request_id,
  3660. req_isp->fence_map_out[i].sync_id);
  3661. rc = cam_sync_signal(
  3662. req_isp->fence_map_out[i].sync_id,
  3663. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3664. CAM_SYNC_ISP_EVENT_FLUSH);
  3665. if (rc) {
  3666. tmp = req_isp->fence_map_out[i].sync_id;
  3667. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3668. "signal fence %d failed", tmp);
  3669. }
  3670. req_isp->fence_map_out[i].sync_id = -1;
  3671. }
  3672. }
  3673. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  3674. req_isp->cdm_reset_before_apply = false;
  3675. list_del_init(&req->list);
  3676. list_add_tail(&req->list, &ctx->free_req_list);
  3677. }
  3678. return 0;
  3679. }
  3680. static int __cam_isp_ctx_flush_req_in_top_state(
  3681. struct cam_context *ctx,
  3682. struct cam_req_mgr_flush_request *flush_req)
  3683. {
  3684. int rc = 0;
  3685. struct cam_isp_context *ctx_isp;
  3686. struct cam_isp_stop_args stop_isp;
  3687. struct cam_hw_stop_args stop_args;
  3688. struct cam_hw_reset_args reset_args;
  3689. struct cam_req_mgr_timer_notify timer;
  3690. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3691. CAM_DBG(CAM_ISP, "Flush pending list");
  3692. spin_lock_bh(&ctx->lock);
  3693. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3694. spin_unlock_bh(&ctx->lock);
  3695. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3696. if (ctx->state <= CAM_CTX_READY) {
  3697. ctx->state = CAM_CTX_ACQUIRED;
  3698. goto end;
  3699. }
  3700. spin_lock_bh(&ctx->lock);
  3701. ctx->state = CAM_CTX_FLUSHED;
  3702. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3703. spin_unlock_bh(&ctx->lock);
  3704. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3705. flush_req->req_id, ctx->ctx_id);
  3706. ctx->last_flush_req = flush_req->req_id;
  3707. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  3708. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3709. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3710. stop_isp.stop_only = true;
  3711. stop_isp.is_internal_stop = false;
  3712. stop_args.args = (void *)&stop_isp;
  3713. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3714. &stop_args);
  3715. if (rc)
  3716. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3717. rc);
  3718. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3719. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3720. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3721. timer.link_hdl = ctx->link_hdl;
  3722. timer.dev_hdl = ctx->dev_hdl;
  3723. timer.state = false;
  3724. ctx->ctx_crm_intf->notify_timer(&timer);
  3725. }
  3726. spin_lock_bh(&ctx->lock);
  3727. if (!list_empty(&ctx->wait_req_list))
  3728. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3729. flush_req);
  3730. if (!list_empty(&ctx->active_req_list))
  3731. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3732. flush_req);
  3733. ctx_isp->active_req_cnt = 0;
  3734. spin_unlock_bh(&ctx->lock);
  3735. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3736. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3737. &reset_args);
  3738. if (rc)
  3739. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3740. ctx_isp->init_received = false;
  3741. }
  3742. end:
  3743. ctx_isp->bubble_frame_cnt = 0;
  3744. atomic_set(&ctx_isp->process_bubble, 0);
  3745. atomic_set(&ctx_isp->rxd_epoch, 0);
  3746. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3747. return rc;
  3748. }
  3749. static int __cam_isp_ctx_flush_req_in_ready(
  3750. struct cam_context *ctx,
  3751. struct cam_req_mgr_flush_request *flush_req)
  3752. {
  3753. int rc = 0;
  3754. CAM_DBG(CAM_ISP, "try to flush pending list");
  3755. spin_lock_bh(&ctx->lock);
  3756. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3757. /* if nothing is in pending req list, change state to acquire */
  3758. if (list_empty(&ctx->pending_req_list))
  3759. ctx->state = CAM_CTX_ACQUIRED;
  3760. spin_unlock_bh(&ctx->lock);
  3761. trace_cam_context_state("ISP", ctx);
  3762. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3763. ctx->state);
  3764. return rc;
  3765. }
  3766. static struct cam_ctx_ops
  3767. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3768. /* SOF */
  3769. {
  3770. .ioctl_ops = {},
  3771. .crm_ops = {
  3772. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3773. .notify_frame_skip =
  3774. __cam_isp_ctx_apply_default_req_settings,
  3775. },
  3776. .irq_ops = NULL,
  3777. },
  3778. /* APPLIED */
  3779. {
  3780. .ioctl_ops = {},
  3781. .crm_ops = {},
  3782. .irq_ops = NULL,
  3783. },
  3784. /* EPOCH */
  3785. {
  3786. .ioctl_ops = {},
  3787. .crm_ops = {
  3788. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3789. .notify_frame_skip =
  3790. __cam_isp_ctx_apply_default_req_settings,
  3791. },
  3792. .irq_ops = NULL,
  3793. },
  3794. /* BUBBLE */
  3795. {
  3796. .ioctl_ops = {},
  3797. .crm_ops = {
  3798. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3799. .notify_frame_skip =
  3800. __cam_isp_ctx_apply_default_req_settings,
  3801. },
  3802. .irq_ops = NULL,
  3803. },
  3804. /* Bubble Applied */
  3805. {
  3806. .ioctl_ops = {},
  3807. .crm_ops = {},
  3808. .irq_ops = NULL,
  3809. },
  3810. /* HW ERROR */
  3811. {
  3812. .ioctl_ops = {},
  3813. .crm_ops = {},
  3814. .irq_ops = NULL,
  3815. },
  3816. /* HALT */
  3817. {
  3818. .ioctl_ops = {},
  3819. .crm_ops = {},
  3820. .irq_ops = NULL,
  3821. },
  3822. };
  3823. static struct cam_ctx_ops
  3824. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3825. /* SOF */
  3826. {
  3827. .ioctl_ops = {},
  3828. .crm_ops = {
  3829. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3830. },
  3831. .irq_ops = NULL,
  3832. },
  3833. /* APPLIED */
  3834. {
  3835. .ioctl_ops = {},
  3836. .crm_ops = {},
  3837. .irq_ops = NULL,
  3838. },
  3839. /* EPOCH */
  3840. {
  3841. .ioctl_ops = {},
  3842. .crm_ops = {
  3843. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3844. },
  3845. .irq_ops = NULL,
  3846. },
  3847. /* BUBBLE */
  3848. {
  3849. .ioctl_ops = {},
  3850. .crm_ops = {
  3851. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3852. },
  3853. .irq_ops = NULL,
  3854. },
  3855. /* Bubble Applied */
  3856. {
  3857. .ioctl_ops = {},
  3858. .crm_ops = {},
  3859. .irq_ops = NULL,
  3860. },
  3861. /* HW ERROR */
  3862. {
  3863. .ioctl_ops = {},
  3864. .crm_ops = {},
  3865. .irq_ops = NULL,
  3866. },
  3867. /* HALT */
  3868. {
  3869. .ioctl_ops = {},
  3870. .crm_ops = {},
  3871. .irq_ops = NULL,
  3872. },
  3873. };
  3874. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3875. struct cam_isp_context *ctx_isp, void *evt_data)
  3876. {
  3877. int rc = 0;
  3878. struct cam_context *ctx = ctx_isp->base;
  3879. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3880. uint64_t request_id = 0;
  3881. if (!evt_data) {
  3882. CAM_ERR(CAM_ISP, "in valid sof event data");
  3883. return -EINVAL;
  3884. }
  3885. ctx_isp->frame_id++;
  3886. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3887. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3888. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3889. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3890. /*
  3891. * notify reqmgr with sof signal. Note, due to scheduling delay
  3892. * we can run into situation that two active requests has already
  3893. * be in the active queue while we try to do the notification.
  3894. * In this case, we need to skip the current notification. This
  3895. * helps the state machine to catch up the delay.
  3896. */
  3897. if (ctx_isp->active_req_cnt <= 2) {
  3898. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3899. /*
  3900. * It's possible for rup done to be processed before
  3901. * SOF, check for first active request shutter here
  3902. */
  3903. if (!list_empty(&ctx->active_req_list)) {
  3904. struct cam_ctx_request *req = NULL;
  3905. req = list_first_entry(&ctx->active_req_list,
  3906. struct cam_ctx_request, list);
  3907. if (req->request_id > ctx_isp->reported_req_id) {
  3908. request_id = req->request_id;
  3909. ctx_isp->reported_req_id = request_id;
  3910. }
  3911. }
  3912. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3913. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3914. } else {
  3915. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3916. }
  3917. if (list_empty(&ctx->active_req_list))
  3918. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3919. else
  3920. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3921. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3922. __cam_isp_ctx_substate_val_to_type(
  3923. ctx_isp->substate_activated));
  3924. return rc;
  3925. }
  3926. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3927. struct cam_isp_context *ctx_isp, void *evt_data)
  3928. {
  3929. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3930. if (!evt_data) {
  3931. CAM_ERR(CAM_ISP, "in valid sof event data");
  3932. return -EINVAL;
  3933. }
  3934. ctx_isp->frame_id++;
  3935. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3936. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3937. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3938. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3939. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3940. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3941. __cam_isp_ctx_substate_val_to_type(
  3942. ctx_isp->substate_activated));
  3943. return 0;
  3944. }
  3945. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3946. struct cam_isp_context *ctx_isp, void *evt_data)
  3947. {
  3948. struct cam_ctx_request *req;
  3949. struct cam_isp_ctx_req *req_isp;
  3950. struct cam_context *ctx = ctx_isp->base;
  3951. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3952. uint64_t request_id = 0;
  3953. /*
  3954. * Sof in bubble applied state means, reg update not received.
  3955. * before increment frame id and override time stamp value, send
  3956. * the previous sof time stamp that got captured in the
  3957. * sof in applied state.
  3958. */
  3959. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3960. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3961. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3962. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3963. ctx_isp->frame_id++;
  3964. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3965. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3966. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3967. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3968. if (list_empty(&ctx->wait_req_list)) {
  3969. /*
  3970. * If no pending req in epoch, this is an error case.
  3971. * The recovery is to go back to sof state
  3972. */
  3973. CAM_ERR(CAM_ISP, "No wait request");
  3974. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3975. /* Send SOF event as empty frame*/
  3976. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3977. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3978. goto end;
  3979. }
  3980. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  3981. list);
  3982. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3983. req_isp->bubble_detected = true;
  3984. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  3985. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  3986. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  3987. req_isp->cdm_reset_before_apply = false;
  3988. if (req_isp->bubble_report) {
  3989. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  3990. req->request_id, ctx_isp);
  3991. atomic_set(&ctx_isp->process_bubble, 1);
  3992. } else {
  3993. req_isp->bubble_report = 0;
  3994. }
  3995. /*
  3996. * Always move the request to active list. Let buf done
  3997. * function handles the rest.
  3998. */
  3999. list_del_init(&req->list);
  4000. list_add_tail(&req->list, &ctx->active_req_list);
  4001. ctx_isp->active_req_cnt++;
  4002. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  4003. req->request_id, ctx_isp->active_req_cnt);
  4004. if (!req_isp->bubble_report) {
  4005. if (req->request_id > ctx_isp->reported_req_id) {
  4006. request_id = req->request_id;
  4007. ctx_isp->reported_req_id = request_id;
  4008. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4009. CAM_REQ_MGR_SOF_EVENT_ERROR);
  4010. } else
  4011. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4012. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4013. } else
  4014. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4015. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4016. /* change the state to bubble, as reg update has not come */
  4017. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  4018. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4019. __cam_isp_ctx_substate_val_to_type(
  4020. ctx_isp->substate_activated));
  4021. end:
  4022. return 0;
  4023. }
  4024. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  4025. struct cam_isp_context *ctx_isp, void *evt_data)
  4026. {
  4027. uint32_t i;
  4028. struct cam_ctx_request *req;
  4029. struct cam_context *ctx = ctx_isp->base;
  4030. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4031. struct cam_isp_ctx_req *req_isp;
  4032. struct cam_hw_cmd_args hw_cmd_args;
  4033. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4034. uint64_t request_id = 0;
  4035. uint64_t last_cdm_done_req = 0;
  4036. int rc = 0;
  4037. if (!evt_data) {
  4038. CAM_ERR(CAM_ISP, "in valid sof event data");
  4039. return -EINVAL;
  4040. }
  4041. ctx_isp->frame_id++;
  4042. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  4043. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  4044. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4045. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4046. if (atomic_read(&ctx_isp->process_bubble)) {
  4047. if (list_empty(&ctx->active_req_list)) {
  4048. CAM_ERR(CAM_ISP, "No available active req in bubble");
  4049. atomic_set(&ctx_isp->process_bubble, 0);
  4050. return -EINVAL;
  4051. }
  4052. if (ctx_isp->last_sof_timestamp ==
  4053. ctx_isp->sof_timestamp_val) {
  4054. CAM_DBG(CAM_ISP,
  4055. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %d",
  4056. ctx_isp->frame_id,
  4057. ctx_isp->sof_timestamp_val,
  4058. ctx->ctx_id);
  4059. goto end;
  4060. }
  4061. req = list_first_entry(&ctx->active_req_list,
  4062. struct cam_ctx_request, list);
  4063. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4064. if (req_isp->bubble_detected) {
  4065. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4066. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4067. isp_hw_cmd_args.cmd_type =
  4068. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  4069. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4070. rc = ctx->hw_mgr_intf->hw_cmd(
  4071. ctx->hw_mgr_intf->hw_mgr_priv,
  4072. &hw_cmd_args);
  4073. if (rc) {
  4074. CAM_ERR(CAM_ISP, "HW command failed");
  4075. return rc;
  4076. }
  4077. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  4078. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %d",
  4079. last_cdm_done_req, ctx->ctx_id);
  4080. if (last_cdm_done_req >= req->request_id) {
  4081. CAM_DBG(CAM_ISP,
  4082. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  4083. req->request_id);
  4084. goto end;
  4085. } else {
  4086. CAM_WARN(CAM_ISP,
  4087. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  4088. req->request_id);
  4089. req_isp->num_acked = 0;
  4090. req_isp->num_deferred_acks = 0;
  4091. req_isp->bubble_detected = false;
  4092. req_isp->cdm_reset_before_apply = true;
  4093. list_del_init(&req->list);
  4094. list_add(&req->list, &ctx->pending_req_list);
  4095. atomic_set(&ctx_isp->process_bubble, 0);
  4096. ctx_isp->active_req_cnt--;
  4097. CAM_DBG(CAM_REQ,
  4098. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  4099. req->request_id,
  4100. ctx_isp->active_req_cnt, ctx->ctx_id);
  4101. }
  4102. goto end;
  4103. }
  4104. }
  4105. /*
  4106. * Signal all active requests with error and move the all the active
  4107. * requests to free list
  4108. */
  4109. while (!list_empty(&ctx->active_req_list)) {
  4110. req = list_first_entry(&ctx->active_req_list,
  4111. struct cam_ctx_request, list);
  4112. list_del_init(&req->list);
  4113. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4114. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4115. req_isp->num_fence_map_out);
  4116. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4117. if (req_isp->fence_map_out[i].sync_id != -1) {
  4118. cam_sync_signal(
  4119. req_isp->fence_map_out[i].sync_id,
  4120. CAM_SYNC_STATE_SIGNALED_ERROR,
  4121. CAM_SYNC_ISP_EVENT_BUBBLE);
  4122. }
  4123. list_add_tail(&req->list, &ctx->free_req_list);
  4124. ctx_isp->active_req_cnt--;
  4125. }
  4126. end:
  4127. /* notify reqmgr with sof signal */
  4128. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4129. /*
  4130. * It is idle frame with out any applied request id, send
  4131. * request id as zero
  4132. */
  4133. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4134. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4135. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4136. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4137. __cam_isp_ctx_substate_val_to_type(
  4138. ctx_isp->substate_activated));
  4139. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  4140. return 0;
  4141. }
  4142. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_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. req = list_first_entry(&ctx->active_req_list,
  4148. struct cam_ctx_request, list);
  4149. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  4150. return 0;
  4151. }
  4152. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  4153. struct cam_isp_context *ctx_isp, void *evt_data)
  4154. {
  4155. struct cam_ctx_request *req = NULL;
  4156. struct cam_context *ctx = ctx_isp->base;
  4157. struct cam_isp_ctx_req *req_isp;
  4158. uint64_t request_id = 0;
  4159. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  4160. /* notify reqmgr with sof signal*/
  4161. if (list_empty(&ctx->wait_req_list)) {
  4162. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  4163. goto error;
  4164. }
  4165. req = list_first_entry(&ctx->wait_req_list,
  4166. struct cam_ctx_request, list);
  4167. list_del_init(&req->list);
  4168. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4169. request_id =
  4170. (req_isp->hw_update_data.packet_opcode_type ==
  4171. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  4172. if (req_isp->num_fence_map_out != 0) {
  4173. list_add_tail(&req->list, &ctx->active_req_list);
  4174. ctx_isp->active_req_cnt++;
  4175. CAM_DBG(CAM_ISP,
  4176. "move request %lld to active list(cnt = %d)",
  4177. req->request_id, ctx_isp->active_req_cnt);
  4178. /* if packet has buffers, set correct request id */
  4179. request_id = req->request_id;
  4180. } else {
  4181. /* no io config, so the request is completed. */
  4182. list_add_tail(&req->list, &ctx->free_req_list);
  4183. CAM_DBG(CAM_ISP,
  4184. "move active req %lld to free list(cnt=%d)",
  4185. req->request_id, ctx_isp->active_req_cnt);
  4186. }
  4187. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4188. if (request_id)
  4189. ctx_isp->reported_req_id = request_id;
  4190. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4191. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4192. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4193. __cam_isp_ctx_substate_val_to_type(
  4194. ctx_isp->substate_activated));
  4195. __cam_isp_ctx_update_event_record(ctx_isp,
  4196. CAM_ISP_CTX_EVENT_RUP, req);
  4197. return 0;
  4198. error:
  4199. /* Send SOF event as idle frame*/
  4200. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4201. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4202. __cam_isp_ctx_update_event_record(ctx_isp,
  4203. CAM_ISP_CTX_EVENT_RUP, NULL);
  4204. /*
  4205. * There is no request in the pending list, move the sub state machine
  4206. * to SOF sub state
  4207. */
  4208. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4209. return 0;
  4210. }
  4211. static struct cam_isp_ctx_irq_ops
  4212. cam_isp_ctx_rdi_only_activated_state_machine_irq
  4213. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4214. /* SOF */
  4215. {
  4216. .irq_ops = {
  4217. NULL,
  4218. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4219. __cam_isp_ctx_reg_upd_in_sof,
  4220. NULL,
  4221. NULL,
  4222. NULL,
  4223. },
  4224. },
  4225. /* APPLIED */
  4226. {
  4227. .irq_ops = {
  4228. __cam_isp_ctx_handle_error,
  4229. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  4230. __cam_isp_ctx_reg_upd_in_applied_state,
  4231. NULL,
  4232. NULL,
  4233. __cam_isp_ctx_buf_done_in_applied,
  4234. },
  4235. },
  4236. /* EPOCH */
  4237. {
  4238. .irq_ops = {
  4239. __cam_isp_ctx_handle_error,
  4240. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4241. NULL,
  4242. NULL,
  4243. NULL,
  4244. __cam_isp_ctx_buf_done_in_epoch,
  4245. },
  4246. },
  4247. /* BUBBLE*/
  4248. {
  4249. .irq_ops = {
  4250. __cam_isp_ctx_handle_error,
  4251. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  4252. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  4253. NULL,
  4254. NULL,
  4255. __cam_isp_ctx_buf_done_in_bubble,
  4256. },
  4257. },
  4258. /* BUBBLE APPLIED ie PRE_BUBBLE */
  4259. {
  4260. .irq_ops = {
  4261. __cam_isp_ctx_handle_error,
  4262. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  4263. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  4264. NULL,
  4265. NULL,
  4266. __cam_isp_ctx_buf_done_in_bubble_applied,
  4267. },
  4268. },
  4269. /* HW ERROR */
  4270. {
  4271. },
  4272. /* HALT */
  4273. {
  4274. },
  4275. };
  4276. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  4277. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4278. {
  4279. int rc = 0;
  4280. struct cam_isp_context *ctx_isp =
  4281. (struct cam_isp_context *) ctx->ctx_priv;
  4282. CAM_DBG(CAM_ISP, "current Substate[%s]",
  4283. __cam_isp_ctx_substate_val_to_type(
  4284. ctx_isp->substate_activated));
  4285. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4286. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4287. CAM_DBG(CAM_ISP, "new Substate[%s]",
  4288. __cam_isp_ctx_substate_val_to_type(
  4289. ctx_isp->substate_activated));
  4290. if (rc)
  4291. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4292. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  4293. ctx->ctx_id,
  4294. __cam_isp_ctx_substate_val_to_type(
  4295. ctx_isp->substate_activated), rc);
  4296. return rc;
  4297. }
  4298. static struct cam_ctx_ops
  4299. cam_isp_ctx_rdi_only_activated_state_machine
  4300. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4301. /* SOF */
  4302. {
  4303. .ioctl_ops = {},
  4304. .crm_ops = {
  4305. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4306. },
  4307. .irq_ops = NULL,
  4308. },
  4309. /* APPLIED */
  4310. {
  4311. .ioctl_ops = {},
  4312. .crm_ops = {},
  4313. .irq_ops = NULL,
  4314. },
  4315. /* EPOCH */
  4316. {
  4317. .ioctl_ops = {},
  4318. .crm_ops = {
  4319. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4320. },
  4321. .irq_ops = NULL,
  4322. },
  4323. /* PRE BUBBLE */
  4324. {
  4325. .ioctl_ops = {},
  4326. .crm_ops = {},
  4327. .irq_ops = NULL,
  4328. },
  4329. /* BUBBLE */
  4330. {
  4331. .ioctl_ops = {},
  4332. .crm_ops = {},
  4333. .irq_ops = NULL,
  4334. },
  4335. /* HW ERROR */
  4336. {
  4337. .ioctl_ops = {},
  4338. .crm_ops = {},
  4339. .irq_ops = NULL,
  4340. },
  4341. /* HALT */
  4342. {
  4343. .ioctl_ops = {},
  4344. .crm_ops = {},
  4345. .irq_ops = NULL,
  4346. },
  4347. };
  4348. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  4349. struct cam_flush_dev_cmd *cmd)
  4350. {
  4351. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4352. struct cam_req_mgr_flush_request flush_req;
  4353. if (!ctx_isp->offline_context) {
  4354. CAM_ERR(CAM_ISP, "flush dev only supported in offline context");
  4355. return -EINVAL;
  4356. }
  4357. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  4358. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  4359. flush_req.req_id = cmd->req_id;
  4360. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu)", flush_req.type, flush_req.req_id);
  4361. switch (ctx->state) {
  4362. case CAM_CTX_ACQUIRED:
  4363. case CAM_CTX_ACTIVATED:
  4364. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  4365. case CAM_CTX_READY:
  4366. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  4367. default:
  4368. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d", ctx->state);
  4369. return -EINVAL;
  4370. }
  4371. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  4372. cam_req_mgr_workq_flush(ctx_isp->workq);
  4373. }
  4374. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  4375. {
  4376. int i;
  4377. if (ctx->out_map_entries) {
  4378. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4379. kfree(ctx->out_map_entries[i]);
  4380. ctx->out_map_entries[i] = NULL;
  4381. }
  4382. kfree(ctx->out_map_entries);
  4383. ctx->out_map_entries = NULL;
  4384. }
  4385. if (ctx->in_map_entries) {
  4386. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4387. kfree(ctx->in_map_entries[i]);
  4388. ctx->in_map_entries[i] = NULL;
  4389. }
  4390. kfree(ctx->in_map_entries);
  4391. ctx->in_map_entries = NULL;
  4392. }
  4393. if (ctx->hw_update_entry) {
  4394. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4395. kfree(ctx->hw_update_entry[i]);
  4396. ctx->hw_update_entry[i] = NULL;
  4397. }
  4398. kfree(ctx->hw_update_entry);
  4399. ctx->hw_update_entry = NULL;
  4400. }
  4401. ctx->max_out_map_entries = 0;
  4402. ctx->max_in_map_entries = 0;
  4403. ctx->max_hw_update_entries = 0;
  4404. }
  4405. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  4406. void *cmd)
  4407. {
  4408. int rc = 0;
  4409. struct cam_hw_release_args rel_arg;
  4410. struct cam_isp_context *ctx_isp =
  4411. (struct cam_isp_context *) ctx->ctx_priv;
  4412. struct cam_req_mgr_flush_request flush_req;
  4413. int i;
  4414. if (ctx_isp->hw_ctx) {
  4415. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4416. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4417. &rel_arg);
  4418. ctx_isp->hw_ctx = NULL;
  4419. } else {
  4420. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  4421. }
  4422. ctx->last_flush_req = 0;
  4423. ctx_isp->custom_enabled = false;
  4424. ctx_isp->use_frame_header_ts = false;
  4425. ctx_isp->use_default_apply = false;
  4426. ctx_isp->frame_id = 0;
  4427. ctx_isp->active_req_cnt = 0;
  4428. ctx_isp->reported_req_id = 0;
  4429. ctx_isp->hw_acquired = false;
  4430. ctx_isp->init_received = false;
  4431. ctx_isp->support_consumed_addr = false;
  4432. ctx_isp->aeb_enabled = false;
  4433. ctx_isp->do_internal_recovery = false;
  4434. ctx_isp->req_info.last_bufdone_req_id = 0;
  4435. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4436. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4437. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4438. /*
  4439. * Ideally, we should never have any active request here.
  4440. * But we still add some sanity check code here to help the debug
  4441. */
  4442. if (!list_empty(&ctx->active_req_list))
  4443. CAM_WARN(CAM_ISP, "Active list is not empty");
  4444. /* Flush all the pending request list */
  4445. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4446. flush_req.link_hdl = ctx->link_hdl;
  4447. flush_req.dev_hdl = ctx->dev_hdl;
  4448. flush_req.req_id = 0;
  4449. CAM_DBG(CAM_ISP, "try to flush pending list");
  4450. spin_lock_bh(&ctx->lock);
  4451. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4452. spin_unlock_bh(&ctx->lock);
  4453. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4454. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  4455. ctx->state = CAM_CTX_ACQUIRED;
  4456. trace_cam_context_state("ISP", ctx);
  4457. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4458. ctx->ctx_id, ctx->state);
  4459. return rc;
  4460. }
  4461. /* top level state machine */
  4462. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  4463. struct cam_release_dev_cmd *cmd)
  4464. {
  4465. int rc = 0;
  4466. int i;
  4467. struct cam_hw_release_args rel_arg;
  4468. struct cam_isp_context *ctx_isp =
  4469. (struct cam_isp_context *) ctx->ctx_priv;
  4470. struct cam_req_mgr_flush_request flush_req;
  4471. if (cmd && ctx_isp->hw_ctx) {
  4472. CAM_ERR(CAM_ISP, "releasing hw");
  4473. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  4474. }
  4475. if (ctx_isp->hw_ctx) {
  4476. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4477. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4478. &rel_arg);
  4479. ctx_isp->hw_ctx = NULL;
  4480. }
  4481. ctx->session_hdl = -1;
  4482. ctx->dev_hdl = -1;
  4483. ctx->link_hdl = -1;
  4484. ctx->ctx_crm_intf = NULL;
  4485. ctx->last_flush_req = 0;
  4486. ctx_isp->frame_id = 0;
  4487. ctx_isp->active_req_cnt = 0;
  4488. ctx_isp->reported_req_id = 0;
  4489. ctx_isp->hw_acquired = false;
  4490. ctx_isp->init_received = false;
  4491. ctx_isp->offline_context = false;
  4492. ctx_isp->rdi_only_context = false;
  4493. ctx_isp->req_info.last_bufdone_req_id = 0;
  4494. ctx_isp->v4l2_event_sub_ids = 0;
  4495. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4496. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4497. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4498. /*
  4499. * Ideally, we should never have any active request here.
  4500. * But we still add some sanity check code here to help the debug
  4501. */
  4502. if (!list_empty(&ctx->active_req_list))
  4503. CAM_ERR(CAM_ISP, "Active list is not empty");
  4504. /* Flush all the pending request list */
  4505. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4506. flush_req.link_hdl = ctx->link_hdl;
  4507. flush_req.dev_hdl = ctx->dev_hdl;
  4508. flush_req.req_id = 0;
  4509. CAM_DBG(CAM_ISP, "try to flush pending list");
  4510. spin_lock_bh(&ctx->lock);
  4511. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4512. spin_unlock_bh(&ctx->lock);
  4513. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4514. ctx->state = CAM_CTX_AVAILABLE;
  4515. trace_cam_context_state("ISP", ctx);
  4516. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4517. ctx->ctx_id, ctx->state);
  4518. return rc;
  4519. }
  4520. static int __cam_isp_ctx_config_dev_in_top_state(
  4521. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  4522. {
  4523. int rc = 0, i;
  4524. struct cam_ctx_request *req = NULL;
  4525. struct cam_isp_ctx_req *req_isp;
  4526. struct cam_packet *packet;
  4527. size_t remain_len = 0;
  4528. struct cam_hw_prepare_update_args cfg = {0};
  4529. struct cam_req_mgr_add_request add_req;
  4530. struct cam_isp_context *ctx_isp =
  4531. (struct cam_isp_context *) ctx->ctx_priv;
  4532. struct cam_hw_cmd_args hw_cmd_args;
  4533. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4534. uint32_t packet_opcode = 0;
  4535. CAM_DBG(CAM_ISP, "get free request object......");
  4536. /* get free request */
  4537. spin_lock_bh(&ctx->lock);
  4538. if (!list_empty(&ctx->free_req_list)) {
  4539. req = list_first_entry(&ctx->free_req_list,
  4540. struct cam_ctx_request, list);
  4541. list_del_init(&req->list);
  4542. }
  4543. spin_unlock_bh(&ctx->lock);
  4544. if (!req) {
  4545. CAM_ERR(CAM_ISP, "No more request obj free");
  4546. return -ENOMEM;
  4547. }
  4548. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4549. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  4550. if (IS_ERR(packet)) {
  4551. rc = PTR_ERR(packet);
  4552. goto free_req;
  4553. }
  4554. /* Query the packet opcode */
  4555. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4556. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4557. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  4558. isp_hw_cmd_args.cmd_data = (void *)packet;
  4559. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4560. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4561. &hw_cmd_args);
  4562. if (rc) {
  4563. CAM_ERR(CAM_ISP, "HW command failed");
  4564. goto free_req;
  4565. }
  4566. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  4567. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  4568. && (packet->header.request_id <= ctx->last_flush_req)) {
  4569. CAM_INFO(CAM_ISP,
  4570. "request %lld has been flushed, reject packet",
  4571. packet->header.request_id);
  4572. rc = -EBADR;
  4573. goto free_req;
  4574. } else if ((packet_opcode == CAM_ISP_PACKET_INIT_DEV)
  4575. && (packet->header.request_id <= ctx->last_flush_req)
  4576. && ctx->last_flush_req && packet->header.request_id) {
  4577. CAM_WARN(CAM_ISP,
  4578. "last flushed req is %lld, config dev(init) for req %lld",
  4579. ctx->last_flush_req, packet->header.request_id);
  4580. rc = -EBADR;
  4581. goto free_req;
  4582. }
  4583. cfg.packet = packet;
  4584. cfg.remain_len = remain_len;
  4585. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4586. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  4587. cfg.hw_update_entries = req_isp->cfg;
  4588. cfg.max_out_map_entries = ctx->max_out_map_entries;
  4589. cfg.max_in_map_entries = ctx->max_in_map_entries;
  4590. cfg.out_map_entries = req_isp->fence_map_out;
  4591. cfg.in_map_entries = req_isp->fence_map_in;
  4592. cfg.priv = &req_isp->hw_update_data;
  4593. cfg.pf_data = &(req->pf_data);
  4594. cfg.num_out_map_entries = 0;
  4595. cfg.num_in_map_entries = 0;
  4596. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  4597. rc = ctx->hw_mgr_intf->hw_prepare_update(
  4598. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4599. if (rc != 0) {
  4600. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4601. rc = -EFAULT;
  4602. goto free_req;
  4603. }
  4604. req_isp->num_cfg = cfg.num_hw_update_entries;
  4605. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4606. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4607. req_isp->num_acked = 0;
  4608. req_isp->num_deferred_acks = 0;
  4609. req_isp->bubble_detected = false;
  4610. req_isp->cdm_reset_before_apply = false;
  4611. req_isp->hw_update_data.packet = packet;
  4612. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4613. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4614. if (rc) {
  4615. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4616. req_isp->fence_map_out[i].sync_id);
  4617. goto put_ref;
  4618. }
  4619. }
  4620. CAM_DBG(CAM_ISP,
  4621. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d",
  4622. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  4623. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in);
  4624. req->request_id = packet->header.request_id;
  4625. req->status = 1;
  4626. if (req_isp->hw_update_data.packet_opcode_type ==
  4627. CAM_ISP_PACKET_INIT_DEV) {
  4628. if (ctx->state < CAM_CTX_ACTIVATED) {
  4629. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4630. if (rc)
  4631. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4632. ctx_isp->init_received = true;
  4633. } else {
  4634. rc = -EINVAL;
  4635. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4636. ctx->state);
  4637. }
  4638. } else {
  4639. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  4640. rc = -EINVAL;
  4641. CAM_ERR(CAM_ISP, "Received update req %lld in wrong state:%d",
  4642. req->request_id, ctx->state);
  4643. goto put_ref;
  4644. }
  4645. if (ctx_isp->offline_context) {
  4646. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4647. } else if (ctx->ctx_crm_intf->add_req) {
  4648. memset(&add_req, 0, sizeof(add_req));
  4649. add_req.link_hdl = ctx->link_hdl;
  4650. add_req.dev_hdl = ctx->dev_hdl;
  4651. add_req.req_id = req->request_id;
  4652. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4653. if (rc) {
  4654. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4655. req->request_id);
  4656. } else {
  4657. __cam_isp_ctx_enqueue_request_in_order(
  4658. ctx, req);
  4659. }
  4660. } else {
  4661. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu", req->request_id);
  4662. rc = -ENODEV;
  4663. }
  4664. }
  4665. if (rc)
  4666. goto put_ref;
  4667. CAM_DBG(CAM_REQ,
  4668. "Preprocessing Config req_id %lld successful on ctx %u",
  4669. req->request_id, ctx->ctx_id);
  4670. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4671. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4672. }
  4673. return rc;
  4674. put_ref:
  4675. for (--i; i >= 0; i--) {
  4676. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4677. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4678. req_isp->fence_map_out[i].sync_id);
  4679. }
  4680. free_req:
  4681. spin_lock_bh(&ctx->lock);
  4682. list_add_tail(&req->list, &ctx->free_req_list);
  4683. spin_unlock_bh(&ctx->lock);
  4684. return rc;
  4685. }
  4686. static int __cam_isp_ctx_allocate_mem_hw_entries(
  4687. struct cam_context *ctx,
  4688. struct cam_hw_acquire_args *param)
  4689. {
  4690. int rc = 0, i;
  4691. uint32_t max_res = 0;
  4692. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  4693. struct cam_ctx_request *req;
  4694. struct cam_ctx_request *temp_req;
  4695. struct cam_isp_ctx_req *req_isp;
  4696. if (!param->op_params.param_list[0])
  4697. max_res = CAM_ISP_CTX_RES_MAX;
  4698. else {
  4699. max_res = param->op_params.param_list[0];
  4700. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  4701. max_res += param->op_params.param_list[1];
  4702. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  4703. }
  4704. }
  4705. ctx->max_in_map_entries = max_res;
  4706. ctx->max_out_map_entries = max_res;
  4707. ctx->max_hw_update_entries = max_hw_upd_entries;
  4708. CAM_DBG(CAM_ISP,
  4709. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  4710. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  4711. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  4712. GFP_KERNEL);
  4713. if (!ctx->hw_update_entry) {
  4714. CAM_ERR(CAM_CTXT, "%s[%d] no memory ",
  4715. ctx->dev_name, ctx->ctx_id);
  4716. return -ENOMEM;
  4717. }
  4718. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4719. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  4720. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  4721. if (!ctx->hw_update_entry[i]) {
  4722. CAM_ERR(CAM_CTXT, "%s[%d] no memory for hw_update_entry: %u",
  4723. ctx->dev_name, ctx->ctx_id, i);
  4724. return -ENOMEM;
  4725. }
  4726. }
  4727. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4728. GFP_KERNEL);
  4729. if (!ctx->in_map_entries) {
  4730. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries",
  4731. ctx->dev_name, ctx->ctx_id);
  4732. rc = -ENOMEM;
  4733. goto end;
  4734. }
  4735. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4736. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  4737. sizeof(struct cam_hw_fence_map_entry),
  4738. GFP_KERNEL);
  4739. if (!ctx->in_map_entries[i]) {
  4740. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries: %u",
  4741. ctx->dev_name, ctx->ctx_id, i);
  4742. rc = -ENOMEM;
  4743. goto end;
  4744. }
  4745. }
  4746. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4747. GFP_KERNEL);
  4748. if (!ctx->out_map_entries) {
  4749. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries",
  4750. ctx->dev_name, ctx->ctx_id);
  4751. rc = -ENOMEM;
  4752. goto end;
  4753. }
  4754. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4755. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  4756. sizeof(struct cam_hw_fence_map_entry),
  4757. GFP_KERNEL);
  4758. if (!ctx->out_map_entries[i]) {
  4759. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries: %u",
  4760. ctx->dev_name, ctx->ctx_id, i);
  4761. rc = -ENOMEM;
  4762. goto end;
  4763. }
  4764. }
  4765. list_for_each_entry_safe(req, temp_req,
  4766. &ctx->free_req_list, list) {
  4767. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4768. req_isp->cfg = ctx->hw_update_entry[req->index];
  4769. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  4770. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  4771. }
  4772. return rc;
  4773. end:
  4774. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4775. return rc;
  4776. }
  4777. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4778. struct cam_acquire_dev_cmd *cmd)
  4779. {
  4780. int rc = 0;
  4781. int i;
  4782. struct cam_hw_acquire_args param;
  4783. struct cam_isp_resource *isp_res = NULL;
  4784. struct cam_create_dev_hdl req_hdl_param;
  4785. struct cam_hw_release_args release;
  4786. struct cam_isp_context *ctx_isp =
  4787. (struct cam_isp_context *) ctx->ctx_priv;
  4788. struct cam_hw_cmd_args hw_cmd_args;
  4789. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4790. if (!ctx->hw_mgr_intf) {
  4791. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4792. rc = -EFAULT;
  4793. goto end;
  4794. }
  4795. CAM_DBG(CAM_ISP,
  4796. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4797. cmd->session_handle, cmd->num_resources,
  4798. cmd->handle_type, cmd->resource_hdl);
  4799. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  4800. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4801. ctx_isp->split_acquire = true;
  4802. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4803. goto get_dev_handle;
  4804. }
  4805. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4806. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4807. rc = -ENOMEM;
  4808. goto end;
  4809. }
  4810. /* for now we only support user pointer */
  4811. if (cmd->handle_type != 1) {
  4812. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4813. rc = -EINVAL;
  4814. goto end;
  4815. }
  4816. isp_res = kzalloc(
  4817. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4818. if (!isp_res) {
  4819. rc = -ENOMEM;
  4820. goto end;
  4821. }
  4822. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4823. cmd->num_resources);
  4824. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4825. sizeof(*isp_res)*cmd->num_resources)) {
  4826. rc = -EFAULT;
  4827. goto free_res;
  4828. }
  4829. memset(&param, 0, sizeof(param));
  4830. param.context_data = ctx;
  4831. param.event_cb = ctx->irq_cb_intf;
  4832. param.num_acq = cmd->num_resources;
  4833. param.acquire_info = (uintptr_t) isp_res;
  4834. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4835. if (rc) {
  4836. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4837. ctx->ctx_id);
  4838. goto free_res;
  4839. }
  4840. /* call HW manager to reserve the resource */
  4841. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4842. &param);
  4843. if (rc != 0) {
  4844. CAM_ERR(CAM_ISP, "Acquire device failed");
  4845. goto free_res;
  4846. }
  4847. /* Query the context has rdi only resource */
  4848. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4849. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4850. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4851. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4852. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4853. &hw_cmd_args);
  4854. if (rc) {
  4855. CAM_ERR(CAM_ISP, "HW command failed");
  4856. goto free_hw;
  4857. }
  4858. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4859. /*
  4860. * this context has rdi only resource assign rdi only
  4861. * state machine
  4862. */
  4863. CAM_DBG(CAM_ISP, "RDI only session Context");
  4864. ctx_isp->substate_machine_irq =
  4865. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4866. ctx_isp->substate_machine =
  4867. cam_isp_ctx_rdi_only_activated_state_machine;
  4868. ctx_isp->rdi_only_context = true;
  4869. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4870. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4871. ctx_isp->substate_machine_irq =
  4872. cam_isp_ctx_fs2_state_machine_irq;
  4873. ctx_isp->substate_machine =
  4874. cam_isp_ctx_fs2_state_machine;
  4875. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4876. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4877. ctx_isp->substate_machine_irq =
  4878. cam_isp_ctx_offline_state_machine_irq;
  4879. } else {
  4880. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4881. ctx_isp->substate_machine_irq =
  4882. cam_isp_ctx_activated_state_machine_irq;
  4883. ctx_isp->substate_machine =
  4884. cam_isp_ctx_activated_state_machine;
  4885. }
  4886. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4887. ctx_isp->hw_acquired = true;
  4888. ctx_isp->split_acquire = false;
  4889. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4890. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4891. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4892. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4893. kfree(isp_res);
  4894. isp_res = NULL;
  4895. get_dev_handle:
  4896. req_hdl_param.session_hdl = cmd->session_handle;
  4897. /* bridge is not ready for these flags. so false for now */
  4898. req_hdl_param.v4l2_sub_dev_flag = 0;
  4899. req_hdl_param.media_entity_flag = 0;
  4900. req_hdl_param.ops = ctx->crm_ctx_intf;
  4901. req_hdl_param.priv = ctx;
  4902. req_hdl_param.dev_id = CAM_ISP;
  4903. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4904. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4905. if (ctx->dev_hdl <= 0) {
  4906. rc = -EFAULT;
  4907. CAM_ERR(CAM_ISP, "Can not create device handle");
  4908. goto free_hw;
  4909. }
  4910. cmd->dev_handle = ctx->dev_hdl;
  4911. /* store session information */
  4912. ctx->session_hdl = cmd->session_handle;
  4913. ctx->state = CAM_CTX_ACQUIRED;
  4914. trace_cam_context_state("ISP", ctx);
  4915. CAM_DBG(CAM_ISP,
  4916. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4917. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4918. return rc;
  4919. free_hw:
  4920. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4921. if (ctx_isp->hw_acquired)
  4922. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4923. &release);
  4924. ctx_isp->hw_ctx = NULL;
  4925. ctx_isp->hw_acquired = false;
  4926. free_res:
  4927. kfree(isp_res);
  4928. end:
  4929. return rc;
  4930. }
  4931. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4932. void *args)
  4933. {
  4934. int rc = 0;
  4935. int i;
  4936. struct cam_acquire_hw_cmd_v1 *cmd =
  4937. (struct cam_acquire_hw_cmd_v1 *)args;
  4938. struct cam_hw_acquire_args param;
  4939. struct cam_hw_release_args release;
  4940. struct cam_isp_context *ctx_isp =
  4941. (struct cam_isp_context *) ctx->ctx_priv;
  4942. struct cam_hw_cmd_args hw_cmd_args;
  4943. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4944. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4945. if (!ctx->hw_mgr_intf) {
  4946. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4947. rc = -EFAULT;
  4948. goto end;
  4949. }
  4950. CAM_DBG(CAM_ISP,
  4951. "session_hdl 0x%x, hdl type %d, res %lld",
  4952. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4953. /* for now we only support user pointer */
  4954. if (cmd->handle_type != 1) {
  4955. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4956. rc = -EINVAL;
  4957. goto end;
  4958. }
  4959. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4960. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4961. goto end;
  4962. }
  4963. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4964. if (!acquire_hw_info) {
  4965. rc = -ENOMEM;
  4966. goto end;
  4967. }
  4968. CAM_DBG(CAM_ISP, "start copy resources from user");
  4969. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4970. cmd->data_size)) {
  4971. rc = -EFAULT;
  4972. goto free_res;
  4973. }
  4974. memset(&param, 0, sizeof(param));
  4975. param.context_data = ctx;
  4976. param.event_cb = ctx->irq_cb_intf;
  4977. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4978. param.acquire_info_size = cmd->data_size;
  4979. param.acquire_info = (uint64_t) acquire_hw_info;
  4980. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  4981. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  4982. &param);
  4983. if (rc) {
  4984. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4985. ctx->ctx_id);
  4986. goto free_res;
  4987. }
  4988. /* call HW manager to reserve the resource */
  4989. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4990. &param);
  4991. if (rc != 0) {
  4992. CAM_ERR(CAM_ISP, "Acquire device failed");
  4993. goto free_res;
  4994. }
  4995. ctx_isp->support_consumed_addr =
  4996. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  4997. /* Query the context has rdi only resource */
  4998. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4999. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5000. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5001. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5002. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5003. &hw_cmd_args);
  5004. if (rc) {
  5005. CAM_ERR(CAM_ISP, "HW command failed");
  5006. goto free_hw;
  5007. }
  5008. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5009. /*
  5010. * this context has rdi only resource assign rdi only
  5011. * state machine
  5012. */
  5013. CAM_DBG(CAM_ISP, "RDI only session Context");
  5014. ctx_isp->substate_machine_irq =
  5015. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5016. ctx_isp->substate_machine =
  5017. cam_isp_ctx_rdi_only_activated_state_machine;
  5018. ctx_isp->rdi_only_context = true;
  5019. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5020. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5021. ctx_isp->substate_machine_irq =
  5022. cam_isp_ctx_fs2_state_machine_irq;
  5023. ctx_isp->substate_machine =
  5024. cam_isp_ctx_fs2_state_machine;
  5025. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5026. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  5027. ctx_isp->substate_machine_irq =
  5028. cam_isp_ctx_offline_state_machine_irq;
  5029. ctx_isp->substate_machine = NULL;
  5030. } else {
  5031. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5032. ctx_isp->substate_machine_irq =
  5033. cam_isp_ctx_activated_state_machine_irq;
  5034. ctx_isp->substate_machine =
  5035. cam_isp_ctx_activated_state_machine;
  5036. }
  5037. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5038. ctx_isp->hw_acquired = true;
  5039. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5040. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5041. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5042. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5043. trace_cam_context_state("ISP", ctx);
  5044. CAM_DBG(CAM_ISP,
  5045. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5046. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5047. kfree(acquire_hw_info);
  5048. return rc;
  5049. free_hw:
  5050. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5051. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5052. ctx_isp->hw_ctx = NULL;
  5053. ctx_isp->hw_acquired = false;
  5054. free_res:
  5055. kfree(acquire_hw_info);
  5056. end:
  5057. return rc;
  5058. }
  5059. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  5060. {
  5061. cam_req_mgr_process_workq(w);
  5062. }
  5063. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  5064. void *args)
  5065. {
  5066. int rc = 0, i, j;
  5067. struct cam_acquire_hw_cmd_v2 *cmd =
  5068. (struct cam_acquire_hw_cmd_v2 *)args;
  5069. struct cam_hw_acquire_args param;
  5070. struct cam_hw_release_args release;
  5071. struct cam_isp_context *ctx_isp =
  5072. (struct cam_isp_context *) ctx->ctx_priv;
  5073. struct cam_hw_cmd_args hw_cmd_args;
  5074. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5075. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  5076. if (!ctx->hw_mgr_intf) {
  5077. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5078. rc = -EFAULT;
  5079. goto end;
  5080. }
  5081. CAM_DBG(CAM_ISP,
  5082. "session_hdl 0x%x, hdl type %d, res %lld",
  5083. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  5084. /* for now we only support user pointer */
  5085. if (cmd->handle_type != 1) {
  5086. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5087. rc = -EINVAL;
  5088. goto end;
  5089. }
  5090. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  5091. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  5092. goto end;
  5093. }
  5094. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  5095. if (!acquire_hw_info) {
  5096. rc = -ENOMEM;
  5097. goto end;
  5098. }
  5099. CAM_DBG(CAM_ISP, "start copy resources from user");
  5100. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  5101. cmd->data_size)) {
  5102. rc = -EFAULT;
  5103. goto free_res;
  5104. }
  5105. memset(&param, 0, sizeof(param));
  5106. param.context_data = ctx;
  5107. param.event_cb = ctx->irq_cb_intf;
  5108. param.num_acq = CAM_API_COMPAT_CONSTANT;
  5109. param.acquire_info_size = cmd->data_size;
  5110. param.acquire_info = (uint64_t) acquire_hw_info;
  5111. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5112. /* call HW manager to reserve the resource */
  5113. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5114. &param);
  5115. if (rc != 0) {
  5116. CAM_ERR(CAM_ISP, "Acquire device failed");
  5117. goto free_res;
  5118. }
  5119. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  5120. if (rc) {
  5121. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5122. ctx->ctx_id);
  5123. goto free_hw;
  5124. }
  5125. /*
  5126. * Set feature flag if applicable
  5127. * custom hw is supported only on v2
  5128. */
  5129. ctx_isp->custom_enabled =
  5130. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  5131. ctx_isp->use_frame_header_ts =
  5132. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5133. ctx_isp->use_default_apply =
  5134. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  5135. ctx_isp->support_consumed_addr =
  5136. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  5137. ctx_isp->aeb_enabled =
  5138. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  5139. if ((ctx_isp->aeb_enabled) && (!isp_ctx_debug.disable_internal_recovery))
  5140. ctx_isp->do_internal_recovery = true;
  5141. /* Query the context has rdi only resource */
  5142. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5143. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5144. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5145. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5146. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5147. &hw_cmd_args);
  5148. if (rc) {
  5149. CAM_ERR(CAM_ISP, "HW command failed");
  5150. goto free_hw;
  5151. }
  5152. if (param.valid_acquired_hw) {
  5153. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5154. cmd->hw_info.acquired_hw_id[i] =
  5155. param.acquired_hw_id[i];
  5156. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5157. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  5158. cmd->hw_info.acquired_hw_path[i][j] =
  5159. param.acquired_hw_path[i][j];
  5160. }
  5161. cmd->hw_info.valid_acquired_hw =
  5162. param.valid_acquired_hw;
  5163. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  5164. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5165. /*
  5166. * this context has rdi only resource assign rdi only
  5167. * state machine
  5168. */
  5169. CAM_DBG(CAM_ISP, "RDI only session Context");
  5170. ctx_isp->substate_machine_irq =
  5171. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5172. ctx_isp->substate_machine =
  5173. cam_isp_ctx_rdi_only_activated_state_machine;
  5174. ctx_isp->rdi_only_context = true;
  5175. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5176. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5177. ctx_isp->substate_machine_irq =
  5178. cam_isp_ctx_fs2_state_machine_irq;
  5179. ctx_isp->substate_machine =
  5180. cam_isp_ctx_fs2_state_machine;
  5181. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5182. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  5183. ctx_isp->substate_machine_irq =
  5184. cam_isp_ctx_offline_state_machine_irq;
  5185. ctx_isp->substate_machine = NULL;
  5186. ctx_isp->offline_context = true;
  5187. rc = cam_req_mgr_workq_create("offline_ife", 20,
  5188. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  5189. cam_req_mgr_process_workq_offline_ife_worker);
  5190. if (rc)
  5191. CAM_ERR(CAM_ISP,
  5192. "Failed to create workq for offline IFE rc:%d",
  5193. rc);
  5194. } else {
  5195. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5196. ctx_isp->substate_machine_irq =
  5197. cam_isp_ctx_activated_state_machine_irq;
  5198. ctx_isp->substate_machine =
  5199. cam_isp_ctx_activated_state_machine;
  5200. }
  5201. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5202. ctx_isp->hw_acquired = true;
  5203. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5204. trace_cam_context_state("ISP", ctx);
  5205. CAM_DBG(CAM_ISP,
  5206. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5207. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5208. kfree(acquire_hw_info);
  5209. return rc;
  5210. free_hw:
  5211. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5212. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5213. ctx_isp->hw_ctx = NULL;
  5214. ctx_isp->hw_acquired = false;
  5215. free_res:
  5216. kfree(acquire_hw_info);
  5217. end:
  5218. return rc;
  5219. }
  5220. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  5221. void *args)
  5222. {
  5223. int rc = -EINVAL;
  5224. uint32_t api_version;
  5225. if (!ctx || !args) {
  5226. CAM_ERR(CAM_ISP, "Invalid input pointer");
  5227. return rc;
  5228. }
  5229. api_version = *((uint32_t *)args);
  5230. if (api_version == 1)
  5231. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  5232. else if (api_version == 2)
  5233. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  5234. else
  5235. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  5236. return rc;
  5237. }
  5238. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  5239. struct cam_config_dev_cmd *cmd)
  5240. {
  5241. int rc = 0;
  5242. struct cam_isp_context *ctx_isp =
  5243. (struct cam_isp_context *) ctx->ctx_priv;
  5244. if (!ctx_isp->hw_acquired) {
  5245. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5246. return -EINVAL;
  5247. }
  5248. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5249. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  5250. ctx->state = CAM_CTX_READY;
  5251. trace_cam_context_state("ISP", ctx);
  5252. }
  5253. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5254. return rc;
  5255. }
  5256. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  5257. struct cam_config_dev_cmd *cmd)
  5258. {
  5259. int rc = 0;
  5260. struct cam_start_stop_dev_cmd start_cmd;
  5261. struct cam_hw_cmd_args hw_cmd_args;
  5262. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5263. struct cam_isp_context *ctx_isp =
  5264. (struct cam_isp_context *) ctx->ctx_priv;
  5265. if (!ctx_isp->hw_acquired) {
  5266. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5267. rc = -EINVAL;
  5268. goto end;
  5269. }
  5270. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5271. if (rc)
  5272. goto end;
  5273. if (!ctx_isp->init_received) {
  5274. CAM_WARN(CAM_ISP,
  5275. "Received update packet in flushed state, skip start");
  5276. goto end;
  5277. }
  5278. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5279. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5280. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5281. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5282. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5283. &hw_cmd_args);
  5284. if (rc) {
  5285. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  5286. goto end;
  5287. }
  5288. start_cmd.dev_handle = cmd->dev_handle;
  5289. start_cmd.session_handle = cmd->session_handle;
  5290. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  5291. if (rc)
  5292. CAM_ERR(CAM_ISP,
  5293. "Failed to re-start HW after flush rc: %d", rc);
  5294. else
  5295. CAM_INFO(CAM_ISP,
  5296. "Received init after flush. Re-start HW complete in ctx:%d",
  5297. ctx->ctx_id);
  5298. end:
  5299. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  5300. ctx_isp->substate_activated);
  5301. return rc;
  5302. }
  5303. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  5304. struct cam_req_mgr_core_dev_link_setup *link)
  5305. {
  5306. int rc = 0;
  5307. struct cam_isp_context *ctx_isp =
  5308. (struct cam_isp_context *) ctx->ctx_priv;
  5309. if (!link) {
  5310. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u",
  5311. link, ctx->ctx_id);
  5312. return -EINVAL;
  5313. }
  5314. if (!link->crm_cb) {
  5315. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u",
  5316. link->crm_cb, ctx->ctx_id);
  5317. return -EINVAL;
  5318. }
  5319. CAM_DBG(CAM_ISP, "Enter.........");
  5320. ctx->link_hdl = link->link_hdl;
  5321. ctx->ctx_crm_intf = link->crm_cb;
  5322. ctx_isp->subscribe_event =
  5323. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  5324. ctx_isp->trigger_id = link->trigger_id;
  5325. /* change state only if we had the init config */
  5326. if (ctx_isp->init_received) {
  5327. ctx->state = CAM_CTX_READY;
  5328. trace_cam_context_state("ISP", ctx);
  5329. }
  5330. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5331. return rc;
  5332. }
  5333. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  5334. struct cam_req_mgr_core_dev_link_setup *unlink)
  5335. {
  5336. int rc = 0;
  5337. struct cam_isp_context *ctx_isp =
  5338. (struct cam_isp_context *) ctx->ctx_priv;
  5339. ctx->link_hdl = -1;
  5340. ctx->ctx_crm_intf = NULL;
  5341. ctx_isp->trigger_id = -1;
  5342. return rc;
  5343. }
  5344. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  5345. struct cam_req_mgr_device_info *dev_info)
  5346. {
  5347. int rc = 0;
  5348. dev_info->dev_hdl = ctx->dev_hdl;
  5349. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  5350. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  5351. dev_info->p_delay = 1;
  5352. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  5353. dev_info->trigger_on = true;
  5354. return rc;
  5355. }
  5356. static inline void __cam_isp_context_reset_ctx_params(
  5357. struct cam_isp_context *ctx_isp)
  5358. {
  5359. atomic_set(&ctx_isp->process_bubble, 0);
  5360. atomic_set(&ctx_isp->rxd_epoch, 0);
  5361. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5362. ctx_isp->frame_id = 0;
  5363. ctx_isp->sof_timestamp_val = 0;
  5364. ctx_isp->boot_timestamp = 0;
  5365. ctx_isp->active_req_cnt = 0;
  5366. ctx_isp->reported_req_id = 0;
  5367. ctx_isp->bubble_frame_cnt = 0;
  5368. ctx_isp->recovery_req_id = 0;
  5369. }
  5370. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  5371. struct cam_start_stop_dev_cmd *cmd)
  5372. {
  5373. int rc = 0;
  5374. int i;
  5375. struct cam_isp_start_args start_isp;
  5376. struct cam_ctx_request *req;
  5377. struct cam_isp_ctx_req *req_isp;
  5378. struct cam_isp_context *ctx_isp =
  5379. (struct cam_isp_context *) ctx->ctx_priv;
  5380. if (cmd->session_handle != ctx->session_hdl ||
  5381. cmd->dev_handle != ctx->dev_hdl) {
  5382. rc = -EPERM;
  5383. goto end;
  5384. }
  5385. if (list_empty(&ctx->pending_req_list)) {
  5386. /* should never happen */
  5387. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  5388. rc = -EFAULT;
  5389. goto end;
  5390. } else {
  5391. req = list_first_entry(&ctx->pending_req_list,
  5392. struct cam_ctx_request, list);
  5393. }
  5394. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5395. if (!ctx_isp->hw_ctx) {
  5396. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  5397. rc = -EFAULT;
  5398. goto end;
  5399. }
  5400. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5401. start_isp.hw_config.request_id = req->request_id;
  5402. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5403. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5404. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5405. start_isp.hw_config.init_packet = 1;
  5406. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5407. start_isp.hw_config.cdm_reset_before_apply = false;
  5408. start_isp.is_internal_start = false;
  5409. ctx_isp->last_applied_req_id = req->request_id;
  5410. if (ctx->state == CAM_CTX_FLUSHED)
  5411. start_isp.start_only = true;
  5412. else
  5413. start_isp.start_only = false;
  5414. __cam_isp_context_reset_ctx_params(ctx_isp);
  5415. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5416. CAM_ISP_CTX_ACTIVATED_APPLIED :
  5417. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  5418. CAM_ISP_CTX_ACTIVATED_SOF;
  5419. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5420. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5421. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5422. /*
  5423. * In case of CSID TPG we might receive SOF and RUP IRQs
  5424. * before hw_mgr_intf->hw_start has returned. So move
  5425. * req out of pending list before hw_start and add it
  5426. * back to pending list if hw_start fails.
  5427. */
  5428. list_del_init(&req->list);
  5429. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  5430. list_add_tail(&req->list, &ctx->free_req_list);
  5431. atomic_set(&ctx_isp->rxd_epoch, 1);
  5432. CAM_DBG(CAM_REQ,
  5433. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  5434. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5435. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  5436. list_add_tail(&req->list, &ctx->wait_req_list);
  5437. CAM_DBG(CAM_REQ,
  5438. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  5439. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5440. } else {
  5441. list_add_tail(&req->list, &ctx->active_req_list);
  5442. ctx_isp->active_req_cnt++;
  5443. CAM_DBG(CAM_REQ,
  5444. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  5445. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  5446. ctx_isp->offline_context);
  5447. }
  5448. /*
  5449. * Only place to change state before calling the hw due to
  5450. * hardware tasklet has higher priority that can cause the
  5451. * irq handling comes early
  5452. */
  5453. ctx->state = CAM_CTX_ACTIVATED;
  5454. trace_cam_context_state("ISP", ctx);
  5455. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5456. &start_isp);
  5457. if (rc) {
  5458. /* HW failure. user need to clean up the resource */
  5459. CAM_ERR(CAM_ISP, "Start HW failed");
  5460. ctx->state = CAM_CTX_READY;
  5461. if ((rc == -ETIMEDOUT) &&
  5462. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  5463. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  5464. trace_cam_context_state("ISP", ctx);
  5465. list_del_init(&req->list);
  5466. list_add(&req->list, &ctx->pending_req_list);
  5467. goto end;
  5468. }
  5469. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  5470. end:
  5471. return rc;
  5472. }
  5473. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  5474. struct cam_req_mgr_core_dev_link_setup *unlink)
  5475. {
  5476. int rc = 0;
  5477. ctx->link_hdl = -1;
  5478. ctx->ctx_crm_intf = NULL;
  5479. ctx->state = CAM_CTX_ACQUIRED;
  5480. trace_cam_context_state("ISP", ctx);
  5481. return rc;
  5482. }
  5483. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  5484. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  5485. {
  5486. int rc = 0;
  5487. uint32_t i;
  5488. struct cam_hw_stop_args stop;
  5489. struct cam_ctx_request *req;
  5490. struct cam_isp_ctx_req *req_isp;
  5491. struct cam_isp_context *ctx_isp =
  5492. (struct cam_isp_context *) ctx->ctx_priv;
  5493. struct cam_isp_stop_args stop_isp;
  5494. /* Mask off all the incoming hardware events */
  5495. spin_lock_bh(&ctx->lock);
  5496. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5497. spin_unlock_bh(&ctx->lock);
  5498. /* stop hw first */
  5499. if (ctx_isp->hw_ctx) {
  5500. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5501. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5502. stop_isp.stop_only = false;
  5503. stop_isp.is_internal_stop = false;
  5504. stop.args = (void *) &stop_isp;
  5505. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5506. &stop);
  5507. }
  5508. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5509. __cam_isp_ctx_substate_val_to_type(
  5510. ctx_isp->substate_activated));
  5511. if (ctx->ctx_crm_intf &&
  5512. ctx->ctx_crm_intf->notify_stop) {
  5513. struct cam_req_mgr_notify_stop notify;
  5514. notify.link_hdl = ctx->link_hdl;
  5515. CAM_DBG(CAM_ISP,
  5516. "Notify CRM about device stop ctx %u link 0x%x",
  5517. ctx->ctx_id, ctx->link_hdl);
  5518. ctx->ctx_crm_intf->notify_stop(&notify);
  5519. } else if (!ctx_isp->offline_context)
  5520. CAM_ERR(CAM_ISP, "cb not present");
  5521. while (!list_empty(&ctx->pending_req_list)) {
  5522. req = list_first_entry(&ctx->pending_req_list,
  5523. struct cam_ctx_request, list);
  5524. list_del_init(&req->list);
  5525. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5526. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  5527. req_isp->num_fence_map_out);
  5528. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5529. if (req_isp->fence_map_out[i].sync_id != -1) {
  5530. cam_sync_signal(
  5531. req_isp->fence_map_out[i].sync_id,
  5532. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5533. CAM_SYNC_ISP_EVENT_HW_STOP);
  5534. }
  5535. list_add_tail(&req->list, &ctx->free_req_list);
  5536. }
  5537. while (!list_empty(&ctx->wait_req_list)) {
  5538. req = list_first_entry(&ctx->wait_req_list,
  5539. struct cam_ctx_request, list);
  5540. list_del_init(&req->list);
  5541. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5542. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  5543. req_isp->num_fence_map_out);
  5544. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5545. if (req_isp->fence_map_out[i].sync_id != -1) {
  5546. cam_sync_signal(
  5547. req_isp->fence_map_out[i].sync_id,
  5548. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5549. CAM_SYNC_ISP_EVENT_HW_STOP);
  5550. }
  5551. list_add_tail(&req->list, &ctx->free_req_list);
  5552. }
  5553. while (!list_empty(&ctx->active_req_list)) {
  5554. req = list_first_entry(&ctx->active_req_list,
  5555. struct cam_ctx_request, list);
  5556. list_del_init(&req->list);
  5557. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5558. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  5559. req_isp->num_fence_map_out);
  5560. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5561. if (req_isp->fence_map_out[i].sync_id != -1) {
  5562. cam_sync_signal(
  5563. req_isp->fence_map_out[i].sync_id,
  5564. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5565. CAM_SYNC_ISP_EVENT_HW_STOP);
  5566. }
  5567. list_add_tail(&req->list, &ctx->free_req_list);
  5568. }
  5569. ctx_isp->frame_id = 0;
  5570. ctx_isp->active_req_cnt = 0;
  5571. ctx_isp->reported_req_id = 0;
  5572. ctx_isp->last_applied_req_id = 0;
  5573. ctx_isp->req_info.last_bufdone_req_id = 0;
  5574. ctx_isp->bubble_frame_cnt = 0;
  5575. atomic_set(&ctx_isp->process_bubble, 0);
  5576. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5577. atomic_set(&ctx_isp->rxd_epoch, 0);
  5578. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5579. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5580. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5581. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  5582. ctx->state, ctx->ctx_id);
  5583. if (!stop_cmd) {
  5584. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  5585. if (rc)
  5586. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5587. }
  5588. return rc;
  5589. }
  5590. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  5591. struct cam_start_stop_dev_cmd *cmd)
  5592. {
  5593. int rc = 0;
  5594. struct cam_isp_context *ctx_isp =
  5595. (struct cam_isp_context *)ctx->ctx_priv;
  5596. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  5597. ctx_isp->init_received = false;
  5598. ctx->state = CAM_CTX_ACQUIRED;
  5599. trace_cam_context_state("ISP", ctx);
  5600. return rc;
  5601. }
  5602. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  5603. struct cam_release_dev_cmd *cmd)
  5604. {
  5605. int rc = 0;
  5606. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5607. if (rc)
  5608. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5609. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  5610. if (rc)
  5611. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  5612. return rc;
  5613. }
  5614. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  5615. void *cmd)
  5616. {
  5617. int rc = 0;
  5618. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5619. if (rc)
  5620. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5621. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  5622. if (rc)
  5623. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  5624. return rc;
  5625. }
  5626. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  5627. {
  5628. int rc = 0;
  5629. struct cam_hw_cmd_args hw_cmd_args;
  5630. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5631. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5632. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5633. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  5634. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5635. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5636. &hw_cmd_args);
  5637. return rc;
  5638. }
  5639. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  5640. {
  5641. int rc = 0;
  5642. struct cam_hw_cmd_args hw_cmd_args;
  5643. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5644. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5645. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5646. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5647. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5648. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5649. &hw_cmd_args);
  5650. return rc;
  5651. }
  5652. static int __cam_isp_ctx_handle_sof_freeze_evt(
  5653. struct cam_context *ctx)
  5654. {
  5655. int rc = 0;
  5656. struct cam_hw_cmd_args hw_cmd_args;
  5657. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5658. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5659. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5660. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  5661. isp_hw_cmd_args.u.sof_irq_enable = 1;
  5662. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5663. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5664. &hw_cmd_args);
  5665. return rc;
  5666. }
  5667. static int __cam_isp_ctx_reset_and_recover(
  5668. bool skip_resume, struct cam_context *ctx)
  5669. {
  5670. int rc = 0;
  5671. struct cam_isp_context *ctx_isp =
  5672. (struct cam_isp_context *)ctx->ctx_priv;
  5673. struct cam_isp_stop_args stop_isp;
  5674. struct cam_hw_stop_args stop_args;
  5675. struct cam_isp_start_args start_isp;
  5676. struct cam_hw_cmd_args hw_cmd_args;
  5677. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5678. struct cam_ctx_request *req;
  5679. struct cam_isp_ctx_req *req_isp;
  5680. spin_lock(&ctx->lock);
  5681. if (ctx_isp->active_req_cnt) {
  5682. spin_unlock(&ctx->lock);
  5683. CAM_WARN(CAM_ISP,
  5684. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  5685. ctx_isp->active_req_cnt, ctx->ctx_id,
  5686. ctx->link_hdl, ctx_isp->recovery_req_id);
  5687. goto end;
  5688. }
  5689. if (ctx->state != CAM_CTX_ACTIVATED) {
  5690. spin_unlock(&ctx->lock);
  5691. CAM_ERR(CAM_ISP,
  5692. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  5693. ctx->state, ctx->ctx_id,
  5694. ctx->link_hdl, ctx_isp->recovery_req_id);
  5695. rc = -EINVAL;
  5696. goto end;
  5697. }
  5698. if (list_empty(&ctx->pending_req_list)) {
  5699. /* Cannot start with no request */
  5700. spin_unlock(&ctx->lock);
  5701. CAM_ERR(CAM_ISP,
  5702. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  5703. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  5704. rc = -EFAULT;
  5705. goto end;
  5706. }
  5707. spin_unlock(&ctx->lock);
  5708. if (!ctx_isp->hw_ctx) {
  5709. CAM_ERR(CAM_ISP,
  5710. "Invalid hw context pointer ctx: %u on link: 0x%x",
  5711. ctx->ctx_id, ctx->link_hdl);
  5712. rc = -EFAULT;
  5713. goto end;
  5714. }
  5715. req = list_first_entry(&ctx->pending_req_list,
  5716. struct cam_ctx_request, list);
  5717. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5718. req_isp->bubble_detected = false;
  5719. CAM_INFO(CAM_ISP,
  5720. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  5721. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  5722. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5723. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5724. stop_isp.stop_only = true;
  5725. stop_isp.is_internal_stop = true;
  5726. stop_args.args = (void *)&stop_isp;
  5727. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5728. &stop_args);
  5729. if (rc) {
  5730. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u",
  5731. rc, ctx->ctx_id);
  5732. goto end;
  5733. }
  5734. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  5735. ctx->ctx_id, ctx->link_hdl);
  5736. /* API provides provision to stream off and not resume as well in case of fatal errors */
  5737. if (skip_resume) {
  5738. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5739. CAM_INFO(CAM_ISP,
  5740. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  5741. ctx->ctx_id, ctx_isp->last_applied_req_id,
  5742. ctx_isp->recovery_req_id, ctx->link_hdl);
  5743. goto end;
  5744. }
  5745. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5746. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5747. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5748. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5749. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5750. &hw_cmd_args);
  5751. if (rc) {
  5752. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u", rc, ctx->ctx_id);
  5753. goto end;
  5754. }
  5755. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  5756. ctx->ctx_id, ctx->link_hdl);
  5757. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5758. start_isp.hw_config.request_id = req->request_id;
  5759. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5760. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5761. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5762. start_isp.hw_config.init_packet = 1;
  5763. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  5764. start_isp.hw_config.cdm_reset_before_apply = false;
  5765. start_isp.start_only = true;
  5766. start_isp.is_internal_start = true;
  5767. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  5768. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5769. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  5770. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5771. &start_isp);
  5772. if (rc) {
  5773. CAM_ERR(CAM_ISP, "Start HW failed");
  5774. ctx->state = CAM_CTX_READY;
  5775. goto end;
  5776. }
  5777. /* IQ applied for this request, on next trigger skip IQ cfg */
  5778. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5779. CAM_DBG(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x",
  5780. ctx->ctx_id, ctx->link_hdl);
  5781. end:
  5782. return rc;
  5783. }
  5784. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  5785. struct cam_req_mgr_link_evt_data *link_evt_data)
  5786. {
  5787. int rc = 0;
  5788. switch (link_evt_data->evt_type) {
  5789. case CAM_REQ_MGR_LINK_EVT_ERR:
  5790. /* No handling */
  5791. break;
  5792. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  5793. rc = __cam_isp_ctx_link_pause(ctx);
  5794. break;
  5795. case CAM_REQ_MGR_LINK_EVT_RESUME:
  5796. rc = __cam_isp_ctx_link_resume(ctx);
  5797. break;
  5798. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  5799. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  5800. break;
  5801. case CAM_REQ_MGR_LINK_EVT_STALLED:
  5802. if (ctx->state == CAM_CTX_ACTIVATED)
  5803. rc = __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  5804. break;
  5805. default:
  5806. CAM_WARN(CAM_ISP,
  5807. "Unsupported event type: 0x%x on ctx: %u",
  5808. link_evt_data->evt_type, ctx->ctx_id);
  5809. rc = -EINVAL;
  5810. break;
  5811. }
  5812. return rc;
  5813. }
  5814. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  5815. struct cam_req_mgr_core_dev_link_setup *unlink)
  5816. {
  5817. int rc = 0;
  5818. CAM_WARN(CAM_ISP,
  5819. "Received unlink in activated state. It's unexpected");
  5820. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5821. if (rc)
  5822. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  5823. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  5824. if (rc)
  5825. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5826. return rc;
  5827. }
  5828. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  5829. struct cam_req_mgr_apply_request *apply)
  5830. {
  5831. int rc = 0;
  5832. struct cam_ctx_ops *ctx_ops = NULL;
  5833. struct cam_isp_context *ctx_isp =
  5834. (struct cam_isp_context *) ctx->ctx_priv;
  5835. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  5836. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  5837. __cam_isp_ctx_substate_val_to_type(
  5838. ctx_isp->substate_activated), apply->request_id);
  5839. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5840. if (ctx_ops->crm_ops.apply_req) {
  5841. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  5842. } else {
  5843. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5844. "No handle function in activated Substate[%s]",
  5845. __cam_isp_ctx_substate_val_to_type(
  5846. ctx_isp->substate_activated));
  5847. rc = -EFAULT;
  5848. }
  5849. if (rc)
  5850. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5851. "Apply failed in active Substate[%s] rc %d",
  5852. __cam_isp_ctx_substate_val_to_type(
  5853. ctx_isp->substate_activated), rc);
  5854. return rc;
  5855. }
  5856. static int __cam_isp_ctx_apply_default_settings(
  5857. struct cam_context *ctx,
  5858. struct cam_req_mgr_apply_request *apply)
  5859. {
  5860. int rc = 0;
  5861. struct cam_ctx_ops *ctx_ops = NULL;
  5862. struct cam_isp_context *ctx_isp =
  5863. (struct cam_isp_context *) ctx->ctx_priv;
  5864. if (!ctx_isp->use_default_apply)
  5865. return 0;
  5866. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  5867. CAM_WARN(CAM_ISP,
  5868. "Trigger: %u not subscribed for: %u",
  5869. apply->trigger_point, ctx_isp->subscribe_event);
  5870. return 0;
  5871. }
  5872. /* Allow apply default settings for IFE only at SOF */
  5873. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  5874. return 0;
  5875. if ((ctx_isp->aeb_enabled) && (atomic_read(&ctx_isp->internal_recovery_set)))
  5876. return __cam_isp_ctx_reset_and_recover(false, ctx);
  5877. CAM_DBG(CAM_ISP,
  5878. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  5879. ctx_isp->substate_activated, apply->request_id,
  5880. ctx->ctx_id, ctx->link_hdl);
  5881. ctx_ops = &ctx_isp->substate_machine[
  5882. ctx_isp->substate_activated];
  5883. if (ctx_ops->crm_ops.notify_frame_skip) {
  5884. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  5885. } else {
  5886. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5887. "No handle function in activated substate %d",
  5888. ctx_isp->substate_activated);
  5889. rc = -EFAULT;
  5890. }
  5891. if (rc)
  5892. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5893. "Apply default failed in active substate %d rc %d",
  5894. ctx_isp->substate_activated, rc);
  5895. return rc;
  5896. }
  5897. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  5898. uint32_t evt_id, void *evt_data)
  5899. {
  5900. int rc = 0;
  5901. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  5902. struct cam_context *ctx = (struct cam_context *)context;
  5903. struct cam_isp_context *ctx_isp =
  5904. (struct cam_isp_context *)ctx->ctx_priv;
  5905. spin_lock(&ctx->lock);
  5906. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5907. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5908. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5909. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5910. ctx_isp->substate_activated), evt_id);
  5911. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5912. if (irq_ops->irq_ops[evt_id]) {
  5913. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5914. } else {
  5915. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5916. __cam_isp_ctx_substate_val_to_type(
  5917. ctx_isp->substate_activated));
  5918. if (isp_ctx_debug.enable_state_monitor_dump)
  5919. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5920. }
  5921. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5922. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5923. ctx_isp->substate_activated));
  5924. spin_unlock(&ctx->lock);
  5925. return rc;
  5926. }
  5927. /* top state machine */
  5928. static struct cam_ctx_ops
  5929. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5930. /* Uninit */
  5931. {
  5932. .ioctl_ops = {},
  5933. .crm_ops = {},
  5934. .irq_ops = NULL,
  5935. },
  5936. /* Available */
  5937. {
  5938. .ioctl_ops = {
  5939. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5940. },
  5941. .crm_ops = {},
  5942. .irq_ops = NULL,
  5943. },
  5944. /* Acquired */
  5945. {
  5946. .ioctl_ops = {
  5947. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5948. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5949. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5950. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5951. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5952. },
  5953. .crm_ops = {
  5954. .link = __cam_isp_ctx_link_in_acquired,
  5955. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5956. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5957. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  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. /* Ready */
  5965. {
  5966. .ioctl_ops = {
  5967. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5968. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5969. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5970. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5971. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5972. },
  5973. .crm_ops = {
  5974. .unlink = __cam_isp_ctx_unlink_in_ready,
  5975. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5976. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5977. },
  5978. .irq_ops = NULL,
  5979. .pagefault_ops = cam_isp_context_dump_requests,
  5980. .dumpinfo_ops = cam_isp_context_info_dump,
  5981. },
  5982. /* Flushed */
  5983. {
  5984. .ioctl_ops = {
  5985. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  5986. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  5987. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  5988. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  5989. },
  5990. .crm_ops = {
  5991. .unlink = __cam_isp_ctx_unlink_in_ready,
  5992. .process_evt = __cam_isp_ctx_process_evt,
  5993. .flush_req = __cam_isp_ctx_flush_req_in_flushed_state,
  5994. },
  5995. .irq_ops = NULL,
  5996. .pagefault_ops = cam_isp_context_dump_requests,
  5997. .dumpinfo_ops = cam_isp_context_info_dump,
  5998. },
  5999. /* Activated */
  6000. {
  6001. .ioctl_ops = {
  6002. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6003. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6004. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  6005. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6006. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6007. },
  6008. .crm_ops = {
  6009. .unlink = __cam_isp_ctx_unlink_in_activated,
  6010. .apply_req = __cam_isp_ctx_apply_req,
  6011. .notify_frame_skip =
  6012. __cam_isp_ctx_apply_default_settings,
  6013. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  6014. .process_evt = __cam_isp_ctx_process_evt,
  6015. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6016. },
  6017. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  6018. .pagefault_ops = cam_isp_context_dump_requests,
  6019. .dumpinfo_ops = cam_isp_context_info_dump,
  6020. .recovery_ops = cam_isp_context_hw_recovery,
  6021. },
  6022. };
  6023. static int cam_isp_context_hw_recovery(void *priv, void *data)
  6024. {
  6025. struct cam_context *ctx = priv;
  6026. int rc = -EPERM;
  6027. if (ctx->hw_mgr_intf->hw_recovery)
  6028. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  6029. else
  6030. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery");
  6031. return rc;
  6032. }
  6033. static int cam_isp_context_dump_requests(void *data,
  6034. struct cam_smmu_pf_info *pf_info)
  6035. {
  6036. struct cam_context *ctx = (struct cam_context *)data;
  6037. struct cam_ctx_request *req = NULL;
  6038. struct cam_ctx_request *req_temp = NULL;
  6039. struct cam_isp_ctx_req *req_isp = NULL;
  6040. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  6041. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  6042. struct cam_req_mgr_message req_msg = {0};
  6043. struct cam_isp_context *ctx_isp;
  6044. uint32_t resource_type = 0;
  6045. bool mem_found = false, ctx_found = false, send_error = false;
  6046. int rc = 0;
  6047. struct cam_isp_context *isp_ctx =
  6048. (struct cam_isp_context *)ctx->ctx_priv;
  6049. if (!isp_ctx) {
  6050. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  6051. return -EINVAL;
  6052. }
  6053. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  6054. ctx->ctx_id, ctx->state);
  6055. list_for_each_entry_safe(req, req_temp,
  6056. &ctx->active_req_list, list) {
  6057. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6058. hw_update_data = &req_isp->hw_update_data;
  6059. pf_dbg_entry = &(req->pf_data);
  6060. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  6061. req->request_id);
  6062. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6063. &mem_found, &ctx_found, &resource_type, pf_info);
  6064. if (rc)
  6065. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6066. if (ctx_found)
  6067. send_error = true;
  6068. }
  6069. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  6070. ctx->ctx_id, ctx->state);
  6071. list_for_each_entry_safe(req, req_temp,
  6072. &ctx->wait_req_list, list) {
  6073. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6074. hw_update_data = &req_isp->hw_update_data;
  6075. pf_dbg_entry = &(req->pf_data);
  6076. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  6077. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6078. &mem_found, &ctx_found, &resource_type, pf_info);
  6079. if (rc)
  6080. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6081. if (ctx_found)
  6082. send_error = true;
  6083. }
  6084. /*
  6085. * In certain scenarios we observe both overflow and SMMU pagefault
  6086. * for a particular request. If overflow is handled before page fault
  6087. * we need to traverse through pending request list because if
  6088. * bubble recovery is enabled on any request we move that request
  6089. * and all the subsequent requests to the pending list while handling
  6090. * overflow error.
  6091. */
  6092. CAM_INFO(CAM_ISP,
  6093. "Iterating over pending req list of isp ctx %d state %d",
  6094. ctx->ctx_id, ctx->state);
  6095. list_for_each_entry_safe(req, req_temp,
  6096. &ctx->pending_req_list, list) {
  6097. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6098. hw_update_data = &req_isp->hw_update_data;
  6099. pf_dbg_entry = &(req->pf_data);
  6100. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  6101. req->request_id);
  6102. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6103. &mem_found, &ctx_found, &resource_type, pf_info);
  6104. if (rc)
  6105. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6106. if (ctx_found)
  6107. send_error = true;
  6108. }
  6109. if (resource_type) {
  6110. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  6111. CAM_ERR(CAM_ISP,
  6112. "Page fault on resource:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  6113. __cam_isp_resource_handle_id_to_type(
  6114. ctx_isp->isp_device_type, resource_type),
  6115. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  6116. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  6117. }
  6118. if (send_error) {
  6119. CAM_INFO(CAM_ISP,
  6120. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6121. ctx->ctx_id, ctx->session_hdl,
  6122. ctx->dev_hdl, ctx->link_hdl);
  6123. req_msg.session_hdl = ctx->session_hdl;
  6124. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  6125. req_msg.u.err_msg.error_type =
  6126. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  6127. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  6128. req_msg.u.err_msg.request_id = 0;
  6129. req_msg.u.err_msg.resource_size = 0x0;
  6130. req_msg.u.err_msg.error_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  6131. if (cam_req_mgr_notify_message(&req_msg,
  6132. V4L_EVENT_CAM_REQ_MGR_ERROR,
  6133. V4L_EVENT_CAM_REQ_MGR_EVENT))
  6134. CAM_ERR(CAM_ISP,
  6135. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6136. ctx->ctx_id, ctx->session_hdl,
  6137. ctx->dev_hdl, ctx->link_hdl);
  6138. }
  6139. return rc;
  6140. }
  6141. static int cam_isp_context_debug_register(void)
  6142. {
  6143. int rc = 0;
  6144. struct dentry *dbgfileptr = NULL;
  6145. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  6146. if (!dbgfileptr) {
  6147. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  6148. rc = -ENOENT;
  6149. goto end;
  6150. }
  6151. /* Store parent inode for cleanup in caller */
  6152. isp_ctx_debug.dentry = dbgfileptr;
  6153. debugfs_create_u32("enable_state_monitor_dump", 0644,
  6154. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  6155. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  6156. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  6157. debugfs_create_bool("disable_internal_recovery", 0644,
  6158. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery);
  6159. if (IS_ERR(dbgfileptr)) {
  6160. if (PTR_ERR(dbgfileptr) == -ENODEV)
  6161. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  6162. else
  6163. rc = PTR_ERR(dbgfileptr);
  6164. }
  6165. end:
  6166. return rc;
  6167. }
  6168. int cam_isp_context_init(struct cam_isp_context *ctx,
  6169. struct cam_context *ctx_base,
  6170. struct cam_req_mgr_kmd_ops *crm_node_intf,
  6171. struct cam_hw_mgr_intf *hw_intf,
  6172. uint32_t ctx_id,
  6173. uint32_t isp_device_type,
  6174. int img_iommu_hdl)
  6175. {
  6176. int rc = -1;
  6177. int i;
  6178. if (!ctx || !ctx_base) {
  6179. CAM_ERR(CAM_ISP, "Invalid Context");
  6180. goto err;
  6181. }
  6182. /* ISP context setup */
  6183. memset(ctx, 0, sizeof(*ctx));
  6184. ctx->base = ctx_base;
  6185. ctx->frame_id = 0;
  6186. ctx->custom_enabled = false;
  6187. ctx->use_frame_header_ts = false;
  6188. ctx->use_default_apply = false;
  6189. ctx->active_req_cnt = 0;
  6190. ctx->reported_req_id = 0;
  6191. ctx->bubble_frame_cnt = 0;
  6192. ctx->req_info.last_bufdone_req_id = 0;
  6193. ctx->v4l2_event_sub_ids = 0;
  6194. ctx->hw_ctx = NULL;
  6195. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  6196. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  6197. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  6198. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  6199. ctx->isp_device_type = isp_device_type;
  6200. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6201. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  6202. ctx->req_isp[i].base = &ctx->req_base[i];
  6203. }
  6204. /* camera context setup */
  6205. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  6206. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  6207. if (rc) {
  6208. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  6209. goto err;
  6210. }
  6211. /* link camera context with isp context */
  6212. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  6213. ctx_base->ctx_priv = ctx;
  6214. /* initializing current state for error logging */
  6215. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  6216. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  6217. CAM_ISP_CTX_ACTIVATED_MAX;
  6218. }
  6219. atomic64_set(&ctx->state_monitor_head, -1);
  6220. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6221. atomic64_set(&ctx->event_record_head[i], -1);
  6222. if (!isp_ctx_debug.dentry)
  6223. cam_isp_context_debug_register();
  6224. err:
  6225. return rc;
  6226. }
  6227. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  6228. {
  6229. if (ctx->base)
  6230. cam_context_deinit(ctx->base);
  6231. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  6232. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  6233. __cam_isp_ctx_substate_val_to_type(
  6234. ctx->substate_activated));
  6235. debugfs_remove_recursive(isp_ctx_debug.dentry);
  6236. isp_ctx_debug.dentry = NULL;
  6237. memset(ctx, 0, sizeof(*ctx));
  6238. return 0;
  6239. }