cam_isp_context.c 113 KB

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