cam_isp_context.c 161 KB

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