pci.c 197 KB

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