gsi.c 126 KB

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