pci.c 197 KB

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