cam_isp_context.c 202 KB

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