cam_isp_context.c 214 KB

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