cam_isp_context.c 126 KB

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