cam_isp_context.c 193 KB

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