pci.c 196 KB

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