cam_isp_context.c 119 KB

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