cam_isp_context.c 124 KB

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