pci.c 189 KB

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