gsi.c 164 KB

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