cam_isp_context.c 190 KB

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