pci.c 186 KB

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