cam_isp_context.c 123 KB

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