cam_isp_context.c 211 KB

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