cam_isp_context.c 118 KB

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