cam_isp_context.c 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143
  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. struct cam_isp_ctx_req *req_isp;
  1680. bool req_in_pending_wait_list = false;
  1681. if (list_empty(&ctx->active_req_list)) {
  1682. if (!list_empty(&ctx->wait_req_list)) {
  1683. req = list_first_entry(&ctx->wait_req_list,
  1684. struct cam_ctx_request, list);
  1685. req_in_pending_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. } else if (!list_empty(&ctx->pending_req_list)) {
  1708. /*
  1709. * We saw the case that the hw config is blocked due to
  1710. * some reason, the we get the reg upd and buf done before
  1711. * the req is added to wait req list.
  1712. */
  1713. req = list_first_entry(&ctx->pending_req_list,
  1714. struct cam_ctx_request, list);
  1715. req_in_pending_wait_list = true;
  1716. if (ctx_isp->last_applied_req_id !=
  1717. ctx_isp->last_bufdone_err_apply_req_id) {
  1718. CAM_WARN(CAM_ISP,
  1719. "Buf done with no active request but with req in pending list, req %llu last apply id:%lld last err id:%lld",
  1720. req->request_id,
  1721. ctx_isp->last_applied_req_id,
  1722. ctx_isp->last_bufdone_err_apply_req_id);
  1723. ctx_isp->last_bufdone_err_apply_req_id =
  1724. ctx_isp->last_applied_req_id;
  1725. }
  1726. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1727. /*
  1728. * Verify consumed address for this request to make sure
  1729. * we are handling the buf_done for the correct
  1730. * buffer. Also defer actual buf_done handling, i.e
  1731. * do not signal the fence as this request may go into
  1732. * Bubble state eventully.
  1733. */
  1734. rc =
  1735. __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1736. ctx_isp, req, done, bubble_state, true, true);
  1737. }
  1738. if (!req_in_pending_wait_list && (ctx_isp->last_applied_req_id !=
  1739. ctx_isp->last_bufdone_err_apply_req_id)) {
  1740. CAM_WARN(CAM_ISP,
  1741. "Buf done with no active request bubble_state=%d last_applied_req_id:%lld ",
  1742. bubble_state, ctx_isp->last_applied_req_id);
  1743. ctx_isp->last_bufdone_err_apply_req_id =
  1744. ctx_isp->last_applied_req_id;
  1745. }
  1746. return 0;
  1747. }
  1748. req = list_first_entry(&ctx->active_req_list,
  1749. struct cam_ctx_request, list);
  1750. if (ctx_isp->active_req_cnt > 1) {
  1751. next_req = list_last_entry(
  1752. &ctx->active_req_list,
  1753. struct cam_ctx_request, list);
  1754. if (next_req->request_id != req->request_id)
  1755. __cam_isp_ctx_buf_done_match_req(next_req, done,
  1756. &irq_delay_detected);
  1757. else
  1758. CAM_WARN(CAM_ISP,
  1759. "Req %lld only active request, spurious buf_done rxd",
  1760. req->request_id);
  1761. }
  1762. /*
  1763. * If irq delay isn't detected, then we need to verify
  1764. * the consumed address for current req, otherwise, we
  1765. * can't verify the consumed address.
  1766. */
  1767. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1768. ctx_isp, req, done, bubble_state,
  1769. !irq_delay_detected, false);
  1770. /*
  1771. * Verify the consumed address for next req all the time,
  1772. * since the reported buf done event may belong to current
  1773. * req, then we can't signal this event for next req.
  1774. */
  1775. if (!rc && irq_delay_detected)
  1776. rc = __cam_isp_ctx_handle_buf_done_for_request_verify_addr(
  1777. ctx_isp, next_req, done,
  1778. bubble_state, true, false);
  1779. return rc;
  1780. }
  1781. static int __cam_isp_ctx_handle_buf_done_in_activated_state(
  1782. struct cam_isp_context *ctx_isp,
  1783. struct cam_isp_hw_done_event_data *done,
  1784. uint32_t bubble_state)
  1785. {
  1786. int rc = 0;
  1787. if (ctx_isp->support_consumed_addr)
  1788. rc = __cam_isp_ctx_handle_buf_done_verify_addr(
  1789. ctx_isp, done, bubble_state);
  1790. else
  1791. rc = __cam_isp_ctx_handle_buf_done(
  1792. ctx_isp, done, bubble_state);
  1793. return rc;
  1794. }
  1795. static int __cam_isp_ctx_apply_req_offline(
  1796. void *priv, void *data)
  1797. {
  1798. int rc = 0;
  1799. int64_t prev_applied_req;
  1800. struct cam_context *ctx = NULL;
  1801. struct cam_isp_context *ctx_isp = priv;
  1802. struct cam_ctx_request *req;
  1803. struct cam_isp_ctx_req *req_isp;
  1804. struct cam_hw_config_args cfg;
  1805. if (!ctx_isp) {
  1806. CAM_ERR(CAM_ISP, "Invalid ctx_isp:%pK", ctx);
  1807. rc = -EINVAL;
  1808. goto end;
  1809. }
  1810. ctx = ctx_isp->base;
  1811. if (list_empty(&ctx->pending_req_list)) {
  1812. CAM_DBG(CAM_ISP, "No pending requests to apply");
  1813. rc = -EFAULT;
  1814. goto end;
  1815. }
  1816. if ((ctx->state != CAM_CTX_ACTIVATED) ||
  1817. (!atomic_read(&ctx_isp->rxd_epoch)) ||
  1818. (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_APPLIED))
  1819. goto end;
  1820. if (ctx_isp->active_req_cnt >= 2)
  1821. goto end;
  1822. spin_lock_bh(&ctx->lock);
  1823. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  1824. list);
  1825. spin_unlock_bh(&ctx->lock);
  1826. CAM_DBG(CAM_REQ, "Apply request %lld in substate %d ctx %u",
  1827. req->request_id, ctx_isp->substate_activated, ctx->ctx_id);
  1828. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1829. memset(&cfg, 0, sizeof(cfg));
  1830. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  1831. cfg.request_id = req->request_id;
  1832. cfg.hw_update_entries = req_isp->cfg;
  1833. cfg.num_hw_update_entries = req_isp->num_cfg;
  1834. cfg.priv = &req_isp->hw_update_data;
  1835. cfg.init_packet = 0;
  1836. /*
  1837. * Offline mode may receive the SOF and REG_UPD earlier than
  1838. * CDM processing return back, so we set the substate before
  1839. * apply setting.
  1840. */
  1841. spin_lock_bh(&ctx->lock);
  1842. atomic_set(&ctx_isp->rxd_epoch, 0);
  1843. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_APPLIED;
  1844. prev_applied_req = ctx_isp->last_applied_req_id;
  1845. ctx_isp->last_applied_req_id = req->request_id;
  1846. atomic_set(&ctx_isp->apply_in_progress, 1);
  1847. list_del_init(&req->list);
  1848. list_add_tail(&req->list, &ctx->wait_req_list);
  1849. spin_unlock_bh(&ctx->lock);
  1850. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  1851. if (rc) {
  1852. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not apply the configuration");
  1853. spin_lock_bh(&ctx->lock);
  1854. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  1855. ctx_isp->last_applied_req_id = prev_applied_req;
  1856. atomic_set(&ctx_isp->apply_in_progress, 0);
  1857. list_del_init(&req->list);
  1858. list_add(&req->list, &ctx->pending_req_list);
  1859. spin_unlock_bh(&ctx->lock);
  1860. } else {
  1861. atomic_set(&ctx_isp->apply_in_progress, 0);
  1862. CAM_DBG(CAM_ISP, "New substate state %d, applied req %lld",
  1863. CAM_ISP_CTX_ACTIVATED_APPLIED,
  1864. ctx_isp->last_applied_req_id);
  1865. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1866. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  1867. req->request_id);
  1868. }
  1869. end:
  1870. return rc;
  1871. }
  1872. static int __cam_isp_ctx_schedule_apply_req_offline(
  1873. struct cam_isp_context *ctx_isp)
  1874. {
  1875. int rc = 0;
  1876. struct crm_workq_task *task;
  1877. task = cam_req_mgr_workq_get_task(ctx_isp->workq);
  1878. if (!task) {
  1879. CAM_ERR(CAM_ISP, "No task for worker");
  1880. return -ENOMEM;
  1881. }
  1882. task->process_cb = __cam_isp_ctx_apply_req_offline;
  1883. rc = cam_req_mgr_workq_enqueue_task(task, ctx_isp, CRM_TASK_PRIORITY_0);
  1884. if (rc)
  1885. CAM_ERR(CAM_ISP, "Failed to schedule task rc:%d", rc);
  1886. return rc;
  1887. }
  1888. static int __cam_isp_ctx_offline_epoch_in_activated_state(
  1889. struct cam_isp_context *ctx_isp, void *evt_data)
  1890. {
  1891. struct cam_context *ctx = ctx_isp->base;
  1892. struct cam_ctx_request *req, *req_temp;
  1893. uint64_t request_id = 0;
  1894. atomic_set(&ctx_isp->rxd_epoch, 1);
  1895. CAM_DBG(CAM_ISP, "SOF frame %lld ctx %u", ctx_isp->frame_id,
  1896. ctx->ctx_id);
  1897. list_for_each_entry_safe(req, req_temp, &ctx->active_req_list, list) {
  1898. if (req->request_id > ctx_isp->reported_req_id) {
  1899. request_id = req->request_id;
  1900. ctx_isp->reported_req_id = request_id;
  1901. break;
  1902. }
  1903. }
  1904. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  1905. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  1906. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  1907. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1908. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  1909. request_id);
  1910. return 0;
  1911. }
  1912. static int __cam_isp_ctx_reg_upd_in_epoch_bubble_state(
  1913. struct cam_isp_context *ctx_isp, void *evt_data)
  1914. {
  1915. if (ctx_isp->frame_id == 1)
  1916. CAM_DBG(CAM_ISP, "Reg update in Substate[%s] for early PCR",
  1917. __cam_isp_ctx_substate_val_to_type(
  1918. ctx_isp->substate_activated));
  1919. else
  1920. CAM_WARN_RATE_LIMIT(CAM_ISP,
  1921. "ctx_id:%d Unexpected reg update in activated Substate[%s] for frame_id:%lld",
  1922. ctx_isp->base->ctx_id,
  1923. __cam_isp_ctx_substate_val_to_type(
  1924. ctx_isp->substate_activated),
  1925. ctx_isp->frame_id);
  1926. return 0;
  1927. }
  1928. static int __cam_isp_ctx_reg_upd_in_applied_state(
  1929. struct cam_isp_context *ctx_isp, void *evt_data)
  1930. {
  1931. int rc = 0;
  1932. struct cam_ctx_request *req;
  1933. struct cam_context *ctx = ctx_isp->base;
  1934. struct cam_isp_ctx_req *req_isp;
  1935. uint64_t request_id = 0;
  1936. if (list_empty(&ctx->wait_req_list)) {
  1937. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  1938. goto end;
  1939. }
  1940. req = list_first_entry(&ctx->wait_req_list,
  1941. struct cam_ctx_request, list);
  1942. list_del_init(&req->list);
  1943. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  1944. if (req_isp->num_fence_map_out != 0) {
  1945. list_add_tail(&req->list, &ctx->active_req_list);
  1946. ctx_isp->active_req_cnt++;
  1947. request_id = req->request_id;
  1948. CAM_DBG(CAM_REQ,
  1949. "move request %lld to active list(cnt = %d), ctx %u",
  1950. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1951. __cam_isp_ctx_update_event_record(ctx_isp,
  1952. CAM_ISP_CTX_EVENT_RUP, req);
  1953. } else {
  1954. /* no io config, so the request is completed. */
  1955. list_add_tail(&req->list, &ctx->free_req_list);
  1956. CAM_DBG(CAM_ISP,
  1957. "move active request %lld to free list(cnt = %d), ctx %u",
  1958. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  1959. }
  1960. /*
  1961. * This function only called directly from applied and bubble applied
  1962. * state so change substate here.
  1963. */
  1964. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  1965. CAM_DBG(CAM_ISP, "next Substate[%s]",
  1966. __cam_isp_ctx_substate_val_to_type(
  1967. ctx_isp->substate_activated));
  1968. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  1969. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE, request_id);
  1970. end:
  1971. return rc;
  1972. }
  1973. static int __cam_isp_ctx_notify_sof_in_activated_state(
  1974. struct cam_isp_context *ctx_isp, void *evt_data)
  1975. {
  1976. int rc = 0;
  1977. uint64_t request_id = 0;
  1978. struct cam_context *ctx = ctx_isp->base;
  1979. struct cam_ctx_request *req;
  1980. struct cam_isp_ctx_req *req_isp;
  1981. struct cam_hw_cmd_args hw_cmd_args;
  1982. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  1983. uint64_t last_cdm_done_req = 0;
  1984. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  1985. (struct cam_isp_hw_epoch_event_data *)evt_data;
  1986. if (!evt_data) {
  1987. CAM_ERR(CAM_ISP, "invalid event data");
  1988. return -EINVAL;
  1989. }
  1990. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  1991. if (atomic_read(&ctx_isp->process_bubble)) {
  1992. if (list_empty(&ctx->active_req_list)) {
  1993. CAM_ERR(CAM_ISP,
  1994. "No available active req in bubble");
  1995. atomic_set(&ctx_isp->process_bubble, 0);
  1996. ctx_isp->bubble_frame_cnt = 0;
  1997. rc = -EINVAL;
  1998. return rc;
  1999. }
  2000. if (ctx_isp->last_sof_timestamp ==
  2001. ctx_isp->sof_timestamp_val) {
  2002. CAM_DBG(CAM_ISP,
  2003. "Tasklet delay detected! Bubble frame check skipped, sof_timestamp: %lld",
  2004. ctx_isp->sof_timestamp_val);
  2005. goto notify_only;
  2006. }
  2007. req = list_first_entry(&ctx->active_req_list,
  2008. struct cam_ctx_request, list);
  2009. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2010. if (ctx_isp->bubble_frame_cnt >= 1 &&
  2011. req_isp->bubble_detected) {
  2012. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  2013. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  2014. isp_hw_cmd_args.cmd_type =
  2015. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  2016. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  2017. rc = ctx->hw_mgr_intf->hw_cmd(
  2018. ctx->hw_mgr_intf->hw_mgr_priv,
  2019. &hw_cmd_args);
  2020. if (rc) {
  2021. CAM_ERR(CAM_ISP, "HW command failed");
  2022. return rc;
  2023. }
  2024. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  2025. CAM_DBG(CAM_ISP, "last_cdm_done req: %d",
  2026. last_cdm_done_req);
  2027. if (last_cdm_done_req >= req->request_id) {
  2028. CAM_DBG(CAM_ISP,
  2029. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  2030. req->request_id);
  2031. ctx_isp->bubble_frame_cnt = 0;
  2032. } else {
  2033. CAM_DBG(CAM_ISP,
  2034. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  2035. req->request_id);
  2036. req_isp->num_acked = 0;
  2037. req_isp->num_deferred_acks = 0;
  2038. ctx_isp->bubble_frame_cnt = 0;
  2039. req_isp->bubble_detected = false;
  2040. req_isp->cdm_reset_before_apply = true;
  2041. list_del_init(&req->list);
  2042. list_add(&req->list, &ctx->pending_req_list);
  2043. atomic_set(&ctx_isp->process_bubble, 0);
  2044. ctx_isp->active_req_cnt--;
  2045. CAM_DBG(CAM_REQ,
  2046. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  2047. req->request_id,
  2048. ctx_isp->active_req_cnt, ctx->ctx_id);
  2049. }
  2050. } else if (req_isp->bubble_detected) {
  2051. ctx_isp->bubble_frame_cnt++;
  2052. CAM_DBG(CAM_ISP,
  2053. "Waiting on bufdone for bubble req: %lld, since frame_cnt = %lld",
  2054. req->request_id,
  2055. ctx_isp->bubble_frame_cnt);
  2056. } else {
  2057. CAM_DBG(CAM_ISP, "Delayed bufdone for req: %lld",
  2058. req->request_id);
  2059. }
  2060. }
  2061. notify_only:
  2062. /*
  2063. * notify reqmgr with sof signal. Note, due to scheduling delay
  2064. * we can run into situation that two active requests has already
  2065. * be in the active queue while we try to do the notification.
  2066. * In this case, we need to skip the current notification. This
  2067. * helps the state machine to catch up the delay.
  2068. */
  2069. if (ctx_isp->active_req_cnt <= 2) {
  2070. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2071. list_for_each_entry(req, &ctx->active_req_list, list) {
  2072. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2073. if ((!req_isp->bubble_detected) &&
  2074. (req->request_id > ctx_isp->reported_req_id)) {
  2075. request_id = req->request_id;
  2076. __cam_isp_ctx_update_event_record(ctx_isp,
  2077. CAM_ISP_CTX_EVENT_EPOCH, req);
  2078. break;
  2079. }
  2080. }
  2081. if (ctx_isp->substate_activated == CAM_ISP_CTX_ACTIVATED_BUBBLE)
  2082. request_id = 0;
  2083. if (request_id != 0)
  2084. ctx_isp->reported_req_id = request_id;
  2085. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2086. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2087. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2088. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  2089. request_id);
  2090. }
  2091. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  2092. return 0;
  2093. }
  2094. static int __cam_isp_ctx_notify_eof_in_activated_state(
  2095. struct cam_isp_context *ctx_isp, void *evt_data)
  2096. {
  2097. int rc = 0;
  2098. /* notify reqmgr with eof signal */
  2099. rc = __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_EOF, ctx_isp);
  2100. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2101. CAM_ISP_STATE_CHANGE_TRIGGER_EOF, 0);
  2102. return rc;
  2103. }
  2104. static int __cam_isp_ctx_reg_upd_in_hw_error(
  2105. struct cam_isp_context *ctx_isp, void *evt_data)
  2106. {
  2107. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2108. return 0;
  2109. }
  2110. static int __cam_isp_ctx_sof_in_activated_state(
  2111. struct cam_isp_context *ctx_isp, void *evt_data)
  2112. {
  2113. int rc = 0;
  2114. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2115. struct cam_ctx_request *req = NULL;
  2116. struct cam_context *ctx = ctx_isp->base;
  2117. uint64_t request_id = 0;
  2118. /* First check if there is a valid request in active list */
  2119. list_for_each_entry(req, &ctx->active_req_list, list) {
  2120. if (req->request_id > ctx_isp->reported_req_id) {
  2121. request_id = req->request_id;
  2122. break;
  2123. }
  2124. }
  2125. /*
  2126. * If nothing in active list, current request might have not moved
  2127. * from wait to active list. This could happen if REG_UPDATE to sw
  2128. * is coming immediately after SOF
  2129. */
  2130. if (request_id == 0) {
  2131. req = list_first_entry(&ctx->wait_req_list,
  2132. struct cam_ctx_request, list);
  2133. if (req)
  2134. request_id = req->request_id;
  2135. }
  2136. if (!evt_data) {
  2137. CAM_ERR(CAM_ISP, "in valid sof event data");
  2138. return -EINVAL;
  2139. }
  2140. ctx_isp->frame_id++;
  2141. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2142. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2143. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2144. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2145. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx, ctx %u",
  2146. ctx_isp->frame_id, ctx_isp->sof_timestamp_val, ctx->ctx_id);
  2147. return rc;
  2148. }
  2149. static int __cam_isp_ctx_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2150. void *evt_data)
  2151. {
  2152. int rc = 0;
  2153. struct cam_ctx_request *req = NULL;
  2154. struct cam_isp_ctx_req *req_isp;
  2155. struct cam_context *ctx = ctx_isp->base;
  2156. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2157. CAM_DBG(CAM_ISP, "invalid RUP");
  2158. goto end;
  2159. }
  2160. /*
  2161. * This is for the first update. The initial setting will
  2162. * cause the reg_upd in the first frame.
  2163. */
  2164. if (!list_empty(&ctx->wait_req_list)) {
  2165. req = list_first_entry(&ctx->wait_req_list,
  2166. struct cam_ctx_request, list);
  2167. list_del_init(&req->list);
  2168. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2169. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2170. list_add_tail(&req->list, &ctx->free_req_list);
  2171. else
  2172. CAM_ERR(CAM_ISP,
  2173. "receive rup in unexpected state");
  2174. }
  2175. if (req != NULL) {
  2176. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2177. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2178. req->request_id);
  2179. }
  2180. end:
  2181. return rc;
  2182. }
  2183. static int __cam_isp_ctx_epoch_in_applied(struct cam_isp_context *ctx_isp,
  2184. void *evt_data)
  2185. {
  2186. uint64_t request_id = 0;
  2187. uint32_t sof_event_status = CAM_REQ_MGR_SOF_EVENT_SUCCESS;
  2188. struct cam_ctx_request *req;
  2189. struct cam_isp_ctx_req *req_isp;
  2190. struct cam_context *ctx = ctx_isp->base;
  2191. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2192. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2193. if (!evt_data) {
  2194. CAM_ERR(CAM_ISP, "invalid event data");
  2195. return -EINVAL;
  2196. }
  2197. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2198. if (list_empty(&ctx->wait_req_list)) {
  2199. /*
  2200. * If no wait req in epoch, this is an error case.
  2201. * The recovery is to go back to sof state
  2202. */
  2203. CAM_ERR(CAM_ISP, "Ctx:%d No wait request", ctx->ctx_id);
  2204. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2205. /* Send SOF event as empty frame*/
  2206. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2207. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2208. __cam_isp_ctx_update_event_record(ctx_isp,
  2209. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2210. goto end;
  2211. }
  2212. /* Update state prior to notifying CRM */
  2213. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2214. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2215. list);
  2216. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2217. req_isp->bubble_detected = true;
  2218. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  2219. req_isp->cdm_reset_before_apply = false;
  2220. CAM_INFO_RATE_LIMIT(CAM_ISP, "ctx:%d Report Bubble flag %d req id:%lld",
  2221. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  2222. if (req_isp->bubble_report) {
  2223. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  2224. req->request_id, ctx_isp);
  2225. trace_cam_log_event("Bubble", "Rcvd epoch in applied state",
  2226. req->request_id, ctx->ctx_id);
  2227. atomic_set(&ctx_isp->process_bubble, 1);
  2228. } else {
  2229. req_isp->bubble_report = 0;
  2230. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  2231. req->request_id, ctx->ctx_id);
  2232. if (ctx_isp->active_req_cnt <= 1)
  2233. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2234. atomic_set(&ctx_isp->process_bubble, 1);
  2235. }
  2236. /*
  2237. * Always move the request to active list. Let buf done
  2238. * function handles the rest.
  2239. */
  2240. list_del_init(&req->list);
  2241. list_add_tail(&req->list, &ctx->active_req_list);
  2242. ctx_isp->active_req_cnt++;
  2243. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d), ctx %u",
  2244. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  2245. /*
  2246. * Update the record before req pointer to
  2247. * other invalid req.
  2248. */
  2249. __cam_isp_ctx_update_event_record(ctx_isp,
  2250. CAM_ISP_CTX_EVENT_EPOCH, req);
  2251. /*
  2252. * Get the req again from active_req_list in case
  2253. * the active req cnt is 2.
  2254. */
  2255. list_for_each_entry(req, &ctx->active_req_list, list) {
  2256. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2257. if ((!req_isp->bubble_report) &&
  2258. (req->request_id > ctx_isp->reported_req_id)) {
  2259. request_id = req->request_id;
  2260. ctx_isp->reported_req_id = request_id;
  2261. CAM_DBG(CAM_ISP,
  2262. "ctx %d reported_req_id update to %lld",
  2263. ctx->ctx_id, ctx_isp->reported_req_id);
  2264. break;
  2265. }
  2266. }
  2267. if ((request_id != 0) && req_isp->bubble_detected)
  2268. sof_event_status = CAM_REQ_MGR_SOF_EVENT_ERROR;
  2269. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2270. sof_event_status);
  2271. cam_req_mgr_debug_delay_detect();
  2272. trace_cam_delay_detect("ISP",
  2273. "bubble epoch_in_applied", req->request_id,
  2274. ctx->ctx_id, ctx->link_hdl, ctx->session_hdl,
  2275. CAM_DEFAULT_VALUE);
  2276. end:
  2277. if (request_id == 0) {
  2278. req = list_last_entry(&ctx->active_req_list,
  2279. struct cam_ctx_request, list);
  2280. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2281. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2282. } else {
  2283. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2284. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, request_id);
  2285. }
  2286. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2287. __cam_isp_ctx_substate_val_to_type(
  2288. ctx_isp->substate_activated));
  2289. return 0;
  2290. }
  2291. static int __cam_isp_ctx_buf_done_in_sof(struct cam_isp_context *ctx_isp,
  2292. void *evt_data)
  2293. {
  2294. int rc = 0;
  2295. struct cam_isp_hw_done_event_data *done =
  2296. (struct cam_isp_hw_done_event_data *) evt_data;
  2297. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2298. return rc;
  2299. }
  2300. static int __cam_isp_ctx_buf_done_in_applied(struct cam_isp_context *ctx_isp,
  2301. void *evt_data)
  2302. {
  2303. int rc = 0;
  2304. struct cam_isp_hw_done_event_data *done =
  2305. (struct cam_isp_hw_done_event_data *) evt_data;
  2306. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2307. return rc;
  2308. }
  2309. static int __cam_isp_ctx_sof_in_epoch(struct cam_isp_context *ctx_isp,
  2310. void *evt_data)
  2311. {
  2312. int rc = 0;
  2313. struct cam_context *ctx = ctx_isp->base;
  2314. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2315. struct cam_ctx_request *req;
  2316. if (!evt_data) {
  2317. CAM_ERR(CAM_ISP, "in valid sof event data");
  2318. return -EINVAL;
  2319. }
  2320. if (atomic_read(&ctx_isp->apply_in_progress))
  2321. CAM_INFO(CAM_ISP, "Apply is in progress at the time of SOF");
  2322. ctx_isp->frame_id++;
  2323. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2324. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2325. if (list_empty(&ctx->active_req_list))
  2326. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  2327. else
  2328. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  2329. req = list_last_entry(&ctx->active_req_list,
  2330. struct cam_ctx_request, list);
  2331. if (req)
  2332. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2333. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  2334. req->request_id);
  2335. if (ctx_isp->frame_id == 1)
  2336. CAM_INFO(CAM_ISP,
  2337. "First SOF in EPCR ctx:%d frame_id:%lld next substate %s",
  2338. ctx->ctx_id, ctx_isp->frame_id,
  2339. __cam_isp_ctx_substate_val_to_type(
  2340. ctx_isp->substate_activated));
  2341. CAM_DBG(CAM_ISP, "SOF in epoch ctx:%d frame_id:%lld next substate:%s",
  2342. ctx->ctx_id, ctx_isp->frame_id,
  2343. __cam_isp_ctx_substate_val_to_type(
  2344. ctx_isp->substate_activated));
  2345. return rc;
  2346. }
  2347. static int __cam_isp_ctx_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2348. void *evt_data)
  2349. {
  2350. int rc = 0;
  2351. struct cam_isp_hw_done_event_data *done =
  2352. (struct cam_isp_hw_done_event_data *) evt_data;
  2353. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2354. return rc;
  2355. }
  2356. static int __cam_isp_ctx_buf_done_in_bubble(
  2357. struct cam_isp_context *ctx_isp, void *evt_data)
  2358. {
  2359. int rc = 0;
  2360. struct cam_isp_hw_done_event_data *done =
  2361. (struct cam_isp_hw_done_event_data *) evt_data;
  2362. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2363. return rc;
  2364. }
  2365. static int __cam_isp_ctx_epoch_in_bubble_applied(
  2366. struct cam_isp_context *ctx_isp, void *evt_data)
  2367. {
  2368. uint64_t request_id = 0;
  2369. struct cam_ctx_request *req;
  2370. struct cam_isp_ctx_req *req_isp;
  2371. struct cam_context *ctx = ctx_isp->base;
  2372. struct cam_isp_hw_epoch_event_data *epoch_done_event_data =
  2373. (struct cam_isp_hw_epoch_event_data *)evt_data;
  2374. if (!evt_data) {
  2375. CAM_ERR(CAM_ISP, "invalid event data");
  2376. return -EINVAL;
  2377. }
  2378. ctx_isp->frame_id_meta = epoch_done_event_data->frame_id_meta;
  2379. /*
  2380. * This means we missed the reg upd ack. So we need to
  2381. * transition to BUBBLE state again.
  2382. */
  2383. if (list_empty(&ctx->wait_req_list)) {
  2384. /*
  2385. * If no pending req in epoch, this is an error case.
  2386. * Just go back to the bubble state.
  2387. */
  2388. CAM_ERR(CAM_ISP, "ctx:%d No pending request.", ctx->ctx_id);
  2389. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2390. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2391. __cam_isp_ctx_update_event_record(ctx_isp,
  2392. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2393. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2394. goto end;
  2395. }
  2396. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  2397. list);
  2398. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2399. req_isp->bubble_detected = true;
  2400. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  2401. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  2402. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  2403. req_isp->cdm_reset_before_apply = false;
  2404. if (req_isp->bubble_report) {
  2405. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  2406. req->request_id, ctx_isp);
  2407. atomic_set(&ctx_isp->process_bubble, 1);
  2408. } else {
  2409. req_isp->bubble_report = 0;
  2410. CAM_DBG(CAM_ISP, "Skip bubble recovery for req %lld ctx %u",
  2411. req->request_id, ctx->ctx_id);
  2412. if (ctx_isp->active_req_cnt <= 1)
  2413. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2414. atomic_set(&ctx_isp->process_bubble, 1);
  2415. }
  2416. /*
  2417. * Always move the request to active list. Let buf done
  2418. * function handles the rest.
  2419. */
  2420. list_del_init(&req->list);
  2421. list_add_tail(&req->list, &ctx->active_req_list);
  2422. ctx_isp->active_req_cnt++;
  2423. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d) ctx %u",
  2424. req->request_id, ctx_isp->active_req_cnt);
  2425. if (!req_isp->bubble_report) {
  2426. if (req->request_id > ctx_isp->reported_req_id) {
  2427. request_id = req->request_id;
  2428. ctx_isp->reported_req_id = request_id;
  2429. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2430. CAM_REQ_MGR_SOF_EVENT_ERROR);
  2431. __cam_isp_ctx_update_event_record(ctx_isp,
  2432. CAM_ISP_CTX_EVENT_EPOCH, req);
  2433. } else {
  2434. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2435. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2436. __cam_isp_ctx_update_event_record(ctx_isp,
  2437. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2438. }
  2439. } else {
  2440. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2441. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2442. __cam_isp_ctx_update_event_record(ctx_isp,
  2443. CAM_ISP_CTX_EVENT_EPOCH, NULL);
  2444. }
  2445. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2446. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2447. __cam_isp_ctx_substate_val_to_type(
  2448. ctx_isp->substate_activated));
  2449. cam_req_mgr_debug_delay_detect();
  2450. trace_cam_delay_detect("ISP",
  2451. "bubble epoch_in_bubble_applied",
  2452. req->request_id, ctx->ctx_id,
  2453. ctx->link_hdl, ctx->session_hdl,
  2454. CAM_DEFAULT_VALUE);
  2455. end:
  2456. req = list_last_entry(&ctx->active_req_list, struct cam_ctx_request,
  2457. list);
  2458. if (req)
  2459. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2460. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH, req->request_id);
  2461. return 0;
  2462. }
  2463. static int __cam_isp_ctx_buf_done_in_bubble_applied(
  2464. struct cam_isp_context *ctx_isp, void *evt_data)
  2465. {
  2466. int rc = 0;
  2467. struct cam_isp_hw_done_event_data *done =
  2468. (struct cam_isp_hw_done_event_data *) evt_data;
  2469. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 1);
  2470. return rc;
  2471. }
  2472. static uint32_t get_evt_param(uint32_t error_type)
  2473. {
  2474. switch (error_type) {
  2475. case CAM_ISP_HW_ERROR_OVERFLOW:
  2476. return CAM_SYNC_ISP_EVENT_OVERFLOW;
  2477. case CAM_ISP_HW_ERROR_P2I_ERROR:
  2478. return CAM_SYNC_ISP_EVENT_P2I_ERROR;
  2479. case CAM_ISP_HW_ERROR_VIOLATION:
  2480. return CAM_SYNC_ISP_EVENT_VIOLATION;
  2481. case CAM_ISP_HW_ERROR_BUSIF_OVERFLOW:
  2482. return CAM_SYNC_ISP_EVENT_BUSIF_OVERFLOW;
  2483. default:
  2484. return CAM_SYNC_ISP_EVENT_UNKNOWN;
  2485. }
  2486. }
  2487. static int __cam_isp_ctx_handle_error(struct cam_isp_context *ctx_isp,
  2488. void *evt_data)
  2489. {
  2490. int rc = 0;
  2491. enum cam_req_mgr_device_error error;
  2492. uint32_t i = 0;
  2493. bool found = 0;
  2494. struct cam_ctx_request *req = NULL;
  2495. struct cam_ctx_request *req_to_report = NULL;
  2496. struct cam_ctx_request *req_to_dump = NULL;
  2497. struct cam_ctx_request *req_temp;
  2498. struct cam_isp_ctx_req *req_isp = NULL;
  2499. struct cam_isp_ctx_req *req_isp_to_report = NULL;
  2500. uint64_t error_request_id;
  2501. struct cam_hw_fence_map_entry *fence_map_out = NULL;
  2502. uint32_t evt_param;
  2503. struct cam_context *ctx = ctx_isp->base;
  2504. struct cam_isp_hw_error_event_data *error_event_data =
  2505. (struct cam_isp_hw_error_event_data *)evt_data;
  2506. uint32_t error_type = error_event_data->error_type;
  2507. CAM_DBG(CAM_ISP, "Enter error_type = %d", error_type);
  2508. __cam_isp_ctx_pause_crm_timer(ctx);
  2509. if ((error_type == CAM_ISP_HW_ERROR_OVERFLOW) ||
  2510. (error_type == CAM_ISP_HW_ERROR_BUSIF_OVERFLOW) ||
  2511. (error_type == CAM_ISP_HW_ERROR_VIOLATION))
  2512. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  2513. evt_param = get_evt_param(error_type);
  2514. /*
  2515. * The error is likely caused by first request on the active list.
  2516. * If active list is empty check wait list (maybe error hit as soon
  2517. * as RUP and we handle error before RUP.
  2518. */
  2519. if (list_empty(&ctx->active_req_list)) {
  2520. CAM_DBG(CAM_ISP,
  2521. "handling error with no active request");
  2522. if (list_empty(&ctx->wait_req_list)) {
  2523. CAM_ERR_RATE_LIMIT(CAM_ISP,
  2524. "Error with no active/wait request");
  2525. goto end;
  2526. } else {
  2527. req_to_dump = list_first_entry(&ctx->wait_req_list,
  2528. struct cam_ctx_request, list);
  2529. }
  2530. } else {
  2531. req_to_dump = list_first_entry(&ctx->active_req_list,
  2532. struct cam_ctx_request, list);
  2533. }
  2534. req_isp = (struct cam_isp_ctx_req *) req_to_dump->req_priv;
  2535. if (error_event_data->enable_req_dump)
  2536. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  2537. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2538. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR, req_to_dump->request_id);
  2539. list_for_each_entry_safe(req, req_temp,
  2540. &ctx->active_req_list, list) {
  2541. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2542. if (!req_isp->bubble_report) {
  2543. CAM_ERR(CAM_ISP, "signalled error for req %llu",
  2544. req->request_id);
  2545. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2546. fence_map_out =
  2547. &req_isp->fence_map_out[i];
  2548. if (req_isp->fence_map_out[i].sync_id != -1) {
  2549. CAM_DBG(CAM_ISP,
  2550. "req %llu, Sync fd 0x%x ctx %u",
  2551. req->request_id,
  2552. req_isp->fence_map_out[i].sync_id,
  2553. ctx->ctx_id);
  2554. rc = cam_sync_signal(
  2555. fence_map_out->sync_id,
  2556. CAM_SYNC_STATE_SIGNALED_ERROR,
  2557. evt_param);
  2558. fence_map_out->sync_id = -1;
  2559. }
  2560. }
  2561. list_del_init(&req->list);
  2562. list_add_tail(&req->list, &ctx->free_req_list);
  2563. ctx_isp->active_req_cnt--;
  2564. } else {
  2565. found = 1;
  2566. break;
  2567. }
  2568. }
  2569. if (found)
  2570. goto move_to_pending;
  2571. list_for_each_entry_safe(req, req_temp,
  2572. &ctx->wait_req_list, list) {
  2573. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2574. if (!req_isp->bubble_report) {
  2575. CAM_ERR(CAM_ISP, "signalled error for req %llu",
  2576. req->request_id);
  2577. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2578. fence_map_out =
  2579. &req_isp->fence_map_out[i];
  2580. if (req_isp->fence_map_out[i].sync_id != -1) {
  2581. CAM_DBG(CAM_ISP,
  2582. "req %llu, Sync fd 0x%x ctx %u",
  2583. req->request_id,
  2584. req_isp->fence_map_out[i].sync_id,
  2585. ctx->ctx_id);
  2586. rc = cam_sync_signal(
  2587. fence_map_out->sync_id,
  2588. CAM_SYNC_STATE_SIGNALED_ERROR,
  2589. evt_param);
  2590. fence_map_out->sync_id = -1;
  2591. }
  2592. }
  2593. list_del_init(&req->list);
  2594. list_add_tail(&req->list, &ctx->free_req_list);
  2595. } else {
  2596. found = 1;
  2597. break;
  2598. }
  2599. }
  2600. move_to_pending:
  2601. /*
  2602. * If bubble recovery is enabled on any request we need to move that
  2603. * request and all the subsequent requests to the pending list.
  2604. * Note:
  2605. * We need to traverse the active list in reverse order and add
  2606. * to head of pending list.
  2607. * e.g. pending current state: 10, 11 | active current state: 8, 9
  2608. * intermittent for loop iteration- pending: 9, 10, 11 | active: 8
  2609. * final state - pending: 8, 9, 10, 11 | active: NULL
  2610. */
  2611. if (found) {
  2612. list_for_each_entry_safe_reverse(req, req_temp,
  2613. &ctx->active_req_list, list) {
  2614. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2615. list_del_init(&req->list);
  2616. list_add(&req->list, &ctx->pending_req_list);
  2617. ctx_isp->active_req_cnt--;
  2618. }
  2619. list_for_each_entry_safe_reverse(req, req_temp,
  2620. &ctx->wait_req_list, list) {
  2621. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2622. list_del_init(&req->list);
  2623. list_add(&req->list, &ctx->pending_req_list);
  2624. }
  2625. }
  2626. end:
  2627. do {
  2628. if (list_empty(&ctx->pending_req_list)) {
  2629. error_request_id = ctx_isp->last_applied_req_id;
  2630. req_isp = NULL;
  2631. break;
  2632. }
  2633. req = list_first_entry(&ctx->pending_req_list,
  2634. struct cam_ctx_request, list);
  2635. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2636. error_request_id = ctx_isp->last_applied_req_id;
  2637. if (req_isp->bubble_report) {
  2638. req_to_report = req;
  2639. req_isp_to_report = req_to_report->req_priv;
  2640. break;
  2641. }
  2642. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  2643. if (req_isp->fence_map_out[i].sync_id != -1)
  2644. rc = cam_sync_signal(
  2645. req_isp->fence_map_out[i].sync_id,
  2646. CAM_SYNC_STATE_SIGNALED_ERROR,
  2647. evt_param);
  2648. req_isp->fence_map_out[i].sync_id = -1;
  2649. }
  2650. list_del_init(&req->list);
  2651. list_add_tail(&req->list, &ctx->free_req_list);
  2652. } while (req->request_id < ctx_isp->last_applied_req_id);
  2653. if (ctx_isp->offline_context)
  2654. goto exit;
  2655. error = CRM_KMD_ERR_FATAL;
  2656. if (req_isp_to_report && req_isp_to_report->bubble_report)
  2657. if (error_event_data->recovery_enabled)
  2658. error = CRM_KMD_ERR_BUBBLE;
  2659. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, error,
  2660. error_request_id, ctx_isp);
  2661. /*
  2662. * Need to send error occurred in KMD
  2663. * This will help UMD to take necessary action
  2664. * and to dump relevant info
  2665. */
  2666. if (error == CRM_KMD_ERR_FATAL) {
  2667. uint32_t req_mgr_error_type = CAM_REQ_MGR_ERROR_TYPE_RECOVERY;
  2668. if (error_type == CAM_ISP_HW_ERROR_CSID_FATAL)
  2669. req_mgr_error_type =
  2670. CAM_REQ_MGR_ERROR_TYPE_FULL_RECOVERY;
  2671. __cam_isp_ctx_notify_v4l2_error_event(req_mgr_error_type,
  2672. error_event_data->error_code, error_request_id, ctx);
  2673. }
  2674. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HW_ERROR;
  2675. CAM_DBG(CAM_ISP, "Handling error done on ctx: %u", ctx->ctx_id);
  2676. exit:
  2677. return rc;
  2678. }
  2679. static int __cam_isp_ctx_fs2_sof_in_sof_state(
  2680. struct cam_isp_context *ctx_isp, void *evt_data)
  2681. {
  2682. int rc = 0;
  2683. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  2684. struct cam_ctx_request *req;
  2685. struct cam_context *ctx = ctx_isp->base;
  2686. uint64_t request_id = 0;
  2687. if (!evt_data) {
  2688. CAM_ERR(CAM_ISP, "in valid sof event data");
  2689. return -EINVAL;
  2690. }
  2691. ctx_isp->frame_id++;
  2692. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  2693. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  2694. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  2695. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  2696. if (!(list_empty(&ctx->wait_req_list)))
  2697. goto end;
  2698. if (ctx_isp->active_req_cnt <= 2) {
  2699. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2700. list_for_each_entry(req, &ctx->active_req_list, list) {
  2701. if (req->request_id > ctx_isp->reported_req_id) {
  2702. request_id = req->request_id;
  2703. ctx_isp->reported_req_id = request_id;
  2704. break;
  2705. }
  2706. }
  2707. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2708. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2709. }
  2710. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2711. CAM_ISP_STATE_CHANGE_TRIGGER_SOF, request_id);
  2712. end:
  2713. return rc;
  2714. }
  2715. static int __cam_isp_ctx_fs2_buf_done(struct cam_isp_context *ctx_isp,
  2716. void *evt_data)
  2717. {
  2718. int rc = 0;
  2719. struct cam_isp_hw_done_event_data *done =
  2720. (struct cam_isp_hw_done_event_data *) evt_data;
  2721. struct cam_context *ctx = ctx_isp->base;
  2722. int prev_active_req_cnt = 0;
  2723. int curr_req_id = 0;
  2724. struct cam_ctx_request *req;
  2725. prev_active_req_cnt = ctx_isp->active_req_cnt;
  2726. req = list_first_entry(&ctx->active_req_list,
  2727. struct cam_ctx_request, list);
  2728. if (req)
  2729. curr_req_id = req->request_id;
  2730. rc = __cam_isp_ctx_handle_buf_done_in_activated_state(ctx_isp, done, 0);
  2731. if (prev_active_req_cnt == ctx_isp->active_req_cnt + 1) {
  2732. if (list_empty(&ctx->wait_req_list) &&
  2733. list_empty(&ctx->active_req_list)) {
  2734. CAM_DBG(CAM_ISP, "No request, move to SOF");
  2735. ctx_isp->substate_activated =
  2736. CAM_ISP_CTX_ACTIVATED_SOF;
  2737. if (ctx_isp->reported_req_id < curr_req_id) {
  2738. ctx_isp->reported_req_id = curr_req_id;
  2739. __cam_isp_ctx_send_sof_timestamp(ctx_isp,
  2740. curr_req_id,
  2741. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2742. }
  2743. }
  2744. }
  2745. return rc;
  2746. }
  2747. static int __cam_isp_ctx_fs2_buf_done_in_epoch(struct cam_isp_context *ctx_isp,
  2748. void *evt_data)
  2749. {
  2750. int rc = 0;
  2751. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2752. return rc;
  2753. }
  2754. static int __cam_isp_ctx_fs2_buf_done_in_applied(
  2755. struct cam_isp_context *ctx_isp,
  2756. void *evt_data)
  2757. {
  2758. int rc = 0;
  2759. rc = __cam_isp_ctx_fs2_buf_done(ctx_isp, evt_data);
  2760. return rc;
  2761. }
  2762. static int __cam_isp_ctx_fs2_reg_upd_in_sof(struct cam_isp_context *ctx_isp,
  2763. void *evt_data)
  2764. {
  2765. int rc = 0;
  2766. struct cam_ctx_request *req = NULL;
  2767. struct cam_isp_ctx_req *req_isp;
  2768. struct cam_context *ctx = ctx_isp->base;
  2769. if (ctx->state != CAM_CTX_ACTIVATED && ctx_isp->frame_id > 1) {
  2770. CAM_DBG(CAM_ISP, "invalid RUP");
  2771. goto end;
  2772. }
  2773. /*
  2774. * This is for the first update. The initial setting will
  2775. * cause the reg_upd in the first frame.
  2776. */
  2777. if (!list_empty(&ctx->wait_req_list)) {
  2778. req = list_first_entry(&ctx->wait_req_list,
  2779. struct cam_ctx_request, list);
  2780. list_del_init(&req->list);
  2781. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2782. if (req_isp->num_fence_map_out == req_isp->num_acked)
  2783. list_add_tail(&req->list, &ctx->free_req_list);
  2784. else
  2785. CAM_ERR(CAM_ISP,
  2786. "receive rup in unexpected state");
  2787. }
  2788. if (req != NULL) {
  2789. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2790. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2791. req->request_id);
  2792. }
  2793. end:
  2794. return rc;
  2795. }
  2796. static int __cam_isp_ctx_fs2_reg_upd_in_applied_state(
  2797. struct cam_isp_context *ctx_isp, void *evt_data)
  2798. {
  2799. int rc = 0;
  2800. struct cam_ctx_request *req = NULL;
  2801. struct cam_context *ctx = ctx_isp->base;
  2802. struct cam_isp_ctx_req *req_isp;
  2803. uint64_t request_id = 0;
  2804. if (list_empty(&ctx->wait_req_list)) {
  2805. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  2806. goto end;
  2807. }
  2808. req = list_first_entry(&ctx->wait_req_list,
  2809. struct cam_ctx_request, list);
  2810. list_del_init(&req->list);
  2811. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  2812. if (req_isp->num_fence_map_out != 0) {
  2813. list_add_tail(&req->list, &ctx->active_req_list);
  2814. ctx_isp->active_req_cnt++;
  2815. CAM_DBG(CAM_REQ, "move request %lld to active list(cnt = %d)",
  2816. req->request_id, ctx_isp->active_req_cnt);
  2817. } else {
  2818. /* no io config, so the request is completed. */
  2819. list_add_tail(&req->list, &ctx->free_req_list);
  2820. }
  2821. /*
  2822. * This function only called directly from applied and bubble applied
  2823. * state so change substate here.
  2824. */
  2825. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  2826. if (req_isp->num_fence_map_out != 1)
  2827. goto end;
  2828. if (ctx_isp->active_req_cnt <= 2) {
  2829. list_for_each_entry(req, &ctx->active_req_list, list) {
  2830. if (req->request_id > ctx_isp->reported_req_id) {
  2831. request_id = req->request_id;
  2832. ctx_isp->reported_req_id = request_id;
  2833. break;
  2834. }
  2835. }
  2836. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  2837. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  2838. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  2839. }
  2840. CAM_DBG(CAM_ISP, "next Substate[%s]",
  2841. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated));
  2842. end:
  2843. if (req != NULL && !rc) {
  2844. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2845. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  2846. req->request_id);
  2847. }
  2848. return rc;
  2849. }
  2850. static void __cam_isp_ctx_notify_aeb_error_for_sec_event(
  2851. struct cam_isp_context *ctx_isp)
  2852. {
  2853. struct cam_context *ctx = ctx_isp->base;
  2854. CAM_ERR(CAM_ISP,
  2855. "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",
  2856. ctx->ctx_id, ctx->link_hdl);
  2857. /* Pause CRM timer */
  2858. __cam_isp_ctx_pause_crm_timer(ctx);
  2859. /* Trigger reg dump */
  2860. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  2861. /* Notify CRM on fatal error */
  2862. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_FATAL,
  2863. ctx_isp->last_applied_req_id, ctx_isp);
  2864. /* Notify userland on error */
  2865. __cam_isp_ctx_notify_v4l2_error_event(CAM_REQ_MGR_ERROR_TYPE_RECOVERY,
  2866. CAM_REQ_MGR_CSID_ERR_ON_SENSOR_SWITCHING, ctx_isp->last_applied_req_id, ctx);
  2867. /* Change state to HALT, stop further processing of HW events */
  2868. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  2869. }
  2870. static int __cam_isp_ctx_trigger_internal_recovery(
  2871. bool sync_frame_drop, struct cam_isp_context *ctx_isp)
  2872. {
  2873. int rc = 0;
  2874. bool do_recovery = true;
  2875. struct cam_context *ctx = ctx_isp->base;
  2876. struct cam_ctx_request *req = NULL;
  2877. struct cam_isp_ctx_req *req_isp = NULL;
  2878. if (list_empty(&ctx->wait_req_list)) {
  2879. /*
  2880. * If the wait list is empty, and we encounter a "silent" frame drop
  2881. * then the settings applied on the previous frame, did not reflect
  2882. * at the next frame boundary, it's expected to latch a frame after.
  2883. * No need to recover. If it's an out of sync drop use pending req
  2884. */
  2885. if (sync_frame_drop && !list_empty(&ctx->pending_req_list))
  2886. req = list_first_entry(&ctx->pending_req_list,
  2887. struct cam_ctx_request, list);
  2888. else
  2889. do_recovery = false;
  2890. }
  2891. /* If both wait and pending list have no request to recover on */
  2892. if (!do_recovery) {
  2893. CAM_WARN(CAM_ISP,
  2894. "No request to perform recovery - ctx: %u on link: 0x%x last_applied: %lld last_buf_done: %lld",
  2895. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id,
  2896. ctx_isp->req_info.last_bufdone_req_id);
  2897. goto end;
  2898. }
  2899. if (!req) {
  2900. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request, list);
  2901. if (req->request_id != ctx_isp->last_applied_req_id)
  2902. CAM_WARN(CAM_ISP,
  2903. "Top of wait list req: %llu does not match with last applied: %llu in ctx: %u on link: 0x%x",
  2904. req->request_id, ctx_isp->last_applied_req_id,
  2905. ctx->ctx_id, ctx->link_hdl);
  2906. }
  2907. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  2908. /*
  2909. * Treat this as bubble, after recovery re-start from appropriate sub-state
  2910. * This will block servicing any further apply calls from CRM
  2911. */
  2912. atomic_set(&ctx_isp->internal_recovery_set, 1);
  2913. atomic_set(&ctx_isp->process_bubble, 1);
  2914. ctx_isp->recovery_req_id = req->request_id;
  2915. /* Wait for active request's to finish before issuing recovery */
  2916. if (ctx_isp->active_req_cnt) {
  2917. req_isp->bubble_detected = true;
  2918. CAM_WARN(CAM_ISP,
  2919. "Active req cnt: %u wait for all buf dones before kicking in recovery on req: %lld ctx: %u on link: 0x%x",
  2920. ctx_isp->active_req_cnt, ctx_isp->recovery_req_id,
  2921. ctx->ctx_id, ctx->link_hdl);
  2922. } else {
  2923. rc = __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  2924. ctx_isp->recovery_req_id, ctx_isp);
  2925. if (rc) {
  2926. /* Unable to do bubble recovery reset back to normal */
  2927. CAM_WARN(CAM_ISP,
  2928. "Unable to perform internal recovery [bubble reporting failed] for req: %llu in ctx: %u on link: 0x%x",
  2929. ctx_isp->recovery_req_id, ctx->ctx_id, ctx->link_hdl);
  2930. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  2931. goto end;
  2932. }
  2933. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  2934. list_del_init(&req->list);
  2935. list_add(&req->list, &ctx->pending_req_list);
  2936. }
  2937. end:
  2938. return rc;
  2939. }
  2940. static int __cam_isp_ctx_handle_secondary_events(
  2941. struct cam_isp_context *ctx_isp, void *evt_data)
  2942. {
  2943. int rc = 0;
  2944. bool recover = false, sync_frame_drop = false;
  2945. struct cam_context *ctx = ctx_isp->base;
  2946. struct cam_isp_hw_secondary_event_data *sec_evt_data =
  2947. (struct cam_isp_hw_secondary_event_data *)evt_data;
  2948. /* Current scheme to handle only for custom AEB */
  2949. if (!ctx_isp->aeb_enabled) {
  2950. CAM_WARN(CAM_ISP,
  2951. "Recovery not supported for non-AEB ctx: %u on link: 0x%x reject sec evt: %u",
  2952. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  2953. goto end;
  2954. }
  2955. if (atomic_read(&ctx_isp->internal_recovery_set)) {
  2956. CAM_WARN(CAM_ISP,
  2957. "Internal recovery in progress in ctx: %u on link: 0x%x reject sec evt: %u",
  2958. ctx->ctx_id, ctx->link_hdl, sec_evt_data->evt_type);
  2959. goto end;
  2960. }
  2961. /*
  2962. * In case of custom AEB ensure first exposure frame has
  2963. * not moved forward with its settings without second/third
  2964. * expoure frame coming in. Also track for bubble, in case of system
  2965. * delays it's possible for the IFE settings to be not written to
  2966. * HW on a given frame. If these scenarios occurs flag as error,
  2967. * and recover.
  2968. */
  2969. switch (sec_evt_data->evt_type) {
  2970. case CAM_ISP_HW_SEC_EVENT_SOF:
  2971. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2972. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  2973. ctx_isp->last_applied_req_id);
  2974. /* Slave RDI's frame starting post IFE EPOCH - Fatal */
  2975. if ((ctx_isp->substate_activated ==
  2976. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  2977. (ctx_isp->substate_activated ==
  2978. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED))
  2979. __cam_isp_ctx_notify_aeb_error_for_sec_event(ctx_isp);
  2980. break;
  2981. case CAM_ISP_HW_SEC_EVENT_EPOCH:
  2982. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  2983. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  2984. ctx_isp->last_applied_req_id);
  2985. /*
  2986. * Master RDI frame dropped in CSID, due to programming delay no RUP/AUP
  2987. * On such occasions use CSID CAMIF EPOCH for bubble detection, flag
  2988. * on detection and perform necessary bubble recovery
  2989. */
  2990. if ((ctx_isp->substate_activated ==
  2991. CAM_ISP_CTX_ACTIVATED_APPLIED) ||
  2992. (ctx_isp->substate_activated ==
  2993. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED)) {
  2994. recover = true;
  2995. CAM_WARN(CAM_ISP,
  2996. "Programming delay input frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  2997. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  2998. }
  2999. break;
  3000. case CAM_ISP_HW_SEC_EVENT_OUT_OF_SYNC_FRAME_DROP:
  3001. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3002. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  3003. ctx_isp->last_applied_req_id);
  3004. /* Avoid recovery loop if frame is dropped at stream on */
  3005. if (!ctx_isp->frame_id) {
  3006. CAM_ERR(CAM_ISP,
  3007. "Sensor sync [vc mismatch] frame dropped at stream on ctx: %u link: 0x%x frame_id: %u last_applied_req: %lld",
  3008. ctx->ctx_id, ctx->link_hdl,
  3009. ctx_isp->frame_id, ctx_isp->last_applied_req_id);
  3010. rc = -EPERM;
  3011. break;
  3012. }
  3013. recover = true;
  3014. sync_frame_drop = true;
  3015. CAM_WARN(CAM_ISP,
  3016. "Sensor sync [vc mismatch] frame dropped ctx: %u on link: 0x%x last_applied_req: %llu, kicking in internal recovery....",
  3017. ctx->ctx_id, ctx->link_hdl, ctx_isp->last_applied_req_id);
  3018. break;
  3019. default:
  3020. break;
  3021. }
  3022. if (recover && ctx_isp->do_internal_recovery)
  3023. rc = __cam_isp_ctx_trigger_internal_recovery(sync_frame_drop, ctx_isp);
  3024. end:
  3025. return rc;
  3026. }
  3027. static struct cam_isp_ctx_irq_ops
  3028. cam_isp_ctx_activated_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3029. /* SOF */
  3030. {
  3031. .irq_ops = {
  3032. __cam_isp_ctx_handle_error,
  3033. __cam_isp_ctx_sof_in_activated_state,
  3034. __cam_isp_ctx_reg_upd_in_sof,
  3035. __cam_isp_ctx_notify_sof_in_activated_state,
  3036. __cam_isp_ctx_notify_eof_in_activated_state,
  3037. __cam_isp_ctx_buf_done_in_sof,
  3038. __cam_isp_ctx_handle_secondary_events,
  3039. },
  3040. },
  3041. /* APPLIED */
  3042. {
  3043. .irq_ops = {
  3044. __cam_isp_ctx_handle_error,
  3045. __cam_isp_ctx_sof_in_activated_state,
  3046. __cam_isp_ctx_reg_upd_in_applied_state,
  3047. __cam_isp_ctx_epoch_in_applied,
  3048. __cam_isp_ctx_notify_eof_in_activated_state,
  3049. __cam_isp_ctx_buf_done_in_applied,
  3050. __cam_isp_ctx_handle_secondary_events,
  3051. },
  3052. },
  3053. /* EPOCH */
  3054. {
  3055. .irq_ops = {
  3056. __cam_isp_ctx_handle_error,
  3057. __cam_isp_ctx_sof_in_epoch,
  3058. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3059. __cam_isp_ctx_notify_sof_in_activated_state,
  3060. __cam_isp_ctx_notify_eof_in_activated_state,
  3061. __cam_isp_ctx_buf_done_in_epoch,
  3062. __cam_isp_ctx_handle_secondary_events,
  3063. },
  3064. },
  3065. /* BUBBLE */
  3066. {
  3067. .irq_ops = {
  3068. __cam_isp_ctx_handle_error,
  3069. __cam_isp_ctx_sof_in_activated_state,
  3070. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3071. __cam_isp_ctx_notify_sof_in_activated_state,
  3072. __cam_isp_ctx_notify_eof_in_activated_state,
  3073. __cam_isp_ctx_buf_done_in_bubble,
  3074. __cam_isp_ctx_handle_secondary_events,
  3075. },
  3076. },
  3077. /* Bubble Applied */
  3078. {
  3079. .irq_ops = {
  3080. __cam_isp_ctx_handle_error,
  3081. __cam_isp_ctx_sof_in_activated_state,
  3082. __cam_isp_ctx_reg_upd_in_applied_state,
  3083. __cam_isp_ctx_epoch_in_bubble_applied,
  3084. NULL,
  3085. __cam_isp_ctx_buf_done_in_bubble_applied,
  3086. __cam_isp_ctx_handle_secondary_events,
  3087. },
  3088. },
  3089. /* HW ERROR */
  3090. {
  3091. .irq_ops = {
  3092. NULL,
  3093. __cam_isp_ctx_sof_in_activated_state,
  3094. __cam_isp_ctx_reg_upd_in_hw_error,
  3095. NULL,
  3096. NULL,
  3097. NULL,
  3098. },
  3099. },
  3100. /* HALT */
  3101. {
  3102. },
  3103. };
  3104. static struct cam_isp_ctx_irq_ops
  3105. cam_isp_ctx_fs2_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3106. /* SOF */
  3107. {
  3108. .irq_ops = {
  3109. __cam_isp_ctx_handle_error,
  3110. __cam_isp_ctx_fs2_sof_in_sof_state,
  3111. __cam_isp_ctx_fs2_reg_upd_in_sof,
  3112. __cam_isp_ctx_fs2_sof_in_sof_state,
  3113. __cam_isp_ctx_notify_eof_in_activated_state,
  3114. NULL,
  3115. },
  3116. },
  3117. /* APPLIED */
  3118. {
  3119. .irq_ops = {
  3120. __cam_isp_ctx_handle_error,
  3121. __cam_isp_ctx_sof_in_activated_state,
  3122. __cam_isp_ctx_fs2_reg_upd_in_applied_state,
  3123. __cam_isp_ctx_epoch_in_applied,
  3124. __cam_isp_ctx_notify_eof_in_activated_state,
  3125. __cam_isp_ctx_fs2_buf_done_in_applied,
  3126. },
  3127. },
  3128. /* EPOCH */
  3129. {
  3130. .irq_ops = {
  3131. __cam_isp_ctx_handle_error,
  3132. __cam_isp_ctx_sof_in_epoch,
  3133. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3134. __cam_isp_ctx_notify_sof_in_activated_state,
  3135. __cam_isp_ctx_notify_eof_in_activated_state,
  3136. __cam_isp_ctx_fs2_buf_done_in_epoch,
  3137. },
  3138. },
  3139. /* BUBBLE */
  3140. {
  3141. .irq_ops = {
  3142. __cam_isp_ctx_handle_error,
  3143. __cam_isp_ctx_sof_in_activated_state,
  3144. __cam_isp_ctx_reg_upd_in_epoch_bubble_state,
  3145. __cam_isp_ctx_notify_sof_in_activated_state,
  3146. __cam_isp_ctx_notify_eof_in_activated_state,
  3147. __cam_isp_ctx_buf_done_in_bubble,
  3148. },
  3149. },
  3150. /* Bubble 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. __cam_isp_ctx_epoch_in_bubble_applied,
  3157. NULL,
  3158. __cam_isp_ctx_buf_done_in_bubble_applied,
  3159. },
  3160. },
  3161. /* HW ERROR */
  3162. {
  3163. .irq_ops = {
  3164. NULL,
  3165. __cam_isp_ctx_sof_in_activated_state,
  3166. __cam_isp_ctx_reg_upd_in_hw_error,
  3167. NULL,
  3168. NULL,
  3169. NULL,
  3170. },
  3171. },
  3172. /* HALT */
  3173. {
  3174. },
  3175. };
  3176. static struct cam_isp_ctx_irq_ops
  3177. cam_isp_ctx_offline_state_machine_irq[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3178. /* SOF */
  3179. {
  3180. .irq_ops = {
  3181. __cam_isp_ctx_handle_error,
  3182. NULL,
  3183. NULL,
  3184. NULL,
  3185. NULL,
  3186. NULL,
  3187. },
  3188. },
  3189. /* APPLIED */
  3190. {
  3191. .irq_ops = {
  3192. __cam_isp_ctx_handle_error,
  3193. __cam_isp_ctx_sof_in_activated_state,
  3194. __cam_isp_ctx_reg_upd_in_applied_state,
  3195. NULL,
  3196. NULL,
  3197. __cam_isp_ctx_buf_done_in_applied,
  3198. },
  3199. },
  3200. /* EPOCH */
  3201. {
  3202. .irq_ops = {
  3203. __cam_isp_ctx_handle_error,
  3204. NULL,
  3205. NULL,
  3206. __cam_isp_ctx_offline_epoch_in_activated_state,
  3207. NULL,
  3208. __cam_isp_ctx_buf_done_in_epoch,
  3209. },
  3210. },
  3211. /* BUBBLE */
  3212. {
  3213. },
  3214. /* Bubble Applied */
  3215. {
  3216. },
  3217. /* HW ERROR */
  3218. {
  3219. .irq_ops = {
  3220. NULL,
  3221. __cam_isp_ctx_sof_in_activated_state,
  3222. __cam_isp_ctx_reg_upd_in_hw_error,
  3223. NULL,
  3224. NULL,
  3225. NULL,
  3226. },
  3227. },
  3228. /* HALT */
  3229. {
  3230. },
  3231. };
  3232. static int __cam_isp_ctx_apply_req_in_activated_state(
  3233. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply,
  3234. enum cam_isp_ctx_activated_substate next_state)
  3235. {
  3236. int rc = 0;
  3237. struct cam_ctx_request *req;
  3238. struct cam_ctx_request *active_req = NULL;
  3239. struct cam_isp_ctx_req *req_isp;
  3240. struct cam_isp_ctx_req *active_req_isp;
  3241. struct cam_isp_context *ctx_isp = NULL;
  3242. struct cam_hw_config_args cfg = {0};
  3243. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3244. if (apply->re_apply)
  3245. if (apply->request_id <= ctx_isp->last_applied_req_id) {
  3246. CAM_INFO_RATE_LIMIT(CAM_ISP,
  3247. "ctx_id:%d Trying to reapply the same request %llu again",
  3248. ctx->ctx_id,
  3249. apply->request_id);
  3250. return 0;
  3251. }
  3252. if (list_empty(&ctx->pending_req_list)) {
  3253. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3254. "ctx_id:%d No available request for Apply id %lld",
  3255. ctx->ctx_id,
  3256. apply->request_id);
  3257. rc = -EFAULT;
  3258. goto end;
  3259. }
  3260. /*
  3261. * When the pipeline has issue, the requests can be queued up in the
  3262. * pipeline. In this case, we should reject the additional request.
  3263. * The maximum number of request allowed to be outstanding is 2.
  3264. *
  3265. */
  3266. if (atomic_read(&ctx_isp->process_bubble)) {
  3267. CAM_INFO_RATE_LIMIT(CAM_ISP,
  3268. "ctx_id:%d Processing bubble cannot apply Request Id %llu",
  3269. ctx->ctx_id,
  3270. apply->request_id);
  3271. rc = -EAGAIN;
  3272. goto end;
  3273. }
  3274. spin_lock_bh(&ctx->lock);
  3275. req = list_first_entry(&ctx->pending_req_list, struct cam_ctx_request,
  3276. list);
  3277. spin_unlock_bh(&ctx->lock);
  3278. /*
  3279. * Check whether the request id is matching the tip, if not, this means
  3280. * we are in the middle of the error handling. Need to reject this apply
  3281. */
  3282. if (req->request_id != apply->request_id) {
  3283. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3284. "ctx_id:%d Invalid Request Id asking %llu existing %llu",
  3285. ctx->ctx_id,
  3286. apply->request_id, req->request_id);
  3287. rc = -EFAULT;
  3288. goto end;
  3289. }
  3290. CAM_DBG(CAM_REQ, "Apply request %lld in Substate[%s] ctx %u",
  3291. req->request_id,
  3292. __cam_isp_ctx_substate_val_to_type(ctx_isp->substate_activated),
  3293. ctx->ctx_id);
  3294. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3295. if (ctx_isp->active_req_cnt >= 2) {
  3296. CAM_WARN_RATE_LIMIT(CAM_ISP,
  3297. "Reject apply request (id %lld) due to congestion(cnt = %d) ctx %u",
  3298. req->request_id,
  3299. ctx_isp->active_req_cnt,
  3300. ctx->ctx_id);
  3301. spin_lock_bh(&ctx->lock);
  3302. if (!list_empty(&ctx->active_req_list))
  3303. active_req = list_first_entry(&ctx->active_req_list,
  3304. struct cam_ctx_request, list);
  3305. else
  3306. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3307. "WARNING: should not happen (cnt = %d) but active_list empty",
  3308. ctx_isp->active_req_cnt);
  3309. spin_unlock_bh(&ctx->lock);
  3310. if (active_req) {
  3311. active_req_isp =
  3312. (struct cam_isp_ctx_req *) active_req->req_priv;
  3313. __cam_isp_ctx_handle_buf_done_fail_log(
  3314. active_req->request_id, active_req_isp,
  3315. ctx_isp->isp_device_type);
  3316. }
  3317. rc = -EFAULT;
  3318. goto end;
  3319. }
  3320. req_isp->bubble_report = apply->report_if_bubble;
  3321. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3322. cfg.request_id = req->request_id;
  3323. cfg.hw_update_entries = req_isp->cfg;
  3324. cfg.num_hw_update_entries = req_isp->num_cfg;
  3325. cfg.priv = &req_isp->hw_update_data;
  3326. cfg.init_packet = 0;
  3327. cfg.reapply_type = req_isp->reapply_type;
  3328. cfg.cdm_reset_before_apply = req_isp->cdm_reset_before_apply;
  3329. atomic_set(&ctx_isp->apply_in_progress, 1);
  3330. rc = ctx->hw_mgr_intf->hw_config(ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  3331. if (!rc) {
  3332. spin_lock_bh(&ctx->lock);
  3333. ctx_isp->substate_activated = next_state;
  3334. ctx_isp->last_applied_req_id = apply->request_id;
  3335. list_del_init(&req->list);
  3336. list_add_tail(&req->list, &ctx->wait_req_list);
  3337. CAM_DBG(CAM_ISP, "new substate Substate[%s], applied req %lld",
  3338. __cam_isp_ctx_substate_val_to_type(next_state),
  3339. ctx_isp->last_applied_req_id);
  3340. spin_unlock_bh(&ctx->lock);
  3341. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3342. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  3343. req->request_id);
  3344. __cam_isp_ctx_update_event_record(ctx_isp,
  3345. CAM_ISP_CTX_EVENT_APPLY, req);
  3346. } else if (rc == -EALREADY) {
  3347. spin_lock_bh(&ctx->lock);
  3348. req_isp->bubble_detected = true;
  3349. req_isp->cdm_reset_before_apply = false;
  3350. atomic_set(&ctx_isp->process_bubble, 1);
  3351. list_del_init(&req->list);
  3352. list_add(&req->list, &ctx->active_req_list);
  3353. ctx_isp->active_req_cnt++;
  3354. spin_unlock_bh(&ctx->lock);
  3355. CAM_DBG(CAM_REQ,
  3356. "move request %lld to active list(cnt = %d), ctx %u",
  3357. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  3358. } else {
  3359. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3360. "ctx_id:%d ,Can not apply (req %lld) the configuration, rc %d",
  3361. ctx->ctx_id, apply->request_id, rc);
  3362. }
  3363. atomic_set(&ctx_isp->apply_in_progress, 0);
  3364. end:
  3365. return rc;
  3366. }
  3367. static int __cam_isp_ctx_apply_req_in_sof(
  3368. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3369. {
  3370. int rc = 0;
  3371. struct cam_isp_context *ctx_isp =
  3372. (struct cam_isp_context *) ctx->ctx_priv;
  3373. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3374. __cam_isp_ctx_substate_val_to_type(
  3375. ctx_isp->substate_activated));
  3376. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3377. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3378. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3379. __cam_isp_ctx_substate_val_to_type(
  3380. ctx_isp->substate_activated));
  3381. if (rc)
  3382. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3383. __cam_isp_ctx_substate_val_to_type(
  3384. ctx_isp->substate_activated), rc);
  3385. return rc;
  3386. }
  3387. static int __cam_isp_ctx_apply_req_in_epoch(
  3388. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3389. {
  3390. int rc = 0;
  3391. struct cam_isp_context *ctx_isp =
  3392. (struct cam_isp_context *) ctx->ctx_priv;
  3393. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3394. __cam_isp_ctx_substate_val_to_type(
  3395. ctx_isp->substate_activated));
  3396. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3397. CAM_ISP_CTX_ACTIVATED_APPLIED);
  3398. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3399. __cam_isp_ctx_substate_val_to_type(
  3400. ctx_isp->substate_activated));
  3401. if (rc)
  3402. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3403. __cam_isp_ctx_substate_val_to_type(
  3404. ctx_isp->substate_activated), rc);
  3405. return rc;
  3406. }
  3407. static int __cam_isp_ctx_apply_req_in_bubble(
  3408. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3409. {
  3410. int rc = 0;
  3411. struct cam_isp_context *ctx_isp =
  3412. (struct cam_isp_context *) ctx->ctx_priv;
  3413. CAM_DBG(CAM_ISP, "current Substate[%s]",
  3414. __cam_isp_ctx_substate_val_to_type(
  3415. ctx_isp->substate_activated));
  3416. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  3417. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED);
  3418. CAM_DBG(CAM_ISP, "new Substate[%s]",
  3419. __cam_isp_ctx_substate_val_to_type(
  3420. ctx_isp->substate_activated));
  3421. if (rc)
  3422. CAM_DBG(CAM_ISP, "Apply failed in Substate[%s], rc %d",
  3423. __cam_isp_ctx_substate_val_to_type(
  3424. ctx_isp->substate_activated), rc);
  3425. return rc;
  3426. }
  3427. static int __cam_isp_ctx_apply_default_req_settings(
  3428. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  3429. {
  3430. int rc = 0;
  3431. struct cam_isp_context *isp_ctx =
  3432. (struct cam_isp_context *) ctx->ctx_priv;
  3433. struct cam_hw_cmd_args hw_cmd_args;
  3434. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  3435. hw_cmd_args.ctxt_to_hw_map = isp_ctx->hw_ctx;
  3436. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  3437. isp_hw_cmd_args.cmd_type =
  3438. CAM_ISP_HW_MGR_CMD_PROG_DEFAULT_CFG;
  3439. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  3440. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  3441. &hw_cmd_args);
  3442. if (rc)
  3443. CAM_ERR(CAM_ISP,
  3444. "Failed to apply default settings rc %d", rc);
  3445. else
  3446. CAM_DBG(CAM_ISP, "Applied default settings rc %d", rc);
  3447. return rc;
  3448. }
  3449. static int __cam_isp_ctx_dump_req_info(
  3450. struct cam_context *ctx,
  3451. struct cam_ctx_request *req,
  3452. uintptr_t cpu_addr,
  3453. size_t buf_len,
  3454. size_t *offset)
  3455. {
  3456. int i, rc;
  3457. uint8_t *dst;
  3458. int32_t *addr, *start;
  3459. uint32_t min_len;
  3460. size_t remain_len;
  3461. struct cam_isp_ctx_req *req_isp;
  3462. struct cam_isp_context *ctx_isp;
  3463. struct cam_isp_context_dump_header *hdr;
  3464. if (!req || !ctx || !offset || !cpu_addr || !buf_len) {
  3465. CAM_ERR(CAM_ISP, "Invalid parameters %pK %pK %pK %zu",
  3466. req, ctx, offset, buf_len);
  3467. return -EINVAL;
  3468. }
  3469. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  3470. ctx_isp = (struct cam_isp_context *)ctx->ctx_priv;
  3471. if (buf_len <= *offset) {
  3472. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3473. buf_len, *offset);
  3474. return -ENOSPC;
  3475. }
  3476. remain_len = buf_len - *offset;
  3477. min_len = sizeof(struct cam_isp_context_dump_header) +
  3478. (CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS *
  3479. req_isp->num_fence_map_out *
  3480. sizeof(int32_t));
  3481. if (remain_len < min_len) {
  3482. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3483. remain_len, min_len);
  3484. return -ENOSPC;
  3485. }
  3486. dst = (uint8_t *)cpu_addr + *offset;
  3487. hdr = (struct cam_isp_context_dump_header *)dst;
  3488. hdr->word_size = sizeof(int32_t);
  3489. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3490. "ISP_OUT_FENCE:");
  3491. addr = (int32_t *)(dst + sizeof(struct cam_isp_context_dump_header));
  3492. start = addr;
  3493. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3494. *addr++ = req_isp->fence_map_out[i].resource_handle;
  3495. *addr++ = req_isp->fence_map_out[i].sync_id;
  3496. }
  3497. hdr->size = hdr->word_size * (addr - start);
  3498. *offset += hdr->size + sizeof(struct cam_isp_context_dump_header);
  3499. rc = cam_isp_ctx_dump_req(req_isp, cpu_addr, buf_len,
  3500. offset, true);
  3501. return rc;
  3502. }
  3503. static int __cam_isp_ctx_dump_in_top_state(
  3504. struct cam_context *ctx,
  3505. struct cam_req_mgr_dump_info *dump_info)
  3506. {
  3507. int rc = 0;
  3508. bool dump_only_event_record = false;
  3509. size_t buf_len;
  3510. size_t remain_len;
  3511. uint8_t *dst;
  3512. ktime_t cur_time;
  3513. uint32_t min_len;
  3514. uint64_t diff;
  3515. uint64_t *addr, *start;
  3516. uintptr_t cpu_addr;
  3517. struct timespec64 ts;
  3518. struct cam_isp_context *ctx_isp;
  3519. struct cam_ctx_request *req = NULL;
  3520. struct cam_isp_ctx_req *req_isp;
  3521. struct cam_ctx_request *req_temp;
  3522. struct cam_hw_dump_args dump_args;
  3523. struct cam_isp_context_dump_header *hdr;
  3524. spin_lock_bh(&ctx->lock);
  3525. list_for_each_entry_safe(req, req_temp,
  3526. &ctx->active_req_list, list) {
  3527. if (req->request_id == dump_info->req_id) {
  3528. CAM_INFO(CAM_ISP, "isp dump active list req: %lld",
  3529. dump_info->req_id);
  3530. goto hw_dump;
  3531. }
  3532. }
  3533. list_for_each_entry_safe(req, req_temp,
  3534. &ctx->wait_req_list, list) {
  3535. if (req->request_id == dump_info->req_id) {
  3536. CAM_INFO(CAM_ISP, "isp dump wait list req: %lld",
  3537. dump_info->req_id);
  3538. goto hw_dump;
  3539. }
  3540. }
  3541. spin_unlock_bh(&ctx->lock);
  3542. return rc;
  3543. hw_dump:
  3544. rc = cam_mem_get_cpu_buf(dump_info->buf_handle,
  3545. &cpu_addr, &buf_len);
  3546. if (rc) {
  3547. CAM_ERR(CAM_ISP, "Invalid handle %u rc %d",
  3548. dump_info->buf_handle, rc);
  3549. spin_unlock_bh(&ctx->lock);
  3550. return rc;
  3551. }
  3552. if (buf_len <= dump_info->offset) {
  3553. spin_unlock_bh(&ctx->lock);
  3554. CAM_WARN(CAM_ISP, "Dump buffer overshoot len %zu offset %zu",
  3555. buf_len, dump_info->offset);
  3556. return -ENOSPC;
  3557. }
  3558. remain_len = buf_len - dump_info->offset;
  3559. min_len = sizeof(struct cam_isp_context_dump_header) +
  3560. (CAM_ISP_CTX_DUMP_NUM_WORDS * sizeof(uint64_t));
  3561. if (remain_len < min_len) {
  3562. spin_unlock_bh(&ctx->lock);
  3563. CAM_WARN(CAM_ISP, "Dump buffer exhaust remain %zu min %u",
  3564. remain_len, min_len);
  3565. return -ENOSPC;
  3566. }
  3567. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3568. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3569. cur_time = ktime_get();
  3570. diff = ktime_us_delta(
  3571. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY],
  3572. cur_time);
  3573. if (diff < CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD) {
  3574. CAM_INFO(CAM_ISP, "req %lld found no error",
  3575. req->request_id);
  3576. dump_only_event_record = true;
  3577. }
  3578. dst = (uint8_t *)cpu_addr + dump_info->offset;
  3579. hdr = (struct cam_isp_context_dump_header *)dst;
  3580. scnprintf(hdr->tag, CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN,
  3581. "ISP_CTX_DUMP:");
  3582. hdr->word_size = sizeof(uint64_t);
  3583. addr = (uint64_t *)(dst +
  3584. sizeof(struct cam_isp_context_dump_header));
  3585. start = addr;
  3586. *addr++ = req->request_id;
  3587. ts = ktime_to_timespec64(
  3588. req_isp->event_timestamp[CAM_ISP_CTX_EVENT_APPLY]);
  3589. *addr++ = ts.tv_sec;
  3590. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3591. ts = ktime_to_timespec64(cur_time);
  3592. *addr++ = ts.tv_sec;
  3593. *addr++ = ts.tv_nsec/NSEC_PER_USEC;
  3594. hdr->size = hdr->word_size * (addr - start);
  3595. dump_info->offset += hdr->size +
  3596. sizeof(struct cam_isp_context_dump_header);
  3597. rc = __cam_isp_ctx_dump_event_record(ctx_isp, cpu_addr,
  3598. buf_len, &dump_info->offset);
  3599. if (rc) {
  3600. CAM_ERR(CAM_ISP, "Dump event fail %lld",
  3601. req->request_id);
  3602. spin_unlock_bh(&ctx->lock);
  3603. return rc;
  3604. }
  3605. if (dump_only_event_record) {
  3606. spin_unlock_bh(&ctx->lock);
  3607. return rc;
  3608. }
  3609. rc = __cam_isp_ctx_dump_req_info(ctx, req, cpu_addr,
  3610. buf_len, &dump_info->offset);
  3611. if (rc) {
  3612. CAM_ERR(CAM_ISP, "Dump Req info fail %lld",
  3613. req->request_id);
  3614. spin_unlock_bh(&ctx->lock);
  3615. return rc;
  3616. }
  3617. spin_unlock_bh(&ctx->lock);
  3618. if (ctx->hw_mgr_intf->hw_dump) {
  3619. dump_args.offset = dump_info->offset;
  3620. dump_args.request_id = dump_info->req_id;
  3621. dump_args.buf_handle = dump_info->buf_handle;
  3622. dump_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3623. rc = ctx->hw_mgr_intf->hw_dump(
  3624. ctx->hw_mgr_intf->hw_mgr_priv,
  3625. &dump_args);
  3626. dump_info->offset = dump_args.offset;
  3627. }
  3628. return rc;
  3629. }
  3630. static int __cam_isp_ctx_flush_req(struct cam_context *ctx,
  3631. struct list_head *req_list, struct cam_req_mgr_flush_request *flush_req)
  3632. {
  3633. int i, rc, tmp = 0;
  3634. uint32_t cancel_req_id_found = 0;
  3635. struct cam_ctx_request *req;
  3636. struct cam_ctx_request *req_temp;
  3637. struct cam_isp_ctx_req *req_isp;
  3638. struct list_head flush_list;
  3639. struct cam_isp_context *ctx_isp = NULL;
  3640. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3641. INIT_LIST_HEAD(&flush_list);
  3642. if (list_empty(req_list)) {
  3643. CAM_DBG(CAM_ISP, "request list is empty");
  3644. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3645. CAM_INFO(CAM_ISP, "no request to cancel (last applied:%lld cancel:%lld)",
  3646. ctx_isp->last_applied_req_id, flush_req->req_id);
  3647. return -EINVAL;
  3648. } else
  3649. return 0;
  3650. }
  3651. CAM_DBG(CAM_REQ, "Flush [%u] in progress for req_id %llu",
  3652. flush_req->type, flush_req->req_id);
  3653. list_for_each_entry_safe(req, req_temp, req_list, list) {
  3654. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ) {
  3655. if (req->request_id != flush_req->req_id) {
  3656. continue;
  3657. } else {
  3658. list_del_init(&req->list);
  3659. list_add_tail(&req->list, &flush_list);
  3660. cancel_req_id_found = 1;
  3661. __cam_isp_ctx_update_state_monitor_array(
  3662. ctx_isp,
  3663. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  3664. req->request_id);
  3665. break;
  3666. }
  3667. }
  3668. list_del_init(&req->list);
  3669. list_add_tail(&req->list, &flush_list);
  3670. __cam_isp_ctx_update_state_monitor_array(ctx_isp,
  3671. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH, req->request_id);
  3672. }
  3673. if (list_empty(&flush_list)) {
  3674. /*
  3675. * Maybe the req isn't sent to KMD since UMD already skip
  3676. * req in CSL layer.
  3677. */
  3678. CAM_INFO(CAM_ISP,
  3679. "flush list is empty, flush type %d for req %llu",
  3680. flush_req->type, flush_req->req_id);
  3681. return 0;
  3682. }
  3683. list_for_each_entry_safe(req, req_temp, &flush_list, list) {
  3684. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  3685. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  3686. if (req_isp->fence_map_out[i].sync_id != -1) {
  3687. CAM_DBG(CAM_ISP, "Flush req 0x%llx, fence %d",
  3688. req->request_id,
  3689. req_isp->fence_map_out[i].sync_id);
  3690. rc = cam_sync_signal(
  3691. req_isp->fence_map_out[i].sync_id,
  3692. CAM_SYNC_STATE_SIGNALED_CANCEL,
  3693. CAM_SYNC_ISP_EVENT_FLUSH);
  3694. if (rc) {
  3695. tmp = req_isp->fence_map_out[i].sync_id;
  3696. CAM_ERR_RATE_LIMIT(CAM_ISP,
  3697. "signal fence %d failed", tmp);
  3698. }
  3699. req_isp->fence_map_out[i].sync_id = -1;
  3700. }
  3701. }
  3702. req_isp->reapply_type = CAM_CONFIG_REAPPLY_NONE;
  3703. req_isp->cdm_reset_before_apply = false;
  3704. list_del_init(&req->list);
  3705. list_add_tail(&req->list, &ctx->free_req_list);
  3706. }
  3707. return 0;
  3708. }
  3709. static int __cam_isp_ctx_flush_req_in_top_state(
  3710. struct cam_context *ctx,
  3711. struct cam_req_mgr_flush_request *flush_req)
  3712. {
  3713. int rc = 0;
  3714. struct cam_isp_context *ctx_isp;
  3715. struct cam_isp_stop_args stop_isp;
  3716. struct cam_hw_stop_args stop_args;
  3717. struct cam_hw_reset_args reset_args;
  3718. struct cam_req_mgr_timer_notify timer;
  3719. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  3720. CAM_DBG(CAM_ISP, "Flush pending list");
  3721. spin_lock_bh(&ctx->lock);
  3722. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3723. spin_unlock_bh(&ctx->lock);
  3724. if (flush_req->type == CAM_REQ_MGR_FLUSH_TYPE_ALL) {
  3725. if (ctx->state <= CAM_CTX_READY) {
  3726. ctx->state = CAM_CTX_ACQUIRED;
  3727. goto end;
  3728. }
  3729. spin_lock_bh(&ctx->lock);
  3730. ctx->state = CAM_CTX_FLUSHED;
  3731. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  3732. spin_unlock_bh(&ctx->lock);
  3733. CAM_INFO(CAM_ISP, "Last request id to flush is %lld, ctx_id:%d",
  3734. flush_req->req_id, ctx->ctx_id);
  3735. ctx->last_flush_req = flush_req->req_id;
  3736. __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_FLUSH, ctx);
  3737. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3738. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  3739. stop_isp.stop_only = true;
  3740. stop_isp.is_internal_stop = false;
  3741. stop_args.args = (void *)&stop_isp;
  3742. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  3743. &stop_args);
  3744. if (rc)
  3745. CAM_ERR(CAM_ISP, "Failed to stop HW in Flush rc: %d",
  3746. rc);
  3747. CAM_INFO(CAM_ISP, "Stop HW complete. Reset HW next.");
  3748. CAM_DBG(CAM_ISP, "Flush wait and active lists");
  3749. if (ctx->ctx_crm_intf && ctx->ctx_crm_intf->notify_timer) {
  3750. timer.link_hdl = ctx->link_hdl;
  3751. timer.dev_hdl = ctx->dev_hdl;
  3752. timer.state = false;
  3753. ctx->ctx_crm_intf->notify_timer(&timer);
  3754. }
  3755. spin_lock_bh(&ctx->lock);
  3756. if (!list_empty(&ctx->wait_req_list))
  3757. rc = __cam_isp_ctx_flush_req(ctx, &ctx->wait_req_list,
  3758. flush_req);
  3759. if (!list_empty(&ctx->active_req_list))
  3760. rc = __cam_isp_ctx_flush_req(ctx, &ctx->active_req_list,
  3761. flush_req);
  3762. ctx_isp->active_req_cnt = 0;
  3763. spin_unlock_bh(&ctx->lock);
  3764. reset_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  3765. rc = ctx->hw_mgr_intf->hw_reset(ctx->hw_mgr_intf->hw_mgr_priv,
  3766. &reset_args);
  3767. if (rc)
  3768. CAM_ERR(CAM_ISP, "Failed to reset HW rc: %d", rc);
  3769. ctx_isp->init_received = false;
  3770. }
  3771. end:
  3772. ctx_isp->bubble_frame_cnt = 0;
  3773. atomic_set(&ctx_isp->process_bubble, 0);
  3774. atomic_set(&ctx_isp->rxd_epoch, 0);
  3775. atomic_set(&ctx_isp->internal_recovery_set, 0);
  3776. return rc;
  3777. }
  3778. static int __cam_isp_ctx_flush_req_in_ready(
  3779. struct cam_context *ctx,
  3780. struct cam_req_mgr_flush_request *flush_req)
  3781. {
  3782. int rc = 0;
  3783. CAM_DBG(CAM_ISP, "try to flush pending list");
  3784. spin_lock_bh(&ctx->lock);
  3785. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, flush_req);
  3786. /* if nothing is in pending req list, change state to acquire */
  3787. if (list_empty(&ctx->pending_req_list))
  3788. ctx->state = CAM_CTX_ACQUIRED;
  3789. spin_unlock_bh(&ctx->lock);
  3790. trace_cam_context_state("ISP", ctx);
  3791. CAM_DBG(CAM_ISP, "Flush request in ready state. next state %d",
  3792. ctx->state);
  3793. return rc;
  3794. }
  3795. static struct cam_ctx_ops
  3796. cam_isp_ctx_activated_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3797. /* SOF */
  3798. {
  3799. .ioctl_ops = {},
  3800. .crm_ops = {
  3801. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3802. .notify_frame_skip =
  3803. __cam_isp_ctx_apply_default_req_settings,
  3804. },
  3805. .irq_ops = NULL,
  3806. },
  3807. /* APPLIED */
  3808. {
  3809. .ioctl_ops = {},
  3810. .crm_ops = {},
  3811. .irq_ops = NULL,
  3812. },
  3813. /* EPOCH */
  3814. {
  3815. .ioctl_ops = {},
  3816. .crm_ops = {
  3817. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3818. .notify_frame_skip =
  3819. __cam_isp_ctx_apply_default_req_settings,
  3820. },
  3821. .irq_ops = NULL,
  3822. },
  3823. /* BUBBLE */
  3824. {
  3825. .ioctl_ops = {},
  3826. .crm_ops = {
  3827. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3828. .notify_frame_skip =
  3829. __cam_isp_ctx_apply_default_req_settings,
  3830. },
  3831. .irq_ops = NULL,
  3832. },
  3833. /* Bubble Applied */
  3834. {
  3835. .ioctl_ops = {},
  3836. .crm_ops = {},
  3837. .irq_ops = NULL,
  3838. },
  3839. /* HW ERROR */
  3840. {
  3841. .ioctl_ops = {},
  3842. .crm_ops = {},
  3843. .irq_ops = NULL,
  3844. },
  3845. /* HALT */
  3846. {
  3847. .ioctl_ops = {},
  3848. .crm_ops = {},
  3849. .irq_ops = NULL,
  3850. },
  3851. };
  3852. static struct cam_ctx_ops
  3853. cam_isp_ctx_fs2_state_machine[CAM_ISP_CTX_ACTIVATED_MAX] = {
  3854. /* SOF */
  3855. {
  3856. .ioctl_ops = {},
  3857. .crm_ops = {
  3858. .apply_req = __cam_isp_ctx_apply_req_in_sof,
  3859. },
  3860. .irq_ops = NULL,
  3861. },
  3862. /* APPLIED */
  3863. {
  3864. .ioctl_ops = {},
  3865. .crm_ops = {},
  3866. .irq_ops = NULL,
  3867. },
  3868. /* EPOCH */
  3869. {
  3870. .ioctl_ops = {},
  3871. .crm_ops = {
  3872. .apply_req = __cam_isp_ctx_apply_req_in_epoch,
  3873. },
  3874. .irq_ops = NULL,
  3875. },
  3876. /* BUBBLE */
  3877. {
  3878. .ioctl_ops = {},
  3879. .crm_ops = {
  3880. .apply_req = __cam_isp_ctx_apply_req_in_bubble,
  3881. },
  3882. .irq_ops = NULL,
  3883. },
  3884. /* Bubble Applied */
  3885. {
  3886. .ioctl_ops = {},
  3887. .crm_ops = {},
  3888. .irq_ops = NULL,
  3889. },
  3890. /* HW ERROR */
  3891. {
  3892. .ioctl_ops = {},
  3893. .crm_ops = {},
  3894. .irq_ops = NULL,
  3895. },
  3896. /* HALT */
  3897. {
  3898. .ioctl_ops = {},
  3899. .crm_ops = {},
  3900. .irq_ops = NULL,
  3901. },
  3902. };
  3903. static int __cam_isp_ctx_rdi_only_sof_in_top_state(
  3904. struct cam_isp_context *ctx_isp, void *evt_data)
  3905. {
  3906. int rc = 0;
  3907. struct cam_context *ctx = ctx_isp->base;
  3908. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3909. uint64_t request_id = 0;
  3910. if (!evt_data) {
  3911. CAM_ERR(CAM_ISP, "in valid sof event data");
  3912. return -EINVAL;
  3913. }
  3914. ctx_isp->frame_id++;
  3915. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3916. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3917. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3918. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3919. /*
  3920. * notify reqmgr with sof signal. Note, due to scheduling delay
  3921. * we can run into situation that two active requests has already
  3922. * be in the active queue while we try to do the notification.
  3923. * In this case, we need to skip the current notification. This
  3924. * helps the state machine to catch up the delay.
  3925. */
  3926. if (ctx_isp->active_req_cnt <= 2) {
  3927. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  3928. /*
  3929. * It's possible for rup done to be processed before
  3930. * SOF, check for first active request shutter here
  3931. */
  3932. if (!list_empty(&ctx->active_req_list)) {
  3933. struct cam_ctx_request *req = NULL;
  3934. req = list_first_entry(&ctx->active_req_list,
  3935. struct cam_ctx_request, list);
  3936. if (req->request_id > ctx_isp->reported_req_id) {
  3937. request_id = req->request_id;
  3938. ctx_isp->reported_req_id = request_id;
  3939. }
  3940. }
  3941. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3942. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3943. } else {
  3944. CAM_ERR_RATE_LIMIT(CAM_ISP, "Can not notify SOF to CRM");
  3945. }
  3946. if (list_empty(&ctx->active_req_list))
  3947. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  3948. else
  3949. CAM_DBG(CAM_ISP, "Still need to wait for the buf done");
  3950. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3951. __cam_isp_ctx_substate_val_to_type(
  3952. ctx_isp->substate_activated));
  3953. return rc;
  3954. }
  3955. static int __cam_isp_ctx_rdi_only_sof_in_applied_state(
  3956. struct cam_isp_context *ctx_isp, void *evt_data)
  3957. {
  3958. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3959. if (!evt_data) {
  3960. CAM_ERR(CAM_ISP, "in valid sof event data");
  3961. return -EINVAL;
  3962. }
  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. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED;
  3969. CAM_DBG(CAM_ISP, "next Substate[%s]",
  3970. __cam_isp_ctx_substate_val_to_type(
  3971. ctx_isp->substate_activated));
  3972. return 0;
  3973. }
  3974. static int __cam_isp_ctx_rdi_only_sof_in_bubble_applied(
  3975. struct cam_isp_context *ctx_isp, void *evt_data)
  3976. {
  3977. struct cam_ctx_request *req;
  3978. struct cam_isp_ctx_req *req_isp;
  3979. struct cam_context *ctx = ctx_isp->base;
  3980. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  3981. uint64_t request_id = 0;
  3982. /*
  3983. * Sof in bubble applied state means, reg update not received.
  3984. * before increment frame id and override time stamp value, send
  3985. * the previous sof time stamp that got captured in the
  3986. * sof in applied state.
  3987. */
  3988. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3989. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3990. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  3991. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  3992. ctx_isp->frame_id++;
  3993. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  3994. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  3995. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  3996. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  3997. if (list_empty(&ctx->wait_req_list)) {
  3998. /*
  3999. * If no pending req in epoch, this is an error case.
  4000. * The recovery is to go back to sof state
  4001. */
  4002. CAM_ERR(CAM_ISP, "No wait request");
  4003. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4004. /* Send SOF event as empty frame*/
  4005. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4006. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4007. goto end;
  4008. }
  4009. req = list_first_entry(&ctx->wait_req_list, struct cam_ctx_request,
  4010. list);
  4011. req_isp = (struct cam_isp_ctx_req *)req->req_priv;
  4012. req_isp->bubble_detected = true;
  4013. CAM_INFO_RATE_LIMIT(CAM_ISP, "Ctx:%d Report Bubble flag %d req id:%lld",
  4014. ctx->ctx_id, req_isp->bubble_report, req->request_id);
  4015. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  4016. req_isp->cdm_reset_before_apply = false;
  4017. if (req_isp->bubble_report) {
  4018. __cam_isp_ctx_notify_error_util(CAM_TRIGGER_POINT_SOF, CRM_KMD_ERR_BUBBLE,
  4019. req->request_id, ctx_isp);
  4020. atomic_set(&ctx_isp->process_bubble, 1);
  4021. } else {
  4022. req_isp->bubble_report = 0;
  4023. }
  4024. /*
  4025. * Always move the request to active list. Let buf done
  4026. * function handles the rest.
  4027. */
  4028. list_del_init(&req->list);
  4029. list_add_tail(&req->list, &ctx->active_req_list);
  4030. ctx_isp->active_req_cnt++;
  4031. CAM_DBG(CAM_ISP, "move request %lld to active list(cnt = %d)",
  4032. req->request_id, ctx_isp->active_req_cnt);
  4033. if (!req_isp->bubble_report) {
  4034. if (req->request_id > ctx_isp->reported_req_id) {
  4035. request_id = req->request_id;
  4036. ctx_isp->reported_req_id = request_id;
  4037. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4038. CAM_REQ_MGR_SOF_EVENT_ERROR);
  4039. } else
  4040. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4041. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4042. } else
  4043. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4044. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4045. /* change the state to bubble, as reg update has not come */
  4046. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_BUBBLE;
  4047. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4048. __cam_isp_ctx_substate_val_to_type(
  4049. ctx_isp->substate_activated));
  4050. end:
  4051. return 0;
  4052. }
  4053. static int __cam_isp_ctx_rdi_only_sof_in_bubble_state(
  4054. struct cam_isp_context *ctx_isp, void *evt_data)
  4055. {
  4056. uint32_t i;
  4057. struct cam_ctx_request *req;
  4058. struct cam_context *ctx = ctx_isp->base;
  4059. struct cam_isp_hw_sof_event_data *sof_event_data = evt_data;
  4060. struct cam_isp_ctx_req *req_isp;
  4061. struct cam_hw_cmd_args hw_cmd_args;
  4062. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4063. uint64_t request_id = 0;
  4064. uint64_t last_cdm_done_req = 0;
  4065. int rc = 0;
  4066. if (!evt_data) {
  4067. CAM_ERR(CAM_ISP, "in valid sof event data");
  4068. return -EINVAL;
  4069. }
  4070. ctx_isp->frame_id++;
  4071. ctx_isp->sof_timestamp_val = sof_event_data->timestamp;
  4072. ctx_isp->boot_timestamp = sof_event_data->boot_time;
  4073. CAM_DBG(CAM_ISP, "frame id: %lld time stamp:0x%llx",
  4074. ctx_isp->frame_id, ctx_isp->sof_timestamp_val);
  4075. if (atomic_read(&ctx_isp->process_bubble)) {
  4076. if (list_empty(&ctx->active_req_list)) {
  4077. CAM_ERR(CAM_ISP, "No available active req in bubble");
  4078. atomic_set(&ctx_isp->process_bubble, 0);
  4079. return -EINVAL;
  4080. }
  4081. if (ctx_isp->last_sof_timestamp ==
  4082. ctx_isp->sof_timestamp_val) {
  4083. CAM_DBG(CAM_ISP,
  4084. "Tasklet delay detected! Bubble frame: %lld check skipped, sof_timestamp: %lld, ctx_id: %d",
  4085. ctx_isp->frame_id,
  4086. ctx_isp->sof_timestamp_val,
  4087. ctx->ctx_id);
  4088. goto end;
  4089. }
  4090. req = list_first_entry(&ctx->active_req_list,
  4091. struct cam_ctx_request, list);
  4092. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4093. if (req_isp->bubble_detected) {
  4094. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4095. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4096. isp_hw_cmd_args.cmd_type =
  4097. CAM_ISP_HW_MGR_GET_LAST_CDM_DONE;
  4098. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4099. rc = ctx->hw_mgr_intf->hw_cmd(
  4100. ctx->hw_mgr_intf->hw_mgr_priv,
  4101. &hw_cmd_args);
  4102. if (rc) {
  4103. CAM_ERR(CAM_ISP, "HW command failed");
  4104. return rc;
  4105. }
  4106. last_cdm_done_req = isp_hw_cmd_args.u.last_cdm_done;
  4107. CAM_DBG(CAM_ISP, "last_cdm_done req: %d ctx_id: %d",
  4108. last_cdm_done_req, ctx->ctx_id);
  4109. if (last_cdm_done_req >= req->request_id) {
  4110. CAM_DBG(CAM_ISP,
  4111. "CDM callback detected for req: %lld, possible buf_done delay, waiting for buf_done",
  4112. req->request_id);
  4113. goto end;
  4114. } else {
  4115. CAM_WARN(CAM_ISP,
  4116. "CDM callback not happened for req: %lld, possible CDM stuck or workqueue delay",
  4117. req->request_id);
  4118. req_isp->num_acked = 0;
  4119. req_isp->num_deferred_acks = 0;
  4120. req_isp->bubble_detected = false;
  4121. req_isp->cdm_reset_before_apply = true;
  4122. list_del_init(&req->list);
  4123. list_add(&req->list, &ctx->pending_req_list);
  4124. atomic_set(&ctx_isp->process_bubble, 0);
  4125. ctx_isp->active_req_cnt--;
  4126. CAM_DBG(CAM_REQ,
  4127. "Move active req: %lld to pending list(cnt = %d) [bubble re-apply],ctx %u",
  4128. req->request_id,
  4129. ctx_isp->active_req_cnt, ctx->ctx_id);
  4130. }
  4131. goto end;
  4132. }
  4133. }
  4134. /*
  4135. * Signal all active requests with error and move the all the active
  4136. * requests to free list
  4137. */
  4138. while (!list_empty(&ctx->active_req_list)) {
  4139. req = list_first_entry(&ctx->active_req_list,
  4140. struct cam_ctx_request, list);
  4141. list_del_init(&req->list);
  4142. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4143. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  4144. req_isp->num_fence_map_out);
  4145. for (i = 0; i < req_isp->num_fence_map_out; i++)
  4146. if (req_isp->fence_map_out[i].sync_id != -1) {
  4147. cam_sync_signal(
  4148. req_isp->fence_map_out[i].sync_id,
  4149. CAM_SYNC_STATE_SIGNALED_ERROR,
  4150. CAM_SYNC_ISP_EVENT_BUBBLE);
  4151. }
  4152. list_add_tail(&req->list, &ctx->free_req_list);
  4153. ctx_isp->active_req_cnt--;
  4154. }
  4155. end:
  4156. /* notify reqmgr with sof signal */
  4157. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4158. /*
  4159. * It is idle frame with out any applied request id, send
  4160. * request id as zero
  4161. */
  4162. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4163. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4164. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4165. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4166. __cam_isp_ctx_substate_val_to_type(
  4167. ctx_isp->substate_activated));
  4168. ctx_isp->last_sof_timestamp = ctx_isp->sof_timestamp_val;
  4169. return 0;
  4170. }
  4171. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state(
  4172. struct cam_isp_context *ctx_isp, void *evt_data)
  4173. {
  4174. struct cam_ctx_request *req = NULL;
  4175. struct cam_context *ctx = ctx_isp->base;
  4176. req = list_first_entry(&ctx->active_req_list,
  4177. struct cam_ctx_request, list);
  4178. CAM_INFO(CAM_ISP, "Received RUP for Bubble Request", req->request_id);
  4179. return 0;
  4180. }
  4181. static int __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state(
  4182. struct cam_isp_context *ctx_isp, void *evt_data)
  4183. {
  4184. struct cam_ctx_request *req = NULL;
  4185. struct cam_context *ctx = ctx_isp->base;
  4186. struct cam_isp_ctx_req *req_isp;
  4187. uint64_t request_id = 0;
  4188. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_EPOCH;
  4189. /* notify reqmgr with sof signal*/
  4190. if (list_empty(&ctx->wait_req_list)) {
  4191. CAM_ERR(CAM_ISP, "Reg upd ack with no waiting request");
  4192. goto error;
  4193. }
  4194. req = list_first_entry(&ctx->wait_req_list,
  4195. struct cam_ctx_request, list);
  4196. list_del_init(&req->list);
  4197. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4198. request_id =
  4199. (req_isp->hw_update_data.packet_opcode_type ==
  4200. CAM_ISP_PACKET_INIT_DEV) ? 0 : req->request_id;
  4201. if (req_isp->num_fence_map_out != 0) {
  4202. list_add_tail(&req->list, &ctx->active_req_list);
  4203. ctx_isp->active_req_cnt++;
  4204. CAM_DBG(CAM_ISP,
  4205. "move request %lld to active list(cnt = %d)",
  4206. req->request_id, ctx_isp->active_req_cnt);
  4207. /* if packet has buffers, set correct request id */
  4208. request_id = req->request_id;
  4209. } else {
  4210. /* no io config, so the request is completed. */
  4211. list_add_tail(&req->list, &ctx->free_req_list);
  4212. CAM_DBG(CAM_ISP,
  4213. "move active req %lld to free list(cnt=%d)",
  4214. req->request_id, ctx_isp->active_req_cnt);
  4215. }
  4216. __cam_isp_ctx_notify_trigger_util(CAM_TRIGGER_POINT_SOF, ctx_isp);
  4217. if (request_id)
  4218. ctx_isp->reported_req_id = request_id;
  4219. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4220. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4221. CAM_DBG(CAM_ISP, "next Substate[%s]",
  4222. __cam_isp_ctx_substate_val_to_type(
  4223. ctx_isp->substate_activated));
  4224. __cam_isp_ctx_update_event_record(ctx_isp,
  4225. CAM_ISP_CTX_EVENT_RUP, req);
  4226. return 0;
  4227. error:
  4228. /* Send SOF event as idle frame*/
  4229. __cam_isp_ctx_send_sof_timestamp(ctx_isp, request_id,
  4230. CAM_REQ_MGR_SOF_EVENT_SUCCESS);
  4231. __cam_isp_ctx_update_event_record(ctx_isp,
  4232. CAM_ISP_CTX_EVENT_RUP, NULL);
  4233. /*
  4234. * There is no request in the pending list, move the sub state machine
  4235. * to SOF sub state
  4236. */
  4237. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  4238. return 0;
  4239. }
  4240. static struct cam_isp_ctx_irq_ops
  4241. cam_isp_ctx_rdi_only_activated_state_machine_irq
  4242. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4243. /* SOF */
  4244. {
  4245. .irq_ops = {
  4246. NULL,
  4247. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4248. __cam_isp_ctx_reg_upd_in_sof,
  4249. NULL,
  4250. NULL,
  4251. NULL,
  4252. },
  4253. },
  4254. /* APPLIED */
  4255. {
  4256. .irq_ops = {
  4257. __cam_isp_ctx_handle_error,
  4258. __cam_isp_ctx_rdi_only_sof_in_applied_state,
  4259. __cam_isp_ctx_reg_upd_in_applied_state,
  4260. NULL,
  4261. NULL,
  4262. __cam_isp_ctx_buf_done_in_applied,
  4263. },
  4264. },
  4265. /* EPOCH */
  4266. {
  4267. .irq_ops = {
  4268. __cam_isp_ctx_handle_error,
  4269. __cam_isp_ctx_rdi_only_sof_in_top_state,
  4270. NULL,
  4271. NULL,
  4272. NULL,
  4273. __cam_isp_ctx_buf_done_in_epoch,
  4274. },
  4275. },
  4276. /* BUBBLE*/
  4277. {
  4278. .irq_ops = {
  4279. __cam_isp_ctx_handle_error,
  4280. __cam_isp_ctx_rdi_only_sof_in_bubble_state,
  4281. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_state,
  4282. NULL,
  4283. NULL,
  4284. __cam_isp_ctx_buf_done_in_bubble,
  4285. },
  4286. },
  4287. /* BUBBLE APPLIED ie PRE_BUBBLE */
  4288. {
  4289. .irq_ops = {
  4290. __cam_isp_ctx_handle_error,
  4291. __cam_isp_ctx_rdi_only_sof_in_bubble_applied,
  4292. __cam_isp_ctx_rdi_only_reg_upd_in_bubble_applied_state,
  4293. NULL,
  4294. NULL,
  4295. __cam_isp_ctx_buf_done_in_bubble_applied,
  4296. },
  4297. },
  4298. /* HW ERROR */
  4299. {
  4300. },
  4301. /* HALT */
  4302. {
  4303. },
  4304. };
  4305. static int __cam_isp_ctx_rdi_only_apply_req_top_state(
  4306. struct cam_context *ctx, struct cam_req_mgr_apply_request *apply)
  4307. {
  4308. int rc = 0;
  4309. struct cam_isp_context *ctx_isp =
  4310. (struct cam_isp_context *) ctx->ctx_priv;
  4311. CAM_DBG(CAM_ISP, "current Substate[%s]",
  4312. __cam_isp_ctx_substate_val_to_type(
  4313. ctx_isp->substate_activated));
  4314. rc = __cam_isp_ctx_apply_req_in_activated_state(ctx, apply,
  4315. CAM_ISP_CTX_ACTIVATED_APPLIED);
  4316. CAM_DBG(CAM_ISP, "new Substate[%s]",
  4317. __cam_isp_ctx_substate_val_to_type(
  4318. ctx_isp->substate_activated));
  4319. if (rc)
  4320. CAM_ERR_RATE_LIMIT(CAM_ISP,
  4321. "ctx_id:%d Apply failed in Substate[%s], rc %d",
  4322. ctx->ctx_id,
  4323. __cam_isp_ctx_substate_val_to_type(
  4324. ctx_isp->substate_activated), rc);
  4325. return rc;
  4326. }
  4327. static struct cam_ctx_ops
  4328. cam_isp_ctx_rdi_only_activated_state_machine
  4329. [CAM_ISP_CTX_ACTIVATED_MAX] = {
  4330. /* SOF */
  4331. {
  4332. .ioctl_ops = {},
  4333. .crm_ops = {
  4334. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4335. },
  4336. .irq_ops = NULL,
  4337. },
  4338. /* APPLIED */
  4339. {
  4340. .ioctl_ops = {},
  4341. .crm_ops = {},
  4342. .irq_ops = NULL,
  4343. },
  4344. /* EPOCH */
  4345. {
  4346. .ioctl_ops = {},
  4347. .crm_ops = {
  4348. .apply_req = __cam_isp_ctx_rdi_only_apply_req_top_state,
  4349. },
  4350. .irq_ops = NULL,
  4351. },
  4352. /* PRE BUBBLE */
  4353. {
  4354. .ioctl_ops = {},
  4355. .crm_ops = {},
  4356. .irq_ops = NULL,
  4357. },
  4358. /* BUBBLE */
  4359. {
  4360. .ioctl_ops = {},
  4361. .crm_ops = {},
  4362. .irq_ops = NULL,
  4363. },
  4364. /* HW ERROR */
  4365. {
  4366. .ioctl_ops = {},
  4367. .crm_ops = {},
  4368. .irq_ops = NULL,
  4369. },
  4370. /* HALT */
  4371. {
  4372. .ioctl_ops = {},
  4373. .crm_ops = {},
  4374. .irq_ops = NULL,
  4375. },
  4376. };
  4377. static int __cam_isp_ctx_flush_dev_in_top_state(struct cam_context *ctx,
  4378. struct cam_flush_dev_cmd *cmd)
  4379. {
  4380. struct cam_isp_context *ctx_isp = ctx->ctx_priv;
  4381. struct cam_req_mgr_flush_request flush_req;
  4382. if (!ctx_isp->offline_context) {
  4383. CAM_ERR(CAM_ISP, "flush dev only supported in offline context");
  4384. return -EINVAL;
  4385. }
  4386. flush_req.type = (cmd->flush_type == CAM_FLUSH_TYPE_ALL) ? CAM_REQ_MGR_FLUSH_TYPE_ALL :
  4387. CAM_REQ_MGR_FLUSH_TYPE_CANCEL_REQ;
  4388. flush_req.req_id = cmd->req_id;
  4389. CAM_DBG(CAM_ISP, "offline flush (type:%u, req:%lu)", flush_req.type, flush_req.req_id);
  4390. switch (ctx->state) {
  4391. case CAM_CTX_ACQUIRED:
  4392. case CAM_CTX_ACTIVATED:
  4393. return __cam_isp_ctx_flush_req_in_top_state(ctx, &flush_req);
  4394. case CAM_CTX_READY:
  4395. return __cam_isp_ctx_flush_req_in_ready(ctx, &flush_req);
  4396. default:
  4397. CAM_ERR(CAM_ISP, "flush dev in wrong state: %d", ctx->state);
  4398. return -EINVAL;
  4399. }
  4400. if (cmd->flush_type == CAM_FLUSH_TYPE_ALL)
  4401. cam_req_mgr_workq_flush(ctx_isp->workq);
  4402. }
  4403. static void __cam_isp_ctx_free_mem_hw_entries(struct cam_context *ctx)
  4404. {
  4405. int i;
  4406. if (ctx->out_map_entries) {
  4407. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4408. kfree(ctx->out_map_entries[i]);
  4409. ctx->out_map_entries[i] = NULL;
  4410. }
  4411. kfree(ctx->out_map_entries);
  4412. ctx->out_map_entries = NULL;
  4413. }
  4414. if (ctx->in_map_entries) {
  4415. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4416. kfree(ctx->in_map_entries[i]);
  4417. ctx->in_map_entries[i] = NULL;
  4418. }
  4419. kfree(ctx->in_map_entries);
  4420. ctx->in_map_entries = NULL;
  4421. }
  4422. if (ctx->hw_update_entry) {
  4423. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4424. kfree(ctx->hw_update_entry[i]);
  4425. ctx->hw_update_entry[i] = NULL;
  4426. }
  4427. kfree(ctx->hw_update_entry);
  4428. ctx->hw_update_entry = NULL;
  4429. }
  4430. ctx->max_out_map_entries = 0;
  4431. ctx->max_in_map_entries = 0;
  4432. ctx->max_hw_update_entries = 0;
  4433. }
  4434. static int __cam_isp_ctx_release_hw_in_top_state(struct cam_context *ctx,
  4435. void *cmd)
  4436. {
  4437. int rc = 0;
  4438. struct cam_hw_release_args rel_arg;
  4439. struct cam_isp_context *ctx_isp =
  4440. (struct cam_isp_context *) ctx->ctx_priv;
  4441. struct cam_req_mgr_flush_request flush_req;
  4442. int i;
  4443. if (ctx_isp->hw_ctx) {
  4444. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4445. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4446. &rel_arg);
  4447. ctx_isp->hw_ctx = NULL;
  4448. } else {
  4449. CAM_ERR(CAM_ISP, "No hw resources acquired for ctx[%u]", ctx->ctx_id);
  4450. }
  4451. ctx->last_flush_req = 0;
  4452. ctx_isp->custom_enabled = false;
  4453. ctx_isp->use_frame_header_ts = false;
  4454. ctx_isp->use_default_apply = false;
  4455. ctx_isp->frame_id = 0;
  4456. ctx_isp->active_req_cnt = 0;
  4457. ctx_isp->reported_req_id = 0;
  4458. ctx_isp->hw_acquired = false;
  4459. ctx_isp->init_received = false;
  4460. ctx_isp->support_consumed_addr = false;
  4461. ctx_isp->aeb_enabled = false;
  4462. ctx_isp->do_internal_recovery = false;
  4463. ctx_isp->req_info.last_bufdone_req_id = 0;
  4464. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4465. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4466. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4467. /*
  4468. * Ideally, we should never have any active request here.
  4469. * But we still add some sanity check code here to help the debug
  4470. */
  4471. if (!list_empty(&ctx->active_req_list))
  4472. CAM_WARN(CAM_ISP, "Active list is not empty");
  4473. /* Flush all the pending request list */
  4474. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4475. flush_req.link_hdl = ctx->link_hdl;
  4476. flush_req.dev_hdl = ctx->dev_hdl;
  4477. flush_req.req_id = 0;
  4478. CAM_DBG(CAM_ISP, "try to flush pending list");
  4479. spin_lock_bh(&ctx->lock);
  4480. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4481. spin_unlock_bh(&ctx->lock);
  4482. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4483. cam_req_mgr_workq_destroy(&ctx_isp->workq);
  4484. ctx->state = CAM_CTX_ACQUIRED;
  4485. trace_cam_context_state("ISP", ctx);
  4486. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4487. ctx->ctx_id, ctx->state);
  4488. return rc;
  4489. }
  4490. /* top level state machine */
  4491. static int __cam_isp_ctx_release_dev_in_top_state(struct cam_context *ctx,
  4492. struct cam_release_dev_cmd *cmd)
  4493. {
  4494. int rc = 0;
  4495. int i;
  4496. struct cam_hw_release_args rel_arg;
  4497. struct cam_isp_context *ctx_isp =
  4498. (struct cam_isp_context *) ctx->ctx_priv;
  4499. struct cam_req_mgr_flush_request flush_req;
  4500. if (cmd && ctx_isp->hw_ctx) {
  4501. CAM_ERR(CAM_ISP, "releasing hw");
  4502. __cam_isp_ctx_release_hw_in_top_state(ctx, NULL);
  4503. }
  4504. if (ctx_isp->hw_ctx) {
  4505. rel_arg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4506. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4507. &rel_arg);
  4508. ctx_isp->hw_ctx = NULL;
  4509. }
  4510. ctx->session_hdl = -1;
  4511. ctx->dev_hdl = -1;
  4512. ctx->link_hdl = -1;
  4513. ctx->ctx_crm_intf = NULL;
  4514. ctx->last_flush_req = 0;
  4515. ctx_isp->frame_id = 0;
  4516. ctx_isp->active_req_cnt = 0;
  4517. ctx_isp->reported_req_id = 0;
  4518. ctx_isp->hw_acquired = false;
  4519. ctx_isp->init_received = false;
  4520. ctx_isp->offline_context = false;
  4521. ctx_isp->rdi_only_context = false;
  4522. ctx_isp->req_info.last_bufdone_req_id = 0;
  4523. ctx_isp->v4l2_event_sub_ids = 0;
  4524. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4525. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4526. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4527. /*
  4528. * Ideally, we should never have any active request here.
  4529. * But we still add some sanity check code here to help the debug
  4530. */
  4531. if (!list_empty(&ctx->active_req_list))
  4532. CAM_ERR(CAM_ISP, "Active list is not empty");
  4533. /* Flush all the pending request list */
  4534. flush_req.type = CAM_REQ_MGR_FLUSH_TYPE_ALL;
  4535. flush_req.link_hdl = ctx->link_hdl;
  4536. flush_req.dev_hdl = ctx->dev_hdl;
  4537. flush_req.req_id = 0;
  4538. CAM_DBG(CAM_ISP, "try to flush pending list");
  4539. spin_lock_bh(&ctx->lock);
  4540. rc = __cam_isp_ctx_flush_req(ctx, &ctx->pending_req_list, &flush_req);
  4541. spin_unlock_bh(&ctx->lock);
  4542. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4543. ctx->state = CAM_CTX_AVAILABLE;
  4544. trace_cam_context_state("ISP", ctx);
  4545. CAM_DBG(CAM_ISP, "Release device success[%u] next state %d",
  4546. ctx->ctx_id, ctx->state);
  4547. return rc;
  4548. }
  4549. static int __cam_isp_ctx_config_dev_in_top_state(
  4550. struct cam_context *ctx, struct cam_config_dev_cmd *cmd)
  4551. {
  4552. int rc = 0, i;
  4553. struct cam_ctx_request *req = NULL;
  4554. struct cam_isp_ctx_req *req_isp;
  4555. struct cam_packet *packet;
  4556. size_t remain_len = 0;
  4557. struct cam_hw_prepare_update_args cfg = {0};
  4558. struct cam_req_mgr_add_request add_req;
  4559. struct cam_isp_context *ctx_isp =
  4560. (struct cam_isp_context *) ctx->ctx_priv;
  4561. struct cam_hw_cmd_args hw_cmd_args;
  4562. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4563. uint32_t packet_opcode = 0;
  4564. CAM_DBG(CAM_ISP, "get free request object......");
  4565. /* get free request */
  4566. spin_lock_bh(&ctx->lock);
  4567. if (!list_empty(&ctx->free_req_list)) {
  4568. req = list_first_entry(&ctx->free_req_list,
  4569. struct cam_ctx_request, list);
  4570. list_del_init(&req->list);
  4571. }
  4572. spin_unlock_bh(&ctx->lock);
  4573. if (!req) {
  4574. CAM_ERR(CAM_ISP, "No more request obj free");
  4575. return -ENOMEM;
  4576. }
  4577. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4578. remain_len = cam_context_parse_config_cmd(ctx, cmd, &packet);
  4579. if (IS_ERR(packet)) {
  4580. rc = PTR_ERR(packet);
  4581. goto free_req;
  4582. }
  4583. /* Query the packet opcode */
  4584. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4585. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4586. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_GET_PACKET_OPCODE;
  4587. isp_hw_cmd_args.cmd_data = (void *)packet;
  4588. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4589. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4590. &hw_cmd_args);
  4591. if (rc) {
  4592. CAM_ERR(CAM_ISP, "HW command failed");
  4593. goto free_req;
  4594. }
  4595. packet_opcode = isp_hw_cmd_args.u.packet_op_code;
  4596. if ((packet_opcode == CAM_ISP_PACKET_UPDATE_DEV)
  4597. && (packet->header.request_id <= ctx->last_flush_req)) {
  4598. CAM_INFO(CAM_ISP,
  4599. "request %lld has been flushed, reject packet",
  4600. packet->header.request_id);
  4601. rc = -EBADR;
  4602. goto free_req;
  4603. }
  4604. cfg.packet = packet;
  4605. cfg.remain_len = remain_len;
  4606. cfg.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4607. cfg.max_hw_update_entries = ctx->max_hw_update_entries;
  4608. cfg.hw_update_entries = req_isp->cfg;
  4609. cfg.max_out_map_entries = ctx->max_out_map_entries;
  4610. cfg.max_in_map_entries = ctx->max_in_map_entries;
  4611. cfg.out_map_entries = req_isp->fence_map_out;
  4612. cfg.in_map_entries = req_isp->fence_map_in;
  4613. cfg.priv = &req_isp->hw_update_data;
  4614. cfg.pf_data = &(req->pf_data);
  4615. cfg.num_out_map_entries = 0;
  4616. cfg.num_in_map_entries = 0;
  4617. memset(&req_isp->hw_update_data, 0, sizeof(req_isp->hw_update_data));
  4618. rc = ctx->hw_mgr_intf->hw_prepare_update(
  4619. ctx->hw_mgr_intf->hw_mgr_priv, &cfg);
  4620. if (rc != 0) {
  4621. CAM_ERR(CAM_ISP, "Prepare config packet failed in HW layer");
  4622. rc = -EFAULT;
  4623. goto free_req;
  4624. }
  4625. req_isp->num_cfg = cfg.num_hw_update_entries;
  4626. req_isp->num_fence_map_out = cfg.num_out_map_entries;
  4627. req_isp->num_fence_map_in = cfg.num_in_map_entries;
  4628. req_isp->num_acked = 0;
  4629. req_isp->num_deferred_acks = 0;
  4630. req_isp->bubble_detected = false;
  4631. req_isp->cdm_reset_before_apply = false;
  4632. req_isp->hw_update_data.packet = packet;
  4633. for (i = 0; i < req_isp->num_fence_map_out; i++) {
  4634. rc = cam_sync_get_obj_ref(req_isp->fence_map_out[i].sync_id);
  4635. if (rc) {
  4636. CAM_ERR(CAM_ISP, "Can't get ref for fence %d",
  4637. req_isp->fence_map_out[i].sync_id);
  4638. goto put_ref;
  4639. }
  4640. }
  4641. CAM_DBG(CAM_ISP,
  4642. "packet req-id:%lld, opcode:%d, num_entry:%d, num_fence_out: %d, num_fence_in: %d",
  4643. packet->header.request_id, req_isp->hw_update_data.packet_opcode_type,
  4644. req_isp->num_cfg, req_isp->num_fence_map_out, req_isp->num_fence_map_in);
  4645. req->request_id = packet->header.request_id;
  4646. req->status = 1;
  4647. if (req_isp->hw_update_data.packet_opcode_type ==
  4648. CAM_ISP_PACKET_INIT_DEV) {
  4649. if (ctx->state < CAM_CTX_ACTIVATED) {
  4650. rc = __cam_isp_ctx_enqueue_init_request(ctx, req);
  4651. if (rc)
  4652. CAM_ERR(CAM_ISP, "Enqueue INIT pkt failed");
  4653. ctx_isp->init_received = true;
  4654. } else {
  4655. rc = -EINVAL;
  4656. CAM_ERR(CAM_ISP, "Recevied INIT pkt in wrong state:%d",
  4657. ctx->state);
  4658. }
  4659. } else {
  4660. if ((ctx->state == CAM_CTX_FLUSHED) || (ctx->state < CAM_CTX_READY)) {
  4661. rc = -EINVAL;
  4662. CAM_ERR(CAM_ISP, "Received update req %lld in wrong state:%d",
  4663. req->request_id, ctx->state);
  4664. goto put_ref;
  4665. }
  4666. if (ctx_isp->offline_context) {
  4667. __cam_isp_ctx_enqueue_request_in_order(ctx, req);
  4668. } else if (ctx->ctx_crm_intf->add_req) {
  4669. memset(&add_req, 0, sizeof(add_req));
  4670. add_req.link_hdl = ctx->link_hdl;
  4671. add_req.dev_hdl = ctx->dev_hdl;
  4672. add_req.req_id = req->request_id;
  4673. rc = ctx->ctx_crm_intf->add_req(&add_req);
  4674. if (rc) {
  4675. CAM_ERR(CAM_ISP, "Add req failed: req id=%llu",
  4676. req->request_id);
  4677. } else {
  4678. __cam_isp_ctx_enqueue_request_in_order(
  4679. ctx, req);
  4680. }
  4681. } else {
  4682. CAM_ERR(CAM_ISP, "Unable to add request: req id=%llu", req->request_id);
  4683. rc = -ENODEV;
  4684. }
  4685. }
  4686. if (rc)
  4687. goto put_ref;
  4688. CAM_DBG(CAM_REQ,
  4689. "Preprocessing Config req_id %lld successful on ctx %u",
  4690. req->request_id, ctx->ctx_id);
  4691. if (ctx_isp->offline_context && atomic_read(&ctx_isp->rxd_epoch)) {
  4692. __cam_isp_ctx_schedule_apply_req_offline(ctx_isp);
  4693. }
  4694. return rc;
  4695. put_ref:
  4696. for (--i; i >= 0; i--) {
  4697. if (cam_sync_put_obj_ref(req_isp->fence_map_out[i].sync_id))
  4698. CAM_ERR(CAM_CTXT, "Failed to put ref of fence %d",
  4699. req_isp->fence_map_out[i].sync_id);
  4700. }
  4701. free_req:
  4702. spin_lock_bh(&ctx->lock);
  4703. list_add_tail(&req->list, &ctx->free_req_list);
  4704. spin_unlock_bh(&ctx->lock);
  4705. return rc;
  4706. }
  4707. static int __cam_isp_ctx_allocate_mem_hw_entries(
  4708. struct cam_context *ctx,
  4709. struct cam_hw_acquire_args *param)
  4710. {
  4711. int rc = 0, i;
  4712. uint32_t max_res = 0;
  4713. uint32_t max_hw_upd_entries = CAM_ISP_CTX_CFG_MAX;
  4714. struct cam_ctx_request *req;
  4715. struct cam_ctx_request *temp_req;
  4716. struct cam_isp_ctx_req *req_isp;
  4717. if (!param->op_params.param_list[0])
  4718. max_res = CAM_ISP_CTX_RES_MAX;
  4719. else {
  4720. max_res = param->op_params.param_list[0];
  4721. if (param->op_flags & CAM_IFE_CTX_SFE_EN) {
  4722. max_res += param->op_params.param_list[1];
  4723. max_hw_upd_entries = CAM_ISP_SFE_CTX_CFG_MAX;
  4724. }
  4725. }
  4726. ctx->max_in_map_entries = max_res;
  4727. ctx->max_out_map_entries = max_res;
  4728. ctx->max_hw_update_entries = max_hw_upd_entries;
  4729. CAM_DBG(CAM_ISP,
  4730. "Allocate max_entries: 0x%x max_res: 0x%x is_sfe_en: %d",
  4731. max_hw_upd_entries, max_res, (param->op_flags & CAM_IFE_CTX_SFE_EN));
  4732. ctx->hw_update_entry = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_update_entry *),
  4733. GFP_KERNEL);
  4734. if (!ctx->hw_update_entry) {
  4735. CAM_ERR(CAM_CTXT, "%s[%d] no memory ",
  4736. ctx->dev_name, ctx->ctx_id);
  4737. return -ENOMEM;
  4738. }
  4739. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4740. ctx->hw_update_entry[i] = kcalloc(ctx->max_hw_update_entries,
  4741. sizeof(struct cam_hw_update_entry), GFP_KERNEL);
  4742. if (!ctx->hw_update_entry[i]) {
  4743. CAM_ERR(CAM_CTXT, "%s[%d] no memory for hw_update_entry: %u",
  4744. ctx->dev_name, ctx->ctx_id, i);
  4745. return -ENOMEM;
  4746. }
  4747. }
  4748. ctx->in_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4749. GFP_KERNEL);
  4750. if (!ctx->in_map_entries) {
  4751. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries",
  4752. ctx->dev_name, ctx->ctx_id);
  4753. rc = -ENOMEM;
  4754. goto end;
  4755. }
  4756. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4757. ctx->in_map_entries[i] = kcalloc(ctx->max_in_map_entries,
  4758. sizeof(struct cam_hw_fence_map_entry),
  4759. GFP_KERNEL);
  4760. if (!ctx->in_map_entries[i]) {
  4761. CAM_ERR(CAM_CTXT, "%s[%d] no memory for in_map_entries: %u",
  4762. ctx->dev_name, ctx->ctx_id, i);
  4763. rc = -ENOMEM;
  4764. goto end;
  4765. }
  4766. }
  4767. ctx->out_map_entries = kcalloc(CAM_ISP_CTX_REQ_MAX, sizeof(struct cam_hw_fence_map_entry *),
  4768. GFP_KERNEL);
  4769. if (!ctx->out_map_entries) {
  4770. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries",
  4771. ctx->dev_name, ctx->ctx_id);
  4772. rc = -ENOMEM;
  4773. goto end;
  4774. }
  4775. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  4776. ctx->out_map_entries[i] = kcalloc(ctx->max_out_map_entries,
  4777. sizeof(struct cam_hw_fence_map_entry),
  4778. GFP_KERNEL);
  4779. if (!ctx->out_map_entries[i]) {
  4780. CAM_ERR(CAM_CTXT, "%s[%d] no memory for out_map_entries: %u",
  4781. ctx->dev_name, ctx->ctx_id, i);
  4782. rc = -ENOMEM;
  4783. goto end;
  4784. }
  4785. }
  4786. list_for_each_entry_safe(req, temp_req,
  4787. &ctx->free_req_list, list) {
  4788. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  4789. req_isp->cfg = ctx->hw_update_entry[req->index];
  4790. req_isp->fence_map_in = ctx->in_map_entries[req->index];
  4791. req_isp->fence_map_out = ctx->out_map_entries[req->index];
  4792. }
  4793. return rc;
  4794. end:
  4795. __cam_isp_ctx_free_mem_hw_entries(ctx);
  4796. return rc;
  4797. }
  4798. static int __cam_isp_ctx_acquire_dev_in_available(struct cam_context *ctx,
  4799. struct cam_acquire_dev_cmd *cmd)
  4800. {
  4801. int rc = 0;
  4802. int i;
  4803. struct cam_hw_acquire_args param;
  4804. struct cam_isp_resource *isp_res = NULL;
  4805. struct cam_create_dev_hdl req_hdl_param;
  4806. struct cam_hw_release_args release;
  4807. struct cam_isp_context *ctx_isp =
  4808. (struct cam_isp_context *) ctx->ctx_priv;
  4809. struct cam_hw_cmd_args hw_cmd_args;
  4810. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4811. if (!ctx->hw_mgr_intf) {
  4812. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4813. rc = -EFAULT;
  4814. goto end;
  4815. }
  4816. CAM_DBG(CAM_ISP,
  4817. "session_hdl 0x%x, num_resources %d, hdl type %d, res %lld",
  4818. cmd->session_handle, cmd->num_resources,
  4819. cmd->handle_type, cmd->resource_hdl);
  4820. ctx_isp->v4l2_event_sub_ids = cam_req_mgr_get_id_subscribed();
  4821. if (cmd->num_resources == CAM_API_COMPAT_CONSTANT) {
  4822. ctx_isp->split_acquire = true;
  4823. CAM_DBG(CAM_ISP, "Acquire dev handle");
  4824. goto get_dev_handle;
  4825. }
  4826. if (cmd->num_resources > CAM_ISP_CTX_RES_MAX) {
  4827. CAM_ERR(CAM_ISP, "Too much resources in the acquire");
  4828. rc = -ENOMEM;
  4829. goto end;
  4830. }
  4831. /* for now we only support user pointer */
  4832. if (cmd->handle_type != 1) {
  4833. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4834. rc = -EINVAL;
  4835. goto end;
  4836. }
  4837. isp_res = kzalloc(
  4838. sizeof(*isp_res)*cmd->num_resources, GFP_KERNEL);
  4839. if (!isp_res) {
  4840. rc = -ENOMEM;
  4841. goto end;
  4842. }
  4843. CAM_DBG(CAM_ISP, "start copy %d resources from user",
  4844. cmd->num_resources);
  4845. if (copy_from_user(isp_res, u64_to_user_ptr(cmd->resource_hdl),
  4846. sizeof(*isp_res)*cmd->num_resources)) {
  4847. rc = -EFAULT;
  4848. goto free_res;
  4849. }
  4850. memset(&param, 0, sizeof(param));
  4851. param.context_data = ctx;
  4852. param.event_cb = ctx->irq_cb_intf;
  4853. param.num_acq = cmd->num_resources;
  4854. param.acquire_info = (uintptr_t) isp_res;
  4855. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  4856. if (rc) {
  4857. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  4858. ctx->ctx_id);
  4859. goto free_res;
  4860. }
  4861. /* call HW manager to reserve the resource */
  4862. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  4863. &param);
  4864. if (rc != 0) {
  4865. CAM_ERR(CAM_ISP, "Acquire device failed");
  4866. goto free_res;
  4867. }
  4868. /* Query the context has rdi only resource */
  4869. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  4870. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  4871. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  4872. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  4873. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  4874. &hw_cmd_args);
  4875. if (rc) {
  4876. CAM_ERR(CAM_ISP, "HW command failed");
  4877. goto free_hw;
  4878. }
  4879. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  4880. /*
  4881. * this context has rdi only resource assign rdi only
  4882. * state machine
  4883. */
  4884. CAM_DBG(CAM_ISP, "RDI only session Context");
  4885. ctx_isp->substate_machine_irq =
  4886. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  4887. ctx_isp->substate_machine =
  4888. cam_isp_ctx_rdi_only_activated_state_machine;
  4889. ctx_isp->rdi_only_context = true;
  4890. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  4891. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  4892. ctx_isp->substate_machine_irq =
  4893. cam_isp_ctx_fs2_state_machine_irq;
  4894. ctx_isp->substate_machine =
  4895. cam_isp_ctx_fs2_state_machine;
  4896. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  4897. CAM_DBG(CAM_ISP, "offline Session has PIX and RD resources");
  4898. ctx_isp->substate_machine_irq =
  4899. cam_isp_ctx_offline_state_machine_irq;
  4900. } else {
  4901. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  4902. ctx_isp->substate_machine_irq =
  4903. cam_isp_ctx_activated_state_machine_irq;
  4904. ctx_isp->substate_machine =
  4905. cam_isp_ctx_activated_state_machine;
  4906. }
  4907. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  4908. ctx_isp->hw_acquired = true;
  4909. ctx_isp->split_acquire = false;
  4910. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  4911. atomic64_set(&ctx_isp->state_monitor_head, -1);
  4912. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  4913. atomic64_set(&ctx_isp->event_record_head[i], -1);
  4914. kfree(isp_res);
  4915. isp_res = NULL;
  4916. get_dev_handle:
  4917. req_hdl_param.session_hdl = cmd->session_handle;
  4918. /* bridge is not ready for these flags. so false for now */
  4919. req_hdl_param.v4l2_sub_dev_flag = 0;
  4920. req_hdl_param.media_entity_flag = 0;
  4921. req_hdl_param.ops = ctx->crm_ctx_intf;
  4922. req_hdl_param.priv = ctx;
  4923. req_hdl_param.dev_id = CAM_ISP;
  4924. CAM_DBG(CAM_ISP, "get device handle form bridge");
  4925. ctx->dev_hdl = cam_create_device_hdl(&req_hdl_param);
  4926. if (ctx->dev_hdl <= 0) {
  4927. rc = -EFAULT;
  4928. CAM_ERR(CAM_ISP, "Can not create device handle");
  4929. goto free_hw;
  4930. }
  4931. cmd->dev_handle = ctx->dev_hdl;
  4932. /* store session information */
  4933. ctx->session_hdl = cmd->session_handle;
  4934. ctx->state = CAM_CTX_ACQUIRED;
  4935. trace_cam_context_state("ISP", ctx);
  4936. CAM_DBG(CAM_ISP,
  4937. "Acquire success on session_hdl 0x%x num_rsrces %d ctx %u",
  4938. cmd->session_handle, cmd->num_resources, ctx->ctx_id);
  4939. return rc;
  4940. free_hw:
  4941. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  4942. if (ctx_isp->hw_acquired)
  4943. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv,
  4944. &release);
  4945. ctx_isp->hw_ctx = NULL;
  4946. ctx_isp->hw_acquired = false;
  4947. free_res:
  4948. kfree(isp_res);
  4949. end:
  4950. return rc;
  4951. }
  4952. static int __cam_isp_ctx_acquire_hw_v1(struct cam_context *ctx,
  4953. void *args)
  4954. {
  4955. int rc = 0;
  4956. int i;
  4957. struct cam_acquire_hw_cmd_v1 *cmd =
  4958. (struct cam_acquire_hw_cmd_v1 *)args;
  4959. struct cam_hw_acquire_args param;
  4960. struct cam_hw_release_args release;
  4961. struct cam_isp_context *ctx_isp =
  4962. (struct cam_isp_context *) ctx->ctx_priv;
  4963. struct cam_hw_cmd_args hw_cmd_args;
  4964. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  4965. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  4966. if (!ctx->hw_mgr_intf) {
  4967. CAM_ERR(CAM_ISP, "HW interface is not ready");
  4968. rc = -EFAULT;
  4969. goto end;
  4970. }
  4971. CAM_DBG(CAM_ISP,
  4972. "session_hdl 0x%x, hdl type %d, res %lld",
  4973. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  4974. /* for now we only support user pointer */
  4975. if (cmd->handle_type != 1) {
  4976. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  4977. rc = -EINVAL;
  4978. goto end;
  4979. }
  4980. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  4981. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  4982. goto end;
  4983. }
  4984. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  4985. if (!acquire_hw_info) {
  4986. rc = -ENOMEM;
  4987. goto end;
  4988. }
  4989. CAM_DBG(CAM_ISP, "start copy resources from user");
  4990. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  4991. cmd->data_size)) {
  4992. rc = -EFAULT;
  4993. goto free_res;
  4994. }
  4995. memset(&param, 0, sizeof(param));
  4996. param.context_data = ctx;
  4997. param.event_cb = ctx->irq_cb_intf;
  4998. param.num_acq = CAM_API_COMPAT_CONSTANT;
  4999. param.acquire_info_size = cmd->data_size;
  5000. param.acquire_info = (uint64_t) acquire_hw_info;
  5001. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5002. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx,
  5003. &param);
  5004. if (rc) {
  5005. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5006. ctx->ctx_id);
  5007. goto free_res;
  5008. }
  5009. /* call HW manager to reserve the resource */
  5010. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5011. &param);
  5012. if (rc != 0) {
  5013. CAM_ERR(CAM_ISP, "Acquire device failed");
  5014. goto free_res;
  5015. }
  5016. ctx_isp->support_consumed_addr =
  5017. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5018. /* Query the context has rdi only resource */
  5019. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5020. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5021. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5022. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5023. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5024. &hw_cmd_args);
  5025. if (rc) {
  5026. CAM_ERR(CAM_ISP, "HW command failed");
  5027. goto free_hw;
  5028. }
  5029. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5030. /*
  5031. * this context has rdi only resource assign rdi only
  5032. * state machine
  5033. */
  5034. CAM_DBG(CAM_ISP, "RDI only session Context");
  5035. ctx_isp->substate_machine_irq =
  5036. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5037. ctx_isp->substate_machine =
  5038. cam_isp_ctx_rdi_only_activated_state_machine;
  5039. ctx_isp->rdi_only_context = true;
  5040. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5041. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5042. ctx_isp->substate_machine_irq =
  5043. cam_isp_ctx_fs2_state_machine_irq;
  5044. ctx_isp->substate_machine =
  5045. cam_isp_ctx_fs2_state_machine;
  5046. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5047. CAM_DBG(CAM_ISP, "Offline session has PIX and RD resources");
  5048. ctx_isp->substate_machine_irq =
  5049. cam_isp_ctx_offline_state_machine_irq;
  5050. ctx_isp->substate_machine = NULL;
  5051. } else {
  5052. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5053. ctx_isp->substate_machine_irq =
  5054. cam_isp_ctx_activated_state_machine_irq;
  5055. ctx_isp->substate_machine =
  5056. cam_isp_ctx_activated_state_machine;
  5057. }
  5058. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5059. ctx_isp->hw_acquired = true;
  5060. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5061. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5062. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5063. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5064. trace_cam_context_state("ISP", ctx);
  5065. CAM_DBG(CAM_ISP,
  5066. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5067. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5068. kfree(acquire_hw_info);
  5069. return rc;
  5070. free_hw:
  5071. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5072. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5073. ctx_isp->hw_ctx = NULL;
  5074. ctx_isp->hw_acquired = false;
  5075. free_res:
  5076. kfree(acquire_hw_info);
  5077. end:
  5078. return rc;
  5079. }
  5080. static void cam_req_mgr_process_workq_offline_ife_worker(struct work_struct *w)
  5081. {
  5082. cam_req_mgr_process_workq(w);
  5083. }
  5084. static int __cam_isp_ctx_acquire_hw_v2(struct cam_context *ctx,
  5085. void *args)
  5086. {
  5087. int rc = 0, i, j;
  5088. struct cam_acquire_hw_cmd_v2 *cmd =
  5089. (struct cam_acquire_hw_cmd_v2 *)args;
  5090. struct cam_hw_acquire_args param;
  5091. struct cam_hw_release_args release;
  5092. struct cam_isp_context *ctx_isp =
  5093. (struct cam_isp_context *) ctx->ctx_priv;
  5094. struct cam_hw_cmd_args hw_cmd_args;
  5095. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5096. struct cam_isp_acquire_hw_info *acquire_hw_info = NULL;
  5097. if (!ctx->hw_mgr_intf) {
  5098. CAM_ERR(CAM_ISP, "HW interface is not ready");
  5099. rc = -EFAULT;
  5100. goto end;
  5101. }
  5102. CAM_DBG(CAM_ISP,
  5103. "session_hdl 0x%x, hdl type %d, res %lld",
  5104. cmd->session_handle, cmd->handle_type, cmd->resource_hdl);
  5105. /* for now we only support user pointer */
  5106. if (cmd->handle_type != 1) {
  5107. CAM_ERR(CAM_ISP, "Only user pointer is supported");
  5108. rc = -EINVAL;
  5109. goto end;
  5110. }
  5111. if (cmd->data_size < sizeof(*acquire_hw_info)) {
  5112. CAM_ERR(CAM_ISP, "data_size is not a valid value");
  5113. goto end;
  5114. }
  5115. acquire_hw_info = kzalloc(cmd->data_size, GFP_KERNEL);
  5116. if (!acquire_hw_info) {
  5117. rc = -ENOMEM;
  5118. goto end;
  5119. }
  5120. CAM_DBG(CAM_ISP, "start copy resources from user");
  5121. if (copy_from_user(acquire_hw_info, (void __user *)cmd->resource_hdl,
  5122. cmd->data_size)) {
  5123. rc = -EFAULT;
  5124. goto free_res;
  5125. }
  5126. memset(&param, 0, sizeof(param));
  5127. param.context_data = ctx;
  5128. param.event_cb = ctx->irq_cb_intf;
  5129. param.num_acq = CAM_API_COMPAT_CONSTANT;
  5130. param.acquire_info_size = cmd->data_size;
  5131. param.acquire_info = (uint64_t) acquire_hw_info;
  5132. param.mini_dump_cb = __cam_isp_ctx_minidump_cb;
  5133. /* call HW manager to reserve the resource */
  5134. rc = ctx->hw_mgr_intf->hw_acquire(ctx->hw_mgr_intf->hw_mgr_priv,
  5135. &param);
  5136. if (rc != 0) {
  5137. CAM_ERR(CAM_ISP, "Acquire device failed");
  5138. goto free_res;
  5139. }
  5140. rc = __cam_isp_ctx_allocate_mem_hw_entries(ctx, &param);
  5141. if (rc) {
  5142. CAM_ERR(CAM_ISP, "Ctx[%d] allocate hw entry fail",
  5143. ctx->ctx_id);
  5144. goto free_hw;
  5145. }
  5146. /*
  5147. * Set feature flag if applicable
  5148. * custom hw is supported only on v2
  5149. */
  5150. ctx_isp->custom_enabled =
  5151. (param.op_flags & CAM_IFE_CTX_CUSTOM_EN);
  5152. ctx_isp->use_frame_header_ts =
  5153. (param.op_flags & CAM_IFE_CTX_FRAME_HEADER_EN);
  5154. ctx_isp->use_default_apply =
  5155. (param.op_flags & CAM_IFE_CTX_APPLY_DEFAULT_CFG);
  5156. ctx_isp->support_consumed_addr =
  5157. (param.op_flags & CAM_IFE_CTX_CONSUME_ADDR_EN);
  5158. ctx_isp->aeb_enabled =
  5159. (param.op_flags & CAM_IFE_CTX_AEB_EN);
  5160. if ((ctx_isp->aeb_enabled) && (!isp_ctx_debug.disable_internal_recovery))
  5161. ctx_isp->do_internal_recovery = true;
  5162. /* Query the context has rdi only resource */
  5163. hw_cmd_args.ctxt_to_hw_map = param.ctxt_to_hw_map;
  5164. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5165. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_CTX_TYPE;
  5166. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5167. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5168. &hw_cmd_args);
  5169. if (rc) {
  5170. CAM_ERR(CAM_ISP, "HW command failed");
  5171. goto free_hw;
  5172. }
  5173. if (param.valid_acquired_hw) {
  5174. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5175. cmd->hw_info.acquired_hw_id[i] =
  5176. param.acquired_hw_id[i];
  5177. for (i = 0; i < CAM_MAX_ACQ_RES; i++)
  5178. for (j = 0; j < CAM_MAX_HW_SPLIT; j++)
  5179. cmd->hw_info.acquired_hw_path[i][j] =
  5180. param.acquired_hw_path[i][j];
  5181. }
  5182. cmd->hw_info.valid_acquired_hw =
  5183. param.valid_acquired_hw;
  5184. cmd->hw_info.valid_acquired_hw = param.valid_acquired_hw;
  5185. if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_RDI) {
  5186. /*
  5187. * this context has rdi only resource assign rdi only
  5188. * state machine
  5189. */
  5190. CAM_DBG(CAM_ISP, "RDI only session Context");
  5191. ctx_isp->substate_machine_irq =
  5192. cam_isp_ctx_rdi_only_activated_state_machine_irq;
  5193. ctx_isp->substate_machine =
  5194. cam_isp_ctx_rdi_only_activated_state_machine;
  5195. ctx_isp->rdi_only_context = true;
  5196. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_FS2) {
  5197. CAM_DBG(CAM_ISP, "FS2 Session has PIX, RD and RDI");
  5198. ctx_isp->substate_machine_irq =
  5199. cam_isp_ctx_fs2_state_machine_irq;
  5200. ctx_isp->substate_machine =
  5201. cam_isp_ctx_fs2_state_machine;
  5202. } else if (isp_hw_cmd_args.u.ctx_type == CAM_ISP_CTX_OFFLINE) {
  5203. CAM_DBG(CAM_ISP, "Offline Session has PIX and RD resources");
  5204. ctx_isp->substate_machine_irq =
  5205. cam_isp_ctx_offline_state_machine_irq;
  5206. ctx_isp->substate_machine = NULL;
  5207. ctx_isp->offline_context = true;
  5208. rc = cam_req_mgr_workq_create("offline_ife", 20,
  5209. &ctx_isp->workq, CRM_WORKQ_USAGE_IRQ, 0,
  5210. cam_req_mgr_process_workq_offline_ife_worker);
  5211. if (rc)
  5212. CAM_ERR(CAM_ISP,
  5213. "Failed to create workq for offline IFE rc:%d",
  5214. rc);
  5215. } else {
  5216. CAM_DBG(CAM_ISP, "Session has PIX or PIX and RDI resources");
  5217. ctx_isp->substate_machine_irq =
  5218. cam_isp_ctx_activated_state_machine_irq;
  5219. ctx_isp->substate_machine =
  5220. cam_isp_ctx_activated_state_machine;
  5221. }
  5222. ctx_isp->hw_ctx = param.ctxt_to_hw_map;
  5223. ctx_isp->hw_acquired = true;
  5224. ctx->ctxt_to_hw_map = param.ctxt_to_hw_map;
  5225. trace_cam_context_state("ISP", ctx);
  5226. CAM_DBG(CAM_ISP,
  5227. "Acquire success on session_hdl 0x%xs ctx_type %d ctx_id %u",
  5228. ctx->session_hdl, isp_hw_cmd_args.u.ctx_type, ctx->ctx_id);
  5229. kfree(acquire_hw_info);
  5230. return rc;
  5231. free_hw:
  5232. release.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5233. ctx->hw_mgr_intf->hw_release(ctx->hw_mgr_intf->hw_mgr_priv, &release);
  5234. ctx_isp->hw_ctx = NULL;
  5235. ctx_isp->hw_acquired = false;
  5236. free_res:
  5237. kfree(acquire_hw_info);
  5238. end:
  5239. return rc;
  5240. }
  5241. static int __cam_isp_ctx_acquire_hw_in_acquired(struct cam_context *ctx,
  5242. void *args)
  5243. {
  5244. int rc = -EINVAL;
  5245. uint32_t api_version;
  5246. if (!ctx || !args) {
  5247. CAM_ERR(CAM_ISP, "Invalid input pointer");
  5248. return rc;
  5249. }
  5250. api_version = *((uint32_t *)args);
  5251. if (api_version == 1)
  5252. rc = __cam_isp_ctx_acquire_hw_v1(ctx, args);
  5253. else if (api_version == 2)
  5254. rc = __cam_isp_ctx_acquire_hw_v2(ctx, args);
  5255. else
  5256. CAM_ERR(CAM_ISP, "Unsupported api version %d", api_version);
  5257. return rc;
  5258. }
  5259. static int __cam_isp_ctx_config_dev_in_acquired(struct cam_context *ctx,
  5260. struct cam_config_dev_cmd *cmd)
  5261. {
  5262. int rc = 0;
  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. return -EINVAL;
  5268. }
  5269. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5270. if (!rc && ((ctx->link_hdl >= 0) || ctx_isp->offline_context)) {
  5271. ctx->state = CAM_CTX_READY;
  5272. trace_cam_context_state("ISP", ctx);
  5273. }
  5274. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5275. return rc;
  5276. }
  5277. static int __cam_isp_ctx_config_dev_in_flushed(struct cam_context *ctx,
  5278. struct cam_config_dev_cmd *cmd)
  5279. {
  5280. int rc = 0;
  5281. struct cam_start_stop_dev_cmd start_cmd;
  5282. struct cam_hw_cmd_args hw_cmd_args;
  5283. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5284. struct cam_isp_context *ctx_isp =
  5285. (struct cam_isp_context *) ctx->ctx_priv;
  5286. if (!ctx_isp->hw_acquired) {
  5287. CAM_ERR(CAM_ISP, "HW is not acquired, reject packet");
  5288. rc = -EINVAL;
  5289. goto end;
  5290. }
  5291. rc = __cam_isp_ctx_config_dev_in_top_state(ctx, cmd);
  5292. if (rc)
  5293. goto end;
  5294. if (!ctx_isp->init_received) {
  5295. CAM_WARN(CAM_ISP,
  5296. "Received update packet in flushed state, skip start");
  5297. goto end;
  5298. }
  5299. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5300. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5301. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5302. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5303. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5304. &hw_cmd_args);
  5305. if (rc) {
  5306. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d", rc);
  5307. goto end;
  5308. }
  5309. start_cmd.dev_handle = cmd->dev_handle;
  5310. start_cmd.session_handle = cmd->session_handle;
  5311. rc = __cam_isp_ctx_start_dev_in_ready(ctx, &start_cmd);
  5312. if (rc)
  5313. CAM_ERR(CAM_ISP,
  5314. "Failed to re-start HW after flush rc: %d", rc);
  5315. else
  5316. CAM_INFO(CAM_ISP,
  5317. "Received init after flush. Re-start HW complete in ctx:%d",
  5318. ctx->ctx_id);
  5319. end:
  5320. CAM_DBG(CAM_ISP, "next state %d sub_state:%d", ctx->state,
  5321. ctx_isp->substate_activated);
  5322. return rc;
  5323. }
  5324. static int __cam_isp_ctx_link_in_acquired(struct cam_context *ctx,
  5325. struct cam_req_mgr_core_dev_link_setup *link)
  5326. {
  5327. int rc = 0;
  5328. struct cam_isp_context *ctx_isp =
  5329. (struct cam_isp_context *) ctx->ctx_priv;
  5330. if (!link) {
  5331. CAM_ERR(CAM_ISP, "setup link info is null: %pK ctx: %u",
  5332. link, ctx->ctx_id);
  5333. return -EINVAL;
  5334. }
  5335. if (!link->crm_cb) {
  5336. CAM_ERR(CAM_ISP, "crm cb is null: %pK ctx: %u",
  5337. link->crm_cb, ctx->ctx_id);
  5338. return -EINVAL;
  5339. }
  5340. CAM_DBG(CAM_ISP, "Enter.........");
  5341. ctx->link_hdl = link->link_hdl;
  5342. ctx->ctx_crm_intf = link->crm_cb;
  5343. ctx_isp->subscribe_event =
  5344. CAM_TRIGGER_POINT_SOF | CAM_TRIGGER_POINT_EOF;
  5345. ctx_isp->trigger_id = link->trigger_id;
  5346. /* change state only if we had the init config */
  5347. if (ctx_isp->init_received) {
  5348. ctx->state = CAM_CTX_READY;
  5349. trace_cam_context_state("ISP", ctx);
  5350. }
  5351. CAM_DBG(CAM_ISP, "next state %d", ctx->state);
  5352. return rc;
  5353. }
  5354. static int __cam_isp_ctx_unlink_in_acquired(struct cam_context *ctx,
  5355. struct cam_req_mgr_core_dev_link_setup *unlink)
  5356. {
  5357. int rc = 0;
  5358. struct cam_isp_context *ctx_isp =
  5359. (struct cam_isp_context *) ctx->ctx_priv;
  5360. ctx->link_hdl = -1;
  5361. ctx->ctx_crm_intf = NULL;
  5362. ctx_isp->trigger_id = -1;
  5363. return rc;
  5364. }
  5365. static int __cam_isp_ctx_get_dev_info_in_acquired(struct cam_context *ctx,
  5366. struct cam_req_mgr_device_info *dev_info)
  5367. {
  5368. int rc = 0;
  5369. dev_info->dev_hdl = ctx->dev_hdl;
  5370. strlcpy(dev_info->name, CAM_ISP_DEV_NAME, sizeof(dev_info->name));
  5371. dev_info->dev_id = CAM_REQ_MGR_DEVICE_IFE;
  5372. dev_info->p_delay = 1;
  5373. dev_info->trigger = CAM_TRIGGER_POINT_SOF;
  5374. dev_info->trigger_on = true;
  5375. return rc;
  5376. }
  5377. static inline void __cam_isp_context_reset_ctx_params(
  5378. struct cam_isp_context *ctx_isp)
  5379. {
  5380. atomic_set(&ctx_isp->process_bubble, 0);
  5381. atomic_set(&ctx_isp->rxd_epoch, 0);
  5382. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5383. ctx_isp->frame_id = 0;
  5384. ctx_isp->sof_timestamp_val = 0;
  5385. ctx_isp->boot_timestamp = 0;
  5386. ctx_isp->active_req_cnt = 0;
  5387. ctx_isp->reported_req_id = 0;
  5388. ctx_isp->bubble_frame_cnt = 0;
  5389. ctx_isp->recovery_req_id = 0;
  5390. }
  5391. static int __cam_isp_ctx_start_dev_in_ready(struct cam_context *ctx,
  5392. struct cam_start_stop_dev_cmd *cmd)
  5393. {
  5394. int rc = 0;
  5395. int i;
  5396. struct cam_isp_start_args start_isp;
  5397. struct cam_ctx_request *req;
  5398. struct cam_isp_ctx_req *req_isp;
  5399. struct cam_isp_context *ctx_isp =
  5400. (struct cam_isp_context *) ctx->ctx_priv;
  5401. if (cmd->session_handle != ctx->session_hdl ||
  5402. cmd->dev_handle != ctx->dev_hdl) {
  5403. rc = -EPERM;
  5404. goto end;
  5405. }
  5406. if (list_empty(&ctx->pending_req_list)) {
  5407. /* should never happen */
  5408. CAM_ERR(CAM_ISP, "Start device with empty configuration");
  5409. rc = -EFAULT;
  5410. goto end;
  5411. } else {
  5412. req = list_first_entry(&ctx->pending_req_list,
  5413. struct cam_ctx_request, list);
  5414. }
  5415. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5416. if (!ctx_isp->hw_ctx) {
  5417. CAM_ERR(CAM_ISP, "Wrong hw context pointer.");
  5418. rc = -EFAULT;
  5419. goto end;
  5420. }
  5421. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5422. start_isp.hw_config.request_id = req->request_id;
  5423. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5424. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5425. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5426. start_isp.hw_config.init_packet = 1;
  5427. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_NONE;
  5428. start_isp.hw_config.cdm_reset_before_apply = false;
  5429. start_isp.is_internal_start = false;
  5430. ctx_isp->last_applied_req_id = req->request_id;
  5431. if (ctx->state == CAM_CTX_FLUSHED)
  5432. start_isp.start_only = true;
  5433. else
  5434. start_isp.start_only = false;
  5435. __cam_isp_context_reset_ctx_params(ctx_isp);
  5436. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5437. CAM_ISP_CTX_ACTIVATED_APPLIED :
  5438. (req_isp->num_fence_map_out) ? CAM_ISP_CTX_ACTIVATED_EPOCH :
  5439. CAM_ISP_CTX_ACTIVATED_SOF;
  5440. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5441. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5442. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5443. /*
  5444. * In case of CSID TPG we might receive SOF and RUP IRQs
  5445. * before hw_mgr_intf->hw_start has returned. So move
  5446. * req out of pending list before hw_start and add it
  5447. * back to pending list if hw_start fails.
  5448. */
  5449. list_del_init(&req->list);
  5450. if (ctx_isp->offline_context && !req_isp->num_fence_map_out) {
  5451. list_add_tail(&req->list, &ctx->free_req_list);
  5452. atomic_set(&ctx_isp->rxd_epoch, 1);
  5453. CAM_DBG(CAM_REQ,
  5454. "Move pending req: %lld to free list(cnt: %d) offline ctx %u",
  5455. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5456. } else if (ctx_isp->rdi_only_context || !req_isp->num_fence_map_out) {
  5457. list_add_tail(&req->list, &ctx->wait_req_list);
  5458. CAM_DBG(CAM_REQ,
  5459. "Move pending req: %lld to wait list(cnt: %d) ctx %u",
  5460. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id);
  5461. } else {
  5462. list_add_tail(&req->list, &ctx->active_req_list);
  5463. ctx_isp->active_req_cnt++;
  5464. CAM_DBG(CAM_REQ,
  5465. "Move pending req: %lld to active list(cnt: %d) ctx %u offline %d",
  5466. req->request_id, ctx_isp->active_req_cnt, ctx->ctx_id,
  5467. ctx_isp->offline_context);
  5468. }
  5469. /*
  5470. * Only place to change state before calling the hw due to
  5471. * hardware tasklet has higher priority that can cause the
  5472. * irq handling comes early
  5473. */
  5474. ctx->state = CAM_CTX_ACTIVATED;
  5475. trace_cam_context_state("ISP", ctx);
  5476. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5477. &start_isp);
  5478. if (rc) {
  5479. /* HW failure. user need to clean up the resource */
  5480. CAM_ERR(CAM_ISP, "Start HW failed");
  5481. ctx->state = CAM_CTX_READY;
  5482. if ((rc == -ETIMEDOUT) &&
  5483. (isp_ctx_debug.enable_cdm_cmd_buff_dump))
  5484. rc = cam_isp_ctx_dump_req(req_isp, 0, 0, NULL, false);
  5485. trace_cam_context_state("ISP", ctx);
  5486. list_del_init(&req->list);
  5487. list_add(&req->list, &ctx->pending_req_list);
  5488. goto end;
  5489. }
  5490. CAM_DBG(CAM_ISP, "start device success ctx %u", ctx->ctx_id);
  5491. end:
  5492. return rc;
  5493. }
  5494. static int __cam_isp_ctx_unlink_in_ready(struct cam_context *ctx,
  5495. struct cam_req_mgr_core_dev_link_setup *unlink)
  5496. {
  5497. int rc = 0;
  5498. ctx->link_hdl = -1;
  5499. ctx->ctx_crm_intf = NULL;
  5500. ctx->state = CAM_CTX_ACQUIRED;
  5501. trace_cam_context_state("ISP", ctx);
  5502. return rc;
  5503. }
  5504. static int __cam_isp_ctx_stop_dev_in_activated_unlock(
  5505. struct cam_context *ctx, struct cam_start_stop_dev_cmd *stop_cmd)
  5506. {
  5507. int rc = 0;
  5508. uint32_t i;
  5509. struct cam_hw_stop_args stop;
  5510. struct cam_ctx_request *req;
  5511. struct cam_isp_ctx_req *req_isp;
  5512. struct cam_isp_context *ctx_isp =
  5513. (struct cam_isp_context *) ctx->ctx_priv;
  5514. struct cam_isp_stop_args stop_isp;
  5515. /* Mask off all the incoming hardware events */
  5516. spin_lock_bh(&ctx->lock);
  5517. ctx_isp->substate_activated = CAM_ISP_CTX_ACTIVATED_HALT;
  5518. spin_unlock_bh(&ctx->lock);
  5519. /* stop hw first */
  5520. if (ctx_isp->hw_ctx) {
  5521. stop.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5522. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5523. stop_isp.stop_only = false;
  5524. stop_isp.is_internal_stop = false;
  5525. stop.args = (void *) &stop_isp;
  5526. ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5527. &stop);
  5528. }
  5529. CAM_DBG(CAM_ISP, "next Substate[%s]",
  5530. __cam_isp_ctx_substate_val_to_type(
  5531. ctx_isp->substate_activated));
  5532. if (ctx->ctx_crm_intf &&
  5533. ctx->ctx_crm_intf->notify_stop) {
  5534. struct cam_req_mgr_notify_stop notify;
  5535. notify.link_hdl = ctx->link_hdl;
  5536. CAM_DBG(CAM_ISP,
  5537. "Notify CRM about device stop ctx %u link 0x%x",
  5538. ctx->ctx_id, ctx->link_hdl);
  5539. ctx->ctx_crm_intf->notify_stop(&notify);
  5540. } else if (!ctx_isp->offline_context)
  5541. CAM_ERR(CAM_ISP, "cb not present");
  5542. while (!list_empty(&ctx->pending_req_list)) {
  5543. req = list_first_entry(&ctx->pending_req_list,
  5544. struct cam_ctx_request, list);
  5545. list_del_init(&req->list);
  5546. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5547. CAM_DBG(CAM_ISP, "signal fence in pending list. fence num %d",
  5548. req_isp->num_fence_map_out);
  5549. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5550. if (req_isp->fence_map_out[i].sync_id != -1) {
  5551. cam_sync_signal(
  5552. req_isp->fence_map_out[i].sync_id,
  5553. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5554. CAM_SYNC_ISP_EVENT_HW_STOP);
  5555. }
  5556. list_add_tail(&req->list, &ctx->free_req_list);
  5557. }
  5558. while (!list_empty(&ctx->wait_req_list)) {
  5559. req = list_first_entry(&ctx->wait_req_list,
  5560. struct cam_ctx_request, list);
  5561. list_del_init(&req->list);
  5562. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5563. CAM_DBG(CAM_ISP, "signal fence in wait list. fence num %d",
  5564. req_isp->num_fence_map_out);
  5565. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5566. if (req_isp->fence_map_out[i].sync_id != -1) {
  5567. cam_sync_signal(
  5568. req_isp->fence_map_out[i].sync_id,
  5569. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5570. CAM_SYNC_ISP_EVENT_HW_STOP);
  5571. }
  5572. list_add_tail(&req->list, &ctx->free_req_list);
  5573. }
  5574. while (!list_empty(&ctx->active_req_list)) {
  5575. req = list_first_entry(&ctx->active_req_list,
  5576. struct cam_ctx_request, list);
  5577. list_del_init(&req->list);
  5578. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5579. CAM_DBG(CAM_ISP, "signal fence in active list. fence num %d",
  5580. req_isp->num_fence_map_out);
  5581. for (i = 0; i < req_isp->num_fence_map_out; i++)
  5582. if (req_isp->fence_map_out[i].sync_id != -1) {
  5583. cam_sync_signal(
  5584. req_isp->fence_map_out[i].sync_id,
  5585. CAM_SYNC_STATE_SIGNALED_CANCEL,
  5586. CAM_SYNC_ISP_EVENT_HW_STOP);
  5587. }
  5588. list_add_tail(&req->list, &ctx->free_req_list);
  5589. }
  5590. ctx_isp->frame_id = 0;
  5591. ctx_isp->active_req_cnt = 0;
  5592. ctx_isp->reported_req_id = 0;
  5593. ctx_isp->last_applied_req_id = 0;
  5594. ctx_isp->req_info.last_bufdone_req_id = 0;
  5595. ctx_isp->bubble_frame_cnt = 0;
  5596. atomic_set(&ctx_isp->process_bubble, 0);
  5597. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5598. atomic_set(&ctx_isp->rxd_epoch, 0);
  5599. atomic64_set(&ctx_isp->state_monitor_head, -1);
  5600. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  5601. atomic64_set(&ctx_isp->event_record_head[i], -1);
  5602. CAM_DBG(CAM_ISP, "Stop device success next state %d on ctx %u",
  5603. ctx->state, ctx->ctx_id);
  5604. if (!stop_cmd) {
  5605. rc = __cam_isp_ctx_unlink_in_ready(ctx, NULL);
  5606. if (rc)
  5607. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5608. }
  5609. return rc;
  5610. }
  5611. static int __cam_isp_ctx_stop_dev_in_activated(struct cam_context *ctx,
  5612. struct cam_start_stop_dev_cmd *cmd)
  5613. {
  5614. int rc = 0;
  5615. struct cam_isp_context *ctx_isp =
  5616. (struct cam_isp_context *)ctx->ctx_priv;
  5617. __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, cmd);
  5618. ctx_isp->init_received = false;
  5619. ctx->state = CAM_CTX_ACQUIRED;
  5620. trace_cam_context_state("ISP", ctx);
  5621. return rc;
  5622. }
  5623. static int __cam_isp_ctx_release_dev_in_activated(struct cam_context *ctx,
  5624. struct cam_release_dev_cmd *cmd)
  5625. {
  5626. int rc = 0;
  5627. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5628. if (rc)
  5629. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5630. rc = __cam_isp_ctx_release_dev_in_top_state(ctx, cmd);
  5631. if (rc)
  5632. CAM_ERR(CAM_ISP, "Release device failed rc=%d", rc);
  5633. return rc;
  5634. }
  5635. static int __cam_isp_ctx_release_hw_in_activated(struct cam_context *ctx,
  5636. void *cmd)
  5637. {
  5638. int rc = 0;
  5639. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5640. if (rc)
  5641. CAM_ERR(CAM_ISP, "Stop device failed rc=%d", rc);
  5642. rc = __cam_isp_ctx_release_hw_in_top_state(ctx, cmd);
  5643. if (rc)
  5644. CAM_ERR(CAM_ISP, "Release hw failed rc=%d", rc);
  5645. return rc;
  5646. }
  5647. static int __cam_isp_ctx_link_pause(struct cam_context *ctx)
  5648. {
  5649. int rc = 0;
  5650. struct cam_hw_cmd_args hw_cmd_args;
  5651. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5652. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5653. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5654. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_PAUSE_HW;
  5655. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5656. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5657. &hw_cmd_args);
  5658. return rc;
  5659. }
  5660. static int __cam_isp_ctx_link_resume(struct cam_context *ctx)
  5661. {
  5662. int rc = 0;
  5663. struct cam_hw_cmd_args hw_cmd_args;
  5664. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5665. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5666. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5667. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5668. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5669. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5670. &hw_cmd_args);
  5671. return rc;
  5672. }
  5673. static int __cam_isp_ctx_handle_sof_freeze_evt(
  5674. struct cam_context *ctx)
  5675. {
  5676. int rc = 0;
  5677. struct cam_hw_cmd_args hw_cmd_args;
  5678. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5679. hw_cmd_args.ctxt_to_hw_map = ctx->ctxt_to_hw_map;
  5680. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5681. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_SOF_DEBUG;
  5682. isp_hw_cmd_args.u.sof_irq_enable = 1;
  5683. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5684. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5685. &hw_cmd_args);
  5686. return rc;
  5687. }
  5688. static int __cam_isp_ctx_reset_and_recover(
  5689. bool skip_resume, struct cam_context *ctx)
  5690. {
  5691. int rc = 0;
  5692. struct cam_isp_context *ctx_isp =
  5693. (struct cam_isp_context *)ctx->ctx_priv;
  5694. struct cam_isp_stop_args stop_isp;
  5695. struct cam_hw_stop_args stop_args;
  5696. struct cam_isp_start_args start_isp;
  5697. struct cam_hw_cmd_args hw_cmd_args;
  5698. struct cam_isp_hw_cmd_args isp_hw_cmd_args;
  5699. struct cam_ctx_request *req;
  5700. struct cam_isp_ctx_req *req_isp;
  5701. spin_lock(&ctx->lock);
  5702. if (ctx_isp->active_req_cnt) {
  5703. spin_unlock(&ctx->lock);
  5704. CAM_WARN(CAM_ISP,
  5705. "Active list not empty: %u in ctx: %u on link: 0x%x, retry recovery for req: %lld after buf_done",
  5706. ctx_isp->active_req_cnt, ctx->ctx_id,
  5707. ctx->link_hdl, ctx_isp->recovery_req_id);
  5708. goto end;
  5709. }
  5710. if (ctx->state != CAM_CTX_ACTIVATED) {
  5711. spin_unlock(&ctx->lock);
  5712. CAM_ERR(CAM_ISP,
  5713. "In wrong state %d, for recovery ctx: %u in link: 0x%x recovery req: %lld",
  5714. ctx->state, ctx->ctx_id,
  5715. ctx->link_hdl, ctx_isp->recovery_req_id);
  5716. rc = -EINVAL;
  5717. goto end;
  5718. }
  5719. if (list_empty(&ctx->pending_req_list)) {
  5720. /* Cannot start with no request */
  5721. spin_unlock(&ctx->lock);
  5722. CAM_ERR(CAM_ISP,
  5723. "Failed to reset and recover last_applied_req: %llu in ctx: %u on link: 0x%x",
  5724. ctx_isp->last_applied_req_id, ctx->ctx_id, ctx->link_hdl);
  5725. rc = -EFAULT;
  5726. goto end;
  5727. }
  5728. spin_unlock(&ctx->lock);
  5729. if (!ctx_isp->hw_ctx) {
  5730. CAM_ERR(CAM_ISP,
  5731. "Invalid hw context pointer ctx: %u on link: 0x%x",
  5732. ctx->ctx_id, ctx->link_hdl);
  5733. rc = -EFAULT;
  5734. goto end;
  5735. }
  5736. req = list_first_entry(&ctx->pending_req_list,
  5737. struct cam_ctx_request, list);
  5738. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  5739. req_isp->bubble_detected = false;
  5740. CAM_INFO(CAM_ISP,
  5741. "Trigger Halt, Reset & Resume for req: %llu ctx: %u in state: %d link: 0x%x",
  5742. req->request_id, ctx->ctx_id, ctx->state, ctx->link_hdl);
  5743. stop_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5744. stop_isp.hw_stop_cmd = CAM_ISP_HW_STOP_IMMEDIATELY;
  5745. stop_isp.stop_only = true;
  5746. stop_isp.is_internal_stop = true;
  5747. stop_args.args = (void *)&stop_isp;
  5748. rc = ctx->hw_mgr_intf->hw_stop(ctx->hw_mgr_intf->hw_mgr_priv,
  5749. &stop_args);
  5750. if (rc) {
  5751. CAM_ERR(CAM_ISP, "Failed to stop HW rc: %d ctx: %u",
  5752. rc, ctx->ctx_id);
  5753. goto end;
  5754. }
  5755. CAM_DBG(CAM_ISP, "Stop HW success ctx: %u link: 0x%x",
  5756. ctx->ctx_id, ctx->link_hdl);
  5757. /* API provides provision to stream off and not resume as well in case of fatal errors */
  5758. if (skip_resume) {
  5759. atomic_set(&ctx_isp->internal_recovery_set, 0);
  5760. CAM_INFO(CAM_ISP,
  5761. "Halting streaming off IFE/SFE ctx: %u last_applied_req: %lld [recovery_req: %lld] on link: 0x%x",
  5762. ctx->ctx_id, ctx_isp->last_applied_req_id,
  5763. ctx_isp->recovery_req_id, ctx->link_hdl);
  5764. goto end;
  5765. }
  5766. hw_cmd_args.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5767. hw_cmd_args.cmd_type = CAM_HW_MGR_CMD_INTERNAL;
  5768. isp_hw_cmd_args.cmd_type = CAM_ISP_HW_MGR_CMD_RESUME_HW;
  5769. hw_cmd_args.u.internal_args = (void *)&isp_hw_cmd_args;
  5770. rc = ctx->hw_mgr_intf->hw_cmd(ctx->hw_mgr_intf->hw_mgr_priv,
  5771. &hw_cmd_args);
  5772. if (rc) {
  5773. CAM_ERR(CAM_ISP, "Failed to resume HW rc: %d ctx: %u", rc, ctx->ctx_id);
  5774. goto end;
  5775. }
  5776. CAM_DBG(CAM_ISP, "Resume call success ctx: %u on link: 0x%x",
  5777. ctx->ctx_id, ctx->link_hdl);
  5778. start_isp.hw_config.ctxt_to_hw_map = ctx_isp->hw_ctx;
  5779. start_isp.hw_config.request_id = req->request_id;
  5780. start_isp.hw_config.hw_update_entries = req_isp->cfg;
  5781. start_isp.hw_config.num_hw_update_entries = req_isp->num_cfg;
  5782. start_isp.hw_config.priv = &req_isp->hw_update_data;
  5783. start_isp.hw_config.init_packet = 1;
  5784. start_isp.hw_config.reapply_type = CAM_CONFIG_REAPPLY_IQ;
  5785. start_isp.hw_config.cdm_reset_before_apply = false;
  5786. start_isp.start_only = true;
  5787. start_isp.is_internal_start = true;
  5788. __cam_isp_context_reset_internal_recovery_params(ctx_isp);
  5789. ctx_isp->substate_activated = ctx_isp->rdi_only_context ?
  5790. CAM_ISP_CTX_ACTIVATED_APPLIED : CAM_ISP_CTX_ACTIVATED_SOF;
  5791. rc = ctx->hw_mgr_intf->hw_start(ctx->hw_mgr_intf->hw_mgr_priv,
  5792. &start_isp);
  5793. if (rc) {
  5794. CAM_ERR(CAM_ISP, "Start HW failed");
  5795. ctx->state = CAM_CTX_READY;
  5796. goto end;
  5797. }
  5798. /* IQ applied for this request, on next trigger skip IQ cfg */
  5799. req_isp->reapply_type = CAM_CONFIG_REAPPLY_IO;
  5800. CAM_DBG(CAM_ISP, "Internal Start HW success ctx %u on link: 0x%x",
  5801. ctx->ctx_id, ctx->link_hdl);
  5802. end:
  5803. return rc;
  5804. }
  5805. static int __cam_isp_ctx_process_evt(struct cam_context *ctx,
  5806. struct cam_req_mgr_link_evt_data *link_evt_data)
  5807. {
  5808. int rc = 0;
  5809. switch (link_evt_data->evt_type) {
  5810. case CAM_REQ_MGR_LINK_EVT_ERR:
  5811. /* No handling */
  5812. break;
  5813. case CAM_REQ_MGR_LINK_EVT_PAUSE:
  5814. rc = __cam_isp_ctx_link_pause(ctx);
  5815. break;
  5816. case CAM_REQ_MGR_LINK_EVT_RESUME:
  5817. rc = __cam_isp_ctx_link_resume(ctx);
  5818. break;
  5819. case CAM_REQ_MGR_LINK_EVT_SOF_FREEZE:
  5820. rc = __cam_isp_ctx_handle_sof_freeze_evt(ctx);
  5821. break;
  5822. case CAM_REQ_MGR_LINK_EVT_STALLED:
  5823. if (ctx->state == CAM_CTX_ACTIVATED)
  5824. rc = __cam_isp_ctx_trigger_reg_dump(CAM_HW_MGR_CMD_REG_DUMP_ON_ERROR, ctx);
  5825. break;
  5826. default:
  5827. CAM_WARN(CAM_ISP,
  5828. "Unsupported event type: 0x%x on ctx: %u",
  5829. link_evt_data->evt_type, ctx->ctx_id);
  5830. rc = -EINVAL;
  5831. break;
  5832. }
  5833. return rc;
  5834. }
  5835. static int __cam_isp_ctx_unlink_in_activated(struct cam_context *ctx,
  5836. struct cam_req_mgr_core_dev_link_setup *unlink)
  5837. {
  5838. int rc = 0;
  5839. CAM_WARN(CAM_ISP,
  5840. "Received unlink in activated state. It's unexpected");
  5841. rc = __cam_isp_ctx_stop_dev_in_activated_unlock(ctx, NULL);
  5842. if (rc)
  5843. CAM_WARN(CAM_ISP, "Stop device failed rc=%d", rc);
  5844. rc = __cam_isp_ctx_unlink_in_ready(ctx, unlink);
  5845. if (rc)
  5846. CAM_ERR(CAM_ISP, "Unlink failed rc=%d", rc);
  5847. return rc;
  5848. }
  5849. static int __cam_isp_ctx_apply_req(struct cam_context *ctx,
  5850. struct cam_req_mgr_apply_request *apply)
  5851. {
  5852. int rc = 0;
  5853. struct cam_ctx_ops *ctx_ops = NULL;
  5854. struct cam_isp_context *ctx_isp =
  5855. (struct cam_isp_context *) ctx->ctx_priv;
  5856. trace_cam_apply_req("ISP", ctx->ctx_id, apply->request_id, apply->link_hdl);
  5857. CAM_DBG(CAM_ISP, "Enter: apply req in Substate[%s] request_id:%lld",
  5858. __cam_isp_ctx_substate_val_to_type(
  5859. ctx_isp->substate_activated), apply->request_id);
  5860. ctx_ops = &ctx_isp->substate_machine[ctx_isp->substate_activated];
  5861. if (ctx_ops->crm_ops.apply_req) {
  5862. rc = ctx_ops->crm_ops.apply_req(ctx, apply);
  5863. } else {
  5864. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5865. "No handle function in activated Substate[%s]",
  5866. __cam_isp_ctx_substate_val_to_type(
  5867. ctx_isp->substate_activated));
  5868. rc = -EFAULT;
  5869. }
  5870. if (rc)
  5871. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5872. "Apply failed in active Substate[%s] rc %d",
  5873. __cam_isp_ctx_substate_val_to_type(
  5874. ctx_isp->substate_activated), rc);
  5875. return rc;
  5876. }
  5877. static int __cam_isp_ctx_apply_default_settings(
  5878. struct cam_context *ctx,
  5879. struct cam_req_mgr_apply_request *apply)
  5880. {
  5881. int rc = 0;
  5882. struct cam_ctx_ops *ctx_ops = NULL;
  5883. struct cam_isp_context *ctx_isp =
  5884. (struct cam_isp_context *) ctx->ctx_priv;
  5885. if (!ctx_isp->use_default_apply)
  5886. return 0;
  5887. if (!(apply->trigger_point & ctx_isp->subscribe_event)) {
  5888. CAM_WARN(CAM_ISP,
  5889. "Trigger: %u not subscribed for: %u",
  5890. apply->trigger_point, ctx_isp->subscribe_event);
  5891. return 0;
  5892. }
  5893. /* Allow apply default settings for IFE only at SOF */
  5894. if (apply->trigger_point != CAM_TRIGGER_POINT_SOF)
  5895. return 0;
  5896. if ((ctx_isp->aeb_enabled) && (atomic_read(&ctx_isp->internal_recovery_set)))
  5897. return __cam_isp_ctx_reset_and_recover(false, ctx);
  5898. CAM_DBG(CAM_ISP,
  5899. "Enter: apply req in Substate:%d request _id:%lld ctx:%u on link:0x%x",
  5900. ctx_isp->substate_activated, apply->request_id,
  5901. ctx->ctx_id, ctx->link_hdl);
  5902. ctx_ops = &ctx_isp->substate_machine[
  5903. ctx_isp->substate_activated];
  5904. if (ctx_ops->crm_ops.notify_frame_skip) {
  5905. rc = ctx_ops->crm_ops.notify_frame_skip(ctx, apply);
  5906. } else {
  5907. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5908. "No handle function in activated substate %d",
  5909. ctx_isp->substate_activated);
  5910. rc = -EFAULT;
  5911. }
  5912. if (rc)
  5913. CAM_WARN_RATE_LIMIT(CAM_ISP,
  5914. "Apply default failed in active substate %d rc %d",
  5915. ctx_isp->substate_activated, rc);
  5916. return rc;
  5917. }
  5918. static int __cam_isp_ctx_handle_irq_in_activated(void *context,
  5919. uint32_t evt_id, void *evt_data)
  5920. {
  5921. int rc = 0;
  5922. struct cam_isp_ctx_irq_ops *irq_ops = NULL;
  5923. struct cam_context *ctx = (struct cam_context *)context;
  5924. struct cam_isp_context *ctx_isp =
  5925. (struct cam_isp_context *)ctx->ctx_priv;
  5926. spin_lock(&ctx->lock);
  5927. trace_cam_isp_activated_irq(ctx, ctx_isp->substate_activated, evt_id,
  5928. __cam_isp_ctx_get_event_ts(evt_id, evt_data));
  5929. CAM_DBG(CAM_ISP, "Enter: State %d, Substate[%s], evt id %d",
  5930. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5931. ctx_isp->substate_activated), evt_id);
  5932. irq_ops = &ctx_isp->substate_machine_irq[ctx_isp->substate_activated];
  5933. if (irq_ops->irq_ops[evt_id]) {
  5934. rc = irq_ops->irq_ops[evt_id](ctx_isp, evt_data);
  5935. } else {
  5936. CAM_DBG(CAM_ISP, "No handle function for Substate[%s]",
  5937. __cam_isp_ctx_substate_val_to_type(
  5938. ctx_isp->substate_activated));
  5939. if (isp_ctx_debug.enable_state_monitor_dump)
  5940. __cam_isp_ctx_dump_state_monitor_array(ctx_isp);
  5941. }
  5942. CAM_DBG(CAM_ISP, "Exit: State %d Substate[%s]",
  5943. ctx->state, __cam_isp_ctx_substate_val_to_type(
  5944. ctx_isp->substate_activated));
  5945. spin_unlock(&ctx->lock);
  5946. return rc;
  5947. }
  5948. /* top state machine */
  5949. static struct cam_ctx_ops
  5950. cam_isp_ctx_top_state_machine[CAM_CTX_STATE_MAX] = {
  5951. /* Uninit */
  5952. {
  5953. .ioctl_ops = {},
  5954. .crm_ops = {},
  5955. .irq_ops = NULL,
  5956. },
  5957. /* Available */
  5958. {
  5959. .ioctl_ops = {
  5960. .acquire_dev = __cam_isp_ctx_acquire_dev_in_available,
  5961. },
  5962. .crm_ops = {},
  5963. .irq_ops = NULL,
  5964. },
  5965. /* Acquired */
  5966. {
  5967. .ioctl_ops = {
  5968. .acquire_hw = __cam_isp_ctx_acquire_hw_in_acquired,
  5969. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5970. .config_dev = __cam_isp_ctx_config_dev_in_acquired,
  5971. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5972. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5973. },
  5974. .crm_ops = {
  5975. .link = __cam_isp_ctx_link_in_acquired,
  5976. .unlink = __cam_isp_ctx_unlink_in_acquired,
  5977. .get_dev_info = __cam_isp_ctx_get_dev_info_in_acquired,
  5978. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  5979. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5980. },
  5981. .irq_ops = NULL,
  5982. .pagefault_ops = cam_isp_context_dump_requests,
  5983. .dumpinfo_ops = cam_isp_context_info_dump,
  5984. },
  5985. /* Ready */
  5986. {
  5987. .ioctl_ops = {
  5988. .start_dev = __cam_isp_ctx_start_dev_in_ready,
  5989. .release_dev = __cam_isp_ctx_release_dev_in_top_state,
  5990. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  5991. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  5992. .release_hw = __cam_isp_ctx_release_hw_in_top_state,
  5993. },
  5994. .crm_ops = {
  5995. .unlink = __cam_isp_ctx_unlink_in_ready,
  5996. .flush_req = __cam_isp_ctx_flush_req_in_ready,
  5997. .dump_req = __cam_isp_ctx_dump_in_top_state,
  5998. },
  5999. .irq_ops = NULL,
  6000. .pagefault_ops = cam_isp_context_dump_requests,
  6001. .dumpinfo_ops = cam_isp_context_info_dump,
  6002. },
  6003. /* Flushed */
  6004. {
  6005. .ioctl_ops = {
  6006. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6007. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6008. .config_dev = __cam_isp_ctx_config_dev_in_flushed,
  6009. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6010. },
  6011. .crm_ops = {
  6012. .unlink = __cam_isp_ctx_unlink_in_ready,
  6013. .process_evt = __cam_isp_ctx_process_evt,
  6014. },
  6015. .irq_ops = NULL,
  6016. .pagefault_ops = cam_isp_context_dump_requests,
  6017. .dumpinfo_ops = cam_isp_context_info_dump,
  6018. },
  6019. /* Activated */
  6020. {
  6021. .ioctl_ops = {
  6022. .stop_dev = __cam_isp_ctx_stop_dev_in_activated,
  6023. .release_dev = __cam_isp_ctx_release_dev_in_activated,
  6024. .config_dev = __cam_isp_ctx_config_dev_in_top_state,
  6025. .flush_dev = __cam_isp_ctx_flush_dev_in_top_state,
  6026. .release_hw = __cam_isp_ctx_release_hw_in_activated,
  6027. },
  6028. .crm_ops = {
  6029. .unlink = __cam_isp_ctx_unlink_in_activated,
  6030. .apply_req = __cam_isp_ctx_apply_req,
  6031. .notify_frame_skip =
  6032. __cam_isp_ctx_apply_default_settings,
  6033. .flush_req = __cam_isp_ctx_flush_req_in_top_state,
  6034. .process_evt = __cam_isp_ctx_process_evt,
  6035. .dump_req = __cam_isp_ctx_dump_in_top_state,
  6036. },
  6037. .irq_ops = __cam_isp_ctx_handle_irq_in_activated,
  6038. .pagefault_ops = cam_isp_context_dump_requests,
  6039. .dumpinfo_ops = cam_isp_context_info_dump,
  6040. .recovery_ops = cam_isp_context_hw_recovery,
  6041. },
  6042. };
  6043. static int cam_isp_context_hw_recovery(void *priv, void *data)
  6044. {
  6045. struct cam_context *ctx = priv;
  6046. int rc = -EPERM;
  6047. if (ctx->hw_mgr_intf->hw_recovery)
  6048. rc = ctx->hw_mgr_intf->hw_recovery(ctx->hw_mgr_intf->hw_mgr_priv, data);
  6049. else
  6050. CAM_ERR(CAM_ISP, "hw mgr doesn't support recovery");
  6051. return rc;
  6052. }
  6053. static int cam_isp_context_dump_requests(void *data,
  6054. struct cam_smmu_pf_info *pf_info)
  6055. {
  6056. struct cam_context *ctx = (struct cam_context *)data;
  6057. struct cam_ctx_request *req = NULL;
  6058. struct cam_ctx_request *req_temp = NULL;
  6059. struct cam_isp_ctx_req *req_isp = NULL;
  6060. struct cam_isp_prepare_hw_update_data *hw_update_data = NULL;
  6061. struct cam_hw_mgr_dump_pf_data *pf_dbg_entry = NULL;
  6062. struct cam_req_mgr_message req_msg = {0};
  6063. struct cam_isp_context *ctx_isp;
  6064. uint32_t resource_type = 0;
  6065. bool mem_found = false, ctx_found = false, send_error = false;
  6066. int rc = 0;
  6067. struct cam_isp_context *isp_ctx =
  6068. (struct cam_isp_context *)ctx->ctx_priv;
  6069. if (!isp_ctx) {
  6070. CAM_ERR(CAM_ISP, "Invalid isp ctx");
  6071. return -EINVAL;
  6072. }
  6073. CAM_INFO(CAM_ISP, "iommu fault handler for isp ctx %d state %d",
  6074. ctx->ctx_id, ctx->state);
  6075. list_for_each_entry_safe(req, req_temp,
  6076. &ctx->active_req_list, list) {
  6077. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6078. hw_update_data = &req_isp->hw_update_data;
  6079. pf_dbg_entry = &(req->pf_data);
  6080. CAM_INFO(CAM_ISP, "Active List: req_id : %lld ",
  6081. req->request_id);
  6082. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6083. &mem_found, &ctx_found, &resource_type, pf_info);
  6084. if (rc)
  6085. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6086. if (ctx_found)
  6087. send_error = true;
  6088. }
  6089. CAM_INFO(CAM_ISP, "Iterating over wait_list of isp ctx %d state %d",
  6090. ctx->ctx_id, ctx->state);
  6091. list_for_each_entry_safe(req, req_temp,
  6092. &ctx->wait_req_list, list) {
  6093. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6094. hw_update_data = &req_isp->hw_update_data;
  6095. pf_dbg_entry = &(req->pf_data);
  6096. CAM_INFO(CAM_ISP, "Wait List: req_id : %lld ", req->request_id);
  6097. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6098. &mem_found, &ctx_found, &resource_type, pf_info);
  6099. if (rc)
  6100. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6101. if (ctx_found)
  6102. send_error = true;
  6103. }
  6104. /*
  6105. * In certain scenarios we observe both overflow and SMMU pagefault
  6106. * for a particular request. If overflow is handled before page fault
  6107. * we need to traverse through pending request list because if
  6108. * bubble recovery is enabled on any request we move that request
  6109. * and all the subsequent requests to the pending list while handling
  6110. * overflow error.
  6111. */
  6112. CAM_INFO(CAM_ISP,
  6113. "Iterating over pending req list of isp ctx %d state %d",
  6114. ctx->ctx_id, ctx->state);
  6115. list_for_each_entry_safe(req, req_temp,
  6116. &ctx->pending_req_list, list) {
  6117. req_isp = (struct cam_isp_ctx_req *) req->req_priv;
  6118. hw_update_data = &req_isp->hw_update_data;
  6119. pf_dbg_entry = &(req->pf_data);
  6120. CAM_INFO(CAM_ISP, "Pending List: req_id : %lld ",
  6121. req->request_id);
  6122. rc = cam_context_dump_pf_info_to_hw(ctx, pf_dbg_entry,
  6123. &mem_found, &ctx_found, &resource_type, pf_info);
  6124. if (rc)
  6125. CAM_ERR(CAM_ISP, "Failed to dump pf info");
  6126. if (ctx_found)
  6127. send_error = true;
  6128. }
  6129. if (resource_type) {
  6130. ctx_isp = (struct cam_isp_context *) ctx->ctx_priv;
  6131. CAM_ERR(CAM_ISP,
  6132. "Page fault on resource:%s (0x%x) ctx id:%d frame id:%d reported id:%lld applied id:%lld",
  6133. __cam_isp_resource_handle_id_to_type(
  6134. ctx_isp->isp_device_type, resource_type),
  6135. resource_type, ctx->ctx_id, ctx_isp->frame_id,
  6136. ctx_isp->reported_req_id, ctx_isp->last_applied_req_id);
  6137. }
  6138. if (send_error) {
  6139. CAM_INFO(CAM_ISP,
  6140. "page fault notifying to umd ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6141. ctx->ctx_id, ctx->session_hdl,
  6142. ctx->dev_hdl, ctx->link_hdl);
  6143. req_msg.session_hdl = ctx->session_hdl;
  6144. req_msg.u.err_msg.device_hdl = ctx->dev_hdl;
  6145. req_msg.u.err_msg.error_type =
  6146. CAM_REQ_MGR_ERROR_TYPE_PAGE_FAULT;
  6147. req_msg.u.err_msg.link_hdl = ctx->link_hdl;
  6148. req_msg.u.err_msg.request_id = 0;
  6149. req_msg.u.err_msg.resource_size = 0x0;
  6150. req_msg.u.err_msg.error_code = CAM_REQ_MGR_ISP_UNREPORTED_ERROR;
  6151. if (cam_req_mgr_notify_message(&req_msg,
  6152. V4L_EVENT_CAM_REQ_MGR_ERROR,
  6153. V4L_EVENT_CAM_REQ_MGR_EVENT))
  6154. CAM_ERR(CAM_ISP,
  6155. "could not send page fault notification ctx %u session_hdl:%d device_hdl:%d link_hdl:%d",
  6156. ctx->ctx_id, ctx->session_hdl,
  6157. ctx->dev_hdl, ctx->link_hdl);
  6158. }
  6159. return rc;
  6160. }
  6161. static int cam_isp_context_debug_register(void)
  6162. {
  6163. int rc = 0;
  6164. struct dentry *dbgfileptr = NULL;
  6165. dbgfileptr = debugfs_create_dir("camera_isp_ctx", NULL);
  6166. if (!dbgfileptr) {
  6167. CAM_ERR(CAM_ISP, "DebugFS could not create directory!");
  6168. rc = -ENOENT;
  6169. goto end;
  6170. }
  6171. /* Store parent inode for cleanup in caller */
  6172. isp_ctx_debug.dentry = dbgfileptr;
  6173. debugfs_create_u32("enable_state_monitor_dump", 0644,
  6174. isp_ctx_debug.dentry, &isp_ctx_debug.enable_state_monitor_dump);
  6175. debugfs_create_u8("enable_cdm_cmd_buffer_dump", 0644,
  6176. isp_ctx_debug.dentry, &isp_ctx_debug.enable_cdm_cmd_buff_dump);
  6177. debugfs_create_bool("disable_internal_recovery", 0644,
  6178. isp_ctx_debug.dentry, &isp_ctx_debug.disable_internal_recovery);
  6179. if (IS_ERR(dbgfileptr)) {
  6180. if (PTR_ERR(dbgfileptr) == -ENODEV)
  6181. CAM_WARN(CAM_ISP, "DebugFS not enabled in kernel!");
  6182. else
  6183. rc = PTR_ERR(dbgfileptr);
  6184. }
  6185. end:
  6186. return rc;
  6187. }
  6188. int cam_isp_context_init(struct cam_isp_context *ctx,
  6189. struct cam_context *ctx_base,
  6190. struct cam_req_mgr_kmd_ops *crm_node_intf,
  6191. struct cam_hw_mgr_intf *hw_intf,
  6192. uint32_t ctx_id,
  6193. uint32_t isp_device_type,
  6194. int img_iommu_hdl)
  6195. {
  6196. int rc = -1;
  6197. int i;
  6198. if (!ctx || !ctx_base) {
  6199. CAM_ERR(CAM_ISP, "Invalid Context");
  6200. goto err;
  6201. }
  6202. /* ISP context setup */
  6203. memset(ctx, 0, sizeof(*ctx));
  6204. ctx->base = ctx_base;
  6205. ctx->frame_id = 0;
  6206. ctx->custom_enabled = false;
  6207. ctx->use_frame_header_ts = false;
  6208. ctx->use_default_apply = false;
  6209. ctx->active_req_cnt = 0;
  6210. ctx->reported_req_id = 0;
  6211. ctx->bubble_frame_cnt = 0;
  6212. ctx->req_info.last_bufdone_req_id = 0;
  6213. ctx->v4l2_event_sub_ids = 0;
  6214. ctx->hw_ctx = NULL;
  6215. ctx->substate_activated = CAM_ISP_CTX_ACTIVATED_SOF;
  6216. ctx->substate_machine = cam_isp_ctx_activated_state_machine;
  6217. ctx->substate_machine_irq = cam_isp_ctx_activated_state_machine_irq;
  6218. ctx->init_timestamp = jiffies_to_msecs(jiffies);
  6219. ctx->isp_device_type = isp_device_type;
  6220. for (i = 0; i < CAM_ISP_CTX_REQ_MAX; i++) {
  6221. ctx->req_base[i].req_priv = &ctx->req_isp[i];
  6222. ctx->req_isp[i].base = &ctx->req_base[i];
  6223. }
  6224. /* camera context setup */
  6225. rc = cam_context_init(ctx_base, isp_dev_name, CAM_ISP, ctx_id,
  6226. crm_node_intf, hw_intf, ctx->req_base, CAM_ISP_CTX_REQ_MAX, img_iommu_hdl);
  6227. if (rc) {
  6228. CAM_ERR(CAM_ISP, "Camera Context Base init failed");
  6229. goto err;
  6230. }
  6231. /* link camera context with isp context */
  6232. ctx_base->state_machine = cam_isp_ctx_top_state_machine;
  6233. ctx_base->ctx_priv = ctx;
  6234. /* initializing current state for error logging */
  6235. for (i = 0; i < CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES; i++) {
  6236. ctx->cam_isp_ctx_state_monitor[i].curr_state =
  6237. CAM_ISP_CTX_ACTIVATED_MAX;
  6238. }
  6239. atomic64_set(&ctx->state_monitor_head, -1);
  6240. for (i = 0; i < CAM_ISP_CTX_EVENT_MAX; i++)
  6241. atomic64_set(&ctx->event_record_head[i], -1);
  6242. if (!isp_ctx_debug.dentry)
  6243. cam_isp_context_debug_register();
  6244. err:
  6245. return rc;
  6246. }
  6247. int cam_isp_context_deinit(struct cam_isp_context *ctx)
  6248. {
  6249. if (ctx->base)
  6250. cam_context_deinit(ctx->base);
  6251. if (ctx->substate_activated != CAM_ISP_CTX_ACTIVATED_SOF)
  6252. CAM_ERR(CAM_ISP, "ISP context Substate[%s] is invalid",
  6253. __cam_isp_ctx_substate_val_to_type(
  6254. ctx->substate_activated));
  6255. debugfs_remove_recursive(isp_ctx_debug.dentry);
  6256. isp_ctx_debug.dentry = NULL;
  6257. memset(ctx, 0, sizeof(*ctx));
  6258. return 0;
  6259. }