cam_isp_context.c 165 KB

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