cam_isp_context.c 139 KB

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