gsi.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/log2.h>
  9. #include <linux/module.h>
  10. #include <linux/msm_gsi.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/delay.h>
  13. #include "gsi.h"
  14. #include "gsi_reg.h"
  15. #include "gsi_emulation.h"
  16. #define GSI_CMD_TIMEOUT (5*HZ)
  17. #define GSI_START_CMD_TIMEOUT_MS 1000
  18. #define GSI_CMD_POLL_CNT 5
  19. #define GSI_STOP_CMD_TIMEOUT_MS 200
  20. #define GSI_MAX_CH_LOW_WEIGHT 15
  21. #define GSI_STOP_CMD_POLL_CNT 4
  22. #define GSI_STOP_IN_PROC_CMD_POLL_CNT 2
  23. #define GSI_RESET_WA_MIN_SLEEP 1000
  24. #define GSI_RESET_WA_MAX_SLEEP 2000
  25. #define GSI_CHNL_STATE_MAX_RETRYCNT 10
  26. #define GSI_STTS_REG_BITS 32
  27. #ifndef CONFIG_DEBUG_FS
  28. void gsi_debugfs_init(void)
  29. {
  30. }
  31. #endif
  32. static const struct of_device_id msm_gsi_match[] = {
  33. { .compatible = "qcom,msm_gsi", },
  34. { },
  35. };
  36. #if defined(CONFIG_IPA_EMULATION)
  37. static bool running_emulation = true;
  38. #else
  39. static bool running_emulation;
  40. #endif
  41. struct gsi_ctx *gsi_ctx;
  42. static void __gsi_config_type_irq(int ee, uint32_t mask, uint32_t val)
  43. {
  44. uint32_t curr;
  45. curr = gsi_readl(gsi_ctx->base +
  46. GSI_EE_n_CNTXT_TYPE_IRQ_MSK_OFFS(ee));
  47. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  48. GSI_EE_n_CNTXT_TYPE_IRQ_MSK_OFFS(ee));
  49. }
  50. static void __gsi_config_ch_irq(int ee, uint32_t mask, uint32_t val)
  51. {
  52. uint32_t curr;
  53. curr = gsi_readl(gsi_ctx->base +
  54. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_OFFS(ee));
  55. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  56. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_MSK_OFFS(ee));
  57. }
  58. static void __gsi_config_evt_irq(int ee, uint32_t mask, uint32_t val)
  59. {
  60. uint32_t curr;
  61. curr = gsi_readl(gsi_ctx->base +
  62. GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_OFFS(ee));
  63. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  64. GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_MSK_OFFS(ee));
  65. }
  66. static void __gsi_config_ieob_irq(int ee, uint32_t mask, uint32_t val)
  67. {
  68. uint32_t curr;
  69. curr = gsi_readl(gsi_ctx->base +
  70. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
  71. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  72. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
  73. GSIDBG("current IEO_IRQ_MSK: 0x%x, change to: 0x%x\n",
  74. curr, ((curr & ~mask) | (val & mask)));
  75. }
  76. static void __gsi_config_glob_irq(int ee, uint32_t mask, uint32_t val)
  77. {
  78. uint32_t curr;
  79. curr = gsi_readl(gsi_ctx->base +
  80. GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS(ee));
  81. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  82. GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS(ee));
  83. }
  84. static void __gsi_config_gen_irq(int ee, uint32_t mask, uint32_t val)
  85. {
  86. uint32_t curr;
  87. curr = gsi_readl(gsi_ctx->base +
  88. GSI_EE_n_CNTXT_GSI_IRQ_EN_OFFS(ee));
  89. gsi_writel((curr & ~mask) | (val & mask), gsi_ctx->base +
  90. GSI_EE_n_CNTXT_GSI_IRQ_EN_OFFS(ee));
  91. }
  92. static void gsi_channel_state_change_wait(unsigned long chan_hdl,
  93. struct gsi_chan_ctx *ctx,
  94. uint32_t tm, enum gsi_ch_cmd_opcode op)
  95. {
  96. int poll_cnt;
  97. int gsi_pending_intr;
  98. int res;
  99. uint32_t type;
  100. uint32_t val;
  101. int ee = gsi_ctx->per.ee;
  102. enum gsi_chan_state curr_state = GSI_CHAN_STATE_NOT_ALLOCATED;
  103. int stop_in_proc_retry = 0;
  104. int stop_retry = 0;
  105. /*
  106. * Start polling the GSI channel for
  107. * duration = tm * GSI_CMD_POLL_CNT.
  108. * We need to do polling of gsi state for improving debugability
  109. * of gsi hw state.
  110. */
  111. for (poll_cnt = 0;
  112. poll_cnt < GSI_CMD_POLL_CNT;
  113. poll_cnt++) {
  114. res = wait_for_completion_timeout(&ctx->compl,
  115. msecs_to_jiffies(tm));
  116. /* Interrupt received, return */
  117. if (res != 0)
  118. return;
  119. type = gsi_readl(gsi_ctx->base +
  120. GSI_EE_n_CNTXT_TYPE_IRQ_OFFS(gsi_ctx->per.ee));
  121. gsi_pending_intr = gsi_readl(gsi_ctx->base +
  122. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_OFFS(ee));
  123. /* Update the channel state only if interrupt was raised
  124. * on praticular channel and also checking global interrupt
  125. * is raised for channel control.
  126. */
  127. if ((type & GSI_EE_n_CNTXT_TYPE_IRQ_CH_CTRL_BMSK) &&
  128. ((gsi_pending_intr >> chan_hdl) & 1)) {
  129. /*
  130. * Check channel state here in case the channel is
  131. * already started but interrupt is not yet received.
  132. */
  133. val = gsi_readl(gsi_ctx->base +
  134. GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(chan_hdl,
  135. gsi_ctx->per.ee));
  136. curr_state = (val &
  137. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
  138. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
  139. }
  140. if (op == GSI_CH_START) {
  141. if (curr_state == GSI_CHAN_STATE_STARTED) {
  142. ctx->state = curr_state;
  143. return;
  144. }
  145. }
  146. if (op == GSI_CH_STOP) {
  147. if (curr_state == GSI_CHAN_STATE_STOPPED)
  148. stop_retry++;
  149. else if (curr_state == GSI_CHAN_STATE_STOP_IN_PROC)
  150. stop_in_proc_retry++;
  151. }
  152. /* if interrupt marked reg after poll count reaching to max
  153. * keep loop to continue reach max stop proc and max stop count.
  154. */
  155. if (stop_retry == 1 || stop_in_proc_retry == 1)
  156. poll_cnt = 0;
  157. /* If stop channel retry reached to max count
  158. * clear the pending interrupt, if channel already stopped.
  159. */
  160. if (stop_retry == GSI_STOP_CMD_POLL_CNT) {
  161. gsi_writel(gsi_pending_intr, gsi_ctx->base +
  162. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR_OFFS(ee));
  163. ctx->state = curr_state;
  164. return;
  165. }
  166. /* If channel state stop in progress case no need
  167. * to wait for long time.
  168. */
  169. if (stop_in_proc_retry == GSI_STOP_IN_PROC_CMD_POLL_CNT) {
  170. ctx->state = curr_state;
  171. return;
  172. }
  173. GSIDBG("GSI wait on chan_hld=%lu irqtyp=%u state=%u intr=%u\n",
  174. chan_hdl,
  175. type,
  176. ctx->state,
  177. gsi_pending_intr);
  178. }
  179. GSIDBG("invalidating the channel state when timeout happens\n");
  180. ctx->state = curr_state;
  181. }
  182. static void gsi_handle_ch_ctrl(int ee)
  183. {
  184. uint32_t ch;
  185. int i;
  186. uint32_t val;
  187. struct gsi_chan_ctx *ctx;
  188. ch = gsi_readl(gsi_ctx->base +
  189. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_OFFS(ee));
  190. gsi_writel(ch, gsi_ctx->base +
  191. GSI_EE_n_CNTXT_SRC_GSI_CH_IRQ_CLR_OFFS(ee));
  192. GSIDBG("ch %x\n", ch);
  193. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  194. if ((1 << i) & ch) {
  195. if (i >= gsi_ctx->max_ch || i >= GSI_CHAN_MAX) {
  196. GSIERR("invalid channel %d\n", i);
  197. break;
  198. }
  199. ctx = &gsi_ctx->chan[i];
  200. val = gsi_readl(gsi_ctx->base +
  201. GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(i, ee));
  202. ctx->state = (val &
  203. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
  204. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
  205. GSIDBG("ch %u state updated to %u\n", i, ctx->state);
  206. complete(&ctx->compl);
  207. gsi_ctx->ch_dbg[i].cmd_completed++;
  208. }
  209. }
  210. }
  211. static void gsi_handle_ev_ctrl(int ee)
  212. {
  213. uint32_t ch;
  214. int i;
  215. uint32_t val;
  216. struct gsi_evt_ctx *ctx;
  217. ch = gsi_readl(gsi_ctx->base +
  218. GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_OFFS(ee));
  219. gsi_writel(ch, gsi_ctx->base +
  220. GSI_EE_n_CNTXT_SRC_EV_CH_IRQ_CLR_OFFS(ee));
  221. GSIDBG("ev %x\n", ch);
  222. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  223. if ((1 << i) & ch) {
  224. if (i >= gsi_ctx->max_ev || i >= GSI_EVT_RING_MAX) {
  225. GSIERR("invalid event %d\n", i);
  226. break;
  227. }
  228. ctx = &gsi_ctx->evtr[i];
  229. val = gsi_readl(gsi_ctx->base +
  230. GSI_EE_n_EV_CH_k_CNTXT_0_OFFS(i, ee));
  231. ctx->state = (val &
  232. GSI_EE_n_EV_CH_k_CNTXT_0_CHSTATE_BMSK) >>
  233. GSI_EE_n_EV_CH_k_CNTXT_0_CHSTATE_SHFT;
  234. GSIDBG("evt %u state updated to %u\n", i, ctx->state);
  235. complete(&ctx->compl);
  236. }
  237. }
  238. }
  239. static void gsi_handle_glob_err(uint32_t err)
  240. {
  241. struct gsi_log_err *log;
  242. struct gsi_chan_ctx *ch;
  243. struct gsi_evt_ctx *ev;
  244. struct gsi_chan_err_notify chan_notify;
  245. struct gsi_evt_err_notify evt_notify;
  246. struct gsi_per_notify per_notify;
  247. uint32_t val;
  248. enum gsi_err_type err_type;
  249. log = (struct gsi_log_err *)&err;
  250. GSIERR("log err_type=%u ee=%u idx=%u\n", log->err_type, log->ee,
  251. log->virt_idx);
  252. GSIERR("code=%u arg1=%u arg2=%u arg3=%u\n", log->code, log->arg1,
  253. log->arg2, log->arg3);
  254. err_type = log->err_type;
  255. /*
  256. * These are errors thrown by hardware. We need
  257. * BUG_ON() to capture the hardware state right
  258. * when it is unexpected.
  259. */
  260. switch (err_type) {
  261. case GSI_ERR_TYPE_GLOB:
  262. per_notify.evt_id = GSI_PER_EVT_GLOB_ERROR;
  263. per_notify.user_data = gsi_ctx->per.user_data;
  264. per_notify.data.err_desc = err & 0xFFFF;
  265. gsi_ctx->per.notify_cb(&per_notify);
  266. break;
  267. case GSI_ERR_TYPE_CHAN:
  268. if (WARN_ON(log->virt_idx >= gsi_ctx->max_ch)) {
  269. GSIERR("Unexpected ch %d\n", log->virt_idx);
  270. return;
  271. }
  272. ch = &gsi_ctx->chan[log->virt_idx];
  273. chan_notify.chan_user_data = ch->props.chan_user_data;
  274. chan_notify.err_desc = err & 0xFFFF;
  275. if (log->code == GSI_INVALID_TRE_ERR) {
  276. if (log->ee != gsi_ctx->per.ee) {
  277. GSIERR("unexpected EE in event %d\n", log->ee);
  278. GSI_ASSERT();
  279. }
  280. val = gsi_readl(gsi_ctx->base +
  281. GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(log->virt_idx,
  282. gsi_ctx->per.ee));
  283. ch->state = (val &
  284. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_BMSK) >>
  285. GSI_EE_n_GSI_CH_k_CNTXT_0_CHSTATE_SHFT;
  286. GSIDBG("ch %u state updated to %u\n", log->virt_idx,
  287. ch->state);
  288. ch->stats.invalid_tre_error++;
  289. if (ch->state == GSI_CHAN_STATE_ERROR) {
  290. GSIERR("Unexpected channel state %d\n",
  291. ch->state);
  292. GSI_ASSERT();
  293. }
  294. chan_notify.evt_id = GSI_CHAN_INVALID_TRE_ERR;
  295. } else if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
  296. if (log->ee != gsi_ctx->per.ee) {
  297. GSIERR("unexpected EE in event %d\n", log->ee);
  298. GSI_ASSERT();
  299. }
  300. chan_notify.evt_id = GSI_CHAN_OUT_OF_BUFFERS_ERR;
  301. } else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
  302. if (log->ee != gsi_ctx->per.ee) {
  303. GSIERR("unexpected EE in event %d\n", log->ee);
  304. GSI_ASSERT();
  305. }
  306. chan_notify.evt_id = GSI_CHAN_OUT_OF_RESOURCES_ERR;
  307. complete(&ch->compl);
  308. } else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
  309. chan_notify.evt_id =
  310. GSI_CHAN_UNSUPPORTED_INTER_EE_OP_ERR;
  311. } else if (log->code == GSI_NON_ALLOCATED_EVT_ACCESS_ERR) {
  312. if (log->ee != gsi_ctx->per.ee) {
  313. GSIERR("unexpected EE in event %d\n", log->ee);
  314. GSI_ASSERT();
  315. }
  316. chan_notify.evt_id =
  317. GSI_CHAN_NON_ALLOCATED_EVT_ACCESS_ERR;
  318. } else if (log->code == GSI_HWO_1_ERR) {
  319. if (log->ee != gsi_ctx->per.ee) {
  320. GSIERR("unexpected EE in event %d\n", log->ee);
  321. GSI_ASSERT();
  322. }
  323. chan_notify.evt_id = GSI_CHAN_HWO_1_ERR;
  324. } else {
  325. GSIERR("unexpected event log code %d\n", log->code);
  326. GSI_ASSERT();
  327. }
  328. ch->props.err_cb(&chan_notify);
  329. break;
  330. case GSI_ERR_TYPE_EVT:
  331. if (WARN_ON(log->virt_idx >= gsi_ctx->max_ev)) {
  332. GSIERR("Unexpected ev %d\n", log->virt_idx);
  333. return;
  334. }
  335. ev = &gsi_ctx->evtr[log->virt_idx];
  336. evt_notify.user_data = ev->props.user_data;
  337. evt_notify.err_desc = err & 0xFFFF;
  338. if (log->code == GSI_OUT_OF_BUFFERS_ERR) {
  339. if (log->ee != gsi_ctx->per.ee) {
  340. GSIERR("unexpected EE in event %d\n", log->ee);
  341. GSI_ASSERT();
  342. }
  343. evt_notify.evt_id = GSI_EVT_OUT_OF_BUFFERS_ERR;
  344. } else if (log->code == GSI_OUT_OF_RESOURCES_ERR) {
  345. if (log->ee != gsi_ctx->per.ee) {
  346. GSIERR("unexpected EE in event %d\n", log->ee);
  347. GSI_ASSERT();
  348. }
  349. evt_notify.evt_id = GSI_EVT_OUT_OF_RESOURCES_ERR;
  350. complete(&ev->compl);
  351. } else if (log->code == GSI_UNSUPPORTED_INTER_EE_OP_ERR) {
  352. evt_notify.evt_id = GSI_EVT_UNSUPPORTED_INTER_EE_OP_ERR;
  353. } else if (log->code == GSI_EVT_RING_EMPTY_ERR) {
  354. if (log->ee != gsi_ctx->per.ee) {
  355. GSIERR("unexpected EE in event %d\n", log->ee);
  356. GSI_ASSERT();
  357. }
  358. evt_notify.evt_id = GSI_EVT_EVT_RING_EMPTY_ERR;
  359. } else {
  360. GSIERR("unexpected event log code %d\n", log->code);
  361. GSI_ASSERT();
  362. }
  363. ev->props.err_cb(&evt_notify);
  364. break;
  365. }
  366. }
  367. static void gsi_handle_gp_int1(void)
  368. {
  369. complete(&gsi_ctx->gen_ee_cmd_compl);
  370. }
  371. static void gsi_handle_glob_ee(int ee)
  372. {
  373. uint32_t val;
  374. uint32_t err;
  375. struct gsi_per_notify notify;
  376. uint32_t clr = ~0;
  377. val = gsi_readl(gsi_ctx->base +
  378. GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS(ee));
  379. notify.user_data = gsi_ctx->per.user_data;
  380. if (val & GSI_EE_n_CNTXT_GLOB_IRQ_STTS_ERROR_INT_BMSK) {
  381. err = gsi_readl(gsi_ctx->base +
  382. GSI_EE_n_ERROR_LOG_OFFS(ee));
  383. if (gsi_ctx->per.ver >= GSI_VER_1_2)
  384. gsi_writel(0, gsi_ctx->base +
  385. GSI_EE_n_ERROR_LOG_OFFS(ee));
  386. gsi_writel(clr, gsi_ctx->base +
  387. GSI_EE_n_ERROR_LOG_CLR_OFFS(ee));
  388. gsi_handle_glob_err(err);
  389. }
  390. if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT1_BMSK)
  391. gsi_handle_gp_int1();
  392. if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT2_BMSK) {
  393. notify.evt_id = GSI_PER_EVT_GLOB_GP2;
  394. gsi_ctx->per.notify_cb(&notify);
  395. }
  396. if (val & GSI_EE_n_CNTXT_GLOB_IRQ_EN_GP_INT3_BMSK) {
  397. notify.evt_id = GSI_PER_EVT_GLOB_GP3;
  398. gsi_ctx->per.notify_cb(&notify);
  399. }
  400. gsi_writel(val, gsi_ctx->base +
  401. GSI_EE_n_CNTXT_GLOB_IRQ_CLR_OFFS(ee));
  402. }
  403. static void gsi_incr_ring_wp(struct gsi_ring_ctx *ctx)
  404. {
  405. ctx->wp_local += ctx->elem_sz;
  406. if (ctx->wp_local == ctx->end)
  407. ctx->wp_local = ctx->base;
  408. }
  409. static void gsi_incr_ring_rp(struct gsi_ring_ctx *ctx)
  410. {
  411. ctx->rp_local += ctx->elem_sz;
  412. if (ctx->rp_local == ctx->end)
  413. ctx->rp_local = ctx->base;
  414. }
  415. uint16_t gsi_find_idx_from_addr(struct gsi_ring_ctx *ctx, uint64_t addr)
  416. {
  417. WARN_ON(addr < ctx->base || addr >= ctx->end);
  418. return (uint32_t)(addr - ctx->base) / ctx->elem_sz;
  419. }
  420. static uint16_t gsi_get_complete_num(struct gsi_ring_ctx *ctx, uint64_t addr1,
  421. uint64_t addr2)
  422. {
  423. uint32_t addr_diff;
  424. GSIDBG_LOW("gsi base addr 0x%llx end addr 0x%llx\n",
  425. ctx->base, ctx->end);
  426. if (addr1 < ctx->base || addr1 >= ctx->end) {
  427. GSIERR("address = 0x%llx not in range\n", addr1);
  428. GSI_ASSERT();
  429. }
  430. if (addr2 < ctx->base || addr2 >= ctx->end) {
  431. GSIERR("address = 0x%llx not in range\n", addr2);
  432. GSI_ASSERT();
  433. }
  434. addr_diff = (uint32_t)(addr2 - addr1);
  435. if (addr1 < addr2)
  436. return addr_diff / ctx->elem_sz;
  437. else
  438. return (addr_diff + ctx->len) / ctx->elem_sz;
  439. }
  440. static void gsi_process_chan(struct gsi_xfer_compl_evt *evt,
  441. struct gsi_chan_xfer_notify *notify, bool callback)
  442. {
  443. uint32_t ch_id;
  444. struct gsi_chan_ctx *ch_ctx;
  445. uint16_t rp_idx;
  446. uint64_t rp;
  447. ch_id = evt->chid;
  448. if (WARN_ON(ch_id >= gsi_ctx->max_ch)) {
  449. GSIERR("Unexpected ch %d\n", ch_id);
  450. return;
  451. }
  452. ch_ctx = &gsi_ctx->chan[ch_id];
  453. if (WARN_ON(ch_ctx->props.prot != GSI_CHAN_PROT_GPI &&
  454. ch_ctx->props.prot != GSI_CHAN_PROT_GCI))
  455. return;
  456. if (evt->type != GSI_XFER_COMPL_TYPE_GCI) {
  457. rp = evt->xfer_ptr;
  458. if (ch_ctx->ring.rp_local != rp) {
  459. ch_ctx->stats.completed +=
  460. gsi_get_complete_num(&ch_ctx->ring,
  461. ch_ctx->ring.rp_local, rp);
  462. ch_ctx->ring.rp_local = rp;
  463. }
  464. /* the element at RP is also processed */
  465. gsi_incr_ring_rp(&ch_ctx->ring);
  466. ch_ctx->ring.rp = ch_ctx->ring.rp_local;
  467. rp_idx = gsi_find_idx_from_addr(&ch_ctx->ring, rp);
  468. notify->veid = GSI_VEID_DEFAULT;
  469. } else {
  470. rp_idx = evt->cookie;
  471. notify->veid = evt->veid;
  472. }
  473. ch_ctx->stats.completed++;
  474. WARN_ON(!ch_ctx->user_data[rp_idx].valid);
  475. notify->xfer_user_data = ch_ctx->user_data[rp_idx].p;
  476. ch_ctx->user_data[rp_idx].valid = false;
  477. notify->chan_user_data = ch_ctx->props.chan_user_data;
  478. notify->evt_id = evt->code;
  479. notify->bytes_xfered = evt->len;
  480. if (callback) {
  481. if (atomic_read(&ch_ctx->poll_mode)) {
  482. GSIERR("Calling client callback in polling mode\n");
  483. WARN_ON(1);
  484. }
  485. ch_ctx->props.xfer_cb(notify);
  486. }
  487. }
  488. static void gsi_process_evt_re(struct gsi_evt_ctx *ctx,
  489. struct gsi_chan_xfer_notify *notify, bool callback)
  490. {
  491. struct gsi_xfer_compl_evt *evt;
  492. evt = (struct gsi_xfer_compl_evt *)(ctx->ring.base_va +
  493. ctx->ring.rp_local - ctx->ring.base);
  494. gsi_process_chan(evt, notify, callback);
  495. gsi_incr_ring_rp(&ctx->ring);
  496. /* recycle this element */
  497. gsi_incr_ring_wp(&ctx->ring);
  498. ctx->stats.completed++;
  499. }
  500. static void gsi_ring_evt_doorbell(struct gsi_evt_ctx *ctx)
  501. {
  502. uint32_t val;
  503. ctx->ring.wp = ctx->ring.wp_local;
  504. val = (ctx->ring.wp_local &
  505. GSI_EE_n_EV_CH_k_DOORBELL_0_WRITE_PTR_LSB_BMSK) <<
  506. GSI_EE_n_EV_CH_k_DOORBELL_0_WRITE_PTR_LSB_SHFT;
  507. gsi_writel(val, gsi_ctx->base +
  508. GSI_EE_n_EV_CH_k_DOORBELL_0_OFFS(ctx->id,
  509. gsi_ctx->per.ee));
  510. }
  511. static void gsi_ring_chan_doorbell(struct gsi_chan_ctx *ctx)
  512. {
  513. uint32_t val;
  514. /*
  515. * allocate new events for this channel first
  516. * before submitting the new TREs.
  517. * for TO_GSI channels the event ring doorbell is rang as part of
  518. * interrupt handling.
  519. */
  520. if (ctx->evtr && ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
  521. gsi_ring_evt_doorbell(ctx->evtr);
  522. ctx->ring.wp = ctx->ring.wp_local;
  523. val = (ctx->ring.wp_local &
  524. GSI_EE_n_GSI_CH_k_DOORBELL_0_WRITE_PTR_LSB_BMSK) <<
  525. GSI_EE_n_GSI_CH_k_DOORBELL_0_WRITE_PTR_LSB_SHFT;
  526. gsi_writel(val, gsi_ctx->base +
  527. GSI_EE_n_GSI_CH_k_DOORBELL_0_OFFS(ctx->props.ch_id,
  528. gsi_ctx->per.ee));
  529. }
  530. static void gsi_handle_ieob(int ee)
  531. {
  532. uint32_t ch;
  533. int i;
  534. uint64_t rp;
  535. struct gsi_evt_ctx *ctx;
  536. struct gsi_chan_xfer_notify notify;
  537. unsigned long flags;
  538. unsigned long cntr;
  539. uint32_t msk;
  540. bool empty;
  541. ch = gsi_readl(gsi_ctx->base +
  542. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_OFFS(ee));
  543. msk = gsi_readl(gsi_ctx->base +
  544. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_MSK_OFFS(ee));
  545. gsi_writel(ch & msk, gsi_ctx->base +
  546. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(ee));
  547. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  548. if ((1 << i) & ch & msk) {
  549. if (i >= gsi_ctx->max_ev || i >= GSI_EVT_RING_MAX) {
  550. GSIERR("invalid event %d\n", i);
  551. break;
  552. }
  553. ctx = &gsi_ctx->evtr[i];
  554. /*
  555. * Don't handle MSI interrupts, only handle IEOB
  556. * IRQs
  557. */
  558. if (ctx->props.intr == GSI_INTR_MSI)
  559. continue;
  560. if (ctx->props.intf != GSI_EVT_CHTYPE_GPI_EV) {
  561. GSIERR("Unexpected irq intf %d\n",
  562. ctx->props.intf);
  563. GSI_ASSERT();
  564. }
  565. spin_lock_irqsave(&ctx->ring.slock, flags);
  566. check_again:
  567. cntr = 0;
  568. empty = true;
  569. rp = gsi_readl(gsi_ctx->base +
  570. GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(i, ee));
  571. rp |= ctx->ring.rp & 0xFFFFFFFF00000000;
  572. ctx->ring.rp = rp;
  573. while (ctx->ring.rp_local != rp) {
  574. ++cntr;
  575. if (ctx->props.exclusive &&
  576. atomic_read(&ctx->chan->poll_mode)) {
  577. cntr = 0;
  578. break;
  579. }
  580. gsi_process_evt_re(ctx, &notify, true);
  581. empty = false;
  582. }
  583. if (!empty)
  584. gsi_ring_evt_doorbell(ctx);
  585. if (cntr != 0)
  586. goto check_again;
  587. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  588. }
  589. }
  590. }
  591. static void gsi_handle_inter_ee_ch_ctrl(int ee)
  592. {
  593. uint32_t ch;
  594. int i;
  595. ch = gsi_readl(gsi_ctx->base +
  596. GSI_INTER_EE_n_SRC_GSI_CH_IRQ_OFFS(ee));
  597. gsi_writel(ch, gsi_ctx->base +
  598. GSI_INTER_EE_n_SRC_GSI_CH_IRQ_CLR_OFFS(ee));
  599. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  600. if ((1 << i) & ch) {
  601. /* not currently expected */
  602. GSIERR("ch %u was inter-EE changed\n", i);
  603. }
  604. }
  605. }
  606. static void gsi_handle_inter_ee_ev_ctrl(int ee)
  607. {
  608. uint32_t ch;
  609. int i;
  610. ch = gsi_readl(gsi_ctx->base +
  611. GSI_INTER_EE_n_SRC_EV_CH_IRQ_OFFS(ee));
  612. gsi_writel(ch, gsi_ctx->base +
  613. GSI_INTER_EE_n_SRC_EV_CH_IRQ_CLR_OFFS(ee));
  614. for (i = 0; i < GSI_STTS_REG_BITS; i++) {
  615. if ((1 << i) & ch) {
  616. /* not currently expected */
  617. GSIERR("evt %u was inter-EE changed\n", i);
  618. }
  619. }
  620. }
  621. static void gsi_handle_general(int ee)
  622. {
  623. uint32_t val;
  624. struct gsi_per_notify notify;
  625. val = gsi_readl(gsi_ctx->base +
  626. GSI_EE_n_CNTXT_GSI_IRQ_STTS_OFFS(ee));
  627. notify.user_data = gsi_ctx->per.user_data;
  628. if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_MCS_STACK_OVRFLOW_BMSK)
  629. notify.evt_id = GSI_PER_EVT_GENERAL_MCS_STACK_OVERFLOW;
  630. if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_CMD_FIFO_OVRFLOW_BMSK)
  631. notify.evt_id = GSI_PER_EVT_GENERAL_CMD_FIFO_OVERFLOW;
  632. if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BUS_ERROR_BMSK)
  633. notify.evt_id = GSI_PER_EVT_GENERAL_BUS_ERROR;
  634. if (val & GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BREAK_POINT_BMSK)
  635. notify.evt_id = GSI_PER_EVT_GENERAL_BREAK_POINT;
  636. if (gsi_ctx->per.notify_cb)
  637. gsi_ctx->per.notify_cb(&notify);
  638. gsi_writel(val, gsi_ctx->base +
  639. GSI_EE_n_CNTXT_GSI_IRQ_CLR_OFFS(ee));
  640. }
  641. #define GSI_ISR_MAX_ITER 50
  642. static void gsi_handle_irq(void)
  643. {
  644. uint32_t type;
  645. int ee = gsi_ctx->per.ee;
  646. unsigned long cnt = 0;
  647. while (1) {
  648. type = gsi_readl(gsi_ctx->base +
  649. GSI_EE_n_CNTXT_TYPE_IRQ_OFFS(ee));
  650. if (!type)
  651. break;
  652. GSIDBG_LOW("type 0x%x\n", type);
  653. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_CH_CTRL_BMSK)
  654. gsi_handle_ch_ctrl(ee);
  655. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_EV_CTRL_BMSK)
  656. gsi_handle_ev_ctrl(ee);
  657. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_GLOB_EE_BMSK)
  658. gsi_handle_glob_ee(ee);
  659. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_IEOB_BMSK)
  660. gsi_handle_ieob(ee);
  661. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_INTER_EE_CH_CTRL_BMSK)
  662. gsi_handle_inter_ee_ch_ctrl(ee);
  663. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_INTER_EE_EV_CTRL_BMSK)
  664. gsi_handle_inter_ee_ev_ctrl(ee);
  665. if (type & GSI_EE_n_CNTXT_TYPE_IRQ_GENERAL_BMSK)
  666. gsi_handle_general(ee);
  667. if (++cnt > GSI_ISR_MAX_ITER) {
  668. /*
  669. * Max number of spurious interrupts from hardware.
  670. * Unexpected hardware state.
  671. */
  672. GSIERR("Too many spurious interrupt from GSI HW\n");
  673. GSI_ASSERT();
  674. }
  675. }
  676. }
  677. static irqreturn_t gsi_isr(int irq, void *ctxt)
  678. {
  679. if (gsi_ctx->per.req_clk_cb) {
  680. bool granted = false;
  681. gsi_ctx->per.req_clk_cb(gsi_ctx->per.user_data, &granted);
  682. if (granted) {
  683. gsi_handle_irq();
  684. gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
  685. }
  686. } else if (!gsi_ctx->per.clk_status_cb()) {
  687. return IRQ_HANDLED;
  688. } else {
  689. gsi_handle_irq();
  690. }
  691. return IRQ_HANDLED;
  692. }
  693. static uint32_t gsi_get_max_channels(enum gsi_ver ver)
  694. {
  695. uint32_t reg = 0;
  696. switch (ver) {
  697. case GSI_VER_ERR:
  698. case GSI_VER_MAX:
  699. GSIERR("GSI version is not supported %d\n", ver);
  700. WARN_ON(1);
  701. break;
  702. case GSI_VER_1_0:
  703. reg = gsi_readl(gsi_ctx->base +
  704. GSI_V1_0_EE_n_GSI_HW_PARAM_OFFS(gsi_ctx->per.ee));
  705. reg = (reg & GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_CH_NUM_BMSK) >>
  706. GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_CH_NUM_SHFT;
  707. break;
  708. case GSI_VER_1_2:
  709. reg = gsi_readl(gsi_ctx->base +
  710. GSI_V1_2_EE_n_GSI_HW_PARAM_0_OFFS(gsi_ctx->per.ee));
  711. reg = (reg & GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_CH_NUM_BMSK) >>
  712. GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_CH_NUM_SHFT;
  713. break;
  714. case GSI_VER_1_3:
  715. reg = gsi_readl(gsi_ctx->base +
  716. GSI_V1_3_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  717. reg = (reg &
  718. GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
  719. GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
  720. break;
  721. case GSI_VER_2_0:
  722. reg = gsi_readl(gsi_ctx->base +
  723. GSI_V2_0_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  724. reg = (reg &
  725. GSI_V2_0_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
  726. GSI_V2_0_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
  727. break;
  728. case GSI_VER_2_2:
  729. reg = gsi_readl(gsi_ctx->base +
  730. GSI_V2_2_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  731. reg = (reg &
  732. GSI_V2_2_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
  733. GSI_V2_2_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
  734. break;
  735. case GSI_VER_2_5:
  736. reg = gsi_readl(gsi_ctx->base +
  737. GSI_V2_5_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  738. reg = (reg &
  739. GSI_V2_5_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
  740. GSI_V2_5_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
  741. break;
  742. case GSI_VER_2_7:
  743. reg = gsi_readl(gsi_ctx->base +
  744. GSI_V2_7_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  745. reg = (reg &
  746. GSI_V2_7_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_BMSK) >>
  747. GSI_V2_7_EE_n_GSI_HW_PARAM_2_GSI_NUM_CH_PER_EE_SHFT;
  748. break;
  749. }
  750. GSIDBG("max channels %d\n", reg);
  751. return reg;
  752. }
  753. static uint32_t gsi_get_max_event_rings(enum gsi_ver ver)
  754. {
  755. uint32_t reg = 0;
  756. switch (ver) {
  757. case GSI_VER_ERR:
  758. case GSI_VER_MAX:
  759. GSIERR("GSI version is not supported %d\n", ver);
  760. WARN_ON(1);
  761. break;
  762. case GSI_VER_1_0:
  763. reg = gsi_readl(gsi_ctx->base +
  764. GSI_V1_0_EE_n_GSI_HW_PARAM_OFFS(gsi_ctx->per.ee));
  765. reg = (reg & GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_EV_CH_NUM_BMSK) >>
  766. GSI_V1_0_EE_n_GSI_HW_PARAM_GSI_EV_CH_NUM_SHFT;
  767. break;
  768. case GSI_VER_1_2:
  769. reg = gsi_readl(gsi_ctx->base +
  770. GSI_V1_2_EE_n_GSI_HW_PARAM_0_OFFS(gsi_ctx->per.ee));
  771. reg = (reg & GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_EV_CH_NUM_BMSK) >>
  772. GSI_V1_2_EE_n_GSI_HW_PARAM_0_GSI_EV_CH_NUM_SHFT;
  773. break;
  774. case GSI_VER_1_3:
  775. reg = gsi_readl(gsi_ctx->base +
  776. GSI_V1_3_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  777. reg = (reg &
  778. GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
  779. GSI_V1_3_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
  780. break;
  781. case GSI_VER_2_0:
  782. reg = gsi_readl(gsi_ctx->base +
  783. GSI_V2_0_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  784. reg = (reg &
  785. GSI_V2_0_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
  786. GSI_V2_0_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
  787. break;
  788. case GSI_VER_2_2:
  789. reg = gsi_readl(gsi_ctx->base +
  790. GSI_V2_2_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  791. reg = (reg &
  792. GSI_V2_2_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
  793. GSI_V2_2_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
  794. break;
  795. case GSI_VER_2_5:
  796. reg = gsi_readl(gsi_ctx->base +
  797. GSI_V2_5_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  798. reg = (reg &
  799. GSI_V2_5_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
  800. GSI_V2_5_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
  801. break;
  802. case GSI_VER_2_7:
  803. reg = gsi_readl(gsi_ctx->base +
  804. GSI_V2_7_EE_n_GSI_HW_PARAM_2_OFFS(gsi_ctx->per.ee));
  805. reg = (reg &
  806. GSI_V2_7_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_BMSK) >>
  807. GSI_V2_7_EE_n_GSI_HW_PARAM_2_GSI_NUM_EV_PER_EE_SHFT;
  808. break;
  809. }
  810. GSIDBG("max event rings %d\n", reg);
  811. return reg;
  812. }
  813. int gsi_complete_clk_grant(unsigned long dev_hdl)
  814. {
  815. unsigned long flags;
  816. if (!gsi_ctx) {
  817. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  818. return -GSI_STATUS_NODEV;
  819. }
  820. if (!gsi_ctx->per_registered) {
  821. GSIERR("no client registered\n");
  822. return -GSI_STATUS_INVALID_PARAMS;
  823. }
  824. if (dev_hdl != (uintptr_t)gsi_ctx) {
  825. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  826. gsi_ctx);
  827. return -GSI_STATUS_INVALID_PARAMS;
  828. }
  829. spin_lock_irqsave(&gsi_ctx->slock, flags);
  830. gsi_handle_irq();
  831. gsi_ctx->per.rel_clk_cb(gsi_ctx->per.user_data);
  832. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  833. return GSI_STATUS_SUCCESS;
  834. }
  835. EXPORT_SYMBOL(gsi_complete_clk_grant);
  836. int gsi_map_base(phys_addr_t gsi_base_addr, u32 gsi_size)
  837. {
  838. if (!gsi_ctx) {
  839. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  840. return -GSI_STATUS_NODEV;
  841. }
  842. gsi_ctx->base = devm_ioremap_nocache(
  843. gsi_ctx->dev, gsi_base_addr, gsi_size);
  844. if (!gsi_ctx->base) {
  845. GSIERR("failed to map access to GSI HW\n");
  846. return -GSI_STATUS_RES_ALLOC_FAILURE;
  847. }
  848. GSIDBG("GSI base(%pa) mapped to (%pK) with len (0x%x)\n",
  849. &gsi_base_addr,
  850. gsi_ctx->base,
  851. gsi_size);
  852. return 0;
  853. }
  854. EXPORT_SYMBOL(gsi_map_base);
  855. int gsi_unmap_base(void)
  856. {
  857. if (!gsi_ctx) {
  858. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  859. return -GSI_STATUS_NODEV;
  860. }
  861. if (!gsi_ctx->base) {
  862. GSIERR("access to GSI HW has not been mapped\n");
  863. return -GSI_STATUS_INVALID_PARAMS;
  864. }
  865. devm_iounmap(gsi_ctx->dev, gsi_ctx->base);
  866. gsi_ctx->base = NULL;
  867. return 0;
  868. }
  869. EXPORT_SYMBOL(gsi_unmap_base);
  870. int gsi_register_device(struct gsi_per_props *props, unsigned long *dev_hdl)
  871. {
  872. int res;
  873. uint32_t val;
  874. int needed_reg_ver;
  875. if (!gsi_ctx) {
  876. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  877. return -GSI_STATUS_NODEV;
  878. }
  879. if (!props || !dev_hdl) {
  880. GSIERR("bad params props=%pK dev_hdl=%pK\n", props, dev_hdl);
  881. return -GSI_STATUS_INVALID_PARAMS;
  882. }
  883. if (props->ver <= GSI_VER_ERR || props->ver >= GSI_VER_MAX) {
  884. GSIERR("bad params gsi_ver=%d\n", props->ver);
  885. return -GSI_STATUS_INVALID_PARAMS;
  886. }
  887. if (!props->notify_cb) {
  888. GSIERR("notify callback must be provided\n");
  889. return -GSI_STATUS_INVALID_PARAMS;
  890. }
  891. if (props->req_clk_cb && !props->rel_clk_cb) {
  892. GSIERR("rel callback must be provided\n");
  893. return -GSI_STATUS_INVALID_PARAMS;
  894. }
  895. if (gsi_ctx->per_registered) {
  896. GSIERR("per already registered\n");
  897. return -GSI_STATUS_UNSUPPORTED_OP;
  898. }
  899. switch (props->ver) {
  900. case GSI_VER_1_0:
  901. case GSI_VER_1_2:
  902. case GSI_VER_1_3:
  903. case GSI_VER_2_0:
  904. case GSI_VER_2_2:
  905. needed_reg_ver = GSI_REGISTER_VER_1;
  906. break;
  907. case GSI_VER_2_5:
  908. case GSI_VER_2_7:
  909. needed_reg_ver = GSI_REGISTER_VER_2;
  910. break;
  911. case GSI_VER_ERR:
  912. case GSI_VER_MAX:
  913. default:
  914. GSIERR("GSI version is not supported %d\n", props->ver);
  915. return -GSI_STATUS_INVALID_PARAMS;
  916. }
  917. if (needed_reg_ver != GSI_REGISTER_VER_CURRENT) {
  918. GSIERR("Invalid register version. current=%d, needed=%d\n",
  919. GSI_REGISTER_VER_CURRENT, needed_reg_ver);
  920. return -GSI_STATUS_UNSUPPORTED_OP;
  921. }
  922. GSIDBG("gsi ver %d register ver %d needed register ver %d\n",
  923. props->ver, GSI_REGISTER_VER_CURRENT, needed_reg_ver);
  924. spin_lock_init(&gsi_ctx->slock);
  925. if (props->intr == GSI_INTR_IRQ) {
  926. if (!props->irq) {
  927. GSIERR("bad irq specified %u\n", props->irq);
  928. return -GSI_STATUS_INVALID_PARAMS;
  929. }
  930. /*
  931. * On a real UE, there are two separate interrupt
  932. * vectors that get directed toward the GSI/IPA
  933. * drivers. They are handled by gsi_isr() and
  934. * (ipa_isr() or ipa3_isr()) respectively. In the
  935. * emulation environment, this is not the case;
  936. * instead, interrupt vectors are routed to the
  937. * emualation hardware's interrupt controller, which
  938. * in turn, forwards a single interrupt to the GSI/IPA
  939. * driver. When the new interrupt vector is received,
  940. * the driver needs to probe the interrupt
  941. * controller's registers so see if one, the other, or
  942. * both interrupts have occurred. Given the above, we
  943. * now need to handle both situations, namely: the
  944. * emulator's and the real UE.
  945. */
  946. if (running_emulation) {
  947. /*
  948. * New scheme involving the emulator's
  949. * interrupt controller.
  950. */
  951. res = devm_request_threaded_irq(
  952. gsi_ctx->dev,
  953. props->irq,
  954. /* top half handler to follow */
  955. emulator_hard_irq_isr,
  956. /* threaded bottom half handler to follow */
  957. emulator_soft_irq_isr,
  958. IRQF_SHARED,
  959. "emulator_intcntrlr",
  960. gsi_ctx);
  961. } else {
  962. /*
  963. * Traditional scheme used on the real UE.
  964. */
  965. res = devm_request_irq(gsi_ctx->dev, props->irq,
  966. gsi_isr,
  967. props->req_clk_cb ? IRQF_TRIGGER_RISING :
  968. IRQF_TRIGGER_HIGH,
  969. "gsi",
  970. gsi_ctx);
  971. }
  972. if (res) {
  973. GSIERR(
  974. "failed to register isr for %u\n",
  975. props->irq);
  976. return -GSI_STATUS_ERROR;
  977. }
  978. GSIDBG(
  979. "succeeded to register isr for %u\n",
  980. props->irq);
  981. res = enable_irq_wake(props->irq);
  982. if (res)
  983. GSIERR("failed to enable wake irq %u\n", props->irq);
  984. else
  985. GSIERR("GSI irq is wake enabled %u\n", props->irq);
  986. } else {
  987. GSIERR("do not support interrupt type %u\n", props->intr);
  988. return -GSI_STATUS_UNSUPPORTED_OP;
  989. }
  990. /*
  991. * If base not previously mapped via gsi_map_base(), map it
  992. * now...
  993. */
  994. if (!gsi_ctx->base) {
  995. res = gsi_map_base(props->phys_addr, props->size);
  996. if (res)
  997. return res;
  998. }
  999. if (running_emulation) {
  1000. GSIDBG("GSI SW ver register value 0x%x\n",
  1001. gsi_readl(gsi_ctx->base +
  1002. GSI_EE_n_GSI_SW_VERSION_OFFS(0)));
  1003. gsi_ctx->intcntrlr_mem_size =
  1004. props->emulator_intcntrlr_size;
  1005. gsi_ctx->intcntrlr_base =
  1006. devm_ioremap_nocache(
  1007. gsi_ctx->dev,
  1008. props->emulator_intcntrlr_addr,
  1009. props->emulator_intcntrlr_size);
  1010. if (!gsi_ctx->intcntrlr_base) {
  1011. GSIERR(
  1012. "failed to remap emulator's interrupt controller HW\n");
  1013. gsi_unmap_base();
  1014. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1015. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1016. }
  1017. GSIDBG(
  1018. "Emulator's interrupt controller base(%pa) mapped to (%pK) with len (0x%lx)\n",
  1019. &(props->emulator_intcntrlr_addr),
  1020. gsi_ctx->intcntrlr_base,
  1021. props->emulator_intcntrlr_size);
  1022. gsi_ctx->intcntrlr_gsi_isr = gsi_isr;
  1023. gsi_ctx->intcntrlr_client_isr =
  1024. props->emulator_intcntrlr_client_isr;
  1025. }
  1026. gsi_ctx->per = *props;
  1027. gsi_ctx->per_registered = true;
  1028. mutex_init(&gsi_ctx->mlock);
  1029. atomic_set(&gsi_ctx->num_chan, 0);
  1030. atomic_set(&gsi_ctx->num_evt_ring, 0);
  1031. gsi_ctx->max_ch = gsi_get_max_channels(gsi_ctx->per.ver);
  1032. if (gsi_ctx->max_ch == 0) {
  1033. gsi_unmap_base();
  1034. if (running_emulation)
  1035. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1036. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1037. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1038. GSIERR("failed to get max channels\n");
  1039. return -GSI_STATUS_ERROR;
  1040. }
  1041. gsi_ctx->max_ev = gsi_get_max_event_rings(gsi_ctx->per.ver);
  1042. if (gsi_ctx->max_ev == 0) {
  1043. gsi_unmap_base();
  1044. if (running_emulation)
  1045. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1046. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1047. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1048. GSIERR("failed to get max event rings\n");
  1049. return -GSI_STATUS_ERROR;
  1050. }
  1051. if (gsi_ctx->max_ev > GSI_EVT_RING_MAX) {
  1052. GSIERR("max event rings are beyond absolute maximum\n");
  1053. return -GSI_STATUS_ERROR;
  1054. }
  1055. if (props->mhi_er_id_limits_valid &&
  1056. props->mhi_er_id_limits[0] > (gsi_ctx->max_ev - 1)) {
  1057. gsi_unmap_base();
  1058. if (running_emulation)
  1059. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1060. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1061. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1062. GSIERR("MHI event ring start id %u is beyond max %u\n",
  1063. props->mhi_er_id_limits[0], gsi_ctx->max_ev);
  1064. return -GSI_STATUS_ERROR;
  1065. }
  1066. gsi_ctx->evt_bmap = ~((1 << gsi_ctx->max_ev) - 1);
  1067. /* exclude reserved mhi events */
  1068. if (props->mhi_er_id_limits_valid)
  1069. gsi_ctx->evt_bmap |=
  1070. ((1 << (props->mhi_er_id_limits[1] + 1)) - 1) ^
  1071. ((1 << (props->mhi_er_id_limits[0])) - 1);
  1072. /*
  1073. * enable all interrupts but GSI_BREAK_POINT.
  1074. * Inter EE commands / interrupt are no supported.
  1075. */
  1076. __gsi_config_type_irq(props->ee, ~0, ~0);
  1077. __gsi_config_ch_irq(props->ee, ~0, ~0);
  1078. __gsi_config_evt_irq(props->ee, ~0, ~0);
  1079. __gsi_config_ieob_irq(props->ee, ~0, ~0);
  1080. __gsi_config_glob_irq(props->ee, ~0, ~0);
  1081. __gsi_config_gen_irq(props->ee, ~0,
  1082. ~GSI_EE_n_CNTXT_GSI_IRQ_CLR_GSI_BREAK_POINT_BMSK);
  1083. gsi_writel(props->intr, gsi_ctx->base +
  1084. GSI_EE_n_CNTXT_INTSET_OFFS(gsi_ctx->per.ee));
  1085. /* set GSI_TOP_EE_n_CNTXT_MSI_BASE_LSB/MSB to 0 */
  1086. if ((gsi_ctx->per.ver >= GSI_VER_2_0) &&
  1087. (props->intr != GSI_INTR_MSI)) {
  1088. gsi_writel(0, gsi_ctx->base +
  1089. GSI_EE_n_CNTXT_MSI_BASE_LSB(gsi_ctx->per.ee));
  1090. gsi_writel(0, gsi_ctx->base +
  1091. GSI_EE_n_CNTXT_MSI_BASE_MSB(gsi_ctx->per.ee));
  1092. }
  1093. val = gsi_readl(gsi_ctx->base +
  1094. GSI_EE_n_GSI_STATUS_OFFS(gsi_ctx->per.ee));
  1095. if (val & GSI_EE_n_GSI_STATUS_ENABLED_BMSK)
  1096. gsi_ctx->enabled = true;
  1097. else
  1098. GSIERR("Manager EE has not enabled GSI, GSI un-usable\n");
  1099. if (gsi_ctx->per.ver >= GSI_VER_1_2)
  1100. gsi_writel(0, gsi_ctx->base +
  1101. GSI_EE_n_ERROR_LOG_OFFS(gsi_ctx->per.ee));
  1102. if (running_emulation) {
  1103. /*
  1104. * Set up the emulator's interrupt controller...
  1105. */
  1106. res = setup_emulator_cntrlr(
  1107. gsi_ctx->intcntrlr_base, gsi_ctx->intcntrlr_mem_size);
  1108. if (res != 0) {
  1109. gsi_unmap_base();
  1110. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1111. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1112. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1113. GSIERR("setup_emulator_cntrlr() failed\n");
  1114. return res;
  1115. }
  1116. }
  1117. *dev_hdl = (uintptr_t)gsi_ctx;
  1118. return GSI_STATUS_SUCCESS;
  1119. }
  1120. EXPORT_SYMBOL(gsi_register_device);
  1121. int gsi_write_device_scratch(unsigned long dev_hdl,
  1122. struct gsi_device_scratch *val)
  1123. {
  1124. unsigned int max_usb_pkt_size = 0;
  1125. if (!gsi_ctx) {
  1126. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1127. return -GSI_STATUS_NODEV;
  1128. }
  1129. if (!gsi_ctx->per_registered) {
  1130. GSIERR("no client registered\n");
  1131. return -GSI_STATUS_INVALID_PARAMS;
  1132. }
  1133. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1134. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1135. gsi_ctx);
  1136. return -GSI_STATUS_INVALID_PARAMS;
  1137. }
  1138. if (val->max_usb_pkt_size_valid &&
  1139. val->max_usb_pkt_size != 1024 &&
  1140. val->max_usb_pkt_size != 512 &&
  1141. val->max_usb_pkt_size != 64) {
  1142. GSIERR("bad USB max pkt size dev_hdl=0x%lx sz=%u\n", dev_hdl,
  1143. val->max_usb_pkt_size);
  1144. return -GSI_STATUS_INVALID_PARAMS;
  1145. }
  1146. mutex_lock(&gsi_ctx->mlock);
  1147. if (val->mhi_base_chan_idx_valid)
  1148. gsi_ctx->scratch.word0.s.mhi_base_chan_idx =
  1149. val->mhi_base_chan_idx;
  1150. if (val->max_usb_pkt_size_valid) {
  1151. max_usb_pkt_size = 2;
  1152. if (val->max_usb_pkt_size > 64)
  1153. max_usb_pkt_size =
  1154. (val->max_usb_pkt_size == 1024) ? 1 : 0;
  1155. gsi_ctx->scratch.word0.s.max_usb_pkt_size = max_usb_pkt_size;
  1156. }
  1157. gsi_writel(gsi_ctx->scratch.word0.val,
  1158. gsi_ctx->base +
  1159. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  1160. mutex_unlock(&gsi_ctx->mlock);
  1161. return GSI_STATUS_SUCCESS;
  1162. }
  1163. EXPORT_SYMBOL(gsi_write_device_scratch);
  1164. int gsi_deregister_device(unsigned long dev_hdl, bool force)
  1165. {
  1166. if (!gsi_ctx) {
  1167. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1168. return -GSI_STATUS_NODEV;
  1169. }
  1170. if (!gsi_ctx->per_registered) {
  1171. GSIERR("no client registered\n");
  1172. return -GSI_STATUS_INVALID_PARAMS;
  1173. }
  1174. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1175. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1176. gsi_ctx);
  1177. return -GSI_STATUS_INVALID_PARAMS;
  1178. }
  1179. if (!force && atomic_read(&gsi_ctx->num_chan)) {
  1180. GSIERR("cannot deregister %u channels are still connected\n",
  1181. atomic_read(&gsi_ctx->num_chan));
  1182. return -GSI_STATUS_UNSUPPORTED_OP;
  1183. }
  1184. if (!force && atomic_read(&gsi_ctx->num_evt_ring)) {
  1185. GSIERR("cannot deregister %u events are still connected\n",
  1186. atomic_read(&gsi_ctx->num_evt_ring));
  1187. return -GSI_STATUS_UNSUPPORTED_OP;
  1188. }
  1189. /* disable all interrupts */
  1190. __gsi_config_type_irq(gsi_ctx->per.ee, ~0, 0);
  1191. __gsi_config_ch_irq(gsi_ctx->per.ee, ~0, 0);
  1192. __gsi_config_evt_irq(gsi_ctx->per.ee, ~0, 0);
  1193. __gsi_config_ieob_irq(gsi_ctx->per.ee, ~0, 0);
  1194. __gsi_config_glob_irq(gsi_ctx->per.ee, ~0, 0);
  1195. __gsi_config_gen_irq(gsi_ctx->per.ee, ~0, 0);
  1196. devm_free_irq(gsi_ctx->dev, gsi_ctx->per.irq, gsi_ctx);
  1197. gsi_unmap_base();
  1198. memset(gsi_ctx, 0, sizeof(*gsi_ctx));
  1199. return GSI_STATUS_SUCCESS;
  1200. }
  1201. EXPORT_SYMBOL(gsi_deregister_device);
  1202. static void gsi_program_evt_ring_ctx(struct gsi_evt_ring_props *props,
  1203. uint8_t evt_id, unsigned int ee)
  1204. {
  1205. uint32_t val;
  1206. GSIDBG("intf=%u intr=%u re=%u\n", props->intf, props->intr,
  1207. props->re_size);
  1208. val = (((props->intf << GSI_EE_n_EV_CH_k_CNTXT_0_CHTYPE_SHFT) &
  1209. GSI_EE_n_EV_CH_k_CNTXT_0_CHTYPE_BMSK) |
  1210. ((props->intr << GSI_EE_n_EV_CH_k_CNTXT_0_INTYPE_SHFT) &
  1211. GSI_EE_n_EV_CH_k_CNTXT_0_INTYPE_BMSK) |
  1212. ((props->re_size << GSI_EE_n_EV_CH_k_CNTXT_0_ELEMENT_SIZE_SHFT)
  1213. & GSI_EE_n_EV_CH_k_CNTXT_0_ELEMENT_SIZE_BMSK));
  1214. gsi_writel(val, gsi_ctx->base +
  1215. GSI_EE_n_EV_CH_k_CNTXT_0_OFFS(evt_id, ee));
  1216. val = (props->ring_len & GSI_EE_n_EV_CH_k_CNTXT_1_R_LENGTH_BMSK) <<
  1217. GSI_EE_n_EV_CH_k_CNTXT_1_R_LENGTH_SHFT;
  1218. gsi_writel(val, gsi_ctx->base +
  1219. GSI_EE_n_EV_CH_k_CNTXT_1_OFFS(evt_id, ee));
  1220. val = (props->ring_base_addr &
  1221. GSI_EE_n_EV_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_BMSK) <<
  1222. GSI_EE_n_EV_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_SHFT;
  1223. gsi_writel(val, gsi_ctx->base +
  1224. GSI_EE_n_EV_CH_k_CNTXT_2_OFFS(evt_id, ee));
  1225. val = ((props->ring_base_addr >> 32) &
  1226. GSI_EE_n_EV_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_BMSK) <<
  1227. GSI_EE_n_EV_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_SHFT;
  1228. gsi_writel(val, gsi_ctx->base +
  1229. GSI_EE_n_EV_CH_k_CNTXT_3_OFFS(evt_id, ee));
  1230. val = (((props->int_modt << GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODT_SHFT) &
  1231. GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODT_BMSK) |
  1232. ((props->int_modc << GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODC_SHFT) &
  1233. GSI_EE_n_EV_CH_k_CNTXT_8_INT_MODC_BMSK));
  1234. gsi_writel(val, gsi_ctx->base +
  1235. GSI_EE_n_EV_CH_k_CNTXT_8_OFFS(evt_id, ee));
  1236. val = (props->intvec & GSI_EE_n_EV_CH_k_CNTXT_9_INTVEC_BMSK) <<
  1237. GSI_EE_n_EV_CH_k_CNTXT_9_INTVEC_SHFT;
  1238. gsi_writel(val, gsi_ctx->base +
  1239. GSI_EE_n_EV_CH_k_CNTXT_9_OFFS(evt_id, ee));
  1240. val = (props->msi_addr & GSI_EE_n_EV_CH_k_CNTXT_10_MSI_ADDR_LSB_BMSK) <<
  1241. GSI_EE_n_EV_CH_k_CNTXT_10_MSI_ADDR_LSB_SHFT;
  1242. gsi_writel(val, gsi_ctx->base +
  1243. GSI_EE_n_EV_CH_k_CNTXT_10_OFFS(evt_id, ee));
  1244. val = ((props->msi_addr >> 32) &
  1245. GSI_EE_n_EV_CH_k_CNTXT_11_MSI_ADDR_MSB_BMSK) <<
  1246. GSI_EE_n_EV_CH_k_CNTXT_11_MSI_ADDR_MSB_SHFT;
  1247. gsi_writel(val, gsi_ctx->base +
  1248. GSI_EE_n_EV_CH_k_CNTXT_11_OFFS(evt_id, ee));
  1249. val = (props->rp_update_addr &
  1250. GSI_EE_n_EV_CH_k_CNTXT_12_RP_UPDATE_ADDR_LSB_BMSK) <<
  1251. GSI_EE_n_EV_CH_k_CNTXT_12_RP_UPDATE_ADDR_LSB_SHFT;
  1252. gsi_writel(val, gsi_ctx->base +
  1253. GSI_EE_n_EV_CH_k_CNTXT_12_OFFS(evt_id, ee));
  1254. val = ((props->rp_update_addr >> 32) &
  1255. GSI_EE_n_EV_CH_k_CNTXT_13_RP_UPDATE_ADDR_MSB_BMSK) <<
  1256. GSI_EE_n_EV_CH_k_CNTXT_13_RP_UPDATE_ADDR_MSB_SHFT;
  1257. gsi_writel(val, gsi_ctx->base +
  1258. GSI_EE_n_EV_CH_k_CNTXT_13_OFFS(evt_id, ee));
  1259. }
  1260. static void gsi_init_evt_ring(struct gsi_evt_ring_props *props,
  1261. struct gsi_ring_ctx *ctx)
  1262. {
  1263. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  1264. ctx->base = props->ring_base_addr;
  1265. ctx->wp = ctx->base;
  1266. ctx->rp = ctx->base;
  1267. ctx->wp_local = ctx->base;
  1268. ctx->rp_local = ctx->base;
  1269. ctx->len = props->ring_len;
  1270. ctx->elem_sz = props->re_size;
  1271. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  1272. ctx->end = ctx->base + (ctx->max_num_elem + 1) * ctx->elem_sz;
  1273. }
  1274. static void gsi_prime_evt_ring(struct gsi_evt_ctx *ctx)
  1275. {
  1276. unsigned long flags;
  1277. uint32_t val;
  1278. spin_lock_irqsave(&ctx->ring.slock, flags);
  1279. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1280. ctx->ring.wp_local = ctx->ring.base +
  1281. ctx->ring.max_num_elem * ctx->ring.elem_sz;
  1282. /* write order MUST be MSB followed by LSB */
  1283. val = ((ctx->ring.wp_local >> 32) &
  1284. GSI_EE_n_EV_CH_k_DOORBELL_1_WRITE_PTR_MSB_BMSK) <<
  1285. GSI_EE_n_EV_CH_k_DOORBELL_1_WRITE_PTR_MSB_SHFT;
  1286. gsi_writel(val, gsi_ctx->base +
  1287. GSI_EE_n_EV_CH_k_DOORBELL_1_OFFS(ctx->id,
  1288. gsi_ctx->per.ee));
  1289. gsi_ring_evt_doorbell(ctx);
  1290. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1291. }
  1292. static void gsi_prime_evt_ring_wdi(struct gsi_evt_ctx *ctx)
  1293. {
  1294. unsigned long flags;
  1295. spin_lock_irqsave(&ctx->ring.slock, flags);
  1296. if (ctx->ring.base_va)
  1297. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1298. ctx->ring.wp_local = ctx->ring.base +
  1299. ((ctx->ring.max_num_elem + 2) * ctx->ring.elem_sz);
  1300. gsi_ring_evt_doorbell(ctx);
  1301. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1302. }
  1303. static int gsi_validate_evt_ring_props(struct gsi_evt_ring_props *props)
  1304. {
  1305. uint64_t ra;
  1306. if ((props->re_size == GSI_EVT_RING_RE_SIZE_4B &&
  1307. props->ring_len % 4) ||
  1308. (props->re_size == GSI_EVT_RING_RE_SIZE_8B &&
  1309. props->ring_len % 8) ||
  1310. (props->re_size == GSI_EVT_RING_RE_SIZE_16B &&
  1311. props->ring_len % 16)) {
  1312. GSIERR("bad params ring_len %u not a multiple of RE size %u\n",
  1313. props->ring_len, props->re_size);
  1314. return -GSI_STATUS_INVALID_PARAMS;
  1315. }
  1316. ra = props->ring_base_addr;
  1317. do_div(ra, roundup_pow_of_two(props->ring_len));
  1318. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  1319. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  1320. props->ring_base_addr,
  1321. roundup_pow_of_two(props->ring_len));
  1322. return -GSI_STATUS_INVALID_PARAMS;
  1323. }
  1324. if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
  1325. !props->ring_base_vaddr) {
  1326. GSIERR("protocol %u requires ring base VA\n", props->intf);
  1327. return -GSI_STATUS_INVALID_PARAMS;
  1328. }
  1329. if (props->intf == GSI_EVT_CHTYPE_MHI_EV &&
  1330. (!props->evchid_valid ||
  1331. props->evchid > gsi_ctx->per.mhi_er_id_limits[1] ||
  1332. props->evchid < gsi_ctx->per.mhi_er_id_limits[0])) {
  1333. GSIERR("MHI requires evchid valid=%d val=%u\n",
  1334. props->evchid_valid, props->evchid);
  1335. return -GSI_STATUS_INVALID_PARAMS;
  1336. }
  1337. if (props->intf != GSI_EVT_CHTYPE_MHI_EV &&
  1338. props->evchid_valid) {
  1339. GSIERR("protocol %u cannot specify evchid\n", props->intf);
  1340. return -GSI_STATUS_INVALID_PARAMS;
  1341. }
  1342. if (!props->err_cb) {
  1343. GSIERR("err callback must be provided\n");
  1344. return -GSI_STATUS_INVALID_PARAMS;
  1345. }
  1346. return GSI_STATUS_SUCCESS;
  1347. }
  1348. /**
  1349. * gsi_cleanup_xfer_user_data: cleanup the user data array using callback passed
  1350. * by IPA driver. Need to do this in GSI since only GSI knows which TRE
  1351. * are being used or not. However, IPA is the one that does cleaning,
  1352. * therefore we pass a callback from IPA and call it using params from GSI
  1353. *
  1354. * @chan_hdl: hdl of the gsi channel user data array to be cleaned
  1355. * @cleanup_cb: callback used to clean the user data array. takes 2 inputs
  1356. * @chan_user_data: ipa_sys_context of the gsi_channel
  1357. * @xfer_uder_data: user data array element (rx_pkt wrapper)
  1358. *
  1359. * Returns: 0 on success, negative on failure
  1360. */
  1361. static int gsi_cleanup_xfer_user_data(unsigned long chan_hdl,
  1362. void (*cleanup_cb)(void *chan_user_data, void *xfer_user_data))
  1363. {
  1364. struct gsi_chan_ctx *ctx;
  1365. uint64_t i;
  1366. uint16_t rp_idx;
  1367. ctx = &gsi_ctx->chan[chan_hdl];
  1368. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  1369. GSIERR("bad state %d\n", ctx->state);
  1370. return -GSI_STATUS_UNSUPPORTED_OP;
  1371. }
  1372. /* for coalescing, traverse the whole array */
  1373. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  1374. size_t user_data_size =
  1375. ctx->ring.max_num_elem + 1 + GSI_VEID_MAX;
  1376. for (i = 0; i < user_data_size; i++) {
  1377. if (ctx->user_data[i].valid)
  1378. cleanup_cb(ctx->props.chan_user_data,
  1379. ctx->user_data[i].p);
  1380. }
  1381. } else {
  1382. /* for non-coalescing, clean between RP and WP */
  1383. while (ctx->ring.rp_local != ctx->ring.wp_local) {
  1384. rp_idx = gsi_find_idx_from_addr(&ctx->ring,
  1385. ctx->ring.rp_local);
  1386. WARN_ON(!ctx->user_data[rp_idx].valid);
  1387. cleanup_cb(ctx->props.chan_user_data,
  1388. ctx->user_data[rp_idx].p);
  1389. gsi_incr_ring_rp(&ctx->ring);
  1390. }
  1391. }
  1392. return 0;
  1393. }
  1394. int gsi_alloc_evt_ring(struct gsi_evt_ring_props *props, unsigned long dev_hdl,
  1395. unsigned long *evt_ring_hdl)
  1396. {
  1397. unsigned long evt_id;
  1398. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_ALLOCATE;
  1399. uint32_t val;
  1400. struct gsi_evt_ctx *ctx;
  1401. int res;
  1402. int ee;
  1403. unsigned long flags;
  1404. if (!gsi_ctx) {
  1405. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1406. return -GSI_STATUS_NODEV;
  1407. }
  1408. if (!props || !evt_ring_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  1409. GSIERR("bad params props=%pK dev_hdl=0x%lx evt_ring_hdl=%pK\n",
  1410. props, dev_hdl, evt_ring_hdl);
  1411. return -GSI_STATUS_INVALID_PARAMS;
  1412. }
  1413. if (gsi_validate_evt_ring_props(props)) {
  1414. GSIERR("invalid params\n");
  1415. return -GSI_STATUS_INVALID_PARAMS;
  1416. }
  1417. if (!props->evchid_valid) {
  1418. mutex_lock(&gsi_ctx->mlock);
  1419. evt_id = find_first_zero_bit(&gsi_ctx->evt_bmap,
  1420. sizeof(unsigned long) * BITS_PER_BYTE);
  1421. if (evt_id == sizeof(unsigned long) * BITS_PER_BYTE) {
  1422. GSIERR("failed to alloc event ID\n");
  1423. mutex_unlock(&gsi_ctx->mlock);
  1424. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1425. }
  1426. set_bit(evt_id, &gsi_ctx->evt_bmap);
  1427. mutex_unlock(&gsi_ctx->mlock);
  1428. } else {
  1429. evt_id = props->evchid;
  1430. }
  1431. GSIDBG("Using %lu as virt evt id\n", evt_id);
  1432. ctx = &gsi_ctx->evtr[evt_id];
  1433. memset(ctx, 0, sizeof(*ctx));
  1434. mutex_init(&ctx->mlock);
  1435. init_completion(&ctx->compl);
  1436. atomic_set(&ctx->chan_ref_cnt, 0);
  1437. ctx->props = *props;
  1438. mutex_lock(&gsi_ctx->mlock);
  1439. val = (((evt_id << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
  1440. GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
  1441. ((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
  1442. GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
  1443. ee = gsi_ctx->per.ee;
  1444. gsi_writel(val, gsi_ctx->base +
  1445. GSI_EE_n_EV_CH_CMD_OFFS(ee));
  1446. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  1447. if (res == 0) {
  1448. GSIERR("evt_id=%lu timed out\n", evt_id);
  1449. if (!props->evchid_valid)
  1450. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1451. mutex_unlock(&gsi_ctx->mlock);
  1452. return -GSI_STATUS_TIMED_OUT;
  1453. }
  1454. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1455. GSIERR("evt_id=%lu allocation failed state=%u\n",
  1456. evt_id, ctx->state);
  1457. if (!props->evchid_valid)
  1458. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1459. mutex_unlock(&gsi_ctx->mlock);
  1460. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1461. }
  1462. gsi_program_evt_ring_ctx(props, evt_id, gsi_ctx->per.ee);
  1463. spin_lock_init(&ctx->ring.slock);
  1464. gsi_init_evt_ring(props, &ctx->ring);
  1465. ctx->id = evt_id;
  1466. *evt_ring_hdl = evt_id;
  1467. atomic_inc(&gsi_ctx->num_evt_ring);
  1468. if (props->intf == GSI_EVT_CHTYPE_GPI_EV)
  1469. gsi_prime_evt_ring(ctx);
  1470. else if (props->intf == GSI_EVT_CHTYPE_WDI2_EV)
  1471. gsi_prime_evt_ring_wdi(ctx);
  1472. mutex_unlock(&gsi_ctx->mlock);
  1473. spin_lock_irqsave(&gsi_ctx->slock, flags);
  1474. gsi_writel(1 << evt_id, gsi_ctx->base +
  1475. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(ee));
  1476. /* enable ieob interrupts for GPI, enable MSI interrupts */
  1477. if ((props->intf != GSI_EVT_CHTYPE_GPI_EV) &&
  1478. (props->intr != GSI_INTR_MSI))
  1479. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << evt_id, 0);
  1480. else
  1481. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->id, ~0);
  1482. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  1483. return GSI_STATUS_SUCCESS;
  1484. }
  1485. EXPORT_SYMBOL(gsi_alloc_evt_ring);
  1486. static void __gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  1487. union __packed gsi_evt_scratch val)
  1488. {
  1489. gsi_writel(val.data.word1, gsi_ctx->base +
  1490. GSI_EE_n_EV_CH_k_SCRATCH_0_OFFS(evt_ring_hdl,
  1491. gsi_ctx->per.ee));
  1492. gsi_writel(val.data.word2, gsi_ctx->base +
  1493. GSI_EE_n_EV_CH_k_SCRATCH_1_OFFS(evt_ring_hdl,
  1494. gsi_ctx->per.ee));
  1495. }
  1496. int gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  1497. union __packed gsi_evt_scratch val)
  1498. {
  1499. struct gsi_evt_ctx *ctx;
  1500. if (!gsi_ctx) {
  1501. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1502. return -GSI_STATUS_NODEV;
  1503. }
  1504. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1505. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1506. return -GSI_STATUS_INVALID_PARAMS;
  1507. }
  1508. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1509. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1510. GSIERR("bad state %d\n",
  1511. gsi_ctx->evtr[evt_ring_hdl].state);
  1512. return -GSI_STATUS_UNSUPPORTED_OP;
  1513. }
  1514. mutex_lock(&ctx->mlock);
  1515. ctx->scratch = val;
  1516. __gsi_write_evt_ring_scratch(evt_ring_hdl, val);
  1517. mutex_unlock(&ctx->mlock);
  1518. return GSI_STATUS_SUCCESS;
  1519. }
  1520. EXPORT_SYMBOL(gsi_write_evt_ring_scratch);
  1521. int gsi_dealloc_evt_ring(unsigned long evt_ring_hdl)
  1522. {
  1523. uint32_t val;
  1524. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_DE_ALLOC;
  1525. struct gsi_evt_ctx *ctx;
  1526. int res;
  1527. if (!gsi_ctx) {
  1528. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1529. return -GSI_STATUS_NODEV;
  1530. }
  1531. if (evt_ring_hdl >= gsi_ctx->max_ev ||
  1532. evt_ring_hdl >= GSI_EVT_RING_MAX) {
  1533. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1534. return -GSI_STATUS_INVALID_PARAMS;
  1535. }
  1536. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1537. if (atomic_read(&ctx->chan_ref_cnt)) {
  1538. GSIERR("%d channels still using this event ring\n",
  1539. atomic_read(&ctx->chan_ref_cnt));
  1540. return -GSI_STATUS_UNSUPPORTED_OP;
  1541. }
  1542. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1543. GSIERR("bad state %d\n", ctx->state);
  1544. return -GSI_STATUS_UNSUPPORTED_OP;
  1545. }
  1546. mutex_lock(&gsi_ctx->mlock);
  1547. reinit_completion(&ctx->compl);
  1548. val = (((evt_ring_hdl << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
  1549. GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
  1550. ((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
  1551. GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
  1552. gsi_writel(val, gsi_ctx->base +
  1553. GSI_EE_n_EV_CH_CMD_OFFS(gsi_ctx->per.ee));
  1554. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  1555. if (res == 0) {
  1556. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  1557. mutex_unlock(&gsi_ctx->mlock);
  1558. return -GSI_STATUS_TIMED_OUT;
  1559. }
  1560. if (ctx->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  1561. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  1562. ctx->state);
  1563. /*
  1564. * IPA Hardware returned GSI RING not allocated, which is
  1565. * unexpected hardware state.
  1566. */
  1567. GSI_ASSERT();
  1568. }
  1569. mutex_unlock(&gsi_ctx->mlock);
  1570. if (!ctx->props.evchid_valid) {
  1571. mutex_lock(&gsi_ctx->mlock);
  1572. clear_bit(evt_ring_hdl, &gsi_ctx->evt_bmap);
  1573. mutex_unlock(&gsi_ctx->mlock);
  1574. }
  1575. atomic_dec(&gsi_ctx->num_evt_ring);
  1576. return GSI_STATUS_SUCCESS;
  1577. }
  1578. EXPORT_SYMBOL(gsi_dealloc_evt_ring);
  1579. int gsi_query_evt_ring_db_addr(unsigned long evt_ring_hdl,
  1580. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  1581. {
  1582. struct gsi_evt_ctx *ctx;
  1583. if (!gsi_ctx) {
  1584. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1585. return -GSI_STATUS_NODEV;
  1586. }
  1587. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  1588. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  1589. db_addr_wp_lsb);
  1590. return -GSI_STATUS_INVALID_PARAMS;
  1591. }
  1592. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1593. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1594. return -GSI_STATUS_INVALID_PARAMS;
  1595. }
  1596. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1597. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1598. GSIERR("bad state %d\n",
  1599. gsi_ctx->evtr[evt_ring_hdl].state);
  1600. return -GSI_STATUS_UNSUPPORTED_OP;
  1601. }
  1602. *db_addr_wp_lsb = gsi_ctx->per.phys_addr +
  1603. GSI_EE_n_EV_CH_k_DOORBELL_0_OFFS(evt_ring_hdl, gsi_ctx->per.ee);
  1604. *db_addr_wp_msb = gsi_ctx->per.phys_addr +
  1605. GSI_EE_n_EV_CH_k_DOORBELL_1_OFFS(evt_ring_hdl, gsi_ctx->per.ee);
  1606. return GSI_STATUS_SUCCESS;
  1607. }
  1608. EXPORT_SYMBOL(gsi_query_evt_ring_db_addr);
  1609. int gsi_ring_evt_ring_db(unsigned long evt_ring_hdl, uint64_t value)
  1610. {
  1611. struct gsi_evt_ctx *ctx;
  1612. if (!gsi_ctx) {
  1613. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1614. return -GSI_STATUS_NODEV;
  1615. }
  1616. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1617. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1618. return -GSI_STATUS_INVALID_PARAMS;
  1619. }
  1620. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1621. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1622. GSIERR("bad state %d\n",
  1623. gsi_ctx->evtr[evt_ring_hdl].state);
  1624. return -GSI_STATUS_UNSUPPORTED_OP;
  1625. }
  1626. ctx->ring.wp_local = value;
  1627. gsi_ring_evt_doorbell(ctx);
  1628. return GSI_STATUS_SUCCESS;
  1629. }
  1630. EXPORT_SYMBOL(gsi_ring_evt_ring_db);
  1631. int gsi_ring_ch_ring_db(unsigned long chan_hdl, uint64_t value)
  1632. {
  1633. struct gsi_chan_ctx *ctx;
  1634. uint32_t val;
  1635. if (!gsi_ctx) {
  1636. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1637. return -GSI_STATUS_NODEV;
  1638. }
  1639. if (chan_hdl >= gsi_ctx->max_ch) {
  1640. GSIERR("bad chan_hdl=%lu\n", chan_hdl);
  1641. return -GSI_STATUS_INVALID_PARAMS;
  1642. }
  1643. ctx = &gsi_ctx->chan[chan_hdl];
  1644. if (ctx->state != GSI_CHAN_STATE_STARTED) {
  1645. GSIERR("bad state %d\n", ctx->state);
  1646. return -GSI_STATUS_UNSUPPORTED_OP;
  1647. }
  1648. ctx->ring.wp_local = value;
  1649. /* write MSB first */
  1650. val = ((ctx->ring.wp_local >> 32) &
  1651. GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_BMSK) <<
  1652. GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_SHFT;
  1653. gsi_writel(val, gsi_ctx->base +
  1654. GSI_EE_n_GSI_CH_k_DOORBELL_1_OFFS(ctx->props.ch_id,
  1655. gsi_ctx->per.ee));
  1656. gsi_ring_chan_doorbell(ctx);
  1657. return GSI_STATUS_SUCCESS;
  1658. }
  1659. EXPORT_SYMBOL(gsi_ring_ch_ring_db);
  1660. int gsi_reset_evt_ring(unsigned long evt_ring_hdl)
  1661. {
  1662. uint32_t val;
  1663. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_RESET;
  1664. struct gsi_evt_ctx *ctx;
  1665. int res;
  1666. if (!gsi_ctx) {
  1667. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1668. return -GSI_STATUS_NODEV;
  1669. }
  1670. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1671. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1672. return -GSI_STATUS_INVALID_PARAMS;
  1673. }
  1674. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1675. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1676. GSIERR("bad state %d\n", ctx->state);
  1677. return -GSI_STATUS_UNSUPPORTED_OP;
  1678. }
  1679. mutex_lock(&gsi_ctx->mlock);
  1680. reinit_completion(&ctx->compl);
  1681. val = (((evt_ring_hdl << GSI_EE_n_EV_CH_CMD_CHID_SHFT) &
  1682. GSI_EE_n_EV_CH_CMD_CHID_BMSK) |
  1683. ((op << GSI_EE_n_EV_CH_CMD_OPCODE_SHFT) &
  1684. GSI_EE_n_EV_CH_CMD_OPCODE_BMSK));
  1685. gsi_writel(val, gsi_ctx->base +
  1686. GSI_EE_n_EV_CH_CMD_OFFS(gsi_ctx->per.ee));
  1687. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  1688. if (res == 0) {
  1689. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  1690. mutex_unlock(&gsi_ctx->mlock);
  1691. return -GSI_STATUS_TIMED_OUT;
  1692. }
  1693. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1694. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  1695. ctx->state);
  1696. /*
  1697. * IPA Hardware returned GSI RING not allocated, which is
  1698. * unexpected. Indicates hardware instability.
  1699. */
  1700. GSI_ASSERT();
  1701. }
  1702. gsi_program_evt_ring_ctx(&ctx->props, evt_ring_hdl, gsi_ctx->per.ee);
  1703. gsi_init_evt_ring(&ctx->props, &ctx->ring);
  1704. /* restore scratch */
  1705. __gsi_write_evt_ring_scratch(evt_ring_hdl, ctx->scratch);
  1706. if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV)
  1707. gsi_prime_evt_ring(ctx);
  1708. if (ctx->props.intf == GSI_EVT_CHTYPE_WDI2_EV)
  1709. gsi_prime_evt_ring_wdi(ctx);
  1710. mutex_unlock(&gsi_ctx->mlock);
  1711. return GSI_STATUS_SUCCESS;
  1712. }
  1713. EXPORT_SYMBOL(gsi_reset_evt_ring);
  1714. int gsi_get_evt_ring_cfg(unsigned long evt_ring_hdl,
  1715. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  1716. {
  1717. struct gsi_evt_ctx *ctx;
  1718. if (!gsi_ctx) {
  1719. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1720. return -GSI_STATUS_NODEV;
  1721. }
  1722. if (!props || !scr) {
  1723. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  1724. return -GSI_STATUS_INVALID_PARAMS;
  1725. }
  1726. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1727. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1728. return -GSI_STATUS_INVALID_PARAMS;
  1729. }
  1730. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1731. if (ctx->state == GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  1732. GSIERR("bad state %d\n", ctx->state);
  1733. return -GSI_STATUS_UNSUPPORTED_OP;
  1734. }
  1735. mutex_lock(&ctx->mlock);
  1736. *props = ctx->props;
  1737. *scr = ctx->scratch;
  1738. mutex_unlock(&ctx->mlock);
  1739. return GSI_STATUS_SUCCESS;
  1740. }
  1741. EXPORT_SYMBOL(gsi_get_evt_ring_cfg);
  1742. int gsi_set_evt_ring_cfg(unsigned long evt_ring_hdl,
  1743. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  1744. {
  1745. struct gsi_evt_ctx *ctx;
  1746. if (!gsi_ctx) {
  1747. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1748. return -GSI_STATUS_NODEV;
  1749. }
  1750. if (!props || gsi_validate_evt_ring_props(props)) {
  1751. GSIERR("bad params props=%pK\n", props);
  1752. return -GSI_STATUS_INVALID_PARAMS;
  1753. }
  1754. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  1755. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  1756. return -GSI_STATUS_INVALID_PARAMS;
  1757. }
  1758. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  1759. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1760. GSIERR("bad state %d\n", ctx->state);
  1761. return -GSI_STATUS_UNSUPPORTED_OP;
  1762. }
  1763. if (ctx->props.exclusive != props->exclusive) {
  1764. GSIERR("changing immutable fields not supported\n");
  1765. return -GSI_STATUS_UNSUPPORTED_OP;
  1766. }
  1767. mutex_lock(&ctx->mlock);
  1768. ctx->props = *props;
  1769. if (scr)
  1770. ctx->scratch = *scr;
  1771. mutex_unlock(&ctx->mlock);
  1772. return gsi_reset_evt_ring(evt_ring_hdl);
  1773. }
  1774. EXPORT_SYMBOL(gsi_set_evt_ring_cfg);
  1775. static void gsi_program_chan_ctx_qos(struct gsi_chan_props *props,
  1776. unsigned int ee)
  1777. {
  1778. uint32_t val;
  1779. val =
  1780. (((props->low_weight <<
  1781. GSI_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_SHFT) &
  1782. GSI_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_BMSK) |
  1783. ((props->max_prefetch <<
  1784. GSI_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_SHFT) &
  1785. GSI_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_BMSK) |
  1786. ((props->use_db_eng <<
  1787. GSI_EE_n_GSI_CH_k_QOS_USE_DB_ENG_SHFT) &
  1788. GSI_EE_n_GSI_CH_k_QOS_USE_DB_ENG_BMSK));
  1789. if (gsi_ctx->per.ver >= GSI_VER_2_0)
  1790. val |= ((props->prefetch_mode <<
  1791. GSI_EE_n_GSI_CH_k_QOS_USE_ESCAPE_BUF_ONLY_SHFT)
  1792. & GSI_EE_n_GSI_CH_k_QOS_USE_ESCAPE_BUF_ONLY_BMSK);
  1793. gsi_writel(val, gsi_ctx->base +
  1794. GSI_EE_n_GSI_CH_k_QOS_OFFS(props->ch_id, ee));
  1795. }
  1796. static void gsi_program_chan_ctx_qos_v2_5(struct gsi_chan_props *props,
  1797. unsigned int ee)
  1798. {
  1799. uint32_t val;
  1800. val =
  1801. (((props->low_weight <<
  1802. GSI_V2_5_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_SHFT) &
  1803. GSI_V2_5_EE_n_GSI_CH_k_QOS_WRR_WEIGHT_BMSK) |
  1804. ((props->max_prefetch <<
  1805. GSI_V2_5_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_SHFT) &
  1806. GSI_V2_5_EE_n_GSI_CH_k_QOS_MAX_PREFETCH_BMSK) |
  1807. ((props->use_db_eng <<
  1808. GSI_V2_5_EE_n_GSI_CH_k_QOS_USE_DB_ENG_SHFT) &
  1809. GSI_V2_5_EE_n_GSI_CH_k_QOS_USE_DB_ENG_BMSK) |
  1810. ((props->prefetch_mode <<
  1811. GSI_V2_5_EE_n_GSI_CH_k_QOS_PREFETCH_MODE_SHFT) &
  1812. GSI_V2_5_EE_n_GSI_CH_k_QOS_PREFETCH_MODE_BMSK) |
  1813. ((props->empty_lvl_threshold <<
  1814. GSI_V2_5_EE_n_GSI_CH_k_QOS_EMPTY_LVL_THRSHOLD_SHFT) &
  1815. GSI_V2_5_EE_n_GSI_CH_k_QOS_EMPTY_LVL_THRSHOLD_BMSK));
  1816. gsi_writel(val, gsi_ctx->base +
  1817. GSI_V2_5_EE_n_GSI_CH_k_QOS_OFFS(props->ch_id, ee));
  1818. }
  1819. static void gsi_program_chan_ctx(struct gsi_chan_props *props, unsigned int ee,
  1820. uint8_t erindex)
  1821. {
  1822. uint32_t val;
  1823. uint32_t prot;
  1824. uint32_t prot_msb;
  1825. switch (props->prot) {
  1826. case GSI_CHAN_PROT_MHI:
  1827. case GSI_CHAN_PROT_XHCI:
  1828. case GSI_CHAN_PROT_GPI:
  1829. case GSI_CHAN_PROT_XDCI:
  1830. case GSI_CHAN_PROT_WDI2:
  1831. case GSI_CHAN_PROT_WDI3:
  1832. case GSI_CHAN_PROT_GCI:
  1833. case GSI_CHAN_PROT_MHIP:
  1834. prot_msb = 0;
  1835. break;
  1836. case GSI_CHAN_PROT_AQC:
  1837. case GSI_CHAN_PROT_11AD:
  1838. prot_msb = 1;
  1839. break;
  1840. default:
  1841. GSIERR("Unsupported protocol %d\n", props->prot);
  1842. WARN_ON(1);
  1843. return;
  1844. }
  1845. prot = props->prot;
  1846. val = ((prot <<
  1847. GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_SHFT) &
  1848. GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_BMSK);
  1849. if (gsi_ctx->per.ver >= GSI_VER_2_5) {
  1850. val |= ((prot_msb <<
  1851. GSI_V2_5_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_MSB_SHFT) &
  1852. GSI_V2_5_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_PROTOCOL_MSB_BMSK);
  1853. }
  1854. val |= (((props->dir << GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_DIR_SHFT) &
  1855. GSI_EE_n_GSI_CH_k_CNTXT_0_CHTYPE_DIR_BMSK) |
  1856. ((erindex << GSI_EE_n_GSI_CH_k_CNTXT_0_ERINDEX_SHFT) &
  1857. GSI_EE_n_GSI_CH_k_CNTXT_0_ERINDEX_BMSK) |
  1858. ((props->re_size << GSI_EE_n_GSI_CH_k_CNTXT_0_ELEMENT_SIZE_SHFT)
  1859. & GSI_EE_n_GSI_CH_k_CNTXT_0_ELEMENT_SIZE_BMSK));
  1860. gsi_writel(val, gsi_ctx->base +
  1861. GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(props->ch_id, ee));
  1862. val = (props->ring_len & GSI_EE_n_GSI_CH_k_CNTXT_1_R_LENGTH_BMSK) <<
  1863. GSI_EE_n_GSI_CH_k_CNTXT_1_R_LENGTH_SHFT;
  1864. gsi_writel(val, gsi_ctx->base +
  1865. GSI_EE_n_GSI_CH_k_CNTXT_1_OFFS(props->ch_id, ee));
  1866. val = (props->ring_base_addr &
  1867. GSI_EE_n_GSI_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_BMSK) <<
  1868. GSI_EE_n_GSI_CH_k_CNTXT_2_R_BASE_ADDR_LSBS_SHFT;
  1869. gsi_writel(val, gsi_ctx->base +
  1870. GSI_EE_n_GSI_CH_k_CNTXT_2_OFFS(props->ch_id, ee));
  1871. val = ((props->ring_base_addr >> 32) &
  1872. GSI_EE_n_GSI_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_BMSK) <<
  1873. GSI_EE_n_GSI_CH_k_CNTXT_3_R_BASE_ADDR_MSBS_SHFT;
  1874. gsi_writel(val, gsi_ctx->base +
  1875. GSI_EE_n_GSI_CH_k_CNTXT_3_OFFS(props->ch_id, ee));
  1876. if (gsi_ctx->per.ver >= GSI_VER_2_5)
  1877. gsi_program_chan_ctx_qos_v2_5(props, ee);
  1878. else
  1879. gsi_program_chan_ctx_qos(props, ee);
  1880. }
  1881. static void gsi_init_chan_ring(struct gsi_chan_props *props,
  1882. struct gsi_ring_ctx *ctx)
  1883. {
  1884. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  1885. ctx->base = props->ring_base_addr;
  1886. ctx->wp = ctx->base;
  1887. ctx->rp = ctx->base;
  1888. ctx->wp_local = ctx->base;
  1889. ctx->rp_local = ctx->base;
  1890. ctx->len = props->ring_len;
  1891. ctx->elem_sz = props->re_size;
  1892. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  1893. ctx->end = ctx->base + (ctx->max_num_elem + 1) *
  1894. ctx->elem_sz;
  1895. }
  1896. static int gsi_validate_channel_props(struct gsi_chan_props *props)
  1897. {
  1898. uint64_t ra;
  1899. uint64_t last;
  1900. if (props->ch_id >= gsi_ctx->max_ch) {
  1901. GSIERR("ch_id %u invalid\n", props->ch_id);
  1902. return -GSI_STATUS_INVALID_PARAMS;
  1903. }
  1904. if ((props->re_size == GSI_CHAN_RE_SIZE_4B &&
  1905. props->ring_len % 4) ||
  1906. (props->re_size == GSI_CHAN_RE_SIZE_8B &&
  1907. props->ring_len % 8) ||
  1908. (props->re_size == GSI_CHAN_RE_SIZE_16B &&
  1909. props->ring_len % 16) ||
  1910. (props->re_size == GSI_CHAN_RE_SIZE_32B &&
  1911. props->ring_len % 32)) {
  1912. GSIERR("bad params ring_len %u not a multiple of re size %u\n",
  1913. props->ring_len, props->re_size);
  1914. return -GSI_STATUS_INVALID_PARAMS;
  1915. }
  1916. ra = props->ring_base_addr;
  1917. do_div(ra, roundup_pow_of_two(props->ring_len));
  1918. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  1919. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  1920. props->ring_base_addr,
  1921. roundup_pow_of_two(props->ring_len));
  1922. return -GSI_STATUS_INVALID_PARAMS;
  1923. }
  1924. last = props->ring_base_addr + props->ring_len - props->re_size;
  1925. /* MSB should stay same within the ring */
  1926. if ((props->ring_base_addr & 0xFFFFFFFF00000000ULL) !=
  1927. (last & 0xFFFFFFFF00000000ULL)) {
  1928. GSIERR("MSB is not fixed on ring base 0x%llx size 0x%x\n",
  1929. props->ring_base_addr,
  1930. props->ring_len);
  1931. return -GSI_STATUS_INVALID_PARAMS;
  1932. }
  1933. if (props->prot == GSI_CHAN_PROT_GPI &&
  1934. !props->ring_base_vaddr) {
  1935. GSIERR("protocol %u requires ring base VA\n", props->prot);
  1936. return -GSI_STATUS_INVALID_PARAMS;
  1937. }
  1938. if (props->low_weight > GSI_MAX_CH_LOW_WEIGHT) {
  1939. GSIERR("invalid channel low weight %u\n", props->low_weight);
  1940. return -GSI_STATUS_INVALID_PARAMS;
  1941. }
  1942. if (props->prot == GSI_CHAN_PROT_GPI && !props->xfer_cb) {
  1943. GSIERR("xfer callback must be provided\n");
  1944. return -GSI_STATUS_INVALID_PARAMS;
  1945. }
  1946. if (!props->err_cb) {
  1947. GSIERR("err callback must be provided\n");
  1948. return -GSI_STATUS_INVALID_PARAMS;
  1949. }
  1950. return GSI_STATUS_SUCCESS;
  1951. }
  1952. int gsi_alloc_channel(struct gsi_chan_props *props, unsigned long dev_hdl,
  1953. unsigned long *chan_hdl)
  1954. {
  1955. struct gsi_chan_ctx *ctx;
  1956. uint32_t val;
  1957. int res;
  1958. int ee;
  1959. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  1960. uint8_t erindex;
  1961. struct gsi_user_data *user_data;
  1962. size_t user_data_size;
  1963. if (!gsi_ctx) {
  1964. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1965. return -GSI_STATUS_NODEV;
  1966. }
  1967. if (!props || !chan_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  1968. GSIERR("bad params props=%pK dev_hdl=0x%lx chan_hdl=%pK\n",
  1969. props, dev_hdl, chan_hdl);
  1970. return -GSI_STATUS_INVALID_PARAMS;
  1971. }
  1972. if (gsi_validate_channel_props(props)) {
  1973. GSIERR("bad params\n");
  1974. return -GSI_STATUS_INVALID_PARAMS;
  1975. }
  1976. if (props->evt_ring_hdl != ~0) {
  1977. if (props->evt_ring_hdl >= gsi_ctx->max_ev) {
  1978. GSIERR("invalid evt ring=%lu\n", props->evt_ring_hdl);
  1979. return -GSI_STATUS_INVALID_PARAMS;
  1980. }
  1981. if (atomic_read(
  1982. &gsi_ctx->evtr[props->evt_ring_hdl].chan_ref_cnt) &&
  1983. gsi_ctx->evtr[props->evt_ring_hdl].props.exclusive &&
  1984. gsi_ctx->evtr[props->evt_ring_hdl].chan->props.prot !=
  1985. GSI_CHAN_PROT_GCI) {
  1986. GSIERR("evt ring=%lu exclusively used by ch_hdl=%pK\n",
  1987. props->evt_ring_hdl, chan_hdl);
  1988. return -GSI_STATUS_UNSUPPORTED_OP;
  1989. }
  1990. }
  1991. ctx = &gsi_ctx->chan[props->ch_id];
  1992. if (ctx->allocated) {
  1993. GSIERR("chan %d already allocated\n", props->ch_id);
  1994. return -GSI_STATUS_NODEV;
  1995. }
  1996. memset(ctx, 0, sizeof(*ctx));
  1997. /* For IPA offloaded WDI channels not required user_data pointer */
  1998. if (props->prot != GSI_CHAN_PROT_WDI2 &&
  1999. props->prot != GSI_CHAN_PROT_WDI3)
  2000. user_data_size = props->ring_len / props->re_size;
  2001. else
  2002. user_data_size = props->re_size;
  2003. /*
  2004. * GCI channels might have OOO event completions up to GSI_VEID_MAX.
  2005. * user_data needs to be large enough to accommodate those.
  2006. * TODO: increase user data size if GSI_VEID_MAX is not enough
  2007. */
  2008. if (props->prot == GSI_CHAN_PROT_GCI)
  2009. user_data_size += GSI_VEID_MAX;
  2010. user_data = devm_kzalloc(gsi_ctx->dev,
  2011. user_data_size * sizeof(*user_data),
  2012. GFP_KERNEL);
  2013. if (user_data == NULL) {
  2014. GSIERR("context not allocated\n");
  2015. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2016. }
  2017. mutex_init(&ctx->mlock);
  2018. init_completion(&ctx->compl);
  2019. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2020. ctx->props = *props;
  2021. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  2022. mutex_lock(&gsi_ctx->mlock);
  2023. ee = gsi_ctx->per.ee;
  2024. gsi_ctx->ch_dbg[props->ch_id].ch_allocate++;
  2025. val = (((props->ch_id << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2026. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2027. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2028. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2029. gsi_writel(val, gsi_ctx->base +
  2030. GSI_EE_n_GSI_CH_CMD_OFFS(ee));
  2031. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2032. if (res == 0) {
  2033. GSIERR("chan_hdl=%u timed out\n", props->ch_id);
  2034. mutex_unlock(&gsi_ctx->mlock);
  2035. devm_kfree(gsi_ctx->dev, user_data);
  2036. return -GSI_STATUS_TIMED_OUT;
  2037. }
  2038. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2039. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2040. props->ch_id, ctx->state);
  2041. mutex_unlock(&gsi_ctx->mlock);
  2042. devm_kfree(gsi_ctx->dev, user_data);
  2043. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2044. }
  2045. mutex_unlock(&gsi_ctx->mlock);
  2046. } else {
  2047. mutex_lock(&gsi_ctx->mlock);
  2048. ctx->state = GSI_CHAN_STATE_ALLOCATED;
  2049. mutex_unlock(&gsi_ctx->mlock);
  2050. }
  2051. erindex = props->evt_ring_hdl != ~0 ? props->evt_ring_hdl :
  2052. GSI_NO_EVT_ERINDEX;
  2053. if (erindex != GSI_NO_EVT_ERINDEX && erindex >= GSI_EVT_RING_MAX) {
  2054. GSIERR("invalid erindex %u\n", erindex);
  2055. devm_kfree(gsi_ctx->dev, user_data);
  2056. return -GSI_STATUS_INVALID_PARAMS;
  2057. }
  2058. if (erindex < GSI_EVT_RING_MAX) {
  2059. ctx->evtr = &gsi_ctx->evtr[erindex];
  2060. if (props->prot != GSI_CHAN_PROT_GCI)
  2061. atomic_inc(&ctx->evtr->chan_ref_cnt);
  2062. if (props->prot != GSI_CHAN_PROT_GCI &&
  2063. ctx->evtr->props.exclusive &&
  2064. atomic_read(&ctx->evtr->chan_ref_cnt) == 1)
  2065. ctx->evtr->chan = ctx;
  2066. }
  2067. gsi_program_chan_ctx(props, gsi_ctx->per.ee, erindex);
  2068. spin_lock_init(&ctx->ring.slock);
  2069. gsi_init_chan_ring(props, &ctx->ring);
  2070. if (!props->max_re_expected)
  2071. ctx->props.max_re_expected = ctx->ring.max_num_elem;
  2072. ctx->user_data = user_data;
  2073. *chan_hdl = props->ch_id;
  2074. ctx->allocated = true;
  2075. ctx->stats.dp.last_timestamp = jiffies_to_msecs(jiffies);
  2076. atomic_inc(&gsi_ctx->num_chan);
  2077. return GSI_STATUS_SUCCESS;
  2078. }
  2079. EXPORT_SYMBOL(gsi_alloc_channel);
  2080. static int gsi_alloc_ap_channel(unsigned int chan_hdl)
  2081. {
  2082. struct gsi_chan_ctx *ctx;
  2083. uint32_t val;
  2084. int res;
  2085. int ee;
  2086. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  2087. if (!gsi_ctx) {
  2088. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2089. return -GSI_STATUS_NODEV;
  2090. }
  2091. ctx = &gsi_ctx->chan[chan_hdl];
  2092. if (ctx->allocated) {
  2093. GSIERR("chan %d already allocated\n", chan_hdl);
  2094. return -GSI_STATUS_NODEV;
  2095. }
  2096. memset(ctx, 0, sizeof(*ctx));
  2097. mutex_init(&ctx->mlock);
  2098. init_completion(&ctx->compl);
  2099. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2100. mutex_lock(&gsi_ctx->mlock);
  2101. ee = gsi_ctx->per.ee;
  2102. gsi_ctx->ch_dbg[chan_hdl].ch_allocate++;
  2103. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2104. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2105. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2106. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2107. gsi_writel(val, gsi_ctx->base +
  2108. GSI_EE_n_GSI_CH_CMD_OFFS(ee));
  2109. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2110. if (res == 0) {
  2111. GSIERR("chan_hdl=%u timed out\n", chan_hdl);
  2112. mutex_unlock(&gsi_ctx->mlock);
  2113. return -GSI_STATUS_TIMED_OUT;
  2114. }
  2115. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2116. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2117. chan_hdl, ctx->state);
  2118. mutex_unlock(&gsi_ctx->mlock);
  2119. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2120. }
  2121. mutex_unlock(&gsi_ctx->mlock);
  2122. return GSI_STATUS_SUCCESS;
  2123. }
  2124. static void __gsi_write_channel_scratch(unsigned long chan_hdl,
  2125. union __packed gsi_channel_scratch val)
  2126. {
  2127. gsi_writel(val.data.word1, gsi_ctx->base +
  2128. GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
  2129. gsi_ctx->per.ee));
  2130. gsi_writel(val.data.word2, gsi_ctx->base +
  2131. GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
  2132. gsi_ctx->per.ee));
  2133. gsi_writel(val.data.word3, gsi_ctx->base +
  2134. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  2135. gsi_ctx->per.ee));
  2136. gsi_writel(val.data.word4, gsi_ctx->base +
  2137. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  2138. gsi_ctx->per.ee));
  2139. }
  2140. int gsi_write_channel_scratch3_reg(unsigned long chan_hdl,
  2141. union __packed gsi_wdi_channel_scratch3_reg val)
  2142. {
  2143. struct gsi_chan_ctx *ctx;
  2144. if (!gsi_ctx) {
  2145. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2146. return -GSI_STATUS_NODEV;
  2147. }
  2148. if (chan_hdl >= gsi_ctx->max_ch) {
  2149. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2150. return -GSI_STATUS_INVALID_PARAMS;
  2151. }
  2152. ctx = &gsi_ctx->chan[chan_hdl];
  2153. mutex_lock(&ctx->mlock);
  2154. ctx->scratch.wdi.endp_metadatareg_offset =
  2155. val.wdi.endp_metadatareg_offset;
  2156. ctx->scratch.wdi.qmap_id = val.wdi.qmap_id;
  2157. gsi_writel(val.data.word1, gsi_ctx->base +
  2158. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  2159. gsi_ctx->per.ee));
  2160. mutex_unlock(&ctx->mlock);
  2161. return GSI_STATUS_SUCCESS;
  2162. }
  2163. EXPORT_SYMBOL(gsi_write_channel_scratch3_reg);
  2164. int gsi_write_channel_scratch2_reg(unsigned long chan_hdl,
  2165. union __packed gsi_wdi2_channel_scratch2_reg val)
  2166. {
  2167. struct gsi_chan_ctx *ctx;
  2168. if (!gsi_ctx) {
  2169. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2170. return -GSI_STATUS_NODEV;
  2171. }
  2172. if (chan_hdl >= gsi_ctx->max_ch) {
  2173. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2174. return -GSI_STATUS_INVALID_PARAMS;
  2175. }
  2176. ctx = &gsi_ctx->chan[chan_hdl];
  2177. mutex_lock(&ctx->mlock);
  2178. ctx->scratch.wdi2_new.endp_metadatareg_offset =
  2179. val.wdi.endp_metadatareg_offset;
  2180. ctx->scratch.wdi2_new.qmap_id = val.wdi.qmap_id;
  2181. val.wdi.update_ri_moderation_threshold =
  2182. ctx->scratch.wdi2_new.update_ri_moderation_threshold;
  2183. gsi_writel(val.data.word1, gsi_ctx->base +
  2184. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  2185. gsi_ctx->per.ee));
  2186. mutex_unlock(&ctx->mlock);
  2187. return GSI_STATUS_SUCCESS;
  2188. }
  2189. EXPORT_SYMBOL(gsi_write_channel_scratch2_reg);
  2190. static void __gsi_read_channel_scratch(unsigned long chan_hdl,
  2191. union __packed gsi_channel_scratch * val)
  2192. {
  2193. val->data.word1 = gsi_readl(gsi_ctx->base +
  2194. GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
  2195. gsi_ctx->per.ee));
  2196. val->data.word2 = gsi_readl(gsi_ctx->base +
  2197. GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
  2198. gsi_ctx->per.ee));
  2199. val->data.word3 = gsi_readl(gsi_ctx->base +
  2200. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  2201. gsi_ctx->per.ee));
  2202. val->data.word4 = gsi_readl(gsi_ctx->base +
  2203. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  2204. gsi_ctx->per.ee));
  2205. }
  2206. static union __packed gsi_channel_scratch __gsi_update_mhi_channel_scratch(
  2207. unsigned long chan_hdl, struct __packed gsi_mhi_channel_scratch mscr)
  2208. {
  2209. union __packed gsi_channel_scratch scr;
  2210. /* below sequence is not atomic. assumption is sequencer specific fields
  2211. * will remain unchanged across this sequence
  2212. */
  2213. /* READ */
  2214. scr.data.word1 = gsi_readl(gsi_ctx->base +
  2215. GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
  2216. gsi_ctx->per.ee));
  2217. scr.data.word2 = gsi_readl(gsi_ctx->base +
  2218. GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
  2219. gsi_ctx->per.ee));
  2220. scr.data.word3 = gsi_readl(gsi_ctx->base +
  2221. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  2222. gsi_ctx->per.ee));
  2223. scr.data.word4 = gsi_readl(gsi_ctx->base +
  2224. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  2225. gsi_ctx->per.ee));
  2226. /* UPDATE */
  2227. scr.mhi.mhi_host_wp_addr = mscr.mhi_host_wp_addr;
  2228. scr.mhi.assert_bit40 = mscr.assert_bit40;
  2229. scr.mhi.polling_configuration = mscr.polling_configuration;
  2230. scr.mhi.burst_mode_enabled = mscr.burst_mode_enabled;
  2231. scr.mhi.polling_mode = mscr.polling_mode;
  2232. scr.mhi.oob_mod_threshold = mscr.oob_mod_threshold;
  2233. if (gsi_ctx->per.ver < GSI_VER_2_5) {
  2234. scr.mhi.max_outstanding_tre = mscr.max_outstanding_tre;
  2235. scr.mhi.outstanding_threshold = mscr.outstanding_threshold;
  2236. }
  2237. /* WRITE */
  2238. gsi_writel(scr.data.word1, gsi_ctx->base +
  2239. GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
  2240. gsi_ctx->per.ee));
  2241. gsi_writel(scr.data.word2, gsi_ctx->base +
  2242. GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
  2243. gsi_ctx->per.ee));
  2244. gsi_writel(scr.data.word3, gsi_ctx->base +
  2245. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  2246. gsi_ctx->per.ee));
  2247. gsi_writel(scr.data.word4, gsi_ctx->base +
  2248. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  2249. gsi_ctx->per.ee));
  2250. return scr;
  2251. }
  2252. int gsi_write_channel_scratch(unsigned long chan_hdl,
  2253. union __packed gsi_channel_scratch val)
  2254. {
  2255. struct gsi_chan_ctx *ctx;
  2256. if (!gsi_ctx) {
  2257. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2258. return -GSI_STATUS_NODEV;
  2259. }
  2260. if (chan_hdl >= gsi_ctx->max_ch) {
  2261. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2262. return -GSI_STATUS_INVALID_PARAMS;
  2263. }
  2264. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2265. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2266. GSIERR("bad state %d\n",
  2267. gsi_ctx->chan[chan_hdl].state);
  2268. return -GSI_STATUS_UNSUPPORTED_OP;
  2269. }
  2270. ctx = &gsi_ctx->chan[chan_hdl];
  2271. mutex_lock(&ctx->mlock);
  2272. ctx->scratch = val;
  2273. __gsi_write_channel_scratch(chan_hdl, val);
  2274. mutex_unlock(&ctx->mlock);
  2275. return GSI_STATUS_SUCCESS;
  2276. }
  2277. EXPORT_SYMBOL(gsi_write_channel_scratch);
  2278. int gsi_read_channel_scratch(unsigned long chan_hdl,
  2279. union __packed gsi_channel_scratch *val)
  2280. {
  2281. struct gsi_chan_ctx *ctx;
  2282. if (!gsi_ctx) {
  2283. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2284. return -GSI_STATUS_NODEV;
  2285. }
  2286. if (chan_hdl >= gsi_ctx->max_ch) {
  2287. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2288. return -GSI_STATUS_INVALID_PARAMS;
  2289. }
  2290. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2291. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2292. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2293. GSIERR("bad state %d\n",
  2294. gsi_ctx->chan[chan_hdl].state);
  2295. return -GSI_STATUS_UNSUPPORTED_OP;
  2296. }
  2297. ctx = &gsi_ctx->chan[chan_hdl];
  2298. mutex_lock(&ctx->mlock);
  2299. __gsi_read_channel_scratch(chan_hdl, val);
  2300. mutex_unlock(&ctx->mlock);
  2301. return GSI_STATUS_SUCCESS;
  2302. }
  2303. EXPORT_SYMBOL(gsi_read_channel_scratch);
  2304. int gsi_update_mhi_channel_scratch(unsigned long chan_hdl,
  2305. struct __packed gsi_mhi_channel_scratch mscr)
  2306. {
  2307. struct gsi_chan_ctx *ctx;
  2308. if (!gsi_ctx) {
  2309. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2310. return -GSI_STATUS_NODEV;
  2311. }
  2312. if (chan_hdl >= gsi_ctx->max_ch) {
  2313. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2314. return -GSI_STATUS_INVALID_PARAMS;
  2315. }
  2316. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2317. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2318. GSIERR("bad state %d\n",
  2319. gsi_ctx->chan[chan_hdl].state);
  2320. return -GSI_STATUS_UNSUPPORTED_OP;
  2321. }
  2322. ctx = &gsi_ctx->chan[chan_hdl];
  2323. mutex_lock(&ctx->mlock);
  2324. ctx->scratch = __gsi_update_mhi_channel_scratch(chan_hdl, mscr);
  2325. mutex_unlock(&ctx->mlock);
  2326. return GSI_STATUS_SUCCESS;
  2327. }
  2328. EXPORT_SYMBOL(gsi_update_mhi_channel_scratch);
  2329. int gsi_query_channel_db_addr(unsigned long chan_hdl,
  2330. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  2331. {
  2332. if (!gsi_ctx) {
  2333. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2334. return -GSI_STATUS_NODEV;
  2335. }
  2336. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  2337. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  2338. db_addr_wp_lsb);
  2339. return -GSI_STATUS_INVALID_PARAMS;
  2340. }
  2341. if (chan_hdl >= gsi_ctx->max_ch) {
  2342. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2343. return -GSI_STATUS_INVALID_PARAMS;
  2344. }
  2345. if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  2346. GSIERR("bad state %d\n",
  2347. gsi_ctx->chan[chan_hdl].state);
  2348. return -GSI_STATUS_UNSUPPORTED_OP;
  2349. }
  2350. *db_addr_wp_lsb = gsi_ctx->per.phys_addr +
  2351. GSI_EE_n_GSI_CH_k_DOORBELL_0_OFFS(chan_hdl, gsi_ctx->per.ee);
  2352. *db_addr_wp_msb = gsi_ctx->per.phys_addr +
  2353. GSI_EE_n_GSI_CH_k_DOORBELL_1_OFFS(chan_hdl, gsi_ctx->per.ee);
  2354. return GSI_STATUS_SUCCESS;
  2355. }
  2356. EXPORT_SYMBOL(gsi_query_channel_db_addr);
  2357. int gsi_start_channel(unsigned long chan_hdl)
  2358. {
  2359. enum gsi_ch_cmd_opcode op = GSI_CH_START;
  2360. uint32_t val;
  2361. struct gsi_chan_ctx *ctx;
  2362. if (!gsi_ctx) {
  2363. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2364. return -GSI_STATUS_NODEV;
  2365. }
  2366. if (chan_hdl >= gsi_ctx->max_ch) {
  2367. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2368. return -GSI_STATUS_INVALID_PARAMS;
  2369. }
  2370. ctx = &gsi_ctx->chan[chan_hdl];
  2371. if (ctx->state != GSI_CHAN_STATE_ALLOCATED &&
  2372. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  2373. ctx->state != GSI_CHAN_STATE_STOPPED) {
  2374. GSIERR("bad state %d\n", ctx->state);
  2375. return -GSI_STATUS_UNSUPPORTED_OP;
  2376. }
  2377. mutex_lock(&gsi_ctx->mlock);
  2378. reinit_completion(&ctx->compl);
  2379. /* check if INTSET is in IRQ mode for GPI channel */
  2380. val = gsi_readl(gsi_ctx->base +
  2381. GSI_EE_n_CNTXT_INTSET_OFFS(gsi_ctx->per.ee));
  2382. if (ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2383. val != GSI_INTR_IRQ) {
  2384. GSIERR("GSI_EE_n_CNTXT_INTSET_OFFS %d\n", val);
  2385. BUG();
  2386. }
  2387. gsi_ctx->ch_dbg[chan_hdl].ch_start++;
  2388. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2389. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2390. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2391. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2392. gsi_writel(val, gsi_ctx->base +
  2393. GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
  2394. GSIDBG("GSI Channel Start, waiting for completion\n");
  2395. gsi_channel_state_change_wait(chan_hdl,
  2396. ctx,
  2397. GSI_START_CMD_TIMEOUT_MS, op);
  2398. if (ctx->state != GSI_CHAN_STATE_STARTED) {
  2399. /*
  2400. * Hardware returned unexpected status, unexpected
  2401. * hardware state.
  2402. */
  2403. GSIERR("chan=%lu timed out, unexpected state=%u\n",
  2404. chan_hdl, ctx->state);
  2405. GSI_ASSERT();
  2406. }
  2407. GSIDBG("GSI Channel=%lu Start success\n", chan_hdl);
  2408. /* write order MUST be MSB followed by LSB */
  2409. val = ((ctx->ring.wp_local >> 32) &
  2410. GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_BMSK) <<
  2411. GSI_EE_n_GSI_CH_k_DOORBELL_1_WRITE_PTR_MSB_SHFT;
  2412. gsi_writel(val, gsi_ctx->base +
  2413. GSI_EE_n_GSI_CH_k_DOORBELL_1_OFFS(ctx->props.ch_id,
  2414. gsi_ctx->per.ee));
  2415. mutex_unlock(&gsi_ctx->mlock);
  2416. return GSI_STATUS_SUCCESS;
  2417. }
  2418. EXPORT_SYMBOL(gsi_start_channel);
  2419. int gsi_stop_channel(unsigned long chan_hdl)
  2420. {
  2421. enum gsi_ch_cmd_opcode op = GSI_CH_STOP;
  2422. int res;
  2423. uint32_t val;
  2424. struct gsi_chan_ctx *ctx;
  2425. if (!gsi_ctx) {
  2426. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2427. return -GSI_STATUS_NODEV;
  2428. }
  2429. if (chan_hdl >= gsi_ctx->max_ch) {
  2430. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2431. return -GSI_STATUS_INVALID_PARAMS;
  2432. }
  2433. ctx = &gsi_ctx->chan[chan_hdl];
  2434. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  2435. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  2436. return GSI_STATUS_SUCCESS;
  2437. }
  2438. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  2439. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  2440. ctx->state != GSI_CHAN_STATE_ERROR) {
  2441. GSIERR("bad state %d\n", ctx->state);
  2442. return -GSI_STATUS_UNSUPPORTED_OP;
  2443. }
  2444. mutex_lock(&gsi_ctx->mlock);
  2445. reinit_completion(&ctx->compl);
  2446. /* check if INTSET is in IRQ mode for GPI channel */
  2447. val = gsi_readl(gsi_ctx->base +
  2448. GSI_EE_n_CNTXT_INTSET_OFFS(gsi_ctx->per.ee));
  2449. if (ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2450. val != GSI_INTR_IRQ) {
  2451. GSIERR("GSI_EE_n_CNTXT_INTSET_OFFS %d\n", val);
  2452. BUG();
  2453. }
  2454. gsi_ctx->ch_dbg[chan_hdl].ch_stop++;
  2455. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2456. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2457. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2458. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2459. gsi_writel(val, gsi_ctx->base +
  2460. GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
  2461. GSIDBG("GSI Channel Stop, waiting for completion\n");
  2462. gsi_channel_state_change_wait(chan_hdl,
  2463. ctx,
  2464. GSI_STOP_CMD_TIMEOUT_MS, op);
  2465. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  2466. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  2467. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  2468. res = -GSI_STATUS_BAD_STATE;
  2469. BUG();
  2470. goto free_lock;
  2471. }
  2472. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  2473. GSIERR("chan=%lu busy try again\n", chan_hdl);
  2474. res = -GSI_STATUS_AGAIN;
  2475. goto free_lock;
  2476. }
  2477. res = GSI_STATUS_SUCCESS;
  2478. free_lock:
  2479. mutex_unlock(&gsi_ctx->mlock);
  2480. return res;
  2481. }
  2482. EXPORT_SYMBOL(gsi_stop_channel);
  2483. int gsi_stop_db_channel(unsigned long chan_hdl)
  2484. {
  2485. enum gsi_ch_cmd_opcode op = GSI_CH_DB_STOP;
  2486. int res;
  2487. uint32_t val;
  2488. struct gsi_chan_ctx *ctx;
  2489. if (!gsi_ctx) {
  2490. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2491. return -GSI_STATUS_NODEV;
  2492. }
  2493. if (chan_hdl >= gsi_ctx->max_ch) {
  2494. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2495. return -GSI_STATUS_INVALID_PARAMS;
  2496. }
  2497. ctx = &gsi_ctx->chan[chan_hdl];
  2498. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  2499. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  2500. return GSI_STATUS_SUCCESS;
  2501. }
  2502. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  2503. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  2504. GSIERR("bad state %d\n", ctx->state);
  2505. return -GSI_STATUS_UNSUPPORTED_OP;
  2506. }
  2507. mutex_lock(&gsi_ctx->mlock);
  2508. reinit_completion(&ctx->compl);
  2509. gsi_ctx->ch_dbg[chan_hdl].ch_db_stop++;
  2510. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2511. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2512. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2513. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2514. gsi_writel(val, gsi_ctx->base +
  2515. GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
  2516. res = wait_for_completion_timeout(&ctx->compl,
  2517. msecs_to_jiffies(GSI_STOP_CMD_TIMEOUT_MS));
  2518. if (res == 0) {
  2519. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  2520. res = -GSI_STATUS_TIMED_OUT;
  2521. goto free_lock;
  2522. }
  2523. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  2524. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  2525. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  2526. res = -GSI_STATUS_BAD_STATE;
  2527. goto free_lock;
  2528. }
  2529. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  2530. GSIERR("chan=%lu busy try again\n", chan_hdl);
  2531. res = -GSI_STATUS_AGAIN;
  2532. goto free_lock;
  2533. }
  2534. res = GSI_STATUS_SUCCESS;
  2535. free_lock:
  2536. mutex_unlock(&gsi_ctx->mlock);
  2537. return res;
  2538. }
  2539. EXPORT_SYMBOL(gsi_stop_db_channel);
  2540. int gsi_reset_channel(unsigned long chan_hdl)
  2541. {
  2542. enum gsi_ch_cmd_opcode op = GSI_CH_RESET;
  2543. int res;
  2544. uint32_t val;
  2545. struct gsi_chan_ctx *ctx;
  2546. bool reset_done = false;
  2547. uint32_t retry_cnt = 0;
  2548. if (!gsi_ctx) {
  2549. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2550. return -GSI_STATUS_NODEV;
  2551. }
  2552. if (chan_hdl >= gsi_ctx->max_ch) {
  2553. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2554. return -GSI_STATUS_INVALID_PARAMS;
  2555. }
  2556. ctx = &gsi_ctx->chan[chan_hdl];
  2557. /*
  2558. * In WDI3 case, if SAP enabled but no client connected,
  2559. * GSI will be in allocated state. When SAP disabled,
  2560. * gsi_reset_channel will be called and reset is needed.
  2561. */
  2562. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  2563. ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2564. GSIERR("bad state %d\n", ctx->state);
  2565. return -GSI_STATUS_UNSUPPORTED_OP;
  2566. }
  2567. mutex_lock(&gsi_ctx->mlock);
  2568. reset:
  2569. reinit_completion(&ctx->compl);
  2570. gsi_ctx->ch_dbg[chan_hdl].ch_reset++;
  2571. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2572. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2573. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2574. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2575. gsi_writel(val, gsi_ctx->base +
  2576. GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
  2577. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2578. if (res == 0) {
  2579. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  2580. mutex_unlock(&gsi_ctx->mlock);
  2581. return -GSI_STATUS_TIMED_OUT;
  2582. }
  2583. revrfy_chnlstate:
  2584. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2585. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  2586. ctx->state);
  2587. /* GSI register update state not sync with gsi channel
  2588. * context state not sync, need to wait for 1ms to sync.
  2589. */
  2590. retry_cnt++;
  2591. if (retry_cnt <= GSI_CHNL_STATE_MAX_RETRYCNT) {
  2592. usleep_range(GSI_RESET_WA_MIN_SLEEP,
  2593. GSI_RESET_WA_MAX_SLEEP);
  2594. goto revrfy_chnlstate;
  2595. }
  2596. /*
  2597. * Hardware returned incorrect state, unexpected
  2598. * hardware state.
  2599. */
  2600. GSI_ASSERT();
  2601. }
  2602. /* Hardware issue fixed from GSI 2.0 and no need for the WA */
  2603. if (gsi_ctx->per.ver >= GSI_VER_2_0)
  2604. reset_done = true;
  2605. /* workaround: reset GSI producers again */
  2606. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && !reset_done) {
  2607. usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
  2608. reset_done = true;
  2609. goto reset;
  2610. }
  2611. if (ctx->props.cleanup_cb)
  2612. gsi_cleanup_xfer_user_data(chan_hdl, ctx->props.cleanup_cb);
  2613. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  2614. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  2615. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  2616. /* restore scratch */
  2617. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  2618. mutex_unlock(&gsi_ctx->mlock);
  2619. return GSI_STATUS_SUCCESS;
  2620. }
  2621. EXPORT_SYMBOL(gsi_reset_channel);
  2622. int gsi_dealloc_channel(unsigned long chan_hdl)
  2623. {
  2624. enum gsi_ch_cmd_opcode op = GSI_CH_DE_ALLOC;
  2625. int res;
  2626. uint32_t val;
  2627. struct gsi_chan_ctx *ctx;
  2628. if (!gsi_ctx) {
  2629. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2630. return -GSI_STATUS_NODEV;
  2631. }
  2632. if (chan_hdl >= gsi_ctx->max_ch) {
  2633. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2634. return -GSI_STATUS_INVALID_PARAMS;
  2635. }
  2636. ctx = &gsi_ctx->chan[chan_hdl];
  2637. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2638. GSIERR("bad state %d\n", ctx->state);
  2639. return -GSI_STATUS_UNSUPPORTED_OP;
  2640. }
  2641. /*In GSI_VER_2_2 version deallocation channel not supported*/
  2642. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  2643. mutex_lock(&gsi_ctx->mlock);
  2644. reinit_completion(&ctx->compl);
  2645. gsi_ctx->ch_dbg[chan_hdl].ch_de_alloc++;
  2646. val = (((chan_hdl << GSI_EE_n_GSI_CH_CMD_CHID_SHFT) &
  2647. GSI_EE_n_GSI_CH_CMD_CHID_BMSK) |
  2648. ((op << GSI_EE_n_GSI_CH_CMD_OPCODE_SHFT) &
  2649. GSI_EE_n_GSI_CH_CMD_OPCODE_BMSK));
  2650. gsi_writel(val, gsi_ctx->base +
  2651. GSI_EE_n_GSI_CH_CMD_OFFS(gsi_ctx->per.ee));
  2652. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2653. if (res == 0) {
  2654. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  2655. mutex_unlock(&gsi_ctx->mlock);
  2656. return -GSI_STATUS_TIMED_OUT;
  2657. }
  2658. if (ctx->state != GSI_CHAN_STATE_NOT_ALLOCATED) {
  2659. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  2660. ctx->state);
  2661. /* Hardware returned incorrect value */
  2662. GSI_ASSERT();
  2663. }
  2664. mutex_unlock(&gsi_ctx->mlock);
  2665. } else {
  2666. mutex_lock(&gsi_ctx->mlock);
  2667. GSIDBG("In GSI_VER_2_2 channel deallocation not supported\n");
  2668. ctx->state = GSI_CHAN_STATE_NOT_ALLOCATED;
  2669. GSIDBG("chan_hdl=%lu Channel state = %u\n", chan_hdl,
  2670. ctx->state);
  2671. mutex_unlock(&gsi_ctx->mlock);
  2672. }
  2673. devm_kfree(gsi_ctx->dev, ctx->user_data);
  2674. ctx->allocated = false;
  2675. if (ctx->evtr)
  2676. atomic_dec(&ctx->evtr->chan_ref_cnt);
  2677. atomic_dec(&gsi_ctx->num_chan);
  2678. return GSI_STATUS_SUCCESS;
  2679. }
  2680. EXPORT_SYMBOL(gsi_dealloc_channel);
  2681. void gsi_update_ch_dp_stats(struct gsi_chan_ctx *ctx, uint16_t used)
  2682. {
  2683. unsigned long now = jiffies_to_msecs(jiffies);
  2684. unsigned long elapsed;
  2685. if (used == 0) {
  2686. elapsed = now - ctx->stats.dp.last_timestamp;
  2687. if (ctx->stats.dp.empty_time < elapsed)
  2688. ctx->stats.dp.empty_time = elapsed;
  2689. }
  2690. if (used <= ctx->props.max_re_expected / 3)
  2691. ++ctx->stats.dp.ch_below_lo;
  2692. else if (used <= 2 * ctx->props.max_re_expected / 3)
  2693. ++ctx->stats.dp.ch_below_hi;
  2694. else
  2695. ++ctx->stats.dp.ch_above_hi;
  2696. ctx->stats.dp.last_timestamp = now;
  2697. }
  2698. static void __gsi_query_channel_free_re(struct gsi_chan_ctx *ctx,
  2699. uint16_t *num_free_re)
  2700. {
  2701. uint16_t start;
  2702. uint16_t end;
  2703. uint64_t rp;
  2704. int ee = gsi_ctx->per.ee;
  2705. uint16_t used;
  2706. WARN_ON(ctx->props.prot != GSI_CHAN_PROT_GPI);
  2707. if (!ctx->evtr) {
  2708. rp = gsi_readl(gsi_ctx->base +
  2709. GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
  2710. rp |= ctx->ring.rp & 0xFFFFFFFF00000000;
  2711. ctx->ring.rp = rp;
  2712. } else {
  2713. rp = ctx->ring.rp_local;
  2714. }
  2715. start = gsi_find_idx_from_addr(&ctx->ring, rp);
  2716. end = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  2717. if (end >= start)
  2718. used = end - start;
  2719. else
  2720. used = ctx->ring.max_num_elem + 1 - (start - end);
  2721. *num_free_re = ctx->ring.max_num_elem - used;
  2722. }
  2723. int gsi_query_channel_info(unsigned long chan_hdl,
  2724. struct gsi_chan_info *info)
  2725. {
  2726. struct gsi_chan_ctx *ctx;
  2727. spinlock_t *slock;
  2728. unsigned long flags;
  2729. uint64_t rp;
  2730. uint64_t wp;
  2731. int ee;
  2732. if (!gsi_ctx) {
  2733. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2734. return -GSI_STATUS_NODEV;
  2735. }
  2736. if (chan_hdl >= gsi_ctx->max_ch || !info) {
  2737. GSIERR("bad params chan_hdl=%lu info=%pK\n", chan_hdl, info);
  2738. return -GSI_STATUS_INVALID_PARAMS;
  2739. }
  2740. ctx = &gsi_ctx->chan[chan_hdl];
  2741. if (ctx->evtr) {
  2742. slock = &ctx->evtr->ring.slock;
  2743. info->evt_valid = true;
  2744. } else {
  2745. slock = &ctx->ring.slock;
  2746. info->evt_valid = false;
  2747. }
  2748. spin_lock_irqsave(slock, flags);
  2749. ee = gsi_ctx->per.ee;
  2750. rp = gsi_readl(gsi_ctx->base +
  2751. GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
  2752. rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
  2753. GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS(ctx->props.ch_id, ee))) << 32;
  2754. ctx->ring.rp = rp;
  2755. info->rp = rp;
  2756. wp = gsi_readl(gsi_ctx->base +
  2757. GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS(ctx->props.ch_id, ee));
  2758. wp |= ((uint64_t)gsi_readl(gsi_ctx->base +
  2759. GSI_EE_n_GSI_CH_k_CNTXT_7_OFFS(ctx->props.ch_id, ee))) << 32;
  2760. ctx->ring.wp = wp;
  2761. info->wp = wp;
  2762. if (info->evt_valid) {
  2763. rp = gsi_readl(gsi_ctx->base +
  2764. GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(ctx->evtr->id, ee));
  2765. rp |= ((uint64_t)gsi_readl(gsi_ctx->base +
  2766. GSI_EE_n_EV_CH_k_CNTXT_5_OFFS(ctx->evtr->id, ee)))
  2767. << 32;
  2768. info->evt_rp = rp;
  2769. wp = gsi_readl(gsi_ctx->base +
  2770. GSI_EE_n_EV_CH_k_CNTXT_6_OFFS(ctx->evtr->id, ee));
  2771. wp |= ((uint64_t)gsi_readl(gsi_ctx->base +
  2772. GSI_EE_n_EV_CH_k_CNTXT_7_OFFS(ctx->evtr->id, ee)))
  2773. << 32;
  2774. info->evt_wp = wp;
  2775. }
  2776. spin_unlock_irqrestore(slock, flags);
  2777. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx ev_valid=%d ERP=0x%llx EWP=0x%llx\n",
  2778. chan_hdl, info->rp, info->wp,
  2779. info->evt_valid, info->evt_rp, info->evt_wp);
  2780. return GSI_STATUS_SUCCESS;
  2781. }
  2782. EXPORT_SYMBOL(gsi_query_channel_info);
  2783. int gsi_is_channel_empty(unsigned long chan_hdl, bool *is_empty)
  2784. {
  2785. struct gsi_chan_ctx *ctx;
  2786. spinlock_t *slock;
  2787. unsigned long flags;
  2788. uint64_t rp;
  2789. uint64_t wp;
  2790. uint64_t rp_local;
  2791. int ee;
  2792. if (!gsi_ctx) {
  2793. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2794. return -GSI_STATUS_NODEV;
  2795. }
  2796. if (chan_hdl >= gsi_ctx->max_ch || !is_empty) {
  2797. GSIERR("bad params chan_hdl=%lu is_empty=%pK\n",
  2798. chan_hdl, is_empty);
  2799. return -GSI_STATUS_INVALID_PARAMS;
  2800. }
  2801. ctx = &gsi_ctx->chan[chan_hdl];
  2802. ee = gsi_ctx->per.ee;
  2803. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  2804. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  2805. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  2806. return -GSI_STATUS_UNSUPPORTED_OP;
  2807. }
  2808. if (ctx->evtr)
  2809. slock = &ctx->evtr->ring.slock;
  2810. else
  2811. slock = &ctx->ring.slock;
  2812. spin_lock_irqsave(slock, flags);
  2813. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr) {
  2814. rp = gsi_readl(gsi_ctx->base +
  2815. GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(ctx->evtr->id, ee));
  2816. rp |= ctx->evtr->ring.rp & 0xFFFFFFFF00000000;
  2817. ctx->evtr->ring.rp = rp;
  2818. wp = gsi_readl(gsi_ctx->base +
  2819. GSI_EE_n_EV_CH_k_CNTXT_6_OFFS(ctx->evtr->id, ee));
  2820. wp |= ctx->evtr->ring.wp & 0xFFFFFFFF00000000;
  2821. ctx->evtr->ring.wp = wp;
  2822. rp_local = ctx->evtr->ring.rp_local;
  2823. } else {
  2824. rp = gsi_readl(gsi_ctx->base +
  2825. GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(ctx->props.ch_id, ee));
  2826. rp |= ctx->ring.rp & 0xFFFFFFFF00000000;
  2827. ctx->ring.rp = rp;
  2828. wp = gsi_readl(gsi_ctx->base +
  2829. GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS(ctx->props.ch_id, ee));
  2830. wp |= ctx->ring.wp & 0xFFFFFFFF00000000;
  2831. ctx->ring.wp = wp;
  2832. rp_local = ctx->ring.rp_local;
  2833. }
  2834. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
  2835. *is_empty = (rp_local == rp) ? true : false;
  2836. else
  2837. *is_empty = (wp == rp) ? true : false;
  2838. spin_unlock_irqrestore(slock, flags);
  2839. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr)
  2840. GSIDBG("ch=%ld ev=%d RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  2841. chan_hdl, ctx->evtr->id, rp, wp, rp_local);
  2842. else
  2843. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  2844. chan_hdl, rp, wp, rp_local);
  2845. return GSI_STATUS_SUCCESS;
  2846. }
  2847. EXPORT_SYMBOL(gsi_is_channel_empty);
  2848. int __gsi_get_gci_cookie(struct gsi_chan_ctx *ctx, uint16_t idx)
  2849. {
  2850. int i;
  2851. int end;
  2852. if (!ctx->user_data[idx].valid) {
  2853. ctx->user_data[idx].valid = true;
  2854. return idx;
  2855. }
  2856. /*
  2857. * at this point we need to find an "escape buffer" for the cookie
  2858. * as the userdata in this spot is in use. This happens if the TRE at
  2859. * idx is not completed yet and it is getting reused by a new TRE.
  2860. */
  2861. ctx->stats.userdata_in_use++;
  2862. end = ctx->ring.max_num_elem + 1;
  2863. for (i = 0; i < GSI_VEID_MAX; i++) {
  2864. if (!ctx->user_data[end + i].valid) {
  2865. ctx->user_data[end + i].valid = true;
  2866. return end + i;
  2867. }
  2868. }
  2869. /* TODO: Increase escape buffer size if we hit this */
  2870. GSIERR("user_data is full\n");
  2871. return 0xFFFF;
  2872. }
  2873. int __gsi_populate_gci_tre(struct gsi_chan_ctx *ctx,
  2874. struct gsi_xfer_elem *xfer)
  2875. {
  2876. struct gsi_gci_tre gci_tre;
  2877. struct gsi_gci_tre *tre_gci_ptr;
  2878. uint16_t idx;
  2879. memset(&gci_tre, 0, sizeof(gci_tre));
  2880. if (xfer->addr & 0xFFFFFF0000000000) {
  2881. GSIERR("chan_hdl=%u add too large=%llx\n",
  2882. ctx->props.ch_id, xfer->addr);
  2883. return -EINVAL;
  2884. }
  2885. if (xfer->type != GSI_XFER_ELEM_DATA) {
  2886. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  2887. xfer->type);
  2888. return -EINVAL;
  2889. }
  2890. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  2891. tre_gci_ptr = (struct gsi_gci_tre *)(ctx->ring.base_va +
  2892. idx * ctx->ring.elem_sz);
  2893. gci_tre.buffer_ptr = xfer->addr;
  2894. gci_tre.buf_len = xfer->len;
  2895. gci_tre.re_type = GSI_RE_COAL;
  2896. gci_tre.cookie = __gsi_get_gci_cookie(ctx, idx);
  2897. if (gci_tre.cookie > (ctx->ring.max_num_elem + GSI_VEID_MAX))
  2898. return -EPERM;
  2899. /* write the TRE to ring */
  2900. *tre_gci_ptr = gci_tre;
  2901. ctx->user_data[idx].p = xfer->xfer_user_data;
  2902. return 0;
  2903. }
  2904. int __gsi_populate_tre(struct gsi_chan_ctx *ctx,
  2905. struct gsi_xfer_elem *xfer)
  2906. {
  2907. struct gsi_tre tre;
  2908. struct gsi_tre *tre_ptr;
  2909. uint16_t idx;
  2910. memset(&tre, 0, sizeof(tre));
  2911. tre.buffer_ptr = xfer->addr;
  2912. tre.buf_len = xfer->len;
  2913. if (xfer->type == GSI_XFER_ELEM_DATA) {
  2914. tre.re_type = GSI_RE_XFER;
  2915. } else if (xfer->type == GSI_XFER_ELEM_IMME_CMD) {
  2916. tre.re_type = GSI_RE_IMMD_CMD;
  2917. } else if (xfer->type == GSI_XFER_ELEM_NOP) {
  2918. tre.re_type = GSI_RE_NOP;
  2919. } else {
  2920. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  2921. xfer->type);
  2922. return -EINVAL;
  2923. }
  2924. tre.bei = (xfer->flags & GSI_XFER_FLAG_BEI) ? 1 : 0;
  2925. tre.ieot = (xfer->flags & GSI_XFER_FLAG_EOT) ? 1 : 0;
  2926. tre.ieob = (xfer->flags & GSI_XFER_FLAG_EOB) ? 1 : 0;
  2927. tre.chain = (xfer->flags & GSI_XFER_FLAG_CHAIN) ? 1 : 0;
  2928. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  2929. tre_ptr = (struct gsi_tre *)(ctx->ring.base_va +
  2930. idx * ctx->ring.elem_sz);
  2931. /* write the TRE to ring */
  2932. *tre_ptr = tre;
  2933. ctx->user_data[idx].valid = true;
  2934. ctx->user_data[idx].p = xfer->xfer_user_data;
  2935. return 0;
  2936. }
  2937. int gsi_queue_xfer(unsigned long chan_hdl, uint16_t num_xfers,
  2938. struct gsi_xfer_elem *xfer, bool ring_db)
  2939. {
  2940. struct gsi_chan_ctx *ctx;
  2941. uint16_t free;
  2942. uint64_t wp_rollback;
  2943. int i;
  2944. spinlock_t *slock;
  2945. unsigned long flags;
  2946. if (!gsi_ctx) {
  2947. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2948. return -GSI_STATUS_NODEV;
  2949. }
  2950. if (chan_hdl >= gsi_ctx->max_ch || (num_xfers && !xfer)) {
  2951. GSIERR("bad params chan_hdl=%lu num_xfers=%u xfer=%pK\n",
  2952. chan_hdl, num_xfers, xfer);
  2953. return -GSI_STATUS_INVALID_PARAMS;
  2954. }
  2955. ctx = &gsi_ctx->chan[chan_hdl];
  2956. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  2957. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  2958. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  2959. return -GSI_STATUS_UNSUPPORTED_OP;
  2960. }
  2961. if (ctx->evtr)
  2962. slock = &ctx->evtr->ring.slock;
  2963. else
  2964. slock = &ctx->ring.slock;
  2965. spin_lock_irqsave(slock, flags);
  2966. /* allow only ring doorbell */
  2967. if (!num_xfers)
  2968. goto ring_doorbell;
  2969. /*
  2970. * for GCI channels the responsibility is on the caller to make sure
  2971. * there is enough room in the TRE.
  2972. */
  2973. if (ctx->props.prot != GSI_CHAN_PROT_GCI) {
  2974. __gsi_query_channel_free_re(ctx, &free);
  2975. if (num_xfers > free) {
  2976. GSIERR("chan_hdl=%lu num_xfers=%u free=%u\n",
  2977. chan_hdl, num_xfers, free);
  2978. spin_unlock_irqrestore(slock, flags);
  2979. return -GSI_STATUS_RING_INSUFFICIENT_SPACE;
  2980. }
  2981. }
  2982. wp_rollback = ctx->ring.wp_local;
  2983. for (i = 0; i < num_xfers; i++) {
  2984. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  2985. if (__gsi_populate_gci_tre(ctx, &xfer[i]))
  2986. break;
  2987. } else {
  2988. if (__gsi_populate_tre(ctx, &xfer[i]))
  2989. break;
  2990. }
  2991. gsi_incr_ring_wp(&ctx->ring);
  2992. }
  2993. if (i != num_xfers) {
  2994. /* reject all the xfers */
  2995. ctx->ring.wp_local = wp_rollback;
  2996. spin_unlock_irqrestore(slock, flags);
  2997. return -GSI_STATUS_INVALID_PARAMS;
  2998. }
  2999. ctx->stats.queued += num_xfers;
  3000. ring_doorbell:
  3001. if (ring_db) {
  3002. /* ensure TRE is set before ringing doorbell */
  3003. wmb();
  3004. gsi_ring_chan_doorbell(ctx);
  3005. }
  3006. spin_unlock_irqrestore(slock, flags);
  3007. return GSI_STATUS_SUCCESS;
  3008. }
  3009. EXPORT_SYMBOL(gsi_queue_xfer);
  3010. int gsi_start_xfer(unsigned long chan_hdl)
  3011. {
  3012. struct gsi_chan_ctx *ctx;
  3013. if (!gsi_ctx) {
  3014. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3015. return -GSI_STATUS_NODEV;
  3016. }
  3017. if (chan_hdl >= gsi_ctx->max_ch) {
  3018. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3019. return -GSI_STATUS_INVALID_PARAMS;
  3020. }
  3021. ctx = &gsi_ctx->chan[chan_hdl];
  3022. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3023. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3024. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3025. return -GSI_STATUS_UNSUPPORTED_OP;
  3026. }
  3027. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3028. GSIERR("bad state %d\n", ctx->state);
  3029. return -GSI_STATUS_UNSUPPORTED_OP;
  3030. }
  3031. if (ctx->ring.wp == ctx->ring.wp_local)
  3032. return GSI_STATUS_SUCCESS;
  3033. gsi_ring_chan_doorbell(ctx);
  3034. return GSI_STATUS_SUCCESS;
  3035. };
  3036. EXPORT_SYMBOL(gsi_start_xfer);
  3037. int gsi_poll_channel(unsigned long chan_hdl,
  3038. struct gsi_chan_xfer_notify *notify)
  3039. {
  3040. int unused_var;
  3041. return gsi_poll_n_channel(chan_hdl, notify, 1, &unused_var);
  3042. }
  3043. EXPORT_SYMBOL(gsi_poll_channel);
  3044. int gsi_poll_n_channel(unsigned long chan_hdl,
  3045. struct gsi_chan_xfer_notify *notify,
  3046. int expected_num, int *actual_num)
  3047. {
  3048. struct gsi_chan_ctx *ctx;
  3049. uint64_t rp;
  3050. int ee;
  3051. int i;
  3052. unsigned long flags;
  3053. if (!gsi_ctx) {
  3054. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3055. return -GSI_STATUS_NODEV;
  3056. }
  3057. if (chan_hdl >= gsi_ctx->max_ch || !notify ||
  3058. !actual_num || expected_num <= 0) {
  3059. GSIERR("bad params chan_hdl=%lu notify=%pK\n",
  3060. chan_hdl, notify);
  3061. GSIERR("actual_num=%pK expected_num=%d\n",
  3062. actual_num, expected_num);
  3063. return -GSI_STATUS_INVALID_PARAMS;
  3064. }
  3065. ctx = &gsi_ctx->chan[chan_hdl];
  3066. ee = gsi_ctx->per.ee;
  3067. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3068. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3069. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3070. return -GSI_STATUS_UNSUPPORTED_OP;
  3071. }
  3072. if (!ctx->evtr) {
  3073. GSIERR("no event ring associated chan_hdl=%lu\n", chan_hdl);
  3074. return -GSI_STATUS_UNSUPPORTED_OP;
  3075. }
  3076. spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
  3077. if (ctx->evtr->ring.rp == ctx->evtr->ring.rp_local) {
  3078. /* update rp to see of we have anything new to process */
  3079. rp = gsi_readl(gsi_ctx->base +
  3080. GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(ctx->evtr->id, ee));
  3081. rp |= ctx->ring.rp & 0xFFFFFFFF00000000ULL;
  3082. ctx->evtr->ring.rp = rp;
  3083. /* read gsi event ring rp again if last read is empty */
  3084. if (rp == ctx->evtr->ring.rp_local) {
  3085. /* event ring is empty */
  3086. gsi_writel(1 << ctx->evtr->id, gsi_ctx->base +
  3087. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(ee));
  3088. /* do another read to close a small window */
  3089. __iowmb();
  3090. rp = gsi_readl(gsi_ctx->base +
  3091. GSI_EE_n_EV_CH_k_CNTXT_4_OFFS(
  3092. ctx->evtr->id, ee));
  3093. rp |= ctx->ring.rp & 0xFFFFFFFF00000000ULL;
  3094. ctx->evtr->ring.rp = rp;
  3095. if (rp == ctx->evtr->ring.rp_local) {
  3096. spin_unlock_irqrestore(
  3097. &ctx->evtr->ring.slock,
  3098. flags);
  3099. ctx->stats.poll_empty++;
  3100. return GSI_STATUS_POLL_EMPTY;
  3101. }
  3102. }
  3103. }
  3104. *actual_num = gsi_get_complete_num(&ctx->evtr->ring,
  3105. ctx->evtr->ring.rp_local, ctx->evtr->ring.rp);
  3106. if (*actual_num > expected_num)
  3107. *actual_num = expected_num;
  3108. for (i = 0; i < *actual_num; i++)
  3109. gsi_process_evt_re(ctx->evtr, notify + i, false);
  3110. spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
  3111. ctx->stats.poll_ok++;
  3112. return GSI_STATUS_SUCCESS;
  3113. }
  3114. EXPORT_SYMBOL(gsi_poll_n_channel);
  3115. int gsi_config_channel_mode(unsigned long chan_hdl, enum gsi_chan_mode mode)
  3116. {
  3117. struct gsi_chan_ctx *ctx;
  3118. enum gsi_chan_mode curr;
  3119. unsigned long flags;
  3120. enum gsi_chan_mode chan_mode;
  3121. if (!gsi_ctx) {
  3122. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3123. return -GSI_STATUS_NODEV;
  3124. }
  3125. if (chan_hdl >= gsi_ctx->max_ch) {
  3126. GSIERR("bad params chan_hdl=%lu mode=%u\n", chan_hdl, mode);
  3127. return -GSI_STATUS_INVALID_PARAMS;
  3128. }
  3129. ctx = &gsi_ctx->chan[chan_hdl];
  3130. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3131. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3132. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3133. return -GSI_STATUS_UNSUPPORTED_OP;
  3134. }
  3135. if (!ctx->evtr || !ctx->evtr->props.exclusive) {
  3136. GSIERR("cannot configure mode on chan_hdl=%lu\n",
  3137. chan_hdl);
  3138. return -GSI_STATUS_UNSUPPORTED_OP;
  3139. }
  3140. if (atomic_read(&ctx->poll_mode))
  3141. curr = GSI_CHAN_MODE_POLL;
  3142. else
  3143. curr = GSI_CHAN_MODE_CALLBACK;
  3144. if (mode == curr) {
  3145. GSIERR("already in requested mode %u chan_hdl=%lu\n",
  3146. curr, chan_hdl);
  3147. return -GSI_STATUS_UNSUPPORTED_OP;
  3148. }
  3149. spin_lock_irqsave(&gsi_ctx->slock, flags);
  3150. if (curr == GSI_CHAN_MODE_CALLBACK &&
  3151. mode == GSI_CHAN_MODE_POLL) {
  3152. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, 0);
  3153. gsi_writel(1 << ctx->evtr->id, gsi_ctx->base +
  3154. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(gsi_ctx->per.ee));
  3155. atomic_set(&ctx->poll_mode, mode);
  3156. if (ctx->props.prot == GSI_CHAN_PROT_GCI)
  3157. atomic_set(&ctx->evtr->chan->poll_mode, mode);
  3158. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3159. ctx->evtr->id, mode);
  3160. ctx->stats.callback_to_poll++;
  3161. }
  3162. if (curr == GSI_CHAN_MODE_POLL &&
  3163. mode == GSI_CHAN_MODE_CALLBACK) {
  3164. atomic_set(&ctx->poll_mode, mode);
  3165. if (ctx->props.prot == GSI_CHAN_PROT_GCI)
  3166. atomic_set(&ctx->evtr->chan->poll_mode, mode);
  3167. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, ~0);
  3168. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3169. ctx->evtr->id, mode);
  3170. /*
  3171. * In GSI 2.2 and 2.5 there is a limitation that can lead
  3172. * to losing an interrupt. For these versions an
  3173. * explicit check is needed after enabling the interrupt
  3174. */
  3175. if (gsi_ctx->per.ver == GSI_VER_2_2 ||
  3176. gsi_ctx->per.ver == GSI_VER_2_5) {
  3177. u32 src = gsi_readl(gsi_ctx->base +
  3178. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_OFFS(
  3179. gsi_ctx->per.ee));
  3180. if (src & (1 << ctx->evtr->id)) {
  3181. __gsi_config_ieob_irq(
  3182. gsi_ctx->per.ee, 1 << ctx->evtr->id, 0);
  3183. gsi_writel(1 << ctx->evtr->id, gsi_ctx->base +
  3184. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_OFFS(
  3185. gsi_ctx->per.ee));
  3186. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3187. spin_lock_irqsave(&ctx->evtr->ring.slock,
  3188. flags);
  3189. chan_mode = atomic_xchg(&ctx->poll_mode,
  3190. GSI_CHAN_MODE_POLL);
  3191. spin_unlock_irqrestore(
  3192. &ctx->evtr->ring.slock, flags);
  3193. ctx->stats.poll_pending_irq++;
  3194. GSIDBG("In IEOB WA pnd cnt = %ld prvmode = %d\n",
  3195. ctx->stats.poll_pending_irq,
  3196. chan_mode);
  3197. if (chan_mode == GSI_CHAN_MODE_POLL)
  3198. return GSI_STATUS_SUCCESS;
  3199. else
  3200. return -GSI_STATUS_PENDING_IRQ;
  3201. }
  3202. }
  3203. ctx->stats.poll_to_callback++;
  3204. }
  3205. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3206. return GSI_STATUS_SUCCESS;
  3207. }
  3208. EXPORT_SYMBOL(gsi_config_channel_mode);
  3209. int gsi_get_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3210. union gsi_channel_scratch *scr)
  3211. {
  3212. struct gsi_chan_ctx *ctx;
  3213. if (!gsi_ctx) {
  3214. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3215. return -GSI_STATUS_NODEV;
  3216. }
  3217. if (!props || !scr) {
  3218. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  3219. return -GSI_STATUS_INVALID_PARAMS;
  3220. }
  3221. if (chan_hdl >= gsi_ctx->max_ch) {
  3222. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3223. return -GSI_STATUS_INVALID_PARAMS;
  3224. }
  3225. ctx = &gsi_ctx->chan[chan_hdl];
  3226. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3227. GSIERR("bad state %d\n", ctx->state);
  3228. return -GSI_STATUS_UNSUPPORTED_OP;
  3229. }
  3230. mutex_lock(&ctx->mlock);
  3231. *props = ctx->props;
  3232. *scr = ctx->scratch;
  3233. mutex_unlock(&ctx->mlock);
  3234. return GSI_STATUS_SUCCESS;
  3235. }
  3236. EXPORT_SYMBOL(gsi_get_channel_cfg);
  3237. int gsi_set_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3238. union gsi_channel_scratch *scr)
  3239. {
  3240. struct gsi_chan_ctx *ctx;
  3241. if (!gsi_ctx) {
  3242. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3243. return -GSI_STATUS_NODEV;
  3244. }
  3245. if (!props || gsi_validate_channel_props(props)) {
  3246. GSIERR("bad params props=%pK\n", props);
  3247. return -GSI_STATUS_INVALID_PARAMS;
  3248. }
  3249. if (chan_hdl >= gsi_ctx->max_ch) {
  3250. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3251. return -GSI_STATUS_INVALID_PARAMS;
  3252. }
  3253. ctx = &gsi_ctx->chan[chan_hdl];
  3254. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3255. GSIERR("bad state %d\n", ctx->state);
  3256. return -GSI_STATUS_UNSUPPORTED_OP;
  3257. }
  3258. if (ctx->props.ch_id != props->ch_id ||
  3259. ctx->props.evt_ring_hdl != props->evt_ring_hdl) {
  3260. GSIERR("changing immutable fields not supported\n");
  3261. return -GSI_STATUS_UNSUPPORTED_OP;
  3262. }
  3263. mutex_lock(&ctx->mlock);
  3264. ctx->props = *props;
  3265. if (scr)
  3266. ctx->scratch = *scr;
  3267. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  3268. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  3269. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  3270. /* restore scratch */
  3271. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  3272. mutex_unlock(&ctx->mlock);
  3273. return GSI_STATUS_SUCCESS;
  3274. }
  3275. EXPORT_SYMBOL(gsi_set_channel_cfg);
  3276. static void gsi_configure_ieps(void *base, enum gsi_ver ver)
  3277. {
  3278. void __iomem *gsi_base = base;
  3279. gsi_writel(1, gsi_base + GSI_GSI_IRAM_PTR_CH_CMD_OFFS);
  3280. gsi_writel(2, gsi_base + GSI_GSI_IRAM_PTR_CH_DB_OFFS);
  3281. gsi_writel(3, gsi_base + GSI_GSI_IRAM_PTR_CH_DIS_COMP_OFFS);
  3282. gsi_writel(4, gsi_base + GSI_GSI_IRAM_PTR_CH_EMPTY_OFFS);
  3283. gsi_writel(5, gsi_base + GSI_GSI_IRAM_PTR_EE_GENERIC_CMD_OFFS);
  3284. gsi_writel(6, gsi_base + GSI_GSI_IRAM_PTR_EVENT_GEN_COMP_OFFS);
  3285. gsi_writel(7, gsi_base + GSI_GSI_IRAM_PTR_INT_MOD_STOPPED_OFFS);
  3286. gsi_writel(8, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_0_OFFS);
  3287. gsi_writel(9, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_2_OFFS);
  3288. gsi_writel(10, gsi_base + GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_1_OFFS);
  3289. gsi_writel(11, gsi_base + GSI_GSI_IRAM_PTR_NEW_RE_OFFS);
  3290. gsi_writel(12, gsi_base + GSI_GSI_IRAM_PTR_READ_ENG_COMP_OFFS);
  3291. gsi_writel(13, gsi_base + GSI_GSI_IRAM_PTR_TIMER_EXPIRED_OFFS);
  3292. gsi_writel(14, gsi_base + GSI_GSI_IRAM_PTR_EV_DB_OFFS);
  3293. gsi_writel(15, gsi_base + GSI_GSI_IRAM_PTR_UC_GP_INT_OFFS);
  3294. gsi_writel(16, gsi_base + GSI_GSI_IRAM_PTR_WRITE_ENG_COMP_OFFS);
  3295. if (ver >= GSI_VER_2_5)
  3296. gsi_writel(17,
  3297. gsi_base + GSI_V2_5_GSI_IRAM_PTR_TLV_CH_NOT_FULL_OFFS);
  3298. }
  3299. static void gsi_configure_bck_prs_matrix(void *base)
  3300. {
  3301. void __iomem *gsi_base = (void __iomem *) base;
  3302. /*
  3303. * For now, these are default values. In the future, GSI FW image will
  3304. * produce optimized back-pressure values based on the FW image.
  3305. */
  3306. gsi_writel(0xfffffffe,
  3307. gsi_base + GSI_IC_DISABLE_CHNL_BCK_PRS_LSB_OFFS);
  3308. gsi_writel(0xffffffff,
  3309. gsi_base + GSI_IC_DISABLE_CHNL_BCK_PRS_MSB_OFFS);
  3310. gsi_writel(0xffffffbf, gsi_base + GSI_IC_GEN_EVNT_BCK_PRS_LSB_OFFS);
  3311. gsi_writel(0xffffffff, gsi_base + GSI_IC_GEN_EVNT_BCK_PRS_MSB_OFFS);
  3312. gsi_writel(0xffffefff, gsi_base + GSI_IC_GEN_INT_BCK_PRS_LSB_OFFS);
  3313. gsi_writel(0xffffffff, gsi_base + GSI_IC_GEN_INT_BCK_PRS_MSB_OFFS);
  3314. gsi_writel(0xffffefff,
  3315. gsi_base + GSI_IC_STOP_INT_MOD_BCK_PRS_LSB_OFFS);
  3316. gsi_writel(0xffffffff,
  3317. gsi_base + GSI_IC_STOP_INT_MOD_BCK_PRS_MSB_OFFS);
  3318. gsi_writel(0x00000000,
  3319. gsi_base + GSI_IC_PROCESS_DESC_BCK_PRS_LSB_OFFS);
  3320. gsi_writel(0x00000000,
  3321. gsi_base + GSI_IC_PROCESS_DESC_BCK_PRS_MSB_OFFS);
  3322. gsi_writel(0xf9ffffff, gsi_base + GSI_IC_TLV_STOP_BCK_PRS_LSB_OFFS);
  3323. gsi_writel(0xffffffff, gsi_base + GSI_IC_TLV_STOP_BCK_PRS_MSB_OFFS);
  3324. gsi_writel(0xf9ffffff, gsi_base + GSI_IC_TLV_RESET_BCK_PRS_LSB_OFFS);
  3325. gsi_writel(0xffffffff, gsi_base + GSI_IC_TLV_RESET_BCK_PRS_MSB_OFFS);
  3326. gsi_writel(0xffffffff, gsi_base + GSI_IC_RGSTR_TIMER_BCK_PRS_LSB_OFFS);
  3327. gsi_writel(0xfffffffe, gsi_base + GSI_IC_RGSTR_TIMER_BCK_PRS_MSB_OFFS);
  3328. gsi_writel(0xffffffff, gsi_base + GSI_IC_READ_BCK_PRS_LSB_OFFS);
  3329. gsi_writel(0xffffefff, gsi_base + GSI_IC_READ_BCK_PRS_MSB_OFFS);
  3330. gsi_writel(0xffffffff, gsi_base + GSI_IC_WRITE_BCK_PRS_LSB_OFFS);
  3331. gsi_writel(0xffffdfff, gsi_base + GSI_IC_WRITE_BCK_PRS_MSB_OFFS);
  3332. gsi_writel(0xffffffff,
  3333. gsi_base + GSI_IC_UCONTROLLER_GPR_BCK_PRS_LSB_OFFS);
  3334. gsi_writel(0xff03ffff,
  3335. gsi_base + GSI_IC_UCONTROLLER_GPR_BCK_PRS_MSB_OFFS);
  3336. }
  3337. int gsi_configure_regs(phys_addr_t per_base_addr, enum gsi_ver ver)
  3338. {
  3339. if (!gsi_ctx) {
  3340. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3341. return -GSI_STATUS_NODEV;
  3342. }
  3343. if (!gsi_ctx->base) {
  3344. GSIERR("access to GSI HW has not been mapped\n");
  3345. return -GSI_STATUS_INVALID_PARAMS;
  3346. }
  3347. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  3348. GSIERR("Incorrect version %d\n", ver);
  3349. return -GSI_STATUS_ERROR;
  3350. }
  3351. gsi_writel(0, gsi_ctx->base + GSI_GSI_PERIPH_BASE_ADDR_MSB_OFFS);
  3352. gsi_writel(per_base_addr,
  3353. gsi_ctx->base + GSI_GSI_PERIPH_BASE_ADDR_LSB_OFFS);
  3354. gsi_configure_bck_prs_matrix((void *)gsi_ctx->base);
  3355. gsi_configure_ieps(gsi_ctx->base, ver);
  3356. return 0;
  3357. }
  3358. EXPORT_SYMBOL(gsi_configure_regs);
  3359. int gsi_enable_fw(phys_addr_t gsi_base_addr, u32 gsi_size, enum gsi_ver ver)
  3360. {
  3361. void __iomem *gsi_base;
  3362. uint32_t value;
  3363. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  3364. GSIERR("Incorrect version %d\n", ver);
  3365. return -GSI_STATUS_ERROR;
  3366. }
  3367. gsi_base = ioremap_nocache(gsi_base_addr, gsi_size);
  3368. if (!gsi_base) {
  3369. GSIERR("ioremap failed\n");
  3370. return -GSI_STATUS_RES_ALLOC_FAILURE;
  3371. }
  3372. /* Enable the MCS and set to x2 clocks */
  3373. if (ver >= GSI_VER_1_2) {
  3374. value = ((1 << GSI_GSI_MCS_CFG_MCS_ENABLE_SHFT) &
  3375. GSI_GSI_MCS_CFG_MCS_ENABLE_BMSK);
  3376. gsi_writel(value, gsi_base + GSI_GSI_MCS_CFG_OFFS);
  3377. value = (((1 << GSI_GSI_CFG_GSI_ENABLE_SHFT) &
  3378. GSI_GSI_CFG_GSI_ENABLE_BMSK) |
  3379. ((0 << GSI_GSI_CFG_MCS_ENABLE_SHFT) &
  3380. GSI_GSI_CFG_MCS_ENABLE_BMSK) |
  3381. ((1 << GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_SHFT) &
  3382. GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_BMSK) |
  3383. ((0 << GSI_GSI_CFG_UC_IS_MCS_SHFT) &
  3384. GSI_GSI_CFG_UC_IS_MCS_BMSK) |
  3385. ((0 << GSI_GSI_CFG_GSI_PWR_CLPS_SHFT) &
  3386. GSI_GSI_CFG_GSI_PWR_CLPS_BMSK) |
  3387. ((0 << GSI_GSI_CFG_BP_MTRIX_DISABLE_SHFT) &
  3388. GSI_GSI_CFG_BP_MTRIX_DISABLE_BMSK));
  3389. } else {
  3390. value = (((1 << GSI_GSI_CFG_GSI_ENABLE_SHFT) &
  3391. GSI_GSI_CFG_GSI_ENABLE_BMSK) |
  3392. ((1 << GSI_GSI_CFG_MCS_ENABLE_SHFT) &
  3393. GSI_GSI_CFG_MCS_ENABLE_BMSK) |
  3394. ((1 << GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_SHFT) &
  3395. GSI_GSI_CFG_DOUBLE_MCS_CLK_FREQ_BMSK) |
  3396. ((0 << GSI_GSI_CFG_UC_IS_MCS_SHFT) &
  3397. GSI_GSI_CFG_UC_IS_MCS_BMSK));
  3398. }
  3399. /* GSI frequency is peripheral frequency divided by 3 (2+1) */
  3400. if (ver >= GSI_VER_2_5)
  3401. value |= ((2 << GSI_V2_5_GSI_CFG_SLEEP_CLK_DIV_SHFT) &
  3402. GSI_V2_5_GSI_CFG_SLEEP_CLK_DIV_BMSK);
  3403. gsi_writel(value, gsi_base + GSI_GSI_CFG_OFFS);
  3404. iounmap(gsi_base);
  3405. return 0;
  3406. }
  3407. EXPORT_SYMBOL(gsi_enable_fw);
  3408. void gsi_get_inst_ram_offset_and_size(unsigned long *base_offset,
  3409. unsigned long *size, enum gsi_ver ver)
  3410. {
  3411. unsigned long maxn;
  3412. if (!gsi_ctx) {
  3413. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3414. return;
  3415. }
  3416. switch (ver) {
  3417. case GSI_VER_1_0:
  3418. case GSI_VER_1_2:
  3419. case GSI_VER_1_3:
  3420. maxn = GSI_GSI_INST_RAM_n_MAXn;
  3421. break;
  3422. case GSI_VER_2_0:
  3423. maxn = GSI_V2_0_GSI_INST_RAM_n_MAXn;
  3424. break;
  3425. case GSI_VER_2_2:
  3426. maxn = GSI_V2_2_GSI_INST_RAM_n_MAXn;
  3427. break;
  3428. case GSI_VER_2_5:
  3429. maxn = GSI_V2_5_GSI_INST_RAM_n_MAXn;
  3430. break;
  3431. case GSI_VER_2_7:
  3432. maxn = GSI_V2_7_GSI_INST_RAM_n_MAXn;
  3433. break;
  3434. case GSI_VER_ERR:
  3435. case GSI_VER_MAX:
  3436. default:
  3437. GSIERR("GSI version is not supported %d\n", ver);
  3438. WARN_ON(1);
  3439. return;
  3440. }
  3441. if (size)
  3442. *size = GSI_GSI_INST_RAM_n_WORD_SZ * (maxn + 1);
  3443. if (base_offset) {
  3444. if (ver < GSI_VER_2_5)
  3445. *base_offset = GSI_GSI_INST_RAM_n_OFFS(0);
  3446. else
  3447. *base_offset = GSI_V2_5_GSI_INST_RAM_n_OFFS(0);
  3448. }
  3449. }
  3450. EXPORT_SYMBOL(gsi_get_inst_ram_offset_and_size);
  3451. int gsi_halt_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  3452. {
  3453. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_HALT_CHANNEL;
  3454. uint32_t val;
  3455. int res;
  3456. if (!gsi_ctx) {
  3457. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3458. return -GSI_STATUS_NODEV;
  3459. }
  3460. if (chan_idx >= gsi_ctx->max_ch || !code) {
  3461. GSIERR("bad params chan_idx=%d\n", chan_idx);
  3462. return -GSI_STATUS_INVALID_PARAMS;
  3463. }
  3464. mutex_lock(&gsi_ctx->mlock);
  3465. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  3466. /* invalidate the response */
  3467. gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
  3468. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3469. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  3470. gsi_writel(gsi_ctx->scratch.word0.val, gsi_ctx->base +
  3471. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3472. gsi_ctx->gen_ee_cmd_dbg.halt_channel++;
  3473. val = (((op << GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_SHFT) &
  3474. GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_BMSK) |
  3475. ((chan_idx << GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_SHFT) &
  3476. GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_BMSK) |
  3477. ((ee << GSI_EE_n_GSI_EE_GENERIC_CMD_EE_SHFT) &
  3478. GSI_EE_n_GSI_EE_GENERIC_CMD_EE_BMSK));
  3479. gsi_writel(val, gsi_ctx->base +
  3480. GSI_EE_n_GSI_EE_GENERIC_CMD_OFFS(gsi_ctx->per.ee));
  3481. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  3482. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  3483. if (res == 0) {
  3484. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  3485. res = -GSI_STATUS_TIMED_OUT;
  3486. goto free_lock;
  3487. }
  3488. gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
  3489. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3490. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  3491. GSI_GEN_EE_CMD_RETURN_CODE_RETRY) {
  3492. GSIDBG("chan_idx=%u ee=%u busy try again\n", chan_idx, ee);
  3493. *code = GSI_GEN_EE_CMD_RETURN_CODE_RETRY;
  3494. res = -GSI_STATUS_AGAIN;
  3495. goto free_lock;
  3496. }
  3497. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  3498. GSIERR("No response received\n");
  3499. res = -GSI_STATUS_ERROR;
  3500. goto free_lock;
  3501. }
  3502. res = GSI_STATUS_SUCCESS;
  3503. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  3504. free_lock:
  3505. mutex_unlock(&gsi_ctx->mlock);
  3506. return res;
  3507. }
  3508. EXPORT_SYMBOL(gsi_halt_channel_ee);
  3509. int gsi_alloc_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  3510. {
  3511. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_ALLOC_CHANNEL;
  3512. struct gsi_chan_ctx *ctx;
  3513. uint32_t val;
  3514. int res;
  3515. if (chan_idx >= gsi_ctx->max_ch || !code) {
  3516. GSIERR("bad params chan_idx=%d\n", chan_idx);
  3517. return -GSI_STATUS_INVALID_PARAMS;
  3518. }
  3519. if (ee == 0)
  3520. return gsi_alloc_ap_channel(chan_idx);
  3521. mutex_lock(&gsi_ctx->mlock);
  3522. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  3523. /* invalidate the response */
  3524. gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
  3525. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3526. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  3527. gsi_writel(gsi_ctx->scratch.word0.val, gsi_ctx->base +
  3528. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3529. val = (((op << GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_SHFT) &
  3530. GSI_EE_n_GSI_EE_GENERIC_CMD_OPCODE_BMSK) |
  3531. ((chan_idx << GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_SHFT) &
  3532. GSI_EE_n_GSI_EE_GENERIC_CMD_VIRT_CHAN_IDX_BMSK) |
  3533. ((ee << GSI_EE_n_GSI_EE_GENERIC_CMD_EE_SHFT) &
  3534. GSI_EE_n_GSI_EE_GENERIC_CMD_EE_BMSK));
  3535. gsi_writel(val, gsi_ctx->base +
  3536. GSI_EE_n_GSI_EE_GENERIC_CMD_OFFS(gsi_ctx->per.ee));
  3537. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  3538. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  3539. if (res == 0) {
  3540. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  3541. res = -GSI_STATUS_TIMED_OUT;
  3542. goto free_lock;
  3543. }
  3544. gsi_ctx->scratch.word0.val = gsi_readl(gsi_ctx->base +
  3545. GSI_EE_n_CNTXT_SCRATCH_0_OFFS(gsi_ctx->per.ee));
  3546. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  3547. GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES) {
  3548. GSIDBG("chan_idx=%u ee=%u out of resources\n", chan_idx, ee);
  3549. *code = GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES;
  3550. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  3551. goto free_lock;
  3552. }
  3553. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  3554. GSIERR("No response received\n");
  3555. res = -GSI_STATUS_ERROR;
  3556. goto free_lock;
  3557. }
  3558. if (ee == 0) {
  3559. ctx = &gsi_ctx->chan[chan_idx];
  3560. gsi_ctx->ch_dbg[chan_idx].ch_allocate++;
  3561. }
  3562. res = GSI_STATUS_SUCCESS;
  3563. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  3564. free_lock:
  3565. mutex_unlock(&gsi_ctx->mlock);
  3566. return res;
  3567. }
  3568. EXPORT_SYMBOL(gsi_alloc_channel_ee);
  3569. int gsi_map_virtual_ch_to_per_ep(u32 ee, u32 chan_num, u32 per_ep_index)
  3570. {
  3571. if (!gsi_ctx) {
  3572. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3573. return -GSI_STATUS_NODEV;
  3574. }
  3575. if (!gsi_ctx->base) {
  3576. GSIERR("access to GSI HW has not been mapped\n");
  3577. return -GSI_STATUS_INVALID_PARAMS;
  3578. }
  3579. gsi_writel(per_ep_index,
  3580. gsi_ctx->base +
  3581. GSI_V2_5_GSI_MAP_EE_n_CH_k_VP_TABLE_OFFS(chan_num, ee));
  3582. return 0;
  3583. }
  3584. EXPORT_SYMBOL(gsi_map_virtual_ch_to_per_ep);
  3585. void gsi_wdi3_write_evt_ring_db(unsigned long evt_ring_hdl,
  3586. uint32_t db_addr_low, uint32_t db_addr_high)
  3587. {
  3588. if (!gsi_ctx) {
  3589. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3590. return;
  3591. }
  3592. gsi_writel(db_addr_low, gsi_ctx->base +
  3593. GSI_EE_n_EV_CH_k_CNTXT_12_OFFS(evt_ring_hdl, gsi_ctx->per.ee));
  3594. gsi_writel(db_addr_high, gsi_ctx->base +
  3595. GSI_EE_n_EV_CH_k_CNTXT_13_OFFS(evt_ring_hdl, gsi_ctx->per.ee));
  3596. }
  3597. EXPORT_SYMBOL(gsi_wdi3_write_evt_ring_db);
  3598. void gsi_wdi3_dump_register(unsigned long chan_hdl)
  3599. {
  3600. uint32_t val;
  3601. if (!gsi_ctx) {
  3602. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3603. return;
  3604. }
  3605. GSIDBG("reg dump ch id %ld\n", chan_hdl);
  3606. val = gsi_readl(gsi_ctx->base +
  3607. GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS(chan_hdl,
  3608. gsi_ctx->per.ee));
  3609. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_0_OFFS 0x%x\n", val);
  3610. val = gsi_readl(gsi_ctx->base +
  3611. GSI_EE_n_GSI_CH_k_CNTXT_1_OFFS(chan_hdl,
  3612. gsi_ctx->per.ee));
  3613. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_1_OFFS 0x%x\n", val);
  3614. val = gsi_readl(gsi_ctx->base +
  3615. GSI_EE_n_GSI_CH_k_CNTXT_2_OFFS(chan_hdl,
  3616. gsi_ctx->per.ee));
  3617. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_2_OFFS 0x%x\n", val);
  3618. val = gsi_readl(gsi_ctx->base +
  3619. GSI_EE_n_GSI_CH_k_CNTXT_3_OFFS(chan_hdl,
  3620. gsi_ctx->per.ee));
  3621. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_3_OFFS 0x%x\n", val);
  3622. val = gsi_readl(gsi_ctx->base +
  3623. GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS(chan_hdl,
  3624. gsi_ctx->per.ee));
  3625. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_4_OFFS 0x%x\n", val);
  3626. val = gsi_readl(gsi_ctx->base +
  3627. GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS(chan_hdl,
  3628. gsi_ctx->per.ee));
  3629. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_5_OFFS 0x%x\n", val);
  3630. val = gsi_readl(gsi_ctx->base +
  3631. GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS(chan_hdl,
  3632. gsi_ctx->per.ee));
  3633. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_6_OFFS 0x%x\n", val);
  3634. val = gsi_readl(gsi_ctx->base +
  3635. GSI_EE_n_GSI_CH_k_CNTXT_7_OFFS(chan_hdl,
  3636. gsi_ctx->per.ee));
  3637. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_7_OFFS 0x%x\n", val);
  3638. val = gsi_readl(gsi_ctx->base +
  3639. GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR_OFFS(chan_hdl,
  3640. gsi_ctx->per.ee));
  3641. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR_OFFS 0x%x\n", val);
  3642. val = gsi_readl(gsi_ctx->base +
  3643. GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR_OFFS(chan_hdl,
  3644. gsi_ctx->per.ee));
  3645. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR_OFFS 0x%x\n", val);
  3646. val = gsi_readl(gsi_ctx->base +
  3647. GSI_EE_n_GSI_CH_k_QOS_OFFS(chan_hdl,
  3648. gsi_ctx->per.ee));
  3649. GSIDBG("GSI_EE_n_GSI_CH_k_QOS_OFFS 0x%x\n", val);
  3650. val = gsi_readl(gsi_ctx->base +
  3651. GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS(chan_hdl,
  3652. gsi_ctx->per.ee));
  3653. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_0_OFFS 0x%x\n", val);
  3654. val = gsi_readl(gsi_ctx->base +
  3655. GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS(chan_hdl,
  3656. gsi_ctx->per.ee));
  3657. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_1_OFFS 0x%x\n", val);
  3658. val = gsi_readl(gsi_ctx->base +
  3659. GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS(chan_hdl,
  3660. gsi_ctx->per.ee));
  3661. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_2_OFFS 0x%x\n", val);
  3662. val = gsi_readl(gsi_ctx->base +
  3663. GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS(chan_hdl,
  3664. gsi_ctx->per.ee));
  3665. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_3_OFFS 0x%x\n", val);
  3666. }
  3667. EXPORT_SYMBOL(gsi_wdi3_dump_register);
  3668. static int msm_gsi_probe(struct platform_device *pdev)
  3669. {
  3670. struct device *dev = &pdev->dev;
  3671. pr_debug("gsi_probe\n");
  3672. gsi_ctx = devm_kzalloc(dev, sizeof(*gsi_ctx), GFP_KERNEL);
  3673. if (!gsi_ctx) {
  3674. dev_err(dev, "failed to allocated gsi context\n");
  3675. return -ENOMEM;
  3676. }
  3677. gsi_ctx->ipc_logbuf = ipc_log_context_create(GSI_IPC_LOG_PAGES,
  3678. "gsi", 0);
  3679. if (gsi_ctx->ipc_logbuf == NULL)
  3680. GSIERR("failed to create IPC log, continue...\n");
  3681. gsi_ctx->dev = dev;
  3682. init_completion(&gsi_ctx->gen_ee_cmd_compl);
  3683. gsi_debugfs_init();
  3684. return 0;
  3685. }
  3686. static struct platform_driver msm_gsi_driver = {
  3687. .probe = msm_gsi_probe,
  3688. .driver = {
  3689. .name = "gsi",
  3690. .of_match_table = msm_gsi_match,
  3691. },
  3692. };
  3693. static struct platform_device *pdev;
  3694. /**
  3695. * Module Init.
  3696. */
  3697. static int __init gsi_init(void)
  3698. {
  3699. int ret;
  3700. pr_debug("%s\n", __func__);
  3701. ret = platform_driver_register(&msm_gsi_driver);
  3702. if (ret < 0)
  3703. goto out;
  3704. if (running_emulation) {
  3705. pdev = platform_device_register_simple("gsi", -1, NULL, 0);
  3706. if (IS_ERR(pdev)) {
  3707. ret = PTR_ERR(pdev);
  3708. platform_driver_unregister(&msm_gsi_driver);
  3709. goto out;
  3710. }
  3711. }
  3712. out:
  3713. return ret;
  3714. }
  3715. arch_initcall(gsi_init);
  3716. /*
  3717. * Module exit.
  3718. */
  3719. static void __exit gsi_exit(void)
  3720. {
  3721. if (running_emulation && pdev)
  3722. platform_device_unregister(pdev);
  3723. platform_driver_unregister(&msm_gsi_driver);
  3724. }
  3725. module_exit(gsi_exit);
  3726. MODULE_LICENSE("GPL v2");
  3727. MODULE_DESCRIPTION("Generic Software Interface (GSI)");