cam_isp_context.c 193 KB

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