cam_isp_context.c 148 KB

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