cam_isp_context.c 228 KB

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