pci.c 196 KB

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