cam_isp_context.c 160 KB

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