gsi.c 164 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965
  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. struct msi_desc *desc = NULL;
  1195. size_t size = 0;
  1196. /* Allocate all MSIs */
  1197. GSIDBG("gsi_ctx->dev = %lu, gsi_ctx->msi.num = %d", gsi_ctx->dev, gsi_ctx->msi.num);
  1198. result = platform_msi_domain_alloc_irqs(gsi_ctx->dev, gsi_ctx->msi.num,
  1199. __gsi_msi_write_msg);
  1200. if (result) {
  1201. GSIERR("error allocating platform MSIs - %d\n", result);
  1202. return -GSI_STATUS_ERROR;
  1203. }
  1204. GSIDBG("MSI allocating is succesful\n");
  1205. /* Loop through the allocated MSIs and save the info, then
  1206. * request the IRQ.
  1207. */
  1208. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0))
  1209. for (unsigned long msi = 0; msi < gsi_ctx->msi.num; msi++) {
  1210. /* Save IRQ */
  1211. gsi_ctx->msi.irq[msi] = msi_get_virq(gsi_ctx->dev, msi);
  1212. GSIDBG("desc->irq =%d\n", desc->irq);
  1213. #else
  1214. for_each_msi_entry(desc, gsi_ctx->dev) {
  1215. unsigned long msi = desc->platform.msi_index;
  1216. /* Ensure a valid index */
  1217. if (msi >= gsi_ctx->msi.num) {
  1218. GSIERR("error invalid MSI %lu\n", msi);
  1219. result = -GSI_STATUS_ERROR;
  1220. goto err_free_msis;
  1221. }
  1222. /* Save IRQ */
  1223. gsi_ctx->msi.irq[msi] = desc->irq;
  1224. GSIDBG("desc->irq =%d\n", desc->irq);
  1225. #endif
  1226. /* Request the IRQ */
  1227. if (__gsi_request_msi_irq(msi)) {
  1228. GSIERR("error requesting IRQ for MSI %lu\n",
  1229. msi);
  1230. result = -GSI_STATUS_ERROR;
  1231. goto err_free_msis;
  1232. }
  1233. GSIDBG("Requesting IRQ succesful\n");
  1234. }
  1235. return result;
  1236. err_free_msis:
  1237. size = sizeof(unsigned long) * BITS_TO_LONGS(gsi_ctx->msi.num);
  1238. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1239. memset(gsi_ctx->msi.allocated, 0, size);
  1240. return result;
  1241. }
  1242. int gsi_register_device(struct gsi_per_props *props, unsigned long *dev_hdl)
  1243. {
  1244. int res;
  1245. int result = GSI_STATUS_SUCCESS;
  1246. struct gsihal_reg_gsi_status gsi_status;
  1247. struct gsihal_reg_gsi_ee_n_cntxt_gsi_irq gen_irq;
  1248. if (!gsi_ctx) {
  1249. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1250. return -GSI_STATUS_NODEV;
  1251. }
  1252. if (!props || !dev_hdl) {
  1253. GSIERR("bad params props=%pK dev_hdl=%pK\n", props, dev_hdl);
  1254. return -GSI_STATUS_INVALID_PARAMS;
  1255. }
  1256. if (props->ver <= GSI_VER_ERR || props->ver >= GSI_VER_MAX) {
  1257. GSIERR("bad params gsi_ver=%d\n", props->ver);
  1258. return -GSI_STATUS_INVALID_PARAMS;
  1259. }
  1260. if (!props->notify_cb) {
  1261. GSIERR("notify callback must be provided\n");
  1262. return -GSI_STATUS_INVALID_PARAMS;
  1263. }
  1264. if (props->req_clk_cb && !props->rel_clk_cb) {
  1265. GSIERR("rel callback must be provided\n");
  1266. return -GSI_STATUS_INVALID_PARAMS;
  1267. }
  1268. if (gsi_ctx->per_registered) {
  1269. GSIERR("per already registered\n");
  1270. return -GSI_STATUS_UNSUPPORTED_OP;
  1271. }
  1272. spin_lock_init(&gsi_ctx->slock);
  1273. gsi_ctx->per = *props;
  1274. if (props->intr == GSI_INTR_IRQ) {
  1275. if (!props->irq) {
  1276. GSIERR("bad irq specified %u\n", props->irq);
  1277. return -GSI_STATUS_INVALID_PARAMS;
  1278. }
  1279. /*
  1280. * On a real UE, there are two separate interrupt
  1281. * vectors that get directed toward the GSI/IPA
  1282. * drivers. They are handled by gsi_isr() and
  1283. * (ipa_isr() or ipa3_isr()) respectively. In the
  1284. * emulation environment, this is not the case;
  1285. * instead, interrupt vectors are routed to the
  1286. * emualation hardware's interrupt controller, which
  1287. * in turn, forwards a single interrupt to the GSI/IPA
  1288. * driver. When the new interrupt vector is received,
  1289. * the driver needs to probe the interrupt
  1290. * controller's registers so see if one, the other, or
  1291. * both interrupts have occurred. Given the above, we
  1292. * now need to handle both situations, namely: the
  1293. * emulator's and the real UE.
  1294. */
  1295. if (running_emulation) {
  1296. /*
  1297. * New scheme involving the emulator's
  1298. * interrupt controller.
  1299. */
  1300. res = devm_request_threaded_irq(
  1301. gsi_ctx->dev,
  1302. props->irq,
  1303. /* top half handler to follow */
  1304. emulator_hard_irq_isr,
  1305. /* threaded bottom half handler to follow */
  1306. emulator_soft_irq_isr,
  1307. IRQF_SHARED,
  1308. "emulator_intcntrlr",
  1309. gsi_ctx);
  1310. } else {
  1311. /*
  1312. * Traditional scheme used on the real UE.
  1313. */
  1314. res = devm_request_irq(gsi_ctx->dev, props->irq,
  1315. gsi_isr,
  1316. props->req_clk_cb ? IRQF_TRIGGER_RISING :
  1317. IRQF_TRIGGER_HIGH,
  1318. "gsi",
  1319. gsi_ctx);
  1320. }
  1321. if (res) {
  1322. GSIERR(
  1323. "failed to register isr for %u\n",
  1324. props->irq);
  1325. return -GSI_STATUS_ERROR;
  1326. }
  1327. GSIDBG(
  1328. "succeeded to register isr for %u\n",
  1329. props->irq);
  1330. res = enable_irq_wake(props->irq);
  1331. if (res)
  1332. GSIERR("failed to enable wake irq %u\n", props->irq);
  1333. else
  1334. GSIERR("GSI irq is wake enabled %u\n", props->irq);
  1335. } else {
  1336. GSIERR("do not support interrupt type %u\n", props->intr);
  1337. return -GSI_STATUS_UNSUPPORTED_OP;
  1338. }
  1339. /* If MSIs are enabled, make sure they are set up */
  1340. if (gsi_ctx->msi.num) {
  1341. if (__gsi_allocate_msis()) {
  1342. GSIERR("failed to allocate MSIs\n");
  1343. goto err_free_irq;
  1344. }
  1345. }
  1346. /*
  1347. * If base not previously mapped via gsi_map_base(), map it
  1348. * now...
  1349. */
  1350. if (!gsi_ctx->base) {
  1351. res = gsi_map_base(props->phys_addr, props->size, props->ver);
  1352. if (res) {
  1353. result = res;
  1354. goto err_free_msis;
  1355. }
  1356. }
  1357. if (running_emulation) {
  1358. GSIDBG("GSI SW ver register value 0x%x\n",
  1359. gsihal_read_reg_n(GSI_EE_n_GSI_SW_VERSION, 0));
  1360. gsi_ctx->intcntrlr_mem_size =
  1361. props->emulator_intcntrlr_size;
  1362. gsi_ctx->intcntrlr_base =
  1363. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0))
  1364. devm_ioremap(
  1365. #else
  1366. devm_ioremap_nocache(
  1367. #endif
  1368. gsi_ctx->dev,
  1369. props->emulator_intcntrlr_addr,
  1370. props->emulator_intcntrlr_size);
  1371. if (!gsi_ctx->intcntrlr_base) {
  1372. GSIERR(
  1373. "failed to remap emulator's interrupt controller HW\n");
  1374. gsi_unmap_base();
  1375. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1376. result = -GSI_STATUS_RES_ALLOC_FAILURE;
  1377. goto err_iounmap;
  1378. }
  1379. GSIDBG(
  1380. "Emulator's interrupt controller base(%pa) mapped to (%pK) with len (0x%lx)\n",
  1381. &(props->emulator_intcntrlr_addr),
  1382. gsi_ctx->intcntrlr_base,
  1383. props->emulator_intcntrlr_size);
  1384. gsi_ctx->intcntrlr_gsi_isr = gsi_isr;
  1385. gsi_ctx->intcntrlr_client_isr =
  1386. props->emulator_intcntrlr_client_isr;
  1387. }
  1388. gsi_ctx->per_registered = true;
  1389. mutex_init(&gsi_ctx->mlock);
  1390. atomic_set(&gsi_ctx->num_chan, 0);
  1391. atomic_set(&gsi_ctx->num_evt_ring, 0);
  1392. gsi_ctx->max_ch = gsi_get_max_channels(gsi_ctx->per.ver);
  1393. if (gsi_ctx->max_ch == 0) {
  1394. gsi_unmap_base();
  1395. if (running_emulation)
  1396. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1397. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1398. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1399. GSIERR("failed to get max channels\n");
  1400. result = -GSI_STATUS_ERROR;
  1401. goto err_iounmap;
  1402. }
  1403. gsi_ctx->max_ev = gsi_get_max_event_rings(gsi_ctx->per.ver);
  1404. if (gsi_ctx->max_ev == 0) {
  1405. gsi_unmap_base();
  1406. if (running_emulation)
  1407. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1408. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1409. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1410. GSIERR("failed to get max event rings\n");
  1411. result = -GSI_STATUS_ERROR;
  1412. goto err_iounmap;
  1413. }
  1414. if (gsi_ctx->max_ev > GSI_EVT_RING_MAX) {
  1415. GSIERR("max event rings are beyond absolute maximum\n");
  1416. result = -GSI_STATUS_ERROR;
  1417. goto err_iounmap;
  1418. }
  1419. if (props->mhi_er_id_limits_valid &&
  1420. props->mhi_er_id_limits[0] > (gsi_ctx->max_ev - 1)) {
  1421. gsi_unmap_base();
  1422. if (running_emulation)
  1423. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1424. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1425. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1426. GSIERR("MHI event ring start id %u is beyond max %u\n",
  1427. props->mhi_er_id_limits[0], gsi_ctx->max_ev);
  1428. result = -GSI_STATUS_ERROR;
  1429. goto err_iounmap;
  1430. }
  1431. gsi_ctx->evt_bmap = ~((((unsigned long)1) << gsi_ctx->max_ev) - 1);
  1432. /* exclude reserved mhi events */
  1433. if (props->mhi_er_id_limits_valid)
  1434. gsi_ctx->evt_bmap |=
  1435. ((1 << (props->mhi_er_id_limits[1] + 1)) - 1) ^
  1436. ((1 << (props->mhi_er_id_limits[0])) - 1);
  1437. /*
  1438. * enable all interrupts but GSI_BREAK_POINT.
  1439. * Inter EE commands / interrupt are no supported.
  1440. */
  1441. __gsi_config_type_irq(props->ee, ~0, ~0);
  1442. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1443. __gsi_config_all_ch_irq(props->ee, ~0, ~0);
  1444. __gsi_config_all_evt_irq(props->ee, ~0, ~0);
  1445. __gsi_config_all_ieob_irq(props->ee, ~0, ~0);
  1446. }
  1447. else {
  1448. __gsi_config_ch_irq(props->ee, ~0, ~0);
  1449. __gsi_config_evt_irq(props->ee, ~0, ~0);
  1450. __gsi_config_ieob_irq(props->ee, ~0, ~0);
  1451. }
  1452. __gsi_config_glob_irq(props->ee, ~0, ~0);
  1453. /*
  1454. * Disabling global INT1 interrupt by default and enable it
  1455. * onlt when sending the generic command.
  1456. */
  1457. __gsi_config_glob_irq(props->ee,
  1458. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  1459. gen_irq.gsi_mcs_stack_ovrflow = 1;
  1460. gen_irq.gsi_cmd_fifo_ovrflow = 1;
  1461. gen_irq.gsi_bus_error = 1;
  1462. gen_irq.gsi_break_point = 0;
  1463. gsihal_write_reg_n_fields(GSI_EE_n_CNTXT_GSI_IRQ_EN,
  1464. gsi_ctx->per.ee, &gen_irq);
  1465. gsihal_write_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee, props->intr);
  1466. /* set GSI_TOP_EE_n_CNTXT_MSI_BASE_LSB/MSB to 0 */
  1467. if ((gsi_ctx->per.ver >= GSI_VER_2_0) &&
  1468. (props->intr != GSI_INTR_MSI)) {
  1469. gsihal_write_reg_n(
  1470. GSI_EE_n_CNTXT_MSI_BASE_LSB, gsi_ctx->per.ee, 0);
  1471. gsihal_write_reg_n(
  1472. GSI_EE_n_CNTXT_MSI_BASE_MSB, gsi_ctx->per.ee, 0);
  1473. }
  1474. gsihal_read_reg_n_fields(GSI_EE_n_GSI_STATUS,
  1475. gsi_ctx->per.ee, &gsi_status);
  1476. if (gsi_status.enabled)
  1477. gsi_ctx->enabled = true;
  1478. else
  1479. GSIERR("Manager EE has not enabled GSI, GSI un-usable\n");
  1480. if (gsi_ctx->per.ver >= GSI_VER_1_2)
  1481. gsihal_write_reg_n(GSI_EE_n_ERROR_LOG, gsi_ctx->per.ee, 0);
  1482. if (running_emulation) {
  1483. /*
  1484. * Set up the emulator's interrupt controller...
  1485. */
  1486. res = setup_emulator_cntrlr(
  1487. gsi_ctx->intcntrlr_base, gsi_ctx->intcntrlr_mem_size);
  1488. if (res != 0) {
  1489. GSIERR("setup_emulator_cntrlr() failed\n");
  1490. result = res;
  1491. goto err_iounmap;
  1492. }
  1493. }
  1494. *dev_hdl = (uintptr_t)gsi_ctx;
  1495. gsi_ctx->gsi_isr_cache_index = 0;
  1496. return result;
  1497. err_iounmap:
  1498. gsi_unmap_base();
  1499. if (running_emulation && gsi_ctx->intcntrlr_base != NULL)
  1500. devm_iounmap(gsi_ctx->dev, gsi_ctx->intcntrlr_base);
  1501. gsi_ctx->base = gsi_ctx->intcntrlr_base = NULL;
  1502. err_free_msis:
  1503. if (gsi_ctx->msi.num) {
  1504. size_t size =
  1505. sizeof(unsigned long) * BITS_TO_LONGS(gsi_ctx->msi.num);
  1506. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1507. memset(gsi_ctx->msi.allocated, 0, size);
  1508. }
  1509. err_free_irq:
  1510. devm_free_irq(gsi_ctx->dev, props->irq, gsi_ctx);
  1511. return result;
  1512. }
  1513. EXPORT_SYMBOL(gsi_register_device);
  1514. int gsi_write_device_scratch(unsigned long dev_hdl,
  1515. struct gsi_device_scratch *val)
  1516. {
  1517. unsigned int max_usb_pkt_size = 0;
  1518. if (!gsi_ctx) {
  1519. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1520. return -GSI_STATUS_NODEV;
  1521. }
  1522. if (!gsi_ctx->per_registered) {
  1523. GSIERR("no client registered\n");
  1524. return -GSI_STATUS_INVALID_PARAMS;
  1525. }
  1526. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1527. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1528. gsi_ctx);
  1529. return -GSI_STATUS_INVALID_PARAMS;
  1530. }
  1531. if (val->max_usb_pkt_size_valid &&
  1532. val->max_usb_pkt_size != 1024 &&
  1533. val->max_usb_pkt_size != 512 &&
  1534. val->max_usb_pkt_size != 64) {
  1535. GSIERR("bad USB max pkt size dev_hdl=0x%lx sz=%u\n", dev_hdl,
  1536. val->max_usb_pkt_size);
  1537. return -GSI_STATUS_INVALID_PARAMS;
  1538. }
  1539. mutex_lock(&gsi_ctx->mlock);
  1540. if (val->mhi_base_chan_idx_valid)
  1541. gsi_ctx->scratch.word0.s.mhi_base_chan_idx =
  1542. val->mhi_base_chan_idx;
  1543. if (val->max_usb_pkt_size_valid) {
  1544. max_usb_pkt_size = 2;
  1545. if (val->max_usb_pkt_size > 64)
  1546. max_usb_pkt_size =
  1547. (val->max_usb_pkt_size == 1024) ? 1 : 0;
  1548. gsi_ctx->scratch.word0.s.max_usb_pkt_size = max_usb_pkt_size;
  1549. }
  1550. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  1551. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  1552. mutex_unlock(&gsi_ctx->mlock);
  1553. return GSI_STATUS_SUCCESS;
  1554. }
  1555. EXPORT_SYMBOL(gsi_write_device_scratch);
  1556. int gsi_deregister_device(unsigned long dev_hdl, bool force)
  1557. {
  1558. if (!gsi_ctx) {
  1559. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1560. return -GSI_STATUS_NODEV;
  1561. }
  1562. if (!gsi_ctx->per_registered) {
  1563. GSIERR("no client registered\n");
  1564. return -GSI_STATUS_INVALID_PARAMS;
  1565. }
  1566. if (dev_hdl != (uintptr_t)gsi_ctx) {
  1567. GSIERR("bad params dev_hdl=0x%lx gsi_ctx=0x%pK\n", dev_hdl,
  1568. gsi_ctx);
  1569. return -GSI_STATUS_INVALID_PARAMS;
  1570. }
  1571. if (!force && atomic_read(&gsi_ctx->num_chan)) {
  1572. GSIERR("cannot deregister %u channels are still connected\n",
  1573. atomic_read(&gsi_ctx->num_chan));
  1574. return -GSI_STATUS_UNSUPPORTED_OP;
  1575. }
  1576. if (!force && atomic_read(&gsi_ctx->num_evt_ring)) {
  1577. GSIERR("cannot deregister %u events are still connected\n",
  1578. atomic_read(&gsi_ctx->num_evt_ring));
  1579. return -GSI_STATUS_UNSUPPORTED_OP;
  1580. }
  1581. /* disable all interrupts */
  1582. __gsi_config_type_irq(gsi_ctx->per.ee, ~0, 0);
  1583. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1584. __gsi_config_all_ch_irq(gsi_ctx->per.ee, ~0, 0);
  1585. __gsi_config_all_evt_irq(gsi_ctx->per.ee, ~0, 0);
  1586. __gsi_config_all_ieob_irq(gsi_ctx->per.ee, ~0, 0);
  1587. }
  1588. else {
  1589. __gsi_config_ch_irq(gsi_ctx->per.ee, ~0, 0);
  1590. __gsi_config_evt_irq(gsi_ctx->per.ee, ~0, 0);
  1591. __gsi_config_ieob_irq(gsi_ctx->per.ee, ~0, 0);
  1592. }
  1593. __gsi_config_glob_irq(gsi_ctx->per.ee, ~0, 0);
  1594. __gsi_config_gen_irq(gsi_ctx->per.ee, ~0, 0);
  1595. if (gsi_ctx->msi.num)
  1596. platform_msi_domain_free_irqs(gsi_ctx->dev);
  1597. devm_free_irq(gsi_ctx->dev, gsi_ctx->per.irq, gsi_ctx);
  1598. gsihal_destroy();
  1599. gsi_unmap_base();
  1600. gsi_ctx->per_registered = false;
  1601. return GSI_STATUS_SUCCESS;
  1602. }
  1603. EXPORT_SYMBOL(gsi_deregister_device);
  1604. static void gsi_program_evt_ring_ctx(struct gsi_evt_ring_props *props,
  1605. uint8_t evt_id, unsigned int ee)
  1606. {
  1607. struct gsihal_reg_ev_ch_k_cntxt_0 ev_ch_k_cntxt_0;
  1608. struct gsihal_reg_ev_ch_k_cntxt_1 ev_ch_k_cntxt_1;
  1609. struct gsihal_reg_ev_ch_k_cntxt_2 ev_ch_k_cntxt_2;
  1610. struct gsihal_reg_ev_ch_k_cntxt_3 ev_ch_k_cntxt_3;
  1611. struct gsihal_reg_ev_ch_k_cntxt_8 ev_ch_k_cntxt_8;
  1612. struct gsihal_reg_ev_ch_k_cntxt_9 ev_ch_k_cntxt_9;
  1613. union gsihal_reg_ev_ch_k_cntxt_10 ev_ch_k_cntxt_10;
  1614. union gsihal_reg_ev_ch_k_cntxt_11 ev_ch_k_cntxt_11;
  1615. struct gsihal_reg_ev_ch_k_cntxt_12 ev_ch_k_cntxt_12;
  1616. struct gsihal_reg_ev_ch_k_cntxt_13 ev_ch_k_cntxt_13;
  1617. GSIDBG("intf=%u intr=%u re=%u\n", props->intf, props->intr,
  1618. props->re_size);
  1619. ev_ch_k_cntxt_0.chtype = props->intf;
  1620. ev_ch_k_cntxt_0.intype = props->intr;
  1621. ev_ch_k_cntxt_0.element_size = props->re_size;
  1622. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_0,
  1623. ee, evt_id, &ev_ch_k_cntxt_0);
  1624. ev_ch_k_cntxt_1.r_length = props->ring_len;
  1625. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_1,
  1626. ee, evt_id,
  1627. &ev_ch_k_cntxt_1);
  1628. ev_ch_k_cntxt_2.r_base_addr_lsbs = GSI_LSB(props->ring_base_addr);
  1629. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_2,
  1630. ee, evt_id,
  1631. &ev_ch_k_cntxt_2);
  1632. ev_ch_k_cntxt_3.r_base_addr_msbs = GSI_MSB(props->ring_base_addr);
  1633. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_3,
  1634. ee, evt_id,
  1635. &ev_ch_k_cntxt_3);
  1636. ev_ch_k_cntxt_8.int_modt = props->int_modt;
  1637. ev_ch_k_cntxt_8.int_modc = props->int_modc;
  1638. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_8,
  1639. ee, evt_id,
  1640. &ev_ch_k_cntxt_8);
  1641. ev_ch_k_cntxt_9.intvec = props->intvec;
  1642. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_9,
  1643. ee, evt_id,
  1644. &ev_ch_k_cntxt_9);
  1645. if(props->intf != GSI_EVT_CHTYPE_WDI3_V2_EV) {
  1646. ev_ch_k_cntxt_10.msi_addr_lsb = GSI_LSB(props->msi_addr);
  1647. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_10,
  1648. ee, evt_id,
  1649. &ev_ch_k_cntxt_10);
  1650. ev_ch_k_cntxt_11.msi_addr_msb = GSI_MSB(props->msi_addr);
  1651. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_11,
  1652. ee, evt_id,
  1653. &ev_ch_k_cntxt_11);
  1654. ev_ch_k_cntxt_12.rp_update_addr_lsb = GSI_LSB(props->rp_update_addr);
  1655. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_12,
  1656. ee, evt_id,
  1657. &ev_ch_k_cntxt_12);
  1658. ev_ch_k_cntxt_13.rp_update_addr_msb = GSI_MSB(props->rp_update_addr);
  1659. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_13,
  1660. ee, evt_id,
  1661. &ev_ch_k_cntxt_13);
  1662. }
  1663. else {
  1664. ev_ch_k_cntxt_10.rp_addr_lsb = GSI_LSB(props->rp_update_addr);
  1665. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_10,
  1666. ee, evt_id,
  1667. &ev_ch_k_cntxt_10);
  1668. ev_ch_k_cntxt_11.rp_addr_msb = GSI_MSB(props->rp_update_addr);
  1669. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_CNTXT_11,
  1670. ee, evt_id,
  1671. &ev_ch_k_cntxt_11);
  1672. }
  1673. }
  1674. static void gsi_init_evt_ring(struct gsi_evt_ring_props *props,
  1675. struct gsi_ring_ctx *ctx)
  1676. {
  1677. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  1678. ctx->base = props->ring_base_addr;
  1679. ctx->wp = ctx->base;
  1680. ctx->rp = ctx->base;
  1681. ctx->wp_local = ctx->base;
  1682. ctx->rp_local = ctx->base;
  1683. ctx->len = props->ring_len;
  1684. ctx->elem_sz = props->re_size;
  1685. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  1686. ctx->end = ctx->base + (ctx->max_num_elem + 1) * ctx->elem_sz;
  1687. if (props->rp_update_vaddr)
  1688. *(uint64_t *)(props->rp_update_vaddr) = ctx->rp_local;
  1689. }
  1690. static void gsi_prime_evt_ring(struct gsi_evt_ctx *ctx)
  1691. {
  1692. unsigned long flags;
  1693. struct gsihal_reg_gsi_ee_n_ev_ch_k_doorbell_1 db;
  1694. spin_lock_irqsave(&ctx->ring.slock, flags);
  1695. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1696. ctx->ring.wp_local = ctx->ring.base +
  1697. ctx->ring.max_num_elem * ctx->ring.elem_sz;
  1698. /* write order MUST be MSB followed by LSB */
  1699. db.write_ptr_msb = GSI_MSB(ctx->ring.wp_local);
  1700. gsihal_write_reg_nk_fields(GSI_EE_n_EV_CH_k_DOORBELL_1,
  1701. gsi_ctx->per.ee, ctx->id, &db);
  1702. gsi_ring_evt_doorbell(ctx);
  1703. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1704. }
  1705. static void gsi_prime_evt_ring_wdi(struct gsi_evt_ctx *ctx)
  1706. {
  1707. unsigned long flags;
  1708. spin_lock_irqsave(&ctx->ring.slock, flags);
  1709. if (ctx->ring.base_va)
  1710. memset((void *)ctx->ring.base_va, 0, ctx->ring.len);
  1711. ctx->ring.wp_local = ctx->ring.base +
  1712. ((ctx->ring.max_num_elem + 2) * ctx->ring.elem_sz);
  1713. gsi_ring_evt_doorbell(ctx);
  1714. spin_unlock_irqrestore(&ctx->ring.slock, flags);
  1715. }
  1716. static int gsi_validate_evt_ring_props(struct gsi_evt_ring_props *props)
  1717. {
  1718. uint64_t ra;
  1719. if ((props->re_size == GSI_EVT_RING_RE_SIZE_4B &&
  1720. props->ring_len % 4) ||
  1721. (props->re_size == GSI_EVT_RING_RE_SIZE_8B &&
  1722. props->ring_len % 8) ||
  1723. (props->re_size == GSI_EVT_RING_RE_SIZE_16B &&
  1724. props->ring_len % 16) ||
  1725. (props->re_size == GSI_EVT_RING_RE_SIZE_32B &&
  1726. props->ring_len % 32)) {
  1727. GSIERR("bad params ring_len %u not a multiple of RE size %u\n",
  1728. props->ring_len, props->re_size);
  1729. return -GSI_STATUS_INVALID_PARAMS;
  1730. }
  1731. if (!gsihal_check_ring_length_valid(props->ring_len, props->re_size))
  1732. return -GSI_STATUS_INVALID_PARAMS;
  1733. ra = props->ring_base_addr;
  1734. do_div(ra, roundup_pow_of_two(props->ring_len));
  1735. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  1736. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  1737. props->ring_base_addr,
  1738. roundup_pow_of_two(props->ring_len));
  1739. return -GSI_STATUS_INVALID_PARAMS;
  1740. }
  1741. if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
  1742. !props->ring_base_vaddr) {
  1743. GSIERR("protocol %u requires ring base VA\n", props->intf);
  1744. return -GSI_STATUS_INVALID_PARAMS;
  1745. }
  1746. if (props->intf == GSI_EVT_CHTYPE_MHI_EV &&
  1747. (!props->evchid_valid ||
  1748. props->evchid > gsi_ctx->per.mhi_er_id_limits[1] ||
  1749. props->evchid < gsi_ctx->per.mhi_er_id_limits[0])) {
  1750. GSIERR("MHI requires evchid valid=%d val=%u\n",
  1751. props->evchid_valid, props->evchid);
  1752. return -GSI_STATUS_INVALID_PARAMS;
  1753. }
  1754. if (props->intf != GSI_EVT_CHTYPE_MHI_EV &&
  1755. props->evchid_valid) {
  1756. GSIERR("protocol %u cannot specify evchid\n", props->intf);
  1757. return -GSI_STATUS_INVALID_PARAMS;
  1758. }
  1759. if (!props->err_cb) {
  1760. GSIERR("err callback must be provided\n");
  1761. return -GSI_STATUS_INVALID_PARAMS;
  1762. }
  1763. return GSI_STATUS_SUCCESS;
  1764. }
  1765. /**
  1766. * gsi_cleanup_xfer_user_data: cleanup the user data array using callback passed
  1767. * by IPA driver. Need to do this in GSI since only GSI knows which TRE
  1768. * are being used or not. However, IPA is the one that does cleaning,
  1769. * therefore we pass a callback from IPA and call it using params from GSI
  1770. *
  1771. * @chan_hdl: hdl of the gsi channel user data array to be cleaned
  1772. * @cleanup_cb: callback used to clean the user data array. takes 2 inputs
  1773. * @chan_user_data: ipa_sys_context of the gsi_channel
  1774. * @xfer_uder_data: user data array element (rx_pkt wrapper)
  1775. *
  1776. * Returns: 0 on success, negative on failure
  1777. */
  1778. static int gsi_cleanup_xfer_user_data(unsigned long chan_hdl,
  1779. void (*cleanup_cb)(void *chan_user_data, void *xfer_user_data))
  1780. {
  1781. struct gsi_chan_ctx *ctx;
  1782. uint64_t i;
  1783. uint16_t rp_idx;
  1784. ctx = &gsi_ctx->chan[chan_hdl];
  1785. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  1786. GSIERR("bad state %d\n", ctx->state);
  1787. return -GSI_STATUS_UNSUPPORTED_OP;
  1788. }
  1789. /* for coalescing, traverse the whole array */
  1790. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  1791. size_t user_data_size =
  1792. ctx->ring.max_num_elem + 1 + GSI_VEID_MAX;
  1793. for (i = 0; i < user_data_size; i++) {
  1794. if (ctx->user_data[i].valid)
  1795. cleanup_cb(ctx->props.chan_user_data,
  1796. ctx->user_data[i].p);
  1797. }
  1798. } else {
  1799. /* for non-coalescing, clean between RP and WP */
  1800. while (ctx->ring.rp_local != ctx->ring.wp_local) {
  1801. rp_idx = gsi_find_idx_from_addr(&ctx->ring,
  1802. ctx->ring.rp_local);
  1803. WARN_ON(!ctx->user_data[rp_idx].valid);
  1804. cleanup_cb(ctx->props.chan_user_data,
  1805. ctx->user_data[rp_idx].p);
  1806. gsi_incr_ring_rp(&ctx->ring);
  1807. }
  1808. }
  1809. return 0;
  1810. }
  1811. /**
  1812. * gsi_read_event_ring_rp_ddr - function returns the RP value of the event
  1813. * ring read from the ring context register.
  1814. *
  1815. * @props: Props structere of the event channel
  1816. * @id: Event channel index
  1817. * @ee: EE
  1818. *
  1819. * @Return pointer to the read pointer
  1820. */
  1821. static inline uint64_t gsi_read_event_ring_rp_ddr(struct gsi_evt_ring_props* props,
  1822. uint8_t id, int ee)
  1823. {
  1824. return readl_relaxed(props->rp_update_vaddr);
  1825. }
  1826. /**
  1827. * gsi_read_event_ring_rp_reg - function returns the RP value of the event ring
  1828. * read from the DDR.
  1829. *
  1830. * @props: Props structere of the event channel
  1831. * @id: Event channel index
  1832. * @ee: EE
  1833. *
  1834. * @Return pointer to the read pointer
  1835. */
  1836. static inline uint64_t gsi_read_event_ring_rp_reg(struct gsi_evt_ring_props* props,
  1837. uint8_t id, int ee)
  1838. {
  1839. uint64_t rp;
  1840. rp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_4, ee, id);
  1841. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_5, ee, id)) << 32;
  1842. return rp;
  1843. }
  1844. static int __gsi_pair_msi(struct gsi_evt_ctx *ctx,
  1845. struct gsi_evt_ring_props *props)
  1846. {
  1847. int result = GSI_STATUS_SUCCESS;
  1848. unsigned long msi = 0;
  1849. if (IS_ERR_OR_NULL(ctx) || IS_ERR_OR_NULL(props) || IS_ERR_OR_NULL(gsi_ctx))
  1850. BUG();
  1851. /* Find the first unused MSI */
  1852. msi = find_first_zero_bit(gsi_ctx->msi.used, gsi_ctx->msi.num);
  1853. if (msi >= gsi_ctx->msi.num) {
  1854. GSIERR("No free MSIs for evt %u\n", ctx->id);
  1855. return -GSI_STATUS_ERROR;
  1856. }
  1857. /* Ensure it's been allocated */
  1858. if (!test_bit((int)msi, gsi_ctx->msi.allocated)) {
  1859. GSIDBG("MSI %lu not allocated\n", msi);
  1860. return -GSI_STATUS_ERROR;
  1861. }
  1862. /* Save the event ID for later lookup */
  1863. gsi_ctx->msi.evt[msi] = ctx->id;
  1864. /* Add this event to the IRQ mask */
  1865. set_bit((int)ctx->id, &gsi_ctx->msi.mask);
  1866. props->intvec = gsi_ctx->msi.msg[msi].data;
  1867. props->msi_addr = (uint64_t)gsi_ctx->msi.msg[msi].address_hi << 32 |
  1868. (uint64_t)gsi_ctx->msi.msg[msi].address_lo;
  1869. GSIDBG("props->intvec = %d, props->msi_addr = %lu\n", props->intvec, props->msi_addr);
  1870. if (props->msi_addr == 0)
  1871. BUG();
  1872. /* Mark MSI as used */
  1873. set_bit(msi, gsi_ctx->msi.used);
  1874. return result;
  1875. }
  1876. int gsi_alloc_evt_ring(struct gsi_evt_ring_props *props, unsigned long dev_hdl,
  1877. unsigned long *evt_ring_hdl)
  1878. {
  1879. unsigned long evt_id;
  1880. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_ALLOCATE;
  1881. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  1882. struct gsi_evt_ctx *ctx;
  1883. int res = 0;
  1884. int ee;
  1885. unsigned long flags;
  1886. if (!gsi_ctx) {
  1887. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  1888. return -GSI_STATUS_NODEV;
  1889. }
  1890. if (!props || !evt_ring_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  1891. GSIERR("bad params props=%pK dev_hdl=0x%lx evt_ring_hdl=%pK\n",
  1892. props, dev_hdl, evt_ring_hdl);
  1893. return -GSI_STATUS_INVALID_PARAMS;
  1894. }
  1895. if (gsi_validate_evt_ring_props(props)) {
  1896. GSIERR("invalid params\n");
  1897. return -GSI_STATUS_INVALID_PARAMS;
  1898. }
  1899. if (!props->evchid_valid) {
  1900. mutex_lock(&gsi_ctx->mlock);
  1901. evt_id = find_first_zero_bit(&gsi_ctx->evt_bmap,
  1902. sizeof(unsigned long) * BITS_PER_BYTE);
  1903. if (evt_id == sizeof(unsigned long) * BITS_PER_BYTE) {
  1904. GSIERR("failed to alloc event ID\n");
  1905. mutex_unlock(&gsi_ctx->mlock);
  1906. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1907. }
  1908. set_bit(evt_id, &gsi_ctx->evt_bmap);
  1909. mutex_unlock(&gsi_ctx->mlock);
  1910. } else {
  1911. evt_id = props->evchid;
  1912. }
  1913. GSIDBG("Using %lu as virt evt id\n", evt_id);
  1914. if (props->rp_update_addr != 0) {
  1915. GSIDBG("Using DDR to read event RP for virt evt id: %lu\n",
  1916. evt_id);
  1917. props->gsi_read_event_ring_rp =
  1918. gsi_read_event_ring_rp_ddr;
  1919. }
  1920. else {
  1921. GSIDBG("Using CONTEXT reg to read event RP for virt evt id: %lu\n",
  1922. evt_id);
  1923. props->gsi_read_event_ring_rp =
  1924. gsi_read_event_ring_rp_reg;
  1925. }
  1926. ctx = &gsi_ctx->evtr[evt_id];
  1927. memset(ctx, 0, sizeof(*ctx));
  1928. mutex_init(&ctx->mlock);
  1929. init_completion(&ctx->compl);
  1930. atomic_set(&ctx->chan_ref_cnt, 0);
  1931. ctx->num_of_chan_allocated = 0;
  1932. ctx->id = evt_id;
  1933. mutex_lock(&gsi_ctx->mlock);
  1934. /* Pair an MSI with this event if this is an MSI and GPI event channel
  1935. * NOTE: This modifies props, so must be before props are saved to ctx.
  1936. */
  1937. if (props->intf == GSI_EVT_CHTYPE_GPI_EV &&
  1938. props->intr == GSI_INTR_MSI) {
  1939. if (__gsi_pair_msi(ctx, props)) {
  1940. GSIERR("evt_id=%lu failed to pair MSI\n", evt_id);
  1941. if (!props->evchid_valid)
  1942. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1943. mutex_unlock(&gsi_ctx->mlock);
  1944. return -GSI_STATUS_NODEV;
  1945. }
  1946. GSIDBG("evt_id=%lu pair MSI succesful\n", evt_id);
  1947. }
  1948. ctx->props = *props;
  1949. ee = gsi_ctx->per.ee;
  1950. ev_ch_cmd.opcode = op;
  1951. ev_ch_cmd.chid = evt_id;
  1952. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD, ee, &ev_ch_cmd);
  1953. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  1954. if (res == 0) {
  1955. GSIERR("evt_id=%lu timed out\n", evt_id);
  1956. if (!props->evchid_valid)
  1957. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1958. mutex_unlock(&gsi_ctx->mlock);
  1959. return -GSI_STATUS_TIMED_OUT;
  1960. }
  1961. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  1962. GSIERR("evt_id=%lu allocation failed state=%u\n",
  1963. evt_id, ctx->state);
  1964. if (!props->evchid_valid)
  1965. clear_bit(evt_id, &gsi_ctx->evt_bmap);
  1966. mutex_unlock(&gsi_ctx->mlock);
  1967. return -GSI_STATUS_RES_ALLOC_FAILURE;
  1968. }
  1969. gsi_program_evt_ring_ctx(props, evt_id, gsi_ctx->per.ee);
  1970. spin_lock_init(&ctx->ring.slock);
  1971. gsi_init_evt_ring(props, &ctx->ring);
  1972. ctx->id = evt_id;
  1973. *evt_ring_hdl = evt_id;
  1974. atomic_inc(&gsi_ctx->num_evt_ring);
  1975. if (props->intf == GSI_EVT_CHTYPE_GPI_EV)
  1976. gsi_prime_evt_ring(ctx);
  1977. else if (props->intf == GSI_EVT_CHTYPE_WDI2_EV)
  1978. gsi_prime_evt_ring_wdi(ctx);
  1979. mutex_unlock(&gsi_ctx->mlock);
  1980. spin_lock_irqsave(&gsi_ctx->slock, flags);
  1981. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1982. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k, ee,
  1983. gsihal_get_ch_reg_idx(evt_id), gsihal_get_ch_reg_mask(evt_id));
  1984. }
  1985. else {
  1986. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR, ee, 1 << evt_id);
  1987. }
  1988. /* enable ieob interrupts for GPI, enable MSI interrupts */
  1989. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  1990. if ((props->intf != GSI_EVT_CHTYPE_GPI_EV) &&
  1991. (props->intr != GSI_INTR_MSI))
  1992. __gsi_config_ieob_irq_k(gsi_ctx->per.ee, gsihal_get_ch_reg_idx(evt_id),
  1993. gsihal_get_ch_reg_mask(evt_id),
  1994. 0);
  1995. else
  1996. __gsi_config_ieob_irq_k(gsi_ctx->per.ee, gsihal_get_ch_reg_idx(evt_id),
  1997. gsihal_get_ch_reg_mask(evt_id),
  1998. ~0);
  1999. }
  2000. else {
  2001. if ((props->intf != GSI_EVT_CHTYPE_GPI_EV) &&
  2002. (props->intr != GSI_INTR_MSI))
  2003. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << evt_id, 0);
  2004. else
  2005. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->id, ~0);
  2006. }
  2007. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  2008. return GSI_STATUS_SUCCESS;
  2009. }
  2010. EXPORT_SYMBOL(gsi_alloc_evt_ring);
  2011. static void __gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  2012. union __packed gsi_evt_scratch val)
  2013. {
  2014. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_SCRATCH_0,
  2015. gsi_ctx->per.ee, evt_ring_hdl, val.data.word1);
  2016. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_SCRATCH_1,
  2017. gsi_ctx->per.ee, evt_ring_hdl, val.data.word2);
  2018. }
  2019. int gsi_write_evt_ring_scratch(unsigned long evt_ring_hdl,
  2020. union __packed gsi_evt_scratch val)
  2021. {
  2022. struct gsi_evt_ctx *ctx;
  2023. if (!gsi_ctx) {
  2024. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2025. return -GSI_STATUS_NODEV;
  2026. }
  2027. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2028. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2029. return -GSI_STATUS_INVALID_PARAMS;
  2030. }
  2031. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2032. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2033. GSIERR("bad state %d\n",
  2034. gsi_ctx->evtr[evt_ring_hdl].state);
  2035. return -GSI_STATUS_UNSUPPORTED_OP;
  2036. }
  2037. mutex_lock(&ctx->mlock);
  2038. ctx->scratch = val;
  2039. __gsi_write_evt_ring_scratch(evt_ring_hdl, val);
  2040. mutex_unlock(&ctx->mlock);
  2041. return GSI_STATUS_SUCCESS;
  2042. }
  2043. EXPORT_SYMBOL(gsi_write_evt_ring_scratch);
  2044. int gsi_dealloc_evt_ring(unsigned long evt_ring_hdl)
  2045. {
  2046. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  2047. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_DE_ALLOC;
  2048. struct gsi_evt_ctx *ctx;
  2049. int res = 0;
  2050. u32 msi;
  2051. if (!gsi_ctx) {
  2052. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2053. return -GSI_STATUS_NODEV;
  2054. }
  2055. if (evt_ring_hdl >= gsi_ctx->max_ev ||
  2056. evt_ring_hdl >= GSI_EVT_RING_MAX) {
  2057. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2058. return -GSI_STATUS_INVALID_PARAMS;
  2059. }
  2060. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2061. if (atomic_read(&ctx->chan_ref_cnt)) {
  2062. GSIERR("%d channels still using this event ring\n",
  2063. atomic_read(&ctx->chan_ref_cnt));
  2064. return -GSI_STATUS_UNSUPPORTED_OP;
  2065. }
  2066. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2067. GSIERR("bad state %d\n", ctx->state);
  2068. return -GSI_STATUS_UNSUPPORTED_OP;
  2069. }
  2070. /* Unpair the MSI */
  2071. if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2072. ctx->props.intr == GSI_INTR_MSI) {
  2073. GSIERR("Interrupt dereg for msi_irq = %d\n", ctx->props.msi_irq);
  2074. for (msi = 0; msi < gsi_ctx->msi.num; msi++) {
  2075. if (gsi_ctx->msi.msg[msi].data == ctx->props.intvec) {
  2076. mutex_lock(&gsi_ctx->mlock);
  2077. clear_bit(msi, gsi_ctx->msi.used);
  2078. gsi_ctx->msi.evt[msi] = 0;
  2079. clear_bit(evt_ring_hdl, &gsi_ctx->msi.mask);
  2080. mutex_unlock(&gsi_ctx->mlock);
  2081. }
  2082. }
  2083. }
  2084. mutex_lock(&gsi_ctx->mlock);
  2085. reinit_completion(&ctx->compl);
  2086. ev_ch_cmd.chid = evt_ring_hdl;
  2087. ev_ch_cmd.opcode = op;
  2088. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD,
  2089. gsi_ctx->per.ee, &ev_ch_cmd);
  2090. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2091. if (res == 0) {
  2092. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  2093. mutex_unlock(&gsi_ctx->mlock);
  2094. return -GSI_STATUS_TIMED_OUT;
  2095. }
  2096. if (ctx->state != GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  2097. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  2098. ctx->state);
  2099. /*
  2100. * IPA Hardware returned GSI RING not allocated, which is
  2101. * unexpected hardware state.
  2102. */
  2103. GSI_ASSERT();
  2104. }
  2105. mutex_unlock(&gsi_ctx->mlock);
  2106. if (!ctx->props.evchid_valid) {
  2107. mutex_lock(&gsi_ctx->mlock);
  2108. clear_bit(evt_ring_hdl, &gsi_ctx->evt_bmap);
  2109. mutex_unlock(&gsi_ctx->mlock);
  2110. }
  2111. atomic_dec(&gsi_ctx->num_evt_ring);
  2112. return GSI_STATUS_SUCCESS;
  2113. }
  2114. EXPORT_SYMBOL(gsi_dealloc_evt_ring);
  2115. int gsi_query_evt_ring_db_addr(unsigned long evt_ring_hdl,
  2116. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  2117. {
  2118. struct gsi_evt_ctx *ctx;
  2119. if (!gsi_ctx) {
  2120. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2121. return -GSI_STATUS_NODEV;
  2122. }
  2123. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  2124. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  2125. db_addr_wp_lsb);
  2126. return -GSI_STATUS_INVALID_PARAMS;
  2127. }
  2128. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2129. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2130. return -GSI_STATUS_INVALID_PARAMS;
  2131. }
  2132. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2133. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2134. GSIERR("bad state %d\n",
  2135. gsi_ctx->evtr[evt_ring_hdl].state);
  2136. return -GSI_STATUS_UNSUPPORTED_OP;
  2137. }
  2138. *db_addr_wp_lsb = gsi_ctx->per.phys_addr + gsihal_get_reg_nk_ofst(
  2139. GSI_EE_n_EV_CH_k_DOORBELL_0, gsi_ctx->per.ee, evt_ring_hdl);
  2140. *db_addr_wp_msb = gsi_ctx->per.phys_addr + gsihal_get_reg_nk_ofst(
  2141. GSI_EE_n_EV_CH_k_DOORBELL_1, gsi_ctx->per.ee, evt_ring_hdl);
  2142. return GSI_STATUS_SUCCESS;
  2143. }
  2144. EXPORT_SYMBOL(gsi_query_evt_ring_db_addr);
  2145. int gsi_ring_evt_ring_db(unsigned long evt_ring_hdl, uint64_t value)
  2146. {
  2147. struct gsi_evt_ctx *ctx;
  2148. if (!gsi_ctx) {
  2149. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2150. return -GSI_STATUS_NODEV;
  2151. }
  2152. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2153. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2154. return -GSI_STATUS_INVALID_PARAMS;
  2155. }
  2156. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2157. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2158. GSIERR("bad state %d\n",
  2159. gsi_ctx->evtr[evt_ring_hdl].state);
  2160. return -GSI_STATUS_UNSUPPORTED_OP;
  2161. }
  2162. ctx->ring.wp_local = value;
  2163. gsi_ring_evt_doorbell(ctx);
  2164. return GSI_STATUS_SUCCESS;
  2165. }
  2166. EXPORT_SYMBOL(gsi_ring_evt_ring_db);
  2167. int gsi_ring_ch_ring_db(unsigned long chan_hdl, uint64_t value)
  2168. {
  2169. struct gsi_chan_ctx *ctx;
  2170. if (!gsi_ctx) {
  2171. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2172. return -GSI_STATUS_NODEV;
  2173. }
  2174. if (chan_hdl >= gsi_ctx->max_ch) {
  2175. GSIERR("bad chan_hdl=%lu\n", chan_hdl);
  2176. return -GSI_STATUS_INVALID_PARAMS;
  2177. }
  2178. ctx = &gsi_ctx->chan[chan_hdl];
  2179. if (ctx->state != GSI_CHAN_STATE_STARTED) {
  2180. GSIERR("bad state %d\n", ctx->state);
  2181. return -GSI_STATUS_UNSUPPORTED_OP;
  2182. }
  2183. ctx->ring.wp_local = value;
  2184. /* write MSB first */
  2185. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2186. gsi_ctx->per.ee, ctx->props.ch_id, GSI_MSB(ctx->ring.wp_local));
  2187. gsi_ring_chan_doorbell(ctx);
  2188. return GSI_STATUS_SUCCESS;
  2189. }
  2190. EXPORT_SYMBOL(gsi_ring_ch_ring_db);
  2191. int gsi_reset_evt_ring(unsigned long evt_ring_hdl)
  2192. {
  2193. struct gsihal_reg_ee_n_ev_ch_cmd ev_ch_cmd;
  2194. enum gsi_evt_ch_cmd_opcode op = GSI_EVT_RESET;
  2195. struct gsi_evt_ctx *ctx;
  2196. int res;
  2197. if (!gsi_ctx) {
  2198. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2199. return -GSI_STATUS_NODEV;
  2200. }
  2201. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2202. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2203. return -GSI_STATUS_INVALID_PARAMS;
  2204. }
  2205. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2206. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2207. GSIERR("bad state %d\n", ctx->state);
  2208. return -GSI_STATUS_UNSUPPORTED_OP;
  2209. }
  2210. mutex_lock(&gsi_ctx->mlock);
  2211. reinit_completion(&ctx->compl);
  2212. ev_ch_cmd.chid = evt_ring_hdl;
  2213. ev_ch_cmd.opcode = op;
  2214. gsihal_write_reg_n_fields(GSI_EE_n_EV_CH_CMD,
  2215. gsi_ctx->per.ee, &ev_ch_cmd);
  2216. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2217. if (res == 0) {
  2218. GSIERR("evt_id=%lu timed out\n", evt_ring_hdl);
  2219. mutex_unlock(&gsi_ctx->mlock);
  2220. return -GSI_STATUS_TIMED_OUT;
  2221. }
  2222. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2223. GSIERR("evt_id=%lu unexpected state=%u\n", evt_ring_hdl,
  2224. ctx->state);
  2225. /*
  2226. * IPA Hardware returned GSI RING not allocated, which is
  2227. * unexpected. Indicates hardware instability.
  2228. */
  2229. GSI_ASSERT();
  2230. }
  2231. gsi_program_evt_ring_ctx(&ctx->props, evt_ring_hdl, gsi_ctx->per.ee);
  2232. gsi_init_evt_ring(&ctx->props, &ctx->ring);
  2233. /* restore scratch */
  2234. __gsi_write_evt_ring_scratch(evt_ring_hdl, ctx->scratch);
  2235. if (ctx->props.intf == GSI_EVT_CHTYPE_GPI_EV)
  2236. gsi_prime_evt_ring(ctx);
  2237. if (ctx->props.intf == GSI_EVT_CHTYPE_WDI2_EV)
  2238. gsi_prime_evt_ring_wdi(ctx);
  2239. mutex_unlock(&gsi_ctx->mlock);
  2240. return GSI_STATUS_SUCCESS;
  2241. }
  2242. EXPORT_SYMBOL(gsi_reset_evt_ring);
  2243. int gsi_get_evt_ring_cfg(unsigned long evt_ring_hdl,
  2244. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  2245. {
  2246. struct gsi_evt_ctx *ctx;
  2247. if (!gsi_ctx) {
  2248. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2249. return -GSI_STATUS_NODEV;
  2250. }
  2251. if (!props || !scr) {
  2252. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  2253. return -GSI_STATUS_INVALID_PARAMS;
  2254. }
  2255. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2256. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2257. return -GSI_STATUS_INVALID_PARAMS;
  2258. }
  2259. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2260. if (ctx->state == GSI_EVT_RING_STATE_NOT_ALLOCATED) {
  2261. GSIERR("bad state %d\n", ctx->state);
  2262. return -GSI_STATUS_UNSUPPORTED_OP;
  2263. }
  2264. mutex_lock(&ctx->mlock);
  2265. *props = ctx->props;
  2266. *scr = ctx->scratch;
  2267. mutex_unlock(&ctx->mlock);
  2268. return GSI_STATUS_SUCCESS;
  2269. }
  2270. EXPORT_SYMBOL(gsi_get_evt_ring_cfg);
  2271. int gsi_set_evt_ring_cfg(unsigned long evt_ring_hdl,
  2272. struct gsi_evt_ring_props *props, union gsi_evt_scratch *scr)
  2273. {
  2274. struct gsi_evt_ctx *ctx;
  2275. if (!gsi_ctx) {
  2276. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2277. return -GSI_STATUS_NODEV;
  2278. }
  2279. if (!props || gsi_validate_evt_ring_props(props)) {
  2280. GSIERR("bad params props=%pK\n", props);
  2281. return -GSI_STATUS_INVALID_PARAMS;
  2282. }
  2283. if (evt_ring_hdl >= gsi_ctx->max_ev) {
  2284. GSIERR("bad params evt_ring_hdl=%lu\n", evt_ring_hdl);
  2285. return -GSI_STATUS_INVALID_PARAMS;
  2286. }
  2287. ctx = &gsi_ctx->evtr[evt_ring_hdl];
  2288. if (ctx->state != GSI_EVT_RING_STATE_ALLOCATED) {
  2289. GSIERR("bad state %d\n", ctx->state);
  2290. return -GSI_STATUS_UNSUPPORTED_OP;
  2291. }
  2292. if (ctx->props.exclusive != props->exclusive) {
  2293. GSIERR("changing immutable fields not supported\n");
  2294. return -GSI_STATUS_UNSUPPORTED_OP;
  2295. }
  2296. mutex_lock(&ctx->mlock);
  2297. ctx->props = *props;
  2298. if (scr)
  2299. ctx->scratch = *scr;
  2300. mutex_unlock(&ctx->mlock);
  2301. return gsi_reset_evt_ring(evt_ring_hdl);
  2302. }
  2303. EXPORT_SYMBOL(gsi_set_evt_ring_cfg);
  2304. static void gsi_program_chan_ctx_qos(struct gsi_chan_props *props,
  2305. unsigned int ee)
  2306. {
  2307. struct gsihal_reg_gsi_ee_n_gsi_ch_k_qos ch_k_qos;
  2308. ch_k_qos.wrr_weight = props->low_weight;
  2309. ch_k_qos.max_prefetch = props->max_prefetch;
  2310. ch_k_qos.use_db_eng = props->use_db_eng;
  2311. if (gsi_ctx->per.ver >= GSI_VER_2_0) {
  2312. if (gsi_ctx->per.ver < GSI_VER_2_5) {
  2313. ch_k_qos.use_escape_buf_only = props->prefetch_mode;
  2314. } else {
  2315. ch_k_qos.prefetch_mode = props->prefetch_mode;
  2316. ch_k_qos.empty_lvl_thrshold =
  2317. props->empty_lvl_threshold;
  2318. if (gsi_ctx->per.ver >= GSI_VER_2_9)
  2319. ch_k_qos.db_in_bytes = props->db_in_bytes;
  2320. if (gsi_ctx->per.ver >= GSI_VER_3_0)
  2321. ch_k_qos.low_latency_en = props->low_latency_en;
  2322. }
  2323. }
  2324. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_QOS,
  2325. ee, props->ch_id, &ch_k_qos);
  2326. }
  2327. static void gsi_program_chan_ctx(struct gsi_chan_props *props, unsigned int ee,
  2328. uint8_t erindex)
  2329. {
  2330. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  2331. struct gsihal_reg_ch_k_cntxt_1 ch_k_cntxt_1;
  2332. switch (props->prot) {
  2333. case GSI_CHAN_PROT_MHI:
  2334. case GSI_CHAN_PROT_XHCI:
  2335. case GSI_CHAN_PROT_GPI:
  2336. case GSI_CHAN_PROT_XDCI:
  2337. case GSI_CHAN_PROT_WDI2:
  2338. case GSI_CHAN_PROT_WDI3:
  2339. case GSI_CHAN_PROT_GCI:
  2340. case GSI_CHAN_PROT_MHIP:
  2341. case GSI_CHAN_PROT_WDI3_V2:
  2342. ch_k_cntxt_0.chtype_protocol_msb = 0;
  2343. break;
  2344. case GSI_CHAN_PROT_AQC:
  2345. case GSI_CHAN_PROT_11AD:
  2346. case GSI_CHAN_PROT_RTK:
  2347. case GSI_CHAN_PROT_QDSS:
  2348. case GSI_CHAN_PROT_NTN:
  2349. ch_k_cntxt_0.chtype_protocol_msb = 1;
  2350. break;
  2351. default:
  2352. GSIERR("Unsupported protocol %d\n", props->prot);
  2353. WARN_ON(1);
  2354. return;
  2355. }
  2356. ch_k_cntxt_0.chtype_protocol = props->prot;
  2357. ch_k_cntxt_0.chtype_dir = props->dir;
  2358. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  2359. ch_k_cntxt_1.erindex = erindex;
  2360. } else {
  2361. ch_k_cntxt_0.erindex = erindex;
  2362. }
  2363. ch_k_cntxt_0.element_size = props->re_size;
  2364. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  2365. ee, props->ch_id, &ch_k_cntxt_0);
  2366. ch_k_cntxt_1.r_length = props->ring_len;
  2367. gsihal_write_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_1,
  2368. ee, props->ch_id, &ch_k_cntxt_1);
  2369. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  2370. ee, props->ch_id, GSI_LSB(props->ring_base_addr));
  2371. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  2372. ee, props->ch_id, GSI_MSB(props->ring_base_addr));
  2373. gsi_program_chan_ctx_qos(props, ee);
  2374. }
  2375. static void gsi_init_chan_ring(struct gsi_chan_props *props,
  2376. struct gsi_ring_ctx *ctx)
  2377. {
  2378. ctx->base_va = (uintptr_t)props->ring_base_vaddr;
  2379. ctx->base = props->ring_base_addr;
  2380. ctx->wp = ctx->base;
  2381. ctx->rp = ctx->base;
  2382. ctx->wp_local = ctx->base;
  2383. ctx->rp_local = ctx->base;
  2384. ctx->len = props->ring_len;
  2385. ctx->elem_sz = props->re_size;
  2386. ctx->max_num_elem = ctx->len / ctx->elem_sz - 1;
  2387. ctx->end = ctx->base + (ctx->max_num_elem + 1) *
  2388. ctx->elem_sz;
  2389. }
  2390. static int gsi_validate_channel_props(struct gsi_chan_props *props)
  2391. {
  2392. uint64_t ra;
  2393. uint64_t last;
  2394. if (props->ch_id >= gsi_ctx->max_ch) {
  2395. GSIERR("ch_id %u invalid\n", props->ch_id);
  2396. return -GSI_STATUS_INVALID_PARAMS;
  2397. }
  2398. if ((props->re_size == GSI_CHAN_RE_SIZE_4B &&
  2399. props->ring_len % 4) ||
  2400. (props->re_size == GSI_CHAN_RE_SIZE_8B &&
  2401. props->ring_len % 8) ||
  2402. (props->re_size == GSI_CHAN_RE_SIZE_16B &&
  2403. props->ring_len % 16) ||
  2404. (props->re_size == GSI_CHAN_RE_SIZE_32B &&
  2405. props->ring_len % 32) ||
  2406. (props->re_size == GSI_CHAN_RE_SIZE_64B &&
  2407. props->ring_len % 64)) {
  2408. GSIERR("bad params ring_len %u not a multiple of re size %u\n",
  2409. props->ring_len, props->re_size);
  2410. return -GSI_STATUS_INVALID_PARAMS;
  2411. }
  2412. if (!gsihal_check_ring_length_valid(props->ring_len, props->re_size))
  2413. return -GSI_STATUS_INVALID_PARAMS;
  2414. ra = props->ring_base_addr;
  2415. do_div(ra, roundup_pow_of_two(props->ring_len));
  2416. if (props->ring_base_addr != ra * roundup_pow_of_two(props->ring_len)) {
  2417. GSIERR("bad params ring base not aligned 0x%llx align 0x%lx\n",
  2418. props->ring_base_addr,
  2419. roundup_pow_of_two(props->ring_len));
  2420. return -GSI_STATUS_INVALID_PARAMS;
  2421. }
  2422. last = props->ring_base_addr + props->ring_len - props->re_size;
  2423. /* MSB should stay same within the ring */
  2424. if ((props->ring_base_addr & 0xFFFFFFFF00000000ULL) !=
  2425. (last & 0xFFFFFFFF00000000ULL)) {
  2426. GSIERR("MSB is not fixed on ring base 0x%llx size 0x%x\n",
  2427. props->ring_base_addr,
  2428. props->ring_len);
  2429. return -GSI_STATUS_INVALID_PARAMS;
  2430. }
  2431. if (props->prot == GSI_CHAN_PROT_GPI &&
  2432. !props->ring_base_vaddr) {
  2433. GSIERR("protocol %u requires ring base VA\n", props->prot);
  2434. return -GSI_STATUS_INVALID_PARAMS;
  2435. }
  2436. if (props->low_weight > GSI_MAX_CH_LOW_WEIGHT) {
  2437. GSIERR("invalid channel low weight %u\n", props->low_weight);
  2438. return -GSI_STATUS_INVALID_PARAMS;
  2439. }
  2440. if (props->prot == GSI_CHAN_PROT_GPI && !props->xfer_cb) {
  2441. GSIERR("xfer callback must be provided\n");
  2442. return -GSI_STATUS_INVALID_PARAMS;
  2443. }
  2444. if (!props->err_cb) {
  2445. GSIERR("err callback must be provided\n");
  2446. return -GSI_STATUS_INVALID_PARAMS;
  2447. }
  2448. return GSI_STATUS_SUCCESS;
  2449. }
  2450. int gsi_alloc_channel(struct gsi_chan_props *props, unsigned long dev_hdl,
  2451. unsigned long *chan_hdl)
  2452. {
  2453. struct gsi_chan_ctx *ctx;
  2454. int res;
  2455. int ee;
  2456. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  2457. uint8_t erindex;
  2458. struct gsi_user_data *user_data;
  2459. size_t user_data_size;
  2460. if (!gsi_ctx) {
  2461. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2462. return -GSI_STATUS_NODEV;
  2463. }
  2464. if (!props || !chan_hdl || dev_hdl != (uintptr_t)gsi_ctx) {
  2465. GSIERR("bad params props=%pK dev_hdl=0x%lx chan_hdl=%pK\n",
  2466. props, dev_hdl, chan_hdl);
  2467. return -GSI_STATUS_INVALID_PARAMS;
  2468. }
  2469. if (gsi_validate_channel_props(props)) {
  2470. GSIERR("bad params\n");
  2471. return -GSI_STATUS_INVALID_PARAMS;
  2472. }
  2473. if (props->evt_ring_hdl != ~0) {
  2474. if (props->evt_ring_hdl >= gsi_ctx->max_ev) {
  2475. GSIERR("invalid evt ring=%lu\n", props->evt_ring_hdl);
  2476. return -GSI_STATUS_INVALID_PARAMS;
  2477. }
  2478. if (atomic_read(
  2479. &gsi_ctx->evtr[props->evt_ring_hdl].chan_ref_cnt) &&
  2480. gsi_ctx->evtr[props->evt_ring_hdl].props.exclusive &&
  2481. gsi_ctx->evtr[props->evt_ring_hdl].chan[0]->props.prot !=
  2482. GSI_CHAN_PROT_GCI) {
  2483. GSIERR("evt ring=%lu exclusively used by ch_hdl=%pK\n",
  2484. props->evt_ring_hdl, chan_hdl);
  2485. return -GSI_STATUS_UNSUPPORTED_OP;
  2486. }
  2487. }
  2488. ctx = &gsi_ctx->chan[props->ch_id];
  2489. if (ctx->allocated) {
  2490. GSIERR("chan %d already allocated\n", props->ch_id);
  2491. return -GSI_STATUS_NODEV;
  2492. }
  2493. memset(ctx, 0, sizeof(*ctx));
  2494. /* For IPA offloaded WDI channels not required user_data pointer */
  2495. if (props->prot != GSI_CHAN_PROT_WDI2 &&
  2496. props->prot != GSI_CHAN_PROT_WDI3)
  2497. user_data_size = props->ring_len / props->re_size;
  2498. else
  2499. user_data_size = props->re_size;
  2500. /*
  2501. * GCI channels might have OOO event completions up to GSI_VEID_MAX.
  2502. * user_data needs to be large enough to accommodate those.
  2503. * TODO: increase user data size if GSI_VEID_MAX is not enough
  2504. */
  2505. if (props->prot == GSI_CHAN_PROT_GCI)
  2506. user_data_size += GSI_VEID_MAX;
  2507. user_data = devm_kzalloc(gsi_ctx->dev,
  2508. user_data_size * sizeof(*user_data),
  2509. GFP_KERNEL);
  2510. if (user_data == NULL) {
  2511. GSIERR("context not allocated\n");
  2512. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2513. }
  2514. mutex_init(&ctx->mlock);
  2515. init_completion(&ctx->compl);
  2516. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2517. ctx->props = *props;
  2518. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  2519. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2520. mutex_lock(&gsi_ctx->mlock);
  2521. ee = gsi_ctx->per.ee;
  2522. gsi_ctx->ch_dbg[props->ch_id].ch_allocate++;
  2523. ch_cmd.chid = props->ch_id;
  2524. ch_cmd.opcode = op;
  2525. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD, ee, &ch_cmd);
  2526. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2527. if (res == 0) {
  2528. GSIERR("chan_hdl=%u timed out\n", props->ch_id);
  2529. mutex_unlock(&gsi_ctx->mlock);
  2530. devm_kfree(gsi_ctx->dev, user_data);
  2531. return -GSI_STATUS_TIMED_OUT;
  2532. }
  2533. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2534. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2535. props->ch_id, ctx->state);
  2536. mutex_unlock(&gsi_ctx->mlock);
  2537. devm_kfree(gsi_ctx->dev, user_data);
  2538. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2539. }
  2540. mutex_unlock(&gsi_ctx->mlock);
  2541. } else {
  2542. mutex_lock(&gsi_ctx->mlock);
  2543. ctx->state = GSI_CHAN_STATE_ALLOCATED;
  2544. mutex_unlock(&gsi_ctx->mlock);
  2545. }
  2546. erindex = props->evt_ring_hdl != ~0 ? props->evt_ring_hdl :
  2547. GSI_NO_EVT_ERINDEX;
  2548. if (erindex != GSI_NO_EVT_ERINDEX && erindex >= GSI_EVT_RING_MAX) {
  2549. GSIERR("invalid erindex %u\n", erindex);
  2550. devm_kfree(gsi_ctx->dev, user_data);
  2551. return -GSI_STATUS_INVALID_PARAMS;
  2552. }
  2553. if (erindex < GSI_EVT_RING_MAX) {
  2554. ctx->evtr = &gsi_ctx->evtr[erindex];
  2555. if(ctx->evtr->num_of_chan_allocated
  2556. >= MAX_CHANNELS_SHARING_EVENT_RING) {
  2557. GSIERR(
  2558. "too many channels sharing the same event ring %u\n",
  2559. erindex);
  2560. GSI_ASSERT();
  2561. }
  2562. if (props->prot != GSI_CHAN_PROT_GCI) {
  2563. atomic_inc(&ctx->evtr->chan_ref_cnt);
  2564. if (ctx->evtr->props.exclusive) {
  2565. if (atomic_read(&ctx->evtr->chan_ref_cnt) == 1)
  2566. ctx->evtr->chan
  2567. [ctx->evtr->num_of_chan_allocated++] = ctx;
  2568. }
  2569. else {
  2570. ctx->evtr->chan[ctx->evtr->num_of_chan_allocated++]
  2571. = ctx;
  2572. }
  2573. }
  2574. }
  2575. gsi_program_chan_ctx(props, gsi_ctx->per.ee, erindex);
  2576. spin_lock_init(&ctx->ring.slock);
  2577. gsi_init_chan_ring(props, &ctx->ring);
  2578. if (!props->max_re_expected)
  2579. ctx->props.max_re_expected = ctx->ring.max_num_elem;
  2580. ctx->user_data = user_data;
  2581. *chan_hdl = props->ch_id;
  2582. ctx->allocated = true;
  2583. ctx->stats.dp.last_timestamp = jiffies_to_msecs(jiffies);
  2584. atomic_inc(&gsi_ctx->num_chan);
  2585. if (props->prot == GSI_CHAN_PROT_GCI) {
  2586. gsi_ctx->coal_info.ch_id = props->ch_id;
  2587. gsi_ctx->coal_info.evchid = props->evt_ring_hdl;
  2588. }
  2589. return GSI_STATUS_SUCCESS;
  2590. }
  2591. EXPORT_SYMBOL(gsi_alloc_channel);
  2592. static int gsi_alloc_ap_channel(unsigned int chan_hdl)
  2593. {
  2594. struct gsi_chan_ctx *ctx;
  2595. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2596. int res;
  2597. int ee;
  2598. enum gsi_ch_cmd_opcode op = GSI_CH_ALLOCATE;
  2599. if (!gsi_ctx) {
  2600. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2601. return -GSI_STATUS_NODEV;
  2602. }
  2603. ctx = &gsi_ctx->chan[chan_hdl];
  2604. if (ctx->allocated) {
  2605. GSIERR("chan %d already allocated\n", chan_hdl);
  2606. return -GSI_STATUS_NODEV;
  2607. }
  2608. memset(ctx, 0, sizeof(*ctx));
  2609. mutex_init(&ctx->mlock);
  2610. init_completion(&ctx->compl);
  2611. atomic_set(&ctx->poll_mode, GSI_CHAN_MODE_CALLBACK);
  2612. mutex_lock(&gsi_ctx->mlock);
  2613. ee = gsi_ctx->per.ee;
  2614. gsi_ctx->ch_dbg[chan_hdl].ch_allocate++;
  2615. ch_cmd.chid = chan_hdl;
  2616. ch_cmd.opcode = op;
  2617. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD, ee, &ch_cmd);
  2618. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  2619. if (res == 0) {
  2620. GSIERR("chan_hdl=%u timed out\n", chan_hdl);
  2621. mutex_unlock(&gsi_ctx->mlock);
  2622. return -GSI_STATUS_TIMED_OUT;
  2623. }
  2624. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  2625. GSIERR("chan_hdl=%u allocation failed state=%d\n",
  2626. chan_hdl, ctx->state);
  2627. mutex_unlock(&gsi_ctx->mlock);
  2628. return -GSI_STATUS_RES_ALLOC_FAILURE;
  2629. }
  2630. mutex_unlock(&gsi_ctx->mlock);
  2631. return GSI_STATUS_SUCCESS;
  2632. }
  2633. static void __gsi_write_channel_scratch(unsigned long chan_hdl,
  2634. union __packed gsi_channel_scratch val)
  2635. {
  2636. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2637. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2638. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2639. gsi_ctx->per.ee, chan_hdl, val.data.word2);
  2640. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2641. gsi_ctx->per.ee, chan_hdl, val.data.word3);
  2642. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2643. gsi_ctx->per.ee, chan_hdl, val.data.word4);
  2644. }
  2645. static void __gsi_write_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2646. union __packed gsi_wdi3_channel_scratch2_reg val)
  2647. {
  2648. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2649. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2650. }
  2651. int gsi_write_channel_scratch3_reg(unsigned long chan_hdl,
  2652. union __packed gsi_wdi_channel_scratch3_reg val)
  2653. {
  2654. struct gsi_chan_ctx *ctx;
  2655. if (!gsi_ctx) {
  2656. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2657. return -GSI_STATUS_NODEV;
  2658. }
  2659. if (chan_hdl >= gsi_ctx->max_ch) {
  2660. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2661. return -GSI_STATUS_INVALID_PARAMS;
  2662. }
  2663. ctx = &gsi_ctx->chan[chan_hdl];
  2664. mutex_lock(&ctx->mlock);
  2665. ctx->scratch.wdi.endp_metadatareg_offset =
  2666. val.wdi.endp_metadatareg_offset;
  2667. ctx->scratch.wdi.qmap_id = val.wdi.qmap_id;
  2668. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2669. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2670. mutex_unlock(&ctx->mlock);
  2671. return GSI_STATUS_SUCCESS;
  2672. }
  2673. EXPORT_SYMBOL(gsi_write_channel_scratch3_reg);
  2674. int gsi_write_channel_scratch2_reg(unsigned long chan_hdl,
  2675. union __packed gsi_wdi2_channel_scratch2_reg val)
  2676. {
  2677. struct gsi_chan_ctx *ctx;
  2678. if (!gsi_ctx) {
  2679. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2680. return -GSI_STATUS_NODEV;
  2681. }
  2682. if (chan_hdl >= gsi_ctx->max_ch) {
  2683. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2684. return -GSI_STATUS_INVALID_PARAMS;
  2685. }
  2686. ctx = &gsi_ctx->chan[chan_hdl];
  2687. mutex_lock(&ctx->mlock);
  2688. ctx->scratch.wdi2_new.endp_metadatareg_offset =
  2689. val.wdi.endp_metadatareg_offset;
  2690. ctx->scratch.wdi2_new.qmap_id = val.wdi.qmap_id;
  2691. val.wdi.update_ri_moderation_threshold =
  2692. ctx->scratch.wdi2_new.update_ri_moderation_threshold;
  2693. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2694. gsi_ctx->per.ee, chan_hdl, val.data.word1);
  2695. mutex_unlock(&ctx->mlock);
  2696. return GSI_STATUS_SUCCESS;
  2697. }
  2698. EXPORT_SYMBOL(gsi_write_channel_scratch2_reg);
  2699. static void __gsi_read_channel_scratch(unsigned long chan_hdl,
  2700. union __packed gsi_channel_scratch * val)
  2701. {
  2702. val->data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2703. gsi_ctx->per.ee, chan_hdl);
  2704. val->data.word2 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2705. gsi_ctx->per.ee, chan_hdl);
  2706. val->data.word3 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2707. gsi_ctx->per.ee, chan_hdl);
  2708. val->data.word4 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  2709. gsi_ctx->per.ee, chan_hdl);
  2710. }
  2711. static void __gsi_read_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2712. union __packed gsi_wdi3_channel_scratch2_reg * val)
  2713. {
  2714. val->data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2715. gsi_ctx->per.ee, chan_hdl);
  2716. }
  2717. int gsi_write_channel_scratch(unsigned long chan_hdl,
  2718. union __packed gsi_channel_scratch val)
  2719. {
  2720. struct gsi_chan_ctx *ctx;
  2721. if (!gsi_ctx) {
  2722. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2723. return -GSI_STATUS_NODEV;
  2724. }
  2725. if (chan_hdl >= gsi_ctx->max_ch) {
  2726. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2727. return -GSI_STATUS_INVALID_PARAMS;
  2728. }
  2729. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2730. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2731. GSIERR("bad state %d\n",
  2732. gsi_ctx->chan[chan_hdl].state);
  2733. return -GSI_STATUS_UNSUPPORTED_OP;
  2734. }
  2735. ctx = &gsi_ctx->chan[chan_hdl];
  2736. mutex_lock(&ctx->mlock);
  2737. ctx->scratch = val;
  2738. __gsi_write_channel_scratch(chan_hdl, val);
  2739. mutex_unlock(&ctx->mlock);
  2740. return GSI_STATUS_SUCCESS;
  2741. }
  2742. EXPORT_SYMBOL(gsi_write_channel_scratch);
  2743. int gsi_write_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2744. union __packed gsi_wdi3_channel_scratch2_reg val)
  2745. {
  2746. struct gsi_chan_ctx *ctx;
  2747. if (!gsi_ctx) {
  2748. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2749. return -GSI_STATUS_NODEV;
  2750. }
  2751. if (chan_hdl >= gsi_ctx->max_ch) {
  2752. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2753. return -GSI_STATUS_INVALID_PARAMS;
  2754. }
  2755. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2756. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2757. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2758. GSIERR("bad state %d\n",
  2759. gsi_ctx->chan[chan_hdl].state);
  2760. return -GSI_STATUS_UNSUPPORTED_OP;
  2761. }
  2762. ctx = &gsi_ctx->chan[chan_hdl];
  2763. mutex_lock(&ctx->mlock);
  2764. ctx->scratch.data.word3 = val.data.word1;
  2765. __gsi_write_wdi3_channel_scratch2_reg(chan_hdl, val);
  2766. mutex_unlock(&ctx->mlock);
  2767. return GSI_STATUS_SUCCESS;
  2768. }
  2769. EXPORT_SYMBOL(gsi_write_wdi3_channel_scratch2_reg);
  2770. int gsi_read_channel_scratch(unsigned long chan_hdl,
  2771. union __packed gsi_channel_scratch *val)
  2772. {
  2773. struct gsi_chan_ctx *ctx;
  2774. if (!gsi_ctx) {
  2775. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2776. return -GSI_STATUS_NODEV;
  2777. }
  2778. if (chan_hdl >= gsi_ctx->max_ch) {
  2779. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2780. return -GSI_STATUS_INVALID_PARAMS;
  2781. }
  2782. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2783. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2784. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2785. GSIERR("bad state %d\n",
  2786. gsi_ctx->chan[chan_hdl].state);
  2787. return -GSI_STATUS_UNSUPPORTED_OP;
  2788. }
  2789. ctx = &gsi_ctx->chan[chan_hdl];
  2790. mutex_lock(&ctx->mlock);
  2791. __gsi_read_channel_scratch(chan_hdl, val);
  2792. mutex_unlock(&ctx->mlock);
  2793. return GSI_STATUS_SUCCESS;
  2794. }
  2795. EXPORT_SYMBOL(gsi_read_channel_scratch);
  2796. int gsi_read_wdi3_channel_scratch2_reg(unsigned long chan_hdl,
  2797. union __packed gsi_wdi3_channel_scratch2_reg * val)
  2798. {
  2799. struct gsi_chan_ctx *ctx;
  2800. if (!gsi_ctx) {
  2801. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2802. return -GSI_STATUS_NODEV;
  2803. }
  2804. if (chan_hdl >= gsi_ctx->max_ch) {
  2805. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2806. return -GSI_STATUS_INVALID_PARAMS;
  2807. }
  2808. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2809. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STARTED &&
  2810. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2811. GSIERR("bad state %d\n",
  2812. gsi_ctx->chan[chan_hdl].state);
  2813. return -GSI_STATUS_UNSUPPORTED_OP;
  2814. }
  2815. ctx = &gsi_ctx->chan[chan_hdl];
  2816. mutex_lock(&ctx->mlock);
  2817. __gsi_read_wdi3_channel_scratch2_reg(chan_hdl, val);
  2818. mutex_unlock(&ctx->mlock);
  2819. return GSI_STATUS_SUCCESS;
  2820. }
  2821. EXPORT_SYMBOL(gsi_read_wdi3_channel_scratch2_reg);
  2822. int gsi_update_mhi_channel_scratch(unsigned long chan_hdl,
  2823. struct __packed gsi_mhi_channel_scratch mscr)
  2824. {
  2825. struct gsi_chan_ctx *ctx;
  2826. if (!gsi_ctx) {
  2827. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2828. return -GSI_STATUS_NODEV;
  2829. }
  2830. if (chan_hdl >= gsi_ctx->max_ch) {
  2831. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2832. return -GSI_STATUS_INVALID_PARAMS;
  2833. }
  2834. if (gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_ALLOCATED &&
  2835. gsi_ctx->chan[chan_hdl].state != GSI_CHAN_STATE_STOPPED) {
  2836. GSIERR("bad state %d\n",
  2837. gsi_ctx->chan[chan_hdl].state);
  2838. return -GSI_STATUS_UNSUPPORTED_OP;
  2839. }
  2840. ctx = &gsi_ctx->chan[chan_hdl];
  2841. mutex_lock(&ctx->mlock);
  2842. ctx->scratch = __gsi_update_mhi_channel_scratch(chan_hdl, mscr);
  2843. mutex_unlock(&ctx->mlock);
  2844. return GSI_STATUS_SUCCESS;
  2845. }
  2846. EXPORT_SYMBOL(gsi_update_mhi_channel_scratch);
  2847. int gsi_query_channel_db_addr(unsigned long chan_hdl,
  2848. uint32_t *db_addr_wp_lsb, uint32_t *db_addr_wp_msb)
  2849. {
  2850. if (!gsi_ctx) {
  2851. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2852. return -GSI_STATUS_NODEV;
  2853. }
  2854. if (!db_addr_wp_msb || !db_addr_wp_lsb) {
  2855. GSIERR("bad params msb=%pK lsb=%pK\n", db_addr_wp_msb,
  2856. db_addr_wp_lsb);
  2857. return -GSI_STATUS_INVALID_PARAMS;
  2858. }
  2859. if (chan_hdl >= gsi_ctx->max_ch) {
  2860. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2861. return -GSI_STATUS_INVALID_PARAMS;
  2862. }
  2863. if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  2864. GSIERR("bad state %d\n",
  2865. gsi_ctx->chan[chan_hdl].state);
  2866. return -GSI_STATUS_UNSUPPORTED_OP;
  2867. }
  2868. *db_addr_wp_lsb = gsi_ctx->per.phys_addr +
  2869. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_DOORBELL_0,
  2870. gsi_ctx->per.ee, chan_hdl);
  2871. *db_addr_wp_msb = gsi_ctx->per.phys_addr +
  2872. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2873. gsi_ctx->per.ee, chan_hdl);
  2874. return GSI_STATUS_SUCCESS;
  2875. }
  2876. EXPORT_SYMBOL(gsi_query_channel_db_addr);
  2877. int gsi_pending_irq_type(void)
  2878. {
  2879. int ee = gsi_ctx->per.ee;
  2880. return gsihal_read_reg_n(GSI_EE_n_CNTXT_TYPE_IRQ, ee);
  2881. }
  2882. EXPORT_SYMBOL(gsi_pending_irq_type);
  2883. int gsi_start_channel(unsigned long chan_hdl)
  2884. {
  2885. enum gsi_ch_cmd_opcode op = GSI_CH_START;
  2886. uint32_t val;
  2887. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  2888. struct gsi_chan_ctx *ctx;
  2889. if (!gsi_ctx) {
  2890. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2891. return -GSI_STATUS_NODEV;
  2892. }
  2893. if (chan_hdl >= gsi_ctx->max_ch) {
  2894. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  2895. return -GSI_STATUS_INVALID_PARAMS;
  2896. }
  2897. ctx = &gsi_ctx->chan[chan_hdl];
  2898. if (ctx->state != GSI_CHAN_STATE_ALLOCATED &&
  2899. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  2900. ctx->state != GSI_CHAN_STATE_STOPPED) {
  2901. GSIERR("bad state %d\n", ctx->state);
  2902. return -GSI_STATUS_UNSUPPORTED_OP;
  2903. }
  2904. mutex_lock(&gsi_ctx->mlock);
  2905. reinit_completion(&ctx->compl);
  2906. /* check if INTSET is in IRQ mode for GPI channel */
  2907. val = gsihal_read_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee);
  2908. if (ctx->evtr &&
  2909. ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  2910. val != GSI_INTR_IRQ) {
  2911. GSIERR("GSI_EE_n_CNTXT_INTSET %d\n", val);
  2912. BUG();
  2913. }
  2914. gsi_ctx->ch_dbg[chan_hdl].ch_start++;
  2915. ch_cmd.chid = chan_hdl;
  2916. ch_cmd.opcode = op;
  2917. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  2918. gsi_ctx->per.ee, &ch_cmd);
  2919. GSIDBG("GSI Channel Start, waiting for completion\n");
  2920. gsi_channel_state_change_wait(chan_hdl,
  2921. ctx,
  2922. GSI_START_CMD_TIMEOUT_MS, op);
  2923. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  2924. ctx->state != GSI_CHAN_STATE_FLOW_CONTROL) {
  2925. /*
  2926. * Hardware returned unexpected status, unexpected
  2927. * hardware state.
  2928. */
  2929. GSIERR("chan=%lu timed out, unexpected state=%u\n",
  2930. chan_hdl, ctx->state);
  2931. gsi_dump_ch_info(chan_hdl);
  2932. GSI_ASSERT();
  2933. }
  2934. GSIDBG("GSI Channel=%lu Start success\n", chan_hdl);
  2935. /* write order MUST be MSB followed by LSB */
  2936. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_DOORBELL_1,
  2937. gsi_ctx->per.ee, ctx->props.ch_id, GSI_MSB(ctx->ring.wp_local));
  2938. mutex_unlock(&gsi_ctx->mlock);
  2939. return GSI_STATUS_SUCCESS;
  2940. }
  2941. EXPORT_SYMBOL(gsi_start_channel);
  2942. void gsi_dump_ch_info(unsigned long chan_hdl)
  2943. {
  2944. uint32_t val;
  2945. if (!gsi_ctx) {
  2946. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  2947. return;
  2948. }
  2949. if (chan_hdl >= gsi_ctx->max_ch) {
  2950. GSIDBG("invalid chan id %u\n", chan_hdl);
  2951. return;
  2952. }
  2953. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_0,
  2954. gsi_ctx->per.ee, chan_hdl);
  2955. GSIERR("CH%2d CTX0 0x%x\n", chan_hdl, val);
  2956. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_1,
  2957. gsi_ctx->per.ee, chan_hdl);
  2958. GSIERR("CH%2d CTX1 0x%x\n", chan_hdl, val);
  2959. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  2960. gsi_ctx->per.ee, chan_hdl);
  2961. GSIERR("CH%2d CTX2 0x%x\n", chan_hdl, val);
  2962. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  2963. gsi_ctx->per.ee, chan_hdl);
  2964. GSIERR("CH%2d CTX3 0x%x\n", chan_hdl, val);
  2965. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  2966. gsi_ctx->per.ee, chan_hdl);
  2967. GSIERR("CH%2d CTX4 0x%x\n", chan_hdl, val);
  2968. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  2969. gsi_ctx->per.ee, chan_hdl);
  2970. GSIERR("CH%2d CTX5 0x%x\n", chan_hdl, val);
  2971. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  2972. gsi_ctx->per.ee, chan_hdl);
  2973. GSIERR("CH%2d CTX6 0x%x\n", chan_hdl, val);
  2974. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  2975. gsi_ctx->per.ee, chan_hdl);
  2976. GSIERR("CH%2d CTX7 0x%x\n", chan_hdl, val);
  2977. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  2978. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_8,
  2979. gsi_ctx->per.ee, chan_hdl);
  2980. GSIERR("CH%2d CTX8 0x%x\n", chan_hdl, val);
  2981. }
  2982. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  2983. gsi_ctx->per.ee, chan_hdl);
  2984. GSIERR("CH%2d REFRP 0x%x\n", chan_hdl, val);
  2985. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  2986. gsi_ctx->per.ee, chan_hdl);
  2987. GSIERR("CH%2d REFWP 0x%x\n", chan_hdl, val);
  2988. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_QOS,
  2989. gsi_ctx->per.ee, chan_hdl);
  2990. GSIERR("CH%2d QOS 0x%x\n", chan_hdl, val);
  2991. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  2992. gsi_ctx->per.ee, chan_hdl);
  2993. GSIERR("CH%2d SCR0 0x%x\n", chan_hdl, val);
  2994. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  2995. gsi_ctx->per.ee, chan_hdl);
  2996. GSIERR("CH%2d SCR1 0x%x\n", chan_hdl, val);
  2997. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  2998. gsi_ctx->per.ee, chan_hdl);
  2999. GSIERR("CH%2d SCR2 0x%x\n", chan_hdl, val);
  3000. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  3001. gsi_ctx->per.ee, chan_hdl);
  3002. GSIERR("CH%2d SCR3 0x%x\n", chan_hdl, val);
  3003. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3004. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4,
  3005. gsi_ctx->per.ee, chan_hdl);
  3006. GSIERR("CH%2d SCR4 0x%x\n", chan_hdl, val);
  3007. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_5,
  3008. gsi_ctx->per.ee, chan_hdl);
  3009. GSIERR("CH%2d SCR5 0x%x\n", chan_hdl, val);
  3010. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_6,
  3011. gsi_ctx->per.ee, chan_hdl);
  3012. GSIERR("CH%2d SCR6 0x%x\n", chan_hdl, val);
  3013. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_7,
  3014. gsi_ctx->per.ee, chan_hdl);
  3015. GSIERR("CH%2d SCR7 0x%x\n", chan_hdl, val);
  3016. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_8,
  3017. gsi_ctx->per.ee, chan_hdl);
  3018. GSIERR("CH%2d SCR8 0x%x\n", chan_hdl, val);
  3019. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_9,
  3020. gsi_ctx->per.ee, chan_hdl);
  3021. GSIERR("CH%2d SCR9 0x%x\n", chan_hdl, val);
  3022. }
  3023. return;
  3024. }
  3025. EXPORT_SYMBOL(gsi_dump_ch_info);
  3026. int gsi_stop_channel(unsigned long chan_hdl)
  3027. {
  3028. enum gsi_ch_cmd_opcode op = GSI_CH_STOP;
  3029. int res;
  3030. uint32_t val;
  3031. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3032. struct gsi_chan_ctx *ctx;
  3033. unsigned long flags;
  3034. if (!gsi_ctx) {
  3035. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3036. return -GSI_STATUS_NODEV;
  3037. }
  3038. if (chan_hdl >= gsi_ctx->max_ch) {
  3039. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3040. return -GSI_STATUS_INVALID_PARAMS;
  3041. }
  3042. ctx = &gsi_ctx->chan[chan_hdl];
  3043. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  3044. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  3045. return GSI_STATUS_SUCCESS;
  3046. }
  3047. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  3048. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC &&
  3049. ctx->state != GSI_CHAN_STATE_ERROR) {
  3050. GSIERR("bad state %d\n", ctx->state);
  3051. return -GSI_STATUS_UNSUPPORTED_OP;
  3052. }
  3053. mutex_lock(&gsi_ctx->mlock);
  3054. reinit_completion(&ctx->compl);
  3055. /* check if INTSET is in IRQ mode for GPI channel */
  3056. val = gsihal_read_reg_n(GSI_EE_n_CNTXT_INTSET, gsi_ctx->per.ee);
  3057. if (ctx->evtr &&
  3058. ctx->evtr->props.intf == GSI_EVT_CHTYPE_GPI_EV &&
  3059. val != GSI_INTR_IRQ) {
  3060. GSIERR("GSI_EE_n_CNTXT_INTSET %d\n", val);
  3061. BUG();
  3062. }
  3063. gsi_ctx->ch_dbg[chan_hdl].ch_stop++;
  3064. ch_cmd.chid = chan_hdl;
  3065. ch_cmd.opcode = op;
  3066. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3067. gsi_ctx->per.ee, &ch_cmd);
  3068. GSIDBG("GSI Channel Stop, waiting for completion: 0x%x\n", val);
  3069. gsi_channel_state_change_wait(chan_hdl,
  3070. ctx,
  3071. GSI_STOP_CMD_TIMEOUT_MS, op);
  3072. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3073. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3074. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  3075. gsi_dump_ch_info(chan_hdl);
  3076. res = -GSI_STATUS_BAD_STATE;
  3077. BUG();
  3078. goto free_lock;
  3079. }
  3080. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  3081. GSIERR("chan=%lu busy try again\n", chan_hdl);
  3082. res = -GSI_STATUS_AGAIN;
  3083. goto free_lock;
  3084. }
  3085. /* If channel is stopped succesfully and has an event with IRQ type MSI
  3086. - clear IEOB */
  3087. if (ctx->evtr && ctx->evtr->props.intr == GSI_INTR_MSI) {
  3088. spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
  3089. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3090. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3091. gsi_ctx->per.ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3092. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3093. } else {
  3094. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3095. gsi_ctx->per.ee, 1 << ctx->evtr->id);
  3096. }
  3097. spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
  3098. }
  3099. res = GSI_STATUS_SUCCESS;
  3100. free_lock:
  3101. mutex_unlock(&gsi_ctx->mlock);
  3102. return res;
  3103. }
  3104. EXPORT_SYMBOL(gsi_stop_channel);
  3105. int gsi_stop_db_channel(unsigned long chan_hdl)
  3106. {
  3107. enum gsi_ch_cmd_opcode op = GSI_CH_DB_STOP;
  3108. int res;
  3109. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3110. struct gsi_chan_ctx *ctx;
  3111. if (!gsi_ctx) {
  3112. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3113. return -GSI_STATUS_NODEV;
  3114. }
  3115. if (chan_hdl >= gsi_ctx->max_ch) {
  3116. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3117. return -GSI_STATUS_INVALID_PARAMS;
  3118. }
  3119. ctx = &gsi_ctx->chan[chan_hdl];
  3120. if (ctx->state == GSI_CHAN_STATE_STOPPED) {
  3121. GSIDBG("chan_hdl=%lu already stopped\n", chan_hdl);
  3122. return GSI_STATUS_SUCCESS;
  3123. }
  3124. if (ctx->state != GSI_CHAN_STATE_STARTED &&
  3125. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3126. GSIERR("bad state %d\n", ctx->state);
  3127. return -GSI_STATUS_UNSUPPORTED_OP;
  3128. }
  3129. mutex_lock(&gsi_ctx->mlock);
  3130. reinit_completion(&ctx->compl);
  3131. gsi_ctx->ch_dbg[chan_hdl].ch_db_stop++;
  3132. ch_cmd.chid = chan_hdl;
  3133. ch_cmd.opcode = op;
  3134. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3135. gsi_ctx->per.ee, &ch_cmd);
  3136. res = wait_for_completion_timeout(&ctx->compl,
  3137. msecs_to_jiffies(GSI_STOP_CMD_TIMEOUT_MS));
  3138. if (res == 0) {
  3139. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3140. res = -GSI_STATUS_TIMED_OUT;
  3141. goto free_lock;
  3142. }
  3143. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3144. ctx->state != GSI_CHAN_STATE_STOP_IN_PROC) {
  3145. GSIERR("chan=%lu unexpected state=%u\n", chan_hdl, ctx->state);
  3146. res = -GSI_STATUS_BAD_STATE;
  3147. goto free_lock;
  3148. }
  3149. if (ctx->state == GSI_CHAN_STATE_STOP_IN_PROC) {
  3150. GSIERR("chan=%lu busy try again\n", chan_hdl);
  3151. res = -GSI_STATUS_AGAIN;
  3152. goto free_lock;
  3153. }
  3154. res = GSI_STATUS_SUCCESS;
  3155. free_lock:
  3156. mutex_unlock(&gsi_ctx->mlock);
  3157. return res;
  3158. }
  3159. EXPORT_SYMBOL(gsi_stop_db_channel);
  3160. int gsi_reset_channel(unsigned long chan_hdl)
  3161. {
  3162. enum gsi_ch_cmd_opcode op = GSI_CH_RESET;
  3163. int res;
  3164. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3165. struct gsi_chan_ctx *ctx;
  3166. bool reset_done = false;
  3167. uint32_t retry_cnt = 0;
  3168. if (!gsi_ctx) {
  3169. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3170. return -GSI_STATUS_NODEV;
  3171. }
  3172. if (chan_hdl >= gsi_ctx->max_ch) {
  3173. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3174. return -GSI_STATUS_INVALID_PARAMS;
  3175. }
  3176. ctx = &gsi_ctx->chan[chan_hdl];
  3177. /*
  3178. * In WDI3 case, if SAP enabled but no client connected,
  3179. * GSI will be in allocated state. When SAP disabled,
  3180. * gsi_reset_channel will be called and reset is needed.
  3181. */
  3182. if (ctx->state != GSI_CHAN_STATE_STOPPED &&
  3183. ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3184. GSIERR("bad state %d\n", ctx->state);
  3185. return -GSI_STATUS_UNSUPPORTED_OP;
  3186. }
  3187. mutex_lock(&gsi_ctx->mlock);
  3188. reset:
  3189. reinit_completion(&ctx->compl);
  3190. gsi_ctx->ch_dbg[chan_hdl].ch_reset++;
  3191. ch_cmd.chid = chan_hdl;
  3192. ch_cmd.opcode = op;
  3193. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3194. gsi_ctx->per.ee, &ch_cmd);
  3195. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  3196. if (res == 0) {
  3197. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3198. mutex_unlock(&gsi_ctx->mlock);
  3199. return -GSI_STATUS_TIMED_OUT;
  3200. }
  3201. revrfy_chnlstate:
  3202. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3203. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  3204. ctx->state);
  3205. /* GSI register update state not sync with gsi channel
  3206. * context state not sync, need to wait for 1ms to sync.
  3207. */
  3208. retry_cnt++;
  3209. if (retry_cnt <= GSI_CHNL_STATE_MAX_RETRYCNT) {
  3210. usleep_range(GSI_RESET_WA_MIN_SLEEP,
  3211. GSI_RESET_WA_MAX_SLEEP);
  3212. goto revrfy_chnlstate;
  3213. }
  3214. /*
  3215. * Hardware returned incorrect state, unexpected
  3216. * hardware state.
  3217. */
  3218. GSI_ASSERT();
  3219. }
  3220. /* Hardware issue fixed from GSI 2.0 and no need for the WA */
  3221. if (gsi_ctx->per.ver >= GSI_VER_2_0)
  3222. reset_done = true;
  3223. /* workaround: reset GSI producers again */
  3224. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && !reset_done) {
  3225. usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
  3226. reset_done = true;
  3227. goto reset;
  3228. }
  3229. if (ctx->props.cleanup_cb)
  3230. gsi_cleanup_xfer_user_data(chan_hdl, ctx->props.cleanup_cb);
  3231. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  3232. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  3233. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  3234. /* restore scratch */
  3235. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  3236. mutex_unlock(&gsi_ctx->mlock);
  3237. return GSI_STATUS_SUCCESS;
  3238. }
  3239. EXPORT_SYMBOL(gsi_reset_channel);
  3240. int gsi_dealloc_channel(unsigned long chan_hdl)
  3241. {
  3242. enum gsi_ch_cmd_opcode op = GSI_CH_DE_ALLOC;
  3243. int res;
  3244. struct gsihal_reg_ee_n_gsi_ch_cmd ch_cmd;
  3245. struct gsi_chan_ctx *ctx;
  3246. if (!gsi_ctx) {
  3247. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3248. return -GSI_STATUS_NODEV;
  3249. }
  3250. if (chan_hdl >= gsi_ctx->max_ch) {
  3251. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3252. return -GSI_STATUS_INVALID_PARAMS;
  3253. }
  3254. ctx = &gsi_ctx->chan[chan_hdl];
  3255. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3256. GSIERR("bad state %d\n", ctx->state);
  3257. return -GSI_STATUS_UNSUPPORTED_OP;
  3258. }
  3259. /*In GSI_VER_2_2 version deallocation channel not supported*/
  3260. if (gsi_ctx->per.ver != GSI_VER_2_2) {
  3261. mutex_lock(&gsi_ctx->mlock);
  3262. reinit_completion(&ctx->compl);
  3263. gsi_ctx->ch_dbg[chan_hdl].ch_de_alloc++;
  3264. ch_cmd.chid = chan_hdl;
  3265. ch_cmd.opcode = op;
  3266. gsihal_write_reg_n_fields(GSI_EE_n_GSI_CH_CMD,
  3267. gsi_ctx->per.ee, &ch_cmd);
  3268. res = wait_for_completion_timeout(&ctx->compl, GSI_CMD_TIMEOUT);
  3269. if (res == 0) {
  3270. GSIERR("chan_hdl=%lu timed out\n", chan_hdl);
  3271. mutex_unlock(&gsi_ctx->mlock);
  3272. return -GSI_STATUS_TIMED_OUT;
  3273. }
  3274. if (ctx->state != GSI_CHAN_STATE_NOT_ALLOCATED) {
  3275. GSIERR("chan_hdl=%lu unexpected state=%u\n", chan_hdl,
  3276. ctx->state);
  3277. /* Hardware returned incorrect value */
  3278. GSI_ASSERT();
  3279. }
  3280. mutex_unlock(&gsi_ctx->mlock);
  3281. } else {
  3282. mutex_lock(&gsi_ctx->mlock);
  3283. GSIDBG("In GSI_VER_2_2 channel deallocation not supported\n");
  3284. ctx->state = GSI_CHAN_STATE_NOT_ALLOCATED;
  3285. GSIDBG("chan_hdl=%lu Channel state = %u\n", chan_hdl,
  3286. ctx->state);
  3287. mutex_unlock(&gsi_ctx->mlock);
  3288. }
  3289. devm_kfree(gsi_ctx->dev, ctx->user_data);
  3290. ctx->allocated = false;
  3291. if (ctx->evtr && (ctx->props.prot != GSI_CHAN_PROT_GCI)) {
  3292. atomic_dec(&ctx->evtr->chan_ref_cnt);
  3293. ctx->evtr->num_of_chan_allocated--;
  3294. }
  3295. atomic_dec(&gsi_ctx->num_chan);
  3296. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  3297. gsi_ctx->coal_info.ch_id = GSI_CHAN_MAX;
  3298. gsi_ctx->coal_info.evchid = GSI_EVT_RING_MAX;
  3299. }
  3300. return GSI_STATUS_SUCCESS;
  3301. }
  3302. EXPORT_SYMBOL(gsi_dealloc_channel);
  3303. void gsi_update_ch_dp_stats(struct gsi_chan_ctx *ctx, uint16_t used)
  3304. {
  3305. unsigned long now = jiffies_to_msecs(jiffies);
  3306. unsigned long elapsed;
  3307. if (used == 0) {
  3308. elapsed = now - ctx->stats.dp.last_timestamp;
  3309. if (ctx->stats.dp.empty_time < elapsed)
  3310. ctx->stats.dp.empty_time = elapsed;
  3311. }
  3312. if (used <= ctx->props.max_re_expected / 3)
  3313. ++ctx->stats.dp.ch_below_lo;
  3314. else if (used <= 2 * ctx->props.max_re_expected / 3)
  3315. ++ctx->stats.dp.ch_below_hi;
  3316. else
  3317. ++ctx->stats.dp.ch_above_hi;
  3318. ctx->stats.dp.last_timestamp = now;
  3319. }
  3320. static void __gsi_query_channel_free_re(struct gsi_chan_ctx *ctx,
  3321. uint16_t *num_free_re)
  3322. {
  3323. uint16_t start;
  3324. uint16_t end;
  3325. uint64_t rp;
  3326. int ee = gsi_ctx->per.ee;
  3327. uint16_t used;
  3328. WARN_ON(ctx->props.prot != GSI_CHAN_PROT_GPI);
  3329. if (!ctx->evtr) {
  3330. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3331. ee, ctx->props.ch_id);
  3332. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3333. ctx->ring.rp = rp;
  3334. } else {
  3335. rp = ctx->ring.rp_local;
  3336. }
  3337. start = gsi_find_idx_from_addr(&ctx->ring, rp);
  3338. end = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3339. if (end >= start)
  3340. used = end - start;
  3341. else
  3342. used = ctx->ring.max_num_elem + 1 - (start - end);
  3343. *num_free_re = ctx->ring.max_num_elem - used;
  3344. }
  3345. int gsi_query_channel_info(unsigned long chan_hdl,
  3346. struct gsi_chan_info *info)
  3347. {
  3348. struct gsi_chan_ctx *ctx;
  3349. spinlock_t *slock;
  3350. unsigned long flags;
  3351. uint64_t rp;
  3352. uint64_t wp;
  3353. int ee;
  3354. if (!gsi_ctx) {
  3355. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3356. return -GSI_STATUS_NODEV;
  3357. }
  3358. if (chan_hdl >= gsi_ctx->max_ch || !info) {
  3359. GSIERR("bad params chan_hdl=%lu info=%pK\n", chan_hdl, info);
  3360. return -GSI_STATUS_INVALID_PARAMS;
  3361. }
  3362. ctx = &gsi_ctx->chan[chan_hdl];
  3363. if (ctx->evtr) {
  3364. slock = &ctx->evtr->ring.slock;
  3365. info->evt_valid = true;
  3366. } else {
  3367. slock = &ctx->ring.slock;
  3368. info->evt_valid = false;
  3369. }
  3370. spin_lock_irqsave(slock, flags);
  3371. ee = gsi_ctx->per.ee;
  3372. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3373. ee, ctx->props.ch_id);
  3374. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  3375. ee, ctx->props.ch_id)) << 32;
  3376. ctx->ring.rp = rp;
  3377. info->rp = rp;
  3378. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  3379. ee, ctx->props.ch_id);
  3380. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  3381. ee, ctx->props.ch_id)) << 32;
  3382. ctx->ring.wp = wp;
  3383. info->wp = wp;
  3384. if (info->evt_valid) {
  3385. rp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_4,
  3386. ee, ctx->evtr->id);
  3387. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_5,
  3388. ee, ctx->evtr->id)) << 32;
  3389. info->evt_rp = rp;
  3390. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  3391. ee, ctx->evtr->id);
  3392. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_7,
  3393. ee, ctx->evtr->id)) << 32;
  3394. info->evt_wp = wp;
  3395. }
  3396. spin_unlock_irqrestore(slock, flags);
  3397. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx ev_valid=%d ERP=0x%llx EWP=0x%llx\n",
  3398. chan_hdl, info->rp, info->wp,
  3399. info->evt_valid, info->evt_rp, info->evt_wp);
  3400. return GSI_STATUS_SUCCESS;
  3401. }
  3402. EXPORT_SYMBOL(gsi_query_channel_info);
  3403. int gsi_is_channel_empty(unsigned long chan_hdl, bool *is_empty)
  3404. {
  3405. struct gsi_chan_ctx *ctx;
  3406. struct gsi_evt_ctx *ev_ctx;
  3407. spinlock_t *slock;
  3408. unsigned long flags;
  3409. uint64_t rp;
  3410. uint64_t wp;
  3411. uint64_t rp_local;
  3412. int ee;
  3413. if (!gsi_ctx) {
  3414. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3415. return -GSI_STATUS_NODEV;
  3416. }
  3417. if (chan_hdl >= gsi_ctx->max_ch || !is_empty) {
  3418. GSIERR("bad params chan_hdl=%lu is_empty=%pK\n",
  3419. chan_hdl, is_empty);
  3420. return -GSI_STATUS_INVALID_PARAMS;
  3421. }
  3422. ctx = &gsi_ctx->chan[chan_hdl];
  3423. ee = gsi_ctx->per.ee;
  3424. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3425. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3426. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3427. return -GSI_STATUS_UNSUPPORTED_OP;
  3428. }
  3429. if (ctx->evtr)
  3430. slock = &ctx->evtr->ring.slock;
  3431. else
  3432. slock = &ctx->ring.slock;
  3433. spin_lock_irqsave(slock, flags);
  3434. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr) {
  3435. ev_ctx = &gsi_ctx->evtr[ctx->evtr->id];
  3436. /* Read the event ring rp from DDR to avoid mismatch */
  3437. rp = ev_ctx->props.gsi_read_event_ring_rp(&ev_ctx->props,
  3438. ev_ctx->id, ee);
  3439. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3440. ctx->evtr->ring.rp = rp;
  3441. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  3442. ee, ctx->evtr->id);
  3443. wp |= ctx->evtr->ring.wp & GSI_MSB_MASK;
  3444. ctx->evtr->ring.wp = wp;
  3445. rp_local = ctx->evtr->ring.rp_local;
  3446. } else {
  3447. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  3448. ee, ctx->props.ch_id);
  3449. rp |= ctx->ring.rp & GSI_MSB_MASK;
  3450. ctx->ring.rp = rp;
  3451. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  3452. ee, ctx->props.ch_id);
  3453. wp |= ctx->ring.wp & GSI_MSB_MASK;
  3454. ctx->ring.wp = wp;
  3455. rp_local = ctx->ring.rp_local;
  3456. }
  3457. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI)
  3458. *is_empty = (rp_local == rp) ? true : false;
  3459. else
  3460. *is_empty = (wp == rp) ? true : false;
  3461. spin_unlock_irqrestore(slock, flags);
  3462. if (ctx->props.dir == GSI_CHAN_DIR_FROM_GSI && ctx->evtr)
  3463. GSIDBG("ch=%ld ev=%d RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  3464. chan_hdl, ctx->evtr->id, rp, wp, rp_local);
  3465. else
  3466. GSIDBG("ch=%lu RP=0x%llx WP=0x%llx RP_LOCAL=0x%llx\n",
  3467. chan_hdl, rp, wp, rp_local);
  3468. return GSI_STATUS_SUCCESS;
  3469. }
  3470. EXPORT_SYMBOL(gsi_is_channel_empty);
  3471. bool gsi_is_event_pending(unsigned long chan_hdl) {
  3472. struct gsi_chan_ctx *ctx;
  3473. uint64_t rp;
  3474. uint64_t rp_local;
  3475. int ee;
  3476. if (chan_hdl >= gsi_ctx->max_ch) {
  3477. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3478. return false;
  3479. }
  3480. ctx = &gsi_ctx->chan[chan_hdl];
  3481. ee = gsi_ctx->per.ee;
  3482. /* read only, updating will be handled in NAPI context if needed */
  3483. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3484. &ctx->evtr->props, ctx->evtr->id, ee);
  3485. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3486. rp_local = ctx->evtr->ring.rp_local;
  3487. return rp != rp_local;
  3488. }
  3489. EXPORT_SYMBOL(gsi_is_event_pending);
  3490. int __gsi_get_gci_cookie(struct gsi_chan_ctx *ctx, uint16_t idx)
  3491. {
  3492. int i;
  3493. int end;
  3494. if (!ctx->user_data[idx].valid) {
  3495. ctx->user_data[idx].valid = true;
  3496. return idx;
  3497. }
  3498. /*
  3499. * at this point we need to find an "escape buffer" for the cookie
  3500. * as the userdata in this spot is in use. This happens if the TRE at
  3501. * idx is not completed yet and it is getting reused by a new TRE.
  3502. */
  3503. ctx->stats.userdata_in_use++;
  3504. end = ctx->ring.max_num_elem + 1;
  3505. for (i = 0; i < GSI_VEID_MAX; i++) {
  3506. if (!ctx->user_data[end + i].valid) {
  3507. ctx->user_data[end + i].valid = true;
  3508. return end + i;
  3509. }
  3510. }
  3511. /* Go over original userdata when escape buffer is full (costly) */
  3512. GSIDBG("escape buffer is full\n");
  3513. for (i = 0; i < end; i++) {
  3514. if (!ctx->user_data[i].valid) {
  3515. ctx->user_data[i].valid = true;
  3516. return i;
  3517. }
  3518. }
  3519. /* Everything is full (possibly a stall) */
  3520. GSIERR("both userdata array and escape buffer is full\n");
  3521. BUG();
  3522. return 0xFFFF;
  3523. }
  3524. int __gsi_populate_gci_tre(struct gsi_chan_ctx *ctx,
  3525. struct gsi_xfer_elem *xfer)
  3526. {
  3527. struct gsi_gci_tre gci_tre;
  3528. struct gsi_gci_tre *tre_gci_ptr;
  3529. uint16_t idx;
  3530. memset(&gci_tre, 0, sizeof(gci_tre));
  3531. if (xfer->addr & 0xFFFFFF0000000000) {
  3532. GSIERR("chan_hdl=%u add too large=%llx\n",
  3533. ctx->props.ch_id, xfer->addr);
  3534. return -EINVAL;
  3535. }
  3536. if (xfer->type != GSI_XFER_ELEM_DATA) {
  3537. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  3538. xfer->type);
  3539. return -EINVAL;
  3540. }
  3541. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3542. tre_gci_ptr = (struct gsi_gci_tre *)(ctx->ring.base_va +
  3543. idx * ctx->ring.elem_sz);
  3544. gci_tre.buffer_ptr = xfer->addr;
  3545. gci_tre.buf_len = xfer->len;
  3546. gci_tre.re_type = GSI_RE_COAL;
  3547. gci_tre.cookie = __gsi_get_gci_cookie(ctx, idx);
  3548. if (gci_tre.cookie > (ctx->ring.max_num_elem + GSI_VEID_MAX))
  3549. return -EPERM;
  3550. /* write the TRE to ring */
  3551. *tre_gci_ptr = gci_tre;
  3552. ctx->user_data[gci_tre.cookie].p = xfer->xfer_user_data;
  3553. return 0;
  3554. }
  3555. int __gsi_populate_tre(struct gsi_chan_ctx *ctx,
  3556. struct gsi_xfer_elem *xfer)
  3557. {
  3558. struct gsi_tre tre;
  3559. struct gsi_tre *tre_ptr;
  3560. uint16_t idx;
  3561. memset(&tre, 0, sizeof(tre));
  3562. tre.buffer_ptr = xfer->addr;
  3563. tre.buf_len = xfer->len;
  3564. if (xfer->type == GSI_XFER_ELEM_DATA) {
  3565. tre.re_type = GSI_RE_XFER;
  3566. } else if (xfer->type == GSI_XFER_ELEM_IMME_CMD) {
  3567. tre.re_type = GSI_RE_IMMD_CMD;
  3568. } else if (xfer->type == GSI_XFER_ELEM_NOP) {
  3569. tre.re_type = GSI_RE_NOP;
  3570. } else {
  3571. GSIERR("chan_hdl=%u bad RE type=%u\n", ctx->props.ch_id,
  3572. xfer->type);
  3573. return -EINVAL;
  3574. }
  3575. tre.bei = (xfer->flags & GSI_XFER_FLAG_BEI) ? 1 : 0;
  3576. tre.ieot = (xfer->flags & GSI_XFER_FLAG_EOT) ? 1 : 0;
  3577. tre.ieob = (xfer->flags & GSI_XFER_FLAG_EOB) ? 1 : 0;
  3578. tre.chain = (xfer->flags & GSI_XFER_FLAG_CHAIN) ? 1 : 0;
  3579. if (unlikely(ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3580. GSIERR("bad state %d\n", ctx->state);
  3581. return -GSI_STATUS_UNSUPPORTED_OP;
  3582. }
  3583. idx = gsi_find_idx_from_addr(&ctx->ring, ctx->ring.wp_local);
  3584. tre_ptr = (struct gsi_tre *)(ctx->ring.base_va +
  3585. idx * ctx->ring.elem_sz);
  3586. /* write the TRE to ring */
  3587. *tre_ptr = tre;
  3588. ctx->user_data[idx].valid = true;
  3589. ctx->user_data[idx].p = xfer->xfer_user_data;
  3590. return 0;
  3591. }
  3592. int gsi_queue_xfer(unsigned long chan_hdl, uint16_t num_xfers,
  3593. struct gsi_xfer_elem *xfer, bool ring_db)
  3594. {
  3595. struct gsi_chan_ctx *ctx;
  3596. uint16_t free;
  3597. uint64_t wp_rollback;
  3598. int i;
  3599. spinlock_t *slock;
  3600. unsigned long flags;
  3601. if (!gsi_ctx) {
  3602. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3603. return -GSI_STATUS_NODEV;
  3604. }
  3605. if (chan_hdl >= gsi_ctx->max_ch || (num_xfers && !xfer)) {
  3606. GSIERR("bad params chan_hdl=%lu num_xfers=%u xfer=%pK\n",
  3607. chan_hdl, num_xfers, xfer);
  3608. return -GSI_STATUS_INVALID_PARAMS;
  3609. }
  3610. if (unlikely(gsi_ctx->chan[chan_hdl].state
  3611. == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3612. GSIERR("bad state %d\n",
  3613. gsi_ctx->chan[chan_hdl].state);
  3614. return -GSI_STATUS_UNSUPPORTED_OP;
  3615. }
  3616. ctx = &gsi_ctx->chan[chan_hdl];
  3617. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3618. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3619. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3620. return -GSI_STATUS_UNSUPPORTED_OP;
  3621. }
  3622. if (ctx->evtr)
  3623. slock = &ctx->evtr->ring.slock;
  3624. else
  3625. slock = &ctx->ring.slock;
  3626. spin_lock_irqsave(slock, flags);
  3627. /* allow only ring doorbell */
  3628. if (!num_xfers)
  3629. goto ring_doorbell;
  3630. /*
  3631. * for GCI channels the responsibility is on the caller to make sure
  3632. * there is enough room in the TRE.
  3633. */
  3634. if (ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3635. __gsi_query_channel_free_re(ctx, &free);
  3636. if (num_xfers > free) {
  3637. GSIERR_RL("chan_hdl=%lu num_xfers=%u free=%u\n",
  3638. chan_hdl, num_xfers, free);
  3639. spin_unlock_irqrestore(slock, flags);
  3640. return -GSI_STATUS_RING_INSUFFICIENT_SPACE;
  3641. }
  3642. }
  3643. wp_rollback = ctx->ring.wp_local;
  3644. for (i = 0; i < num_xfers; i++) {
  3645. if (ctx->props.prot == GSI_CHAN_PROT_GCI) {
  3646. if (__gsi_populate_gci_tre(ctx, &xfer[i]))
  3647. break;
  3648. } else {
  3649. if (__gsi_populate_tre(ctx, &xfer[i]))
  3650. break;
  3651. }
  3652. gsi_incr_ring_wp(&ctx->ring);
  3653. }
  3654. if (i != num_xfers) {
  3655. /* reject all the xfers */
  3656. ctx->ring.wp_local = wp_rollback;
  3657. spin_unlock_irqrestore(slock, flags);
  3658. return -GSI_STATUS_INVALID_PARAMS;
  3659. }
  3660. ctx->stats.queued += num_xfers;
  3661. ring_doorbell:
  3662. if (ring_db) {
  3663. /* ensure TRE is set before ringing doorbell */
  3664. wmb();
  3665. gsi_ring_chan_doorbell(ctx);
  3666. }
  3667. spin_unlock_irqrestore(slock, flags);
  3668. return GSI_STATUS_SUCCESS;
  3669. }
  3670. EXPORT_SYMBOL(gsi_queue_xfer);
  3671. int gsi_start_xfer(unsigned long chan_hdl)
  3672. {
  3673. struct gsi_chan_ctx *ctx;
  3674. if (!gsi_ctx) {
  3675. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3676. return -GSI_STATUS_NODEV;
  3677. }
  3678. if (chan_hdl >= gsi_ctx->max_ch) {
  3679. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3680. return -GSI_STATUS_INVALID_PARAMS;
  3681. }
  3682. ctx = &gsi_ctx->chan[chan_hdl];
  3683. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3684. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3685. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3686. return -GSI_STATUS_UNSUPPORTED_OP;
  3687. }
  3688. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3689. GSIERR("bad state %d\n", ctx->state);
  3690. return -GSI_STATUS_UNSUPPORTED_OP;
  3691. }
  3692. if (ctx->ring.wp == ctx->ring.wp_local)
  3693. return GSI_STATUS_SUCCESS;
  3694. gsi_ring_chan_doorbell(ctx);
  3695. return GSI_STATUS_SUCCESS;
  3696. };
  3697. EXPORT_SYMBOL(gsi_start_xfer);
  3698. int gsi_poll_channel(unsigned long chan_hdl,
  3699. struct gsi_chan_xfer_notify *notify)
  3700. {
  3701. int unused_var;
  3702. return gsi_poll_n_channel(chan_hdl, notify, 1, &unused_var);
  3703. }
  3704. EXPORT_SYMBOL(gsi_poll_channel);
  3705. int gsi_poll_n_channel(unsigned long chan_hdl,
  3706. struct gsi_chan_xfer_notify *notify,
  3707. int expected_num, int *actual_num)
  3708. {
  3709. struct gsi_chan_ctx *ctx;
  3710. uint64_t rp;
  3711. int ee;
  3712. int i;
  3713. unsigned long flags;
  3714. if (!gsi_ctx) {
  3715. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3716. return -GSI_STATUS_NODEV;
  3717. }
  3718. if (chan_hdl >= gsi_ctx->max_ch || !notify ||
  3719. !actual_num || expected_num <= 0) {
  3720. GSIERR("bad params chan_hdl=%lu notify=%pK\n",
  3721. chan_hdl, notify);
  3722. GSIERR("actual_num=%pK expected_num=%d\n",
  3723. actual_num, expected_num);
  3724. return -GSI_STATUS_INVALID_PARAMS;
  3725. }
  3726. ctx = &gsi_ctx->chan[chan_hdl];
  3727. ee = gsi_ctx->per.ee;
  3728. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3729. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3730. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3731. return -GSI_STATUS_UNSUPPORTED_OP;
  3732. }
  3733. /* Before going to poll packet make sure it was in allocated state */
  3734. if (unlikely(ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED)) {
  3735. GSIERR("bad state %d\n", ctx->state);
  3736. return -GSI_STATUS_UNSUPPORTED_OP;
  3737. }
  3738. if (!ctx->evtr) {
  3739. GSIERR("no event ring associated chan_hdl=%lu\n", chan_hdl);
  3740. return -GSI_STATUS_UNSUPPORTED_OP;
  3741. }
  3742. spin_lock_irqsave(&ctx->evtr->ring.slock, flags);
  3743. if (ctx->evtr->ring.rp == ctx->evtr->ring.rp_local) {
  3744. /* update rp to see of we have anything new to process */
  3745. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3746. &ctx->evtr->props, ctx->evtr->id, ee);
  3747. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3748. ctx->evtr->ring.rp = rp;
  3749. /* read gsi event ring rp again if last read is empty */
  3750. if (rp == ctx->evtr->ring.rp_local) {
  3751. /* event ring is empty */
  3752. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3753. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3754. ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3755. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3756. }
  3757. else {
  3758. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3759. ee, 1 << ctx->evtr->id);
  3760. }
  3761. /* do another read to close a small window */
  3762. __iowmb();
  3763. rp = ctx->evtr->props.gsi_read_event_ring_rp(
  3764. &ctx->evtr->props, ctx->evtr->id, ee);
  3765. rp |= ctx->evtr->ring.rp & GSI_MSB_MASK;
  3766. ctx->evtr->ring.rp = rp;
  3767. if (rp == ctx->evtr->ring.rp_local) {
  3768. spin_unlock_irqrestore(
  3769. &ctx->evtr->ring.slock,
  3770. flags);
  3771. ctx->stats.poll_empty++;
  3772. return GSI_STATUS_POLL_EMPTY;
  3773. }
  3774. }
  3775. }
  3776. *actual_num = gsi_get_complete_num(&ctx->evtr->ring,
  3777. ctx->evtr->ring.rp_local, ctx->evtr->ring.rp);
  3778. if (*actual_num > expected_num)
  3779. *actual_num = expected_num;
  3780. for (i = 0; i < *actual_num; i++)
  3781. gsi_process_evt_re(ctx->evtr, notify + i, false);
  3782. spin_unlock_irqrestore(&ctx->evtr->ring.slock, flags);
  3783. ctx->stats.poll_ok++;
  3784. return GSI_STATUS_SUCCESS;
  3785. }
  3786. EXPORT_SYMBOL(gsi_poll_n_channel);
  3787. int gsi_config_channel_mode(unsigned long chan_hdl, enum gsi_chan_mode mode)
  3788. {
  3789. struct gsi_chan_ctx *ctx, *coal_ctx;
  3790. enum gsi_chan_mode curr;
  3791. unsigned long flags;
  3792. enum gsi_chan_mode chan_mode;
  3793. int i;
  3794. if (!gsi_ctx) {
  3795. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3796. return -GSI_STATUS_NODEV;
  3797. }
  3798. if (chan_hdl >= gsi_ctx->max_ch) {
  3799. GSIERR("bad params chan_hdl=%lu mode=%u\n", chan_hdl, mode);
  3800. return -GSI_STATUS_INVALID_PARAMS;
  3801. }
  3802. ctx = &gsi_ctx->chan[chan_hdl];
  3803. if (ctx->props.prot != GSI_CHAN_PROT_GPI &&
  3804. ctx->props.prot != GSI_CHAN_PROT_GCI) {
  3805. GSIERR("op not supported for protocol %u\n", ctx->props.prot);
  3806. return -GSI_STATUS_UNSUPPORTED_OP;
  3807. }
  3808. if (!ctx->evtr) {
  3809. GSIERR("cannot configure mode on chan_hdl=%lu\n",
  3810. chan_hdl);
  3811. return -GSI_STATUS_UNSUPPORTED_OP;
  3812. }
  3813. if (atomic_read(&ctx->poll_mode))
  3814. curr = GSI_CHAN_MODE_POLL;
  3815. else
  3816. curr = GSI_CHAN_MODE_CALLBACK;
  3817. if (mode == curr) {
  3818. GSIDBG("already in requested mode %u chan_hdl=%lu\n",
  3819. curr, chan_hdl);
  3820. return -GSI_STATUS_UNSUPPORTED_OP;
  3821. }
  3822. spin_lock_irqsave(&gsi_ctx->slock, flags);
  3823. if (curr == GSI_CHAN_MODE_CALLBACK &&
  3824. mode == GSI_CHAN_MODE_POLL) {
  3825. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3826. if (ctx->evtr->props.intr != GSI_INTR_MSI) {
  3827. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3828. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3829. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3830. 0);
  3831. }
  3832. }
  3833. else {
  3834. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, 0);
  3835. }
  3836. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3837. gsihal_write_reg_nk(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3838. gsi_ctx->per.ee, gsihal_get_ch_reg_idx(ctx->evtr->id),
  3839. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3840. }
  3841. else {
  3842. gsihal_write_reg_n(GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3843. gsi_ctx->per.ee, 1 << ctx->evtr->id);
  3844. }
  3845. atomic_set(&ctx->poll_mode, mode);
  3846. for(i = 0; i < ctx->evtr->num_of_chan_allocated; i++) {
  3847. atomic_set(&ctx->evtr->chan[i]->poll_mode, mode);
  3848. }
  3849. if ((ctx->props.prot == GSI_CHAN_PROT_GCI) && *ctx->evtr->chan) {
  3850. atomic_set(&ctx->evtr->chan[0]->poll_mode, mode);
  3851. } else if (gsi_ctx->coal_info.evchid == ctx->evtr->id) {
  3852. coal_ctx = &gsi_ctx->chan[gsi_ctx->coal_info.ch_id];
  3853. if (coal_ctx != NULL)
  3854. atomic_set(&coal_ctx->poll_mode, mode);
  3855. }
  3856. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3857. ctx->evtr->id, mode);
  3858. ctx->stats.callback_to_poll++;
  3859. }
  3860. if (curr == GSI_CHAN_MODE_POLL &&
  3861. mode == GSI_CHAN_MODE_CALLBACK) {
  3862. atomic_set(&ctx->poll_mode, mode);
  3863. for(i = 0; i < ctx->evtr->num_of_chan_allocated; i++) {
  3864. atomic_set(&ctx->evtr->chan[i]->poll_mode, mode);
  3865. }
  3866. if ((ctx->props.prot == GSI_CHAN_PROT_GCI) && *ctx->evtr->chan) {
  3867. atomic_set(&ctx->evtr->chan[0]->poll_mode, mode);
  3868. } else if (gsi_ctx->coal_info.evchid == ctx->evtr->id) {
  3869. coal_ctx = &gsi_ctx->chan[gsi_ctx->coal_info.ch_id];
  3870. if (coal_ctx != NULL)
  3871. atomic_set(&coal_ctx->poll_mode, mode);
  3872. }
  3873. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3874. if (ctx->evtr->props.intr != GSI_INTR_MSI) {
  3875. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3876. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3877. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3878. ~0);
  3879. }
  3880. }
  3881. else {
  3882. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 << ctx->evtr->id, ~0);
  3883. }
  3884. GSIDBG("set gsi_ctx evtr_id %d to %d mode\n",
  3885. ctx->evtr->id, mode);
  3886. /*
  3887. * In GSI 2.2 and 2.5 there is a limitation that can lead
  3888. * to losing an interrupt. For these versions an
  3889. * explicit check is needed after enabling the interrupt
  3890. */
  3891. if ((gsi_ctx->per.ver == GSI_VER_2_2 ||
  3892. gsi_ctx->per.ver == GSI_VER_2_5) &&
  3893. !gsi_ctx->per.skip_ieob_mask_wa) {
  3894. u32 src = gsihal_read_reg_n(
  3895. GSI_EE_n_CNTXT_SRC_IEOB_IRQ,
  3896. gsi_ctx->per.ee);
  3897. if (src & (1 << ctx->evtr->id)) {
  3898. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  3899. __gsi_config_ieob_irq_k(gsi_ctx->per.ee,
  3900. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3901. gsihal_get_ch_reg_mask(ctx->evtr->id),
  3902. 0);
  3903. gsihal_write_reg_nk(
  3904. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR_k,
  3905. gsi_ctx->per.ee,
  3906. gsihal_get_ch_reg_idx(ctx->evtr->id),
  3907. gsihal_get_ch_reg_mask(ctx->evtr->id));
  3908. }
  3909. else {
  3910. __gsi_config_ieob_irq(gsi_ctx->per.ee, 1 <<
  3911. ctx->evtr->id, 0);
  3912. gsihal_write_reg_n(
  3913. GSI_EE_n_CNTXT_SRC_IEOB_IRQ_CLR,
  3914. gsi_ctx->per.ee,
  3915. 1 << ctx->evtr->id);
  3916. }
  3917. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3918. spin_lock_irqsave(&ctx->evtr->ring.slock,
  3919. flags);
  3920. chan_mode = atomic_xchg(&ctx->poll_mode,
  3921. GSI_CHAN_MODE_POLL);
  3922. spin_unlock_irqrestore(
  3923. &ctx->evtr->ring.slock, flags);
  3924. ctx->stats.poll_pending_irq++;
  3925. GSIDBG("IEOB WA pnd cnt = %ld prvmode = %d\n",
  3926. ctx->stats.poll_pending_irq,
  3927. chan_mode);
  3928. if (chan_mode == GSI_CHAN_MODE_POLL)
  3929. return GSI_STATUS_SUCCESS;
  3930. else
  3931. return -GSI_STATUS_PENDING_IRQ;
  3932. }
  3933. }
  3934. ctx->stats.poll_to_callback++;
  3935. }
  3936. spin_unlock_irqrestore(&gsi_ctx->slock, flags);
  3937. return GSI_STATUS_SUCCESS;
  3938. }
  3939. EXPORT_SYMBOL(gsi_config_channel_mode);
  3940. int gsi_get_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3941. union gsi_channel_scratch *scr)
  3942. {
  3943. struct gsi_chan_ctx *ctx;
  3944. if (!gsi_ctx) {
  3945. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3946. return -GSI_STATUS_NODEV;
  3947. }
  3948. if (!props || !scr) {
  3949. GSIERR("bad params props=%pK scr=%pK\n", props, scr);
  3950. return -GSI_STATUS_INVALID_PARAMS;
  3951. }
  3952. if (chan_hdl >= gsi_ctx->max_ch) {
  3953. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3954. return -GSI_STATUS_INVALID_PARAMS;
  3955. }
  3956. ctx = &gsi_ctx->chan[chan_hdl];
  3957. if (ctx->state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  3958. GSIERR("bad state %d\n", ctx->state);
  3959. return -GSI_STATUS_UNSUPPORTED_OP;
  3960. }
  3961. mutex_lock(&ctx->mlock);
  3962. *props = ctx->props;
  3963. *scr = ctx->scratch;
  3964. mutex_unlock(&ctx->mlock);
  3965. return GSI_STATUS_SUCCESS;
  3966. }
  3967. EXPORT_SYMBOL(gsi_get_channel_cfg);
  3968. int gsi_set_channel_cfg(unsigned long chan_hdl, struct gsi_chan_props *props,
  3969. union gsi_channel_scratch *scr)
  3970. {
  3971. struct gsi_chan_ctx *ctx;
  3972. if (!gsi_ctx) {
  3973. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  3974. return -GSI_STATUS_NODEV;
  3975. }
  3976. if (!props || gsi_validate_channel_props(props)) {
  3977. GSIERR("bad params props=%pK\n", props);
  3978. return -GSI_STATUS_INVALID_PARAMS;
  3979. }
  3980. if (chan_hdl >= gsi_ctx->max_ch) {
  3981. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  3982. return -GSI_STATUS_INVALID_PARAMS;
  3983. }
  3984. ctx = &gsi_ctx->chan[chan_hdl];
  3985. if (ctx->state != GSI_CHAN_STATE_ALLOCATED) {
  3986. GSIERR("bad state %d\n", ctx->state);
  3987. return -GSI_STATUS_UNSUPPORTED_OP;
  3988. }
  3989. if (ctx->props.ch_id != props->ch_id ||
  3990. ctx->props.evt_ring_hdl != props->evt_ring_hdl) {
  3991. GSIERR("changing immutable fields not supported\n");
  3992. return -GSI_STATUS_UNSUPPORTED_OP;
  3993. }
  3994. mutex_lock(&ctx->mlock);
  3995. ctx->props = *props;
  3996. if (scr)
  3997. ctx->scratch = *scr;
  3998. gsi_program_chan_ctx(&ctx->props, gsi_ctx->per.ee,
  3999. ctx->evtr ? ctx->evtr->id : GSI_NO_EVT_ERINDEX);
  4000. gsi_init_chan_ring(&ctx->props, &ctx->ring);
  4001. /* restore scratch */
  4002. __gsi_write_channel_scratch(chan_hdl, ctx->scratch);
  4003. mutex_unlock(&ctx->mlock);
  4004. return GSI_STATUS_SUCCESS;
  4005. }
  4006. EXPORT_SYMBOL(gsi_set_channel_cfg);
  4007. static void gsi_configure_ieps(enum gsi_ver ver)
  4008. {
  4009. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_CMD, 1);
  4010. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_DB, 2);
  4011. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_DIS_COMP, 3);
  4012. gsihal_write_reg(GSI_GSI_IRAM_PTR_CH_EMPTY, 4);
  4013. gsihal_write_reg(GSI_GSI_IRAM_PTR_EE_GENERIC_CMD, 5);
  4014. gsihal_write_reg(GSI_GSI_IRAM_PTR_EVENT_GEN_COMP, 6);
  4015. gsihal_write_reg(GSI_GSI_IRAM_PTR_INT_MOD_STOPPED, 7);
  4016. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_0, 8);
  4017. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_2, 9);
  4018. gsihal_write_reg(GSI_GSI_IRAM_PTR_PERIPH_IF_TLV_IN_1, 10);
  4019. gsihal_write_reg(GSI_GSI_IRAM_PTR_NEW_RE, 11);
  4020. gsihal_write_reg(GSI_GSI_IRAM_PTR_READ_ENG_COMP, 12);
  4021. gsihal_write_reg(GSI_GSI_IRAM_PTR_TIMER_EXPIRED, 13);
  4022. gsihal_write_reg(GSI_GSI_IRAM_PTR_EV_DB, 14);
  4023. gsihal_write_reg(GSI_GSI_IRAM_PTR_UC_GP_INT, 15);
  4024. gsihal_write_reg(GSI_GSI_IRAM_PTR_WRITE_ENG_COMP, 16);
  4025. if (ver >= GSI_VER_2_5)
  4026. gsihal_write_reg(
  4027. GSI_GSI_IRAM_PTR_TLV_CH_NOT_FULL,
  4028. 17);
  4029. if (ver >= GSI_VER_2_11)
  4030. gsihal_write_reg(
  4031. GSI_GSI_IRAM_PTR_MSI_DB,
  4032. 18);
  4033. if (ver >= GSI_VER_3_0)
  4034. gsihal_write_reg(
  4035. GSI_GSI_IRAM_PTR_INT_NOTIFY_MCS,
  4036. 19);
  4037. }
  4038. static void gsi_configure_bck_prs_matrix(void)
  4039. {
  4040. /*
  4041. * For now, these are default values. In the future, GSI FW image will
  4042. * produce optimized back-pressure values based on the FW image.
  4043. */
  4044. gsihal_write_reg(GSI_IC_DISABLE_CHNL_BCK_PRS_LSB, 0xfffffffe);
  4045. gsihal_write_reg(GSI_IC_DISABLE_CHNL_BCK_PRS_MSB, 0xffffffff);
  4046. gsihal_write_reg(GSI_IC_GEN_EVNT_BCK_PRS_LSB, 0xffffffbf);
  4047. gsihal_write_reg(GSI_IC_GEN_EVNT_BCK_PRS_MSB, 0xffffffff);
  4048. gsihal_write_reg(GSI_IC_GEN_INT_BCK_PRS_LSB, 0xffffefff);
  4049. gsihal_write_reg(GSI_IC_GEN_INT_BCK_PRS_MSB, 0xffffffff);
  4050. gsihal_write_reg(GSI_IC_STOP_INT_MOD_BCK_PRS_LSB, 0xffffefff);
  4051. gsihal_write_reg(GSI_IC_STOP_INT_MOD_BCK_PRS_MSB, 0xffffffff);
  4052. gsihal_write_reg(GSI_IC_PROCESS_DESC_BCK_PRS_LSB, 0x00000000);
  4053. gsihal_write_reg(GSI_IC_PROCESS_DESC_BCK_PRS_MSB, 0x00000000);
  4054. gsihal_write_reg(GSI_IC_TLV_STOP_BCK_PRS_LSB, 0xf9ffffff);
  4055. gsihal_write_reg(GSI_IC_TLV_STOP_BCK_PRS_MSB, 0xffffffff);
  4056. gsihal_write_reg(GSI_IC_TLV_RESET_BCK_PRS_LSB, 0xf9ffffff);
  4057. gsihal_write_reg(GSI_IC_TLV_RESET_BCK_PRS_MSB, 0xffffffff);
  4058. gsihal_write_reg(GSI_IC_RGSTR_TIMER_BCK_PRS_LSB, 0xffffffff);
  4059. gsihal_write_reg(GSI_IC_RGSTR_TIMER_BCK_PRS_MSB, 0xfffffffe);
  4060. gsihal_write_reg(GSI_IC_READ_BCK_PRS_LSB, 0xffffffff);
  4061. gsihal_write_reg(GSI_IC_READ_BCK_PRS_MSB, 0xffffefff);
  4062. gsihal_write_reg(GSI_IC_WRITE_BCK_PRS_LSB, 0xffffffff);
  4063. gsihal_write_reg(GSI_IC_WRITE_BCK_PRS_MSB, 0xffffdfff);
  4064. gsihal_write_reg(GSI_IC_UCONTROLLER_GPR_BCK_PRS_LSB, 0xffffffff);
  4065. gsihal_write_reg(GSI_IC_UCONTROLLER_GPR_BCK_PRS_MSB, 0xff03ffff);
  4066. }
  4067. int gsi_configure_regs(phys_addr_t per_base_addr, enum gsi_ver ver)
  4068. {
  4069. if (!gsi_ctx) {
  4070. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4071. return -GSI_STATUS_NODEV;
  4072. }
  4073. if (!gsi_ctx->base) {
  4074. GSIERR("access to GSI HW has not been mapped\n");
  4075. return -GSI_STATUS_INVALID_PARAMS;
  4076. }
  4077. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  4078. GSIERR("Incorrect version %d\n", ver);
  4079. return -GSI_STATUS_ERROR;
  4080. }
  4081. gsihal_write_reg(GSI_GSI_PERIPH_BASE_ADDR_MSB, 0);
  4082. gsihal_write_reg(GSI_GSI_PERIPH_BASE_ADDR_LSB, per_base_addr);
  4083. gsi_configure_bck_prs_matrix();
  4084. gsi_configure_ieps(ver);
  4085. return 0;
  4086. }
  4087. EXPORT_SYMBOL(gsi_configure_regs);
  4088. int gsi_enable_fw(phys_addr_t gsi_base_addr, u32 gsi_size, enum gsi_ver ver)
  4089. {
  4090. struct gsihal_reg_gsi_cfg gsi_cfg;
  4091. if (ver <= GSI_VER_ERR || ver >= GSI_VER_MAX) {
  4092. GSIERR("Incorrect version %d\n", ver);
  4093. return -GSI_STATUS_ERROR;
  4094. }
  4095. /* Enable the MCS and set to x2 clocks */
  4096. gsi_cfg.gsi_enable = 1;
  4097. gsi_cfg.double_mcs_clk_freq = 1;
  4098. gsi_cfg.uc_is_mcs = 0;
  4099. gsi_cfg.gsi_pwr_clps = 0;
  4100. gsi_cfg.bp_mtrix_disable = 0;
  4101. if (ver >= GSI_VER_1_2) {
  4102. gsihal_write_reg(GSI_GSI_MCS_CFG, 1);
  4103. gsi_cfg.mcs_enable = 0;
  4104. } else {
  4105. gsi_cfg.mcs_enable = 1;
  4106. }
  4107. /* GSI frequency is peripheral frequency divided by 3 (2+1) */
  4108. if (ver >= GSI_VER_2_5)
  4109. gsi_cfg.sleep_clk_div = 2;
  4110. gsihal_write_reg_fields(GSI_GSI_CFG, &gsi_cfg);
  4111. return 0;
  4112. }
  4113. EXPORT_SYMBOL(gsi_enable_fw);
  4114. void gsi_get_inst_ram_offset_and_size(unsigned long *base_offset,
  4115. unsigned long *size, enum gsi_ver ver)
  4116. {
  4117. if (!gsi_ctx) {
  4118. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4119. return;
  4120. }
  4121. if (size)
  4122. *size = gsihal_get_inst_ram_size();
  4123. if (base_offset) {
  4124. *base_offset = gsihal_get_reg_n_ofst(GSI_GSI_INST_RAM_n, 0);
  4125. }
  4126. }
  4127. EXPORT_SYMBOL(gsi_get_inst_ram_offset_and_size);
  4128. /*
  4129. * Dumping the Debug registers for halt issue debugging.
  4130. */
  4131. static void gsi_dump_halt_debug_reg(unsigned int chan_idx, unsigned int ee)
  4132. {
  4133. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  4134. GSIERR("DEBUG_PC_FOR_DEBUG = 0x%x\n",
  4135. gsihal_read_reg(GSI_EE_n_GSI_DEBUG_PC_FOR_DEBUG));
  4136. GSIERR("GSI_DEBUG_BUSY_REG 0x%x\n",
  4137. gsihal_read_reg(GSI_EE_n_GSI_DEBUG_BUSY_REG));
  4138. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS = 0x%x\n",
  4139. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4140. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS IRQ type = 0x%x\n",
  4141. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4142. GSIERR("GSI_EE_n_CNTXT_SCRATCH_0_OFFS = 0x%x\n",
  4143. gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0, gsi_ctx->per.ee));
  4144. if (gsi_ctx->per.ver >= GSI_VER_2_9)
  4145. GSIERR("GSI_EE_n_GSI_CH_k_SCRATCH_4 = 0x%x\n",
  4146. gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, ee, chan_idx));
  4147. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0, ee, chan_idx, &ch_k_cntxt_0);
  4148. GSIERR("Q6 channel [%d] state = %d\n", chan_idx, ch_k_cntxt_0.chstate);
  4149. }
  4150. int gsi_halt_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  4151. {
  4152. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_HALT_CHANNEL;
  4153. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4154. int res;
  4155. if (!gsi_ctx) {
  4156. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4157. return -GSI_STATUS_NODEV;
  4158. }
  4159. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4160. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4161. return -GSI_STATUS_INVALID_PARAMS;
  4162. }
  4163. mutex_lock(&gsi_ctx->mlock);
  4164. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4165. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4166. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4167. /* invalidate the response */
  4168. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(
  4169. GSI_EE_n_CNTXT_SCRATCH_0, gsi_ctx->per.ee);
  4170. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4171. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4172. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4173. gsi_ctx->gen_ee_cmd_dbg.halt_channel++;
  4174. cmd.opcode = op;
  4175. cmd.virt_chan_idx = chan_idx;
  4176. cmd.ee = ee;
  4177. gsihal_write_reg_n_fields(GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4178. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4179. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4180. if (res == 0) {
  4181. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4182. res = -GSI_STATUS_TIMED_OUT;
  4183. goto free_lock;
  4184. }
  4185. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4186. gsi_ctx->per.ee);
  4187. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4188. GSI_GEN_EE_CMD_RETURN_CODE_RETRY) {
  4189. GSIDBG("chan_idx=%u ee=%u busy try again\n", chan_idx, ee);
  4190. *code = GSI_GEN_EE_CMD_RETURN_CODE_RETRY;
  4191. res = -GSI_STATUS_AGAIN;
  4192. goto free_lock;
  4193. }
  4194. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4195. GSIERR("No response received\n");
  4196. gsi_dump_halt_debug_reg(chan_idx, ee);
  4197. usleep_range(GSI_RESET_WA_MIN_SLEEP, GSI_RESET_WA_MAX_SLEEP);
  4198. GSIERR("Reading after usleep scratch 0 reg\n");
  4199. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4200. gsi_ctx->per.ee);
  4201. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4202. GSIERR("No response received second attempt\n");
  4203. gsi_dump_halt_debug_reg(chan_idx, ee);
  4204. res = -GSI_STATUS_ERROR;
  4205. goto free_lock;
  4206. }
  4207. }
  4208. res = GSI_STATUS_SUCCESS;
  4209. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4210. free_lock:
  4211. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4212. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4213. mutex_unlock(&gsi_ctx->mlock);
  4214. return res;
  4215. }
  4216. EXPORT_SYMBOL(gsi_halt_channel_ee);
  4217. int gsi_alloc_channel_ee(unsigned int chan_idx, unsigned int ee, int *code)
  4218. {
  4219. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_ALLOC_CHANNEL;
  4220. struct gsi_chan_ctx *ctx;
  4221. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4222. int res;
  4223. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4224. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4225. return -GSI_STATUS_INVALID_PARAMS;
  4226. }
  4227. if (ee == 0)
  4228. return gsi_alloc_ap_channel(chan_idx);
  4229. mutex_lock(&gsi_ctx->mlock);
  4230. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4231. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4232. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4233. /* invalidate the response */
  4234. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4235. gsi_ctx->per.ee);
  4236. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4237. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4238. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4239. cmd.opcode = op;
  4240. cmd.virt_chan_idx = chan_idx;
  4241. cmd.ee = ee;
  4242. gsihal_write_reg_n_fields(
  4243. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4244. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4245. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4246. if (res == 0) {
  4247. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4248. res = -GSI_STATUS_TIMED_OUT;
  4249. goto free_lock;
  4250. }
  4251. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4252. gsi_ctx->per.ee);
  4253. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4254. GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES) {
  4255. GSIDBG("chan_idx=%u ee=%u out of resources\n", chan_idx, ee);
  4256. *code = GSI_GEN_EE_CMD_RETURN_CODE_OUT_OF_RESOURCES;
  4257. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4258. goto free_lock;
  4259. }
  4260. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4261. GSIERR("No response received\n");
  4262. res = -GSI_STATUS_ERROR;
  4263. goto free_lock;
  4264. }
  4265. if (ee == 0) {
  4266. ctx = &gsi_ctx->chan[chan_idx];
  4267. gsi_ctx->ch_dbg[chan_idx].ch_allocate++;
  4268. }
  4269. res = GSI_STATUS_SUCCESS;
  4270. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4271. free_lock:
  4272. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4273. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4274. mutex_unlock(&gsi_ctx->mlock);
  4275. return res;
  4276. }
  4277. EXPORT_SYMBOL(gsi_alloc_channel_ee);
  4278. int gsi_enable_flow_control_ee(unsigned int chan_idx, unsigned int ee,
  4279. int *code)
  4280. {
  4281. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL;
  4282. struct gsihal_reg_ch_k_cntxt_0 ch_k_cntxt_0;
  4283. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4284. enum gsi_chan_state curr_state = GSI_CHAN_STATE_NOT_ALLOCATED;
  4285. int res;
  4286. if (!gsi_ctx) {
  4287. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4288. return -GSI_STATUS_NODEV;
  4289. }
  4290. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4291. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4292. return -GSI_STATUS_INVALID_PARAMS;
  4293. }
  4294. mutex_lock(&gsi_ctx->mlock);
  4295. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4296. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4297. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4298. /* invalidate the response */
  4299. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4300. gsi_ctx->per.ee);
  4301. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4302. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4303. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4304. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4305. cmd.opcode = op;
  4306. cmd.virt_chan_idx = chan_idx;
  4307. cmd.ee = ee;
  4308. gsihal_write_reg_n_fields(
  4309. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4310. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4311. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4312. if (res == 0) {
  4313. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4314. res = -GSI_STATUS_TIMED_OUT;
  4315. goto free_lock;
  4316. }
  4317. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4318. gsi_ctx->per.ee);
  4319. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4320. GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING) {
  4321. GSIDBG("chan_idx=%u ee=%u not in correct state\n",
  4322. chan_idx, ee);
  4323. *code = GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING;
  4324. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4325. goto free_lock;
  4326. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4327. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_TYPE ||
  4328. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4329. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_INDEX) {
  4330. GSIERR("chan_idx=%u ee=%u not in correct state\n",
  4331. chan_idx, ee);
  4332. GSI_ASSERT();
  4333. }
  4334. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4335. GSIERR("No response received\n");
  4336. res = -GSI_STATUS_ERROR;
  4337. goto free_lock;
  4338. }
  4339. /*Reading current channel state*/
  4340. gsihal_read_reg_nk_fields(GSI_EE_n_GSI_CH_k_CNTXT_0,
  4341. gsi_ctx->per.ee, chan_idx, &ch_k_cntxt_0);
  4342. curr_state = ch_k_cntxt_0.chstate;
  4343. if (curr_state == GSI_CHAN_STATE_FLOW_CONTROL) {
  4344. GSIDBG("ch %u state updated to %u\n", chan_idx, curr_state);
  4345. res = GSI_STATUS_SUCCESS;
  4346. } else {
  4347. GSIERR("ch %u state updated to %u incorrect state\n",
  4348. chan_idx, curr_state);
  4349. res = -GSI_STATUS_ERROR;
  4350. }
  4351. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4352. free_lock:
  4353. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4354. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4355. mutex_unlock(&gsi_ctx->mlock);
  4356. return res;
  4357. }
  4358. EXPORT_SYMBOL(gsi_enable_flow_control_ee);
  4359. int gsi_flow_control_ee(unsigned int chan_idx, int ep_id, unsigned int ee,
  4360. bool enable, bool prmy_scnd_fc, int *code)
  4361. {
  4362. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4363. enum gsi_generic_ee_cmd_opcode op = enable ?
  4364. GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL :
  4365. GSI_GEN_EE_CMD_DISABLE_FLOW_CHANNEL;
  4366. int res;
  4367. int wait_due_pending = 0;
  4368. uint32_t fc_pending = 0;
  4369. if (!gsi_ctx) {
  4370. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4371. return -GSI_STATUS_NODEV;
  4372. }
  4373. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4374. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4375. return -GSI_STATUS_INVALID_PARAMS;
  4376. }
  4377. GSIDBG("GSI flow control opcode=%d, ch_id=%d\n", op, chan_idx);
  4378. mutex_lock(&gsi_ctx->mlock);
  4379. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4380. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4381. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4382. /* invalidate the response */
  4383. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4384. gsi_ctx->per.ee);
  4385. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4386. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4387. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4388. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4389. cmd.opcode = op;
  4390. cmd.virt_chan_idx = chan_idx;
  4391. cmd.ee = ee;
  4392. cmd.prmy_scnd_fc = prmy_scnd_fc;
  4393. gsihal_write_reg_n_fields(
  4394. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4395. wait_again:
  4396. fc_pending = gsihal_read_reg_n(GSI_GSI_SHRAM_n,
  4397. (ep_id * GSI_FC_NUM_WORDS_PER_CHNL_SHRAM) + GSI_FC_STATE_INDEX_SHRAM) &
  4398. GSI_FC_PENDING_MASK;
  4399. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4400. msecs_to_jiffies(GSI_FC_CMD_TIMEOUT));
  4401. if (res == 0) {
  4402. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4403. if (op == GSI_GEN_EE_CMD_ENABLE_FLOW_CHANNEL &&
  4404. wait_due_pending < GSI_FC_MAX_TIMEOUT &&
  4405. fc_pending) {
  4406. wait_due_pending++;
  4407. goto wait_again;
  4408. }
  4409. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_EN_OFFS = 0x%x\n",
  4410. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_EN, gsi_ctx->per.ee));
  4411. GSIERR("GSI_EE_n_CNTXT_GLOB_IRQ_STTS_OFFS IRQ type = 0x%x\n",
  4412. gsihal_read_reg_n(GSI_EE_n_CNTXT_GLOB_IRQ_STTS, gsi_ctx->per.ee));
  4413. }
  4414. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4415. gsi_ctx->per.ee);
  4416. GSIDBG(
  4417. "Flow control command response GENERIC_CMD_RESPONSE_CODE = %u, val = %u\n",
  4418. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code,
  4419. gsi_ctx->scratch.word0.val);
  4420. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4421. GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING) {
  4422. GSIDBG("chan_idx=%u ee=%u not in correct state\n",
  4423. chan_idx, ee);
  4424. *code = GSI_GEN_EE_CMD_RETURN_CODE_CHANNEL_NOT_RUNNING;
  4425. res = -GSI_STATUS_RES_ALLOC_FAILURE;
  4426. goto free_lock;
  4427. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4428. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_TYPE) {
  4429. GSIERR("chan_idx=%u ee=%u not in correct state\n",
  4430. chan_idx, ee);
  4431. GSI_ASSERT();
  4432. } else if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code ==
  4433. GSI_GEN_EE_CMD_RETURN_CODE_INCORRECT_CHANNEL_INDEX) {
  4434. GSIERR("Channel ID = %u ee = %u not allocated\n", chan_idx, ee);
  4435. }
  4436. if (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code == 0) {
  4437. GSIERR("No response received\n");
  4438. res = -GSI_STATUS_ERROR;
  4439. GSI_ASSERT();
  4440. goto free_lock;
  4441. }
  4442. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code;
  4443. res = GSI_STATUS_SUCCESS;
  4444. free_lock:
  4445. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4446. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4447. mutex_unlock(&gsi_ctx->mlock);
  4448. return res;
  4449. }
  4450. EXPORT_SYMBOL(gsi_flow_control_ee);
  4451. int gsi_query_flow_control_state_ee(unsigned int chan_idx, unsigned int ee,
  4452. bool prmy_scnd_fc, int *code)
  4453. {
  4454. struct gsihal_reg_gsi_ee_generic_cmd cmd;
  4455. enum gsi_generic_ee_cmd_opcode op = GSI_GEN_EE_CMD_QUERY_FLOW_CHANNEL;
  4456. int res;
  4457. if (!gsi_ctx) {
  4458. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4459. return -GSI_STATUS_NODEV;
  4460. }
  4461. if (chan_idx >= gsi_ctx->max_ch || !code) {
  4462. GSIERR("bad params chan_idx=%d\n", chan_idx);
  4463. return -GSI_STATUS_INVALID_PARAMS;
  4464. }
  4465. mutex_lock(&gsi_ctx->mlock);
  4466. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4467. gsihal_get_glob_irq_en_gp_int1_mask(), ~0);
  4468. reinit_completion(&gsi_ctx->gen_ee_cmd_compl);
  4469. /* invalidate the response */
  4470. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4471. gsi_ctx->per.ee);
  4472. gsi_ctx->scratch.word0.s.generic_ee_cmd_return_code = 0;
  4473. gsihal_write_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4474. gsi_ctx->per.ee, gsi_ctx->scratch.word0.val);
  4475. gsi_ctx->gen_ee_cmd_dbg.flow_ctrl_channel++;
  4476. cmd.opcode = op;
  4477. cmd.virt_chan_idx = chan_idx;
  4478. cmd.ee = ee;
  4479. cmd.prmy_scnd_fc = prmy_scnd_fc;
  4480. gsihal_write_reg_n_fields(
  4481. GSI_EE_n_GSI_EE_GENERIC_CMD, gsi_ctx->per.ee, &cmd);
  4482. res = wait_for_completion_timeout(&gsi_ctx->gen_ee_cmd_compl,
  4483. msecs_to_jiffies(GSI_CMD_TIMEOUT));
  4484. if (res == 0) {
  4485. GSIERR("chan_idx=%u ee=%u timed out\n", chan_idx, ee);
  4486. res = -GSI_STATUS_TIMED_OUT;
  4487. goto free_lock;
  4488. }
  4489. gsi_ctx->scratch.word0.val = gsihal_read_reg_n(GSI_EE_n_CNTXT_SCRATCH_0,
  4490. gsi_ctx->per.ee);
  4491. *code = gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val;
  4492. if (prmy_scnd_fc)
  4493. res = (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val ==
  4494. GSI_GEN_EE_CMD_RETURN_VAL_FLOW_CONTROL_SECONDARY)?
  4495. GSI_STATUS_SUCCESS:-GSI_STATUS_ERROR;
  4496. else
  4497. res = (gsi_ctx->scratch.word0.s.generic_ee_cmd_return_val ==
  4498. GSI_GEN_EE_CMD_RETURN_VAL_FLOW_CONTROL_PRIMARY)?
  4499. GSI_STATUS_SUCCESS:-GSI_STATUS_ERROR;
  4500. free_lock:
  4501. __gsi_config_glob_irq(gsi_ctx->per.ee,
  4502. gsihal_get_glob_irq_en_gp_int1_mask(), 0);
  4503. mutex_unlock(&gsi_ctx->mlock);
  4504. return res;
  4505. }
  4506. EXPORT_SYMBOL(gsi_query_flow_control_state_ee);
  4507. int gsi_map_virtual_ch_to_per_ep(u32 ee, u32 chan_num, u32 per_ep_index)
  4508. {
  4509. if (!gsi_ctx) {
  4510. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4511. return -GSI_STATUS_NODEV;
  4512. }
  4513. if (!gsi_ctx->base) {
  4514. GSIERR("access to GSI HW has not been mapped\n");
  4515. return -GSI_STATUS_INVALID_PARAMS;
  4516. }
  4517. gsihal_write_reg_nk(GSI_MAP_EE_n_CH_k_VP_TABLE,
  4518. ee, chan_num, per_ep_index);
  4519. return 0;
  4520. }
  4521. EXPORT_SYMBOL(gsi_map_virtual_ch_to_per_ep);
  4522. void gsi_wdi3_write_evt_ring_db(unsigned long evt_ring_hdl,
  4523. uint32_t db_addr_low, uint32_t db_addr_high)
  4524. {
  4525. if (!gsi_ctx) {
  4526. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4527. return;
  4528. }
  4529. if (gsi_ctx->per.ver >= GSI_VER_2_9) {
  4530. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_10,
  4531. gsi_ctx->per.ee, evt_ring_hdl, db_addr_low);
  4532. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_11,
  4533. gsi_ctx->per.ee, evt_ring_hdl, db_addr_high);
  4534. } else {
  4535. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_12,
  4536. gsi_ctx->per.ee, evt_ring_hdl, db_addr_low);
  4537. gsihal_write_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_13,
  4538. gsi_ctx->per.ee, evt_ring_hdl, db_addr_high);
  4539. }
  4540. }
  4541. EXPORT_SYMBOL(gsi_wdi3_write_evt_ring_db);
  4542. int gsi_get_refetch_reg(unsigned long chan_hdl, bool is_rp)
  4543. {
  4544. if (is_rp) {
  4545. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  4546. gsi_ctx->per.ee, chan_hdl);
  4547. } else {
  4548. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4549. gsi_ctx->per.ee, chan_hdl);
  4550. }
  4551. }
  4552. EXPORT_SYMBOL(gsi_get_refetch_reg);
  4553. /*
  4554. * ; +------------------------------------------------------+
  4555. * ; | NTN3 Rx Channel Scratch |
  4556. * ; +-------------+--------------------------------+-------+
  4557. * ; | 32-bit word | Field | Bits |
  4558. * ; +-------------+--------------------------------+-------+
  4559. * ; | 4 | NTN_PENDING_DB_AFTER_ROLLBACK | 18-18 |
  4560. * ; +-------------+--------------------------------+-------+
  4561. * ; | 5 | NTN_MSI_DB_INDEX_VALUE | 0-31 |
  4562. * ; +-------------+--------------------------------+-------+
  4563. * ; | 6 | NTN_RX_CHAIN_COUNTER | 0-31 |
  4564. * ; +-------------+--------------------------------+-------+
  4565. * ; | 7 | NTN_RX_ERR_COUNTER | 0-31 |
  4566. * ; +-------------+--------------------------------+-------+
  4567. * ; | 8 | NTN_ACCUMULATED_TRES_HANDLED | 0-31 |
  4568. * ; +-------------+--------------------------------+-------+
  4569. * ; | 9 | NTN_ROLLBACKS_COUNTER | 0-31 |
  4570. * ; +-------------+--------------------------------+-------+
  4571. * ; | FOR_SEQ_HIGH| NTN_MSI_DB_COUNT | 0-31 |
  4572. * ; +-------------+--------------------------------+-------+
  4573. *
  4574. * ; +------------------------------------------------------+
  4575. * ; | NTN3 Tx Channel Scratch |
  4576. * ; +-------------+--------------------------------+-------+
  4577. * ; | 32-bit word | Field | Bits |
  4578. * ; +-------------+--------------------------------+-------+
  4579. * ; | 4 | NTN_PENDING_DB_AFTER_ROLLBACK | 18-18 |
  4580. * ; +-------------+--------------------------------+-------+
  4581. * ; | 5 | NTN_MSI_DB_INDEX_VALUE | 0-31 |
  4582. * ; +-------------+--------------------------------+-------+
  4583. * ; | 6 | TX_DERR_COUNTER | 0-31 |
  4584. * ; +-------------+--------------------------------+-------+
  4585. * ; | 7 | NTN_TX_OOB_COUNTER | 0-31 |
  4586. * ; +-------------+--------------------------------+-------+
  4587. * ; | 8 | NTN_ACCUMULATED_TRES_HANDLED | 0-31 |
  4588. * ; +-------------+--------------------------------+-------+
  4589. * ; | 9 | NTN_ROLLBACKS_COUNTER | 0-31 |
  4590. * ; +-------------+--------------------------------+-------+
  4591. * ; | FOR_SEQ_HIGH| NTN_MSI_DB_COUNT | 0-31 |
  4592. * ; +-------------+--------------------------------+-------+
  4593. */
  4594. int gsi_ntn3_client_stats_get(unsigned ep_id, int scratch_id, unsigned chan_hdl)
  4595. {
  4596. switch (scratch_id) {
  4597. case -1:
  4598. return gsihal_read_reg_n(GSI_GSI_SHRAM_n, GSI_GSI_SHRAM_n_EP_FOR_SEQ_HIGH_N_GET(ep_id));
  4599. case 4:
  4600. return (gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, gsi_ctx->per.ee,
  4601. chan_hdl) >> GSI_NTN3_PENDING_DB_AFTER_RB_MASK) &
  4602. GSI_NTN3_PENDING_DB_AFTER_RB_SHIFT;
  4603. break;
  4604. case 5:
  4605. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_5, gsi_ctx->per.ee, chan_hdl);
  4606. break;
  4607. case 6:
  4608. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_6, gsi_ctx->per.ee, chan_hdl);
  4609. break;
  4610. case 7:
  4611. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_7, gsi_ctx->per.ee, chan_hdl);
  4612. break;
  4613. case 8:
  4614. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_8, gsi_ctx->per.ee, chan_hdl);
  4615. break;
  4616. case 9:
  4617. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_9, gsi_ctx->per.ee, chan_hdl);
  4618. break;
  4619. default:
  4620. GSIERR("invalid scratch id %d\n", scratch_id);
  4621. return 0;
  4622. }
  4623. return 0;
  4624. }
  4625. EXPORT_SYMBOL(gsi_ntn3_client_stats_get);
  4626. int gsi_get_drop_stats(unsigned long ep_id, int scratch_id,
  4627. unsigned long chan_hdl)
  4628. {
  4629. #define GSI_RTK_ERR_STATS_MASK 0xFFFF
  4630. #define GSI_NTN_ERR_STATS_MASK 0xFFFFFFFF
  4631. #define GSI_AQC_RX_STATUS_MASK 0x1FFF
  4632. #define GSI_AQC_RX_STATUS_SHIFT 0
  4633. #define GSI_AQC_RDM_ERR_MASK 0x1FFF0000
  4634. #define GSI_AQC_RDM_ERR_SHIFT 16
  4635. uint16_t rx_status;
  4636. uint16_t rdm_err;
  4637. uint32_t val;
  4638. /* on newer versions we can read the ch scratch directly from reg */
  4639. if (gsi_ctx->per.ver >= GSI_VER_3_0) {
  4640. switch (scratch_id) {
  4641. case 5:
  4642. return gsihal_read_reg_nk(
  4643. GSI_EE_n_GSI_CH_k_SCRATCH_5,
  4644. gsi_ctx->per.ee,
  4645. chan_hdl) & GSI_RTK_ERR_STATS_MASK;
  4646. break;
  4647. case 6:
  4648. return gsihal_read_reg_nk(
  4649. GSI_EE_n_GSI_CH_k_SCRATCH_6,
  4650. gsi_ctx->per.ee,
  4651. chan_hdl) & GSI_NTN_ERR_STATS_MASK;
  4652. break;
  4653. case 7:
  4654. val = gsihal_read_reg_nk(
  4655. GSI_EE_n_GSI_CH_k_SCRATCH_7,
  4656. gsi_ctx->per.ee,
  4657. chan_hdl);
  4658. rx_status = (val & GSI_AQC_RX_STATUS_MASK)
  4659. >> GSI_AQC_RX_STATUS_SHIFT;
  4660. rdm_err = (val & GSI_AQC_RDM_ERR_MASK)
  4661. >> (GSI_AQC_RDM_ERR_SHIFT);
  4662. return rx_status + rdm_err;
  4663. break;
  4664. default:
  4665. GSIERR("invalid scratch id %d\n", scratch_id);
  4666. return 0;
  4667. }
  4668. /* on older versions we need to read the scratch from SHRAM */
  4669. } else {
  4670. /* RTK use scratch 5 */
  4671. if (scratch_id == 5) {
  4672. /*
  4673. * each channel context is 6 lines of 8 bytes, but n in
  4674. * SHRAM_n is in 4 bytes offsets, so multiplying ep_id
  4675. * by 6*2=12 will give the beginning of the required
  4676. * channel context, and then need to add 7 since the
  4677. * channel context layout has the ring rbase (8 bytes)
  4678. * + channel scratch 0-4 (20 bytes) so adding
  4679. * additional 28/4 = 7 to get to scratch 5 of the
  4680. * required channel.
  4681. */
  4682. return gsihal_read_reg_n(
  4683. GSI_GSI_SHRAM_n,
  4684. ep_id * 12 + 7) & GSI_RTK_ERR_STATS_MASK;
  4685. }
  4686. }
  4687. return 0;
  4688. }
  4689. EXPORT_SYMBOL(gsi_get_drop_stats);
  4690. int gsi_get_wp(unsigned long chan_hdl)
  4691. {
  4692. return gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6, gsi_ctx->per.ee,
  4693. chan_hdl);
  4694. }
  4695. EXPORT_SYMBOL(gsi_get_wp);
  4696. void gsi_wdi3_dump_register(unsigned long chan_hdl)
  4697. {
  4698. uint32_t val;
  4699. if (!gsi_ctx) {
  4700. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4701. return;
  4702. }
  4703. GSIDBG("reg dump ch id %ld\n", chan_hdl);
  4704. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_0,
  4705. gsi_ctx->per.ee, chan_hdl);
  4706. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_0 0x%x\n", val);
  4707. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_1,
  4708. gsi_ctx->per.ee, chan_hdl);
  4709. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_1 0x%x\n", val);
  4710. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_2,
  4711. gsi_ctx->per.ee, chan_hdl);
  4712. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_2 0x%x\n", val);
  4713. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_3,
  4714. gsi_ctx->per.ee, chan_hdl);
  4715. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_3 0x%x\n", val);
  4716. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  4717. gsi_ctx->per.ee, chan_hdl);
  4718. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_4 0x%x\n", val);
  4719. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  4720. gsi_ctx->per.ee, chan_hdl);
  4721. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_5 0x%x\n", val);
  4722. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  4723. gsi_ctx->per.ee, chan_hdl);
  4724. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_6 0x%x\n", val);
  4725. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  4726. gsi_ctx->per.ee, chan_hdl);
  4727. GSIDBG("GSI_EE_n_GSI_CH_k_CNTXT_7 0x%x\n", val);
  4728. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR,
  4729. gsi_ctx->per.ee, chan_hdl);
  4730. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_READ_PTR 0x%x\n", val);
  4731. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4732. gsi_ctx->per.ee, chan_hdl);
  4733. GSIDBG("GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR 0x%x\n", val);
  4734. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_QOS,
  4735. gsi_ctx->per.ee, chan_hdl);
  4736. GSIDBG("GSI_EE_n_GSI_CH_k_QOS 0x%x\n", val);
  4737. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4738. gsi_ctx->per.ee, chan_hdl);
  4739. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_0 0x%x\n", val);
  4740. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4741. gsi_ctx->per.ee, chan_hdl);
  4742. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_1 0x%x\n", val);
  4743. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4744. gsi_ctx->per.ee, chan_hdl);
  4745. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_2 0x%x\n", val);
  4746. val = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4747. gsi_ctx->per.ee, chan_hdl);
  4748. GSIDBG("GSI_EE_n_GSI_CH_k_SCRATCH_3 0x%x\n", val);
  4749. }
  4750. EXPORT_SYMBOL(gsi_wdi3_dump_register);
  4751. int gsi_query_msi_addr(unsigned long chan_hdl, phys_addr_t *addr)
  4752. {
  4753. if (!gsi_ctx) {
  4754. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4755. return -GSI_STATUS_NODEV;
  4756. }
  4757. if (chan_hdl >= gsi_ctx->max_ch) {
  4758. GSIERR("bad params chan_hdl=%lu\n", chan_hdl);
  4759. return -GSI_STATUS_INVALID_PARAMS;
  4760. }
  4761. if (gsi_ctx->chan[chan_hdl].state == GSI_CHAN_STATE_NOT_ALLOCATED) {
  4762. GSIERR("bad state %d\n",
  4763. gsi_ctx->chan[chan_hdl].state);
  4764. return -GSI_STATUS_UNSUPPORTED_OP;
  4765. }
  4766. *addr = (phys_addr_t)(gsi_ctx->per.phys_addr +
  4767. gsihal_get_reg_nk_ofst(GSI_EE_n_GSI_CH_k_CNTXT_8,
  4768. gsi_ctx->per.ee, chan_hdl));
  4769. return 0;
  4770. }
  4771. EXPORT_SYMBOL(gsi_query_msi_addr);
  4772. int gsi_query_device_msi_addr(u64 *addr)
  4773. {
  4774. if (!gsi_ctx) {
  4775. pr_err("%s:%d gsi context not allocated\n", __func__, __LINE__);
  4776. return -GSI_STATUS_NODEV;
  4777. }
  4778. if (gsi_ctx->msi_addr_set)
  4779. *addr = gsi_ctx->msi_addr;
  4780. else
  4781. *addr = 0;
  4782. GSIDBG("Device MSI Addr: 0x%lx", *addr);
  4783. return 0;
  4784. }
  4785. EXPORT_SYMBOL(gsi_query_device_msi_addr);
  4786. uint64_t gsi_read_event_ring_wp(int evtr_id, int ee)
  4787. {
  4788. uint64_t wp;
  4789. wp = gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_6,
  4790. ee, evtr_id);
  4791. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_EV_CH_k_CNTXT_7,
  4792. ee, evtr_id)) << 32;
  4793. return wp;
  4794. }
  4795. EXPORT_SYMBOL(gsi_read_event_ring_wp);
  4796. uint64_t gsi_read_event_ring_bp(int evt_hdl)
  4797. {
  4798. return gsi_ctx->evtr[evt_hdl].ring.base;
  4799. }
  4800. EXPORT_SYMBOL(gsi_read_event_ring_bp);
  4801. uint64_t gsi_get_evt_ring_rp(int evt_hdl)
  4802. {
  4803. return gsi_ctx->evtr[evt_hdl].props.gsi_read_event_ring_rp(
  4804. &gsi_ctx->evtr[evt_hdl].props, evt_hdl, gsi_ctx->per.ee);
  4805. }
  4806. EXPORT_SYMBOL(gsi_get_evt_ring_rp);
  4807. uint64_t gsi_read_chan_ring_rp(int chan_id, int ee)
  4808. {
  4809. uint64_t rp;
  4810. rp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_4,
  4811. ee, chan_id);
  4812. rp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_5,
  4813. ee, chan_id)) << 32;
  4814. return rp;
  4815. }
  4816. EXPORT_SYMBOL(gsi_read_chan_ring_rp);
  4817. uint64_t gsi_read_chan_ring_wp(int chan_id, int ee)
  4818. {
  4819. uint64_t wp;
  4820. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_6,
  4821. ee, chan_id);
  4822. wp |= ((uint64_t)gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_CNTXT_7,
  4823. ee, chan_id)) << 32;
  4824. return wp;
  4825. }
  4826. EXPORT_SYMBOL(gsi_read_chan_ring_wp);
  4827. uint64_t gsi_read_chan_ring_bp(int chan_hdl)
  4828. {
  4829. return gsi_ctx->chan[chan_hdl].ring.base;
  4830. }
  4831. EXPORT_SYMBOL(gsi_read_chan_ring_bp);
  4832. uint64_t gsi_read_chan_ring_re_fetch_wp(int chan_id, int ee)
  4833. {
  4834. uint64_t wp;
  4835. wp = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_RE_FETCH_WRITE_PTR,
  4836. ee, chan_id);
  4837. return wp;
  4838. }
  4839. EXPORT_SYMBOL(gsi_read_chan_ring_re_fetch_wp);
  4840. enum gsi_chan_prot gsi_get_chan_prot_type(int chan_hdl)
  4841. {
  4842. return gsi_ctx->chan[chan_hdl].props.prot;
  4843. }
  4844. EXPORT_SYMBOL(gsi_get_chan_prot_type);
  4845. enum gsi_chan_state gsi_get_chan_state(int chan_hdl)
  4846. {
  4847. return gsi_ctx->chan[chan_hdl].state;
  4848. }
  4849. EXPORT_SYMBOL(gsi_get_chan_state);
  4850. int gsi_get_chan_poll_mode(int chan_hdl)
  4851. {
  4852. return atomic_read(&gsi_ctx->chan[chan_hdl].poll_mode);
  4853. }
  4854. EXPORT_SYMBOL(gsi_get_chan_poll_mode);
  4855. uint32_t gsi_get_ring_len(int chan_hdl)
  4856. {
  4857. return gsi_ctx->chan[chan_hdl].ring.len;
  4858. }
  4859. EXPORT_SYMBOL(gsi_get_ring_len);
  4860. uint8_t gsi_get_chan_props_db_in_bytes(int chan_hdl)
  4861. {
  4862. return gsi_ctx->chan[chan_hdl].props.db_in_bytes;
  4863. }
  4864. EXPORT_SYMBOL(gsi_get_chan_props_db_in_bytes);
  4865. int gsi_get_peripheral_ee(void)
  4866. {
  4867. return gsi_ctx->per.ee;
  4868. }
  4869. EXPORT_SYMBOL(gsi_get_peripheral_ee);
  4870. uint32_t gsi_get_chan_stop_stm(int chan_id, int ee)
  4871. {
  4872. uint32_t ch_scratch;
  4873. ch_scratch = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_4, ee, chan_id);
  4874. /* Only bits 28 - 31 for STM */
  4875. return ((ch_scratch & 0xF0000000) >> 24);
  4876. }
  4877. EXPORT_SYMBOL(gsi_get_chan_stop_stm);
  4878. enum gsi_evt_ring_elem_size gsi_get_evt_ring_re_size(int evt_hdl)
  4879. {
  4880. return gsi_ctx->evtr[evt_hdl].props.re_size;
  4881. }
  4882. EXPORT_SYMBOL(gsi_get_evt_ring_re_size);
  4883. uint32_t gsi_get_evt_ring_len(int evt_hdl)
  4884. {
  4885. return gsi_ctx->evtr[evt_hdl].ring.len;
  4886. }
  4887. EXPORT_SYMBOL(gsi_get_evt_ring_len);
  4888. void gsi_update_almst_empty_thrshold(unsigned long chan_hdl, unsigned short threshold)
  4889. {
  4890. gsihal_write_reg_nk(GSI_EE_n_CH_k_CH_ALMST_EMPTY_THRSHOLD,
  4891. gsi_ctx->per.ee, chan_hdl, threshold);
  4892. }
  4893. EXPORT_SYMBOL(gsi_update_almst_empty_thrshold);
  4894. static union __packed gsi_channel_scratch __gsi_update_mhi_channel_scratch(
  4895. unsigned long chan_hdl, struct __packed gsi_mhi_channel_scratch mscr)
  4896. {
  4897. union __packed gsi_channel_scratch scr;
  4898. /* below sequence is not atomic. assumption is sequencer specific fields
  4899. * will remain unchanged across this sequence
  4900. */
  4901. /* READ */
  4902. scr.data.word1 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4903. gsi_ctx->per.ee, chan_hdl);
  4904. scr.data.word2 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4905. gsi_ctx->per.ee, chan_hdl);
  4906. scr.data.word3 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4907. gsi_ctx->per.ee, chan_hdl);
  4908. scr.data.word4 = gsihal_read_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4909. gsi_ctx->per.ee, chan_hdl);
  4910. /* UPDATE */
  4911. scr.mhi.polling_mode = mscr.polling_mode;
  4912. if (gsi_ctx->per.ver < GSI_VER_2_5) {
  4913. scr.mhi.max_outstanding_tre = mscr.max_outstanding_tre;
  4914. scr.mhi.outstanding_threshold = mscr.outstanding_threshold;
  4915. }
  4916. /* WRITE */
  4917. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_0,
  4918. gsi_ctx->per.ee, chan_hdl, scr.data.word1);
  4919. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_1,
  4920. gsi_ctx->per.ee, chan_hdl, scr.data.word2);
  4921. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_2,
  4922. gsi_ctx->per.ee, chan_hdl, scr.data.word3);
  4923. gsihal_write_reg_nk(GSI_EE_n_GSI_CH_k_SCRATCH_3,
  4924. gsi_ctx->per.ee, chan_hdl, scr.data.word4);
  4925. return scr;
  4926. }
  4927. /**
  4928. * gsi_get_hw_profiling_stats() - Query GSI HW profiling stats
  4929. * @stats: [out] stats blob from client populated by driver
  4930. *
  4931. * Returns: 0 on success, negative on failure
  4932. *
  4933. */
  4934. int gsi_get_hw_profiling_stats(struct gsi_hw_profiling_data *stats)
  4935. {
  4936. if (stats == NULL) {
  4937. GSIERR("bad parms NULL stats == NULL\n");
  4938. return -EINVAL;
  4939. }
  4940. stats->bp_cnt = (u64)gsihal_read_reg(
  4941. GSI_GSI_MCS_PROFILING_BP_CNT_LSB) +
  4942. ((u64)gsihal_read_reg(
  4943. GSI_GSI_MCS_PROFILING_BP_CNT_MSB) << 32);
  4944. stats->bp_and_pending_cnt = (u64)gsihal_read_reg(
  4945. GSI_GSI_MCS_PROFILING_BP_AND_PENDING_CNT_LSB) +
  4946. ((u64)gsihal_read_reg(
  4947. GSI_GSI_MCS_PROFILING_BP_AND_PENDING_CNT_MSB) << 32);
  4948. stats->mcs_busy_cnt = (u64)gsihal_read_reg(
  4949. GSI_GSI_MCS_PROFILING_MCS_BUSY_CNT_LSB) +
  4950. ((u64)gsihal_read_reg(
  4951. GSI_GSI_MCS_PROFILING_MCS_BUSY_CNT_MSB) << 32);
  4952. stats->mcs_idle_cnt = (u64)gsihal_read_reg(
  4953. GSI_GSI_MCS_PROFILING_MCS_IDLE_CNT_LSB) +
  4954. ((u64)gsihal_read_reg(
  4955. GSI_GSI_MCS_PROFILING_MCS_IDLE_CNT_MSB) << 32);
  4956. return 0;
  4957. }
  4958. /**
  4959. * gsi_get_fw_version() - Query GSI FW version
  4960. * @ver: [out] ver blob from client populated by driver
  4961. *
  4962. * Returns: 0 on success, negative on failure
  4963. *
  4964. */
  4965. int gsi_get_fw_version(struct gsi_fw_version *ver)
  4966. {
  4967. u32 raw = 0;
  4968. if (ver == NULL) {
  4969. GSIERR("bad parms: ver == NULL\n");
  4970. return -EINVAL;
  4971. }
  4972. if (gsi_ctx->per.ver < GSI_VER_3_0)
  4973. raw = gsihal_read_reg_n(GSI_GSI_INST_RAM_n,
  4974. GSI_INST_RAM_FW_VER_OFFSET);
  4975. else
  4976. raw = gsihal_read_reg_n(GSI_GSI_INST_RAM_n,
  4977. GSI_INST_RAM_FW_VER_GSI_3_0_OFFSET);
  4978. ver->hw = (raw & GSI_INST_RAM_FW_VER_HW_MASK) >>
  4979. GSI_INST_RAM_FW_VER_HW_SHIFT;
  4980. ver->flavor = (raw & GSI_INST_RAM_FW_VER_FLAVOR_MASK) >>
  4981. GSI_INST_RAM_FW_VER_FLAVOR_SHIFT;
  4982. ver->fw = (raw & GSI_INST_RAM_FW_VER_FW_MASK) >>
  4983. GSI_INST_RAM_FW_VER_FW_SHIFT;
  4984. return 0;
  4985. }
  4986. #if IS_ENABLED(CONFIG_QCOM_VA_MINIDUMP)
  4987. static int qcom_va_md_gsi_notif_handler(struct notifier_block *this,
  4988. unsigned long event, void *ptr)
  4989. {
  4990. struct va_md_entry entry;
  4991. strlcpy(entry.owner, "gsi_mini", sizeof(entry.owner));
  4992. entry.vaddr = (unsigned long)gsi_ctx;
  4993. entry.size = sizeof(struct gsi_ctx);
  4994. qcom_va_md_add_region(&entry);
  4995. return NOTIFY_OK;
  4996. }
  4997. static struct notifier_block qcom_va_md_gsi_notif_blk = {
  4998. .notifier_call = qcom_va_md_gsi_notif_handler,
  4999. .priority = INT_MAX,
  5000. };
  5001. #endif
  5002. static int msm_gsi_probe(struct platform_device *pdev)
  5003. {
  5004. struct device *dev = &pdev->dev;
  5005. int result;
  5006. pr_debug("gsi_probe\n");
  5007. gsi_ctx = devm_kzalloc(dev, sizeof(*gsi_ctx), GFP_KERNEL);
  5008. if (!gsi_ctx) {
  5009. dev_err(dev, "failed to allocated gsi context\n");
  5010. return -ENOMEM;
  5011. }
  5012. gsi_ctx->ipc_logbuf = ipc_log_context_create(GSI_IPC_LOG_PAGES,
  5013. "gsi", MINIDUMP_MASK);
  5014. if (gsi_ctx->ipc_logbuf == NULL)
  5015. GSIERR("failed to create IPC log, continue...\n");
  5016. result = of_property_read_u32(pdev->dev.of_node, "qcom,num-msi",
  5017. &gsi_ctx->msi.num);
  5018. if (result)
  5019. GSIERR("No MSIs configured\n");
  5020. else {
  5021. if (gsi_ctx->msi.num > GSI_MAX_NUM_MSI) {
  5022. GSIERR("Num MSIs %u larger than max %u, normalizing\n",
  5023. gsi_ctx->msi.num,
  5024. GSI_MAX_NUM_MSI);
  5025. gsi_ctx->msi.num = GSI_MAX_NUM_MSI;
  5026. } else GSIDBG("Num MSIs=%u\n", gsi_ctx->msi.num);
  5027. }
  5028. gsi_ctx->dev = dev;
  5029. init_completion(&gsi_ctx->gen_ee_cmd_compl);
  5030. gsi_debugfs_init();
  5031. #if IS_ENABLED(CONFIG_QCOM_VA_MINIDUMP)
  5032. result = qcom_va_md_register("gsi_mini", &qcom_va_md_gsi_notif_blk);
  5033. if(result)
  5034. GSIERR("gsi mini qcom_va_md_register failed = %d\n", result);
  5035. else
  5036. GSIDBG("gsi mini qcom_va_md_register success\n");
  5037. #endif
  5038. return 0;
  5039. }
  5040. static struct platform_driver msm_gsi_driver = {
  5041. .probe = msm_gsi_probe,
  5042. .driver = {
  5043. .name = "gsi",
  5044. .of_match_table = msm_gsi_match,
  5045. },
  5046. };
  5047. static struct platform_device *pdev;
  5048. /**
  5049. * Module Init.
  5050. */
  5051. static int __init gsi_init(void)
  5052. {
  5053. int ret;
  5054. pr_debug("%s\n", __func__);
  5055. ret = platform_driver_register(&msm_gsi_driver);
  5056. if (ret < 0)
  5057. goto out;
  5058. if (running_emulation) {
  5059. pdev = platform_device_register_simple("gsi", -1, NULL, 0);
  5060. if (IS_ERR(pdev)) {
  5061. ret = PTR_ERR(pdev);
  5062. platform_driver_unregister(&msm_gsi_driver);
  5063. goto out;
  5064. }
  5065. }
  5066. out:
  5067. return ret;
  5068. }
  5069. arch_initcall(gsi_init);
  5070. /*
  5071. * Module exit.
  5072. */
  5073. static void __exit gsi_exit(void)
  5074. {
  5075. if (running_emulation && pdev)
  5076. platform_device_unregister(pdev);
  5077. platform_driver_unregister(&msm_gsi_driver);
  5078. }
  5079. module_exit(gsi_exit);
  5080. MODULE_LICENSE("GPL v2");
  5081. MODULE_DESCRIPTION("Generic Software Interface (GSI)");