cam_isp_context.c 214 KB

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