cam_isp_context.c 190 KB

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