pci.c 200 KB

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