cam_isp_context.c 121 KB

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