cam_isp_context.c 179 KB

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