gsi.c 140 KB

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