pci.c 187 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/completion.h>
  7. #include <linux/io.h>
  8. #include <linux/irq.h>
  9. #include <linux/memblock.h>
  10. #include <linux/module.h>
  11. #include <linux/msi.h>
  12. #include <linux/of.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/suspend.h>
  16. #include <linux/version.h>
  17. #include <linux/sched.h>
  18. #include "main.h"
  19. #include "bus.h"
  20. #include "debug.h"
  21. #include "pci.h"
  22. #include "pci_platform.h"
  23. #include "reg.h"
  24. #define PCI_LINK_UP 1
  25. #define PCI_LINK_DOWN 0
  26. #define SAVE_PCI_CONFIG_SPACE 1
  27. #define RESTORE_PCI_CONFIG_SPACE 0
  28. #define PCI_BAR_NUM 0
  29. #define PCI_INVALID_READ(val) ((val) == U32_MAX)
  30. #define PCI_DMA_MASK_32_BIT DMA_BIT_MASK(32)
  31. #define PCI_DMA_MASK_36_BIT DMA_BIT_MASK(36)
  32. #define PCI_DMA_MASK_64_BIT DMA_BIT_MASK(64)
  33. #define MHI_NODE_NAME "qcom,mhi"
  34. #define MHI_MSI_NAME "MHI"
  35. #define QCA6390_PATH_PREFIX "qca6390/"
  36. #define QCA6490_PATH_PREFIX "qca6490/"
  37. #define QCN7605_PATH_PREFIX "qcn7605/"
  38. #define KIWI_PATH_PREFIX "kiwi/"
  39. #define MANGO_PATH_PREFIX "mango/"
  40. #define PEACH_PATH_PREFIX "peach/"
  41. #define DEFAULT_PHY_M3_FILE_NAME "m3.bin"
  42. #define DEFAULT_PHY_UCODE_FILE_NAME "phy_ucode.elf"
  43. #define PHY_UCODE_V2_FILE_NAME "phy_ucode20.elf"
  44. #define DEFAULT_FW_FILE_NAME "amss.bin"
  45. #define FW_V2_FILE_NAME "amss20.bin"
  46. #define FW_V2_FTM_FILE_NAME "amss20_ftm.bin"
  47. #define DEVICE_MAJOR_VERSION_MASK 0xF
  48. #define WAKE_MSI_NAME "WAKE"
  49. #define DEV_RDDM_TIMEOUT 5000
  50. #define WAKE_EVENT_TIMEOUT 5000
  51. #ifdef CONFIG_CNSS_EMULATION
  52. #define EMULATION_HW 1
  53. #else
  54. #define EMULATION_HW 0
  55. #endif
  56. #define RAMDUMP_SIZE_DEFAULT 0x420000
  57. #define CNSS_256KB_SIZE 0x40000
  58. #define DEVICE_RDDM_COOKIE 0xCAFECACE
  59. static bool cnss_driver_registered;
  60. static DEFINE_SPINLOCK(pci_link_down_lock);
  61. static DEFINE_SPINLOCK(pci_reg_window_lock);
  62. static DEFINE_SPINLOCK(time_sync_lock);
  63. #define MHI_TIMEOUT_OVERWRITE_MS (plat_priv->ctrl_params.mhi_timeout)
  64. #define MHI_M2_TIMEOUT_MS (plat_priv->ctrl_params.mhi_m2_timeout)
  65. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US 1000
  66. #define WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US 2000
  67. #define FORCE_WAKE_DELAY_MIN_US 4000
  68. #define FORCE_WAKE_DELAY_MAX_US 6000
  69. #define FORCE_WAKE_DELAY_TIMEOUT_US 60000
  70. #define REG_RETRY_MAX_TIMES 3
  71. #define MHI_SUSPEND_RETRY_MAX_TIMES 3
  72. #define MHI_SUSPEND_RETRY_DELAY_US 5000
  73. #define BOOT_DEBUG_TIMEOUT_MS 7000
  74. #define HANG_DATA_LENGTH 384
  75. #define HST_HANG_DATA_OFFSET ((3 * 1024 * 1024) - HANG_DATA_LENGTH)
  76. #define HSP_HANG_DATA_OFFSET ((2 * 1024 * 1024) - HANG_DATA_LENGTH)
  77. #define AFC_SLOT_SIZE 0x1000
  78. #define AFC_MAX_SLOT 2
  79. #define AFC_MEM_SIZE (AFC_SLOT_SIZE * AFC_MAX_SLOT)
  80. #define AFC_AUTH_STATUS_OFFSET 1
  81. #define AFC_AUTH_SUCCESS 1
  82. #define AFC_AUTH_ERROR 0
  83. static const struct mhi_channel_config cnss_mhi_channels[] = {
  84. {
  85. .num = 0,
  86. .name = "LOOPBACK",
  87. .num_elements = 32,
  88. .event_ring = 1,
  89. .dir = DMA_TO_DEVICE,
  90. .ee_mask = 0x4,
  91. .pollcfg = 0,
  92. .doorbell = MHI_DB_BRST_DISABLE,
  93. .lpm_notify = false,
  94. .offload_channel = false,
  95. .doorbell_mode_switch = false,
  96. .auto_queue = false,
  97. },
  98. {
  99. .num = 1,
  100. .name = "LOOPBACK",
  101. .num_elements = 32,
  102. .event_ring = 1,
  103. .dir = DMA_FROM_DEVICE,
  104. .ee_mask = 0x4,
  105. .pollcfg = 0,
  106. .doorbell = MHI_DB_BRST_DISABLE,
  107. .lpm_notify = false,
  108. .offload_channel = false,
  109. .doorbell_mode_switch = false,
  110. .auto_queue = false,
  111. },
  112. {
  113. .num = 4,
  114. .name = "DIAG",
  115. .num_elements = 64,
  116. .event_ring = 1,
  117. .dir = DMA_TO_DEVICE,
  118. .ee_mask = 0x4,
  119. .pollcfg = 0,
  120. .doorbell = MHI_DB_BRST_DISABLE,
  121. .lpm_notify = false,
  122. .offload_channel = false,
  123. .doorbell_mode_switch = false,
  124. .auto_queue = false,
  125. },
  126. {
  127. .num = 5,
  128. .name = "DIAG",
  129. .num_elements = 64,
  130. .event_ring = 1,
  131. .dir = DMA_FROM_DEVICE,
  132. .ee_mask = 0x4,
  133. .pollcfg = 0,
  134. .doorbell = MHI_DB_BRST_DISABLE,
  135. .lpm_notify = false,
  136. .offload_channel = false,
  137. .doorbell_mode_switch = false,
  138. .auto_queue = false,
  139. },
  140. {
  141. .num = 20,
  142. .name = "IPCR",
  143. .num_elements = 64,
  144. .event_ring = 1,
  145. .dir = DMA_TO_DEVICE,
  146. .ee_mask = 0x4,
  147. .pollcfg = 0,
  148. .doorbell = MHI_DB_BRST_DISABLE,
  149. .lpm_notify = false,
  150. .offload_channel = false,
  151. .doorbell_mode_switch = false,
  152. .auto_queue = false,
  153. },
  154. {
  155. .num = 21,
  156. .name = "IPCR",
  157. .num_elements = 64,
  158. .event_ring = 1,
  159. .dir = DMA_FROM_DEVICE,
  160. .ee_mask = 0x4,
  161. .pollcfg = 0,
  162. .doorbell = MHI_DB_BRST_DISABLE,
  163. .lpm_notify = false,
  164. .offload_channel = false,
  165. .doorbell_mode_switch = false,
  166. .auto_queue = true,
  167. },
  168. /* All MHI satellite config to be at the end of data struct */
  169. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  170. {
  171. .num = 50,
  172. .name = "ADSP_0",
  173. .num_elements = 64,
  174. .event_ring = 3,
  175. .dir = DMA_BIDIRECTIONAL,
  176. .ee_mask = 0x4,
  177. .pollcfg = 0,
  178. .doorbell = MHI_DB_BRST_DISABLE,
  179. .lpm_notify = false,
  180. .offload_channel = true,
  181. .doorbell_mode_switch = false,
  182. .auto_queue = false,
  183. },
  184. {
  185. .num = 51,
  186. .name = "ADSP_1",
  187. .num_elements = 64,
  188. .event_ring = 3,
  189. .dir = DMA_BIDIRECTIONAL,
  190. .ee_mask = 0x4,
  191. .pollcfg = 0,
  192. .doorbell = MHI_DB_BRST_DISABLE,
  193. .lpm_notify = false,
  194. .offload_channel = true,
  195. .doorbell_mode_switch = false,
  196. .auto_queue = false,
  197. },
  198. {
  199. .num = 70,
  200. .name = "ADSP_2",
  201. .num_elements = 64,
  202. .event_ring = 3,
  203. .dir = DMA_BIDIRECTIONAL,
  204. .ee_mask = 0x4,
  205. .pollcfg = 0,
  206. .doorbell = MHI_DB_BRST_DISABLE,
  207. .lpm_notify = false,
  208. .offload_channel = true,
  209. .doorbell_mode_switch = false,
  210. .auto_queue = false,
  211. },
  212. {
  213. .num = 71,
  214. .name = "ADSP_3",
  215. .num_elements = 64,
  216. .event_ring = 3,
  217. .dir = DMA_BIDIRECTIONAL,
  218. .ee_mask = 0x4,
  219. .pollcfg = 0,
  220. .doorbell = MHI_DB_BRST_DISABLE,
  221. .lpm_notify = false,
  222. .offload_channel = true,
  223. .doorbell_mode_switch = false,
  224. .auto_queue = false,
  225. },
  226. #endif
  227. };
  228. static const struct mhi_channel_config cnss_mhi_channels_genoa[] = {
  229. {
  230. .num = 0,
  231. .name = "LOOPBACK",
  232. .num_elements = 32,
  233. .event_ring = 1,
  234. .dir = DMA_TO_DEVICE,
  235. .ee_mask = 0x4,
  236. .pollcfg = 0,
  237. .doorbell = MHI_DB_BRST_DISABLE,
  238. .lpm_notify = false,
  239. .offload_channel = false,
  240. .doorbell_mode_switch = false,
  241. .auto_queue = false,
  242. },
  243. {
  244. .num = 1,
  245. .name = "LOOPBACK",
  246. .num_elements = 32,
  247. .event_ring = 1,
  248. .dir = DMA_FROM_DEVICE,
  249. .ee_mask = 0x4,
  250. .pollcfg = 0,
  251. .doorbell = MHI_DB_BRST_DISABLE,
  252. .lpm_notify = false,
  253. .offload_channel = false,
  254. .doorbell_mode_switch = false,
  255. .auto_queue = false,
  256. },
  257. {
  258. .num = 4,
  259. .name = "DIAG",
  260. .num_elements = 64,
  261. .event_ring = 1,
  262. .dir = DMA_TO_DEVICE,
  263. .ee_mask = 0x4,
  264. .pollcfg = 0,
  265. .doorbell = MHI_DB_BRST_DISABLE,
  266. .lpm_notify = false,
  267. .offload_channel = false,
  268. .doorbell_mode_switch = false,
  269. .auto_queue = false,
  270. },
  271. {
  272. .num = 5,
  273. .name = "DIAG",
  274. .num_elements = 64,
  275. .event_ring = 1,
  276. .dir = DMA_FROM_DEVICE,
  277. .ee_mask = 0x4,
  278. .pollcfg = 0,
  279. .doorbell = MHI_DB_BRST_DISABLE,
  280. .lpm_notify = false,
  281. .offload_channel = false,
  282. .doorbell_mode_switch = false,
  283. .auto_queue = false,
  284. },
  285. {
  286. .num = 16,
  287. .name = "IPCR",
  288. .num_elements = 64,
  289. .event_ring = 1,
  290. .dir = DMA_TO_DEVICE,
  291. .ee_mask = 0x4,
  292. .pollcfg = 0,
  293. .doorbell = MHI_DB_BRST_DISABLE,
  294. .lpm_notify = false,
  295. .offload_channel = false,
  296. .doorbell_mode_switch = false,
  297. .auto_queue = false,
  298. },
  299. {
  300. .num = 17,
  301. .name = "IPCR",
  302. .num_elements = 64,
  303. .event_ring = 1,
  304. .dir = DMA_FROM_DEVICE,
  305. .ee_mask = 0x4,
  306. .pollcfg = 0,
  307. .doorbell = MHI_DB_BRST_DISABLE,
  308. .lpm_notify = false,
  309. .offload_channel = false,
  310. .doorbell_mode_switch = false,
  311. .auto_queue = true,
  312. },
  313. };
  314. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0))
  315. static struct mhi_event_config cnss_mhi_events[] = {
  316. #else
  317. static const struct mhi_event_config cnss_mhi_events[] = {
  318. #endif
  319. {
  320. .num_elements = 32,
  321. .irq_moderation_ms = 0,
  322. .irq = 1,
  323. .mode = MHI_DB_BRST_DISABLE,
  324. .data_type = MHI_ER_CTRL,
  325. .priority = 0,
  326. .hardware_event = false,
  327. .client_managed = false,
  328. .offload_channel = false,
  329. },
  330. {
  331. .num_elements = 256,
  332. .irq_moderation_ms = 0,
  333. .irq = 2,
  334. .mode = MHI_DB_BRST_DISABLE,
  335. .priority = 1,
  336. .hardware_event = false,
  337. .client_managed = false,
  338. .offload_channel = false,
  339. },
  340. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  341. {
  342. .num_elements = 32,
  343. .irq_moderation_ms = 0,
  344. .irq = 1,
  345. .mode = MHI_DB_BRST_DISABLE,
  346. .data_type = MHI_ER_BW_SCALE,
  347. .priority = 2,
  348. .hardware_event = false,
  349. .client_managed = false,
  350. .offload_channel = false,
  351. },
  352. #endif
  353. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  354. {
  355. .num_elements = 256,
  356. .irq_moderation_ms = 0,
  357. .irq = 2,
  358. .mode = MHI_DB_BRST_DISABLE,
  359. .data_type = MHI_ER_DATA,
  360. .priority = 1,
  361. .hardware_event = false,
  362. .client_managed = true,
  363. .offload_channel = true,
  364. },
  365. #endif
  366. };
  367. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  368. #define CNSS_MHI_SATELLITE_CH_CFG_COUNT 4
  369. #define CNSS_MHI_SATELLITE_EVT_COUNT 1
  370. #else
  371. #define CNSS_MHI_SATELLITE_CH_CFG_COUNT 0
  372. #define CNSS_MHI_SATELLITE_EVT_COUNT 0
  373. #endif
  374. static const struct mhi_controller_config cnss_mhi_config_default = {
  375. #if IS_ENABLED(CONFIG_MHI_SATELLITE)
  376. .max_channels = 72,
  377. #else
  378. .max_channels = 32,
  379. #endif
  380. .timeout_ms = 10000,
  381. .use_bounce_buf = false,
  382. .buf_len = 0x8000,
  383. .num_channels = ARRAY_SIZE(cnss_mhi_channels),
  384. .ch_cfg = cnss_mhi_channels,
  385. .num_events = ARRAY_SIZE(cnss_mhi_events),
  386. .event_cfg = cnss_mhi_events,
  387. .m2_no_db = true,
  388. };
  389. static const struct mhi_controller_config cnss_mhi_config_genoa = {
  390. .max_channels = 32,
  391. .timeout_ms = 10000,
  392. .use_bounce_buf = false,
  393. .buf_len = 0x8000,
  394. .num_channels = ARRAY_SIZE(cnss_mhi_channels_genoa),
  395. .ch_cfg = cnss_mhi_channels_genoa,
  396. .num_events = ARRAY_SIZE(cnss_mhi_events) -
  397. CNSS_MHI_SATELLITE_EVT_COUNT,
  398. .event_cfg = cnss_mhi_events,
  399. .m2_no_db = true,
  400. .bhie_offset = 0x0324,
  401. };
  402. static const struct mhi_controller_config cnss_mhi_config_no_satellite = {
  403. .max_channels = 32,
  404. .timeout_ms = 10000,
  405. .use_bounce_buf = false,
  406. .buf_len = 0x8000,
  407. .num_channels = ARRAY_SIZE(cnss_mhi_channels) -
  408. CNSS_MHI_SATELLITE_CH_CFG_COUNT,
  409. .ch_cfg = cnss_mhi_channels,
  410. .num_events = ARRAY_SIZE(cnss_mhi_events) -
  411. CNSS_MHI_SATELLITE_EVT_COUNT,
  412. .event_cfg = cnss_mhi_events,
  413. .m2_no_db = true,
  414. };
  415. static struct cnss_pci_reg ce_src[] = {
  416. { "SRC_RING_BASE_LSB", CE_SRC_RING_BASE_LSB_OFFSET },
  417. { "SRC_RING_BASE_MSB", CE_SRC_RING_BASE_MSB_OFFSET },
  418. { "SRC_RING_ID", CE_SRC_RING_ID_OFFSET },
  419. { "SRC_RING_MISC", CE_SRC_RING_MISC_OFFSET },
  420. { "SRC_CTRL", CE_SRC_CTRL_OFFSET },
  421. { "SRC_R0_CE_CH_SRC_IS", CE_SRC_R0_CE_CH_SRC_IS_OFFSET },
  422. { "SRC_RING_HP", CE_SRC_RING_HP_OFFSET },
  423. { "SRC_RING_TP", CE_SRC_RING_TP_OFFSET },
  424. { NULL },
  425. };
  426. static struct cnss_pci_reg ce_dst[] = {
  427. { "DEST_RING_BASE_LSB", CE_DEST_RING_BASE_LSB_OFFSET },
  428. { "DEST_RING_BASE_MSB", CE_DEST_RING_BASE_MSB_OFFSET },
  429. { "DEST_RING_ID", CE_DEST_RING_ID_OFFSET },
  430. { "DEST_RING_MISC", CE_DEST_RING_MISC_OFFSET },
  431. { "DEST_CTRL", CE_DEST_CTRL_OFFSET },
  432. { "CE_CH_DST_IS", CE_CH_DST_IS_OFFSET },
  433. { "CE_CH_DEST_CTRL2", CE_CH_DEST_CTRL2_OFFSET },
  434. { "DEST_RING_HP", CE_DEST_RING_HP_OFFSET },
  435. { "DEST_RING_TP", CE_DEST_RING_TP_OFFSET },
  436. { "STATUS_RING_BASE_LSB", CE_STATUS_RING_BASE_LSB_OFFSET },
  437. { "STATUS_RING_BASE_MSB", CE_STATUS_RING_BASE_MSB_OFFSET },
  438. { "STATUS_RING_ID", CE_STATUS_RING_ID_OFFSET },
  439. { "STATUS_RING_MISC", CE_STATUS_RING_MISC_OFFSET },
  440. { "STATUS_RING_HP", CE_STATUS_RING_HP_OFFSET },
  441. { "STATUS_RING_TP", CE_STATUS_RING_TP_OFFSET },
  442. { NULL },
  443. };
  444. static struct cnss_pci_reg ce_cmn[] = {
  445. { "GXI_ERR_INTS", CE_COMMON_GXI_ERR_INTS },
  446. { "GXI_ERR_STATS", CE_COMMON_GXI_ERR_STATS },
  447. { "GXI_WDOG_STATUS", CE_COMMON_GXI_WDOG_STATUS },
  448. { "TARGET_IE_0", CE_COMMON_TARGET_IE_0 },
  449. { "TARGET_IE_1", CE_COMMON_TARGET_IE_1 },
  450. { NULL },
  451. };
  452. static struct cnss_pci_reg qdss_csr[] = {
  453. { "QDSSCSR_ETRIRQCTRL", QDSS_APB_DEC_CSR_ETRIRQCTRL_OFFSET },
  454. { "QDSSCSR_PRESERVEETF", QDSS_APB_DEC_CSR_PRESERVEETF_OFFSET },
  455. { "QDSSCSR_PRESERVEETR0", QDSS_APB_DEC_CSR_PRESERVEETR0_OFFSET },
  456. { "QDSSCSR_PRESERVEETR1", QDSS_APB_DEC_CSR_PRESERVEETR1_OFFSET },
  457. { NULL },
  458. };
  459. static struct cnss_pci_reg pci_scratch[] = {
  460. { "PCIE_SCRATCH_0", PCIE_SCRATCH_0_SOC_PCIE_REG },
  461. { "PCIE_SCRATCH_1", PCIE_SCRATCH_1_SOC_PCIE_REG },
  462. { "PCIE_SCRATCH_2", PCIE_SCRATCH_2_SOC_PCIE_REG },
  463. { NULL },
  464. };
  465. /* First field of the structure is the device bit mask. Use
  466. * enum cnss_pci_reg_mask as reference for the value.
  467. */
  468. static struct cnss_misc_reg wcss_reg_access_seq[] = {
  469. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  470. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x802},
  471. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  472. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_PLL_MODE, 0},
  473. {1, 1, QCA6390_GCC_DEBUG_CLK_CTL, 0x805},
  474. {1, 0, QCA6390_GCC_DEBUG_CLK_CTL, 0},
  475. {1, 0, QCA6390_WCSS_WFSS_PMM_WFSS_PMM_R0_PMM_CTRL, 0},
  476. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_CX_CSR, 0},
  477. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_RAW_STAT, 0},
  478. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_EN, 0},
  479. {1, 0, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_STS, 0},
  480. {1, 1, QCA6390_WCSS_PMM_TOP_PMU_TESTBUS_CTL, 0xD},
  481. {1, 0, QCA6390_WCSS_PMM_TOP_TESTBUS_STS, 0},
  482. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  483. {1, 1, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG, 0},
  484. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x8},
  485. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  486. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_STS, 0},
  487. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_CTL, 0},
  488. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_0, 0},
  489. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_SPM_SLP_SEQ_ENTRY_9, 0},
  490. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS0, 0},
  491. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS1, 0},
  492. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS2, 0},
  493. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS3, 0},
  494. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS4, 0},
  495. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS5, 0},
  496. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_STATUS6, 0},
  497. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE0, 0},
  498. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE1, 0},
  499. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE2, 0},
  500. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE3, 0},
  501. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE4, 0},
  502. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE5, 0},
  503. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_ENABLE6, 0},
  504. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING0, 0},
  505. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING1, 0},
  506. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING2, 0},
  507. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING3, 0},
  508. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING4, 0},
  509. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING5, 0},
  510. {1, 0, QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_L2VIC_INT_PENDING6, 0},
  511. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30040},
  512. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  513. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  514. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  515. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  516. {1, 1, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0x30105},
  517. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  518. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  519. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  520. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  521. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  522. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  523. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  524. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_VALUE, 0},
  525. {1, 0, QCA6390_WCSS_Q6SS_PUBCSR_QDSP6SS_TEST_BUS_CTL, 0},
  526. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_NOC_CBCR, 0},
  527. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_AHB_CBCR, 0},
  528. {1, 0, QCA6390_WCSS_CC_WCSS_UMAC_GDSCR, 0},
  529. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN1_GDSCR, 0},
  530. {1, 0, QCA6390_WCSS_CC_WCSS_WLAN2_GDSCR, 0},
  531. {1, 0, QCA6390_WCSS_PMM_TOP_PMM_INT_CLR, 0},
  532. {1, 0, QCA6390_WCSS_PMM_TOP_AON_INT_STICKY_EN, 0},
  533. };
  534. static struct cnss_misc_reg pcie_reg_access_seq[] = {
  535. {1, 0, QCA6390_PCIE_PCIE_WCSS_STATUS_FOR_DEBUG_LOW_PCIE_LOCAL_REG, 0},
  536. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  537. {1, 1, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0x18},
  538. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  539. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_MASK_SOC_PCIE_REG, 0},
  540. {1, 0, QCA6390_PCIE_SOC_PCIE_WRAP_INTR_STATUS_SOC_PCIE_REG, 0},
  541. {1, 0, QCA6390_PCIE_SOC_COMMIT_REPLAY_SOC_PCIE_REG, 0},
  542. {1, 0, QCA6390_TLMM_GPIO_IN_OUT57, 0},
  543. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG57, 0},
  544. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS57, 0},
  545. {1, 0, QCA6390_TLMM_GPIO_IN_OUT59, 0},
  546. {1, 0, QCA6390_TLMM_GPIO_INTR_CFG59, 0},
  547. {1, 0, QCA6390_TLMM_GPIO_INTR_STATUS59, 0},
  548. {1, 0, QCA6390_PCIE_PCIE_PARF_LTSSM, 0},
  549. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS, 0},
  550. {1, 0, QCA6390_PCIE_PCIE_PARF_PM_STTS_1, 0},
  551. {1, 0, QCA6390_PCIE_PCIE_PARF_INT_STATUS, 0},
  552. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_STATUS, 0},
  553. {1, 0, QCA6390_PCIE_PCIE_INT_ALL_MASK, 0},
  554. {1, 0, QCA6390_PCIE_PCIE_PARF_BDF_TO_SID_CFG, 0},
  555. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  556. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_3, 0},
  557. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_CLOCK_RESET_CTRL, 0},
  558. {1, 0, QCA6390_PCIE_PCIE_PARF_MHI_BASE_ADDR_LOWER, 0},
  559. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_STATUS, 0},
  560. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_MODE_HANDLER_CFG, 0},
  561. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  562. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1SUB, 0},
  563. {1, 0, QCA6390_PCIE_PCIE_CORE_CONFIG, 0},
  564. {1, 0, QCA6390_PCIE_PCIE_PARF_L1SS_SLEEP_NO_MHI_ACCESS_HANDLER_RD_4, 0},
  565. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L2, 0},
  566. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_PM_LINKST_IN_L1, 0},
  567. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1, 0},
  568. {1, 0, QCA6390_PCIE_PCIE_PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2, 0},
  569. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_HIGH, 0},
  570. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSSAON_PCIE_SR_STATUS_LOW, 0},
  571. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_HIGH, 0},
  572. {1, 0, QCA6390_PCIE_PCIE_LOCAL_REG_WCSS_STATUS_FOR_DEBUG_LOW, 0},
  573. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_STATUS_REG2, 0},
  574. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_STATUS_REG2, 0},
  575. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN2_CFG_REG1, 0},
  576. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_PMM_WLAN1_CFG_REG1, 0},
  577. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN2_APS_STATUS_REG1, 0},
  578. {1, 0, QCA6390_WFSS_PMM_WFSS_PMM_R0_WLAN1_APS_STATUS_REG1, 0},
  579. {1, 0, QCA6390_PCIE_PCIE_BHI_EXECENV_REG, 0},
  580. };
  581. static struct cnss_misc_reg wlaon_reg_access_seq[] = {
  582. {3, 0, WLAON_SOC_POWER_CTRL, 0},
  583. {3, 0, WLAON_SOC_PWR_WDG_BARK_THRSHD, 0},
  584. {3, 0, WLAON_SOC_PWR_WDG_BITE_THRSHD, 0},
  585. {3, 0, WLAON_SW_COLD_RESET, 0},
  586. {3, 0, WLAON_RFA_MEM_SLP_NRET_N_OVERRIDE, 0},
  587. {3, 0, WLAON_GDSC_DELAY_SETTING, 0},
  588. {3, 0, WLAON_GDSC_DELAY_SETTING2, 0},
  589. {3, 0, WLAON_WL_PWR_STATUS_REG, 0},
  590. {3, 0, WLAON_WL_AON_DBG_CFG_REG, 0},
  591. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP0_REG, 0},
  592. {2, 0, WLAON_WL_AON_DBG_ENABLE_GRP1_REG, 0},
  593. {2, 0, WLAON_WL_AON_APM_CFG_CTRL0, 0},
  594. {2, 0, WLAON_WL_AON_APM_CFG_CTRL1, 0},
  595. {2, 0, WLAON_WL_AON_APM_CFG_CTRL2, 0},
  596. {2, 0, WLAON_WL_AON_APM_CFG_CTRL3, 0},
  597. {2, 0, WLAON_WL_AON_APM_CFG_CTRL4, 0},
  598. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5, 0},
  599. {2, 0, WLAON_WL_AON_APM_CFG_CTRL5_1, 0},
  600. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6, 0},
  601. {2, 0, WLAON_WL_AON_APM_CFG_CTRL6_1, 0},
  602. {2, 0, WLAON_WL_AON_APM_CFG_CTRL7, 0},
  603. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8, 0},
  604. {2, 0, WLAON_WL_AON_APM_CFG_CTRL8_1, 0},
  605. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9, 0},
  606. {2, 0, WLAON_WL_AON_APM_CFG_CTRL9_1, 0},
  607. {2, 0, WLAON_WL_AON_APM_CFG_CTRL10, 0},
  608. {2, 0, WLAON_WL_AON_APM_CFG_CTRL11, 0},
  609. {2, 0, WLAON_WL_AON_APM_CFG_CTRL12, 0},
  610. {2, 0, WLAON_WL_AON_APM_OVERRIDE_REG, 0},
  611. {2, 0, WLAON_WL_AON_CXPC_REG, 0},
  612. {2, 0, WLAON_WL_AON_APM_STATUS0, 0},
  613. {2, 0, WLAON_WL_AON_APM_STATUS1, 0},
  614. {2, 0, WLAON_WL_AON_APM_STATUS2, 0},
  615. {2, 0, WLAON_WL_AON_APM_STATUS3, 0},
  616. {2, 0, WLAON_WL_AON_APM_STATUS4, 0},
  617. {2, 0, WLAON_WL_AON_APM_STATUS5, 0},
  618. {2, 0, WLAON_WL_AON_APM_STATUS6, 0},
  619. {3, 0, WLAON_GLOBAL_COUNTER_CTRL1, 0},
  620. {3, 0, WLAON_GLOBAL_COUNTER_CTRL6, 0},
  621. {3, 0, WLAON_GLOBAL_COUNTER_CTRL7, 0},
  622. {3, 0, WLAON_GLOBAL_COUNTER_CTRL3, 0},
  623. {3, 0, WLAON_GLOBAL_COUNTER_CTRL4, 0},
  624. {3, 0, WLAON_GLOBAL_COUNTER_CTRL5, 0},
  625. {3, 0, WLAON_GLOBAL_COUNTER_CTRL8, 0},
  626. {3, 0, WLAON_GLOBAL_COUNTER_CTRL2, 0},
  627. {3, 0, WLAON_GLOBAL_COUNTER_CTRL9, 0},
  628. {3, 0, WLAON_RTC_CLK_CAL_CTRL1, 0},
  629. {3, 0, WLAON_RTC_CLK_CAL_CTRL2, 0},
  630. {3, 0, WLAON_RTC_CLK_CAL_CTRL3, 0},
  631. {3, 0, WLAON_RTC_CLK_CAL_CTRL4, 0},
  632. {3, 0, WLAON_RTC_CLK_CAL_CTRL5, 0},
  633. {3, 0, WLAON_RTC_CLK_CAL_CTRL6, 0},
  634. {3, 0, WLAON_RTC_CLK_CAL_CTRL7, 0},
  635. {3, 0, WLAON_RTC_CLK_CAL_CTRL8, 0},
  636. {3, 0, WLAON_RTC_CLK_CAL_CTRL9, 0},
  637. {3, 0, WLAON_WCSSAON_CONFIG_REG, 0},
  638. {3, 0, WLAON_WLAN_OEM_DEBUG_REG, 0},
  639. {3, 0, WLAON_WLAN_RAM_DUMP_REG, 0},
  640. {3, 0, WLAON_QDSS_WCSS_REG, 0},
  641. {3, 0, WLAON_QDSS_WCSS_ACK, 0},
  642. {3, 0, WLAON_WL_CLK_CNTL_KDF_REG, 0},
  643. {3, 0, WLAON_WL_CLK_CNTL_PMU_HFRC_REG, 0},
  644. {3, 0, WLAON_QFPROM_PWR_CTRL_REG, 0},
  645. {3, 0, WLAON_DLY_CONFIG, 0},
  646. {3, 0, WLAON_WLAON_Q6_IRQ_REG, 0},
  647. {3, 0, WLAON_PCIE_INTF_SW_CFG_REG, 0},
  648. {3, 0, WLAON_PCIE_INTF_STICKY_SW_CFG_REG, 0},
  649. {3, 0, WLAON_PCIE_INTF_PHY_SW_CFG_REG, 0},
  650. {3, 0, WLAON_PCIE_INTF_PHY_NOCSR_SW_CFG_REG, 0},
  651. {3, 0, WLAON_Q6_COOKIE_BIT, 0},
  652. {3, 0, WLAON_WARM_SW_ENTRY, 0},
  653. {3, 0, WLAON_RESET_DBG_SW_ENTRY, 0},
  654. {3, 0, WLAON_WL_PMUNOC_CFG_REG, 0},
  655. {3, 0, WLAON_RESET_CAUSE_CFG_REG, 0},
  656. {3, 0, WLAON_SOC_WCSSAON_WAKEUP_IRQ_7_EN_REG, 0},
  657. {3, 0, WLAON_DEBUG, 0},
  658. {3, 0, WLAON_SOC_PARAMETERS, 0},
  659. {3, 0, WLAON_WLPM_SIGNAL, 0},
  660. {3, 0, WLAON_SOC_RESET_CAUSE_REG, 0},
  661. {3, 0, WLAON_WAKEUP_PCIE_SOC_REG, 0},
  662. {3, 0, WLAON_PBL_STACK_CANARY, 0},
  663. {3, 0, WLAON_MEM_TOT_NUM_GRP_REG, 0},
  664. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP0_REG, 0},
  665. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP1_REG, 0},
  666. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP2_REG, 0},
  667. {3, 0, WLAON_MEM_TOT_BANKS_IN_GRP3_REG, 0},
  668. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP0_REG, 0},
  669. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP1_REG, 0},
  670. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP2_REG, 0},
  671. {3, 0, WLAON_MEM_TOT_SIZE_IN_GRP3_REG, 0},
  672. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP0_REG, 0},
  673. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP1_REG, 0},
  674. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP2_REG, 0},
  675. {3, 0, WLAON_MEM_SLP_NRET_OVERRIDE_GRP3_REG, 0},
  676. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP0_REG, 0},
  677. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP1_REG, 0},
  678. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP2_REG, 0},
  679. {3, 0, WLAON_MEM_SLP_RET_OVERRIDE_GRP3_REG, 0},
  680. {3, 0, WLAON_MEM_CNT_SEL_REG, 0},
  681. {3, 0, WLAON_MEM_NO_EXTBHS_REG, 0},
  682. {3, 0, WLAON_MEM_DEBUG_REG, 0},
  683. {3, 0, WLAON_MEM_DEBUG_BUS_REG, 0},
  684. {3, 0, WLAON_MEM_REDUN_CFG_REG, 0},
  685. {3, 0, WLAON_WL_AON_SPARE2, 0},
  686. {3, 0, WLAON_VSEL_CFG_FOR_WL_RET_DISABLE_REG, 0},
  687. {3, 0, WLAON_BTFM_WLAN_IPC_STATUS_REG, 0},
  688. {3, 0, WLAON_MPM_COUNTER_CHICKEN_BITS, 0},
  689. {3, 0, WLAON_WLPM_CHICKEN_BITS, 0},
  690. {3, 0, WLAON_PCIE_PHY_PWR_REG, 0},
  691. {3, 0, WLAON_WL_CLK_CNTL_PMU_LPO2M_REG, 0},
  692. {3, 0, WLAON_WL_SS_ROOT_CLK_SWITCH_REG, 0},
  693. {3, 0, WLAON_POWERCTRL_PMU_REG, 0},
  694. {3, 0, WLAON_POWERCTRL_MEM_REG, 0},
  695. {3, 0, WLAON_PCIE_PWR_CTRL_REG, 0},
  696. {3, 0, WLAON_SOC_PWR_PROFILE_REG, 0},
  697. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_HI_REG, 0},
  698. {3, 0, WLAON_WCSSAON_PCIE_SR_STATUS_LO_REG, 0},
  699. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_HI_REG, 0},
  700. {3, 0, WLAON_WCSS_TCSR_PMM_SR_STATUS_LO_REG, 0},
  701. {3, 0, WLAON_MEM_SVS_CFG_REG, 0},
  702. {3, 0, WLAON_CMN_AON_MISC_REG, 0},
  703. {3, 0, WLAON_INTR_STATUS, 0},
  704. {2, 0, WLAON_INTR_ENABLE, 0},
  705. {2, 0, WLAON_NOC_DBG_BUS_SEL_REG, 0},
  706. {2, 0, WLAON_NOC_DBG_BUS_REG, 0},
  707. {2, 0, WLAON_WL_CTRL_MISC_REG, 0},
  708. {2, 0, WLAON_DBG_STATUS0, 0},
  709. {2, 0, WLAON_DBG_STATUS1, 0},
  710. {2, 0, WLAON_TIMERSYNC_OFFSET_L, 0},
  711. {2, 0, WLAON_TIMERSYNC_OFFSET_H, 0},
  712. {2, 0, WLAON_PMU_LDO_SETTLE_REG, 0},
  713. };
  714. static struct cnss_misc_reg syspm_reg_access_seq[] = {
  715. {1, 0, QCA6390_SYSPM_SYSPM_PWR_STATUS, 0},
  716. {1, 0, QCA6390_SYSPM_DBG_BTFM_AON_REG, 0},
  717. {1, 0, QCA6390_SYSPM_DBG_BUS_SEL_REG, 0},
  718. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  719. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  720. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  721. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  722. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  723. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  724. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  725. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  726. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  727. {1, 0, QCA6390_SYSPM_WCSSAON_SR_STATUS, 0},
  728. };
  729. static struct cnss_print_optimize print_optimize;
  730. #define WCSS_REG_SIZE ARRAY_SIZE(wcss_reg_access_seq)
  731. #define PCIE_REG_SIZE ARRAY_SIZE(pcie_reg_access_seq)
  732. #define WLAON_REG_SIZE ARRAY_SIZE(wlaon_reg_access_seq)
  733. #define SYSPM_REG_SIZE ARRAY_SIZE(syspm_reg_access_seq)
  734. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv);
  735. static void cnss_pci_suspend_pwroff(struct pci_dev *pci_dev);
  736. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev);
  737. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  738. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  739. {
  740. mhi_debug_reg_dump(pci_priv->mhi_ctrl);
  741. }
  742. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  743. {
  744. mhi_dump_sfr(pci_priv->mhi_ctrl);
  745. }
  746. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  747. u32 cookie)
  748. {
  749. return mhi_scan_rddm_cookie(pci_priv->mhi_ctrl, cookie);
  750. }
  751. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  752. bool notify_clients)
  753. {
  754. return mhi_pm_fast_suspend(pci_priv->mhi_ctrl, notify_clients);
  755. }
  756. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  757. bool notify_clients)
  758. {
  759. return mhi_pm_fast_resume(pci_priv->mhi_ctrl, notify_clients);
  760. }
  761. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  762. u32 timeout)
  763. {
  764. return mhi_set_m2_timeout_ms(pci_priv->mhi_ctrl, timeout);
  765. }
  766. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  767. int timeout_us, bool in_panic)
  768. {
  769. return mhi_device_get_sync_atomic(pci_priv->mhi_ctrl->mhi_dev,
  770. timeout_us, in_panic);
  771. }
  772. #ifdef CONFIG_CNSS2_SMMU_DB_SUPPORT
  773. static int cnss_mhi_host_notify_db_disable_trace(struct cnss_pci_data *pci_priv)
  774. {
  775. return mhi_host_notify_db_disable_trace(pci_priv->mhi_ctrl);
  776. }
  777. #endif
  778. static void
  779. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  780. int (*cb)(struct mhi_controller *mhi_ctrl,
  781. struct mhi_link_info *link_info))
  782. {
  783. mhi_controller_set_bw_scale_cb(pci_priv->mhi_ctrl, cb);
  784. }
  785. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  786. {
  787. return mhi_force_reset(pci_priv->mhi_ctrl);
  788. }
  789. void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  790. phys_addr_t base)
  791. {
  792. return mhi_controller_set_base(pci_priv->mhi_ctrl, base);
  793. }
  794. #else
  795. static void cnss_mhi_debug_reg_dump(struct cnss_pci_data *pci_priv)
  796. {
  797. }
  798. static void cnss_mhi_dump_sfr(struct cnss_pci_data *pci_priv)
  799. {
  800. }
  801. static bool cnss_mhi_scan_rddm_cookie(struct cnss_pci_data *pci_priv,
  802. u32 cookie)
  803. {
  804. return false;
  805. }
  806. static int cnss_mhi_pm_fast_suspend(struct cnss_pci_data *pci_priv,
  807. bool notify_clients)
  808. {
  809. return -EOPNOTSUPP;
  810. }
  811. static int cnss_mhi_pm_fast_resume(struct cnss_pci_data *pci_priv,
  812. bool notify_clients)
  813. {
  814. return -EOPNOTSUPP;
  815. }
  816. static void cnss_mhi_set_m2_timeout_ms(struct cnss_pci_data *pci_priv,
  817. u32 timeout)
  818. {
  819. }
  820. static int cnss_mhi_device_get_sync_atomic(struct cnss_pci_data *pci_priv,
  821. int timeout_us, bool in_panic)
  822. {
  823. return -EOPNOTSUPP;
  824. }
  825. #ifdef CONFIG_CNSS2_SMMU_DB_SUPPORT
  826. static int cnss_mhi_host_notify_db_disable_trace(struct cnss_pci_data *pci_priv)
  827. {
  828. return -EOPNOTSUPP;
  829. }
  830. #endif
  831. static void
  832. cnss_mhi_controller_set_bw_scale_cb(struct cnss_pci_data *pci_priv,
  833. int (*cb)(struct mhi_controller *mhi_ctrl,
  834. struct mhi_link_info *link_info))
  835. {
  836. }
  837. static int cnss_mhi_force_reset(struct cnss_pci_data *pci_priv)
  838. {
  839. return -EOPNOTSUPP;
  840. }
  841. void cnss_mhi_controller_set_base(struct cnss_pci_data *pci_priv,
  842. phys_addr_t base)
  843. {
  844. }
  845. #endif /* CONFIG_MHI_BUS_MISC */
  846. #ifdef CONFIG_CNSS2_SMMU_DB_SUPPORT
  847. #define CNSS_MHI_WAKE_TIMEOUT 500000
  848. static void cnss_record_smmu_fault_timestamp(struct cnss_pci_data *pci_priv,
  849. enum cnss_smmu_fault_time id)
  850. {
  851. if (id >= SMMU_CB_MAX)
  852. return;
  853. pci_priv->smmu_fault_timestamp[id] = sched_clock();
  854. }
  855. static void cnss_pci_smmu_fault_handler_irq(struct iommu_domain *domain,
  856. void *handler_token)
  857. {
  858. struct cnss_pci_data *pci_priv = handler_token;
  859. int ret = 0;
  860. cnss_record_smmu_fault_timestamp(pci_priv, SMMU_CB_ENTRY);
  861. ret = cnss_mhi_device_get_sync_atomic(pci_priv,
  862. CNSS_MHI_WAKE_TIMEOUT, true);
  863. if (ret < 0) {
  864. cnss_pr_err("Failed to bring mhi in M0 state, ret %d\n", ret);
  865. return;
  866. }
  867. cnss_record_smmu_fault_timestamp(pci_priv, SMMU_CB_DOORBELL_RING);
  868. ret = cnss_mhi_host_notify_db_disable_trace(pci_priv);
  869. if (ret < 0)
  870. cnss_pr_err("Fail to notify wlan fw to stop trace collection, ret %d\n", ret);
  871. cnss_record_smmu_fault_timestamp(pci_priv, SMMU_CB_EXIT);
  872. }
  873. void cnss_register_iommu_fault_handler_irq(struct cnss_pci_data *pci_priv)
  874. {
  875. qcom_iommu_set_fault_handler_irq(pci_priv->iommu_domain,
  876. cnss_pci_smmu_fault_handler_irq, pci_priv);
  877. }
  878. #else
  879. void cnss_register_iommu_fault_handler_irq(struct cnss_pci_data *pci_priv)
  880. {
  881. }
  882. #endif
  883. int cnss_pci_check_link_status(struct cnss_pci_data *pci_priv)
  884. {
  885. u16 device_id;
  886. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  887. cnss_pr_dbg("%ps: PCIe link is in suspend state\n",
  888. (void *)_RET_IP_);
  889. return -EACCES;
  890. }
  891. if (pci_priv->pci_link_down_ind) {
  892. cnss_pr_err("%ps: PCIe link is down\n", (void *)_RET_IP_);
  893. return -EIO;
  894. }
  895. pci_read_config_word(pci_priv->pci_dev, PCI_DEVICE_ID, &device_id);
  896. if (device_id != pci_priv->device_id) {
  897. cnss_fatal_err("%ps: PCI device ID mismatch, link possibly down, current read ID: 0x%x, record ID: 0x%x\n",
  898. (void *)_RET_IP_, device_id,
  899. pci_priv->device_id);
  900. return -EIO;
  901. }
  902. return 0;
  903. }
  904. static void cnss_pci_select_window(struct cnss_pci_data *pci_priv, u32 offset)
  905. {
  906. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  907. u32 window = (offset >> WINDOW_SHIFT) & WINDOW_VALUE_MASK;
  908. u32 window_enable = WINDOW_ENABLE_BIT | window;
  909. u32 val;
  910. if (plat_priv->device_id == PEACH_DEVICE_ID) {
  911. writel_relaxed(window_enable, pci_priv->bar +
  912. PEACH_PCIE_REMAP_BAR_CTRL_OFFSET);
  913. } else {
  914. writel_relaxed(window_enable, pci_priv->bar +
  915. QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  916. }
  917. if (plat_priv->device_id == QCN7605_DEVICE_ID)
  918. window_enable = QCN7605_WINDOW_ENABLE_BIT | window;
  919. if (window != pci_priv->remap_window) {
  920. pci_priv->remap_window = window;
  921. cnss_pr_dbg("Config PCIe remap window register to 0x%x\n",
  922. window_enable);
  923. }
  924. /* Read it back to make sure the write has taken effect */
  925. if (plat_priv->device_id == PEACH_DEVICE_ID) {
  926. val = readl_relaxed(pci_priv->bar +
  927. PEACH_PCIE_REMAP_BAR_CTRL_OFFSET);
  928. } else {
  929. val = readl_relaxed(pci_priv->bar +
  930. QCA6390_PCIE_REMAP_BAR_CTRL_OFFSET);
  931. }
  932. if (val != window_enable) {
  933. cnss_pr_err("Failed to config window register to 0x%x, current value: 0x%x\n",
  934. window_enable, val);
  935. if (!cnss_pci_check_link_status(pci_priv) &&
  936. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  937. CNSS_ASSERT(0);
  938. }
  939. }
  940. static int cnss_pci_reg_read(struct cnss_pci_data *pci_priv,
  941. u32 offset, u32 *val)
  942. {
  943. int ret;
  944. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  945. if (!in_interrupt() && !irqs_disabled()) {
  946. ret = cnss_pci_check_link_status(pci_priv);
  947. if (ret)
  948. return ret;
  949. }
  950. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  951. offset < MAX_UNWINDOWED_ADDRESS) {
  952. *val = readl_relaxed(pci_priv->bar + offset);
  953. return 0;
  954. }
  955. /* If in panic, assumption is kernel panic handler will hold all threads
  956. * and interrupts. Further pci_reg_window_lock could be held before
  957. * panic. So only lock during normal operation.
  958. */
  959. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  960. cnss_pci_select_window(pci_priv, offset);
  961. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  962. (offset & WINDOW_RANGE_MASK));
  963. } else {
  964. spin_lock_bh(&pci_reg_window_lock);
  965. cnss_pci_select_window(pci_priv, offset);
  966. *val = readl_relaxed(pci_priv->bar + WINDOW_START +
  967. (offset & WINDOW_RANGE_MASK));
  968. spin_unlock_bh(&pci_reg_window_lock);
  969. }
  970. return 0;
  971. }
  972. static int cnss_pci_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  973. u32 val)
  974. {
  975. int ret;
  976. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  977. if (!in_interrupt() && !irqs_disabled()) {
  978. ret = cnss_pci_check_link_status(pci_priv);
  979. if (ret)
  980. return ret;
  981. }
  982. if (pci_priv->pci_dev->device == QCA6174_DEVICE_ID ||
  983. offset < MAX_UNWINDOWED_ADDRESS) {
  984. writel_relaxed(val, pci_priv->bar + offset);
  985. return 0;
  986. }
  987. /* Same constraint as PCI register read in panic */
  988. if (test_bit(CNSS_IN_PANIC, &plat_priv->driver_state)) {
  989. cnss_pci_select_window(pci_priv, offset);
  990. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  991. (offset & WINDOW_RANGE_MASK));
  992. } else {
  993. spin_lock_bh(&pci_reg_window_lock);
  994. cnss_pci_select_window(pci_priv, offset);
  995. writel_relaxed(val, pci_priv->bar + WINDOW_START +
  996. (offset & WINDOW_RANGE_MASK));
  997. spin_unlock_bh(&pci_reg_window_lock);
  998. }
  999. return 0;
  1000. }
  1001. static int cnss_pci_force_wake_get(struct cnss_pci_data *pci_priv)
  1002. {
  1003. struct device *dev = &pci_priv->pci_dev->dev;
  1004. int ret;
  1005. ret = cnss_pci_force_wake_request_sync(dev,
  1006. FORCE_WAKE_DELAY_TIMEOUT_US);
  1007. if (ret) {
  1008. if (ret != -EAGAIN)
  1009. cnss_pr_err("Failed to request force wake\n");
  1010. return ret;
  1011. }
  1012. /* If device's M1 state-change event races here, it can be ignored,
  1013. * as the device is expected to immediately move from M2 to M0
  1014. * without entering low power state.
  1015. */
  1016. if (cnss_pci_is_device_awake(dev) != true)
  1017. cnss_pr_warn("MHI not in M0, while reg still accessible\n");
  1018. return 0;
  1019. }
  1020. static int cnss_pci_force_wake_put(struct cnss_pci_data *pci_priv)
  1021. {
  1022. struct device *dev = &pci_priv->pci_dev->dev;
  1023. int ret;
  1024. ret = cnss_pci_force_wake_release(dev);
  1025. if (ret && ret != -EAGAIN)
  1026. cnss_pr_err("Failed to release force wake\n");
  1027. return ret;
  1028. }
  1029. #if IS_ENABLED(CONFIG_INTERCONNECT)
  1030. /**
  1031. * cnss_setup_bus_bandwidth() - Setup interconnect vote for given bandwidth
  1032. * @plat_priv: Platform private data struct
  1033. * @bw: bandwidth
  1034. * @save: toggle flag to save bandwidth to current_bw_vote
  1035. *
  1036. * Setup bandwidth votes for configured interconnect paths
  1037. *
  1038. * Return: 0 for success
  1039. */
  1040. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  1041. u32 bw, bool save)
  1042. {
  1043. int ret = 0;
  1044. struct cnss_bus_bw_info *bus_bw_info;
  1045. if (!plat_priv->icc.path_count)
  1046. return -EOPNOTSUPP;
  1047. if (bw >= plat_priv->icc.bus_bw_cfg_count) {
  1048. cnss_pr_err("Invalid bus bandwidth Type: %d", bw);
  1049. return -EINVAL;
  1050. }
  1051. cnss_pr_buf("Bandwidth vote to %d, save %d\n", bw, save);
  1052. list_for_each_entry(bus_bw_info, &plat_priv->icc.list_head, list) {
  1053. ret = icc_set_bw(bus_bw_info->icc_path,
  1054. bus_bw_info->cfg_table[bw].avg_bw,
  1055. bus_bw_info->cfg_table[bw].peak_bw);
  1056. if (ret) {
  1057. cnss_pr_err("Could not set BW Cfg: %d, err = %d ICC Path: %s Val: %d %d\n",
  1058. bw, ret, bus_bw_info->icc_name,
  1059. bus_bw_info->cfg_table[bw].avg_bw,
  1060. bus_bw_info->cfg_table[bw].peak_bw);
  1061. break;
  1062. }
  1063. }
  1064. if (ret == 0 && save)
  1065. plat_priv->icc.current_bw_vote = bw;
  1066. return ret;
  1067. }
  1068. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  1069. {
  1070. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  1071. if (!plat_priv)
  1072. return -ENODEV;
  1073. if (bandwidth < 0)
  1074. return -EINVAL;
  1075. return cnss_setup_bus_bandwidth(plat_priv, (u32)bandwidth, true);
  1076. }
  1077. #else
  1078. static int cnss_setup_bus_bandwidth(struct cnss_plat_data *plat_priv,
  1079. u32 bw, bool save)
  1080. {
  1081. return 0;
  1082. }
  1083. int cnss_request_bus_bandwidth(struct device *dev, int bandwidth)
  1084. {
  1085. return 0;
  1086. }
  1087. #endif
  1088. EXPORT_SYMBOL(cnss_request_bus_bandwidth);
  1089. int cnss_pci_debug_reg_read(struct cnss_pci_data *pci_priv, u32 offset,
  1090. u32 *val, bool raw_access)
  1091. {
  1092. int ret = 0;
  1093. bool do_force_wake_put = true;
  1094. if (raw_access) {
  1095. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1096. goto out;
  1097. }
  1098. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1099. if (ret)
  1100. goto out;
  1101. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1102. if (ret < 0)
  1103. goto runtime_pm_put;
  1104. ret = cnss_pci_force_wake_get(pci_priv);
  1105. if (ret)
  1106. do_force_wake_put = false;
  1107. ret = cnss_pci_reg_read(pci_priv, offset, val);
  1108. if (ret) {
  1109. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  1110. offset, ret);
  1111. goto force_wake_put;
  1112. }
  1113. force_wake_put:
  1114. if (do_force_wake_put)
  1115. cnss_pci_force_wake_put(pci_priv);
  1116. runtime_pm_put:
  1117. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1118. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1119. out:
  1120. return ret;
  1121. }
  1122. int cnss_pci_debug_reg_write(struct cnss_pci_data *pci_priv, u32 offset,
  1123. u32 val, bool raw_access)
  1124. {
  1125. int ret = 0;
  1126. bool do_force_wake_put = true;
  1127. if (raw_access) {
  1128. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1129. goto out;
  1130. }
  1131. ret = cnss_pci_is_device_down(&pci_priv->pci_dev->dev);
  1132. if (ret)
  1133. goto out;
  1134. ret = cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS);
  1135. if (ret < 0)
  1136. goto runtime_pm_put;
  1137. ret = cnss_pci_force_wake_get(pci_priv);
  1138. if (ret)
  1139. do_force_wake_put = false;
  1140. ret = cnss_pci_reg_write(pci_priv, offset, val);
  1141. if (ret) {
  1142. cnss_pr_err("Failed to write 0x%x to register offset 0x%x, err = %d\n",
  1143. val, offset, ret);
  1144. goto force_wake_put;
  1145. }
  1146. force_wake_put:
  1147. if (do_force_wake_put)
  1148. cnss_pci_force_wake_put(pci_priv);
  1149. runtime_pm_put:
  1150. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  1151. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  1152. out:
  1153. return ret;
  1154. }
  1155. static int cnss_set_pci_config_space(struct cnss_pci_data *pci_priv, bool save)
  1156. {
  1157. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1158. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1159. bool link_down_or_recovery;
  1160. if (!plat_priv)
  1161. return -ENODEV;
  1162. link_down_or_recovery = pci_priv->pci_link_down_ind ||
  1163. (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state));
  1164. if (save) {
  1165. if (link_down_or_recovery) {
  1166. pci_priv->saved_state = NULL;
  1167. } else {
  1168. pci_save_state(pci_dev);
  1169. pci_priv->saved_state = pci_store_saved_state(pci_dev);
  1170. }
  1171. } else {
  1172. if (link_down_or_recovery) {
  1173. pci_load_saved_state(pci_dev, pci_priv->default_state);
  1174. pci_restore_state(pci_dev);
  1175. } else if (pci_priv->saved_state) {
  1176. pci_load_and_free_saved_state(pci_dev,
  1177. &pci_priv->saved_state);
  1178. pci_restore_state(pci_dev);
  1179. }
  1180. }
  1181. return 0;
  1182. }
  1183. static int cnss_pci_get_link_status(struct cnss_pci_data *pci_priv)
  1184. {
  1185. u16 link_status;
  1186. int ret;
  1187. ret = pcie_capability_read_word(pci_priv->pci_dev, PCI_EXP_LNKSTA,
  1188. &link_status);
  1189. if (ret)
  1190. return ret;
  1191. cnss_pr_dbg("Get PCI link status register: %u\n", link_status);
  1192. pci_priv->def_link_speed = link_status & PCI_EXP_LNKSTA_CLS;
  1193. pci_priv->def_link_width =
  1194. (link_status & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
  1195. pci_priv->cur_link_speed = pci_priv->def_link_speed;
  1196. cnss_pr_dbg("Default PCI link speed is 0x%x, link width is 0x%x\n",
  1197. pci_priv->def_link_speed, pci_priv->def_link_width);
  1198. return 0;
  1199. }
  1200. static void cnss_pci_soc_scratch_reg_dump(struct cnss_pci_data *pci_priv)
  1201. {
  1202. u32 reg_offset, val;
  1203. int i;
  1204. switch (pci_priv->device_id) {
  1205. case QCA6390_DEVICE_ID:
  1206. case QCA6490_DEVICE_ID:
  1207. case KIWI_DEVICE_ID:
  1208. case MANGO_DEVICE_ID:
  1209. case PEACH_DEVICE_ID:
  1210. break;
  1211. default:
  1212. return;
  1213. }
  1214. if (in_interrupt() || irqs_disabled())
  1215. return;
  1216. if (cnss_pci_check_link_status(pci_priv))
  1217. return;
  1218. cnss_pr_dbg("Start to dump SOC Scratch registers\n");
  1219. for (i = 0; pci_scratch[i].name; i++) {
  1220. reg_offset = pci_scratch[i].offset;
  1221. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  1222. return;
  1223. cnss_pr_dbg("PCIE_SOC_REG_%s = 0x%x\n",
  1224. pci_scratch[i].name, val);
  1225. }
  1226. }
  1227. int cnss_suspend_pci_link(struct cnss_pci_data *pci_priv)
  1228. {
  1229. int ret = 0;
  1230. if (!pci_priv)
  1231. return -ENODEV;
  1232. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1233. cnss_pr_info("PCI link is already suspended\n");
  1234. goto out;
  1235. }
  1236. pci_clear_master(pci_priv->pci_dev);
  1237. ret = cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  1238. if (ret)
  1239. goto out;
  1240. pci_disable_device(pci_priv->pci_dev);
  1241. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1242. if (pci_set_power_state(pci_priv->pci_dev, PCI_D3hot))
  1243. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  1244. }
  1245. /* Always do PCIe L2 suspend during power off/PCIe link recovery */
  1246. pci_priv->drv_connected_last = 0;
  1247. ret = cnss_set_pci_link(pci_priv, PCI_LINK_DOWN);
  1248. if (ret)
  1249. goto out;
  1250. pci_priv->pci_link_state = PCI_LINK_DOWN;
  1251. return 0;
  1252. out:
  1253. return ret;
  1254. }
  1255. int cnss_resume_pci_link(struct cnss_pci_data *pci_priv)
  1256. {
  1257. int ret = 0;
  1258. if (!pci_priv)
  1259. return -ENODEV;
  1260. if (pci_priv->pci_link_state == PCI_LINK_UP) {
  1261. cnss_pr_info("PCI link is already resumed\n");
  1262. goto out;
  1263. }
  1264. ret = cnss_set_pci_link(pci_priv, PCI_LINK_UP);
  1265. if (ret) {
  1266. ret = -EAGAIN;
  1267. goto out;
  1268. }
  1269. pci_priv->pci_link_state = PCI_LINK_UP;
  1270. if (pci_priv->pci_dev->device != QCA6174_DEVICE_ID) {
  1271. ret = pci_set_power_state(pci_priv->pci_dev, PCI_D0);
  1272. if (ret) {
  1273. cnss_pr_err("Failed to set D0, err = %d\n", ret);
  1274. goto out;
  1275. }
  1276. }
  1277. ret = cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  1278. if (ret)
  1279. goto out;
  1280. ret = pci_enable_device(pci_priv->pci_dev);
  1281. if (ret) {
  1282. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  1283. goto out;
  1284. }
  1285. pci_set_master(pci_priv->pci_dev);
  1286. if (pci_priv->pci_link_down_ind)
  1287. pci_priv->pci_link_down_ind = false;
  1288. return 0;
  1289. out:
  1290. return ret;
  1291. }
  1292. int cnss_pci_recover_link_down(struct cnss_pci_data *pci_priv)
  1293. {
  1294. int ret;
  1295. switch (pci_priv->device_id) {
  1296. case QCA6390_DEVICE_ID:
  1297. case QCA6490_DEVICE_ID:
  1298. case KIWI_DEVICE_ID:
  1299. case MANGO_DEVICE_ID:
  1300. case PEACH_DEVICE_ID:
  1301. break;
  1302. default:
  1303. return -EOPNOTSUPP;
  1304. }
  1305. /* Always wait here to avoid missing WAKE assert for RDDM
  1306. * before link recovery
  1307. */
  1308. msleep(WAKE_EVENT_TIMEOUT);
  1309. ret = cnss_suspend_pci_link(pci_priv);
  1310. if (ret)
  1311. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  1312. ret = cnss_resume_pci_link(pci_priv);
  1313. if (ret) {
  1314. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  1315. del_timer(&pci_priv->dev_rddm_timer);
  1316. return ret;
  1317. }
  1318. mod_timer(&pci_priv->dev_rddm_timer,
  1319. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1320. cnss_mhi_debug_reg_dump(pci_priv);
  1321. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1322. return 0;
  1323. }
  1324. static void cnss_pci_update_link_event(struct cnss_pci_data *pci_priv,
  1325. enum cnss_bus_event_type type,
  1326. void *data)
  1327. {
  1328. struct cnss_bus_event bus_event;
  1329. bus_event.etype = type;
  1330. bus_event.event_data = data;
  1331. cnss_pci_call_driver_uevent(pci_priv, CNSS_BUS_EVENT, &bus_event);
  1332. }
  1333. void cnss_pci_handle_linkdown(struct cnss_pci_data *pci_priv)
  1334. {
  1335. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1336. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1337. unsigned long flags;
  1338. if (test_bit(ENABLE_PCI_LINK_DOWN_PANIC,
  1339. &plat_priv->ctrl_params.quirks))
  1340. panic("cnss: PCI link is down\n");
  1341. spin_lock_irqsave(&pci_link_down_lock, flags);
  1342. if (pci_priv->pci_link_down_ind) {
  1343. cnss_pr_dbg("PCI link down recovery is in progress, ignore\n");
  1344. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1345. return;
  1346. }
  1347. pci_priv->pci_link_down_ind = true;
  1348. spin_unlock_irqrestore(&pci_link_down_lock, flags);
  1349. if (pci_priv->mhi_ctrl) {
  1350. /* Notify MHI about link down*/
  1351. mhi_report_error(pci_priv->mhi_ctrl);
  1352. }
  1353. if (pci_dev->device == QCA6174_DEVICE_ID)
  1354. disable_irq(pci_dev->irq);
  1355. /* Notify bus related event. Now for all supported chips.
  1356. * Here PCIe LINK_DOWN notification taken care.
  1357. * uevent buffer can be extended later, to cover more bus info.
  1358. */
  1359. cnss_pci_update_link_event(pci_priv, BUS_EVENT_PCI_LINK_DOWN, NULL);
  1360. cnss_fatal_err("PCI link down, schedule recovery\n");
  1361. cnss_schedule_recovery(&pci_dev->dev, CNSS_REASON_LINK_DOWN);
  1362. }
  1363. int cnss_pci_link_down(struct device *dev)
  1364. {
  1365. struct pci_dev *pci_dev = to_pci_dev(dev);
  1366. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1367. struct cnss_plat_data *plat_priv = NULL;
  1368. int ret;
  1369. if (!pci_priv) {
  1370. cnss_pr_err("pci_priv is NULL\n");
  1371. return -EINVAL;
  1372. }
  1373. plat_priv = pci_priv->plat_priv;
  1374. if (!plat_priv) {
  1375. cnss_pr_err("plat_priv is NULL\n");
  1376. return -ENODEV;
  1377. }
  1378. if (pci_priv->pci_link_down_ind) {
  1379. cnss_pr_dbg("PCI link down recovery is already in progress\n");
  1380. return -EBUSY;
  1381. }
  1382. if (pci_priv->drv_connected_last &&
  1383. of_property_read_bool(plat_priv->plat_dev->dev.of_node,
  1384. "cnss-enable-self-recovery"))
  1385. plat_priv->ctrl_params.quirks |= BIT(LINK_DOWN_SELF_RECOVERY);
  1386. cnss_pr_err("PCI link down is detected by drivers\n");
  1387. ret = cnss_pci_assert_perst(pci_priv);
  1388. if (ret)
  1389. cnss_pci_handle_linkdown(pci_priv);
  1390. return ret;
  1391. }
  1392. EXPORT_SYMBOL(cnss_pci_link_down);
  1393. int cnss_pci_get_reg_dump(struct device *dev, uint8_t *buffer, uint32_t len)
  1394. {
  1395. struct pci_dev *pci_dev = to_pci_dev(dev);
  1396. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1397. if (!pci_priv) {
  1398. cnss_pr_err("pci_priv is NULL\n");
  1399. return -ENODEV;
  1400. }
  1401. if (pci_priv->pci_link_state == PCI_LINK_DOWN) {
  1402. cnss_pr_dbg("No PCIe reg dump since PCIe is suspended(D3)\n");
  1403. return -EACCES;
  1404. }
  1405. cnss_pr_dbg("Start to get PCIe reg dump\n");
  1406. return _cnss_pci_get_reg_dump(pci_priv, buffer, len);
  1407. }
  1408. EXPORT_SYMBOL(cnss_pci_get_reg_dump);
  1409. int cnss_pcie_is_device_down(struct cnss_pci_data *pci_priv)
  1410. {
  1411. struct cnss_plat_data *plat_priv;
  1412. if (!pci_priv) {
  1413. cnss_pr_err("pci_priv is NULL\n");
  1414. return -ENODEV;
  1415. }
  1416. plat_priv = pci_priv->plat_priv;
  1417. if (!plat_priv) {
  1418. cnss_pr_err("plat_priv is NULL\n");
  1419. return -ENODEV;
  1420. }
  1421. return test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) |
  1422. pci_priv->pci_link_down_ind;
  1423. }
  1424. int cnss_pci_is_device_down(struct device *dev)
  1425. {
  1426. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  1427. return cnss_pcie_is_device_down(pci_priv);
  1428. }
  1429. EXPORT_SYMBOL(cnss_pci_is_device_down);
  1430. void cnss_pci_lock_reg_window(struct device *dev, unsigned long *flags)
  1431. {
  1432. spin_lock_bh(&pci_reg_window_lock);
  1433. }
  1434. EXPORT_SYMBOL(cnss_pci_lock_reg_window);
  1435. void cnss_pci_unlock_reg_window(struct device *dev, unsigned long *flags)
  1436. {
  1437. spin_unlock_bh(&pci_reg_window_lock);
  1438. }
  1439. EXPORT_SYMBOL(cnss_pci_unlock_reg_window);
  1440. int cnss_get_pci_slot(struct device *dev)
  1441. {
  1442. struct pci_dev *pci_dev = to_pci_dev(dev);
  1443. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  1444. struct cnss_plat_data *plat_priv = NULL;
  1445. if (!pci_priv) {
  1446. cnss_pr_err("pci_priv is NULL\n");
  1447. return -EINVAL;
  1448. }
  1449. plat_priv = pci_priv->plat_priv;
  1450. if (!plat_priv) {
  1451. cnss_pr_err("plat_priv is NULL\n");
  1452. return -ENODEV;
  1453. }
  1454. return plat_priv->rc_num;
  1455. }
  1456. EXPORT_SYMBOL(cnss_get_pci_slot);
  1457. /**
  1458. * cnss_pci_dump_bl_sram_mem - Dump WLAN device bootloader debug log
  1459. * @pci_priv: driver PCI bus context pointer
  1460. *
  1461. * Dump primary and secondary bootloader debug log data. For SBL check the
  1462. * log struct address and size for validity.
  1463. *
  1464. * Return: None
  1465. */
  1466. static void cnss_pci_dump_bl_sram_mem(struct cnss_pci_data *pci_priv)
  1467. {
  1468. enum mhi_ee_type ee;
  1469. u32 mem_addr, val, pbl_log_max_size, sbl_log_max_size;
  1470. u32 pbl_log_sram_start;
  1471. u32 pbl_stage, sbl_log_start, sbl_log_size;
  1472. u32 pbl_wlan_boot_cfg, pbl_bootstrap_status;
  1473. u32 pbl_bootstrap_status_reg = PBL_BOOTSTRAP_STATUS;
  1474. u32 sbl_log_def_start = SRAM_START;
  1475. u32 sbl_log_def_end = SRAM_END;
  1476. int i;
  1477. switch (pci_priv->device_id) {
  1478. case QCA6390_DEVICE_ID:
  1479. pbl_log_sram_start = QCA6390_DEBUG_PBL_LOG_SRAM_START;
  1480. pbl_log_max_size = QCA6390_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1481. sbl_log_max_size = QCA6390_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1482. break;
  1483. case QCA6490_DEVICE_ID:
  1484. pbl_log_sram_start = QCA6490_DEBUG_PBL_LOG_SRAM_START;
  1485. pbl_log_max_size = QCA6490_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1486. sbl_log_max_size = QCA6490_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1487. break;
  1488. case KIWI_DEVICE_ID:
  1489. pbl_bootstrap_status_reg = KIWI_PBL_BOOTSTRAP_STATUS;
  1490. pbl_log_sram_start = KIWI_DEBUG_PBL_LOG_SRAM_START;
  1491. pbl_log_max_size = KIWI_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1492. sbl_log_max_size = KIWI_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1493. break;
  1494. case MANGO_DEVICE_ID:
  1495. pbl_bootstrap_status_reg = MANGO_PBL_BOOTSTRAP_STATUS;
  1496. pbl_log_sram_start = MANGO_DEBUG_PBL_LOG_SRAM_START;
  1497. pbl_log_max_size = MANGO_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1498. sbl_log_max_size = MANGO_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1499. break;
  1500. case PEACH_DEVICE_ID:
  1501. pbl_bootstrap_status_reg = PEACH_PBL_BOOTSTRAP_STATUS;
  1502. pbl_log_sram_start = PEACH_DEBUG_PBL_LOG_SRAM_START;
  1503. pbl_log_max_size = PEACH_DEBUG_PBL_LOG_SRAM_MAX_SIZE;
  1504. sbl_log_max_size = PEACH_DEBUG_SBL_LOG_SRAM_MAX_SIZE;
  1505. break;
  1506. default:
  1507. return;
  1508. }
  1509. if (cnss_pci_check_link_status(pci_priv))
  1510. return;
  1511. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1512. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1513. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1514. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1515. cnss_pci_reg_read(pci_priv, pbl_bootstrap_status_reg,
  1516. &pbl_bootstrap_status);
  1517. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x\n",
  1518. pbl_stage, sbl_log_start, sbl_log_size);
  1519. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x PBL_BOOTSTRAP_STATUS: 0x%08x\n",
  1520. pbl_wlan_boot_cfg, pbl_bootstrap_status);
  1521. ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  1522. if (CNSS_MHI_IN_MISSION_MODE(ee)) {
  1523. cnss_pr_dbg("Avoid Dumping PBL log data in Mission mode\n");
  1524. return;
  1525. }
  1526. cnss_pr_dbg("Dumping PBL log data\n");
  1527. for (i = 0; i < pbl_log_max_size; i += sizeof(val)) {
  1528. mem_addr = pbl_log_sram_start + i;
  1529. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1530. break;
  1531. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1532. }
  1533. sbl_log_size = (sbl_log_size > sbl_log_max_size ?
  1534. sbl_log_max_size : sbl_log_size);
  1535. if (sbl_log_start < sbl_log_def_start ||
  1536. sbl_log_start > sbl_log_def_end ||
  1537. (sbl_log_start + sbl_log_size) > sbl_log_def_end) {
  1538. cnss_pr_err("Invalid SBL log data\n");
  1539. return;
  1540. }
  1541. ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  1542. if (CNSS_MHI_IN_MISSION_MODE(ee)) {
  1543. cnss_pr_dbg("Avoid Dumping SBL log data in Mission mode\n");
  1544. return;
  1545. }
  1546. cnss_pr_dbg("Dumping SBL log data\n");
  1547. for (i = 0; i < sbl_log_size; i += sizeof(val)) {
  1548. mem_addr = sbl_log_start + i;
  1549. if (cnss_pci_reg_read(pci_priv, mem_addr, &val))
  1550. break;
  1551. cnss_pr_dbg("SRAM[0x%x] = 0x%x\n", mem_addr, val);
  1552. }
  1553. }
  1554. #ifdef CONFIG_DISABLE_CNSS_SRAM_DUMP
  1555. static void cnss_pci_dump_sram(struct cnss_pci_data *pci_priv)
  1556. {
  1557. }
  1558. #else
  1559. static void cnss_pci_dump_sram(struct cnss_pci_data *pci_priv)
  1560. {
  1561. struct cnss_plat_data *plat_priv;
  1562. u32 i, mem_addr;
  1563. u32 *dump_ptr;
  1564. plat_priv = pci_priv->plat_priv;
  1565. if (plat_priv->device_id != QCA6490_DEVICE_ID ||
  1566. cnss_get_host_build_type() != QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  1567. return;
  1568. if (!plat_priv->sram_dump) {
  1569. cnss_pr_err("SRAM dump memory is not allocated\n");
  1570. return;
  1571. }
  1572. if (cnss_pci_check_link_status(pci_priv))
  1573. return;
  1574. cnss_pr_dbg("Dumping SRAM at 0x%lx\n", plat_priv->sram_dump);
  1575. for (i = 0; i < SRAM_DUMP_SIZE; i += sizeof(u32)) {
  1576. mem_addr = SRAM_START + i;
  1577. dump_ptr = (u32 *)(plat_priv->sram_dump + i);
  1578. if (cnss_pci_reg_read(pci_priv, mem_addr, dump_ptr)) {
  1579. cnss_pr_err("SRAM Dump failed at 0x%x\n", mem_addr);
  1580. break;
  1581. }
  1582. /* Relinquish CPU after dumping 256KB chunks*/
  1583. if (!(i % CNSS_256KB_SIZE))
  1584. cond_resched();
  1585. }
  1586. }
  1587. #endif
  1588. static int cnss_pci_handle_mhi_poweron_timeout(struct cnss_pci_data *pci_priv)
  1589. {
  1590. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  1591. cnss_fatal_err("MHI power up returns timeout\n");
  1592. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE) ||
  1593. cnss_get_dev_sol_value(plat_priv) > 0) {
  1594. /* Wait for RDDM if RDDM cookie is set or device SOL GPIO is
  1595. * high. If RDDM times out, PBL/SBL error region may have been
  1596. * erased so no need to dump them either.
  1597. */
  1598. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  1599. !pci_priv->pci_link_down_ind) {
  1600. mod_timer(&pci_priv->dev_rddm_timer,
  1601. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  1602. }
  1603. } else {
  1604. cnss_pr_dbg("RDDM cookie is not set and device SOL is low\n");
  1605. cnss_mhi_debug_reg_dump(pci_priv);
  1606. cnss_pci_soc_scratch_reg_dump(pci_priv);
  1607. /* Dump PBL/SBL error log if RDDM cookie is not set */
  1608. cnss_pci_dump_bl_sram_mem(pci_priv);
  1609. cnss_pci_dump_sram(pci_priv);
  1610. return -ETIMEDOUT;
  1611. }
  1612. return 0;
  1613. }
  1614. static char *cnss_mhi_state_to_str(enum cnss_mhi_state mhi_state)
  1615. {
  1616. switch (mhi_state) {
  1617. case CNSS_MHI_INIT:
  1618. return "INIT";
  1619. case CNSS_MHI_DEINIT:
  1620. return "DEINIT";
  1621. case CNSS_MHI_POWER_ON:
  1622. return "POWER_ON";
  1623. case CNSS_MHI_POWERING_OFF:
  1624. return "POWERING_OFF";
  1625. case CNSS_MHI_POWER_OFF:
  1626. return "POWER_OFF";
  1627. case CNSS_MHI_FORCE_POWER_OFF:
  1628. return "FORCE_POWER_OFF";
  1629. case CNSS_MHI_SUSPEND:
  1630. return "SUSPEND";
  1631. case CNSS_MHI_RESUME:
  1632. return "RESUME";
  1633. case CNSS_MHI_TRIGGER_RDDM:
  1634. return "TRIGGER_RDDM";
  1635. case CNSS_MHI_RDDM_DONE:
  1636. return "RDDM_DONE";
  1637. default:
  1638. return "UNKNOWN";
  1639. }
  1640. };
  1641. static int cnss_pci_check_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1642. enum cnss_mhi_state mhi_state)
  1643. {
  1644. switch (mhi_state) {
  1645. case CNSS_MHI_INIT:
  1646. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state))
  1647. return 0;
  1648. break;
  1649. case CNSS_MHI_DEINIT:
  1650. case CNSS_MHI_POWER_ON:
  1651. if (test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state) &&
  1652. !test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1653. return 0;
  1654. break;
  1655. case CNSS_MHI_FORCE_POWER_OFF:
  1656. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  1657. return 0;
  1658. break;
  1659. case CNSS_MHI_POWER_OFF:
  1660. case CNSS_MHI_SUSPEND:
  1661. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1662. !test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1663. return 0;
  1664. break;
  1665. case CNSS_MHI_RESUME:
  1666. if (test_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state))
  1667. return 0;
  1668. break;
  1669. case CNSS_MHI_TRIGGER_RDDM:
  1670. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) &&
  1671. !test_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state))
  1672. return 0;
  1673. break;
  1674. case CNSS_MHI_RDDM_DONE:
  1675. return 0;
  1676. default:
  1677. cnss_pr_err("Unhandled MHI state: %s(%d)\n",
  1678. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1679. }
  1680. cnss_pr_err("Cannot set MHI state %s(%d) in current MHI state (0x%lx)\n",
  1681. cnss_mhi_state_to_str(mhi_state), mhi_state,
  1682. pci_priv->mhi_state);
  1683. if (mhi_state != CNSS_MHI_TRIGGER_RDDM)
  1684. CNSS_ASSERT(0);
  1685. return -EINVAL;
  1686. }
  1687. static int cnss_rddm_trigger_debug(struct cnss_pci_data *pci_priv)
  1688. {
  1689. int read_val, ret;
  1690. if (!pci_priv || pci_priv->device_id != QCA6490_DEVICE_ID)
  1691. return -EOPNOTSUPP;
  1692. if (cnss_pci_check_link_status(pci_priv))
  1693. return -EINVAL;
  1694. cnss_pr_err("Write GCC Spare with ACE55 Pattern");
  1695. cnss_pci_reg_write(pci_priv, GCC_GCC_SPARE_REG_1, 0xACE55);
  1696. ret = cnss_pci_reg_read(pci_priv, GCC_GCC_SPARE_REG_1, &read_val);
  1697. cnss_pr_err("Read back GCC Spare: 0x%x, ret: %d", read_val, ret);
  1698. ret = cnss_pci_reg_read(pci_priv, GCC_PRE_ARES_DEBUG_TIMER_VAL,
  1699. &read_val);
  1700. cnss_pr_err("Warm reset allowed check: 0x%x, ret: %d", read_val, ret);
  1701. return ret;
  1702. }
  1703. static int cnss_rddm_trigger_check(struct cnss_pci_data *pci_priv)
  1704. {
  1705. int read_val, ret;
  1706. u32 pbl_stage, sbl_log_start, sbl_log_size, pbl_wlan_boot_cfg;
  1707. if (!pci_priv || pci_priv->device_id != QCA6490_DEVICE_ID)
  1708. return -EOPNOTSUPP;
  1709. if (cnss_pci_check_link_status(pci_priv))
  1710. return -EINVAL;
  1711. ret = cnss_pci_reg_read(pci_priv, GCC_GCC_SPARE_REG_1, &read_val);
  1712. cnss_pr_err("Read GCC spare to check reset status: 0x%x, ret: %d",
  1713. read_val, ret);
  1714. cnss_pci_reg_read(pci_priv, TCSR_PBL_LOGGING_REG, &pbl_stage);
  1715. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG2_REG, &sbl_log_start);
  1716. cnss_pci_reg_read(pci_priv, PCIE_BHI_ERRDBG3_REG, &sbl_log_size);
  1717. cnss_pci_reg_read(pci_priv, PBL_WLAN_BOOT_CFG, &pbl_wlan_boot_cfg);
  1718. cnss_pr_dbg("TCSR_PBL_LOGGING: 0x%08x PCIE_BHI_ERRDBG: Start: 0x%08x Size:0x%08x \n",
  1719. pbl_stage, sbl_log_start, sbl_log_size);
  1720. cnss_pr_dbg("PBL_WLAN_BOOT_CFG: 0x%08x\n", pbl_wlan_boot_cfg);
  1721. return ret;
  1722. }
  1723. static void cnss_pci_set_mhi_state_bit(struct cnss_pci_data *pci_priv,
  1724. enum cnss_mhi_state mhi_state)
  1725. {
  1726. switch (mhi_state) {
  1727. case CNSS_MHI_INIT:
  1728. set_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1729. break;
  1730. case CNSS_MHI_DEINIT:
  1731. clear_bit(CNSS_MHI_INIT, &pci_priv->mhi_state);
  1732. break;
  1733. case CNSS_MHI_POWER_ON:
  1734. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1735. break;
  1736. case CNSS_MHI_POWERING_OFF:
  1737. set_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1738. break;
  1739. case CNSS_MHI_POWER_OFF:
  1740. case CNSS_MHI_FORCE_POWER_OFF:
  1741. clear_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  1742. clear_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state);
  1743. clear_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1744. clear_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1745. break;
  1746. case CNSS_MHI_SUSPEND:
  1747. set_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1748. break;
  1749. case CNSS_MHI_RESUME:
  1750. clear_bit(CNSS_MHI_SUSPEND, &pci_priv->mhi_state);
  1751. break;
  1752. case CNSS_MHI_TRIGGER_RDDM:
  1753. set_bit(CNSS_MHI_TRIGGER_RDDM, &pci_priv->mhi_state);
  1754. break;
  1755. case CNSS_MHI_RDDM_DONE:
  1756. set_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state);
  1757. break;
  1758. default:
  1759. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1760. }
  1761. }
  1762. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0))
  1763. static int cnss_mhi_pm_force_resume(struct cnss_pci_data *pci_priv)
  1764. {
  1765. return mhi_pm_resume_force(pci_priv->mhi_ctrl);
  1766. }
  1767. #else
  1768. static int cnss_mhi_pm_force_resume(struct cnss_pci_data *pci_priv)
  1769. {
  1770. return mhi_pm_resume(pci_priv->mhi_ctrl);
  1771. }
  1772. #endif
  1773. static int cnss_pci_set_mhi_state(struct cnss_pci_data *pci_priv,
  1774. enum cnss_mhi_state mhi_state)
  1775. {
  1776. int ret = 0, retry = 0;
  1777. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  1778. return 0;
  1779. if (mhi_state < 0) {
  1780. cnss_pr_err("Invalid MHI state (%d)\n", mhi_state);
  1781. return -EINVAL;
  1782. }
  1783. ret = cnss_pci_check_mhi_state_bit(pci_priv, mhi_state);
  1784. if (ret)
  1785. goto out;
  1786. cnss_pr_vdbg("Setting MHI state: %s(%d)\n",
  1787. cnss_mhi_state_to_str(mhi_state), mhi_state);
  1788. switch (mhi_state) {
  1789. case CNSS_MHI_INIT:
  1790. ret = mhi_prepare_for_power_up(pci_priv->mhi_ctrl);
  1791. break;
  1792. case CNSS_MHI_DEINIT:
  1793. mhi_unprepare_after_power_down(pci_priv->mhi_ctrl);
  1794. ret = 0;
  1795. break;
  1796. case CNSS_MHI_POWER_ON:
  1797. ret = mhi_sync_power_up(pci_priv->mhi_ctrl);
  1798. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  1799. /* Only set img_pre_alloc when power up succeeds */
  1800. if (!ret && !pci_priv->mhi_ctrl->img_pre_alloc) {
  1801. cnss_pr_dbg("Notify MHI to use already allocated images\n");
  1802. pci_priv->mhi_ctrl->img_pre_alloc = true;
  1803. }
  1804. #endif
  1805. break;
  1806. case CNSS_MHI_POWER_OFF:
  1807. mhi_power_down(pci_priv->mhi_ctrl, true);
  1808. ret = 0;
  1809. break;
  1810. case CNSS_MHI_FORCE_POWER_OFF:
  1811. mhi_power_down(pci_priv->mhi_ctrl, false);
  1812. ret = 0;
  1813. break;
  1814. case CNSS_MHI_SUSPEND:
  1815. retry_mhi_suspend:
  1816. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1817. if (pci_priv->drv_connected_last)
  1818. ret = cnss_mhi_pm_fast_suspend(pci_priv, true);
  1819. else
  1820. ret = mhi_pm_suspend(pci_priv->mhi_ctrl);
  1821. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1822. if (ret == -EBUSY && retry++ < MHI_SUSPEND_RETRY_MAX_TIMES) {
  1823. cnss_pr_dbg("Retry MHI suspend #%d\n", retry);
  1824. usleep_range(MHI_SUSPEND_RETRY_DELAY_US,
  1825. MHI_SUSPEND_RETRY_DELAY_US + 1000);
  1826. goto retry_mhi_suspend;
  1827. }
  1828. break;
  1829. case CNSS_MHI_RESUME:
  1830. mutex_lock(&pci_priv->mhi_ctrl->pm_mutex);
  1831. if (pci_priv->drv_connected_last) {
  1832. ret = cnss_pci_prevent_l1(&pci_priv->pci_dev->dev);
  1833. if (ret) {
  1834. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1835. break;
  1836. }
  1837. ret = cnss_mhi_pm_fast_resume(pci_priv, true);
  1838. cnss_pci_allow_l1(&pci_priv->pci_dev->dev);
  1839. } else {
  1840. if (pci_priv->device_id == QCA6390_DEVICE_ID)
  1841. ret = cnss_mhi_pm_force_resume(pci_priv);
  1842. else
  1843. ret = mhi_pm_resume(pci_priv->mhi_ctrl);
  1844. }
  1845. mutex_unlock(&pci_priv->mhi_ctrl->pm_mutex);
  1846. break;
  1847. case CNSS_MHI_TRIGGER_RDDM:
  1848. cnss_rddm_trigger_debug(pci_priv);
  1849. ret = mhi_force_rddm_mode(pci_priv->mhi_ctrl);
  1850. if (ret) {
  1851. cnss_pr_err("Failed to trigger RDDM, err = %d\n", ret);
  1852. cnss_pr_dbg("Sending host reset req\n");
  1853. ret = cnss_mhi_force_reset(pci_priv);
  1854. cnss_rddm_trigger_check(pci_priv);
  1855. }
  1856. break;
  1857. case CNSS_MHI_RDDM_DONE:
  1858. break;
  1859. default:
  1860. cnss_pr_err("Unhandled MHI state (%d)\n", mhi_state);
  1861. ret = -EINVAL;
  1862. }
  1863. if (ret)
  1864. goto out;
  1865. cnss_pci_set_mhi_state_bit(pci_priv, mhi_state);
  1866. return 0;
  1867. out:
  1868. cnss_pr_err("Failed to set MHI state: %s(%d), err = %d\n",
  1869. cnss_mhi_state_to_str(mhi_state), mhi_state, ret);
  1870. return ret;
  1871. }
  1872. static int cnss_pci_config_msi_addr(struct cnss_pci_data *pci_priv)
  1873. {
  1874. int ret = 0;
  1875. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1876. struct cnss_plat_data *plat_priv;
  1877. if (!pci_dev)
  1878. return -ENODEV;
  1879. if (!pci_dev->msix_enabled)
  1880. return ret;
  1881. plat_priv = pci_priv->plat_priv;
  1882. if (!plat_priv) {
  1883. cnss_pr_err("plat_priv is NULL\n");
  1884. return -ENODEV;
  1885. }
  1886. ret = of_property_read_u32(plat_priv->plat_dev->dev.of_node,
  1887. "msix-match-addr",
  1888. &pci_priv->msix_addr);
  1889. cnss_pr_dbg("MSI-X Match address is 0x%X\n",
  1890. pci_priv->msix_addr);
  1891. return ret;
  1892. }
  1893. static int cnss_pci_config_msi_data(struct cnss_pci_data *pci_priv)
  1894. {
  1895. struct msi_desc *msi_desc;
  1896. struct cnss_msi_config *msi_config;
  1897. struct pci_dev *pci_dev = pci_priv->pci_dev;
  1898. msi_config = pci_priv->msi_config;
  1899. if (pci_dev->msix_enabled) {
  1900. pci_priv->msi_ep_base_data = msi_config->users[0].base_vector;
  1901. cnss_pr_dbg("MSI-X base data is %d\n",
  1902. pci_priv->msi_ep_base_data);
  1903. return 0;
  1904. }
  1905. msi_desc = irq_get_msi_desc(pci_dev->irq);
  1906. if (!msi_desc) {
  1907. cnss_pr_err("msi_desc is NULL!\n");
  1908. return -EINVAL;
  1909. }
  1910. pci_priv->msi_ep_base_data = msi_desc->msg.data;
  1911. cnss_pr_dbg("MSI base data is %d\n", pci_priv->msi_ep_base_data);
  1912. return 0;
  1913. }
  1914. #ifdef CONFIG_CNSS_SUPPORT_DUAL_DEV
  1915. #define PLC_PCIE_NAME_LEN 14
  1916. static struct cnss_plat_data *
  1917. cnss_get_plat_priv_by_driver_ops(struct cnss_wlan_driver *driver_ops)
  1918. {
  1919. int plat_env_count = cnss_get_plat_env_count();
  1920. struct cnss_plat_data *plat_env;
  1921. struct cnss_pci_data *pci_priv;
  1922. int i = 0;
  1923. if (!driver_ops) {
  1924. cnss_pr_err("No cnss driver\n");
  1925. return NULL;
  1926. }
  1927. for (i = 0; i < plat_env_count; i++) {
  1928. plat_env = cnss_get_plat_env(i);
  1929. if (!plat_env)
  1930. continue;
  1931. if (driver_ops->name && plat_env->pld_bus_ops_name) {
  1932. /* driver_ops->name = PLD_PCIE_OPS_NAME
  1933. * #ifdef MULTI_IF_NAME
  1934. * #define PLD_PCIE_OPS_NAME "pld_pcie_" MULTI_IF_NAME
  1935. * #else
  1936. * #define PLD_PCIE_OPS_NAME "pld_pcie"
  1937. * #endif
  1938. */
  1939. if (memcmp(driver_ops->name,
  1940. plat_env->pld_bus_ops_name,
  1941. PLC_PCIE_NAME_LEN) == 0)
  1942. return plat_env;
  1943. }
  1944. }
  1945. cnss_pr_err("Invalid cnss driver name from ko %s\n", driver_ops->name);
  1946. /* in the dual wlan card case, the pld_bus_ops_name from dts
  1947. * and driver_ops-> name from ko should match, otherwise
  1948. * wlanhost driver don't know which plat_env it can use;
  1949. * if doesn't find the match one, then get first available
  1950. * instance insteadly.
  1951. */
  1952. for (i = 0; i < plat_env_count; i++) {
  1953. plat_env = cnss_get_plat_env(i);
  1954. if (!plat_env)
  1955. continue;
  1956. pci_priv = plat_env->bus_priv;
  1957. if (!pci_priv) {
  1958. cnss_pr_err("pci_priv is NULL\n");
  1959. continue;
  1960. }
  1961. if (driver_ops == pci_priv->driver_ops)
  1962. return plat_env;
  1963. }
  1964. /* Doesn't find the existing instance,
  1965. * so return the fist empty instance
  1966. */
  1967. for (i = 0; i < plat_env_count; i++) {
  1968. plat_env = cnss_get_plat_env(i);
  1969. if (!plat_env)
  1970. continue;
  1971. pci_priv = plat_env->bus_priv;
  1972. if (!pci_priv) {
  1973. cnss_pr_err("pci_priv is NULL\n");
  1974. continue;
  1975. }
  1976. if (!pci_priv->driver_ops)
  1977. return plat_env;
  1978. }
  1979. return NULL;
  1980. }
  1981. static int cnss_pci_store_qrtr_node_id(struct cnss_pci_data *pci_priv)
  1982. {
  1983. int ret = 0;
  1984. u32 scratch = QCA6390_PCIE_SOC_PCIE_REG_PCIE_SCRATCH_2_SOC_PCIE_REG;
  1985. struct cnss_plat_data *plat_priv;
  1986. if (!pci_priv) {
  1987. cnss_pr_err("pci_priv is NULL\n");
  1988. return -ENODEV;
  1989. }
  1990. plat_priv = pci_priv->plat_priv;
  1991. /**
  1992. * in the single wlan chipset case, plat_priv->qrtr_node_id always is 0,
  1993. * wlan fw will use the hardcode 7 as the qrtr node id.
  1994. * in the dual Hastings case, we will read qrtr node id
  1995. * from device tree and pass to get plat_priv->qrtr_node_id,
  1996. * which always is not zero. And then store this new value
  1997. * to pcie register, wlan fw will read out this qrtr node id
  1998. * from this register and overwrite to the hardcode one
  1999. * while do initialization for ipc router.
  2000. * without this change, two Hastings will use the same
  2001. * qrtr node instance id, which will mess up qmi message
  2002. * exchange. According to qrtr spec, every node should
  2003. * have unique qrtr node id
  2004. */
  2005. if (plat_priv->device_id == QCA6390_DEVICE_ID &&
  2006. plat_priv->qrtr_node_id) {
  2007. u32 val;
  2008. cnss_pr_dbg("write 0x%x to SCRATCH REG\n",
  2009. plat_priv->qrtr_node_id);
  2010. ret = cnss_pci_reg_write(pci_priv, scratch,
  2011. plat_priv->qrtr_node_id);
  2012. if (ret) {
  2013. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  2014. scratch, ret);
  2015. goto out;
  2016. }
  2017. ret = cnss_pci_reg_read(pci_priv, scratch, &val);
  2018. if (ret) {
  2019. cnss_pr_err("Failed to read SCRATCH REG");
  2020. goto out;
  2021. }
  2022. if (val != plat_priv->qrtr_node_id) {
  2023. cnss_pr_err("qrtr node id write to register doesn't match with readout value");
  2024. return -ERANGE;
  2025. }
  2026. }
  2027. out:
  2028. return ret;
  2029. }
  2030. #else
  2031. static struct cnss_plat_data *
  2032. cnss_get_plat_priv_by_driver_ops(struct cnss_wlan_driver *driver_ops)
  2033. {
  2034. return cnss_bus_dev_to_plat_priv(NULL);
  2035. }
  2036. static int cnss_pci_store_qrtr_node_id(struct cnss_pci_data *pci_priv)
  2037. {
  2038. return 0;
  2039. }
  2040. #endif
  2041. int cnss_pci_start_mhi(struct cnss_pci_data *pci_priv)
  2042. {
  2043. int ret = 0;
  2044. struct cnss_plat_data *plat_priv;
  2045. unsigned int timeout = 0;
  2046. int retry = 0;
  2047. if (!pci_priv) {
  2048. cnss_pr_err("pci_priv is NULL\n");
  2049. return -ENODEV;
  2050. }
  2051. plat_priv = pci_priv->plat_priv;
  2052. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  2053. return 0;
  2054. if (MHI_TIMEOUT_OVERWRITE_MS)
  2055. pci_priv->mhi_ctrl->timeout_ms = MHI_TIMEOUT_OVERWRITE_MS;
  2056. cnss_mhi_set_m2_timeout_ms(pci_priv, MHI_M2_TIMEOUT_MS);
  2057. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_INIT);
  2058. if (ret)
  2059. return ret;
  2060. timeout = pci_priv->mhi_ctrl->timeout_ms;
  2061. /* For non-perf builds the timeout is 10 (default) * 6 seconds */
  2062. if (cnss_get_host_build_type() == QMI_HOST_BUILD_TYPE_PRIMARY_V01)
  2063. pci_priv->mhi_ctrl->timeout_ms *= 6;
  2064. else /* For perf builds the timeout is 10 (default) * 3 seconds */
  2065. pci_priv->mhi_ctrl->timeout_ms *= 3;
  2066. retry:
  2067. ret = cnss_pci_store_qrtr_node_id(pci_priv);
  2068. if (ret) {
  2069. if (retry++ < REG_RETRY_MAX_TIMES)
  2070. goto retry;
  2071. else
  2072. return ret;
  2073. }
  2074. /* Start the timer to dump MHI/PBL/SBL debug data periodically */
  2075. mod_timer(&pci_priv->boot_debug_timer,
  2076. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  2077. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_ON);
  2078. del_timer_sync(&pci_priv->boot_debug_timer);
  2079. if (ret == 0)
  2080. cnss_wlan_adsp_pc_enable(pci_priv, false);
  2081. pci_priv->mhi_ctrl->timeout_ms = timeout;
  2082. if (ret == -ETIMEDOUT) {
  2083. /* This is a special case needs to be handled that if MHI
  2084. * power on returns -ETIMEDOUT, controller needs to take care
  2085. * the cleanup by calling MHI power down. Force to set the bit
  2086. * for driver internal MHI state to make sure it can be handled
  2087. * properly later.
  2088. */
  2089. set_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state);
  2090. ret = cnss_pci_handle_mhi_poweron_timeout(pci_priv);
  2091. } else if (!ret) {
  2092. /* kernel may allocate a dummy vector before request_irq and
  2093. * then allocate a real vector when request_irq is called.
  2094. * So get msi_data here again to avoid spurious interrupt
  2095. * as msi_data will configured to srngs.
  2096. */
  2097. if (cnss_pci_is_one_msi(pci_priv))
  2098. ret = cnss_pci_config_msi_data(pci_priv);
  2099. }
  2100. return ret;
  2101. }
  2102. static void cnss_pci_power_off_mhi(struct cnss_pci_data *pci_priv)
  2103. {
  2104. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2105. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  2106. return;
  2107. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state)) {
  2108. cnss_pr_dbg("MHI is already powered off\n");
  2109. return;
  2110. }
  2111. cnss_wlan_adsp_pc_enable(pci_priv, true);
  2112. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_RESUME);
  2113. cnss_pci_set_mhi_state_bit(pci_priv, CNSS_MHI_POWERING_OFF);
  2114. if (!pci_priv->pci_link_down_ind)
  2115. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_POWER_OFF);
  2116. else
  2117. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_FORCE_POWER_OFF);
  2118. }
  2119. static void cnss_pci_deinit_mhi(struct cnss_pci_data *pci_priv)
  2120. {
  2121. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2122. if (test_bit(FBC_BYPASS, &plat_priv->ctrl_params.quirks))
  2123. return;
  2124. if (!test_bit(CNSS_MHI_INIT, &pci_priv->mhi_state)) {
  2125. cnss_pr_dbg("MHI is already deinited\n");
  2126. return;
  2127. }
  2128. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_DEINIT);
  2129. }
  2130. static void cnss_pci_set_wlaon_pwr_ctrl(struct cnss_pci_data *pci_priv,
  2131. bool set_vddd4blow, bool set_shutdown,
  2132. bool do_force_wake)
  2133. {
  2134. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2135. int ret;
  2136. u32 val;
  2137. if (!plat_priv->set_wlaon_pwr_ctrl)
  2138. return;
  2139. if (pci_priv->pci_link_state == PCI_LINK_DOWN ||
  2140. pci_priv->pci_link_down_ind)
  2141. return;
  2142. if (do_force_wake)
  2143. if (cnss_pci_force_wake_get(pci_priv))
  2144. return;
  2145. ret = cnss_pci_reg_read(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, &val);
  2146. if (ret) {
  2147. cnss_pr_err("Failed to read register offset 0x%x, err = %d\n",
  2148. WLAON_QFPROM_PWR_CTRL_REG, ret);
  2149. goto force_wake_put;
  2150. }
  2151. cnss_pr_dbg("Read register offset 0x%x, val = 0x%x\n",
  2152. WLAON_QFPROM_PWR_CTRL_REG, val);
  2153. if (set_vddd4blow)
  2154. val |= QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  2155. else
  2156. val &= ~QFPROM_PWR_CTRL_VDD4BLOW_SW_EN_MASK;
  2157. if (set_shutdown)
  2158. val |= QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  2159. else
  2160. val &= ~QFPROM_PWR_CTRL_SHUTDOWN_EN_MASK;
  2161. ret = cnss_pci_reg_write(pci_priv, WLAON_QFPROM_PWR_CTRL_REG, val);
  2162. if (ret) {
  2163. cnss_pr_err("Failed to write register offset 0x%x, err = %d\n",
  2164. WLAON_QFPROM_PWR_CTRL_REG, ret);
  2165. goto force_wake_put;
  2166. }
  2167. cnss_pr_dbg("Write val 0x%x to register offset 0x%x\n", val,
  2168. WLAON_QFPROM_PWR_CTRL_REG);
  2169. if (set_shutdown)
  2170. usleep_range(WLAON_PWR_CTRL_SHUTDOWN_DELAY_MIN_US,
  2171. WLAON_PWR_CTRL_SHUTDOWN_DELAY_MAX_US);
  2172. force_wake_put:
  2173. if (do_force_wake)
  2174. cnss_pci_force_wake_put(pci_priv);
  2175. }
  2176. static int cnss_pci_get_device_timestamp(struct cnss_pci_data *pci_priv,
  2177. u64 *time_us)
  2178. {
  2179. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2180. u32 low, high;
  2181. u64 device_ticks;
  2182. if (!plat_priv->device_freq_hz) {
  2183. cnss_pr_err("Device time clock frequency is not valid\n");
  2184. return -EINVAL;
  2185. }
  2186. switch (pci_priv->device_id) {
  2187. case KIWI_DEVICE_ID:
  2188. case MANGO_DEVICE_ID:
  2189. case PEACH_DEVICE_ID:
  2190. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_LOW, &low);
  2191. cnss_pci_reg_read(pci_priv, PCIE_MHI_TIME_HIGH, &high);
  2192. break;
  2193. default:
  2194. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL3, &low);
  2195. cnss_pci_reg_read(pci_priv, WLAON_GLOBAL_COUNTER_CTRL4, &high);
  2196. break;
  2197. }
  2198. device_ticks = (u64)high << 32 | low;
  2199. do_div(device_ticks, plat_priv->device_freq_hz / 100000);
  2200. *time_us = device_ticks * 10;
  2201. return 0;
  2202. }
  2203. static void cnss_pci_enable_time_sync_counter(struct cnss_pci_data *pci_priv)
  2204. {
  2205. switch (pci_priv->device_id) {
  2206. case KIWI_DEVICE_ID:
  2207. case MANGO_DEVICE_ID:
  2208. case PEACH_DEVICE_ID:
  2209. return;
  2210. default:
  2211. break;
  2212. }
  2213. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2214. TIME_SYNC_ENABLE);
  2215. }
  2216. static void cnss_pci_clear_time_sync_counter(struct cnss_pci_data *pci_priv)
  2217. {
  2218. switch (pci_priv->device_id) {
  2219. case KIWI_DEVICE_ID:
  2220. case MANGO_DEVICE_ID:
  2221. case PEACH_DEVICE_ID:
  2222. return;
  2223. default:
  2224. break;
  2225. }
  2226. cnss_pci_reg_write(pci_priv, WLAON_GLOBAL_COUNTER_CTRL5,
  2227. TIME_SYNC_CLEAR);
  2228. }
  2229. static void cnss_pci_time_sync_reg_update(struct cnss_pci_data *pci_priv,
  2230. u32 low, u32 high)
  2231. {
  2232. u32 time_reg_low;
  2233. u32 time_reg_high;
  2234. switch (pci_priv->device_id) {
  2235. case KIWI_DEVICE_ID:
  2236. case MANGO_DEVICE_ID:
  2237. case PEACH_DEVICE_ID:
  2238. /* Use the next two shadow registers after host's usage */
  2239. time_reg_low = PCIE_SHADOW_REG_VALUE_0 +
  2240. (pci_priv->plat_priv->num_shadow_regs_v3 *
  2241. SHADOW_REG_LEN_BYTES);
  2242. time_reg_high = time_reg_low + SHADOW_REG_LEN_BYTES;
  2243. break;
  2244. default:
  2245. time_reg_low = PCIE_SHADOW_REG_VALUE_34;
  2246. time_reg_high = PCIE_SHADOW_REG_VALUE_35;
  2247. break;
  2248. }
  2249. cnss_pci_reg_write(pci_priv, time_reg_low, low);
  2250. cnss_pci_reg_write(pci_priv, time_reg_high, high);
  2251. cnss_pci_reg_read(pci_priv, time_reg_low, &low);
  2252. cnss_pci_reg_read(pci_priv, time_reg_high, &high);
  2253. cnss_pr_dbg("Updated time sync regs [0x%x] = 0x%x, [0x%x] = 0x%x\n",
  2254. time_reg_low, low, time_reg_high, high);
  2255. }
  2256. static int cnss_pci_update_timestamp(struct cnss_pci_data *pci_priv)
  2257. {
  2258. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2259. struct device *dev = &pci_priv->pci_dev->dev;
  2260. unsigned long flags = 0;
  2261. u64 host_time_us, device_time_us, offset;
  2262. u32 low, high;
  2263. int ret;
  2264. ret = cnss_pci_prevent_l1(dev);
  2265. if (ret)
  2266. goto out;
  2267. ret = cnss_pci_force_wake_get(pci_priv);
  2268. if (ret)
  2269. goto allow_l1;
  2270. spin_lock_irqsave(&time_sync_lock, flags);
  2271. cnss_pci_clear_time_sync_counter(pci_priv);
  2272. cnss_pci_enable_time_sync_counter(pci_priv);
  2273. host_time_us = cnss_get_host_timestamp(plat_priv);
  2274. ret = cnss_pci_get_device_timestamp(pci_priv, &device_time_us);
  2275. cnss_pci_clear_time_sync_counter(pci_priv);
  2276. spin_unlock_irqrestore(&time_sync_lock, flags);
  2277. if (ret)
  2278. goto force_wake_put;
  2279. if (host_time_us < device_time_us) {
  2280. cnss_pr_err("Host time (%llu us) is smaller than device time (%llu us), stop\n",
  2281. host_time_us, device_time_us);
  2282. ret = -EINVAL;
  2283. goto force_wake_put;
  2284. }
  2285. offset = host_time_us - device_time_us;
  2286. cnss_pr_dbg("Host time = %llu us, device time = %llu us, offset = %llu us\n",
  2287. host_time_us, device_time_us, offset);
  2288. low = offset & 0xFFFFFFFF;
  2289. high = offset >> 32;
  2290. cnss_pci_time_sync_reg_update(pci_priv, low, high);
  2291. force_wake_put:
  2292. cnss_pci_force_wake_put(pci_priv);
  2293. allow_l1:
  2294. cnss_pci_allow_l1(dev);
  2295. out:
  2296. return ret;
  2297. }
  2298. static void cnss_pci_time_sync_work_hdlr(struct work_struct *work)
  2299. {
  2300. struct cnss_pci_data *pci_priv =
  2301. container_of(work, struct cnss_pci_data, time_sync_work.work);
  2302. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2303. unsigned int time_sync_period_ms =
  2304. plat_priv->ctrl_params.time_sync_period;
  2305. if (test_bit(DISABLE_TIME_SYNC, &plat_priv->ctrl_params.quirks)) {
  2306. cnss_pr_dbg("Time sync is disabled\n");
  2307. return;
  2308. }
  2309. if (!time_sync_period_ms) {
  2310. cnss_pr_dbg("Skip time sync as time period is 0\n");
  2311. return;
  2312. }
  2313. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  2314. return;
  2315. if (cnss_pci_pm_runtime_get_sync(pci_priv, RTPM_ID_CNSS) < 0)
  2316. goto runtime_pm_put;
  2317. mutex_lock(&pci_priv->bus_lock);
  2318. cnss_pci_update_timestamp(pci_priv);
  2319. mutex_unlock(&pci_priv->bus_lock);
  2320. schedule_delayed_work(&pci_priv->time_sync_work,
  2321. msecs_to_jiffies(time_sync_period_ms));
  2322. runtime_pm_put:
  2323. cnss_pci_pm_runtime_mark_last_busy(pci_priv);
  2324. cnss_pci_pm_runtime_put_autosuspend(pci_priv, RTPM_ID_CNSS);
  2325. }
  2326. static int cnss_pci_start_time_sync_update(struct cnss_pci_data *pci_priv)
  2327. {
  2328. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2329. switch (pci_priv->device_id) {
  2330. case QCA6390_DEVICE_ID:
  2331. case QCA6490_DEVICE_ID:
  2332. case KIWI_DEVICE_ID:
  2333. case MANGO_DEVICE_ID:
  2334. case PEACH_DEVICE_ID:
  2335. break;
  2336. default:
  2337. return -EOPNOTSUPP;
  2338. }
  2339. if (!plat_priv->device_freq_hz) {
  2340. cnss_pr_dbg("Device time clock frequency is not valid, skip time sync\n");
  2341. return -EINVAL;
  2342. }
  2343. cnss_pci_time_sync_work_hdlr(&pci_priv->time_sync_work.work);
  2344. return 0;
  2345. }
  2346. static void cnss_pci_stop_time_sync_update(struct cnss_pci_data *pci_priv)
  2347. {
  2348. switch (pci_priv->device_id) {
  2349. case QCA6390_DEVICE_ID:
  2350. case QCA6490_DEVICE_ID:
  2351. case KIWI_DEVICE_ID:
  2352. case MANGO_DEVICE_ID:
  2353. case PEACH_DEVICE_ID:
  2354. break;
  2355. default:
  2356. return;
  2357. }
  2358. cancel_delayed_work_sync(&pci_priv->time_sync_work);
  2359. }
  2360. int cnss_pci_set_therm_cdev_state(struct cnss_pci_data *pci_priv,
  2361. unsigned long thermal_state,
  2362. int tcdev_id)
  2363. {
  2364. if (!pci_priv) {
  2365. cnss_pr_err("pci_priv is NULL!\n");
  2366. return -ENODEV;
  2367. }
  2368. if (!pci_priv->driver_ops || !pci_priv->driver_ops->set_therm_cdev_state) {
  2369. cnss_pr_err("driver_ops or set_therm_cdev_state is NULL\n");
  2370. return -EINVAL;
  2371. }
  2372. return pci_priv->driver_ops->set_therm_cdev_state(pci_priv->pci_dev,
  2373. thermal_state,
  2374. tcdev_id);
  2375. }
  2376. int cnss_pci_update_time_sync_period(struct cnss_pci_data *pci_priv,
  2377. unsigned int time_sync_period)
  2378. {
  2379. struct cnss_plat_data *plat_priv;
  2380. if (!pci_priv)
  2381. return -ENODEV;
  2382. plat_priv = pci_priv->plat_priv;
  2383. cnss_pci_stop_time_sync_update(pci_priv);
  2384. plat_priv->ctrl_params.time_sync_period = time_sync_period;
  2385. cnss_pci_start_time_sync_update(pci_priv);
  2386. cnss_pr_dbg("WLAN time sync period %u ms\n",
  2387. plat_priv->ctrl_params.time_sync_period);
  2388. return 0;
  2389. }
  2390. int cnss_pci_call_driver_probe(struct cnss_pci_data *pci_priv)
  2391. {
  2392. int ret = 0;
  2393. struct cnss_plat_data *plat_priv;
  2394. if (!pci_priv)
  2395. return -ENODEV;
  2396. plat_priv = pci_priv->plat_priv;
  2397. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  2398. cnss_pr_err("Reboot is in progress, skip driver probe\n");
  2399. return -EINVAL;
  2400. }
  2401. if (test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2402. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2403. cnss_pr_dbg("Skip driver probe\n");
  2404. goto out;
  2405. }
  2406. if (!pci_priv->driver_ops) {
  2407. cnss_pr_err("driver_ops is NULL\n");
  2408. ret = -EINVAL;
  2409. goto out;
  2410. }
  2411. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2412. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2413. ret = pci_priv->driver_ops->reinit(pci_priv->pci_dev,
  2414. pci_priv->pci_device_id);
  2415. if (ret) {
  2416. cnss_pr_err("Failed to reinit host driver, err = %d\n",
  2417. ret);
  2418. goto out;
  2419. }
  2420. complete(&plat_priv->recovery_complete);
  2421. } else if (test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state)) {
  2422. ret = pci_priv->driver_ops->probe(pci_priv->pci_dev,
  2423. pci_priv->pci_device_id);
  2424. if (ret) {
  2425. cnss_pr_err("Failed to probe host driver, err = %d\n",
  2426. ret);
  2427. goto out;
  2428. }
  2429. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  2430. set_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2431. cnss_pci_free_blob_mem(pci_priv);
  2432. complete_all(&plat_priv->power_up_complete);
  2433. } else if (test_bit(CNSS_DRIVER_IDLE_RESTART,
  2434. &plat_priv->driver_state)) {
  2435. ret = pci_priv->driver_ops->idle_restart(pci_priv->pci_dev,
  2436. pci_priv->pci_device_id);
  2437. if (ret) {
  2438. cnss_pr_err("Failed to idle restart host driver, err = %d\n",
  2439. ret);
  2440. plat_priv->power_up_error = ret;
  2441. complete_all(&plat_priv->power_up_complete);
  2442. goto out;
  2443. }
  2444. clear_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state);
  2445. complete_all(&plat_priv->power_up_complete);
  2446. } else {
  2447. complete(&plat_priv->power_up_complete);
  2448. }
  2449. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state)) {
  2450. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2451. __pm_relax(plat_priv->recovery_ws);
  2452. }
  2453. cnss_pci_start_time_sync_update(pci_priv);
  2454. return 0;
  2455. out:
  2456. return ret;
  2457. }
  2458. int cnss_pci_call_driver_remove(struct cnss_pci_data *pci_priv)
  2459. {
  2460. struct cnss_plat_data *plat_priv;
  2461. int ret;
  2462. if (!pci_priv)
  2463. return -ENODEV;
  2464. plat_priv = pci_priv->plat_priv;
  2465. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) ||
  2466. test_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state) ||
  2467. test_bit(CNSS_DRIVER_DEBUG, &plat_priv->driver_state)) {
  2468. cnss_pr_dbg("Skip driver remove\n");
  2469. return 0;
  2470. }
  2471. if (!pci_priv->driver_ops) {
  2472. cnss_pr_err("driver_ops is NULL\n");
  2473. return -EINVAL;
  2474. }
  2475. cnss_pci_stop_time_sync_update(pci_priv);
  2476. if (test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  2477. test_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state)) {
  2478. pci_priv->driver_ops->shutdown(pci_priv->pci_dev);
  2479. } else if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state)) {
  2480. pci_priv->driver_ops->remove(pci_priv->pci_dev);
  2481. clear_bit(CNSS_DRIVER_PROBED, &plat_priv->driver_state);
  2482. } else if (test_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2483. &plat_priv->driver_state)) {
  2484. ret = pci_priv->driver_ops->idle_shutdown(pci_priv->pci_dev);
  2485. if (ret == -EAGAIN) {
  2486. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN,
  2487. &plat_priv->driver_state);
  2488. return ret;
  2489. }
  2490. }
  2491. plat_priv->get_info_cb_ctx = NULL;
  2492. plat_priv->get_info_cb = NULL;
  2493. return 0;
  2494. }
  2495. int cnss_pci_call_driver_modem_status(struct cnss_pci_data *pci_priv,
  2496. int modem_current_status)
  2497. {
  2498. struct cnss_wlan_driver *driver_ops;
  2499. if (!pci_priv)
  2500. return -ENODEV;
  2501. driver_ops = pci_priv->driver_ops;
  2502. if (!driver_ops || !driver_ops->modem_status)
  2503. return -EINVAL;
  2504. driver_ops->modem_status(pci_priv->pci_dev, modem_current_status);
  2505. return 0;
  2506. }
  2507. int cnss_pci_update_status(struct cnss_pci_data *pci_priv,
  2508. enum cnss_driver_status status)
  2509. {
  2510. struct cnss_wlan_driver *driver_ops;
  2511. if (!pci_priv)
  2512. return -ENODEV;
  2513. driver_ops = pci_priv->driver_ops;
  2514. if (!driver_ops || !driver_ops->update_status)
  2515. return -EINVAL;
  2516. cnss_pr_dbg("Update driver status: %d\n", status);
  2517. driver_ops->update_status(pci_priv->pci_dev, status);
  2518. return 0;
  2519. }
  2520. static void cnss_pci_misc_reg_dump(struct cnss_pci_data *pci_priv,
  2521. struct cnss_misc_reg *misc_reg,
  2522. u32 misc_reg_size,
  2523. char *reg_name)
  2524. {
  2525. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2526. bool do_force_wake_put = true;
  2527. int i;
  2528. if (!misc_reg)
  2529. return;
  2530. if (in_interrupt() || irqs_disabled())
  2531. return;
  2532. if (cnss_pci_check_link_status(pci_priv))
  2533. return;
  2534. if (cnss_pci_force_wake_get(pci_priv)) {
  2535. /* Continue to dump when device has entered RDDM already */
  2536. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2537. return;
  2538. do_force_wake_put = false;
  2539. }
  2540. cnss_pr_dbg("Start to dump %s registers\n", reg_name);
  2541. for (i = 0; i < misc_reg_size; i++) {
  2542. if (!test_bit(pci_priv->misc_reg_dev_mask,
  2543. &misc_reg[i].dev_mask))
  2544. continue;
  2545. if (misc_reg[i].wr) {
  2546. if (misc_reg[i].offset ==
  2547. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG &&
  2548. i >= 1)
  2549. misc_reg[i].val =
  2550. QCA6390_WCSS_Q6SS_PRIVCSR_QDSP6SS_SAW2_CFG_MSK |
  2551. misc_reg[i - 1].val;
  2552. if (cnss_pci_reg_write(pci_priv,
  2553. misc_reg[i].offset,
  2554. misc_reg[i].val))
  2555. goto force_wake_put;
  2556. cnss_pr_vdbg("Write 0x%X to 0x%X\n",
  2557. misc_reg[i].val,
  2558. misc_reg[i].offset);
  2559. } else {
  2560. if (cnss_pci_reg_read(pci_priv,
  2561. misc_reg[i].offset,
  2562. &misc_reg[i].val))
  2563. goto force_wake_put;
  2564. }
  2565. }
  2566. force_wake_put:
  2567. if (do_force_wake_put)
  2568. cnss_pci_force_wake_put(pci_priv);
  2569. }
  2570. static void cnss_pci_dump_misc_reg(struct cnss_pci_data *pci_priv)
  2571. {
  2572. if (in_interrupt() || irqs_disabled())
  2573. return;
  2574. if (cnss_pci_check_link_status(pci_priv))
  2575. return;
  2576. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wcss_reg,
  2577. WCSS_REG_SIZE, "wcss");
  2578. cnss_pci_misc_reg_dump(pci_priv, pci_priv->pcie_reg,
  2579. PCIE_REG_SIZE, "pcie");
  2580. cnss_pci_misc_reg_dump(pci_priv, pci_priv->wlaon_reg,
  2581. WLAON_REG_SIZE, "wlaon");
  2582. cnss_pci_misc_reg_dump(pci_priv, pci_priv->syspm_reg,
  2583. SYSPM_REG_SIZE, "syspm");
  2584. }
  2585. static void cnss_pci_dump_shadow_reg(struct cnss_pci_data *pci_priv)
  2586. {
  2587. int i, j = 0, array_size = SHADOW_REG_COUNT + SHADOW_REG_INTER_COUNT;
  2588. u32 reg_offset;
  2589. bool do_force_wake_put = true;
  2590. if (in_interrupt() || irqs_disabled())
  2591. return;
  2592. if (cnss_pci_check_link_status(pci_priv))
  2593. return;
  2594. if (!pci_priv->debug_reg) {
  2595. pci_priv->debug_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  2596. sizeof(*pci_priv->debug_reg)
  2597. * array_size, GFP_KERNEL);
  2598. if (!pci_priv->debug_reg)
  2599. return;
  2600. }
  2601. if (cnss_pci_force_wake_get(pci_priv))
  2602. do_force_wake_put = false;
  2603. cnss_pr_dbg("Start to dump shadow registers\n");
  2604. for (i = 0; i < SHADOW_REG_COUNT; i++, j++) {
  2605. reg_offset = PCIE_SHADOW_REG_VALUE_0 + i * 4;
  2606. pci_priv->debug_reg[j].offset = reg_offset;
  2607. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2608. &pci_priv->debug_reg[j].val))
  2609. goto force_wake_put;
  2610. }
  2611. for (i = 0; i < SHADOW_REG_INTER_COUNT; i++, j++) {
  2612. reg_offset = PCIE_SHADOW_REG_INTER_0 + i * 4;
  2613. pci_priv->debug_reg[j].offset = reg_offset;
  2614. if (cnss_pci_reg_read(pci_priv, reg_offset,
  2615. &pci_priv->debug_reg[j].val))
  2616. goto force_wake_put;
  2617. }
  2618. force_wake_put:
  2619. if (do_force_wake_put)
  2620. cnss_pci_force_wake_put(pci_priv);
  2621. }
  2622. static int cnss_qca6174_powerup(struct cnss_pci_data *pci_priv)
  2623. {
  2624. int ret = 0;
  2625. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2626. ret = cnss_power_on_device(plat_priv, false);
  2627. if (ret) {
  2628. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2629. goto out;
  2630. }
  2631. ret = cnss_resume_pci_link(pci_priv);
  2632. if (ret) {
  2633. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2634. goto power_off;
  2635. }
  2636. ret = cnss_pci_call_driver_probe(pci_priv);
  2637. if (ret)
  2638. goto suspend_link;
  2639. return 0;
  2640. suspend_link:
  2641. cnss_suspend_pci_link(pci_priv);
  2642. power_off:
  2643. cnss_power_off_device(plat_priv);
  2644. out:
  2645. return ret;
  2646. }
  2647. static int cnss_qca6174_shutdown(struct cnss_pci_data *pci_priv)
  2648. {
  2649. int ret = 0;
  2650. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2651. cnss_pci_pm_runtime_resume(pci_priv);
  2652. ret = cnss_pci_call_driver_remove(pci_priv);
  2653. if (ret == -EAGAIN)
  2654. goto out;
  2655. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2656. CNSS_BUS_WIDTH_NONE);
  2657. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2658. cnss_pci_set_auto_suspended(pci_priv, 0);
  2659. ret = cnss_suspend_pci_link(pci_priv);
  2660. if (ret)
  2661. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2662. cnss_power_off_device(plat_priv);
  2663. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2664. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2665. out:
  2666. return ret;
  2667. }
  2668. static void cnss_qca6174_crash_shutdown(struct cnss_pci_data *pci_priv)
  2669. {
  2670. if (pci_priv->driver_ops && pci_priv->driver_ops->crash_shutdown)
  2671. pci_priv->driver_ops->crash_shutdown(pci_priv->pci_dev);
  2672. }
  2673. static int cnss_qca6174_ramdump(struct cnss_pci_data *pci_priv)
  2674. {
  2675. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2676. struct cnss_ramdump_info *ramdump_info;
  2677. ramdump_info = &plat_priv->ramdump_info;
  2678. if (!ramdump_info->ramdump_size)
  2679. return -EINVAL;
  2680. return cnss_do_ramdump(plat_priv);
  2681. }
  2682. static void cnss_get_driver_mode_update_fw_name(struct cnss_plat_data *plat_priv)
  2683. {
  2684. struct cnss_pci_data *pci_priv;
  2685. struct cnss_wlan_driver *driver_ops;
  2686. pci_priv = plat_priv->bus_priv;
  2687. driver_ops = pci_priv->driver_ops;
  2688. if (driver_ops && driver_ops->get_driver_mode) {
  2689. plat_priv->driver_mode = driver_ops->get_driver_mode();
  2690. cnss_pci_update_fw_name(pci_priv);
  2691. cnss_pr_dbg("New driver mode is %d", plat_priv->driver_mode);
  2692. }
  2693. }
  2694. static int cnss_qca6290_powerup(struct cnss_pci_data *pci_priv)
  2695. {
  2696. int ret = 0;
  2697. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2698. unsigned int timeout;
  2699. int retry = 0, bt_en_gpio = plat_priv->pinctrl_info.bt_en_gpio;
  2700. int sw_ctrl_gpio = plat_priv->pinctrl_info.sw_ctrl_gpio;
  2701. if (plat_priv->ramdump_info_v2.dump_data_valid) {
  2702. cnss_pci_clear_dump_info(pci_priv);
  2703. cnss_pci_power_off_mhi(pci_priv);
  2704. cnss_suspend_pci_link(pci_priv);
  2705. cnss_pci_deinit_mhi(pci_priv);
  2706. cnss_power_off_device(plat_priv);
  2707. }
  2708. /* Clear QMI send usage count during every power up */
  2709. pci_priv->qmi_send_usage_count = 0;
  2710. plat_priv->power_up_error = 0;
  2711. cnss_get_driver_mode_update_fw_name(plat_priv);
  2712. retry:
  2713. ret = cnss_power_on_device(plat_priv, false);
  2714. if (ret) {
  2715. cnss_pr_err("Failed to power on device, err = %d\n", ret);
  2716. goto out;
  2717. }
  2718. ret = cnss_resume_pci_link(pci_priv);
  2719. if (ret) {
  2720. cnss_pr_err("Failed to resume PCI link, err = %d\n", ret);
  2721. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2722. cnss_get_input_gpio_value(plat_priv, sw_ctrl_gpio));
  2723. if (test_bit(IGNORE_PCI_LINK_FAILURE,
  2724. &plat_priv->ctrl_params.quirks)) {
  2725. cnss_pr_dbg("Ignore PCI link resume failure\n");
  2726. ret = 0;
  2727. goto out;
  2728. }
  2729. if (ret == -EAGAIN && retry++ < POWER_ON_RETRY_MAX_TIMES) {
  2730. cnss_power_off_device(plat_priv);
  2731. /* Force toggle BT_EN GPIO low */
  2732. if (retry == POWER_ON_RETRY_MAX_TIMES) {
  2733. cnss_pr_dbg("Retry #%d. Set BT_EN GPIO(%u) low\n",
  2734. retry, bt_en_gpio);
  2735. if (bt_en_gpio >= 0)
  2736. gpio_direction_output(bt_en_gpio, 0);
  2737. cnss_pr_dbg("BT_EN GPIO val: %d\n",
  2738. gpio_get_value(bt_en_gpio));
  2739. }
  2740. cnss_pr_dbg("Retry to resume PCI link #%d\n", retry);
  2741. cnss_pr_dbg("Value of SW_CTRL GPIO: %d\n",
  2742. cnss_get_input_gpio_value(plat_priv,
  2743. sw_ctrl_gpio));
  2744. msleep(POWER_ON_RETRY_DELAY_MS * retry);
  2745. goto retry;
  2746. }
  2747. /* Assert when it reaches maximum retries */
  2748. CNSS_ASSERT(0);
  2749. goto power_off;
  2750. }
  2751. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false, false);
  2752. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_QMI);
  2753. ret = cnss_pci_start_mhi(pci_priv);
  2754. if (ret) {
  2755. cnss_fatal_err("Failed to start MHI, err = %d\n", ret);
  2756. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  2757. !pci_priv->pci_link_down_ind && timeout) {
  2758. /* Start recovery directly for MHI start failures */
  2759. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  2760. CNSS_REASON_DEFAULT);
  2761. }
  2762. return 0;
  2763. }
  2764. if (test_bit(USE_CORE_ONLY_FW, &plat_priv->ctrl_params.quirks)) {
  2765. clear_bit(CNSS_FW_BOOT_RECOVERY, &plat_priv->driver_state);
  2766. clear_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state);
  2767. return 0;
  2768. }
  2769. cnss_set_pin_connect_status(plat_priv);
  2770. if (test_bit(QMI_BYPASS, &plat_priv->ctrl_params.quirks)) {
  2771. ret = cnss_pci_call_driver_probe(pci_priv);
  2772. if (ret)
  2773. goto stop_mhi;
  2774. } else if (timeout) {
  2775. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state))
  2776. timeout += WLAN_COLD_BOOT_CAL_TIMEOUT;
  2777. else
  2778. timeout += WLAN_MISSION_MODE_TIMEOUT;
  2779. mod_timer(&plat_priv->fw_boot_timer,
  2780. jiffies + msecs_to_jiffies(timeout));
  2781. }
  2782. return 0;
  2783. stop_mhi:
  2784. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, true);
  2785. cnss_pci_power_off_mhi(pci_priv);
  2786. cnss_suspend_pci_link(pci_priv);
  2787. cnss_pci_deinit_mhi(pci_priv);
  2788. power_off:
  2789. cnss_power_off_device(plat_priv);
  2790. out:
  2791. return ret;
  2792. }
  2793. static int cnss_qca6290_shutdown(struct cnss_pci_data *pci_priv)
  2794. {
  2795. int ret = 0;
  2796. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2797. int do_force_wake = true;
  2798. cnss_pci_pm_runtime_resume(pci_priv);
  2799. ret = cnss_pci_call_driver_remove(pci_priv);
  2800. if (ret == -EAGAIN)
  2801. goto out;
  2802. cnss_request_bus_bandwidth(&plat_priv->plat_dev->dev,
  2803. CNSS_BUS_WIDTH_NONE);
  2804. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  2805. cnss_pci_set_auto_suspended(pci_priv, 0);
  2806. if ((test_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state) ||
  2807. test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2808. test_bit(CNSS_DRIVER_IDLE_RESTART, &plat_priv->driver_state) ||
  2809. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state) ||
  2810. test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) &&
  2811. test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  2812. del_timer(&pci_priv->dev_rddm_timer);
  2813. cnss_pci_collect_dump_info(pci_priv, false);
  2814. if (!plat_priv->recovery_enabled)
  2815. CNSS_ASSERT(0);
  2816. }
  2817. if (!cnss_is_device_powered_on(plat_priv)) {
  2818. cnss_pr_dbg("Device is already powered off, ignore\n");
  2819. goto skip_power_off;
  2820. }
  2821. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  2822. do_force_wake = false;
  2823. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, do_force_wake);
  2824. /* FBC image will be freed after powering off MHI, so skip
  2825. * if RAM dump data is still valid.
  2826. */
  2827. if (plat_priv->ramdump_info_v2.dump_data_valid)
  2828. goto skip_power_off;
  2829. cnss_pci_power_off_mhi(pci_priv);
  2830. ret = cnss_suspend_pci_link(pci_priv);
  2831. if (ret)
  2832. cnss_pr_err("Failed to suspend PCI link, err = %d\n", ret);
  2833. cnss_pci_deinit_mhi(pci_priv);
  2834. cnss_power_off_device(plat_priv);
  2835. skip_power_off:
  2836. pci_priv->remap_window = 0;
  2837. clear_bit(CNSS_FW_READY, &plat_priv->driver_state);
  2838. clear_bit(CNSS_FW_MEM_READY, &plat_priv->driver_state);
  2839. if (test_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state) ||
  2840. test_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state)) {
  2841. clear_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  2842. pci_priv->pci_link_down_ind = false;
  2843. }
  2844. clear_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  2845. clear_bit(CNSS_DRIVER_IDLE_SHUTDOWN, &plat_priv->driver_state);
  2846. memset(&print_optimize, 0, sizeof(print_optimize));
  2847. out:
  2848. return ret;
  2849. }
  2850. static void cnss_qca6290_crash_shutdown(struct cnss_pci_data *pci_priv)
  2851. {
  2852. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2853. set_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2854. cnss_pr_dbg("Crash shutdown with driver_state 0x%lx\n",
  2855. plat_priv->driver_state);
  2856. cnss_pci_collect_dump_info(pci_priv, true);
  2857. clear_bit(CNSS_IN_PANIC, &plat_priv->driver_state);
  2858. }
  2859. static int cnss_qca6290_ramdump(struct cnss_pci_data *pci_priv)
  2860. {
  2861. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  2862. struct cnss_ramdump_info_v2 *info_v2 = &plat_priv->ramdump_info_v2;
  2863. struct cnss_dump_data *dump_data = &info_v2->dump_data;
  2864. struct cnss_dump_seg *dump_seg = info_v2->dump_data_vaddr;
  2865. int ret = 0;
  2866. if (!info_v2->dump_data_valid || !dump_seg ||
  2867. dump_data->nentries == 0)
  2868. return 0;
  2869. ret = cnss_do_elf_ramdump(plat_priv);
  2870. cnss_pci_clear_dump_info(pci_priv);
  2871. cnss_pci_power_off_mhi(pci_priv);
  2872. cnss_suspend_pci_link(pci_priv);
  2873. cnss_pci_deinit_mhi(pci_priv);
  2874. cnss_power_off_device(plat_priv);
  2875. return ret;
  2876. }
  2877. int cnss_pci_dev_powerup(struct cnss_pci_data *pci_priv)
  2878. {
  2879. int ret = 0;
  2880. if (!pci_priv) {
  2881. cnss_pr_err("pci_priv is NULL\n");
  2882. return -ENODEV;
  2883. }
  2884. switch (pci_priv->device_id) {
  2885. case QCA6174_DEVICE_ID:
  2886. ret = cnss_qca6174_powerup(pci_priv);
  2887. break;
  2888. case QCA6290_DEVICE_ID:
  2889. case QCA6390_DEVICE_ID:
  2890. case QCN7605_DEVICE_ID:
  2891. case QCA6490_DEVICE_ID:
  2892. case KIWI_DEVICE_ID:
  2893. case MANGO_DEVICE_ID:
  2894. case PEACH_DEVICE_ID:
  2895. ret = cnss_qca6290_powerup(pci_priv);
  2896. break;
  2897. default:
  2898. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2899. pci_priv->device_id);
  2900. ret = -ENODEV;
  2901. }
  2902. return ret;
  2903. }
  2904. int cnss_pci_dev_shutdown(struct cnss_pci_data *pci_priv)
  2905. {
  2906. int ret = 0;
  2907. if (!pci_priv) {
  2908. cnss_pr_err("pci_priv is NULL\n");
  2909. return -ENODEV;
  2910. }
  2911. switch (pci_priv->device_id) {
  2912. case QCA6174_DEVICE_ID:
  2913. ret = cnss_qca6174_shutdown(pci_priv);
  2914. break;
  2915. case QCA6290_DEVICE_ID:
  2916. case QCA6390_DEVICE_ID:
  2917. case QCN7605_DEVICE_ID:
  2918. case QCA6490_DEVICE_ID:
  2919. case KIWI_DEVICE_ID:
  2920. case MANGO_DEVICE_ID:
  2921. case PEACH_DEVICE_ID:
  2922. ret = cnss_qca6290_shutdown(pci_priv);
  2923. break;
  2924. default:
  2925. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2926. pci_priv->device_id);
  2927. ret = -ENODEV;
  2928. }
  2929. return ret;
  2930. }
  2931. int cnss_pci_dev_crash_shutdown(struct cnss_pci_data *pci_priv)
  2932. {
  2933. int ret = 0;
  2934. if (!pci_priv) {
  2935. cnss_pr_err("pci_priv is NULL\n");
  2936. return -ENODEV;
  2937. }
  2938. switch (pci_priv->device_id) {
  2939. case QCA6174_DEVICE_ID:
  2940. cnss_qca6174_crash_shutdown(pci_priv);
  2941. break;
  2942. case QCA6290_DEVICE_ID:
  2943. case QCA6390_DEVICE_ID:
  2944. case QCN7605_DEVICE_ID:
  2945. case QCA6490_DEVICE_ID:
  2946. case KIWI_DEVICE_ID:
  2947. case MANGO_DEVICE_ID:
  2948. case PEACH_DEVICE_ID:
  2949. cnss_qca6290_crash_shutdown(pci_priv);
  2950. break;
  2951. default:
  2952. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2953. pci_priv->device_id);
  2954. ret = -ENODEV;
  2955. }
  2956. return ret;
  2957. }
  2958. int cnss_pci_dev_ramdump(struct cnss_pci_data *pci_priv)
  2959. {
  2960. int ret = 0;
  2961. if (!pci_priv) {
  2962. cnss_pr_err("pci_priv is NULL\n");
  2963. return -ENODEV;
  2964. }
  2965. switch (pci_priv->device_id) {
  2966. case QCA6174_DEVICE_ID:
  2967. ret = cnss_qca6174_ramdump(pci_priv);
  2968. break;
  2969. case QCA6290_DEVICE_ID:
  2970. case QCA6390_DEVICE_ID:
  2971. case QCN7605_DEVICE_ID:
  2972. case QCA6490_DEVICE_ID:
  2973. case KIWI_DEVICE_ID:
  2974. case MANGO_DEVICE_ID:
  2975. case PEACH_DEVICE_ID:
  2976. ret = cnss_qca6290_ramdump(pci_priv);
  2977. break;
  2978. default:
  2979. cnss_pr_err("Unknown device_id found: 0x%x\n",
  2980. pci_priv->device_id);
  2981. ret = -ENODEV;
  2982. }
  2983. return ret;
  2984. }
  2985. int cnss_pci_is_drv_connected(struct device *dev)
  2986. {
  2987. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  2988. if (!pci_priv)
  2989. return -ENODEV;
  2990. return pci_priv->drv_connected_last;
  2991. }
  2992. EXPORT_SYMBOL(cnss_pci_is_drv_connected);
  2993. static void cnss_wlan_reg_driver_work(struct work_struct *work)
  2994. {
  2995. struct cnss_plat_data *plat_priv =
  2996. container_of(work, struct cnss_plat_data, wlan_reg_driver_work.work);
  2997. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  2998. struct cnss_cal_info *cal_info;
  2999. unsigned int timeout;
  3000. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state))
  3001. return;
  3002. if (test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state)) {
  3003. goto reg_driver;
  3004. } else {
  3005. if (plat_priv->charger_mode) {
  3006. cnss_pr_err("Ignore calibration timeout in charger mode\n");
  3007. return;
  3008. }
  3009. if (!test_bit(CNSS_IN_COLD_BOOT_CAL,
  3010. &plat_priv->driver_state)) {
  3011. timeout = cnss_get_timeout(plat_priv,
  3012. CNSS_TIMEOUT_CALIBRATION);
  3013. cnss_pr_dbg("File system not ready to start calibration. Wait for %ds..\n",
  3014. timeout / 1000);
  3015. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  3016. msecs_to_jiffies(timeout));
  3017. return;
  3018. }
  3019. del_timer(&plat_priv->fw_boot_timer);
  3020. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state) &&
  3021. !test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  3022. cnss_pr_err("Timeout waiting for calibration to complete\n");
  3023. CNSS_ASSERT(0);
  3024. }
  3025. cal_info = kzalloc(sizeof(*cal_info), GFP_KERNEL);
  3026. if (!cal_info)
  3027. return;
  3028. cal_info->cal_status = CNSS_CAL_TIMEOUT;
  3029. cnss_driver_event_post(plat_priv,
  3030. CNSS_DRIVER_EVENT_COLD_BOOT_CAL_DONE,
  3031. 0, cal_info);
  3032. }
  3033. reg_driver:
  3034. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  3035. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  3036. return;
  3037. }
  3038. reinit_completion(&plat_priv->power_up_complete);
  3039. cnss_driver_event_post(plat_priv,
  3040. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  3041. CNSS_EVENT_SYNC_UNKILLABLE,
  3042. pci_priv->driver_ops);
  3043. }
  3044. int cnss_wlan_register_driver(struct cnss_wlan_driver *driver_ops)
  3045. {
  3046. int ret = 0;
  3047. struct cnss_plat_data *plat_priv;
  3048. struct cnss_pci_data *pci_priv;
  3049. const struct pci_device_id *id_table = driver_ops->id_table;
  3050. unsigned int timeout;
  3051. if (!cnss_check_driver_loading_allowed()) {
  3052. cnss_pr_info("No cnss2 dtsi entry present");
  3053. return -ENODEV;
  3054. }
  3055. plat_priv = cnss_get_plat_priv_by_driver_ops(driver_ops);
  3056. if (!plat_priv) {
  3057. cnss_pr_buf("plat_priv is not ready for register driver\n");
  3058. return -EAGAIN;
  3059. }
  3060. pci_priv = plat_priv->bus_priv;
  3061. if (test_bit(CNSS_WLAN_HW_DISABLED, &plat_priv->driver_state)) {
  3062. while (id_table && id_table->device) {
  3063. if (plat_priv->device_id == id_table->device) {
  3064. if (plat_priv->device_id == KIWI_DEVICE_ID &&
  3065. driver_ops->chip_version != 2) {
  3066. cnss_pr_err("WLAN HW disabled. kiwi_v2 only supported\n");
  3067. return -ENODEV;
  3068. }
  3069. cnss_pr_info("WLAN register driver deferred for device ID: 0x%x due to HW disable\n",
  3070. id_table->device);
  3071. plat_priv->driver_ops = driver_ops;
  3072. return 0;
  3073. }
  3074. id_table++;
  3075. }
  3076. return -ENODEV;
  3077. }
  3078. if (!test_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state)) {
  3079. cnss_pr_info("pci probe not yet done for register driver\n");
  3080. return -EAGAIN;
  3081. }
  3082. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state)) {
  3083. cnss_pr_err("Driver has already registered\n");
  3084. return -EEXIST;
  3085. }
  3086. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  3087. cnss_pr_dbg("Reboot/Shutdown is in progress, ignore register driver\n");
  3088. return -EINVAL;
  3089. }
  3090. if (!id_table || !pci_dev_present(id_table)) {
  3091. /* id_table pointer will move from pci_dev_present(),
  3092. * so check again using local pointer.
  3093. */
  3094. id_table = driver_ops->id_table;
  3095. while (id_table && id_table->vendor) {
  3096. cnss_pr_info("Host driver is built for PCIe device ID 0x%x\n",
  3097. id_table->device);
  3098. id_table++;
  3099. }
  3100. cnss_pr_err("Enumerated PCIe device id is 0x%x, reject unsupported driver\n",
  3101. pci_priv->device_id);
  3102. return -ENODEV;
  3103. }
  3104. if (driver_ops->chip_version != CNSS_CHIP_VER_ANY &&
  3105. driver_ops->chip_version != plat_priv->device_version.major_version) {
  3106. cnss_pr_err("Driver built for chip ver 0x%x, enumerated ver 0x%x, reject unsupported driver\n",
  3107. driver_ops->chip_version,
  3108. plat_priv->device_version.major_version);
  3109. return -ENODEV;
  3110. }
  3111. cnss_get_driver_mode_update_fw_name(plat_priv);
  3112. set_bit(CNSS_DRIVER_REGISTER, &plat_priv->driver_state);
  3113. if (!plat_priv->cbc_enabled ||
  3114. test_bit(CNSS_COLD_BOOT_CAL_DONE, &plat_priv->driver_state))
  3115. goto register_driver;
  3116. pci_priv->driver_ops = driver_ops;
  3117. /* If Cold Boot Calibration is enabled, it is the 1st step in init
  3118. * sequence.CBC is done on file system_ready trigger. Qcacld will be
  3119. * loaded from vendor_modprobe.sh at early boot and must be deferred
  3120. * until CBC is complete
  3121. */
  3122. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_CALIBRATION);
  3123. INIT_DELAYED_WORK(&plat_priv->wlan_reg_driver_work,
  3124. cnss_wlan_reg_driver_work);
  3125. schedule_delayed_work(&plat_priv->wlan_reg_driver_work,
  3126. msecs_to_jiffies(timeout));
  3127. cnss_pr_info("WLAN register driver deferred for Calibration\n");
  3128. return 0;
  3129. register_driver:
  3130. reinit_completion(&plat_priv->power_up_complete);
  3131. ret = cnss_driver_event_post(plat_priv,
  3132. CNSS_DRIVER_EVENT_REGISTER_DRIVER,
  3133. CNSS_EVENT_SYNC_UNKILLABLE,
  3134. driver_ops);
  3135. return ret;
  3136. }
  3137. EXPORT_SYMBOL(cnss_wlan_register_driver);
  3138. void cnss_wlan_unregister_driver(struct cnss_wlan_driver *driver_ops)
  3139. {
  3140. struct cnss_plat_data *plat_priv;
  3141. int ret = 0;
  3142. unsigned int timeout;
  3143. plat_priv = cnss_get_plat_priv_by_driver_ops(driver_ops);
  3144. if (!plat_priv) {
  3145. cnss_pr_err("plat_priv is NULL\n");
  3146. return;
  3147. }
  3148. mutex_lock(&plat_priv->driver_ops_lock);
  3149. if (plat_priv->device_id == QCA6174_DEVICE_ID)
  3150. goto skip_wait_power_up;
  3151. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_WLAN_WATCHDOG);
  3152. ret = wait_for_completion_timeout(&plat_priv->power_up_complete,
  3153. msecs_to_jiffies(timeout));
  3154. if (!ret) {
  3155. cnss_pr_err("Timeout (%ums) waiting for driver power up to complete\n",
  3156. timeout);
  3157. CNSS_ASSERT(0);
  3158. }
  3159. skip_wait_power_up:
  3160. if (!test_bit(CNSS_DRIVER_RECOVERY, &plat_priv->driver_state) &&
  3161. !test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3162. goto skip_wait_recovery;
  3163. reinit_completion(&plat_priv->recovery_complete);
  3164. timeout = cnss_get_timeout(plat_priv, CNSS_TIMEOUT_RECOVERY);
  3165. ret = wait_for_completion_timeout(&plat_priv->recovery_complete,
  3166. msecs_to_jiffies(timeout));
  3167. if (!ret) {
  3168. cnss_pr_err("Timeout (%ums) waiting for recovery to complete\n",
  3169. timeout);
  3170. CNSS_ASSERT(0);
  3171. }
  3172. skip_wait_recovery:
  3173. cnss_driver_event_post(plat_priv,
  3174. CNSS_DRIVER_EVENT_UNREGISTER_DRIVER,
  3175. CNSS_EVENT_SYNC_UNKILLABLE, NULL);
  3176. mutex_unlock(&plat_priv->driver_ops_lock);
  3177. }
  3178. EXPORT_SYMBOL(cnss_wlan_unregister_driver);
  3179. int cnss_pci_register_driver_hdlr(struct cnss_pci_data *pci_priv,
  3180. void *data)
  3181. {
  3182. int ret = 0;
  3183. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3184. if (test_bit(CNSS_IN_REBOOT, &plat_priv->driver_state)) {
  3185. cnss_pr_dbg("Reboot or shutdown is in progress, ignore register driver\n");
  3186. return -EINVAL;
  3187. }
  3188. set_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  3189. pci_priv->driver_ops = data;
  3190. ret = cnss_pci_dev_powerup(pci_priv);
  3191. if (ret) {
  3192. clear_bit(CNSS_DRIVER_LOADING, &plat_priv->driver_state);
  3193. pci_priv->driver_ops = NULL;
  3194. } else {
  3195. set_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state);
  3196. }
  3197. return ret;
  3198. }
  3199. int cnss_pci_unregister_driver_hdlr(struct cnss_pci_data *pci_priv)
  3200. {
  3201. struct cnss_plat_data *plat_priv;
  3202. if (!pci_priv)
  3203. return -EINVAL;
  3204. plat_priv = pci_priv->plat_priv;
  3205. set_bit(CNSS_DRIVER_UNLOADING, &plat_priv->driver_state);
  3206. cnss_pci_dev_shutdown(pci_priv);
  3207. pci_priv->driver_ops = NULL;
  3208. clear_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state);
  3209. return 0;
  3210. }
  3211. static int cnss_pci_suspend_driver(struct cnss_pci_data *pci_priv)
  3212. {
  3213. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3214. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3215. int ret = 0;
  3216. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3217. pm_message_t state = { .event = PM_EVENT_SUSPEND };
  3218. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state) &&
  3219. driver_ops && driver_ops->suspend) {
  3220. ret = driver_ops->suspend(pci_dev, state);
  3221. if (ret) {
  3222. cnss_pr_err("Failed to suspend host driver, err = %d\n",
  3223. ret);
  3224. ret = -EAGAIN;
  3225. }
  3226. }
  3227. return ret;
  3228. }
  3229. static int cnss_pci_resume_driver(struct cnss_pci_data *pci_priv)
  3230. {
  3231. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3232. struct cnss_wlan_driver *driver_ops = pci_priv->driver_ops;
  3233. int ret = 0;
  3234. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3235. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state) &&
  3236. driver_ops && driver_ops->resume) {
  3237. ret = driver_ops->resume(pci_dev);
  3238. if (ret)
  3239. cnss_pr_err("Failed to resume host driver, err = %d\n",
  3240. ret);
  3241. }
  3242. return ret;
  3243. }
  3244. int cnss_pci_suspend_bus(struct cnss_pci_data *pci_priv)
  3245. {
  3246. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3247. int ret = 0;
  3248. if (pci_priv->pci_link_state == PCI_LINK_DOWN)
  3249. goto out;
  3250. if (cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_SUSPEND)) {
  3251. ret = -EAGAIN;
  3252. goto out;
  3253. }
  3254. if (pci_priv->drv_connected_last)
  3255. goto skip_disable_pci;
  3256. pci_clear_master(pci_dev);
  3257. cnss_set_pci_config_space(pci_priv, SAVE_PCI_CONFIG_SPACE);
  3258. pci_disable_device(pci_dev);
  3259. ret = pci_set_power_state(pci_dev, PCI_D3hot);
  3260. if (ret)
  3261. cnss_pr_err("Failed to set D3Hot, err = %d\n", ret);
  3262. skip_disable_pci:
  3263. if (cnss_set_pci_link(pci_priv, PCI_LINK_DOWN)) {
  3264. ret = -EAGAIN;
  3265. goto resume_mhi;
  3266. }
  3267. pci_priv->pci_link_state = PCI_LINK_DOWN;
  3268. return 0;
  3269. resume_mhi:
  3270. if (!pci_is_enabled(pci_dev))
  3271. if (pci_enable_device(pci_dev))
  3272. cnss_pr_err("Failed to enable PCI device\n");
  3273. if (pci_priv->saved_state)
  3274. cnss_set_pci_config_space(pci_priv, RESTORE_PCI_CONFIG_SPACE);
  3275. pci_set_master(pci_dev);
  3276. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3277. out:
  3278. return ret;
  3279. }
  3280. int cnss_pci_resume_bus(struct cnss_pci_data *pci_priv)
  3281. {
  3282. struct pci_dev *pci_dev = pci_priv->pci_dev;
  3283. int ret = 0;
  3284. if (pci_priv->pci_link_state == PCI_LINK_UP)
  3285. goto out;
  3286. if (cnss_set_pci_link(pci_priv, PCI_LINK_UP)) {
  3287. cnss_fatal_err("Failed to resume PCI link from suspend\n");
  3288. cnss_pci_link_down(&pci_dev->dev);
  3289. ret = -EAGAIN;
  3290. goto out;
  3291. }
  3292. pci_priv->pci_link_state = PCI_LINK_UP;
  3293. if (pci_priv->drv_connected_last)
  3294. goto skip_enable_pci;
  3295. ret = pci_enable_device(pci_dev);
  3296. if (ret) {
  3297. cnss_pr_err("Failed to enable PCI device, err = %d\n",
  3298. ret);
  3299. goto out;
  3300. }
  3301. if (pci_priv->saved_state)
  3302. cnss_set_pci_config_space(pci_priv,
  3303. RESTORE_PCI_CONFIG_SPACE);
  3304. pci_set_master(pci_dev);
  3305. skip_enable_pci:
  3306. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RESUME);
  3307. out:
  3308. return ret;
  3309. }
  3310. static int cnss_pci_suspend(struct device *dev)
  3311. {
  3312. int ret = 0;
  3313. struct pci_dev *pci_dev = to_pci_dev(dev);
  3314. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3315. struct cnss_plat_data *plat_priv;
  3316. if (!pci_priv)
  3317. goto out;
  3318. plat_priv = pci_priv->plat_priv;
  3319. if (!plat_priv)
  3320. goto out;
  3321. if (!cnss_is_device_powered_on(plat_priv))
  3322. goto out;
  3323. /* No mhi state bit set if only finish pcie enumeration,
  3324. * so test_bit is not applicable to check if it is INIT state.
  3325. */
  3326. if (pci_priv->mhi_state == CNSS_MHI_INIT) {
  3327. bool suspend = cnss_should_suspend_pwroff(pci_dev);
  3328. /* Do PCI link suspend and power off in the LPM case
  3329. * if chipset didn't do that after pcie enumeration.
  3330. */
  3331. if (!suspend) {
  3332. ret = cnss_suspend_pci_link(pci_priv);
  3333. if (ret)
  3334. cnss_pr_err("Failed to suspend PCI link, err = %d\n",
  3335. ret);
  3336. cnss_power_off_device(plat_priv);
  3337. goto out;
  3338. }
  3339. }
  3340. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3341. pci_priv->drv_supported) {
  3342. pci_priv->drv_connected_last =
  3343. cnss_pci_get_drv_connected(pci_priv);
  3344. if (!pci_priv->drv_connected_last) {
  3345. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3346. ret = -EAGAIN;
  3347. goto out;
  3348. }
  3349. }
  3350. set_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3351. ret = cnss_pci_suspend_driver(pci_priv);
  3352. if (ret)
  3353. goto clear_flag;
  3354. if (!pci_priv->disable_pc) {
  3355. mutex_lock(&pci_priv->bus_lock);
  3356. ret = cnss_pci_suspend_bus(pci_priv);
  3357. mutex_unlock(&pci_priv->bus_lock);
  3358. if (ret)
  3359. goto resume_driver;
  3360. }
  3361. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  3362. return 0;
  3363. resume_driver:
  3364. cnss_pci_resume_driver(pci_priv);
  3365. clear_flag:
  3366. pci_priv->drv_connected_last = 0;
  3367. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3368. out:
  3369. return ret;
  3370. }
  3371. static int cnss_pci_resume(struct device *dev)
  3372. {
  3373. int ret = 0;
  3374. struct pci_dev *pci_dev = to_pci_dev(dev);
  3375. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3376. struct cnss_plat_data *plat_priv;
  3377. if (!pci_priv)
  3378. goto out;
  3379. plat_priv = pci_priv->plat_priv;
  3380. if (!plat_priv)
  3381. goto out;
  3382. if (pci_priv->pci_link_down_ind)
  3383. goto out;
  3384. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3385. goto out;
  3386. if (!pci_priv->disable_pc) {
  3387. ret = cnss_pci_resume_bus(pci_priv);
  3388. if (ret)
  3389. goto out;
  3390. }
  3391. ret = cnss_pci_resume_driver(pci_priv);
  3392. pci_priv->drv_connected_last = 0;
  3393. clear_bit(CNSS_IN_SUSPEND_RESUME, &plat_priv->driver_state);
  3394. out:
  3395. return ret;
  3396. }
  3397. static int cnss_pci_suspend_noirq(struct device *dev)
  3398. {
  3399. int ret = 0;
  3400. struct pci_dev *pci_dev = to_pci_dev(dev);
  3401. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3402. struct cnss_wlan_driver *driver_ops;
  3403. struct cnss_plat_data *plat_priv;
  3404. if (!pci_priv)
  3405. goto out;
  3406. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3407. goto out;
  3408. driver_ops = pci_priv->driver_ops;
  3409. plat_priv = pci_priv->plat_priv;
  3410. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state) &&
  3411. driver_ops && driver_ops->suspend_noirq)
  3412. ret = driver_ops->suspend_noirq(pci_dev);
  3413. if (pci_priv->disable_pc && !pci_dev->state_saved &&
  3414. !pci_priv->plat_priv->use_pm_domain)
  3415. pci_save_state(pci_dev);
  3416. out:
  3417. return ret;
  3418. }
  3419. static int cnss_pci_resume_noirq(struct device *dev)
  3420. {
  3421. int ret = 0;
  3422. struct pci_dev *pci_dev = to_pci_dev(dev);
  3423. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3424. struct cnss_wlan_driver *driver_ops;
  3425. struct cnss_plat_data *plat_priv;
  3426. if (!pci_priv)
  3427. goto out;
  3428. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3429. goto out;
  3430. plat_priv = pci_priv->plat_priv;
  3431. driver_ops = pci_priv->driver_ops;
  3432. if (test_bit(CNSS_DRIVER_REGISTERED, &plat_priv->driver_state) &&
  3433. driver_ops && driver_ops->resume_noirq &&
  3434. !pci_priv->pci_link_down_ind)
  3435. ret = driver_ops->resume_noirq(pci_dev);
  3436. out:
  3437. return ret;
  3438. }
  3439. static int cnss_pci_runtime_suspend(struct device *dev)
  3440. {
  3441. int ret = 0;
  3442. struct pci_dev *pci_dev = to_pci_dev(dev);
  3443. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3444. struct cnss_plat_data *plat_priv;
  3445. struct cnss_wlan_driver *driver_ops;
  3446. if (!pci_priv)
  3447. return -EAGAIN;
  3448. plat_priv = pci_priv->plat_priv;
  3449. if (!plat_priv)
  3450. return -EAGAIN;
  3451. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3452. return -EAGAIN;
  3453. if (pci_priv->pci_link_down_ind) {
  3454. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3455. return -EAGAIN;
  3456. }
  3457. if (!test_bit(DISABLE_DRV, &plat_priv->ctrl_params.quirks) &&
  3458. pci_priv->drv_supported) {
  3459. pci_priv->drv_connected_last =
  3460. cnss_pci_get_drv_connected(pci_priv);
  3461. if (!pci_priv->drv_connected_last) {
  3462. cnss_pr_dbg("Firmware does not support non-DRV suspend, reject\n");
  3463. return -EAGAIN;
  3464. }
  3465. }
  3466. cnss_pr_vdbg("Runtime suspend start\n");
  3467. driver_ops = pci_priv->driver_ops;
  3468. if (driver_ops && driver_ops->runtime_ops &&
  3469. driver_ops->runtime_ops->runtime_suspend)
  3470. ret = driver_ops->runtime_ops->runtime_suspend(pci_dev);
  3471. else
  3472. ret = cnss_auto_suspend(dev);
  3473. if (ret)
  3474. pci_priv->drv_connected_last = 0;
  3475. cnss_pr_vdbg("Runtime suspend status: %d\n", ret);
  3476. return ret;
  3477. }
  3478. static int cnss_pci_runtime_resume(struct device *dev)
  3479. {
  3480. int ret = 0;
  3481. struct pci_dev *pci_dev = to_pci_dev(dev);
  3482. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3483. struct cnss_wlan_driver *driver_ops;
  3484. if (!pci_priv)
  3485. return -EAGAIN;
  3486. if (!cnss_is_device_powered_on(pci_priv->plat_priv))
  3487. return -EAGAIN;
  3488. if (pci_priv->pci_link_down_ind) {
  3489. cnss_pr_dbg("PCI link down recovery is in progress!\n");
  3490. return -EAGAIN;
  3491. }
  3492. cnss_pr_vdbg("Runtime resume start\n");
  3493. driver_ops = pci_priv->driver_ops;
  3494. if (driver_ops && driver_ops->runtime_ops &&
  3495. driver_ops->runtime_ops->runtime_resume)
  3496. ret = driver_ops->runtime_ops->runtime_resume(pci_dev);
  3497. else
  3498. ret = cnss_auto_resume(dev);
  3499. if (!ret)
  3500. pci_priv->drv_connected_last = 0;
  3501. cnss_pr_vdbg("Runtime resume status: %d\n", ret);
  3502. return ret;
  3503. }
  3504. static int cnss_pci_runtime_idle(struct device *dev)
  3505. {
  3506. cnss_pr_vdbg("Runtime idle\n");
  3507. pm_request_autosuspend(dev);
  3508. return -EBUSY;
  3509. }
  3510. int cnss_wlan_pm_control(struct device *dev, bool vote)
  3511. {
  3512. struct pci_dev *pci_dev = to_pci_dev(dev);
  3513. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3514. int ret = 0;
  3515. if (!pci_priv)
  3516. return -ENODEV;
  3517. ret = cnss_pci_disable_pc(pci_priv, vote);
  3518. if (ret)
  3519. return ret;
  3520. pci_priv->disable_pc = vote;
  3521. cnss_pr_dbg("%s PCIe power collapse\n", vote ? "disable" : "enable");
  3522. return 0;
  3523. }
  3524. EXPORT_SYMBOL(cnss_wlan_pm_control);
  3525. static void cnss_pci_pm_runtime_get_record(struct cnss_pci_data *pci_priv,
  3526. enum cnss_rtpm_id id)
  3527. {
  3528. if (id >= RTPM_ID_MAX)
  3529. return;
  3530. atomic_inc(&pci_priv->pm_stats.runtime_get);
  3531. atomic_inc(&pci_priv->pm_stats.runtime_get_id[id]);
  3532. pci_priv->pm_stats.runtime_get_timestamp_id[id] =
  3533. cnss_get_host_timestamp(pci_priv->plat_priv);
  3534. }
  3535. static void cnss_pci_pm_runtime_put_record(struct cnss_pci_data *pci_priv,
  3536. enum cnss_rtpm_id id)
  3537. {
  3538. if (id >= RTPM_ID_MAX)
  3539. return;
  3540. atomic_inc(&pci_priv->pm_stats.runtime_put);
  3541. atomic_inc(&pci_priv->pm_stats.runtime_put_id[id]);
  3542. pci_priv->pm_stats.runtime_put_timestamp_id[id] =
  3543. cnss_get_host_timestamp(pci_priv->plat_priv);
  3544. }
  3545. void cnss_pci_pm_runtime_show_usage_count(struct cnss_pci_data *pci_priv)
  3546. {
  3547. struct device *dev;
  3548. if (!pci_priv)
  3549. return;
  3550. dev = &pci_priv->pci_dev->dev;
  3551. cnss_pr_dbg("Runtime PM usage count: %d\n",
  3552. atomic_read(&dev->power.usage_count));
  3553. }
  3554. int cnss_pci_pm_request_resume(struct cnss_pci_data *pci_priv)
  3555. {
  3556. struct device *dev;
  3557. enum rpm_status status;
  3558. if (!pci_priv)
  3559. return -ENODEV;
  3560. dev = &pci_priv->pci_dev->dev;
  3561. status = dev->power.runtime_status;
  3562. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3563. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3564. (void *)_RET_IP_);
  3565. return pm_request_resume(dev);
  3566. }
  3567. int cnss_pci_pm_runtime_resume(struct cnss_pci_data *pci_priv)
  3568. {
  3569. struct device *dev;
  3570. enum rpm_status status;
  3571. if (!pci_priv)
  3572. return -ENODEV;
  3573. dev = &pci_priv->pci_dev->dev;
  3574. status = dev->power.runtime_status;
  3575. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3576. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3577. (void *)_RET_IP_);
  3578. return pm_runtime_resume(dev);
  3579. }
  3580. int cnss_pci_pm_runtime_get(struct cnss_pci_data *pci_priv,
  3581. enum cnss_rtpm_id id)
  3582. {
  3583. struct device *dev;
  3584. enum rpm_status status;
  3585. if (!pci_priv)
  3586. return -ENODEV;
  3587. dev = &pci_priv->pci_dev->dev;
  3588. status = dev->power.runtime_status;
  3589. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3590. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3591. (void *)_RET_IP_);
  3592. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3593. return pm_runtime_get(dev);
  3594. }
  3595. int cnss_pci_pm_runtime_get_sync(struct cnss_pci_data *pci_priv,
  3596. enum cnss_rtpm_id id)
  3597. {
  3598. struct device *dev;
  3599. enum rpm_status status;
  3600. if (!pci_priv)
  3601. return -ENODEV;
  3602. dev = &pci_priv->pci_dev->dev;
  3603. status = dev->power.runtime_status;
  3604. if (status == RPM_SUSPENDING || status == RPM_SUSPENDED)
  3605. cnss_pr_vdbg("Runtime PM resume is requested by %ps\n",
  3606. (void *)_RET_IP_);
  3607. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3608. return pm_runtime_get_sync(dev);
  3609. }
  3610. void cnss_pci_pm_runtime_get_noresume(struct cnss_pci_data *pci_priv,
  3611. enum cnss_rtpm_id id)
  3612. {
  3613. if (!pci_priv)
  3614. return;
  3615. cnss_pci_pm_runtime_get_record(pci_priv, id);
  3616. pm_runtime_get_noresume(&pci_priv->pci_dev->dev);
  3617. }
  3618. int cnss_pci_pm_runtime_put_autosuspend(struct cnss_pci_data *pci_priv,
  3619. enum cnss_rtpm_id id)
  3620. {
  3621. struct device *dev;
  3622. if (!pci_priv)
  3623. return -ENODEV;
  3624. dev = &pci_priv->pci_dev->dev;
  3625. if (atomic_read(&dev->power.usage_count) == 0) {
  3626. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3627. return -EINVAL;
  3628. }
  3629. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3630. return pm_runtime_put_autosuspend(&pci_priv->pci_dev->dev);
  3631. }
  3632. void cnss_pci_pm_runtime_put_noidle(struct cnss_pci_data *pci_priv,
  3633. enum cnss_rtpm_id id)
  3634. {
  3635. struct device *dev;
  3636. if (!pci_priv)
  3637. return;
  3638. dev = &pci_priv->pci_dev->dev;
  3639. if (atomic_read(&dev->power.usage_count) == 0) {
  3640. cnss_pr_dbg("Ignore excessive runtime PM put operation\n");
  3641. return;
  3642. }
  3643. cnss_pci_pm_runtime_put_record(pci_priv, id);
  3644. pm_runtime_put_noidle(&pci_priv->pci_dev->dev);
  3645. }
  3646. void cnss_pci_pm_runtime_mark_last_busy(struct cnss_pci_data *pci_priv)
  3647. {
  3648. if (!pci_priv)
  3649. return;
  3650. pm_runtime_mark_last_busy(&pci_priv->pci_dev->dev);
  3651. }
  3652. int cnss_auto_suspend(struct device *dev)
  3653. {
  3654. int ret = 0;
  3655. struct pci_dev *pci_dev = to_pci_dev(dev);
  3656. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3657. struct cnss_plat_data *plat_priv;
  3658. if (!pci_priv)
  3659. return -ENODEV;
  3660. plat_priv = pci_priv->plat_priv;
  3661. if (!plat_priv)
  3662. return -ENODEV;
  3663. mutex_lock(&pci_priv->bus_lock);
  3664. if (!pci_priv->qmi_send_usage_count) {
  3665. ret = cnss_pci_suspend_bus(pci_priv);
  3666. if (ret) {
  3667. mutex_unlock(&pci_priv->bus_lock);
  3668. return ret;
  3669. }
  3670. }
  3671. cnss_pci_set_auto_suspended(pci_priv, 1);
  3672. mutex_unlock(&pci_priv->bus_lock);
  3673. cnss_pci_set_monitor_wake_intr(pci_priv, true);
  3674. /* For suspend temporarily set bandwidth vote to NONE and dont save in
  3675. * current_bw_vote as in resume path we should vote for last used
  3676. * bandwidth vote. Also ignore error if bw voting is not setup.
  3677. */
  3678. cnss_setup_bus_bandwidth(plat_priv, CNSS_BUS_WIDTH_NONE, false);
  3679. return 0;
  3680. }
  3681. EXPORT_SYMBOL(cnss_auto_suspend);
  3682. int cnss_auto_resume(struct device *dev)
  3683. {
  3684. int ret = 0;
  3685. struct pci_dev *pci_dev = to_pci_dev(dev);
  3686. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3687. struct cnss_plat_data *plat_priv;
  3688. if (!pci_priv)
  3689. return -ENODEV;
  3690. plat_priv = pci_priv->plat_priv;
  3691. if (!plat_priv)
  3692. return -ENODEV;
  3693. mutex_lock(&pci_priv->bus_lock);
  3694. ret = cnss_pci_resume_bus(pci_priv);
  3695. if (ret) {
  3696. mutex_unlock(&pci_priv->bus_lock);
  3697. return ret;
  3698. }
  3699. cnss_pci_set_auto_suspended(pci_priv, 0);
  3700. mutex_unlock(&pci_priv->bus_lock);
  3701. cnss_request_bus_bandwidth(dev, plat_priv->icc.current_bw_vote);
  3702. return 0;
  3703. }
  3704. EXPORT_SYMBOL(cnss_auto_resume);
  3705. int cnss_pci_force_wake_request_sync(struct device *dev, int timeout_us)
  3706. {
  3707. struct pci_dev *pci_dev = to_pci_dev(dev);
  3708. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3709. struct cnss_plat_data *plat_priv;
  3710. struct mhi_controller *mhi_ctrl;
  3711. if (!pci_priv)
  3712. return -ENODEV;
  3713. switch (pci_priv->device_id) {
  3714. case QCA6390_DEVICE_ID:
  3715. case QCA6490_DEVICE_ID:
  3716. case KIWI_DEVICE_ID:
  3717. case MANGO_DEVICE_ID:
  3718. case PEACH_DEVICE_ID:
  3719. break;
  3720. default:
  3721. return 0;
  3722. }
  3723. mhi_ctrl = pci_priv->mhi_ctrl;
  3724. if (!mhi_ctrl)
  3725. return -EINVAL;
  3726. plat_priv = pci_priv->plat_priv;
  3727. if (!plat_priv)
  3728. return -ENODEV;
  3729. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3730. return -EAGAIN;
  3731. if (timeout_us) {
  3732. /* Busy wait for timeout_us */
  3733. return cnss_mhi_device_get_sync_atomic(pci_priv,
  3734. timeout_us, false);
  3735. } else {
  3736. /* Sleep wait for mhi_ctrl->timeout_ms */
  3737. return mhi_device_get_sync(mhi_ctrl->mhi_dev);
  3738. }
  3739. }
  3740. EXPORT_SYMBOL(cnss_pci_force_wake_request_sync);
  3741. int cnss_pci_force_wake_request(struct device *dev)
  3742. {
  3743. struct pci_dev *pci_dev = to_pci_dev(dev);
  3744. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3745. struct cnss_plat_data *plat_priv;
  3746. struct mhi_controller *mhi_ctrl;
  3747. if (!pci_priv)
  3748. return -ENODEV;
  3749. switch (pci_priv->device_id) {
  3750. case QCA6390_DEVICE_ID:
  3751. case QCA6490_DEVICE_ID:
  3752. case KIWI_DEVICE_ID:
  3753. case MANGO_DEVICE_ID:
  3754. case PEACH_DEVICE_ID:
  3755. break;
  3756. default:
  3757. return 0;
  3758. }
  3759. mhi_ctrl = pci_priv->mhi_ctrl;
  3760. if (!mhi_ctrl)
  3761. return -EINVAL;
  3762. plat_priv = pci_priv->plat_priv;
  3763. if (!plat_priv)
  3764. return -ENODEV;
  3765. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3766. return -EAGAIN;
  3767. mhi_device_get(mhi_ctrl->mhi_dev);
  3768. return 0;
  3769. }
  3770. EXPORT_SYMBOL(cnss_pci_force_wake_request);
  3771. int cnss_pci_is_device_awake(struct device *dev)
  3772. {
  3773. struct pci_dev *pci_dev = to_pci_dev(dev);
  3774. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3775. struct mhi_controller *mhi_ctrl;
  3776. if (!pci_priv)
  3777. return -ENODEV;
  3778. switch (pci_priv->device_id) {
  3779. case QCA6390_DEVICE_ID:
  3780. case QCA6490_DEVICE_ID:
  3781. case KIWI_DEVICE_ID:
  3782. case MANGO_DEVICE_ID:
  3783. case PEACH_DEVICE_ID:
  3784. break;
  3785. default:
  3786. return 0;
  3787. }
  3788. mhi_ctrl = pci_priv->mhi_ctrl;
  3789. if (!mhi_ctrl)
  3790. return -EINVAL;
  3791. return (mhi_ctrl->dev_state == MHI_STATE_M0);
  3792. }
  3793. EXPORT_SYMBOL(cnss_pci_is_device_awake);
  3794. int cnss_pci_force_wake_release(struct device *dev)
  3795. {
  3796. struct pci_dev *pci_dev = to_pci_dev(dev);
  3797. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  3798. struct cnss_plat_data *plat_priv;
  3799. struct mhi_controller *mhi_ctrl;
  3800. if (!pci_priv)
  3801. return -ENODEV;
  3802. switch (pci_priv->device_id) {
  3803. case QCA6390_DEVICE_ID:
  3804. case QCA6490_DEVICE_ID:
  3805. case KIWI_DEVICE_ID:
  3806. case MANGO_DEVICE_ID:
  3807. case PEACH_DEVICE_ID:
  3808. break;
  3809. default:
  3810. return 0;
  3811. }
  3812. mhi_ctrl = pci_priv->mhi_ctrl;
  3813. if (!mhi_ctrl)
  3814. return -EINVAL;
  3815. plat_priv = pci_priv->plat_priv;
  3816. if (!plat_priv)
  3817. return -ENODEV;
  3818. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state))
  3819. return -EAGAIN;
  3820. mhi_device_put(mhi_ctrl->mhi_dev);
  3821. return 0;
  3822. }
  3823. EXPORT_SYMBOL(cnss_pci_force_wake_release);
  3824. int cnss_pci_qmi_send_get(struct cnss_pci_data *pci_priv)
  3825. {
  3826. int ret = 0;
  3827. if (!pci_priv)
  3828. return -ENODEV;
  3829. mutex_lock(&pci_priv->bus_lock);
  3830. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3831. !pci_priv->qmi_send_usage_count)
  3832. ret = cnss_pci_resume_bus(pci_priv);
  3833. pci_priv->qmi_send_usage_count++;
  3834. cnss_pr_buf("Increased QMI send usage count to %d\n",
  3835. pci_priv->qmi_send_usage_count);
  3836. mutex_unlock(&pci_priv->bus_lock);
  3837. return ret;
  3838. }
  3839. int cnss_pci_qmi_send_put(struct cnss_pci_data *pci_priv)
  3840. {
  3841. int ret = 0;
  3842. if (!pci_priv)
  3843. return -ENODEV;
  3844. mutex_lock(&pci_priv->bus_lock);
  3845. if (pci_priv->qmi_send_usage_count)
  3846. pci_priv->qmi_send_usage_count--;
  3847. cnss_pr_buf("Decreased QMI send usage count to %d\n",
  3848. pci_priv->qmi_send_usage_count);
  3849. if (cnss_pci_get_auto_suspended(pci_priv) &&
  3850. !pci_priv->qmi_send_usage_count &&
  3851. !cnss_pcie_is_device_down(pci_priv))
  3852. ret = cnss_pci_suspend_bus(pci_priv);
  3853. mutex_unlock(&pci_priv->bus_lock);
  3854. return ret;
  3855. }
  3856. int cnss_send_buffer_to_afcmem(struct device *dev, const uint8_t *afcdb,
  3857. uint32_t len, uint8_t slotid)
  3858. {
  3859. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  3860. struct cnss_fw_mem *fw_mem;
  3861. void *mem = NULL;
  3862. int i, ret;
  3863. u32 *status;
  3864. if (!plat_priv)
  3865. return -EINVAL;
  3866. fw_mem = plat_priv->fw_mem;
  3867. if (slotid >= AFC_MAX_SLOT) {
  3868. cnss_pr_err("Invalid slot id %d\n", slotid);
  3869. ret = -EINVAL;
  3870. goto err;
  3871. }
  3872. if (len > AFC_SLOT_SIZE) {
  3873. cnss_pr_err("len %d greater than slot size", len);
  3874. ret = -EINVAL;
  3875. goto err;
  3876. }
  3877. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3878. if (fw_mem[i].type == QMI_WLFW_AFC_MEM_V01) {
  3879. mem = fw_mem[i].va;
  3880. status = mem + (slotid * AFC_SLOT_SIZE);
  3881. break;
  3882. }
  3883. }
  3884. if (!mem) {
  3885. cnss_pr_err("AFC mem is not available\n");
  3886. ret = -ENOMEM;
  3887. goto err;
  3888. }
  3889. memcpy(mem + (slotid * AFC_SLOT_SIZE), afcdb, len);
  3890. if (len < AFC_SLOT_SIZE)
  3891. memset(mem + (slotid * AFC_SLOT_SIZE) + len,
  3892. 0, AFC_SLOT_SIZE - len);
  3893. status[AFC_AUTH_STATUS_OFFSET] = cpu_to_le32(AFC_AUTH_SUCCESS);
  3894. return 0;
  3895. err:
  3896. return ret;
  3897. }
  3898. EXPORT_SYMBOL(cnss_send_buffer_to_afcmem);
  3899. int cnss_reset_afcmem(struct device *dev, uint8_t slotid)
  3900. {
  3901. struct cnss_plat_data *plat_priv = cnss_bus_dev_to_plat_priv(dev);
  3902. struct cnss_fw_mem *fw_mem;
  3903. void *mem = NULL;
  3904. int i, ret;
  3905. if (!plat_priv)
  3906. return -EINVAL;
  3907. fw_mem = plat_priv->fw_mem;
  3908. if (slotid >= AFC_MAX_SLOT) {
  3909. cnss_pr_err("Invalid slot id %d\n", slotid);
  3910. ret = -EINVAL;
  3911. goto err;
  3912. }
  3913. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3914. if (fw_mem[i].type == QMI_WLFW_AFC_MEM_V01) {
  3915. mem = fw_mem[i].va;
  3916. break;
  3917. }
  3918. }
  3919. if (!mem) {
  3920. cnss_pr_err("AFC mem is not available\n");
  3921. ret = -ENOMEM;
  3922. goto err;
  3923. }
  3924. memset(mem + (slotid * AFC_SLOT_SIZE), 0, AFC_SLOT_SIZE);
  3925. return 0;
  3926. err:
  3927. return ret;
  3928. }
  3929. EXPORT_SYMBOL(cnss_reset_afcmem);
  3930. int cnss_pci_alloc_fw_mem(struct cnss_pci_data *pci_priv)
  3931. {
  3932. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3933. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3934. struct device *dev = &pci_priv->pci_dev->dev;
  3935. int i;
  3936. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3937. if (!fw_mem[i].va && fw_mem[i].size) {
  3938. retry:
  3939. fw_mem[i].va =
  3940. dma_alloc_attrs(dev, fw_mem[i].size,
  3941. &fw_mem[i].pa, GFP_KERNEL,
  3942. fw_mem[i].attrs);
  3943. if (!fw_mem[i].va) {
  3944. if ((fw_mem[i].attrs &
  3945. DMA_ATTR_FORCE_CONTIGUOUS)) {
  3946. fw_mem[i].attrs &=
  3947. ~DMA_ATTR_FORCE_CONTIGUOUS;
  3948. cnss_pr_dbg("Fallback to non-contiguous memory for FW, Mem type: %u\n",
  3949. fw_mem[i].type);
  3950. goto retry;
  3951. }
  3952. cnss_pr_err("Failed to allocate memory for FW, size: 0x%zx, type: %u\n",
  3953. fw_mem[i].size, fw_mem[i].type);
  3954. CNSS_ASSERT(0);
  3955. return -ENOMEM;
  3956. }
  3957. }
  3958. }
  3959. return 0;
  3960. }
  3961. static void cnss_pci_free_fw_mem(struct cnss_pci_data *pci_priv)
  3962. {
  3963. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3964. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  3965. struct device *dev = &pci_priv->pci_dev->dev;
  3966. int i;
  3967. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  3968. if (fw_mem[i].va && fw_mem[i].size) {
  3969. cnss_pr_dbg("Freeing memory for FW, va: 0x%pK, pa: %pa, size: 0x%zx, type: %u\n",
  3970. fw_mem[i].va, &fw_mem[i].pa,
  3971. fw_mem[i].size, fw_mem[i].type);
  3972. dma_free_attrs(dev, fw_mem[i].size,
  3973. fw_mem[i].va, fw_mem[i].pa,
  3974. fw_mem[i].attrs);
  3975. fw_mem[i].va = NULL;
  3976. fw_mem[i].pa = 0;
  3977. fw_mem[i].size = 0;
  3978. fw_mem[i].type = 0;
  3979. }
  3980. }
  3981. plat_priv->fw_mem_seg_len = 0;
  3982. }
  3983. int cnss_pci_alloc_qdss_mem(struct cnss_pci_data *pci_priv)
  3984. {
  3985. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  3986. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  3987. int i, j;
  3988. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  3989. if (!qdss_mem[i].va && qdss_mem[i].size) {
  3990. qdss_mem[i].va =
  3991. dma_alloc_coherent(&pci_priv->pci_dev->dev,
  3992. qdss_mem[i].size,
  3993. &qdss_mem[i].pa,
  3994. GFP_KERNEL);
  3995. if (!qdss_mem[i].va) {
  3996. cnss_pr_err("Failed to allocate QDSS memory for FW, size: 0x%zx, type: %u, chuck-ID: %d\n",
  3997. qdss_mem[i].size,
  3998. qdss_mem[i].type, i);
  3999. break;
  4000. }
  4001. }
  4002. }
  4003. /* Best-effort allocation for QDSS trace */
  4004. if (i < plat_priv->qdss_mem_seg_len) {
  4005. for (j = i; j < plat_priv->qdss_mem_seg_len; j++) {
  4006. qdss_mem[j].type = 0;
  4007. qdss_mem[j].size = 0;
  4008. }
  4009. plat_priv->qdss_mem_seg_len = i;
  4010. }
  4011. return 0;
  4012. }
  4013. void cnss_pci_free_qdss_mem(struct cnss_pci_data *pci_priv)
  4014. {
  4015. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4016. struct cnss_fw_mem *qdss_mem = plat_priv->qdss_mem;
  4017. int i;
  4018. for (i = 0; i < plat_priv->qdss_mem_seg_len; i++) {
  4019. if (qdss_mem[i].va && qdss_mem[i].size) {
  4020. cnss_pr_dbg("Freeing memory for QDSS: pa: %pa, size: 0x%zx, type: %u\n",
  4021. &qdss_mem[i].pa, qdss_mem[i].size,
  4022. qdss_mem[i].type);
  4023. dma_free_coherent(&pci_priv->pci_dev->dev,
  4024. qdss_mem[i].size, qdss_mem[i].va,
  4025. qdss_mem[i].pa);
  4026. qdss_mem[i].va = NULL;
  4027. qdss_mem[i].pa = 0;
  4028. qdss_mem[i].size = 0;
  4029. qdss_mem[i].type = 0;
  4030. }
  4031. }
  4032. plat_priv->qdss_mem_seg_len = 0;
  4033. }
  4034. int cnss_pci_load_m3(struct cnss_pci_data *pci_priv)
  4035. {
  4036. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4037. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  4038. char filename[MAX_FIRMWARE_NAME_LEN];
  4039. char *phy_filename = DEFAULT_PHY_UCODE_FILE_NAME;
  4040. const struct firmware *fw_entry;
  4041. int ret = 0;
  4042. /* Use forward compatibility here since for any recent device
  4043. * it should use DEFAULT_PHY_UCODE_FILE_NAME.
  4044. */
  4045. switch (pci_priv->device_id) {
  4046. case QCA6174_DEVICE_ID:
  4047. cnss_pr_err("Invalid device ID (0x%x) to load phy image\n",
  4048. pci_priv->device_id);
  4049. return -EINVAL;
  4050. case QCA6290_DEVICE_ID:
  4051. case QCA6390_DEVICE_ID:
  4052. case QCA6490_DEVICE_ID:
  4053. phy_filename = DEFAULT_PHY_M3_FILE_NAME;
  4054. break;
  4055. case KIWI_DEVICE_ID:
  4056. case MANGO_DEVICE_ID:
  4057. case PEACH_DEVICE_ID:
  4058. switch (plat_priv->device_version.major_version) {
  4059. case FW_V2_NUMBER:
  4060. phy_filename = PHY_UCODE_V2_FILE_NAME;
  4061. break;
  4062. default:
  4063. break;
  4064. }
  4065. break;
  4066. default:
  4067. break;
  4068. }
  4069. if (!m3_mem->va && !m3_mem->size) {
  4070. cnss_pci_add_fw_prefix_name(pci_priv, filename,
  4071. phy_filename);
  4072. ret = firmware_request_nowarn(&fw_entry, filename,
  4073. &pci_priv->pci_dev->dev);
  4074. if (ret) {
  4075. cnss_pr_err("Failed to load M3 image: %s\n", filename);
  4076. return ret;
  4077. }
  4078. m3_mem->va = dma_alloc_coherent(&pci_priv->pci_dev->dev,
  4079. fw_entry->size, &m3_mem->pa,
  4080. GFP_KERNEL);
  4081. if (!m3_mem->va) {
  4082. cnss_pr_err("Failed to allocate memory for M3, size: 0x%zx\n",
  4083. fw_entry->size);
  4084. release_firmware(fw_entry);
  4085. return -ENOMEM;
  4086. }
  4087. memcpy(m3_mem->va, fw_entry->data, fw_entry->size);
  4088. m3_mem->size = fw_entry->size;
  4089. release_firmware(fw_entry);
  4090. }
  4091. return 0;
  4092. }
  4093. static void cnss_pci_free_m3_mem(struct cnss_pci_data *pci_priv)
  4094. {
  4095. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4096. struct cnss_fw_mem *m3_mem = &plat_priv->m3_mem;
  4097. if (m3_mem->va && m3_mem->size) {
  4098. cnss_pr_dbg("Freeing memory for M3, va: 0x%pK, pa: %pa, size: 0x%zx\n",
  4099. m3_mem->va, &m3_mem->pa, m3_mem->size);
  4100. dma_free_coherent(&pci_priv->pci_dev->dev, m3_mem->size,
  4101. m3_mem->va, m3_mem->pa);
  4102. }
  4103. m3_mem->va = NULL;
  4104. m3_mem->pa = 0;
  4105. m3_mem->size = 0;
  4106. }
  4107. #ifdef CONFIG_FREE_M3_BLOB_MEM
  4108. void cnss_pci_free_blob_mem(struct cnss_pci_data *pci_priv)
  4109. {
  4110. cnss_pci_free_m3_mem(pci_priv);
  4111. }
  4112. #else
  4113. void cnss_pci_free_blob_mem(struct cnss_pci_data *pci_priv)
  4114. {
  4115. }
  4116. #endif
  4117. void cnss_pci_fw_boot_timeout_hdlr(struct cnss_pci_data *pci_priv)
  4118. {
  4119. struct cnss_plat_data *plat_priv;
  4120. if (!pci_priv)
  4121. return;
  4122. cnss_fatal_err("Timeout waiting for FW ready indication\n");
  4123. plat_priv = pci_priv->plat_priv;
  4124. if (!plat_priv)
  4125. return;
  4126. if (test_bit(CNSS_IN_COLD_BOOT_CAL, &plat_priv->driver_state)) {
  4127. cnss_pr_dbg("Ignore FW ready timeout for calibration mode\n");
  4128. return;
  4129. }
  4130. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4131. CNSS_REASON_TIMEOUT);
  4132. }
  4133. static void cnss_pci_deinit_smmu(struct cnss_pci_data *pci_priv)
  4134. {
  4135. pci_priv->iommu_domain = NULL;
  4136. }
  4137. int cnss_pci_get_iova(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  4138. {
  4139. if (!pci_priv)
  4140. return -ENODEV;
  4141. if (!pci_priv->smmu_iova_len)
  4142. return -EINVAL;
  4143. *addr = pci_priv->smmu_iova_start;
  4144. *size = pci_priv->smmu_iova_len;
  4145. return 0;
  4146. }
  4147. int cnss_pci_get_iova_ipa(struct cnss_pci_data *pci_priv, u64 *addr, u64 *size)
  4148. {
  4149. if (!pci_priv)
  4150. return -ENODEV;
  4151. if (!pci_priv->smmu_iova_ipa_len)
  4152. return -EINVAL;
  4153. *addr = pci_priv->smmu_iova_ipa_start;
  4154. *size = pci_priv->smmu_iova_ipa_len;
  4155. return 0;
  4156. }
  4157. bool cnss_pci_is_smmu_s1_enabled(struct cnss_pci_data *pci_priv)
  4158. {
  4159. if (pci_priv)
  4160. return pci_priv->smmu_s1_enable;
  4161. return false;
  4162. }
  4163. struct iommu_domain *cnss_smmu_get_domain(struct device *dev)
  4164. {
  4165. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4166. if (!pci_priv)
  4167. return NULL;
  4168. return pci_priv->iommu_domain;
  4169. }
  4170. EXPORT_SYMBOL(cnss_smmu_get_domain);
  4171. int cnss_smmu_map(struct device *dev,
  4172. phys_addr_t paddr, uint32_t *iova_addr, size_t size)
  4173. {
  4174. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4175. struct cnss_plat_data *plat_priv;
  4176. unsigned long iova;
  4177. size_t len;
  4178. int ret = 0;
  4179. int flag = IOMMU_READ | IOMMU_WRITE;
  4180. struct pci_dev *root_port;
  4181. struct device_node *root_of_node;
  4182. bool dma_coherent = false;
  4183. if (!pci_priv)
  4184. return -ENODEV;
  4185. if (!iova_addr) {
  4186. cnss_pr_err("iova_addr is NULL, paddr %pa, size %zu\n",
  4187. &paddr, size);
  4188. return -EINVAL;
  4189. }
  4190. plat_priv = pci_priv->plat_priv;
  4191. len = roundup(size + paddr - rounddown(paddr, PAGE_SIZE), PAGE_SIZE);
  4192. iova = roundup(pci_priv->smmu_iova_ipa_current, PAGE_SIZE);
  4193. if (pci_priv->iommu_geometry &&
  4194. iova >= pci_priv->smmu_iova_ipa_start +
  4195. pci_priv->smmu_iova_ipa_len) {
  4196. cnss_pr_err("No IOVA space to map, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  4197. iova,
  4198. &pci_priv->smmu_iova_ipa_start,
  4199. pci_priv->smmu_iova_ipa_len);
  4200. return -ENOMEM;
  4201. }
  4202. if (!test_bit(DISABLE_IO_COHERENCY,
  4203. &plat_priv->ctrl_params.quirks)) {
  4204. root_port = pcie_find_root_port(pci_priv->pci_dev);
  4205. if (!root_port) {
  4206. cnss_pr_err("Root port is null, so dma_coherent is disabled\n");
  4207. } else {
  4208. root_of_node = root_port->dev.of_node;
  4209. if (root_of_node && root_of_node->parent) {
  4210. dma_coherent =
  4211. of_property_read_bool(root_of_node->parent,
  4212. "dma-coherent");
  4213. cnss_pr_dbg("dma-coherent is %s\n",
  4214. dma_coherent ? "enabled" : "disabled");
  4215. if (dma_coherent)
  4216. flag |= IOMMU_CACHE;
  4217. }
  4218. }
  4219. }
  4220. cnss_pr_dbg("IOMMU map: iova %lx, len %zu\n", iova, len);
  4221. ret = iommu_map(pci_priv->iommu_domain, iova,
  4222. rounddown(paddr, PAGE_SIZE), len, flag);
  4223. if (ret) {
  4224. cnss_pr_err("PA to IOVA mapping failed, ret %d\n", ret);
  4225. return ret;
  4226. }
  4227. pci_priv->smmu_iova_ipa_current = iova + len;
  4228. *iova_addr = (uint32_t)(iova + paddr - rounddown(paddr, PAGE_SIZE));
  4229. cnss_pr_dbg("IOMMU map: iova_addr %lx\n", *iova_addr);
  4230. return 0;
  4231. }
  4232. EXPORT_SYMBOL(cnss_smmu_map);
  4233. int cnss_smmu_unmap(struct device *dev, uint32_t iova_addr, size_t size)
  4234. {
  4235. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4236. unsigned long iova;
  4237. size_t unmapped;
  4238. size_t len;
  4239. if (!pci_priv)
  4240. return -ENODEV;
  4241. iova = rounddown(iova_addr, PAGE_SIZE);
  4242. len = roundup(size + iova_addr - iova, PAGE_SIZE);
  4243. if (iova >= pci_priv->smmu_iova_ipa_start +
  4244. pci_priv->smmu_iova_ipa_len) {
  4245. cnss_pr_err("Out of IOVA space to unmap, iova %lx, smmu_iova_ipa_start %pad, smmu_iova_ipa_len %zu\n",
  4246. iova,
  4247. &pci_priv->smmu_iova_ipa_start,
  4248. pci_priv->smmu_iova_ipa_len);
  4249. return -ENOMEM;
  4250. }
  4251. cnss_pr_dbg("IOMMU unmap: iova %lx, len %zu\n", iova, len);
  4252. unmapped = iommu_unmap(pci_priv->iommu_domain, iova, len);
  4253. if (unmapped != len) {
  4254. cnss_pr_err("IOMMU unmap failed, unmapped = %zu, requested = %zu\n",
  4255. unmapped, len);
  4256. return -EINVAL;
  4257. }
  4258. pci_priv->smmu_iova_ipa_current = iova;
  4259. return 0;
  4260. }
  4261. EXPORT_SYMBOL(cnss_smmu_unmap);
  4262. int cnss_get_soc_info(struct device *dev, struct cnss_soc_info *info)
  4263. {
  4264. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4265. struct cnss_plat_data *plat_priv;
  4266. if (!pci_priv)
  4267. return -ENODEV;
  4268. plat_priv = pci_priv->plat_priv;
  4269. if (!plat_priv)
  4270. return -ENODEV;
  4271. info->va = pci_priv->bar;
  4272. info->pa = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  4273. info->chip_id = plat_priv->chip_info.chip_id;
  4274. info->chip_family = plat_priv->chip_info.chip_family;
  4275. info->board_id = plat_priv->board_info.board_id;
  4276. info->soc_id = plat_priv->soc_info.soc_id;
  4277. info->fw_version = plat_priv->fw_version_info.fw_version;
  4278. strlcpy(info->fw_build_timestamp,
  4279. plat_priv->fw_version_info.fw_build_timestamp,
  4280. sizeof(info->fw_build_timestamp));
  4281. memcpy(&info->device_version, &plat_priv->device_version,
  4282. sizeof(info->device_version));
  4283. memcpy(&info->dev_mem_info, &plat_priv->dev_mem_info,
  4284. sizeof(info->dev_mem_info));
  4285. memcpy(&info->fw_build_id, &plat_priv->fw_build_id,
  4286. sizeof(info->fw_build_id));
  4287. return 0;
  4288. }
  4289. EXPORT_SYMBOL(cnss_get_soc_info);
  4290. int cnss_pci_get_user_msi_assignment(struct cnss_pci_data *pci_priv,
  4291. char *user_name,
  4292. int *num_vectors,
  4293. u32 *user_base_data,
  4294. u32 *base_vector)
  4295. {
  4296. return cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4297. user_name,
  4298. num_vectors,
  4299. user_base_data,
  4300. base_vector);
  4301. }
  4302. static int cnss_pci_enable_msi(struct cnss_pci_data *pci_priv)
  4303. {
  4304. int ret = 0;
  4305. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4306. int num_vectors;
  4307. struct cnss_msi_config *msi_config;
  4308. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4309. return 0;
  4310. if (cnss_pci_is_force_one_msi(pci_priv)) {
  4311. ret = cnss_pci_get_one_msi_assignment(pci_priv);
  4312. cnss_pr_dbg("force one msi\n");
  4313. } else {
  4314. ret = cnss_pci_get_msi_assignment(pci_priv);
  4315. }
  4316. if (ret) {
  4317. cnss_pr_err("Failed to get MSI assignment, err = %d\n", ret);
  4318. goto out;
  4319. }
  4320. msi_config = pci_priv->msi_config;
  4321. if (!msi_config) {
  4322. cnss_pr_err("msi_config is NULL!\n");
  4323. ret = -EINVAL;
  4324. goto out;
  4325. }
  4326. num_vectors = pci_alloc_irq_vectors(pci_dev,
  4327. msi_config->total_vectors,
  4328. msi_config->total_vectors,
  4329. PCI_IRQ_MSI | PCI_IRQ_MSIX);
  4330. if ((num_vectors != msi_config->total_vectors) &&
  4331. !cnss_pci_fallback_one_msi(pci_priv, &num_vectors)) {
  4332. cnss_pr_err("Failed to get enough MSI vectors (%d), available vectors = %d",
  4333. msi_config->total_vectors, num_vectors);
  4334. if (num_vectors >= 0)
  4335. ret = -EINVAL;
  4336. goto reset_msi_config;
  4337. }
  4338. if (cnss_pci_config_msi_addr(pci_priv)) {
  4339. ret = -EINVAL;
  4340. goto free_msi_vector;
  4341. }
  4342. if (cnss_pci_config_msi_data(pci_priv)) {
  4343. ret = -EINVAL;
  4344. goto free_msi_vector;
  4345. }
  4346. return 0;
  4347. free_msi_vector:
  4348. pci_free_irq_vectors(pci_priv->pci_dev);
  4349. reset_msi_config:
  4350. pci_priv->msi_config = NULL;
  4351. out:
  4352. return ret;
  4353. }
  4354. static void cnss_pci_disable_msi(struct cnss_pci_data *pci_priv)
  4355. {
  4356. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  4357. return;
  4358. pci_free_irq_vectors(pci_priv->pci_dev);
  4359. }
  4360. int cnss_get_user_msi_assignment(struct device *dev, char *user_name,
  4361. int *num_vectors, u32 *user_base_data,
  4362. u32 *base_vector)
  4363. {
  4364. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4365. struct cnss_msi_config *msi_config;
  4366. int idx;
  4367. if (!pci_priv)
  4368. return -ENODEV;
  4369. msi_config = pci_priv->msi_config;
  4370. if (!msi_config) {
  4371. cnss_pr_err("MSI is not supported.\n");
  4372. return -EINVAL;
  4373. }
  4374. for (idx = 0; idx < msi_config->total_users; idx++) {
  4375. if (strcmp(user_name, msi_config->users[idx].name) == 0) {
  4376. *num_vectors = msi_config->users[idx].num_vectors;
  4377. *user_base_data = msi_config->users[idx].base_vector
  4378. + pci_priv->msi_ep_base_data;
  4379. *base_vector = msi_config->users[idx].base_vector;
  4380. /*Add only single print for each user*/
  4381. if (print_optimize.msi_log_chk[idx]++)
  4382. goto skip_print;
  4383. cnss_pr_dbg("Assign MSI to user: %s, num_vectors: %d, user_base_data: %u, base_vector: %u\n",
  4384. user_name, *num_vectors, *user_base_data,
  4385. *base_vector);
  4386. skip_print:
  4387. return 0;
  4388. }
  4389. }
  4390. cnss_pr_err("Failed to find MSI assignment for %s!\n", user_name);
  4391. return -EINVAL;
  4392. }
  4393. EXPORT_SYMBOL(cnss_get_user_msi_assignment);
  4394. int cnss_get_msi_irq(struct device *dev, unsigned int vector)
  4395. {
  4396. struct pci_dev *pci_dev = to_pci_dev(dev);
  4397. int irq_num;
  4398. irq_num = pci_irq_vector(pci_dev, vector);
  4399. cnss_pr_dbg("Get IRQ number %d for vector index %d\n", irq_num, vector);
  4400. return irq_num;
  4401. }
  4402. EXPORT_SYMBOL(cnss_get_msi_irq);
  4403. bool cnss_is_one_msi(struct device *dev)
  4404. {
  4405. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(to_pci_dev(dev));
  4406. if (!pci_priv)
  4407. return false;
  4408. return cnss_pci_is_one_msi(pci_priv);
  4409. }
  4410. EXPORT_SYMBOL(cnss_is_one_msi);
  4411. void cnss_get_msi_address(struct device *dev, u32 *msi_addr_low,
  4412. u32 *msi_addr_high)
  4413. {
  4414. struct pci_dev *pci_dev = to_pci_dev(dev);
  4415. struct cnss_pci_data *pci_priv;
  4416. u16 control;
  4417. if (!pci_dev)
  4418. return;
  4419. pci_priv = cnss_get_pci_priv(pci_dev);
  4420. if (!pci_priv)
  4421. return;
  4422. if (pci_dev->msix_enabled) {
  4423. *msi_addr_low = pci_priv->msix_addr;
  4424. *msi_addr_high = 0;
  4425. if (!print_optimize.msi_addr_chk++)
  4426. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4427. *msi_addr_low, *msi_addr_high);
  4428. return;
  4429. }
  4430. pci_read_config_word(pci_dev, pci_dev->msi_cap + PCI_MSI_FLAGS,
  4431. &control);
  4432. pci_read_config_dword(pci_dev, pci_dev->msi_cap + PCI_MSI_ADDRESS_LO,
  4433. msi_addr_low);
  4434. /* Return MSI high address only when device supports 64-bit MSI */
  4435. if (control & PCI_MSI_FLAGS_64BIT)
  4436. pci_read_config_dword(pci_dev,
  4437. pci_dev->msi_cap + PCI_MSI_ADDRESS_HI,
  4438. msi_addr_high);
  4439. else
  4440. *msi_addr_high = 0;
  4441. /*Add only single print as the address is constant*/
  4442. if (!print_optimize.msi_addr_chk++)
  4443. cnss_pr_dbg("Get MSI low addr = 0x%x, high addr = 0x%x\n",
  4444. *msi_addr_low, *msi_addr_high);
  4445. }
  4446. EXPORT_SYMBOL(cnss_get_msi_address);
  4447. u32 cnss_pci_get_wake_msi(struct cnss_pci_data *pci_priv)
  4448. {
  4449. int ret, num_vectors;
  4450. u32 user_base_data, base_vector;
  4451. if (!pci_priv)
  4452. return -ENODEV;
  4453. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  4454. WAKE_MSI_NAME, &num_vectors,
  4455. &user_base_data, &base_vector);
  4456. if (ret) {
  4457. cnss_pr_err("WAKE MSI is not valid\n");
  4458. return 0;
  4459. }
  4460. return user_base_data;
  4461. }
  4462. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0))
  4463. static inline int cnss_pci_set_dma_mask(struct pci_dev *pci_dev, u64 mask)
  4464. {
  4465. return dma_set_mask(&pci_dev->dev, mask);
  4466. }
  4467. static inline int cnss_pci_set_coherent_dma_mask(struct pci_dev *pci_dev,
  4468. u64 mask)
  4469. {
  4470. return dma_set_coherent_mask(&pci_dev->dev, mask);
  4471. }
  4472. #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)) */
  4473. static inline int cnss_pci_set_dma_mask(struct pci_dev *pci_dev, u64 mask)
  4474. {
  4475. return pci_set_dma_mask(pci_dev, mask);
  4476. }
  4477. static inline int cnss_pci_set_coherent_dma_mask(struct pci_dev *pci_dev,
  4478. u64 mask)
  4479. {
  4480. return pci_set_consistent_dma_mask(pci_dev, mask);
  4481. }
  4482. #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 18, 0)) */
  4483. static int cnss_pci_enable_bus(struct cnss_pci_data *pci_priv)
  4484. {
  4485. int ret = 0;
  4486. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4487. u16 device_id;
  4488. pci_read_config_word(pci_dev, PCI_DEVICE_ID, &device_id);
  4489. if (device_id != pci_priv->pci_device_id->device) {
  4490. cnss_pr_err("PCI device ID mismatch, config ID: 0x%x, probe ID: 0x%x\n",
  4491. device_id, pci_priv->pci_device_id->device);
  4492. ret = -EIO;
  4493. goto out;
  4494. }
  4495. ret = pci_assign_resource(pci_dev, PCI_BAR_NUM);
  4496. if (ret) {
  4497. pr_err("Failed to assign PCI resource, err = %d\n", ret);
  4498. goto out;
  4499. }
  4500. ret = pci_enable_device(pci_dev);
  4501. if (ret) {
  4502. cnss_pr_err("Failed to enable PCI device, err = %d\n", ret);
  4503. goto out;
  4504. }
  4505. ret = pci_request_region(pci_dev, PCI_BAR_NUM, "cnss");
  4506. if (ret) {
  4507. cnss_pr_err("Failed to request PCI region, err = %d\n", ret);
  4508. goto disable_device;
  4509. }
  4510. switch (device_id) {
  4511. case QCA6174_DEVICE_ID:
  4512. case QCN7605_DEVICE_ID:
  4513. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4514. break;
  4515. case QCA6390_DEVICE_ID:
  4516. case QCA6490_DEVICE_ID:
  4517. case KIWI_DEVICE_ID:
  4518. case MANGO_DEVICE_ID:
  4519. case PEACH_DEVICE_ID:
  4520. pci_priv->dma_bit_mask = PCI_DMA_MASK_36_BIT;
  4521. break;
  4522. default:
  4523. pci_priv->dma_bit_mask = PCI_DMA_MASK_32_BIT;
  4524. break;
  4525. }
  4526. cnss_pr_dbg("Set PCI DMA MASK (0x%llx)\n", pci_priv->dma_bit_mask);
  4527. ret = cnss_pci_set_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4528. if (ret) {
  4529. cnss_pr_err("Failed to set PCI DMA mask, err = %d\n", ret);
  4530. goto release_region;
  4531. }
  4532. ret = cnss_pci_set_coherent_dma_mask(pci_dev, pci_priv->dma_bit_mask);
  4533. if (ret) {
  4534. cnss_pr_err("Failed to set PCI coherent DMA mask, err = %d\n",
  4535. ret);
  4536. goto release_region;
  4537. }
  4538. pci_priv->bar = pci_iomap(pci_dev, PCI_BAR_NUM, 0);
  4539. if (!pci_priv->bar) {
  4540. cnss_pr_err("Failed to do PCI IO map!\n");
  4541. ret = -EIO;
  4542. goto release_region;
  4543. }
  4544. /* Save default config space without BME enabled */
  4545. pci_save_state(pci_dev);
  4546. pci_priv->default_state = pci_store_saved_state(pci_dev);
  4547. pci_set_master(pci_dev);
  4548. return 0;
  4549. release_region:
  4550. pci_release_region(pci_dev, PCI_BAR_NUM);
  4551. disable_device:
  4552. pci_disable_device(pci_dev);
  4553. out:
  4554. return ret;
  4555. }
  4556. static void cnss_pci_disable_bus(struct cnss_pci_data *pci_priv)
  4557. {
  4558. struct pci_dev *pci_dev = pci_priv->pci_dev;
  4559. pci_clear_master(pci_dev);
  4560. pci_load_and_free_saved_state(pci_dev, &pci_priv->saved_state);
  4561. pci_load_and_free_saved_state(pci_dev, &pci_priv->default_state);
  4562. if (pci_priv->bar) {
  4563. pci_iounmap(pci_dev, pci_priv->bar);
  4564. pci_priv->bar = NULL;
  4565. }
  4566. pci_release_region(pci_dev, PCI_BAR_NUM);
  4567. if (pci_is_enabled(pci_dev))
  4568. pci_disable_device(pci_dev);
  4569. }
  4570. static void cnss_pci_dump_qdss_reg(struct cnss_pci_data *pci_priv)
  4571. {
  4572. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4573. int i, array_size = ARRAY_SIZE(qdss_csr) - 1;
  4574. gfp_t gfp = GFP_KERNEL;
  4575. u32 reg_offset;
  4576. if (in_interrupt() || irqs_disabled())
  4577. gfp = GFP_ATOMIC;
  4578. if (!plat_priv->qdss_reg) {
  4579. plat_priv->qdss_reg = devm_kzalloc(&pci_priv->pci_dev->dev,
  4580. sizeof(*plat_priv->qdss_reg)
  4581. * array_size, gfp);
  4582. if (!plat_priv->qdss_reg)
  4583. return;
  4584. }
  4585. cnss_pr_dbg("Start to dump qdss registers\n");
  4586. for (i = 0; qdss_csr[i].name; i++) {
  4587. reg_offset = QDSS_APB_DEC_CSR_BASE + qdss_csr[i].offset;
  4588. if (cnss_pci_reg_read(pci_priv, reg_offset,
  4589. &plat_priv->qdss_reg[i]))
  4590. return;
  4591. cnss_pr_dbg("%s[0x%x] = 0x%x\n", qdss_csr[i].name, reg_offset,
  4592. plat_priv->qdss_reg[i]);
  4593. }
  4594. }
  4595. static void cnss_pci_dump_ce_reg(struct cnss_pci_data *pci_priv,
  4596. enum cnss_ce_index ce)
  4597. {
  4598. int i;
  4599. u32 ce_base = ce * CE_REG_INTERVAL;
  4600. u32 reg_offset, src_ring_base, dst_ring_base, cmn_base, val;
  4601. switch (pci_priv->device_id) {
  4602. case QCA6390_DEVICE_ID:
  4603. src_ring_base = QCA6390_CE_SRC_RING_REG_BASE;
  4604. dst_ring_base = QCA6390_CE_DST_RING_REG_BASE;
  4605. cmn_base = QCA6390_CE_COMMON_REG_BASE;
  4606. break;
  4607. case QCA6490_DEVICE_ID:
  4608. src_ring_base = QCA6490_CE_SRC_RING_REG_BASE;
  4609. dst_ring_base = QCA6490_CE_DST_RING_REG_BASE;
  4610. cmn_base = QCA6490_CE_COMMON_REG_BASE;
  4611. break;
  4612. default:
  4613. return;
  4614. }
  4615. switch (ce) {
  4616. case CNSS_CE_09:
  4617. case CNSS_CE_10:
  4618. for (i = 0; ce_src[i].name; i++) {
  4619. reg_offset = src_ring_base + ce_base + ce_src[i].offset;
  4620. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4621. return;
  4622. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4623. ce, ce_src[i].name, reg_offset, val);
  4624. }
  4625. for (i = 0; ce_dst[i].name; i++) {
  4626. reg_offset = dst_ring_base + ce_base + ce_dst[i].offset;
  4627. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4628. return;
  4629. cnss_pr_dbg("CE_%02d_%s[0x%x] = 0x%x\n",
  4630. ce, ce_dst[i].name, reg_offset, val);
  4631. }
  4632. break;
  4633. case CNSS_CE_COMMON:
  4634. for (i = 0; ce_cmn[i].name; i++) {
  4635. reg_offset = cmn_base + ce_cmn[i].offset;
  4636. if (cnss_pci_reg_read(pci_priv, reg_offset, &val))
  4637. return;
  4638. cnss_pr_dbg("CE_COMMON_%s[0x%x] = 0x%x\n",
  4639. ce_cmn[i].name, reg_offset, val);
  4640. }
  4641. break;
  4642. default:
  4643. cnss_pr_err("Unsupported CE[%d] registers dump\n", ce);
  4644. }
  4645. }
  4646. static void cnss_pci_dump_debug_reg(struct cnss_pci_data *pci_priv)
  4647. {
  4648. if (cnss_pci_check_link_status(pci_priv))
  4649. return;
  4650. cnss_pr_dbg("Start to dump debug registers\n");
  4651. cnss_mhi_debug_reg_dump(pci_priv);
  4652. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4653. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_COMMON);
  4654. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_09);
  4655. cnss_pci_dump_ce_reg(pci_priv, CNSS_CE_10);
  4656. }
  4657. static int cnss_pci_assert_host_sol(struct cnss_pci_data *pci_priv)
  4658. {
  4659. if (cnss_get_host_sol_value(pci_priv->plat_priv))
  4660. return -EINVAL;
  4661. cnss_pr_dbg("Assert host SOL GPIO to retry RDDM, expecting link down\n");
  4662. cnss_set_host_sol_value(pci_priv->plat_priv, 1);
  4663. return 0;
  4664. }
  4665. static void cnss_pci_mhi_reg_dump(struct cnss_pci_data *pci_priv)
  4666. {
  4667. if (!cnss_pci_check_link_status(pci_priv))
  4668. cnss_mhi_debug_reg_dump(pci_priv);
  4669. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4670. cnss_pci_dump_misc_reg(pci_priv);
  4671. cnss_pci_dump_shadow_reg(pci_priv);
  4672. }
  4673. int cnss_pci_force_fw_assert_hdlr(struct cnss_pci_data *pci_priv)
  4674. {
  4675. int ret;
  4676. struct cnss_plat_data *plat_priv;
  4677. if (!pci_priv)
  4678. return -ENODEV;
  4679. plat_priv = pci_priv->plat_priv;
  4680. if (!plat_priv)
  4681. return -ENODEV;
  4682. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4683. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state))
  4684. return -EINVAL;
  4685. cnss_auto_resume(&pci_priv->pci_dev->dev);
  4686. if (!pci_priv->is_smmu_fault)
  4687. cnss_pci_mhi_reg_dump(pci_priv);
  4688. /* If link is still down here, directly trigger link down recovery */
  4689. ret = cnss_pci_check_link_status(pci_priv);
  4690. if (ret) {
  4691. cnss_pci_link_down(&pci_priv->pci_dev->dev);
  4692. return 0;
  4693. }
  4694. ret = cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_TRIGGER_RDDM);
  4695. if (ret) {
  4696. if (pci_priv->is_smmu_fault) {
  4697. cnss_pci_mhi_reg_dump(pci_priv);
  4698. pci_priv->is_smmu_fault = false;
  4699. }
  4700. if (!test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state) ||
  4701. test_bit(CNSS_MHI_POWERING_OFF, &pci_priv->mhi_state)) {
  4702. cnss_pr_dbg("MHI is not powered on, ignore RDDM failure\n");
  4703. return 0;
  4704. }
  4705. cnss_fatal_err("Failed to trigger RDDM, err = %d\n", ret);
  4706. if (!cnss_pci_assert_host_sol(pci_priv))
  4707. return 0;
  4708. cnss_pci_dump_debug_reg(pci_priv);
  4709. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  4710. CNSS_REASON_DEFAULT);
  4711. return ret;
  4712. }
  4713. if (pci_priv->is_smmu_fault) {
  4714. cnss_pci_mhi_reg_dump(pci_priv);
  4715. pci_priv->is_smmu_fault = false;
  4716. }
  4717. if (!test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state)) {
  4718. mod_timer(&pci_priv->dev_rddm_timer,
  4719. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  4720. }
  4721. return 0;
  4722. }
  4723. static void cnss_pci_add_dump_seg(struct cnss_pci_data *pci_priv,
  4724. struct cnss_dump_seg *dump_seg,
  4725. enum cnss_fw_dump_type type, int seg_no,
  4726. void *va, dma_addr_t dma, size_t size)
  4727. {
  4728. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4729. struct device *dev = &pci_priv->pci_dev->dev;
  4730. phys_addr_t pa;
  4731. dump_seg->address = dma;
  4732. dump_seg->v_address = va;
  4733. dump_seg->size = size;
  4734. dump_seg->type = type;
  4735. cnss_pr_dbg("Seg: %x, va: %pK, dma: %pa, size: 0x%zx\n",
  4736. seg_no, va, &dma, size);
  4737. if (cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS))
  4738. return;
  4739. cnss_minidump_add_region(plat_priv, type, seg_no, va, pa, size);
  4740. }
  4741. static void cnss_pci_remove_dump_seg(struct cnss_pci_data *pci_priv,
  4742. struct cnss_dump_seg *dump_seg,
  4743. enum cnss_fw_dump_type type, int seg_no,
  4744. void *va, dma_addr_t dma, size_t size)
  4745. {
  4746. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4747. struct device *dev = &pci_priv->pci_dev->dev;
  4748. phys_addr_t pa;
  4749. cnss_va_to_pa(dev, size, va, dma, &pa, DMA_ATTR_FORCE_CONTIGUOUS);
  4750. cnss_minidump_remove_region(plat_priv, type, seg_no, va, pa, size);
  4751. }
  4752. int cnss_pci_call_driver_uevent(struct cnss_pci_data *pci_priv,
  4753. enum cnss_driver_status status, void *data)
  4754. {
  4755. struct cnss_uevent_data uevent_data;
  4756. struct cnss_wlan_driver *driver_ops;
  4757. driver_ops = pci_priv->driver_ops;
  4758. if (!driver_ops || !driver_ops->update_event) {
  4759. cnss_pr_dbg("Hang event driver ops is NULL\n");
  4760. return -EINVAL;
  4761. }
  4762. cnss_pr_dbg("Calling driver uevent: %d\n", status);
  4763. uevent_data.status = status;
  4764. uevent_data.data = data;
  4765. return driver_ops->update_event(pci_priv->pci_dev, &uevent_data);
  4766. }
  4767. static void cnss_pci_send_hang_event(struct cnss_pci_data *pci_priv)
  4768. {
  4769. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4770. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4771. struct cnss_hang_event hang_event;
  4772. void *hang_data_va = NULL;
  4773. u64 offset = 0;
  4774. u16 length = 0;
  4775. int i = 0;
  4776. if (!fw_mem || !plat_priv->fw_mem_seg_len)
  4777. return;
  4778. memset(&hang_event, 0, sizeof(hang_event));
  4779. switch (pci_priv->device_id) {
  4780. case QCA6390_DEVICE_ID:
  4781. offset = HST_HANG_DATA_OFFSET;
  4782. length = HANG_DATA_LENGTH;
  4783. break;
  4784. case QCA6490_DEVICE_ID:
  4785. /* Fallback to hard-coded values if hang event params not
  4786. * present in QMI. Once all the firmware branches have the
  4787. * fix to send params over QMI, this can be removed.
  4788. */
  4789. if (plat_priv->hang_event_data_len) {
  4790. offset = plat_priv->hang_data_addr_offset;
  4791. length = plat_priv->hang_event_data_len;
  4792. } else {
  4793. offset = HSP_HANG_DATA_OFFSET;
  4794. length = HANG_DATA_LENGTH;
  4795. }
  4796. break;
  4797. case KIWI_DEVICE_ID:
  4798. case MANGO_DEVICE_ID:
  4799. case PEACH_DEVICE_ID:
  4800. offset = plat_priv->hang_data_addr_offset;
  4801. length = plat_priv->hang_event_data_len;
  4802. break;
  4803. default:
  4804. cnss_pr_err("Skip Hang Event Data as unsupported Device ID received: %d\n",
  4805. pci_priv->device_id);
  4806. return;
  4807. }
  4808. for (i = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4809. if (fw_mem[i].type == QMI_WLFW_MEM_TYPE_DDR_V01 &&
  4810. fw_mem[i].va) {
  4811. /* The offset must be < (fw_mem size- hangdata length) */
  4812. if (!(offset <= fw_mem[i].size - length))
  4813. goto exit;
  4814. hang_data_va = fw_mem[i].va + offset;
  4815. hang_event.hang_event_data = kmemdup(hang_data_va,
  4816. length,
  4817. GFP_ATOMIC);
  4818. if (!hang_event.hang_event_data) {
  4819. cnss_pr_dbg("Hang data memory alloc failed\n");
  4820. return;
  4821. }
  4822. hang_event.hang_event_data_len = length;
  4823. break;
  4824. }
  4825. }
  4826. cnss_pci_call_driver_uevent(pci_priv, CNSS_HANG_EVENT, &hang_event);
  4827. kfree(hang_event.hang_event_data);
  4828. hang_event.hang_event_data = NULL;
  4829. return;
  4830. exit:
  4831. cnss_pr_dbg("Invalid hang event params, offset:0x%x, length:0x%x\n",
  4832. plat_priv->hang_data_addr_offset,
  4833. plat_priv->hang_event_data_len);
  4834. }
  4835. #ifdef CONFIG_CNSS2_SSR_DRIVER_DUMP
  4836. void cnss_pci_collect_host_dump_info(struct cnss_pci_data *pci_priv)
  4837. {
  4838. struct cnss_ssr_driver_dump_entry ssr_entry[CNSS_HOST_DUMP_TYPE_MAX] = {0};
  4839. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4840. size_t num_entries_loaded = 0;
  4841. int x;
  4842. int ret = -1;
  4843. if (pci_priv->driver_ops &&
  4844. pci_priv->driver_ops->collect_driver_dump) {
  4845. ret = pci_priv->driver_ops->collect_driver_dump(pci_priv->pci_dev,
  4846. ssr_entry,
  4847. &num_entries_loaded);
  4848. }
  4849. if (!ret) {
  4850. for (x = 0; x < num_entries_loaded; x++) {
  4851. cnss_pr_info("Idx:%d, ptr: %p, name: %s, size: %d\n",
  4852. x, ssr_entry[x].buffer_pointer,
  4853. ssr_entry[x].region_name,
  4854. ssr_entry[x].buffer_size);
  4855. }
  4856. cnss_do_host_ramdump(plat_priv, ssr_entry, num_entries_loaded);
  4857. } else {
  4858. cnss_pr_info("Host SSR elf dump collection feature disabled\n");
  4859. }
  4860. }
  4861. #endif
  4862. void cnss_pci_collect_dump_info(struct cnss_pci_data *pci_priv, bool in_panic)
  4863. {
  4864. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4865. struct cnss_dump_data *dump_data =
  4866. &plat_priv->ramdump_info_v2.dump_data;
  4867. struct cnss_dump_seg *dump_seg =
  4868. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4869. struct image_info *fw_image, *rddm_image;
  4870. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4871. int ret, i, j;
  4872. if (test_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state) &&
  4873. !test_bit(CNSS_IN_PANIC, &plat_priv->driver_state))
  4874. cnss_pci_send_hang_event(pci_priv);
  4875. if (test_bit(CNSS_MHI_RDDM_DONE, &pci_priv->mhi_state)) {
  4876. cnss_pr_dbg("RAM dump is already collected, skip\n");
  4877. return;
  4878. }
  4879. if (!cnss_is_device_powered_on(plat_priv)) {
  4880. cnss_pr_dbg("Device is already powered off, skip\n");
  4881. return;
  4882. }
  4883. if (!in_panic) {
  4884. mutex_lock(&pci_priv->bus_lock);
  4885. ret = cnss_pci_check_link_status(pci_priv);
  4886. if (ret) {
  4887. if (ret != -EACCES) {
  4888. mutex_unlock(&pci_priv->bus_lock);
  4889. return;
  4890. }
  4891. if (cnss_pci_resume_bus(pci_priv)) {
  4892. mutex_unlock(&pci_priv->bus_lock);
  4893. return;
  4894. }
  4895. }
  4896. mutex_unlock(&pci_priv->bus_lock);
  4897. } else {
  4898. if (cnss_pci_check_link_status(pci_priv))
  4899. return;
  4900. /* Inside panic handler, reduce timeout for RDDM to avoid
  4901. * unnecessary hypervisor watchdog bite.
  4902. */
  4903. pci_priv->mhi_ctrl->timeout_ms /= 2;
  4904. }
  4905. cnss_mhi_debug_reg_dump(pci_priv);
  4906. cnss_pci_soc_scratch_reg_dump(pci_priv);
  4907. cnss_pci_dump_misc_reg(pci_priv);
  4908. cnss_rddm_trigger_debug(pci_priv);
  4909. ret = mhi_download_rddm_image(pci_priv->mhi_ctrl, in_panic);
  4910. if (ret) {
  4911. cnss_fatal_err("Failed to download RDDM image, err = %d\n",
  4912. ret);
  4913. if (!cnss_pci_assert_host_sol(pci_priv))
  4914. return;
  4915. cnss_rddm_trigger_check(pci_priv);
  4916. cnss_pci_dump_debug_reg(pci_priv);
  4917. return;
  4918. }
  4919. cnss_rddm_trigger_check(pci_priv);
  4920. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4921. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4922. dump_data->nentries = 0;
  4923. if (plat_priv->qdss_mem_seg_len)
  4924. cnss_pci_dump_qdss_reg(pci_priv);
  4925. cnss_mhi_dump_sfr(pci_priv);
  4926. if (!dump_seg) {
  4927. cnss_pr_warn("FW image dump collection not setup");
  4928. goto skip_dump;
  4929. }
  4930. cnss_pr_dbg("Collect FW image dump segment, nentries %d\n",
  4931. fw_image->entries);
  4932. for (i = 0; i < fw_image->entries; i++) {
  4933. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4934. fw_image->mhi_buf[i].buf,
  4935. fw_image->mhi_buf[i].dma_addr,
  4936. fw_image->mhi_buf[i].len);
  4937. dump_seg++;
  4938. }
  4939. dump_data->nentries += fw_image->entries;
  4940. cnss_pr_dbg("Collect RDDM image dump segment, nentries %d\n",
  4941. rddm_image->entries);
  4942. for (i = 0; i < rddm_image->entries; i++) {
  4943. cnss_pci_add_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4944. rddm_image->mhi_buf[i].buf,
  4945. rddm_image->mhi_buf[i].dma_addr,
  4946. rddm_image->mhi_buf[i].len);
  4947. dump_seg++;
  4948. }
  4949. dump_data->nentries += rddm_image->entries;
  4950. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  4951. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR) {
  4952. if (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS) {
  4953. cnss_pr_dbg("Collect remote heap dump segment\n");
  4954. cnss_pci_add_dump_seg(pci_priv, dump_seg,
  4955. CNSS_FW_REMOTE_HEAP, j,
  4956. fw_mem[i].va,
  4957. fw_mem[i].pa,
  4958. fw_mem[i].size);
  4959. dump_seg++;
  4960. dump_data->nentries++;
  4961. j++;
  4962. } else {
  4963. cnss_pr_dbg("Skip remote heap dumps as it is non-contiguous\n");
  4964. }
  4965. }
  4966. }
  4967. if (dump_data->nentries > 0)
  4968. plat_priv->ramdump_info_v2.dump_data_valid = true;
  4969. cnss_pci_set_mhi_state(pci_priv, CNSS_MHI_RDDM_DONE);
  4970. skip_dump:
  4971. complete(&plat_priv->rddm_complete);
  4972. }
  4973. void cnss_pci_clear_dump_info(struct cnss_pci_data *pci_priv)
  4974. {
  4975. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  4976. struct cnss_dump_seg *dump_seg =
  4977. plat_priv->ramdump_info_v2.dump_data_vaddr;
  4978. struct image_info *fw_image, *rddm_image;
  4979. struct cnss_fw_mem *fw_mem = plat_priv->fw_mem;
  4980. int i, j;
  4981. if (!dump_seg)
  4982. return;
  4983. fw_image = pci_priv->mhi_ctrl->fbc_image;
  4984. rddm_image = pci_priv->mhi_ctrl->rddm_image;
  4985. for (i = 0; i < fw_image->entries; i++) {
  4986. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_IMAGE, i,
  4987. fw_image->mhi_buf[i].buf,
  4988. fw_image->mhi_buf[i].dma_addr,
  4989. fw_image->mhi_buf[i].len);
  4990. dump_seg++;
  4991. }
  4992. for (i = 0; i < rddm_image->entries; i++) {
  4993. cnss_pci_remove_dump_seg(pci_priv, dump_seg, CNSS_FW_RDDM, i,
  4994. rddm_image->mhi_buf[i].buf,
  4995. rddm_image->mhi_buf[i].dma_addr,
  4996. rddm_image->mhi_buf[i].len);
  4997. dump_seg++;
  4998. }
  4999. for (i = 0, j = 0; i < plat_priv->fw_mem_seg_len; i++) {
  5000. if (fw_mem[i].type == CNSS_MEM_TYPE_DDR &&
  5001. (fw_mem[i].attrs & DMA_ATTR_FORCE_CONTIGUOUS)) {
  5002. cnss_pci_remove_dump_seg(pci_priv, dump_seg,
  5003. CNSS_FW_REMOTE_HEAP, j,
  5004. fw_mem[i].va, fw_mem[i].pa,
  5005. fw_mem[i].size);
  5006. dump_seg++;
  5007. j++;
  5008. }
  5009. }
  5010. plat_priv->ramdump_info_v2.dump_data.nentries = 0;
  5011. plat_priv->ramdump_info_v2.dump_data_valid = false;
  5012. }
  5013. void cnss_pci_device_crashed(struct cnss_pci_data *pci_priv)
  5014. {
  5015. struct cnss_plat_data *plat_priv;
  5016. if (!pci_priv) {
  5017. cnss_pr_err("pci_priv is NULL\n");
  5018. return;
  5019. }
  5020. plat_priv = pci_priv->plat_priv;
  5021. if (!plat_priv) {
  5022. cnss_pr_err("plat_priv is NULL\n");
  5023. return;
  5024. }
  5025. if (plat_priv->recovery_enabled)
  5026. cnss_pci_collect_host_dump_info(pci_priv);
  5027. cnss_device_crashed(&pci_priv->pci_dev->dev);
  5028. }
  5029. static int cnss_mhi_pm_runtime_get(struct mhi_controller *mhi_ctrl)
  5030. {
  5031. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5032. return cnss_pci_pm_runtime_get(pci_priv, RTPM_ID_MHI);
  5033. }
  5034. static void cnss_mhi_pm_runtime_put_noidle(struct mhi_controller *mhi_ctrl)
  5035. {
  5036. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5037. cnss_pci_pm_runtime_put_noidle(pci_priv, RTPM_ID_MHI);
  5038. }
  5039. void cnss_pci_add_fw_prefix_name(struct cnss_pci_data *pci_priv,
  5040. char *prefix_name, char *name)
  5041. {
  5042. struct cnss_plat_data *plat_priv;
  5043. if (!pci_priv)
  5044. return;
  5045. plat_priv = pci_priv->plat_priv;
  5046. if (!plat_priv->use_fw_path_with_prefix) {
  5047. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  5048. return;
  5049. }
  5050. switch (pci_priv->device_id) {
  5051. case QCN7605_DEVICE_ID:
  5052. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5053. QCN7605_PATH_PREFIX "%s", name);
  5054. break;
  5055. case QCA6390_DEVICE_ID:
  5056. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5057. QCA6390_PATH_PREFIX "%s", name);
  5058. break;
  5059. case QCA6490_DEVICE_ID:
  5060. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5061. QCA6490_PATH_PREFIX "%s", name);
  5062. break;
  5063. case KIWI_DEVICE_ID:
  5064. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5065. KIWI_PATH_PREFIX "%s", name);
  5066. break;
  5067. case MANGO_DEVICE_ID:
  5068. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5069. MANGO_PATH_PREFIX "%s", name);
  5070. break;
  5071. case PEACH_DEVICE_ID:
  5072. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN,
  5073. PEACH_PATH_PREFIX "%s", name);
  5074. break;
  5075. default:
  5076. scnprintf(prefix_name, MAX_FIRMWARE_NAME_LEN, "%s", name);
  5077. break;
  5078. }
  5079. cnss_pr_dbg("FW name added with prefix: %s\n", prefix_name);
  5080. }
  5081. static int cnss_pci_update_fw_name(struct cnss_pci_data *pci_priv)
  5082. {
  5083. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5084. switch (pci_priv->device_id) {
  5085. case QCA6390_DEVICE_ID:
  5086. if (plat_priv->device_version.major_version < FW_V2_NUMBER) {
  5087. cnss_pr_dbg("Device ID:version (0x%lx:%d) is not supported\n",
  5088. pci_priv->device_id,
  5089. plat_priv->device_version.major_version);
  5090. return -EINVAL;
  5091. }
  5092. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  5093. FW_V2_FILE_NAME);
  5094. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  5095. FW_V2_FILE_NAME);
  5096. break;
  5097. case QCA6490_DEVICE_ID:
  5098. switch (plat_priv->device_version.major_version) {
  5099. case FW_V2_NUMBER:
  5100. cnss_pci_add_fw_prefix_name(pci_priv,
  5101. plat_priv->firmware_name,
  5102. FW_V2_FILE_NAME);
  5103. snprintf(plat_priv->fw_fallback_name,
  5104. MAX_FIRMWARE_NAME_LEN,
  5105. FW_V2_FILE_NAME);
  5106. break;
  5107. default:
  5108. cnss_pci_add_fw_prefix_name(pci_priv,
  5109. plat_priv->firmware_name,
  5110. DEFAULT_FW_FILE_NAME);
  5111. snprintf(plat_priv->fw_fallback_name,
  5112. MAX_FIRMWARE_NAME_LEN,
  5113. DEFAULT_FW_FILE_NAME);
  5114. break;
  5115. }
  5116. break;
  5117. case KIWI_DEVICE_ID:
  5118. case MANGO_DEVICE_ID:
  5119. case PEACH_DEVICE_ID:
  5120. switch (plat_priv->device_version.major_version) {
  5121. case FW_V2_NUMBER:
  5122. /*
  5123. * kiwiv2 using seprate fw binary for MM and FTM mode,
  5124. * platform driver loads corresponding binary according
  5125. * to current mode indicated by wlan driver. Otherwise
  5126. * use default binary.
  5127. * Mission mode using same binary name as before,
  5128. * if seprate binary is not there, fall back to default.
  5129. */
  5130. if (plat_priv->driver_mode == CNSS_MISSION) {
  5131. cnss_pci_add_fw_prefix_name(pci_priv,
  5132. plat_priv->firmware_name,
  5133. FW_V2_FILE_NAME);
  5134. cnss_pci_add_fw_prefix_name(pci_priv,
  5135. plat_priv->fw_fallback_name,
  5136. FW_V2_FILE_NAME);
  5137. } else if (plat_priv->driver_mode == CNSS_FTM) {
  5138. cnss_pci_add_fw_prefix_name(pci_priv,
  5139. plat_priv->firmware_name,
  5140. FW_V2_FTM_FILE_NAME);
  5141. cnss_pci_add_fw_prefix_name(pci_priv,
  5142. plat_priv->fw_fallback_name,
  5143. FW_V2_FILE_NAME);
  5144. } else {
  5145. /*
  5146. * Since during cold boot calibration phase,
  5147. * wlan driver has not registered, so default
  5148. * fw binary will be used.
  5149. */
  5150. cnss_pci_add_fw_prefix_name(pci_priv,
  5151. plat_priv->firmware_name,
  5152. FW_V2_FILE_NAME);
  5153. snprintf(plat_priv->fw_fallback_name,
  5154. MAX_FIRMWARE_NAME_LEN,
  5155. FW_V2_FILE_NAME);
  5156. }
  5157. break;
  5158. default:
  5159. cnss_pci_add_fw_prefix_name(pci_priv,
  5160. plat_priv->firmware_name,
  5161. DEFAULT_FW_FILE_NAME);
  5162. snprintf(plat_priv->fw_fallback_name,
  5163. MAX_FIRMWARE_NAME_LEN,
  5164. DEFAULT_FW_FILE_NAME);
  5165. break;
  5166. }
  5167. break;
  5168. default:
  5169. cnss_pci_add_fw_prefix_name(pci_priv, plat_priv->firmware_name,
  5170. DEFAULT_FW_FILE_NAME);
  5171. snprintf(plat_priv->fw_fallback_name, MAX_FIRMWARE_NAME_LEN,
  5172. DEFAULT_FW_FILE_NAME);
  5173. break;
  5174. }
  5175. cnss_pr_dbg("FW name is %s, FW fallback name is %s\n",
  5176. plat_priv->firmware_name, plat_priv->fw_fallback_name);
  5177. return 0;
  5178. }
  5179. static char *cnss_mhi_notify_status_to_str(enum mhi_callback status)
  5180. {
  5181. switch (status) {
  5182. case MHI_CB_IDLE:
  5183. return "IDLE";
  5184. case MHI_CB_EE_RDDM:
  5185. return "RDDM";
  5186. case MHI_CB_SYS_ERROR:
  5187. return "SYS_ERROR";
  5188. case MHI_CB_FATAL_ERROR:
  5189. return "FATAL_ERROR";
  5190. case MHI_CB_EE_MISSION_MODE:
  5191. return "MISSION_MODE";
  5192. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  5193. case MHI_CB_FALLBACK_IMG:
  5194. return "FW_FALLBACK";
  5195. #endif
  5196. default:
  5197. return "UNKNOWN";
  5198. }
  5199. };
  5200. static void cnss_dev_rddm_timeout_hdlr(struct timer_list *t)
  5201. {
  5202. struct cnss_pci_data *pci_priv =
  5203. from_timer(pci_priv, t, dev_rddm_timer);
  5204. enum mhi_ee_type mhi_ee;
  5205. if (!pci_priv)
  5206. return;
  5207. cnss_fatal_err("Timeout waiting for RDDM notification\n");
  5208. if (!cnss_pci_assert_host_sol(pci_priv))
  5209. return;
  5210. mhi_ee = mhi_get_exec_env(pci_priv->mhi_ctrl);
  5211. if (mhi_ee == MHI_EE_PBL)
  5212. cnss_pr_err("Unable to collect ramdumps due to abrupt reset\n");
  5213. if (mhi_ee == MHI_EE_RDDM) {
  5214. cnss_pr_info("Device MHI EE is RDDM, try to collect dump\n");
  5215. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  5216. CNSS_REASON_RDDM);
  5217. } else {
  5218. cnss_mhi_debug_reg_dump(pci_priv);
  5219. cnss_pci_soc_scratch_reg_dump(pci_priv);
  5220. cnss_schedule_recovery(&pci_priv->pci_dev->dev,
  5221. CNSS_REASON_TIMEOUT);
  5222. }
  5223. }
  5224. static void cnss_boot_debug_timeout_hdlr(struct timer_list *t)
  5225. {
  5226. struct cnss_pci_data *pci_priv =
  5227. from_timer(pci_priv, t, boot_debug_timer);
  5228. if (!pci_priv)
  5229. return;
  5230. if (cnss_pci_check_link_status(pci_priv))
  5231. return;
  5232. if (cnss_pci_is_device_down(&pci_priv->pci_dev->dev))
  5233. return;
  5234. if (test_bit(CNSS_MHI_POWER_ON, &pci_priv->mhi_state))
  5235. return;
  5236. if (cnss_mhi_scan_rddm_cookie(pci_priv, DEVICE_RDDM_COOKIE))
  5237. return;
  5238. cnss_pr_dbg("Dump MHI/PBL/SBL debug data every %ds during MHI power on\n",
  5239. BOOT_DEBUG_TIMEOUT_MS / 1000);
  5240. cnss_mhi_debug_reg_dump(pci_priv);
  5241. cnss_pci_soc_scratch_reg_dump(pci_priv);
  5242. cnss_pci_dump_bl_sram_mem(pci_priv);
  5243. mod_timer(&pci_priv->boot_debug_timer,
  5244. jiffies + msecs_to_jiffies(BOOT_DEBUG_TIMEOUT_MS));
  5245. }
  5246. static int cnss_pci_handle_mhi_sys_err(struct cnss_pci_data *pci_priv)
  5247. {
  5248. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5249. cnss_ignore_qmi_failure(true);
  5250. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  5251. del_timer(&plat_priv->fw_boot_timer);
  5252. mod_timer(&pci_priv->dev_rddm_timer,
  5253. jiffies + msecs_to_jiffies(DEV_RDDM_TIMEOUT));
  5254. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  5255. return 0;
  5256. }
  5257. int cnss_pci_handle_dev_sol_irq(struct cnss_pci_data *pci_priv)
  5258. {
  5259. return cnss_pci_handle_mhi_sys_err(pci_priv);
  5260. }
  5261. static void cnss_mhi_notify_status(struct mhi_controller *mhi_ctrl,
  5262. enum mhi_callback reason)
  5263. {
  5264. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5265. struct cnss_plat_data *plat_priv;
  5266. enum cnss_recovery_reason cnss_reason;
  5267. if (!pci_priv) {
  5268. cnss_pr_err("pci_priv is NULL");
  5269. return;
  5270. }
  5271. plat_priv = pci_priv->plat_priv;
  5272. if (reason != MHI_CB_IDLE)
  5273. cnss_pr_dbg("MHI status cb is called with reason %s(%d)\n",
  5274. cnss_mhi_notify_status_to_str(reason), reason);
  5275. switch (reason) {
  5276. case MHI_CB_IDLE:
  5277. case MHI_CB_EE_MISSION_MODE:
  5278. return;
  5279. case MHI_CB_FATAL_ERROR:
  5280. cnss_ignore_qmi_failure(true);
  5281. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  5282. del_timer(&plat_priv->fw_boot_timer);
  5283. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  5284. cnss_reason = CNSS_REASON_DEFAULT;
  5285. break;
  5286. case MHI_CB_SYS_ERROR:
  5287. cnss_pci_handle_mhi_sys_err(pci_priv);
  5288. return;
  5289. case MHI_CB_EE_RDDM:
  5290. cnss_ignore_qmi_failure(true);
  5291. set_bit(CNSS_DEV_ERR_NOTIFY, &plat_priv->driver_state);
  5292. del_timer(&plat_priv->fw_boot_timer);
  5293. del_timer(&pci_priv->dev_rddm_timer);
  5294. cnss_pci_update_status(pci_priv, CNSS_FW_DOWN);
  5295. cnss_reason = CNSS_REASON_RDDM;
  5296. break;
  5297. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  5298. case MHI_CB_FALLBACK_IMG:
  5299. /* for kiwi_v2 binary fallback is used, skip path fallback here */
  5300. if (!(pci_priv->device_id == KIWI_DEVICE_ID &&
  5301. plat_priv->device_version.major_version == FW_V2_NUMBER)) {
  5302. plat_priv->use_fw_path_with_prefix = false;
  5303. cnss_pci_update_fw_name(pci_priv);
  5304. }
  5305. return;
  5306. #endif
  5307. default:
  5308. cnss_pr_err("Unsupported MHI status cb reason: %d\n", reason);
  5309. return;
  5310. }
  5311. cnss_schedule_recovery(&pci_priv->pci_dev->dev, cnss_reason);
  5312. }
  5313. static int cnss_pci_get_mhi_msi(struct cnss_pci_data *pci_priv)
  5314. {
  5315. int ret, num_vectors, i;
  5316. u32 user_base_data, base_vector;
  5317. int *irq;
  5318. unsigned int msi_data;
  5319. bool is_one_msi = false;
  5320. ret = cnss_get_user_msi_assignment(&pci_priv->pci_dev->dev,
  5321. MHI_MSI_NAME, &num_vectors,
  5322. &user_base_data, &base_vector);
  5323. if (ret)
  5324. return ret;
  5325. if (cnss_pci_is_one_msi(pci_priv)) {
  5326. is_one_msi = true;
  5327. num_vectors = cnss_pci_get_one_msi_mhi_irq_array_size(pci_priv);
  5328. }
  5329. cnss_pr_dbg("Number of assigned MSI for MHI is %d, base vector is %d\n",
  5330. num_vectors, base_vector);
  5331. irq = kcalloc(num_vectors, sizeof(int), GFP_KERNEL);
  5332. if (!irq)
  5333. return -ENOMEM;
  5334. for (i = 0; i < num_vectors; i++) {
  5335. msi_data = base_vector;
  5336. if (!is_one_msi)
  5337. msi_data += i;
  5338. irq[i] = cnss_get_msi_irq(&pci_priv->pci_dev->dev, msi_data);
  5339. }
  5340. pci_priv->mhi_ctrl->irq = irq;
  5341. pci_priv->mhi_ctrl->nr_irqs = num_vectors;
  5342. return 0;
  5343. }
  5344. static int cnss_mhi_bw_scale(struct mhi_controller *mhi_ctrl,
  5345. struct mhi_link_info *link_info)
  5346. {
  5347. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5348. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5349. int ret = 0;
  5350. cnss_pr_dbg("Setting link speed:0x%x, width:0x%x\n",
  5351. link_info->target_link_speed,
  5352. link_info->target_link_width);
  5353. /* It has to set target link speed here before setting link bandwidth
  5354. * when device requests link speed change. This can avoid setting link
  5355. * bandwidth getting rejected if requested link speed is higher than
  5356. * current one.
  5357. */
  5358. ret = cnss_pci_set_max_link_speed(pci_priv, plat_priv->rc_num,
  5359. link_info->target_link_speed);
  5360. if (ret)
  5361. cnss_pr_err("Failed to set target link speed to 0x%x, err = %d\n",
  5362. link_info->target_link_speed, ret);
  5363. ret = cnss_pci_set_link_bandwidth(pci_priv,
  5364. link_info->target_link_speed,
  5365. link_info->target_link_width);
  5366. if (ret) {
  5367. cnss_pr_err("Failed to set link bandwidth, err = %d\n", ret);
  5368. return ret;
  5369. }
  5370. pci_priv->def_link_speed = link_info->target_link_speed;
  5371. pci_priv->def_link_width = link_info->target_link_width;
  5372. return 0;
  5373. }
  5374. static int cnss_mhi_read_reg(struct mhi_controller *mhi_ctrl,
  5375. void __iomem *addr, u32 *out)
  5376. {
  5377. struct cnss_pci_data *pci_priv = dev_get_drvdata(mhi_ctrl->cntrl_dev);
  5378. u32 tmp = readl_relaxed(addr);
  5379. /* Unexpected value, query the link status */
  5380. if (PCI_INVALID_READ(tmp) &&
  5381. cnss_pci_check_link_status(pci_priv))
  5382. return -EIO;
  5383. *out = tmp;
  5384. return 0;
  5385. }
  5386. static void cnss_mhi_write_reg(struct mhi_controller *mhi_ctrl,
  5387. void __iomem *addr, u32 val)
  5388. {
  5389. writel_relaxed(val, addr);
  5390. }
  5391. static int cnss_get_mhi_soc_info(struct cnss_plat_data *plat_priv,
  5392. struct mhi_controller *mhi_ctrl)
  5393. {
  5394. int ret = 0;
  5395. ret = mhi_get_soc_info(mhi_ctrl);
  5396. if (ret)
  5397. goto exit;
  5398. plat_priv->device_version.family_number = mhi_ctrl->family_number;
  5399. plat_priv->device_version.device_number = mhi_ctrl->device_number;
  5400. plat_priv->device_version.major_version = mhi_ctrl->major_version;
  5401. plat_priv->device_version.minor_version = mhi_ctrl->minor_version;
  5402. cnss_pr_dbg("Get device version info, family number: 0x%x, device number: 0x%x, major version: 0x%x, minor version: 0x%x\n",
  5403. plat_priv->device_version.family_number,
  5404. plat_priv->device_version.device_number,
  5405. plat_priv->device_version.major_version,
  5406. plat_priv->device_version.minor_version);
  5407. /* Only keep lower 4 bits as real device major version */
  5408. plat_priv->device_version.major_version &= DEVICE_MAJOR_VERSION_MASK;
  5409. exit:
  5410. return ret;
  5411. }
  5412. static bool cnss_is_tme_supported(struct cnss_pci_data *pci_priv)
  5413. {
  5414. if (!pci_priv) {
  5415. cnss_pr_dbg("pci_priv is NULL");
  5416. return false;
  5417. }
  5418. switch (pci_priv->device_id) {
  5419. case PEACH_DEVICE_ID:
  5420. return true;
  5421. default:
  5422. return false;
  5423. }
  5424. }
  5425. static int cnss_pci_register_mhi(struct cnss_pci_data *pci_priv)
  5426. {
  5427. int ret = 0;
  5428. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5429. struct pci_dev *pci_dev = pci_priv->pci_dev;
  5430. struct mhi_controller *mhi_ctrl;
  5431. phys_addr_t bar_start;
  5432. const struct mhi_controller_config *cnss_mhi_config =
  5433. &cnss_mhi_config_default;
  5434. ret = cnss_qmi_init(plat_priv);
  5435. if (ret)
  5436. return -EINVAL;
  5437. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5438. return 0;
  5439. mhi_ctrl = mhi_alloc_controller();
  5440. if (!mhi_ctrl) {
  5441. cnss_pr_err("Invalid MHI controller context\n");
  5442. return -EINVAL;
  5443. }
  5444. pci_priv->mhi_ctrl = mhi_ctrl;
  5445. mhi_ctrl->cntrl_dev = &pci_dev->dev;
  5446. mhi_ctrl->fw_image = plat_priv->firmware_name;
  5447. #if IS_ENABLED(CONFIG_MHI_BUS_MISC)
  5448. mhi_ctrl->fallback_fw_image = plat_priv->fw_fallback_name;
  5449. #endif
  5450. mhi_ctrl->regs = pci_priv->bar;
  5451. mhi_ctrl->reg_len = pci_resource_len(pci_priv->pci_dev, PCI_BAR_NUM);
  5452. bar_start = pci_resource_start(pci_priv->pci_dev, PCI_BAR_NUM);
  5453. cnss_pr_dbg("BAR starts at %pa, length is %x\n",
  5454. &bar_start, mhi_ctrl->reg_len);
  5455. ret = cnss_pci_get_mhi_msi(pci_priv);
  5456. if (ret) {
  5457. cnss_pr_err("Failed to get MSI for MHI, err = %d\n", ret);
  5458. goto free_mhi_ctrl;
  5459. }
  5460. if (cnss_pci_is_one_msi(pci_priv))
  5461. mhi_ctrl->irq_flags = IRQF_SHARED | IRQF_NOBALANCING;
  5462. if (pci_priv->smmu_s1_enable) {
  5463. mhi_ctrl->iova_start = pci_priv->smmu_iova_start;
  5464. mhi_ctrl->iova_stop = pci_priv->smmu_iova_start +
  5465. pci_priv->smmu_iova_len;
  5466. } else {
  5467. mhi_ctrl->iova_start = 0;
  5468. mhi_ctrl->iova_stop = pci_priv->dma_bit_mask;
  5469. }
  5470. mhi_ctrl->status_cb = cnss_mhi_notify_status;
  5471. mhi_ctrl->runtime_get = cnss_mhi_pm_runtime_get;
  5472. mhi_ctrl->runtime_put = cnss_mhi_pm_runtime_put_noidle;
  5473. mhi_ctrl->read_reg = cnss_mhi_read_reg;
  5474. mhi_ctrl->write_reg = cnss_mhi_write_reg;
  5475. mhi_ctrl->rddm_size = pci_priv->plat_priv->ramdump_info_v2.ramdump_size;
  5476. if (!mhi_ctrl->rddm_size)
  5477. mhi_ctrl->rddm_size = RAMDUMP_SIZE_DEFAULT;
  5478. if (plat_priv->device_id == QCN7605_DEVICE_ID)
  5479. mhi_ctrl->sbl_size = SZ_256K;
  5480. else
  5481. mhi_ctrl->sbl_size = SZ_512K;
  5482. mhi_ctrl->seg_len = SZ_512K;
  5483. mhi_ctrl->fbc_download = true;
  5484. ret = cnss_get_mhi_soc_info(plat_priv, mhi_ctrl);
  5485. if (ret)
  5486. goto free_mhi_irq;
  5487. /* Satellite config only supported on KIWI V2 and later chipset */
  5488. if (plat_priv->device_id <= QCA6490_DEVICE_ID ||
  5489. (plat_priv->device_id == KIWI_DEVICE_ID &&
  5490. plat_priv->device_version.major_version == 1)) {
  5491. if (plat_priv->device_id == QCN7605_DEVICE_ID)
  5492. cnss_mhi_config = &cnss_mhi_config_genoa;
  5493. else
  5494. cnss_mhi_config = &cnss_mhi_config_no_satellite;
  5495. }
  5496. mhi_ctrl->tme_supported_image = cnss_is_tme_supported(pci_priv);
  5497. ret = mhi_register_controller(mhi_ctrl, cnss_mhi_config);
  5498. if (ret) {
  5499. cnss_pr_err("Failed to register to MHI bus, err = %d\n", ret);
  5500. goto free_mhi_irq;
  5501. }
  5502. /* MHI satellite driver only needs to connect when DRV is supported */
  5503. if (cnss_pci_get_drv_supported(pci_priv))
  5504. cnss_mhi_controller_set_base(pci_priv, bar_start);
  5505. cnss_get_bwscal_info(plat_priv);
  5506. cnss_pr_dbg("no_bwscale: %d\n", plat_priv->no_bwscale);
  5507. /* BW scale CB needs to be set after registering MHI per requirement */
  5508. if (!plat_priv->no_bwscale)
  5509. cnss_mhi_controller_set_bw_scale_cb(pci_priv,
  5510. cnss_mhi_bw_scale);
  5511. ret = cnss_pci_update_fw_name(pci_priv);
  5512. if (ret)
  5513. goto unreg_mhi;
  5514. return 0;
  5515. unreg_mhi:
  5516. mhi_unregister_controller(mhi_ctrl);
  5517. free_mhi_irq:
  5518. kfree(mhi_ctrl->irq);
  5519. free_mhi_ctrl:
  5520. mhi_free_controller(mhi_ctrl);
  5521. return ret;
  5522. }
  5523. static void cnss_pci_unregister_mhi(struct cnss_pci_data *pci_priv)
  5524. {
  5525. struct mhi_controller *mhi_ctrl = pci_priv->mhi_ctrl;
  5526. if (pci_priv->device_id == QCA6174_DEVICE_ID)
  5527. return;
  5528. mhi_unregister_controller(mhi_ctrl);
  5529. kfree(mhi_ctrl->irq);
  5530. mhi_ctrl->irq = NULL;
  5531. mhi_free_controller(mhi_ctrl);
  5532. pci_priv->mhi_ctrl = NULL;
  5533. }
  5534. static void cnss_pci_config_regs(struct cnss_pci_data *pci_priv)
  5535. {
  5536. switch (pci_priv->device_id) {
  5537. case QCA6390_DEVICE_ID:
  5538. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6390;
  5539. pci_priv->wcss_reg = wcss_reg_access_seq;
  5540. pci_priv->pcie_reg = pcie_reg_access_seq;
  5541. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5542. pci_priv->syspm_reg = syspm_reg_access_seq;
  5543. /* Configure WDOG register with specific value so that we can
  5544. * know if HW is in the process of WDOG reset recovery or not
  5545. * when reading the registers.
  5546. */
  5547. cnss_pci_reg_write
  5548. (pci_priv,
  5549. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG,
  5550. QCA6390_PCIE_SOC_WDOG_DISC_BAD_DATA_LOW_CFG_SOC_PCIE_REG_VAL);
  5551. break;
  5552. case QCA6490_DEVICE_ID:
  5553. pci_priv->misc_reg_dev_mask = REG_MASK_QCA6490;
  5554. pci_priv->wlaon_reg = wlaon_reg_access_seq;
  5555. break;
  5556. default:
  5557. return;
  5558. }
  5559. }
  5560. #if !IS_ENABLED(CONFIG_ARCH_QCOM)
  5561. static int cnss_pci_of_reserved_mem_device_init(struct cnss_pci_data *pci_priv)
  5562. {
  5563. return 0;
  5564. }
  5565. static irqreturn_t cnss_pci_wake_handler(int irq, void *data)
  5566. {
  5567. struct cnss_pci_data *pci_priv = data;
  5568. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5569. enum rpm_status status;
  5570. struct device *dev;
  5571. pci_priv->wake_counter++;
  5572. cnss_pr_dbg("WLAN PCI wake IRQ (%u) is asserted #%u\n",
  5573. pci_priv->wake_irq, pci_priv->wake_counter);
  5574. /* Make sure abort current suspend */
  5575. cnss_pm_stay_awake(plat_priv);
  5576. cnss_pm_relax(plat_priv);
  5577. /* Above two pm* API calls will abort system suspend only when
  5578. * plat_dev->dev->ws is initiated by device_init_wakeup() API, and
  5579. * calling pm_system_wakeup() is just to guarantee system suspend
  5580. * can be aborted if it is not initiated in any case.
  5581. */
  5582. pm_system_wakeup();
  5583. dev = &pci_priv->pci_dev->dev;
  5584. status = dev->power.runtime_status;
  5585. if ((cnss_pci_get_monitor_wake_intr(pci_priv) &&
  5586. cnss_pci_get_auto_suspended(pci_priv)) ||
  5587. (status == RPM_SUSPENDING || status == RPM_SUSPENDED)) {
  5588. cnss_pci_set_monitor_wake_intr(pci_priv, false);
  5589. cnss_pci_pm_request_resume(pci_priv);
  5590. }
  5591. return IRQ_HANDLED;
  5592. }
  5593. /**
  5594. * cnss_pci_wake_gpio_init() - Setup PCI wake GPIO for WLAN
  5595. * @pci_priv: driver PCI bus context pointer
  5596. *
  5597. * This function initializes WLAN PCI wake GPIO and corresponding
  5598. * interrupt. It should be used in non-MSM platforms whose PCIe
  5599. * root complex driver doesn't handle the GPIO.
  5600. *
  5601. * Return: 0 for success or skip, negative value for error
  5602. */
  5603. static int cnss_pci_wake_gpio_init(struct cnss_pci_data *pci_priv)
  5604. {
  5605. struct cnss_plat_data *plat_priv = pci_priv->plat_priv;
  5606. struct device *dev = &plat_priv->plat_dev->dev;
  5607. int ret = 0;
  5608. pci_priv->wake_gpio = of_get_named_gpio(dev->of_node,
  5609. "wlan-pci-wake-gpio", 0);
  5610. if (pci_priv->wake_gpio < 0)
  5611. goto out;
  5612. cnss_pr_dbg("Get PCI wake GPIO (%d) from device node\n",
  5613. pci_priv->wake_gpio);
  5614. ret = gpio_request(pci_priv->wake_gpio, "wlan_pci_wake_gpio");
  5615. if (ret) {
  5616. cnss_pr_err("Failed to request PCI wake GPIO, err = %d\n",
  5617. ret);
  5618. goto out;
  5619. }
  5620. gpio_direction_input(pci_priv->wake_gpio);
  5621. pci_priv->wake_irq = gpio_to_irq(pci_priv->wake_gpio);
  5622. ret = request_irq(pci_priv->wake_irq, cnss_pci_wake_handler,
  5623. IRQF_TRIGGER_FALLING, "wlan_pci_wake_irq", pci_priv);
  5624. if (ret) {
  5625. cnss_pr_err("Failed to request PCI wake IRQ, err = %d\n", ret);
  5626. goto free_gpio;
  5627. }
  5628. ret = enable_irq_wake(pci_priv->wake_irq);
  5629. if (ret) {
  5630. cnss_pr_err("Failed to enable PCI wake IRQ, err = %d\n", ret);
  5631. goto free_irq;
  5632. }
  5633. return 0;
  5634. free_irq:
  5635. free_irq(pci_priv->wake_irq, pci_priv);
  5636. free_gpio:
  5637. gpio_free(pci_priv->wake_gpio);
  5638. out:
  5639. return ret;
  5640. }
  5641. static void cnss_pci_wake_gpio_deinit(struct cnss_pci_data *pci_priv)
  5642. {
  5643. if (pci_priv->wake_gpio < 0)
  5644. return;
  5645. disable_irq_wake(pci_priv->wake_irq);
  5646. free_irq(pci_priv->wake_irq, pci_priv);
  5647. gpio_free(pci_priv->wake_gpio);
  5648. }
  5649. #endif
  5650. #ifdef CONFIG_CNSS_SUPPORT_DUAL_DEV
  5651. static int cnss_try_suspend(struct cnss_plat_data *plat_priv)
  5652. {
  5653. int ret = 0;
  5654. /* in the dual wlan card case, if call pci_register_driver after
  5655. * finishing the first pcie device enumeration, it will cause
  5656. * the cnss_pci_probe called in advance with the second wlan card,
  5657. * and the sequence like this:
  5658. * enter msm_pcie_enumerate -> pci_bus_add_devices -> cnss_pci_probe
  5659. * -> exit msm_pcie_enumerate.
  5660. * But the correct sequence we expected is like this:
  5661. * enter msm_pcie_enumerate -> pci_bus_add_devices ->
  5662. * exit msm_pcie_enumerate -> cnss_pci_probe.
  5663. * And this unexpected sequence will make the second wlan card do
  5664. * pcie link suspend while the pcie enumeration not finished.
  5665. * So need to add below logical to avoid doing pcie link suspend
  5666. * if the enumeration has not finish.
  5667. */
  5668. plat_priv->enumerate_done = true;
  5669. /* Now enumeration is finished, try to suspend PCIe link */
  5670. if (plat_priv->bus_priv) {
  5671. struct cnss_pci_data *pci_priv = plat_priv->bus_priv;
  5672. struct pci_dev *pci_dev = pci_priv->pci_dev;
  5673. switch (pci_dev->device) {
  5674. case QCA6390_DEVICE_ID:
  5675. cnss_pci_set_wlaon_pwr_ctrl(pci_priv,
  5676. false,
  5677. true,
  5678. false);
  5679. cnss_pci_suspend_pwroff(pci_dev);
  5680. break;
  5681. default:
  5682. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5683. pci_dev->device);
  5684. ret = -ENODEV;
  5685. }
  5686. }
  5687. return ret;
  5688. }
  5689. #else
  5690. static int cnss_try_suspend(struct cnss_plat_data *plat_priv)
  5691. {
  5692. return 0;
  5693. }
  5694. #endif
  5695. /* Setting to use this cnss_pm_domain ops will let PM framework override the
  5696. * ops from dev->bus->pm which is pci_dev_pm_ops from pci-driver.c. This ops
  5697. * has to take care everything device driver needed which is currently done
  5698. * from pci_dev_pm_ops.
  5699. */
  5700. static struct dev_pm_domain cnss_pm_domain = {
  5701. .ops = {
  5702. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5703. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5704. cnss_pci_resume_noirq)
  5705. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend,
  5706. cnss_pci_runtime_resume,
  5707. cnss_pci_runtime_idle)
  5708. }
  5709. };
  5710. static int cnss_pci_get_dev_cfg_node(struct cnss_plat_data *plat_priv)
  5711. {
  5712. struct device_node *child;
  5713. u32 id, i;
  5714. int id_n, ret;
  5715. if (plat_priv->dt_type != CNSS_DTT_MULTIEXCHG)
  5716. return 0;
  5717. if (!plat_priv->device_id) {
  5718. cnss_pr_err("Invalid device id\n");
  5719. return -EINVAL;
  5720. }
  5721. for_each_available_child_of_node(plat_priv->plat_dev->dev.of_node,
  5722. child) {
  5723. if (strcmp(child->name, "chip_cfg"))
  5724. continue;
  5725. id_n = of_property_count_u32_elems(child, "supported-ids");
  5726. if (id_n <= 0) {
  5727. cnss_pr_err("Device id is NOT set\n");
  5728. return -EINVAL;
  5729. }
  5730. for (i = 0; i < id_n; i++) {
  5731. ret = of_property_read_u32_index(child,
  5732. "supported-ids",
  5733. i, &id);
  5734. if (ret) {
  5735. cnss_pr_err("Failed to read supported ids\n");
  5736. return -EINVAL;
  5737. }
  5738. if (id == plat_priv->device_id) {
  5739. plat_priv->dev_node = child;
  5740. cnss_pr_dbg("got node[%s@%d] for device[0x%x]\n",
  5741. child->name, i, id);
  5742. return 0;
  5743. }
  5744. }
  5745. }
  5746. return -EINVAL;
  5747. }
  5748. #ifdef CONFIG_CNSS2_CONDITIONAL_POWEROFF
  5749. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  5750. {
  5751. bool suspend_pwroff;
  5752. switch (pci_dev->device) {
  5753. case QCA6390_DEVICE_ID:
  5754. case QCA6490_DEVICE_ID:
  5755. suspend_pwroff = false;
  5756. break;
  5757. default:
  5758. suspend_pwroff = true;
  5759. }
  5760. return suspend_pwroff;
  5761. }
  5762. #else
  5763. static bool cnss_should_suspend_pwroff(struct pci_dev *pci_dev)
  5764. {
  5765. return true;
  5766. }
  5767. #endif
  5768. static void cnss_pci_suspend_pwroff(struct pci_dev *pci_dev)
  5769. {
  5770. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5771. int rc_num = pci_dev->bus->domain_nr;
  5772. struct cnss_plat_data *plat_priv;
  5773. int ret = 0;
  5774. bool suspend_pwroff = cnss_should_suspend_pwroff(pci_dev);
  5775. plat_priv = cnss_get_plat_priv_by_rc_num(rc_num);
  5776. if (suspend_pwroff) {
  5777. ret = cnss_suspend_pci_link(pci_priv);
  5778. if (ret)
  5779. cnss_pr_err("Failed to suspend PCI link, err = %d\n",
  5780. ret);
  5781. cnss_power_off_device(plat_priv);
  5782. } else {
  5783. cnss_pr_dbg("bus suspend and dev power off disabled for device [0x%x]\n",
  5784. pci_dev->device);
  5785. }
  5786. }
  5787. static int cnss_pci_probe(struct pci_dev *pci_dev,
  5788. const struct pci_device_id *id)
  5789. {
  5790. int ret = 0;
  5791. struct cnss_pci_data *pci_priv;
  5792. struct device *dev = &pci_dev->dev;
  5793. int rc_num = pci_dev->bus->domain_nr;
  5794. struct cnss_plat_data *plat_priv = cnss_get_plat_priv_by_rc_num(rc_num);
  5795. cnss_pr_dbg("PCI is probing, vendor ID: 0x%x, device ID: 0x%x rc_num %d\n",
  5796. id->vendor, pci_dev->device, rc_num);
  5797. if (!plat_priv) {
  5798. cnss_pr_err("Find match plat_priv with rc number failure\n");
  5799. ret = -ENODEV;
  5800. goto out;
  5801. }
  5802. pci_priv = devm_kzalloc(dev, sizeof(*pci_priv), GFP_KERNEL);
  5803. if (!pci_priv) {
  5804. ret = -ENOMEM;
  5805. goto out;
  5806. }
  5807. pci_priv->pci_link_state = PCI_LINK_UP;
  5808. pci_priv->plat_priv = plat_priv;
  5809. pci_priv->pci_dev = pci_dev;
  5810. pci_priv->pci_device_id = id;
  5811. pci_priv->device_id = pci_dev->device;
  5812. cnss_set_pci_priv(pci_dev, pci_priv);
  5813. plat_priv->device_id = pci_dev->device;
  5814. plat_priv->bus_priv = pci_priv;
  5815. mutex_init(&pci_priv->bus_lock);
  5816. if (plat_priv->use_pm_domain)
  5817. dev->pm_domain = &cnss_pm_domain;
  5818. ret = cnss_pci_get_dev_cfg_node(plat_priv);
  5819. if (ret) {
  5820. cnss_pr_err("Failed to get device cfg node, err = %d\n", ret);
  5821. goto reset_ctx;
  5822. }
  5823. cnss_get_sleep_clk_supported(plat_priv);
  5824. ret = cnss_dev_specific_power_on(plat_priv);
  5825. if (ret < 0)
  5826. goto reset_ctx;
  5827. cnss_pci_of_reserved_mem_device_init(pci_priv);
  5828. ret = cnss_register_subsys(plat_priv);
  5829. if (ret)
  5830. goto reset_ctx;
  5831. ret = cnss_register_ramdump(plat_priv);
  5832. if (ret)
  5833. goto unregister_subsys;
  5834. ret = cnss_pci_init_smmu(pci_priv);
  5835. if (ret)
  5836. goto unregister_ramdump;
  5837. /* update drv support flag */
  5838. cnss_pci_update_drv_supported(pci_priv);
  5839. ret = cnss_reg_pci_event(pci_priv);
  5840. if (ret) {
  5841. cnss_pr_err("Failed to register PCI event, err = %d\n", ret);
  5842. goto deinit_smmu;
  5843. }
  5844. ret = cnss_pci_enable_bus(pci_priv);
  5845. if (ret)
  5846. goto dereg_pci_event;
  5847. ret = cnss_pci_enable_msi(pci_priv);
  5848. if (ret)
  5849. goto disable_bus;
  5850. ret = cnss_pci_register_mhi(pci_priv);
  5851. if (ret)
  5852. goto disable_msi;
  5853. switch (pci_dev->device) {
  5854. case QCA6174_DEVICE_ID:
  5855. pci_read_config_word(pci_dev, QCA6174_REV_ID_OFFSET,
  5856. &pci_priv->revision_id);
  5857. break;
  5858. case QCA6290_DEVICE_ID:
  5859. case QCA6390_DEVICE_ID:
  5860. case QCN7605_DEVICE_ID:
  5861. case QCA6490_DEVICE_ID:
  5862. case KIWI_DEVICE_ID:
  5863. case MANGO_DEVICE_ID:
  5864. case PEACH_DEVICE_ID:
  5865. if ((cnss_is_dual_wlan_enabled() &&
  5866. plat_priv->enumerate_done) || !cnss_is_dual_wlan_enabled())
  5867. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, false,
  5868. false);
  5869. timer_setup(&pci_priv->dev_rddm_timer,
  5870. cnss_dev_rddm_timeout_hdlr, 0);
  5871. timer_setup(&pci_priv->boot_debug_timer,
  5872. cnss_boot_debug_timeout_hdlr, 0);
  5873. INIT_DELAYED_WORK(&pci_priv->time_sync_work,
  5874. cnss_pci_time_sync_work_hdlr);
  5875. cnss_pci_get_link_status(pci_priv);
  5876. cnss_pci_set_wlaon_pwr_ctrl(pci_priv, false, true, false);
  5877. cnss_pci_wake_gpio_init(pci_priv);
  5878. break;
  5879. default:
  5880. cnss_pr_err("Unknown PCI device found: 0x%x\n",
  5881. pci_dev->device);
  5882. ret = -ENODEV;
  5883. goto unreg_mhi;
  5884. }
  5885. cnss_pci_config_regs(pci_priv);
  5886. if (EMULATION_HW)
  5887. goto out;
  5888. if (cnss_is_dual_wlan_enabled() && !plat_priv->enumerate_done)
  5889. goto probe_done;
  5890. cnss_pci_suspend_pwroff(pci_dev);
  5891. probe_done:
  5892. set_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5893. return 0;
  5894. unreg_mhi:
  5895. cnss_pci_unregister_mhi(pci_priv);
  5896. disable_msi:
  5897. cnss_pci_disable_msi(pci_priv);
  5898. disable_bus:
  5899. cnss_pci_disable_bus(pci_priv);
  5900. dereg_pci_event:
  5901. cnss_dereg_pci_event(pci_priv);
  5902. deinit_smmu:
  5903. cnss_pci_deinit_smmu(pci_priv);
  5904. unregister_ramdump:
  5905. cnss_unregister_ramdump(plat_priv);
  5906. unregister_subsys:
  5907. cnss_unregister_subsys(plat_priv);
  5908. reset_ctx:
  5909. plat_priv->bus_priv = NULL;
  5910. out:
  5911. return ret;
  5912. }
  5913. static void cnss_pci_remove(struct pci_dev *pci_dev)
  5914. {
  5915. struct cnss_pci_data *pci_priv = cnss_get_pci_priv(pci_dev);
  5916. struct cnss_plat_data *plat_priv =
  5917. cnss_bus_dev_to_plat_priv(&pci_dev->dev);
  5918. clear_bit(CNSS_PCI_PROBE_DONE, &plat_priv->driver_state);
  5919. cnss_pci_unregister_driver_hdlr(pci_priv);
  5920. cnss_pci_free_m3_mem(pci_priv);
  5921. cnss_pci_free_fw_mem(pci_priv);
  5922. cnss_pci_free_qdss_mem(pci_priv);
  5923. switch (pci_dev->device) {
  5924. case QCA6290_DEVICE_ID:
  5925. case QCA6390_DEVICE_ID:
  5926. case QCN7605_DEVICE_ID:
  5927. case QCA6490_DEVICE_ID:
  5928. case KIWI_DEVICE_ID:
  5929. case MANGO_DEVICE_ID:
  5930. case PEACH_DEVICE_ID:
  5931. cnss_pci_wake_gpio_deinit(pci_priv);
  5932. del_timer(&pci_priv->boot_debug_timer);
  5933. del_timer(&pci_priv->dev_rddm_timer);
  5934. break;
  5935. default:
  5936. break;
  5937. }
  5938. cnss_pci_unregister_mhi(pci_priv);
  5939. cnss_pci_disable_msi(pci_priv);
  5940. cnss_pci_disable_bus(pci_priv);
  5941. cnss_dereg_pci_event(pci_priv);
  5942. cnss_pci_deinit_smmu(pci_priv);
  5943. if (plat_priv) {
  5944. cnss_unregister_ramdump(plat_priv);
  5945. cnss_unregister_subsys(plat_priv);
  5946. plat_priv->bus_priv = NULL;
  5947. } else {
  5948. cnss_pr_err("Plat_priv is null, Unable to unregister ramdump,subsys\n");
  5949. }
  5950. }
  5951. static const struct pci_device_id cnss_pci_id_table[] = {
  5952. { QCA6174_VENDOR_ID, QCA6174_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5953. { QCA6290_VENDOR_ID, QCA6290_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5954. { QCA6390_VENDOR_ID, QCA6390_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5955. { QCN7605_VENDOR_ID, QCN7605_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5956. { QCA6490_VENDOR_ID, QCA6490_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5957. { KIWI_VENDOR_ID, KIWI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5958. { MANGO_VENDOR_ID, MANGO_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5959. { PEACH_VENDOR_ID, PEACH_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID },
  5960. { 0 }
  5961. };
  5962. MODULE_DEVICE_TABLE(pci, cnss_pci_id_table);
  5963. static const struct dev_pm_ops cnss_pm_ops = {
  5964. SET_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend, cnss_pci_resume)
  5965. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cnss_pci_suspend_noirq,
  5966. cnss_pci_resume_noirq)
  5967. SET_RUNTIME_PM_OPS(cnss_pci_runtime_suspend, cnss_pci_runtime_resume,
  5968. cnss_pci_runtime_idle)
  5969. };
  5970. static struct pci_driver cnss_pci_driver = {
  5971. .name = "cnss_pci",
  5972. .id_table = cnss_pci_id_table,
  5973. .probe = cnss_pci_probe,
  5974. .remove = cnss_pci_remove,
  5975. .driver = {
  5976. .pm = &cnss_pm_ops,
  5977. },
  5978. };
  5979. static int cnss_pci_enumerate(struct cnss_plat_data *plat_priv, u32 rc_num)
  5980. {
  5981. int ret, retry = 0;
  5982. /* Always set initial target PCIe link speed to Gen2 for QCA6490 device
  5983. * since there may be link issues if it boots up with Gen3 link speed.
  5984. * Device is able to change it later at any time. It will be rejected
  5985. * if requested speed is higher than the one specified in PCIe DT.
  5986. */
  5987. if (plat_priv->device_id == QCA6490_DEVICE_ID) {
  5988. ret = cnss_pci_set_max_link_speed(plat_priv->bus_priv, rc_num,
  5989. PCI_EXP_LNKSTA_CLS_5_0GB);
  5990. if (ret && ret != -EPROBE_DEFER)
  5991. cnss_pr_err("Failed to set max PCIe RC%x link speed to Gen2, err = %d\n",
  5992. rc_num, ret);
  5993. }
  5994. cnss_pr_dbg("Trying to enumerate with PCIe RC%x\n", rc_num);
  5995. retry:
  5996. ret = _cnss_pci_enumerate(plat_priv, rc_num);
  5997. if (ret) {
  5998. if (ret == -EPROBE_DEFER) {
  5999. cnss_pr_dbg("PCIe RC driver is not ready, defer probe\n");
  6000. goto out;
  6001. }
  6002. cnss_pr_err("Failed to enable PCIe RC%x, err = %d\n",
  6003. rc_num, ret);
  6004. if (retry++ < LINK_TRAINING_RETRY_MAX_TIMES) {
  6005. cnss_pr_dbg("Retry PCI link training #%d\n", retry);
  6006. goto retry;
  6007. } else {
  6008. goto out;
  6009. }
  6010. }
  6011. plat_priv->rc_num = rc_num;
  6012. out:
  6013. return ret;
  6014. }
  6015. int cnss_pci_init(struct cnss_plat_data *plat_priv)
  6016. {
  6017. struct device *dev = &plat_priv->plat_dev->dev;
  6018. const __be32 *prop;
  6019. int ret = 0, prop_len = 0, rc_count, i;
  6020. prop = of_get_property(dev->of_node, "qcom,wlan-rc-num", &prop_len);
  6021. if (!prop || !prop_len) {
  6022. cnss_pr_err("Failed to get PCIe RC number from DT\n");
  6023. goto out;
  6024. }
  6025. rc_count = prop_len / sizeof(__be32);
  6026. for (i = 0; i < rc_count; i++) {
  6027. ret = cnss_pci_enumerate(plat_priv, be32_to_cpup(&prop[i]));
  6028. if (!ret)
  6029. break;
  6030. else if (ret == -EPROBE_DEFER || (ret && i == rc_count - 1))
  6031. goto out;
  6032. }
  6033. ret = cnss_try_suspend(plat_priv);
  6034. if (ret) {
  6035. cnss_pr_err("Failed to suspend, ret: %d\n", ret);
  6036. goto out;
  6037. }
  6038. if (!cnss_driver_registered) {
  6039. ret = pci_register_driver(&cnss_pci_driver);
  6040. if (ret) {
  6041. cnss_pr_err("Failed to register to PCI framework, err = %d\n",
  6042. ret);
  6043. goto out;
  6044. }
  6045. if (!plat_priv->bus_priv) {
  6046. cnss_pr_err("Failed to probe PCI driver\n");
  6047. ret = -ENODEV;
  6048. goto unreg_pci;
  6049. }
  6050. cnss_driver_registered = true;
  6051. }
  6052. return 0;
  6053. unreg_pci:
  6054. pci_unregister_driver(&cnss_pci_driver);
  6055. out:
  6056. return ret;
  6057. }
  6058. void cnss_pci_deinit(struct cnss_plat_data *plat_priv)
  6059. {
  6060. if (cnss_driver_registered) {
  6061. pci_unregister_driver(&cnss_pci_driver);
  6062. cnss_driver_registered = false;
  6063. }
  6064. }