cam_isp_context.c 194 KB

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