cam_isp_context.c 117 KB

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