pci.c 198 KB

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