cam_isp_context.c 193 KB

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