cam_isp_context.c 190 KB

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