cam_isp_context.c 214 KB

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