cam_isp_context.c 149 KB

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