cam_isp_context.c 201 KB

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