cam_isp_context.c 112 KB

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