cam_isp_context.c 149 KB

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