ci_dpm.c 174 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/firmware.h>
  24. #include "drmP.h"
  25. #include "radeon.h"
  26. #include "radeon_asic.h"
  27. #include "radeon_ucode.h"
  28. #include "cikd.h"
  29. #include "r600_dpm.h"
  30. #include "ci_dpm.h"
  31. #include "atom.h"
  32. #include <linux/seq_file.h>
  33. #define MC_CG_ARB_FREQ_F0 0x0a
  34. #define MC_CG_ARB_FREQ_F1 0x0b
  35. #define MC_CG_ARB_FREQ_F2 0x0c
  36. #define MC_CG_ARB_FREQ_F3 0x0d
  37. #define SMC_RAM_END 0x40000
  38. #define VOLTAGE_SCALE 4
  39. #define VOLTAGE_VID_OFFSET_SCALE1 625
  40. #define VOLTAGE_VID_OFFSET_SCALE2 100
  41. static const struct ci_pt_defaults defaults_hawaii_xt =
  42. {
  43. 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0xB0000,
  44. { 0x2E, 0x00, 0x00, 0x88, 0x00, 0x00, 0x72, 0x60, 0x51, 0xA7, 0x79, 0x6B, 0x90, 0xBD, 0x79 },
  45. { 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
  46. };
  47. static const struct ci_pt_defaults defaults_hawaii_pro =
  48. {
  49. 1, 0xF, 0xFD, 0x19, 5, 0x14, 0, 0x65062,
  50. { 0x2E, 0x00, 0x00, 0x88, 0x00, 0x00, 0x72, 0x60, 0x51, 0xA7, 0x79, 0x6B, 0x90, 0xBD, 0x79 },
  51. { 0x217, 0x217, 0x217, 0x242, 0x242, 0x242, 0x269, 0x269, 0x269, 0x2A1, 0x2A1, 0x2A1, 0x2C9, 0x2C9, 0x2C9 }
  52. };
  53. static const struct ci_pt_defaults defaults_bonaire_xt =
  54. {
  55. 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
  56. { 0x79, 0x253, 0x25D, 0xAE, 0x72, 0x80, 0x83, 0x86, 0x6F, 0xC8, 0xC9, 0xC9, 0x2F, 0x4D, 0x61 },
  57. { 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
  58. };
  59. static const struct ci_pt_defaults defaults_bonaire_pro =
  60. {
  61. 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x65062,
  62. { 0x8C, 0x23F, 0x244, 0xA6, 0x83, 0x85, 0x86, 0x86, 0x83, 0xDB, 0xDB, 0xDA, 0x67, 0x60, 0x5F },
  63. { 0x187, 0x193, 0x193, 0x1C7, 0x1D1, 0x1D1, 0x210, 0x219, 0x219, 0x266, 0x26C, 0x26C, 0x2C9, 0x2CB, 0x2CB }
  64. };
  65. static const struct ci_pt_defaults defaults_saturn_xt =
  66. {
  67. 1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x70000,
  68. { 0x8C, 0x247, 0x249, 0xA6, 0x80, 0x81, 0x8B, 0x89, 0x86, 0xC9, 0xCA, 0xC9, 0x4D, 0x4D, 0x4D },
  69. { 0x187, 0x187, 0x187, 0x1C7, 0x1C7, 0x1C7, 0x210, 0x210, 0x210, 0x266, 0x266, 0x266, 0x2C9, 0x2C9, 0x2C9 }
  70. };
  71. static const struct ci_pt_defaults defaults_saturn_pro =
  72. {
  73. 1, 0xF, 0xFD, 0x19, 5, 55, 0, 0x30000,
  74. { 0x96, 0x21D, 0x23B, 0xA1, 0x85, 0x87, 0x83, 0x84, 0x81, 0xE6, 0xE6, 0xE6, 0x71, 0x6A, 0x6A },
  75. { 0x193, 0x19E, 0x19E, 0x1D2, 0x1DC, 0x1DC, 0x21A, 0x223, 0x223, 0x26E, 0x27E, 0x274, 0x2CF, 0x2D2, 0x2D2 }
  76. };
  77. static const struct ci_pt_config_reg didt_config_ci[] =
  78. {
  79. { 0x10, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  80. { 0x10, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  81. { 0x10, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  82. { 0x10, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  83. { 0x11, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  84. { 0x11, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  85. { 0x11, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  86. { 0x11, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  87. { 0x12, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  88. { 0x12, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  89. { 0x12, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  90. { 0x12, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  91. { 0x2, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
  92. { 0x2, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
  93. { 0x2, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
  94. { 0x1, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  95. { 0x1, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  96. { 0x0, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  97. { 0x30, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  98. { 0x30, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  99. { 0x30, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  100. { 0x30, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  101. { 0x31, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  102. { 0x31, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  103. { 0x31, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  104. { 0x31, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  105. { 0x32, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  106. { 0x32, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  107. { 0x32, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  108. { 0x32, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  109. { 0x22, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
  110. { 0x22, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
  111. { 0x22, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
  112. { 0x21, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  113. { 0x21, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  114. { 0x20, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  115. { 0x50, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  116. { 0x50, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  117. { 0x50, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  118. { 0x50, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  119. { 0x51, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  120. { 0x51, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  121. { 0x51, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  122. { 0x51, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  123. { 0x52, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  124. { 0x52, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  125. { 0x52, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  126. { 0x52, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  127. { 0x42, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
  128. { 0x42, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
  129. { 0x42, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
  130. { 0x41, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  131. { 0x41, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  132. { 0x40, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  133. { 0x70, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  134. { 0x70, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  135. { 0x70, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  136. { 0x70, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  137. { 0x71, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  138. { 0x71, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  139. { 0x71, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  140. { 0x71, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  141. { 0x72, 0x000000ff, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  142. { 0x72, 0x0000ff00, 8, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  143. { 0x72, 0x00ff0000, 16, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  144. { 0x72, 0xff000000, 24, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  145. { 0x62, 0x00003fff, 0, 0x4, CISLANDS_CONFIGREG_DIDT_IND },
  146. { 0x62, 0x03ff0000, 16, 0x80, CISLANDS_CONFIGREG_DIDT_IND },
  147. { 0x62, 0x78000000, 27, 0x3, CISLANDS_CONFIGREG_DIDT_IND },
  148. { 0x61, 0x0000ffff, 0, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  149. { 0x61, 0xffff0000, 16, 0x3FFF, CISLANDS_CONFIGREG_DIDT_IND },
  150. { 0x60, 0x00000001, 0, 0x0, CISLANDS_CONFIGREG_DIDT_IND },
  151. { 0xFFFFFFFF }
  152. };
  153. extern u8 rv770_get_memory_module_index(struct radeon_device *rdev);
  154. extern int ni_copy_and_switch_arb_sets(struct radeon_device *rdev,
  155. u32 arb_freq_src, u32 arb_freq_dest);
  156. extern u8 si_get_ddr3_mclk_frequency_ratio(u32 memory_clock);
  157. extern u8 si_get_mclk_frequency_ratio(u32 memory_clock, bool strobe_mode);
  158. extern void si_trim_voltage_table_to_fit_state_table(struct radeon_device *rdev,
  159. u32 max_voltage_steps,
  160. struct atom_voltage_table *voltage_table);
  161. extern void cik_enter_rlc_safe_mode(struct radeon_device *rdev);
  162. extern void cik_exit_rlc_safe_mode(struct radeon_device *rdev);
  163. extern int ci_mc_load_microcode(struct radeon_device *rdev);
  164. extern void cik_update_cg(struct radeon_device *rdev,
  165. u32 block, bool enable);
  166. static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
  167. struct atom_voltage_table_entry *voltage_table,
  168. u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd);
  169. static int ci_set_power_limit(struct radeon_device *rdev, u32 n);
  170. static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
  171. u32 target_tdp);
  172. static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate);
  173. static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
  174. PPSMC_Msg msg, u32 parameter);
  175. static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev);
  176. static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev);
  177. static struct ci_power_info *ci_get_pi(struct radeon_device *rdev)
  178. {
  179. struct ci_power_info *pi = rdev->pm.dpm.priv;
  180. return pi;
  181. }
  182. static struct ci_ps *ci_get_ps(struct radeon_ps *rps)
  183. {
  184. struct ci_ps *ps = rps->ps_priv;
  185. return ps;
  186. }
  187. static void ci_initialize_powertune_defaults(struct radeon_device *rdev)
  188. {
  189. struct ci_power_info *pi = ci_get_pi(rdev);
  190. switch (rdev->pdev->device) {
  191. case 0x6649:
  192. case 0x6650:
  193. case 0x6651:
  194. case 0x6658:
  195. case 0x665C:
  196. case 0x665D:
  197. default:
  198. pi->powertune_defaults = &defaults_bonaire_xt;
  199. break;
  200. case 0x6640:
  201. case 0x6641:
  202. case 0x6646:
  203. case 0x6647:
  204. pi->powertune_defaults = &defaults_saturn_xt;
  205. break;
  206. case 0x67B8:
  207. case 0x67B0:
  208. pi->powertune_defaults = &defaults_hawaii_xt;
  209. break;
  210. case 0x67BA:
  211. case 0x67B1:
  212. pi->powertune_defaults = &defaults_hawaii_pro;
  213. break;
  214. case 0x67A0:
  215. case 0x67A1:
  216. case 0x67A2:
  217. case 0x67A8:
  218. case 0x67A9:
  219. case 0x67AA:
  220. case 0x67B9:
  221. case 0x67BE:
  222. pi->powertune_defaults = &defaults_bonaire_xt;
  223. break;
  224. }
  225. pi->dte_tj_offset = 0;
  226. pi->caps_power_containment = true;
  227. pi->caps_cac = false;
  228. pi->caps_sq_ramping = false;
  229. pi->caps_db_ramping = false;
  230. pi->caps_td_ramping = false;
  231. pi->caps_tcp_ramping = false;
  232. if (pi->caps_power_containment) {
  233. pi->caps_cac = true;
  234. if (rdev->family == CHIP_HAWAII)
  235. pi->enable_bapm_feature = false;
  236. else
  237. pi->enable_bapm_feature = true;
  238. pi->enable_tdc_limit_feature = true;
  239. pi->enable_pkg_pwr_tracking_feature = true;
  240. }
  241. }
  242. static u8 ci_convert_to_vid(u16 vddc)
  243. {
  244. return (6200 - (vddc * VOLTAGE_SCALE)) / 25;
  245. }
  246. static int ci_populate_bapm_vddc_vid_sidd(struct radeon_device *rdev)
  247. {
  248. struct ci_power_info *pi = ci_get_pi(rdev);
  249. u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
  250. u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
  251. u8 *hi2_vid = pi->smc_powertune_table.BapmVddCVidHiSidd2;
  252. u32 i;
  253. if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries == NULL)
  254. return -EINVAL;
  255. if (rdev->pm.dpm.dyn_state.cac_leakage_table.count > 8)
  256. return -EINVAL;
  257. if (rdev->pm.dpm.dyn_state.cac_leakage_table.count !=
  258. rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count)
  259. return -EINVAL;
  260. for (i = 0; i < rdev->pm.dpm.dyn_state.cac_leakage_table.count; i++) {
  261. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
  262. lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc1);
  263. hi_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc2);
  264. hi2_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc3);
  265. } else {
  266. lo_vid[i] = ci_convert_to_vid(rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].vddc);
  267. hi_vid[i] = ci_convert_to_vid((u16)rdev->pm.dpm.dyn_state.cac_leakage_table.entries[i].leakage);
  268. }
  269. }
  270. return 0;
  271. }
  272. static int ci_populate_vddc_vid(struct radeon_device *rdev)
  273. {
  274. struct ci_power_info *pi = ci_get_pi(rdev);
  275. u8 *vid = pi->smc_powertune_table.VddCVid;
  276. u32 i;
  277. if (pi->vddc_voltage_table.count > 8)
  278. return -EINVAL;
  279. for (i = 0; i < pi->vddc_voltage_table.count; i++)
  280. vid[i] = ci_convert_to_vid(pi->vddc_voltage_table.entries[i].value);
  281. return 0;
  282. }
  283. static int ci_populate_svi_load_line(struct radeon_device *rdev)
  284. {
  285. struct ci_power_info *pi = ci_get_pi(rdev);
  286. const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
  287. pi->smc_powertune_table.SviLoadLineEn = pt_defaults->svi_load_line_en;
  288. pi->smc_powertune_table.SviLoadLineVddC = pt_defaults->svi_load_line_vddc;
  289. pi->smc_powertune_table.SviLoadLineTrimVddC = 3;
  290. pi->smc_powertune_table.SviLoadLineOffsetVddC = 0;
  291. return 0;
  292. }
  293. static int ci_populate_tdc_limit(struct radeon_device *rdev)
  294. {
  295. struct ci_power_info *pi = ci_get_pi(rdev);
  296. const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
  297. u16 tdc_limit;
  298. tdc_limit = rdev->pm.dpm.dyn_state.cac_tdp_table->tdc * 256;
  299. pi->smc_powertune_table.TDC_VDDC_PkgLimit = cpu_to_be16(tdc_limit);
  300. pi->smc_powertune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
  301. pt_defaults->tdc_vddc_throttle_release_limit_perc;
  302. pi->smc_powertune_table.TDC_MAWt = pt_defaults->tdc_mawt;
  303. return 0;
  304. }
  305. static int ci_populate_dw8(struct radeon_device *rdev)
  306. {
  307. struct ci_power_info *pi = ci_get_pi(rdev);
  308. const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
  309. int ret;
  310. ret = ci_read_smc_sram_dword(rdev,
  311. SMU7_FIRMWARE_HEADER_LOCATION +
  312. offsetof(SMU7_Firmware_Header, PmFuseTable) +
  313. offsetof(SMU7_Discrete_PmFuses, TdcWaterfallCtl),
  314. (u32 *)&pi->smc_powertune_table.TdcWaterfallCtl,
  315. pi->sram_end);
  316. if (ret)
  317. return -EINVAL;
  318. else
  319. pi->smc_powertune_table.TdcWaterfallCtl = pt_defaults->tdc_waterfall_ctl;
  320. return 0;
  321. }
  322. static int ci_populate_fuzzy_fan(struct radeon_device *rdev)
  323. {
  324. struct ci_power_info *pi = ci_get_pi(rdev);
  325. if ((rdev->pm.dpm.fan.fan_output_sensitivity & (1 << 15)) ||
  326. (rdev->pm.dpm.fan.fan_output_sensitivity == 0))
  327. rdev->pm.dpm.fan.fan_output_sensitivity =
  328. rdev->pm.dpm.fan.default_fan_output_sensitivity;
  329. pi->smc_powertune_table.FuzzyFan_PwmSetDelta =
  330. cpu_to_be16(rdev->pm.dpm.fan.fan_output_sensitivity);
  331. return 0;
  332. }
  333. static int ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(struct radeon_device *rdev)
  334. {
  335. struct ci_power_info *pi = ci_get_pi(rdev);
  336. u8 *hi_vid = pi->smc_powertune_table.BapmVddCVidHiSidd;
  337. u8 *lo_vid = pi->smc_powertune_table.BapmVddCVidLoSidd;
  338. int i, min, max;
  339. min = max = hi_vid[0];
  340. for (i = 0; i < 8; i++) {
  341. if (0 != hi_vid[i]) {
  342. if (min > hi_vid[i])
  343. min = hi_vid[i];
  344. if (max < hi_vid[i])
  345. max = hi_vid[i];
  346. }
  347. if (0 != lo_vid[i]) {
  348. if (min > lo_vid[i])
  349. min = lo_vid[i];
  350. if (max < lo_vid[i])
  351. max = lo_vid[i];
  352. }
  353. }
  354. if ((min == 0) || (max == 0))
  355. return -EINVAL;
  356. pi->smc_powertune_table.GnbLPMLMaxVid = (u8)max;
  357. pi->smc_powertune_table.GnbLPMLMinVid = (u8)min;
  358. return 0;
  359. }
  360. static int ci_populate_bapm_vddc_base_leakage_sidd(struct radeon_device *rdev)
  361. {
  362. struct ci_power_info *pi = ci_get_pi(rdev);
  363. u16 hi_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd;
  364. u16 lo_sidd = pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd;
  365. struct radeon_cac_tdp_table *cac_tdp_table =
  366. rdev->pm.dpm.dyn_state.cac_tdp_table;
  367. hi_sidd = cac_tdp_table->high_cac_leakage / 100 * 256;
  368. lo_sidd = cac_tdp_table->low_cac_leakage / 100 * 256;
  369. pi->smc_powertune_table.BapmVddCBaseLeakageHiSidd = cpu_to_be16(hi_sidd);
  370. pi->smc_powertune_table.BapmVddCBaseLeakageLoSidd = cpu_to_be16(lo_sidd);
  371. return 0;
  372. }
  373. static int ci_populate_bapm_parameters_in_dpm_table(struct radeon_device *rdev)
  374. {
  375. struct ci_power_info *pi = ci_get_pi(rdev);
  376. const struct ci_pt_defaults *pt_defaults = pi->powertune_defaults;
  377. SMU7_Discrete_DpmTable *dpm_table = &pi->smc_state_table;
  378. struct radeon_cac_tdp_table *cac_tdp_table =
  379. rdev->pm.dpm.dyn_state.cac_tdp_table;
  380. struct radeon_ppm_table *ppm = rdev->pm.dpm.dyn_state.ppm_table;
  381. int i, j, k;
  382. const u16 *def1;
  383. const u16 *def2;
  384. dpm_table->DefaultTdp = cac_tdp_table->tdp * 256;
  385. dpm_table->TargetTdp = cac_tdp_table->configurable_tdp * 256;
  386. dpm_table->DTETjOffset = (u8)pi->dte_tj_offset;
  387. dpm_table->GpuTjMax =
  388. (u8)(pi->thermal_temp_setting.temperature_high / 1000);
  389. dpm_table->GpuTjHyst = 8;
  390. dpm_table->DTEAmbientTempBase = pt_defaults->dte_ambient_temp_base;
  391. if (ppm) {
  392. dpm_table->PPM_PkgPwrLimit = cpu_to_be16((u16)ppm->dgpu_tdp * 256 / 1000);
  393. dpm_table->PPM_TemperatureLimit = cpu_to_be16((u16)ppm->tj_max * 256);
  394. } else {
  395. dpm_table->PPM_PkgPwrLimit = cpu_to_be16(0);
  396. dpm_table->PPM_TemperatureLimit = cpu_to_be16(0);
  397. }
  398. dpm_table->BAPM_TEMP_GRADIENT = cpu_to_be32(pt_defaults->bapm_temp_gradient);
  399. def1 = pt_defaults->bapmti_r;
  400. def2 = pt_defaults->bapmti_rc;
  401. for (i = 0; i < SMU7_DTE_ITERATIONS; i++) {
  402. for (j = 0; j < SMU7_DTE_SOURCES; j++) {
  403. for (k = 0; k < SMU7_DTE_SINKS; k++) {
  404. dpm_table->BAPMTI_R[i][j][k] = cpu_to_be16(*def1);
  405. dpm_table->BAPMTI_RC[i][j][k] = cpu_to_be16(*def2);
  406. def1++;
  407. def2++;
  408. }
  409. }
  410. }
  411. return 0;
  412. }
  413. static int ci_populate_pm_base(struct radeon_device *rdev)
  414. {
  415. struct ci_power_info *pi = ci_get_pi(rdev);
  416. u32 pm_fuse_table_offset;
  417. int ret;
  418. if (pi->caps_power_containment) {
  419. ret = ci_read_smc_sram_dword(rdev,
  420. SMU7_FIRMWARE_HEADER_LOCATION +
  421. offsetof(SMU7_Firmware_Header, PmFuseTable),
  422. &pm_fuse_table_offset, pi->sram_end);
  423. if (ret)
  424. return ret;
  425. ret = ci_populate_bapm_vddc_vid_sidd(rdev);
  426. if (ret)
  427. return ret;
  428. ret = ci_populate_vddc_vid(rdev);
  429. if (ret)
  430. return ret;
  431. ret = ci_populate_svi_load_line(rdev);
  432. if (ret)
  433. return ret;
  434. ret = ci_populate_tdc_limit(rdev);
  435. if (ret)
  436. return ret;
  437. ret = ci_populate_dw8(rdev);
  438. if (ret)
  439. return ret;
  440. ret = ci_populate_fuzzy_fan(rdev);
  441. if (ret)
  442. return ret;
  443. ret = ci_min_max_v_gnbl_pm_lid_from_bapm_vddc(rdev);
  444. if (ret)
  445. return ret;
  446. ret = ci_populate_bapm_vddc_base_leakage_sidd(rdev);
  447. if (ret)
  448. return ret;
  449. ret = ci_copy_bytes_to_smc(rdev, pm_fuse_table_offset,
  450. (u8 *)&pi->smc_powertune_table,
  451. sizeof(SMU7_Discrete_PmFuses), pi->sram_end);
  452. if (ret)
  453. return ret;
  454. }
  455. return 0;
  456. }
  457. static void ci_do_enable_didt(struct radeon_device *rdev, const bool enable)
  458. {
  459. struct ci_power_info *pi = ci_get_pi(rdev);
  460. u32 data;
  461. if (pi->caps_sq_ramping) {
  462. data = RREG32_DIDT(DIDT_SQ_CTRL0);
  463. if (enable)
  464. data |= DIDT_CTRL_EN;
  465. else
  466. data &= ~DIDT_CTRL_EN;
  467. WREG32_DIDT(DIDT_SQ_CTRL0, data);
  468. }
  469. if (pi->caps_db_ramping) {
  470. data = RREG32_DIDT(DIDT_DB_CTRL0);
  471. if (enable)
  472. data |= DIDT_CTRL_EN;
  473. else
  474. data &= ~DIDT_CTRL_EN;
  475. WREG32_DIDT(DIDT_DB_CTRL0, data);
  476. }
  477. if (pi->caps_td_ramping) {
  478. data = RREG32_DIDT(DIDT_TD_CTRL0);
  479. if (enable)
  480. data |= DIDT_CTRL_EN;
  481. else
  482. data &= ~DIDT_CTRL_EN;
  483. WREG32_DIDT(DIDT_TD_CTRL0, data);
  484. }
  485. if (pi->caps_tcp_ramping) {
  486. data = RREG32_DIDT(DIDT_TCP_CTRL0);
  487. if (enable)
  488. data |= DIDT_CTRL_EN;
  489. else
  490. data &= ~DIDT_CTRL_EN;
  491. WREG32_DIDT(DIDT_TCP_CTRL0, data);
  492. }
  493. }
  494. static int ci_program_pt_config_registers(struct radeon_device *rdev,
  495. const struct ci_pt_config_reg *cac_config_regs)
  496. {
  497. const struct ci_pt_config_reg *config_regs = cac_config_regs;
  498. u32 data;
  499. u32 cache = 0;
  500. if (config_regs == NULL)
  501. return -EINVAL;
  502. while (config_regs->offset != 0xFFFFFFFF) {
  503. if (config_regs->type == CISLANDS_CONFIGREG_CACHE) {
  504. cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
  505. } else {
  506. switch (config_regs->type) {
  507. case CISLANDS_CONFIGREG_SMC_IND:
  508. data = RREG32_SMC(config_regs->offset);
  509. break;
  510. case CISLANDS_CONFIGREG_DIDT_IND:
  511. data = RREG32_DIDT(config_regs->offset);
  512. break;
  513. default:
  514. data = RREG32(config_regs->offset << 2);
  515. break;
  516. }
  517. data &= ~config_regs->mask;
  518. data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
  519. data |= cache;
  520. switch (config_regs->type) {
  521. case CISLANDS_CONFIGREG_SMC_IND:
  522. WREG32_SMC(config_regs->offset, data);
  523. break;
  524. case CISLANDS_CONFIGREG_DIDT_IND:
  525. WREG32_DIDT(config_regs->offset, data);
  526. break;
  527. default:
  528. WREG32(config_regs->offset << 2, data);
  529. break;
  530. }
  531. cache = 0;
  532. }
  533. config_regs++;
  534. }
  535. return 0;
  536. }
  537. static int ci_enable_didt(struct radeon_device *rdev, bool enable)
  538. {
  539. struct ci_power_info *pi = ci_get_pi(rdev);
  540. int ret;
  541. if (pi->caps_sq_ramping || pi->caps_db_ramping ||
  542. pi->caps_td_ramping || pi->caps_tcp_ramping) {
  543. cik_enter_rlc_safe_mode(rdev);
  544. if (enable) {
  545. ret = ci_program_pt_config_registers(rdev, didt_config_ci);
  546. if (ret) {
  547. cik_exit_rlc_safe_mode(rdev);
  548. return ret;
  549. }
  550. }
  551. ci_do_enable_didt(rdev, enable);
  552. cik_exit_rlc_safe_mode(rdev);
  553. }
  554. return 0;
  555. }
  556. static int ci_enable_power_containment(struct radeon_device *rdev, bool enable)
  557. {
  558. struct ci_power_info *pi = ci_get_pi(rdev);
  559. PPSMC_Result smc_result;
  560. int ret = 0;
  561. if (enable) {
  562. pi->power_containment_features = 0;
  563. if (pi->caps_power_containment) {
  564. if (pi->enable_bapm_feature) {
  565. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableDTE);
  566. if (smc_result != PPSMC_Result_OK)
  567. ret = -EINVAL;
  568. else
  569. pi->power_containment_features |= POWERCONTAINMENT_FEATURE_BAPM;
  570. }
  571. if (pi->enable_tdc_limit_feature) {
  572. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitEnable);
  573. if (smc_result != PPSMC_Result_OK)
  574. ret = -EINVAL;
  575. else
  576. pi->power_containment_features |= POWERCONTAINMENT_FEATURE_TDCLimit;
  577. }
  578. if (pi->enable_pkg_pwr_tracking_feature) {
  579. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitEnable);
  580. if (smc_result != PPSMC_Result_OK) {
  581. ret = -EINVAL;
  582. } else {
  583. struct radeon_cac_tdp_table *cac_tdp_table =
  584. rdev->pm.dpm.dyn_state.cac_tdp_table;
  585. u32 default_pwr_limit =
  586. (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
  587. pi->power_containment_features |= POWERCONTAINMENT_FEATURE_PkgPwrLimit;
  588. ci_set_power_limit(rdev, default_pwr_limit);
  589. }
  590. }
  591. }
  592. } else {
  593. if (pi->caps_power_containment && pi->power_containment_features) {
  594. if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_TDCLimit)
  595. ci_send_msg_to_smc(rdev, PPSMC_MSG_TDCLimitDisable);
  596. if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_BAPM)
  597. ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableDTE);
  598. if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit)
  599. ci_send_msg_to_smc(rdev, PPSMC_MSG_PkgPwrLimitDisable);
  600. pi->power_containment_features = 0;
  601. }
  602. }
  603. return ret;
  604. }
  605. static int ci_enable_smc_cac(struct radeon_device *rdev, bool enable)
  606. {
  607. struct ci_power_info *pi = ci_get_pi(rdev);
  608. PPSMC_Result smc_result;
  609. int ret = 0;
  610. if (pi->caps_cac) {
  611. if (enable) {
  612. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableCac);
  613. if (smc_result != PPSMC_Result_OK) {
  614. ret = -EINVAL;
  615. pi->cac_enabled = false;
  616. } else {
  617. pi->cac_enabled = true;
  618. }
  619. } else if (pi->cac_enabled) {
  620. ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableCac);
  621. pi->cac_enabled = false;
  622. }
  623. }
  624. return ret;
  625. }
  626. static int ci_enable_thermal_based_sclk_dpm(struct radeon_device *rdev,
  627. bool enable)
  628. {
  629. struct ci_power_info *pi = ci_get_pi(rdev);
  630. PPSMC_Result smc_result = PPSMC_Result_OK;
  631. if (pi->thermal_sclk_dpm_enabled) {
  632. if (enable)
  633. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_ENABLE_THERMAL_DPM);
  634. else
  635. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DISABLE_THERMAL_DPM);
  636. }
  637. if (smc_result == PPSMC_Result_OK)
  638. return 0;
  639. else
  640. return -EINVAL;
  641. }
  642. static int ci_power_control_set_level(struct radeon_device *rdev)
  643. {
  644. struct ci_power_info *pi = ci_get_pi(rdev);
  645. struct radeon_cac_tdp_table *cac_tdp_table =
  646. rdev->pm.dpm.dyn_state.cac_tdp_table;
  647. s32 adjust_percent;
  648. s32 target_tdp;
  649. int ret = 0;
  650. bool adjust_polarity = false; /* ??? */
  651. if (pi->caps_power_containment) {
  652. adjust_percent = adjust_polarity ?
  653. rdev->pm.dpm.tdp_adjustment : (-1 * rdev->pm.dpm.tdp_adjustment);
  654. target_tdp = ((100 + adjust_percent) *
  655. (s32)cac_tdp_table->configurable_tdp) / 100;
  656. ret = ci_set_overdrive_target_tdp(rdev, (u32)target_tdp);
  657. }
  658. return ret;
  659. }
  660. void ci_dpm_powergate_uvd(struct radeon_device *rdev, bool gate)
  661. {
  662. struct ci_power_info *pi = ci_get_pi(rdev);
  663. if (pi->uvd_power_gated == gate)
  664. return;
  665. pi->uvd_power_gated = gate;
  666. ci_update_uvd_dpm(rdev, gate);
  667. }
  668. bool ci_dpm_vblank_too_short(struct radeon_device *rdev)
  669. {
  670. struct ci_power_info *pi = ci_get_pi(rdev);
  671. u32 vblank_time = r600_dpm_get_vblank_time(rdev);
  672. u32 switch_limit = pi->mem_gddr5 ? 450 : 300;
  673. /* disable mclk switching if the refresh is >120Hz, even if the
  674. * blanking period would allow it
  675. */
  676. if (r600_dpm_get_vrefresh(rdev) > 120)
  677. return true;
  678. /* disable mclk switching if the refresh is >120Hz, even if the
  679. * blanking period would allow it
  680. */
  681. if (r600_dpm_get_vrefresh(rdev) > 120)
  682. return true;
  683. if (vblank_time < switch_limit)
  684. return true;
  685. else
  686. return false;
  687. }
  688. static void ci_apply_state_adjust_rules(struct radeon_device *rdev,
  689. struct radeon_ps *rps)
  690. {
  691. struct ci_ps *ps = ci_get_ps(rps);
  692. struct ci_power_info *pi = ci_get_pi(rdev);
  693. struct radeon_clock_and_voltage_limits *max_limits;
  694. bool disable_mclk_switching;
  695. u32 sclk, mclk;
  696. int i;
  697. if (rps->vce_active) {
  698. rps->evclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].evclk;
  699. rps->ecclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].ecclk;
  700. } else {
  701. rps->evclk = 0;
  702. rps->ecclk = 0;
  703. }
  704. if ((rdev->pm.dpm.new_active_crtc_count > 1) ||
  705. ci_dpm_vblank_too_short(rdev))
  706. disable_mclk_switching = true;
  707. else
  708. disable_mclk_switching = false;
  709. if ((rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
  710. pi->battery_state = true;
  711. else
  712. pi->battery_state = false;
  713. if (rdev->pm.dpm.ac_power)
  714. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  715. else
  716. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
  717. if (rdev->pm.dpm.ac_power == false) {
  718. for (i = 0; i < ps->performance_level_count; i++) {
  719. if (ps->performance_levels[i].mclk > max_limits->mclk)
  720. ps->performance_levels[i].mclk = max_limits->mclk;
  721. if (ps->performance_levels[i].sclk > max_limits->sclk)
  722. ps->performance_levels[i].sclk = max_limits->sclk;
  723. }
  724. }
  725. /* XXX validate the min clocks required for display */
  726. if (disable_mclk_switching) {
  727. mclk = ps->performance_levels[ps->performance_level_count - 1].mclk;
  728. sclk = ps->performance_levels[0].sclk;
  729. } else {
  730. mclk = ps->performance_levels[0].mclk;
  731. sclk = ps->performance_levels[0].sclk;
  732. }
  733. if (rps->vce_active) {
  734. if (sclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk)
  735. sclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].sclk;
  736. if (mclk < rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk)
  737. mclk = rdev->pm.dpm.vce_states[rdev->pm.dpm.vce_level].mclk;
  738. }
  739. ps->performance_levels[0].sclk = sclk;
  740. ps->performance_levels[0].mclk = mclk;
  741. if (ps->performance_levels[1].sclk < ps->performance_levels[0].sclk)
  742. ps->performance_levels[1].sclk = ps->performance_levels[0].sclk;
  743. if (disable_mclk_switching) {
  744. if (ps->performance_levels[0].mclk < ps->performance_levels[1].mclk)
  745. ps->performance_levels[0].mclk = ps->performance_levels[1].mclk;
  746. } else {
  747. if (ps->performance_levels[1].mclk < ps->performance_levels[0].mclk)
  748. ps->performance_levels[1].mclk = ps->performance_levels[0].mclk;
  749. }
  750. }
  751. static int ci_thermal_set_temperature_range(struct radeon_device *rdev,
  752. int min_temp, int max_temp)
  753. {
  754. int low_temp = 0 * 1000;
  755. int high_temp = 255 * 1000;
  756. u32 tmp;
  757. if (low_temp < min_temp)
  758. low_temp = min_temp;
  759. if (high_temp > max_temp)
  760. high_temp = max_temp;
  761. if (high_temp < low_temp) {
  762. DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
  763. return -EINVAL;
  764. }
  765. tmp = RREG32_SMC(CG_THERMAL_INT);
  766. tmp &= ~(CI_DIG_THERM_INTH_MASK | CI_DIG_THERM_INTL_MASK);
  767. tmp |= CI_DIG_THERM_INTH(high_temp / 1000) |
  768. CI_DIG_THERM_INTL(low_temp / 1000);
  769. WREG32_SMC(CG_THERMAL_INT, tmp);
  770. #if 0
  771. /* XXX: need to figure out how to handle this properly */
  772. tmp = RREG32_SMC(CG_THERMAL_CTRL);
  773. tmp &= DIG_THERM_DPM_MASK;
  774. tmp |= DIG_THERM_DPM(high_temp / 1000);
  775. WREG32_SMC(CG_THERMAL_CTRL, tmp);
  776. #endif
  777. rdev->pm.dpm.thermal.min_temp = low_temp;
  778. rdev->pm.dpm.thermal.max_temp = high_temp;
  779. return 0;
  780. }
  781. static int ci_thermal_enable_alert(struct radeon_device *rdev,
  782. bool enable)
  783. {
  784. u32 thermal_int = RREG32_SMC(CG_THERMAL_INT);
  785. PPSMC_Result result;
  786. if (enable) {
  787. thermal_int &= ~(THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW);
  788. WREG32_SMC(CG_THERMAL_INT, thermal_int);
  789. rdev->irq.dpm_thermal = false;
  790. result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Enable);
  791. if (result != PPSMC_Result_OK) {
  792. DRM_DEBUG_KMS("Could not enable thermal interrupts.\n");
  793. return -EINVAL;
  794. }
  795. } else {
  796. thermal_int |= THERM_INT_MASK_HIGH | THERM_INT_MASK_LOW;
  797. WREG32_SMC(CG_THERMAL_INT, thermal_int);
  798. rdev->irq.dpm_thermal = true;
  799. result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Thermal_Cntl_Disable);
  800. if (result != PPSMC_Result_OK) {
  801. DRM_DEBUG_KMS("Could not disable thermal interrupts.\n");
  802. return -EINVAL;
  803. }
  804. }
  805. return 0;
  806. }
  807. static void ci_fan_ctrl_set_static_mode(struct radeon_device *rdev, u32 mode)
  808. {
  809. struct ci_power_info *pi = ci_get_pi(rdev);
  810. u32 tmp;
  811. if (pi->fan_ctrl_is_in_default_mode) {
  812. tmp = (RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK) >> FDO_PWM_MODE_SHIFT;
  813. pi->fan_ctrl_default_mode = tmp;
  814. tmp = (RREG32_SMC(CG_FDO_CTRL2) & TMIN_MASK) >> TMIN_SHIFT;
  815. pi->t_min = tmp;
  816. pi->fan_ctrl_is_in_default_mode = false;
  817. }
  818. tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
  819. tmp |= TMIN(0);
  820. WREG32_SMC(CG_FDO_CTRL2, tmp);
  821. tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
  822. tmp |= FDO_PWM_MODE(mode);
  823. WREG32_SMC(CG_FDO_CTRL2, tmp);
  824. }
  825. static int ci_thermal_setup_fan_table(struct radeon_device *rdev)
  826. {
  827. struct ci_power_info *pi = ci_get_pi(rdev);
  828. SMU7_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
  829. u32 duty100;
  830. u32 t_diff1, t_diff2, pwm_diff1, pwm_diff2;
  831. u16 fdo_min, slope1, slope2;
  832. u32 reference_clock, tmp;
  833. int ret;
  834. u64 tmp64;
  835. if (!pi->fan_table_start) {
  836. rdev->pm.dpm.fan.ucode_fan_control = false;
  837. return 0;
  838. }
  839. duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
  840. if (duty100 == 0) {
  841. rdev->pm.dpm.fan.ucode_fan_control = false;
  842. return 0;
  843. }
  844. tmp64 = (u64)rdev->pm.dpm.fan.pwm_min * duty100;
  845. do_div(tmp64, 10000);
  846. fdo_min = (u16)tmp64;
  847. t_diff1 = rdev->pm.dpm.fan.t_med - rdev->pm.dpm.fan.t_min;
  848. t_diff2 = rdev->pm.dpm.fan.t_high - rdev->pm.dpm.fan.t_med;
  849. pwm_diff1 = rdev->pm.dpm.fan.pwm_med - rdev->pm.dpm.fan.pwm_min;
  850. pwm_diff2 = rdev->pm.dpm.fan.pwm_high - rdev->pm.dpm.fan.pwm_med;
  851. slope1 = (u16)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
  852. slope2 = (u16)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
  853. fan_table.TempMin = cpu_to_be16((50 + rdev->pm.dpm.fan.t_min) / 100);
  854. fan_table.TempMed = cpu_to_be16((50 + rdev->pm.dpm.fan.t_med) / 100);
  855. fan_table.TempMax = cpu_to_be16((50 + rdev->pm.dpm.fan.t_max) / 100);
  856. fan_table.Slope1 = cpu_to_be16(slope1);
  857. fan_table.Slope2 = cpu_to_be16(slope2);
  858. fan_table.FdoMin = cpu_to_be16(fdo_min);
  859. fan_table.HystDown = cpu_to_be16(rdev->pm.dpm.fan.t_hyst);
  860. fan_table.HystUp = cpu_to_be16(1);
  861. fan_table.HystSlope = cpu_to_be16(1);
  862. fan_table.TempRespLim = cpu_to_be16(5);
  863. reference_clock = radeon_get_xclk(rdev);
  864. fan_table.RefreshPeriod = cpu_to_be32((rdev->pm.dpm.fan.cycle_delay *
  865. reference_clock) / 1600);
  866. fan_table.FdoMax = cpu_to_be16((u16)duty100);
  867. tmp = (RREG32_SMC(CG_MULT_THERMAL_CTRL) & TEMP_SEL_MASK) >> TEMP_SEL_SHIFT;
  868. fan_table.TempSrc = (uint8_t)tmp;
  869. ret = ci_copy_bytes_to_smc(rdev,
  870. pi->fan_table_start,
  871. (u8 *)(&fan_table),
  872. sizeof(fan_table),
  873. pi->sram_end);
  874. if (ret) {
  875. DRM_ERROR("Failed to load fan table to the SMC.");
  876. rdev->pm.dpm.fan.ucode_fan_control = false;
  877. }
  878. return 0;
  879. }
  880. static int ci_fan_ctrl_start_smc_fan_control(struct radeon_device *rdev)
  881. {
  882. struct ci_power_info *pi = ci_get_pi(rdev);
  883. PPSMC_Result ret;
  884. if (pi->caps_od_fuzzy_fan_control_support) {
  885. ret = ci_send_msg_to_smc_with_parameter(rdev,
  886. PPSMC_StartFanControl,
  887. FAN_CONTROL_FUZZY);
  888. if (ret != PPSMC_Result_OK)
  889. return -EINVAL;
  890. ret = ci_send_msg_to_smc_with_parameter(rdev,
  891. PPSMC_MSG_SetFanPwmMax,
  892. rdev->pm.dpm.fan.default_max_fan_pwm);
  893. if (ret != PPSMC_Result_OK)
  894. return -EINVAL;
  895. } else {
  896. ret = ci_send_msg_to_smc_with_parameter(rdev,
  897. PPSMC_StartFanControl,
  898. FAN_CONTROL_TABLE);
  899. if (ret != PPSMC_Result_OK)
  900. return -EINVAL;
  901. }
  902. pi->fan_is_controlled_by_smc = true;
  903. return 0;
  904. }
  905. static int ci_fan_ctrl_stop_smc_fan_control(struct radeon_device *rdev)
  906. {
  907. PPSMC_Result ret;
  908. struct ci_power_info *pi = ci_get_pi(rdev);
  909. ret = ci_send_msg_to_smc(rdev, PPSMC_StopFanControl);
  910. if (ret == PPSMC_Result_OK) {
  911. pi->fan_is_controlled_by_smc = false;
  912. return 0;
  913. } else
  914. return -EINVAL;
  915. }
  916. int ci_fan_ctrl_get_fan_speed_percent(struct radeon_device *rdev,
  917. u32 *speed)
  918. {
  919. u32 duty, duty100;
  920. u64 tmp64;
  921. if (rdev->pm.no_fan)
  922. return -ENOENT;
  923. duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
  924. duty = (RREG32_SMC(CG_THERMAL_STATUS) & FDO_PWM_DUTY_MASK) >> FDO_PWM_DUTY_SHIFT;
  925. if (duty100 == 0)
  926. return -EINVAL;
  927. tmp64 = (u64)duty * 100;
  928. do_div(tmp64, duty100);
  929. *speed = (u32)tmp64;
  930. if (*speed > 100)
  931. *speed = 100;
  932. return 0;
  933. }
  934. int ci_fan_ctrl_set_fan_speed_percent(struct radeon_device *rdev,
  935. u32 speed)
  936. {
  937. u32 tmp;
  938. u32 duty, duty100;
  939. u64 tmp64;
  940. struct ci_power_info *pi = ci_get_pi(rdev);
  941. if (rdev->pm.no_fan)
  942. return -ENOENT;
  943. if (pi->fan_is_controlled_by_smc)
  944. return -EINVAL;
  945. if (speed > 100)
  946. return -EINVAL;
  947. duty100 = (RREG32_SMC(CG_FDO_CTRL1) & FMAX_DUTY100_MASK) >> FMAX_DUTY100_SHIFT;
  948. if (duty100 == 0)
  949. return -EINVAL;
  950. tmp64 = (u64)speed * duty100;
  951. do_div(tmp64, 100);
  952. duty = (u32)tmp64;
  953. tmp = RREG32_SMC(CG_FDO_CTRL0) & ~FDO_STATIC_DUTY_MASK;
  954. tmp |= FDO_STATIC_DUTY(duty);
  955. WREG32_SMC(CG_FDO_CTRL0, tmp);
  956. return 0;
  957. }
  958. void ci_fan_ctrl_set_mode(struct radeon_device *rdev, u32 mode)
  959. {
  960. if (mode) {
  961. /* stop auto-manage */
  962. if (rdev->pm.dpm.fan.ucode_fan_control)
  963. ci_fan_ctrl_stop_smc_fan_control(rdev);
  964. ci_fan_ctrl_set_static_mode(rdev, mode);
  965. } else {
  966. /* restart auto-manage */
  967. if (rdev->pm.dpm.fan.ucode_fan_control)
  968. ci_thermal_start_smc_fan_control(rdev);
  969. else
  970. ci_fan_ctrl_set_default_mode(rdev);
  971. }
  972. }
  973. u32 ci_fan_ctrl_get_mode(struct radeon_device *rdev)
  974. {
  975. struct ci_power_info *pi = ci_get_pi(rdev);
  976. u32 tmp;
  977. if (pi->fan_is_controlled_by_smc)
  978. return 0;
  979. tmp = RREG32_SMC(CG_FDO_CTRL2) & FDO_PWM_MODE_MASK;
  980. return (tmp >> FDO_PWM_MODE_SHIFT);
  981. }
  982. #if 0
  983. static int ci_fan_ctrl_get_fan_speed_rpm(struct radeon_device *rdev,
  984. u32 *speed)
  985. {
  986. u32 tach_period;
  987. u32 xclk = radeon_get_xclk(rdev);
  988. if (rdev->pm.no_fan)
  989. return -ENOENT;
  990. if (rdev->pm.fan_pulses_per_revolution == 0)
  991. return -ENOENT;
  992. tach_period = (RREG32_SMC(CG_TACH_STATUS) & TACH_PERIOD_MASK) >> TACH_PERIOD_SHIFT;
  993. if (tach_period == 0)
  994. return -ENOENT;
  995. *speed = 60 * xclk * 10000 / tach_period;
  996. return 0;
  997. }
  998. static int ci_fan_ctrl_set_fan_speed_rpm(struct radeon_device *rdev,
  999. u32 speed)
  1000. {
  1001. u32 tach_period, tmp;
  1002. u32 xclk = radeon_get_xclk(rdev);
  1003. if (rdev->pm.no_fan)
  1004. return -ENOENT;
  1005. if (rdev->pm.fan_pulses_per_revolution == 0)
  1006. return -ENOENT;
  1007. if ((speed < rdev->pm.fan_min_rpm) ||
  1008. (speed > rdev->pm.fan_max_rpm))
  1009. return -EINVAL;
  1010. if (rdev->pm.dpm.fan.ucode_fan_control)
  1011. ci_fan_ctrl_stop_smc_fan_control(rdev);
  1012. tach_period = 60 * xclk * 10000 / (8 * speed);
  1013. tmp = RREG32_SMC(CG_TACH_CTRL) & ~TARGET_PERIOD_MASK;
  1014. tmp |= TARGET_PERIOD(tach_period);
  1015. WREG32_SMC(CG_TACH_CTRL, tmp);
  1016. ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC_RPM);
  1017. return 0;
  1018. }
  1019. #endif
  1020. static void ci_fan_ctrl_set_default_mode(struct radeon_device *rdev)
  1021. {
  1022. struct ci_power_info *pi = ci_get_pi(rdev);
  1023. u32 tmp;
  1024. if (!pi->fan_ctrl_is_in_default_mode) {
  1025. tmp = RREG32_SMC(CG_FDO_CTRL2) & ~FDO_PWM_MODE_MASK;
  1026. tmp |= FDO_PWM_MODE(pi->fan_ctrl_default_mode);
  1027. WREG32_SMC(CG_FDO_CTRL2, tmp);
  1028. tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TMIN_MASK;
  1029. tmp |= TMIN(pi->t_min);
  1030. WREG32_SMC(CG_FDO_CTRL2, tmp);
  1031. pi->fan_ctrl_is_in_default_mode = true;
  1032. }
  1033. }
  1034. static void ci_thermal_start_smc_fan_control(struct radeon_device *rdev)
  1035. {
  1036. if (rdev->pm.dpm.fan.ucode_fan_control) {
  1037. ci_fan_ctrl_start_smc_fan_control(rdev);
  1038. ci_fan_ctrl_set_static_mode(rdev, FDO_PWM_MODE_STATIC);
  1039. }
  1040. }
  1041. static void ci_thermal_initialize(struct radeon_device *rdev)
  1042. {
  1043. u32 tmp;
  1044. if (rdev->pm.fan_pulses_per_revolution) {
  1045. tmp = RREG32_SMC(CG_TACH_CTRL) & ~EDGE_PER_REV_MASK;
  1046. tmp |= EDGE_PER_REV(rdev->pm.fan_pulses_per_revolution -1);
  1047. WREG32_SMC(CG_TACH_CTRL, tmp);
  1048. }
  1049. tmp = RREG32_SMC(CG_FDO_CTRL2) & ~TACH_PWM_RESP_RATE_MASK;
  1050. tmp |= TACH_PWM_RESP_RATE(0x28);
  1051. WREG32_SMC(CG_FDO_CTRL2, tmp);
  1052. }
  1053. static int ci_thermal_start_thermal_controller(struct radeon_device *rdev)
  1054. {
  1055. int ret;
  1056. ci_thermal_initialize(rdev);
  1057. ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
  1058. if (ret)
  1059. return ret;
  1060. ret = ci_thermal_enable_alert(rdev, true);
  1061. if (ret)
  1062. return ret;
  1063. if (rdev->pm.dpm.fan.ucode_fan_control) {
  1064. ret = ci_thermal_setup_fan_table(rdev);
  1065. if (ret)
  1066. return ret;
  1067. ci_thermal_start_smc_fan_control(rdev);
  1068. }
  1069. return 0;
  1070. }
  1071. static void ci_thermal_stop_thermal_controller(struct radeon_device *rdev)
  1072. {
  1073. if (!rdev->pm.no_fan)
  1074. ci_fan_ctrl_set_default_mode(rdev);
  1075. }
  1076. #if 0
  1077. static int ci_read_smc_soft_register(struct radeon_device *rdev,
  1078. u16 reg_offset, u32 *value)
  1079. {
  1080. struct ci_power_info *pi = ci_get_pi(rdev);
  1081. return ci_read_smc_sram_dword(rdev,
  1082. pi->soft_regs_start + reg_offset,
  1083. value, pi->sram_end);
  1084. }
  1085. #endif
  1086. static int ci_write_smc_soft_register(struct radeon_device *rdev,
  1087. u16 reg_offset, u32 value)
  1088. {
  1089. struct ci_power_info *pi = ci_get_pi(rdev);
  1090. return ci_write_smc_sram_dword(rdev,
  1091. pi->soft_regs_start + reg_offset,
  1092. value, pi->sram_end);
  1093. }
  1094. static void ci_init_fps_limits(struct radeon_device *rdev)
  1095. {
  1096. struct ci_power_info *pi = ci_get_pi(rdev);
  1097. SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
  1098. if (pi->caps_fps) {
  1099. u16 tmp;
  1100. tmp = 45;
  1101. table->FpsHighT = cpu_to_be16(tmp);
  1102. tmp = 30;
  1103. table->FpsLowT = cpu_to_be16(tmp);
  1104. }
  1105. }
  1106. static int ci_update_sclk_t(struct radeon_device *rdev)
  1107. {
  1108. struct ci_power_info *pi = ci_get_pi(rdev);
  1109. int ret = 0;
  1110. u32 low_sclk_interrupt_t = 0;
  1111. if (pi->caps_sclk_throttle_low_notification) {
  1112. low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
  1113. ret = ci_copy_bytes_to_smc(rdev,
  1114. pi->dpm_table_start +
  1115. offsetof(SMU7_Discrete_DpmTable, LowSclkInterruptT),
  1116. (u8 *)&low_sclk_interrupt_t,
  1117. sizeof(u32), pi->sram_end);
  1118. }
  1119. return ret;
  1120. }
  1121. static void ci_get_leakage_voltages(struct radeon_device *rdev)
  1122. {
  1123. struct ci_power_info *pi = ci_get_pi(rdev);
  1124. u16 leakage_id, virtual_voltage_id;
  1125. u16 vddc, vddci;
  1126. int i;
  1127. pi->vddc_leakage.count = 0;
  1128. pi->vddci_leakage.count = 0;
  1129. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_EVV) {
  1130. for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
  1131. virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
  1132. if (radeon_atom_get_voltage_evv(rdev, virtual_voltage_id, &vddc) != 0)
  1133. continue;
  1134. if (vddc != 0 && vddc != virtual_voltage_id) {
  1135. pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
  1136. pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
  1137. pi->vddc_leakage.count++;
  1138. }
  1139. }
  1140. } else if (radeon_atom_get_leakage_id_from_vbios(rdev, &leakage_id) == 0) {
  1141. for (i = 0; i < CISLANDS_MAX_LEAKAGE_COUNT; i++) {
  1142. virtual_voltage_id = ATOM_VIRTUAL_VOLTAGE_ID0 + i;
  1143. if (radeon_atom_get_leakage_vddc_based_on_leakage_params(rdev, &vddc, &vddci,
  1144. virtual_voltage_id,
  1145. leakage_id) == 0) {
  1146. if (vddc != 0 && vddc != virtual_voltage_id) {
  1147. pi->vddc_leakage.actual_voltage[pi->vddc_leakage.count] = vddc;
  1148. pi->vddc_leakage.leakage_id[pi->vddc_leakage.count] = virtual_voltage_id;
  1149. pi->vddc_leakage.count++;
  1150. }
  1151. if (vddci != 0 && vddci != virtual_voltage_id) {
  1152. pi->vddci_leakage.actual_voltage[pi->vddci_leakage.count] = vddci;
  1153. pi->vddci_leakage.leakage_id[pi->vddci_leakage.count] = virtual_voltage_id;
  1154. pi->vddci_leakage.count++;
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. static void ci_set_dpm_event_sources(struct radeon_device *rdev, u32 sources)
  1161. {
  1162. struct ci_power_info *pi = ci_get_pi(rdev);
  1163. bool want_thermal_protection;
  1164. enum radeon_dpm_event_src dpm_event_src;
  1165. u32 tmp;
  1166. switch (sources) {
  1167. case 0:
  1168. default:
  1169. want_thermal_protection = false;
  1170. break;
  1171. case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL):
  1172. want_thermal_protection = true;
  1173. dpm_event_src = RADEON_DPM_EVENT_SRC_DIGITAL;
  1174. break;
  1175. case (1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL):
  1176. want_thermal_protection = true;
  1177. dpm_event_src = RADEON_DPM_EVENT_SRC_EXTERNAL;
  1178. break;
  1179. case ((1 << RADEON_DPM_AUTO_THROTTLE_SRC_EXTERNAL) |
  1180. (1 << RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL)):
  1181. want_thermal_protection = true;
  1182. dpm_event_src = RADEON_DPM_EVENT_SRC_DIGIAL_OR_EXTERNAL;
  1183. break;
  1184. }
  1185. if (want_thermal_protection) {
  1186. #if 0
  1187. /* XXX: need to figure out how to handle this properly */
  1188. tmp = RREG32_SMC(CG_THERMAL_CTRL);
  1189. tmp &= DPM_EVENT_SRC_MASK;
  1190. tmp |= DPM_EVENT_SRC(dpm_event_src);
  1191. WREG32_SMC(CG_THERMAL_CTRL, tmp);
  1192. #endif
  1193. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1194. if (pi->thermal_protection)
  1195. tmp &= ~THERMAL_PROTECTION_DIS;
  1196. else
  1197. tmp |= THERMAL_PROTECTION_DIS;
  1198. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1199. } else {
  1200. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1201. tmp |= THERMAL_PROTECTION_DIS;
  1202. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1203. }
  1204. }
  1205. static void ci_enable_auto_throttle_source(struct radeon_device *rdev,
  1206. enum radeon_dpm_auto_throttle_src source,
  1207. bool enable)
  1208. {
  1209. struct ci_power_info *pi = ci_get_pi(rdev);
  1210. if (enable) {
  1211. if (!(pi->active_auto_throttle_sources & (1 << source))) {
  1212. pi->active_auto_throttle_sources |= 1 << source;
  1213. ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
  1214. }
  1215. } else {
  1216. if (pi->active_auto_throttle_sources & (1 << source)) {
  1217. pi->active_auto_throttle_sources &= ~(1 << source);
  1218. ci_set_dpm_event_sources(rdev, pi->active_auto_throttle_sources);
  1219. }
  1220. }
  1221. }
  1222. static void ci_enable_vr_hot_gpio_interrupt(struct radeon_device *rdev)
  1223. {
  1224. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT)
  1225. ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableVRHotGPIOInterrupt);
  1226. }
  1227. static int ci_unfreeze_sclk_mclk_dpm(struct radeon_device *rdev)
  1228. {
  1229. struct ci_power_info *pi = ci_get_pi(rdev);
  1230. PPSMC_Result smc_result;
  1231. if (!pi->need_update_smu7_dpm_table)
  1232. return 0;
  1233. if ((!pi->sclk_dpm_key_disabled) &&
  1234. (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
  1235. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_UnfreezeLevel);
  1236. if (smc_result != PPSMC_Result_OK)
  1237. return -EINVAL;
  1238. }
  1239. if ((!pi->mclk_dpm_key_disabled) &&
  1240. (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
  1241. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_UnfreezeLevel);
  1242. if (smc_result != PPSMC_Result_OK)
  1243. return -EINVAL;
  1244. }
  1245. pi->need_update_smu7_dpm_table = 0;
  1246. return 0;
  1247. }
  1248. static int ci_enable_sclk_mclk_dpm(struct radeon_device *rdev, bool enable)
  1249. {
  1250. struct ci_power_info *pi = ci_get_pi(rdev);
  1251. PPSMC_Result smc_result;
  1252. if (enable) {
  1253. if (!pi->sclk_dpm_key_disabled) {
  1254. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Enable);
  1255. if (smc_result != PPSMC_Result_OK)
  1256. return -EINVAL;
  1257. }
  1258. if (!pi->mclk_dpm_key_disabled) {
  1259. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Enable);
  1260. if (smc_result != PPSMC_Result_OK)
  1261. return -EINVAL;
  1262. WREG32_P(MC_SEQ_CNTL_3, CAC_EN, ~CAC_EN);
  1263. WREG32_SMC(LCAC_MC0_CNTL, 0x05);
  1264. WREG32_SMC(LCAC_MC1_CNTL, 0x05);
  1265. WREG32_SMC(LCAC_CPL_CNTL, 0x100005);
  1266. udelay(10);
  1267. WREG32_SMC(LCAC_MC0_CNTL, 0x400005);
  1268. WREG32_SMC(LCAC_MC1_CNTL, 0x400005);
  1269. WREG32_SMC(LCAC_CPL_CNTL, 0x500005);
  1270. }
  1271. } else {
  1272. if (!pi->sclk_dpm_key_disabled) {
  1273. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_DPM_Disable);
  1274. if (smc_result != PPSMC_Result_OK)
  1275. return -EINVAL;
  1276. }
  1277. if (!pi->mclk_dpm_key_disabled) {
  1278. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_Disable);
  1279. if (smc_result != PPSMC_Result_OK)
  1280. return -EINVAL;
  1281. }
  1282. }
  1283. return 0;
  1284. }
  1285. static int ci_start_dpm(struct radeon_device *rdev)
  1286. {
  1287. struct ci_power_info *pi = ci_get_pi(rdev);
  1288. PPSMC_Result smc_result;
  1289. int ret;
  1290. u32 tmp;
  1291. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1292. tmp |= GLOBAL_PWRMGT_EN;
  1293. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1294. tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
  1295. tmp |= DYNAMIC_PM_EN;
  1296. WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
  1297. ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VoltageChangeTimeout), 0x1000);
  1298. WREG32_P(BIF_LNCNT_RESET, 0, ~RESET_LNCNT_EN);
  1299. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Enable);
  1300. if (smc_result != PPSMC_Result_OK)
  1301. return -EINVAL;
  1302. ret = ci_enable_sclk_mclk_dpm(rdev, true);
  1303. if (ret)
  1304. return ret;
  1305. if (!pi->pcie_dpm_key_disabled) {
  1306. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Enable);
  1307. if (smc_result != PPSMC_Result_OK)
  1308. return -EINVAL;
  1309. }
  1310. return 0;
  1311. }
  1312. static int ci_freeze_sclk_mclk_dpm(struct radeon_device *rdev)
  1313. {
  1314. struct ci_power_info *pi = ci_get_pi(rdev);
  1315. PPSMC_Result smc_result;
  1316. if (!pi->need_update_smu7_dpm_table)
  1317. return 0;
  1318. if ((!pi->sclk_dpm_key_disabled) &&
  1319. (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK))) {
  1320. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_SCLKDPM_FreezeLevel);
  1321. if (smc_result != PPSMC_Result_OK)
  1322. return -EINVAL;
  1323. }
  1324. if ((!pi->mclk_dpm_key_disabled) &&
  1325. (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)) {
  1326. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_MCLKDPM_FreezeLevel);
  1327. if (smc_result != PPSMC_Result_OK)
  1328. return -EINVAL;
  1329. }
  1330. return 0;
  1331. }
  1332. static int ci_stop_dpm(struct radeon_device *rdev)
  1333. {
  1334. struct ci_power_info *pi = ci_get_pi(rdev);
  1335. PPSMC_Result smc_result;
  1336. int ret;
  1337. u32 tmp;
  1338. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1339. tmp &= ~GLOBAL_PWRMGT_EN;
  1340. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1341. tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
  1342. tmp &= ~DYNAMIC_PM_EN;
  1343. WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
  1344. if (!pi->pcie_dpm_key_disabled) {
  1345. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_PCIeDPM_Disable);
  1346. if (smc_result != PPSMC_Result_OK)
  1347. return -EINVAL;
  1348. }
  1349. ret = ci_enable_sclk_mclk_dpm(rdev, false);
  1350. if (ret)
  1351. return ret;
  1352. smc_result = ci_send_msg_to_smc(rdev, PPSMC_MSG_Voltage_Cntl_Disable);
  1353. if (smc_result != PPSMC_Result_OK)
  1354. return -EINVAL;
  1355. return 0;
  1356. }
  1357. static void ci_enable_sclk_control(struct radeon_device *rdev, bool enable)
  1358. {
  1359. u32 tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
  1360. if (enable)
  1361. tmp &= ~SCLK_PWRMGT_OFF;
  1362. else
  1363. tmp |= SCLK_PWRMGT_OFF;
  1364. WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
  1365. }
  1366. #if 0
  1367. static int ci_notify_hw_of_power_source(struct radeon_device *rdev,
  1368. bool ac_power)
  1369. {
  1370. struct ci_power_info *pi = ci_get_pi(rdev);
  1371. struct radeon_cac_tdp_table *cac_tdp_table =
  1372. rdev->pm.dpm.dyn_state.cac_tdp_table;
  1373. u32 power_limit;
  1374. if (ac_power)
  1375. power_limit = (u32)(cac_tdp_table->maximum_power_delivery_limit * 256);
  1376. else
  1377. power_limit = (u32)(cac_tdp_table->battery_power_limit * 256);
  1378. ci_set_power_limit(rdev, power_limit);
  1379. if (pi->caps_automatic_dc_transition) {
  1380. if (ac_power)
  1381. ci_send_msg_to_smc(rdev, PPSMC_MSG_RunningOnAC);
  1382. else
  1383. ci_send_msg_to_smc(rdev, PPSMC_MSG_Remove_DC_Clamp);
  1384. }
  1385. return 0;
  1386. }
  1387. #endif
  1388. static PPSMC_Result ci_send_msg_to_smc_with_parameter(struct radeon_device *rdev,
  1389. PPSMC_Msg msg, u32 parameter)
  1390. {
  1391. WREG32(SMC_MSG_ARG_0, parameter);
  1392. return ci_send_msg_to_smc(rdev, msg);
  1393. }
  1394. static PPSMC_Result ci_send_msg_to_smc_return_parameter(struct radeon_device *rdev,
  1395. PPSMC_Msg msg, u32 *parameter)
  1396. {
  1397. PPSMC_Result smc_result;
  1398. smc_result = ci_send_msg_to_smc(rdev, msg);
  1399. if ((smc_result == PPSMC_Result_OK) && parameter)
  1400. *parameter = RREG32(SMC_MSG_ARG_0);
  1401. return smc_result;
  1402. }
  1403. static int ci_dpm_force_state_sclk(struct radeon_device *rdev, u32 n)
  1404. {
  1405. struct ci_power_info *pi = ci_get_pi(rdev);
  1406. if (!pi->sclk_dpm_key_disabled) {
  1407. PPSMC_Result smc_result =
  1408. ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_SCLKDPM_SetEnabledMask, 1 << n);
  1409. if (smc_result != PPSMC_Result_OK)
  1410. return -EINVAL;
  1411. }
  1412. return 0;
  1413. }
  1414. static int ci_dpm_force_state_mclk(struct radeon_device *rdev, u32 n)
  1415. {
  1416. struct ci_power_info *pi = ci_get_pi(rdev);
  1417. if (!pi->mclk_dpm_key_disabled) {
  1418. PPSMC_Result smc_result =
  1419. ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_MCLKDPM_SetEnabledMask, 1 << n);
  1420. if (smc_result != PPSMC_Result_OK)
  1421. return -EINVAL;
  1422. }
  1423. return 0;
  1424. }
  1425. static int ci_dpm_force_state_pcie(struct radeon_device *rdev, u32 n)
  1426. {
  1427. struct ci_power_info *pi = ci_get_pi(rdev);
  1428. if (!pi->pcie_dpm_key_disabled) {
  1429. PPSMC_Result smc_result =
  1430. ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PCIeDPM_ForceLevel, n);
  1431. if (smc_result != PPSMC_Result_OK)
  1432. return -EINVAL;
  1433. }
  1434. return 0;
  1435. }
  1436. static int ci_set_power_limit(struct radeon_device *rdev, u32 n)
  1437. {
  1438. struct ci_power_info *pi = ci_get_pi(rdev);
  1439. if (pi->power_containment_features & POWERCONTAINMENT_FEATURE_PkgPwrLimit) {
  1440. PPSMC_Result smc_result =
  1441. ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_PkgPwrSetLimit, n);
  1442. if (smc_result != PPSMC_Result_OK)
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. static int ci_set_overdrive_target_tdp(struct radeon_device *rdev,
  1448. u32 target_tdp)
  1449. {
  1450. PPSMC_Result smc_result =
  1451. ci_send_msg_to_smc_with_parameter(rdev, PPSMC_MSG_OverDriveSetTargetTdp, target_tdp);
  1452. if (smc_result != PPSMC_Result_OK)
  1453. return -EINVAL;
  1454. return 0;
  1455. }
  1456. #if 0
  1457. static int ci_set_boot_state(struct radeon_device *rdev)
  1458. {
  1459. return ci_enable_sclk_mclk_dpm(rdev, false);
  1460. }
  1461. #endif
  1462. static u32 ci_get_average_sclk_freq(struct radeon_device *rdev)
  1463. {
  1464. u32 sclk_freq;
  1465. PPSMC_Result smc_result =
  1466. ci_send_msg_to_smc_return_parameter(rdev,
  1467. PPSMC_MSG_API_GetSclkFrequency,
  1468. &sclk_freq);
  1469. if (smc_result != PPSMC_Result_OK)
  1470. sclk_freq = 0;
  1471. return sclk_freq;
  1472. }
  1473. static u32 ci_get_average_mclk_freq(struct radeon_device *rdev)
  1474. {
  1475. u32 mclk_freq;
  1476. PPSMC_Result smc_result =
  1477. ci_send_msg_to_smc_return_parameter(rdev,
  1478. PPSMC_MSG_API_GetMclkFrequency,
  1479. &mclk_freq);
  1480. if (smc_result != PPSMC_Result_OK)
  1481. mclk_freq = 0;
  1482. return mclk_freq;
  1483. }
  1484. static void ci_dpm_start_smc(struct radeon_device *rdev)
  1485. {
  1486. int i;
  1487. ci_program_jump_on_start(rdev);
  1488. ci_start_smc_clock(rdev);
  1489. ci_start_smc(rdev);
  1490. for (i = 0; i < rdev->usec_timeout; i++) {
  1491. if (RREG32_SMC(FIRMWARE_FLAGS) & INTERRUPTS_ENABLED)
  1492. break;
  1493. }
  1494. }
  1495. static void ci_dpm_stop_smc(struct radeon_device *rdev)
  1496. {
  1497. ci_reset_smc(rdev);
  1498. ci_stop_smc_clock(rdev);
  1499. }
  1500. static int ci_process_firmware_header(struct radeon_device *rdev)
  1501. {
  1502. struct ci_power_info *pi = ci_get_pi(rdev);
  1503. u32 tmp;
  1504. int ret;
  1505. ret = ci_read_smc_sram_dword(rdev,
  1506. SMU7_FIRMWARE_HEADER_LOCATION +
  1507. offsetof(SMU7_Firmware_Header, DpmTable),
  1508. &tmp, pi->sram_end);
  1509. if (ret)
  1510. return ret;
  1511. pi->dpm_table_start = tmp;
  1512. ret = ci_read_smc_sram_dword(rdev,
  1513. SMU7_FIRMWARE_HEADER_LOCATION +
  1514. offsetof(SMU7_Firmware_Header, SoftRegisters),
  1515. &tmp, pi->sram_end);
  1516. if (ret)
  1517. return ret;
  1518. pi->soft_regs_start = tmp;
  1519. ret = ci_read_smc_sram_dword(rdev,
  1520. SMU7_FIRMWARE_HEADER_LOCATION +
  1521. offsetof(SMU7_Firmware_Header, mcRegisterTable),
  1522. &tmp, pi->sram_end);
  1523. if (ret)
  1524. return ret;
  1525. pi->mc_reg_table_start = tmp;
  1526. ret = ci_read_smc_sram_dword(rdev,
  1527. SMU7_FIRMWARE_HEADER_LOCATION +
  1528. offsetof(SMU7_Firmware_Header, FanTable),
  1529. &tmp, pi->sram_end);
  1530. if (ret)
  1531. return ret;
  1532. pi->fan_table_start = tmp;
  1533. ret = ci_read_smc_sram_dword(rdev,
  1534. SMU7_FIRMWARE_HEADER_LOCATION +
  1535. offsetof(SMU7_Firmware_Header, mcArbDramTimingTable),
  1536. &tmp, pi->sram_end);
  1537. if (ret)
  1538. return ret;
  1539. pi->arb_table_start = tmp;
  1540. return 0;
  1541. }
  1542. static void ci_read_clock_registers(struct radeon_device *rdev)
  1543. {
  1544. struct ci_power_info *pi = ci_get_pi(rdev);
  1545. pi->clock_registers.cg_spll_func_cntl =
  1546. RREG32_SMC(CG_SPLL_FUNC_CNTL);
  1547. pi->clock_registers.cg_spll_func_cntl_2 =
  1548. RREG32_SMC(CG_SPLL_FUNC_CNTL_2);
  1549. pi->clock_registers.cg_spll_func_cntl_3 =
  1550. RREG32_SMC(CG_SPLL_FUNC_CNTL_3);
  1551. pi->clock_registers.cg_spll_func_cntl_4 =
  1552. RREG32_SMC(CG_SPLL_FUNC_CNTL_4);
  1553. pi->clock_registers.cg_spll_spread_spectrum =
  1554. RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
  1555. pi->clock_registers.cg_spll_spread_spectrum_2 =
  1556. RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM_2);
  1557. pi->clock_registers.dll_cntl = RREG32(DLL_CNTL);
  1558. pi->clock_registers.mclk_pwrmgt_cntl = RREG32(MCLK_PWRMGT_CNTL);
  1559. pi->clock_registers.mpll_ad_func_cntl = RREG32(MPLL_AD_FUNC_CNTL);
  1560. pi->clock_registers.mpll_dq_func_cntl = RREG32(MPLL_DQ_FUNC_CNTL);
  1561. pi->clock_registers.mpll_func_cntl = RREG32(MPLL_FUNC_CNTL);
  1562. pi->clock_registers.mpll_func_cntl_1 = RREG32(MPLL_FUNC_CNTL_1);
  1563. pi->clock_registers.mpll_func_cntl_2 = RREG32(MPLL_FUNC_CNTL_2);
  1564. pi->clock_registers.mpll_ss1 = RREG32(MPLL_SS1);
  1565. pi->clock_registers.mpll_ss2 = RREG32(MPLL_SS2);
  1566. }
  1567. static void ci_init_sclk_t(struct radeon_device *rdev)
  1568. {
  1569. struct ci_power_info *pi = ci_get_pi(rdev);
  1570. pi->low_sclk_interrupt_t = 0;
  1571. }
  1572. static void ci_enable_thermal_protection(struct radeon_device *rdev,
  1573. bool enable)
  1574. {
  1575. u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
  1576. if (enable)
  1577. tmp &= ~THERMAL_PROTECTION_DIS;
  1578. else
  1579. tmp |= THERMAL_PROTECTION_DIS;
  1580. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1581. }
  1582. static void ci_enable_acpi_power_management(struct radeon_device *rdev)
  1583. {
  1584. u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
  1585. tmp |= STATIC_PM_EN;
  1586. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1587. }
  1588. #if 0
  1589. static int ci_enter_ulp_state(struct radeon_device *rdev)
  1590. {
  1591. WREG32(SMC_MESSAGE_0, PPSMC_MSG_SwitchToMinimumPower);
  1592. udelay(25000);
  1593. return 0;
  1594. }
  1595. static int ci_exit_ulp_state(struct radeon_device *rdev)
  1596. {
  1597. int i;
  1598. WREG32(SMC_MESSAGE_0, PPSMC_MSG_ResumeFromMinimumPower);
  1599. udelay(7000);
  1600. for (i = 0; i < rdev->usec_timeout; i++) {
  1601. if (RREG32(SMC_RESP_0) == 1)
  1602. break;
  1603. udelay(1000);
  1604. }
  1605. return 0;
  1606. }
  1607. #endif
  1608. static int ci_notify_smc_display_change(struct radeon_device *rdev,
  1609. bool has_display)
  1610. {
  1611. PPSMC_Msg msg = has_display ? PPSMC_MSG_HasDisplay : PPSMC_MSG_NoDisplay;
  1612. return (ci_send_msg_to_smc(rdev, msg) == PPSMC_Result_OK) ? 0 : -EINVAL;
  1613. }
  1614. static int ci_enable_ds_master_switch(struct radeon_device *rdev,
  1615. bool enable)
  1616. {
  1617. struct ci_power_info *pi = ci_get_pi(rdev);
  1618. if (enable) {
  1619. if (pi->caps_sclk_ds) {
  1620. if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_ON) != PPSMC_Result_OK)
  1621. return -EINVAL;
  1622. } else {
  1623. if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
  1624. return -EINVAL;
  1625. }
  1626. } else {
  1627. if (pi->caps_sclk_ds) {
  1628. if (ci_send_msg_to_smc(rdev, PPSMC_MSG_MASTER_DeepSleep_OFF) != PPSMC_Result_OK)
  1629. return -EINVAL;
  1630. }
  1631. }
  1632. return 0;
  1633. }
  1634. static void ci_program_display_gap(struct radeon_device *rdev)
  1635. {
  1636. u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
  1637. u32 pre_vbi_time_in_us;
  1638. u32 frame_time_in_us;
  1639. u32 ref_clock = rdev->clock.spll.reference_freq;
  1640. u32 refresh_rate = r600_dpm_get_vrefresh(rdev);
  1641. u32 vblank_time = r600_dpm_get_vblank_time(rdev);
  1642. tmp &= ~DISP_GAP_MASK;
  1643. if (rdev->pm.dpm.new_active_crtc_count > 0)
  1644. tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_VBLANK_OR_WM);
  1645. else
  1646. tmp |= DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE);
  1647. WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
  1648. if (refresh_rate == 0)
  1649. refresh_rate = 60;
  1650. if (vblank_time == 0xffffffff)
  1651. vblank_time = 500;
  1652. frame_time_in_us = 1000000 / refresh_rate;
  1653. pre_vbi_time_in_us =
  1654. frame_time_in_us - 200 - vblank_time;
  1655. tmp = pre_vbi_time_in_us * (ref_clock / 100);
  1656. WREG32_SMC(CG_DISPLAY_GAP_CNTL2, tmp);
  1657. ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, PreVBlankGap), 0x64);
  1658. ci_write_smc_soft_register(rdev, offsetof(SMU7_SoftRegisters, VBlankTimeout), (frame_time_in_us - pre_vbi_time_in_us));
  1659. ci_notify_smc_display_change(rdev, (rdev->pm.dpm.new_active_crtc_count == 1));
  1660. }
  1661. static void ci_enable_spread_spectrum(struct radeon_device *rdev, bool enable)
  1662. {
  1663. struct ci_power_info *pi = ci_get_pi(rdev);
  1664. u32 tmp;
  1665. if (enable) {
  1666. if (pi->caps_sclk_ss_support) {
  1667. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1668. tmp |= DYN_SPREAD_SPECTRUM_EN;
  1669. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1670. }
  1671. } else {
  1672. tmp = RREG32_SMC(CG_SPLL_SPREAD_SPECTRUM);
  1673. tmp &= ~SSEN;
  1674. WREG32_SMC(CG_SPLL_SPREAD_SPECTRUM, tmp);
  1675. tmp = RREG32_SMC(GENERAL_PWRMGT);
  1676. tmp &= ~DYN_SPREAD_SPECTRUM_EN;
  1677. WREG32_SMC(GENERAL_PWRMGT, tmp);
  1678. }
  1679. }
  1680. static void ci_program_sstp(struct radeon_device *rdev)
  1681. {
  1682. WREG32_SMC(CG_SSP, (SSTU(R600_SSTU_DFLT) | SST(R600_SST_DFLT)));
  1683. }
  1684. static void ci_enable_display_gap(struct radeon_device *rdev)
  1685. {
  1686. u32 tmp = RREG32_SMC(CG_DISPLAY_GAP_CNTL);
  1687. tmp &= ~(DISP_GAP_MASK | DISP_GAP_MCHG_MASK);
  1688. tmp |= (DISP_GAP(R600_PM_DISPLAY_GAP_IGNORE) |
  1689. DISP_GAP_MCHG(R600_PM_DISPLAY_GAP_VBLANK));
  1690. WREG32_SMC(CG_DISPLAY_GAP_CNTL, tmp);
  1691. }
  1692. static void ci_program_vc(struct radeon_device *rdev)
  1693. {
  1694. u32 tmp;
  1695. tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
  1696. tmp &= ~(RESET_SCLK_CNT | RESET_BUSY_CNT);
  1697. WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
  1698. WREG32_SMC(CG_FTV_0, CISLANDS_VRC_DFLT0);
  1699. WREG32_SMC(CG_FTV_1, CISLANDS_VRC_DFLT1);
  1700. WREG32_SMC(CG_FTV_2, CISLANDS_VRC_DFLT2);
  1701. WREG32_SMC(CG_FTV_3, CISLANDS_VRC_DFLT3);
  1702. WREG32_SMC(CG_FTV_4, CISLANDS_VRC_DFLT4);
  1703. WREG32_SMC(CG_FTV_5, CISLANDS_VRC_DFLT5);
  1704. WREG32_SMC(CG_FTV_6, CISLANDS_VRC_DFLT6);
  1705. WREG32_SMC(CG_FTV_7, CISLANDS_VRC_DFLT7);
  1706. }
  1707. static void ci_clear_vc(struct radeon_device *rdev)
  1708. {
  1709. u32 tmp;
  1710. tmp = RREG32_SMC(SCLK_PWRMGT_CNTL);
  1711. tmp |= (RESET_SCLK_CNT | RESET_BUSY_CNT);
  1712. WREG32_SMC(SCLK_PWRMGT_CNTL, tmp);
  1713. WREG32_SMC(CG_FTV_0, 0);
  1714. WREG32_SMC(CG_FTV_1, 0);
  1715. WREG32_SMC(CG_FTV_2, 0);
  1716. WREG32_SMC(CG_FTV_3, 0);
  1717. WREG32_SMC(CG_FTV_4, 0);
  1718. WREG32_SMC(CG_FTV_5, 0);
  1719. WREG32_SMC(CG_FTV_6, 0);
  1720. WREG32_SMC(CG_FTV_7, 0);
  1721. }
  1722. static int ci_upload_firmware(struct radeon_device *rdev)
  1723. {
  1724. struct ci_power_info *pi = ci_get_pi(rdev);
  1725. int i, ret;
  1726. for (i = 0; i < rdev->usec_timeout; i++) {
  1727. if (RREG32_SMC(RCU_UC_EVENTS) & BOOT_SEQ_DONE)
  1728. break;
  1729. }
  1730. WREG32_SMC(SMC_SYSCON_MISC_CNTL, 1);
  1731. ci_stop_smc_clock(rdev);
  1732. ci_reset_smc(rdev);
  1733. ret = ci_load_smc_ucode(rdev, pi->sram_end);
  1734. return ret;
  1735. }
  1736. static int ci_get_svi2_voltage_table(struct radeon_device *rdev,
  1737. struct radeon_clock_voltage_dependency_table *voltage_dependency_table,
  1738. struct atom_voltage_table *voltage_table)
  1739. {
  1740. u32 i;
  1741. if (voltage_dependency_table == NULL)
  1742. return -EINVAL;
  1743. voltage_table->mask_low = 0;
  1744. voltage_table->phase_delay = 0;
  1745. voltage_table->count = voltage_dependency_table->count;
  1746. for (i = 0; i < voltage_table->count; i++) {
  1747. voltage_table->entries[i].value = voltage_dependency_table->entries[i].v;
  1748. voltage_table->entries[i].smio_low = 0;
  1749. }
  1750. return 0;
  1751. }
  1752. static int ci_construct_voltage_tables(struct radeon_device *rdev)
  1753. {
  1754. struct ci_power_info *pi = ci_get_pi(rdev);
  1755. int ret;
  1756. if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
  1757. ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDC,
  1758. VOLTAGE_OBJ_GPIO_LUT,
  1759. &pi->vddc_voltage_table);
  1760. if (ret)
  1761. return ret;
  1762. } else if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
  1763. ret = ci_get_svi2_voltage_table(rdev,
  1764. &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
  1765. &pi->vddc_voltage_table);
  1766. if (ret)
  1767. return ret;
  1768. }
  1769. if (pi->vddc_voltage_table.count > SMU7_MAX_LEVELS_VDDC)
  1770. si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDC,
  1771. &pi->vddc_voltage_table);
  1772. if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
  1773. ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_VDDCI,
  1774. VOLTAGE_OBJ_GPIO_LUT,
  1775. &pi->vddci_voltage_table);
  1776. if (ret)
  1777. return ret;
  1778. } else if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
  1779. ret = ci_get_svi2_voltage_table(rdev,
  1780. &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
  1781. &pi->vddci_voltage_table);
  1782. if (ret)
  1783. return ret;
  1784. }
  1785. if (pi->vddci_voltage_table.count > SMU7_MAX_LEVELS_VDDCI)
  1786. si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_VDDCI,
  1787. &pi->vddci_voltage_table);
  1788. if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO) {
  1789. ret = radeon_atom_get_voltage_table(rdev, VOLTAGE_TYPE_MVDDC,
  1790. VOLTAGE_OBJ_GPIO_LUT,
  1791. &pi->mvdd_voltage_table);
  1792. if (ret)
  1793. return ret;
  1794. } else if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
  1795. ret = ci_get_svi2_voltage_table(rdev,
  1796. &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
  1797. &pi->mvdd_voltage_table);
  1798. if (ret)
  1799. return ret;
  1800. }
  1801. if (pi->mvdd_voltage_table.count > SMU7_MAX_LEVELS_MVDD)
  1802. si_trim_voltage_table_to_fit_state_table(rdev, SMU7_MAX_LEVELS_MVDD,
  1803. &pi->mvdd_voltage_table);
  1804. return 0;
  1805. }
  1806. static void ci_populate_smc_voltage_table(struct radeon_device *rdev,
  1807. struct atom_voltage_table_entry *voltage_table,
  1808. SMU7_Discrete_VoltageLevel *smc_voltage_table)
  1809. {
  1810. int ret;
  1811. ret = ci_get_std_voltage_value_sidd(rdev, voltage_table,
  1812. &smc_voltage_table->StdVoltageHiSidd,
  1813. &smc_voltage_table->StdVoltageLoSidd);
  1814. if (ret) {
  1815. smc_voltage_table->StdVoltageHiSidd = voltage_table->value * VOLTAGE_SCALE;
  1816. smc_voltage_table->StdVoltageLoSidd = voltage_table->value * VOLTAGE_SCALE;
  1817. }
  1818. smc_voltage_table->Voltage = cpu_to_be16(voltage_table->value * VOLTAGE_SCALE);
  1819. smc_voltage_table->StdVoltageHiSidd =
  1820. cpu_to_be16(smc_voltage_table->StdVoltageHiSidd);
  1821. smc_voltage_table->StdVoltageLoSidd =
  1822. cpu_to_be16(smc_voltage_table->StdVoltageLoSidd);
  1823. }
  1824. static int ci_populate_smc_vddc_table(struct radeon_device *rdev,
  1825. SMU7_Discrete_DpmTable *table)
  1826. {
  1827. struct ci_power_info *pi = ci_get_pi(rdev);
  1828. unsigned int count;
  1829. table->VddcLevelCount = pi->vddc_voltage_table.count;
  1830. for (count = 0; count < table->VddcLevelCount; count++) {
  1831. ci_populate_smc_voltage_table(rdev,
  1832. &pi->vddc_voltage_table.entries[count],
  1833. &table->VddcLevel[count]);
  1834. if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
  1835. table->VddcLevel[count].Smio |=
  1836. pi->vddc_voltage_table.entries[count].smio_low;
  1837. else
  1838. table->VddcLevel[count].Smio = 0;
  1839. }
  1840. table->VddcLevelCount = cpu_to_be32(table->VddcLevelCount);
  1841. return 0;
  1842. }
  1843. static int ci_populate_smc_vddci_table(struct radeon_device *rdev,
  1844. SMU7_Discrete_DpmTable *table)
  1845. {
  1846. unsigned int count;
  1847. struct ci_power_info *pi = ci_get_pi(rdev);
  1848. table->VddciLevelCount = pi->vddci_voltage_table.count;
  1849. for (count = 0; count < table->VddciLevelCount; count++) {
  1850. ci_populate_smc_voltage_table(rdev,
  1851. &pi->vddci_voltage_table.entries[count],
  1852. &table->VddciLevel[count]);
  1853. if (pi->vddci_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
  1854. table->VddciLevel[count].Smio |=
  1855. pi->vddci_voltage_table.entries[count].smio_low;
  1856. else
  1857. table->VddciLevel[count].Smio = 0;
  1858. }
  1859. table->VddciLevelCount = cpu_to_be32(table->VddciLevelCount);
  1860. return 0;
  1861. }
  1862. static int ci_populate_smc_mvdd_table(struct radeon_device *rdev,
  1863. SMU7_Discrete_DpmTable *table)
  1864. {
  1865. struct ci_power_info *pi = ci_get_pi(rdev);
  1866. unsigned int count;
  1867. table->MvddLevelCount = pi->mvdd_voltage_table.count;
  1868. for (count = 0; count < table->MvddLevelCount; count++) {
  1869. ci_populate_smc_voltage_table(rdev,
  1870. &pi->mvdd_voltage_table.entries[count],
  1871. &table->MvddLevel[count]);
  1872. if (pi->mvdd_control == CISLANDS_VOLTAGE_CONTROL_BY_GPIO)
  1873. table->MvddLevel[count].Smio |=
  1874. pi->mvdd_voltage_table.entries[count].smio_low;
  1875. else
  1876. table->MvddLevel[count].Smio = 0;
  1877. }
  1878. table->MvddLevelCount = cpu_to_be32(table->MvddLevelCount);
  1879. return 0;
  1880. }
  1881. static int ci_populate_smc_voltage_tables(struct radeon_device *rdev,
  1882. SMU7_Discrete_DpmTable *table)
  1883. {
  1884. int ret;
  1885. ret = ci_populate_smc_vddc_table(rdev, table);
  1886. if (ret)
  1887. return ret;
  1888. ret = ci_populate_smc_vddci_table(rdev, table);
  1889. if (ret)
  1890. return ret;
  1891. ret = ci_populate_smc_mvdd_table(rdev, table);
  1892. if (ret)
  1893. return ret;
  1894. return 0;
  1895. }
  1896. static int ci_populate_mvdd_value(struct radeon_device *rdev, u32 mclk,
  1897. SMU7_Discrete_VoltageLevel *voltage)
  1898. {
  1899. struct ci_power_info *pi = ci_get_pi(rdev);
  1900. u32 i = 0;
  1901. if (pi->mvdd_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
  1902. for (i = 0; i < rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count; i++) {
  1903. if (mclk <= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries[i].clk) {
  1904. voltage->Voltage = pi->mvdd_voltage_table.entries[i].value;
  1905. break;
  1906. }
  1907. }
  1908. if (i >= rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.count)
  1909. return -EINVAL;
  1910. }
  1911. return -EINVAL;
  1912. }
  1913. static int ci_get_std_voltage_value_sidd(struct radeon_device *rdev,
  1914. struct atom_voltage_table_entry *voltage_table,
  1915. u16 *std_voltage_hi_sidd, u16 *std_voltage_lo_sidd)
  1916. {
  1917. u16 v_index, idx;
  1918. bool voltage_found = false;
  1919. *std_voltage_hi_sidd = voltage_table->value * VOLTAGE_SCALE;
  1920. *std_voltage_lo_sidd = voltage_table->value * VOLTAGE_SCALE;
  1921. if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries == NULL)
  1922. return -EINVAL;
  1923. if (rdev->pm.dpm.dyn_state.cac_leakage_table.entries) {
  1924. for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
  1925. if (voltage_table->value ==
  1926. rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
  1927. voltage_found = true;
  1928. if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
  1929. idx = v_index;
  1930. else
  1931. idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
  1932. *std_voltage_lo_sidd =
  1933. rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
  1934. *std_voltage_hi_sidd =
  1935. rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
  1936. break;
  1937. }
  1938. }
  1939. if (!voltage_found) {
  1940. for (v_index = 0; (u32)v_index < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; v_index++) {
  1941. if (voltage_table->value <=
  1942. rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[v_index].v) {
  1943. voltage_found = true;
  1944. if ((u32)v_index < rdev->pm.dpm.dyn_state.cac_leakage_table.count)
  1945. idx = v_index;
  1946. else
  1947. idx = rdev->pm.dpm.dyn_state.cac_leakage_table.count - 1;
  1948. *std_voltage_lo_sidd =
  1949. rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].vddc * VOLTAGE_SCALE;
  1950. *std_voltage_hi_sidd =
  1951. rdev->pm.dpm.dyn_state.cac_leakage_table.entries[idx].leakage * VOLTAGE_SCALE;
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. }
  1957. return 0;
  1958. }
  1959. static void ci_populate_phase_value_based_on_sclk(struct radeon_device *rdev,
  1960. const struct radeon_phase_shedding_limits_table *limits,
  1961. u32 sclk,
  1962. u32 *phase_shedding)
  1963. {
  1964. unsigned int i;
  1965. *phase_shedding = 1;
  1966. for (i = 0; i < limits->count; i++) {
  1967. if (sclk < limits->entries[i].sclk) {
  1968. *phase_shedding = i;
  1969. break;
  1970. }
  1971. }
  1972. }
  1973. static void ci_populate_phase_value_based_on_mclk(struct radeon_device *rdev,
  1974. const struct radeon_phase_shedding_limits_table *limits,
  1975. u32 mclk,
  1976. u32 *phase_shedding)
  1977. {
  1978. unsigned int i;
  1979. *phase_shedding = 1;
  1980. for (i = 0; i < limits->count; i++) {
  1981. if (mclk < limits->entries[i].mclk) {
  1982. *phase_shedding = i;
  1983. break;
  1984. }
  1985. }
  1986. }
  1987. static int ci_init_arb_table_index(struct radeon_device *rdev)
  1988. {
  1989. struct ci_power_info *pi = ci_get_pi(rdev);
  1990. u32 tmp;
  1991. int ret;
  1992. ret = ci_read_smc_sram_dword(rdev, pi->arb_table_start,
  1993. &tmp, pi->sram_end);
  1994. if (ret)
  1995. return ret;
  1996. tmp &= 0x00FFFFFF;
  1997. tmp |= MC_CG_ARB_FREQ_F1 << 24;
  1998. return ci_write_smc_sram_dword(rdev, pi->arb_table_start,
  1999. tmp, pi->sram_end);
  2000. }
  2001. static int ci_get_dependency_volt_by_clk(struct radeon_device *rdev,
  2002. struct radeon_clock_voltage_dependency_table *allowed_clock_voltage_table,
  2003. u32 clock, u32 *voltage)
  2004. {
  2005. u32 i = 0;
  2006. if (allowed_clock_voltage_table->count == 0)
  2007. return -EINVAL;
  2008. for (i = 0; i < allowed_clock_voltage_table->count; i++) {
  2009. if (allowed_clock_voltage_table->entries[i].clk >= clock) {
  2010. *voltage = allowed_clock_voltage_table->entries[i].v;
  2011. return 0;
  2012. }
  2013. }
  2014. *voltage = allowed_clock_voltage_table->entries[i-1].v;
  2015. return 0;
  2016. }
  2017. static u8 ci_get_sleep_divider_id_from_clock(struct radeon_device *rdev,
  2018. u32 sclk, u32 min_sclk_in_sr)
  2019. {
  2020. u32 i;
  2021. u32 tmp;
  2022. u32 min = (min_sclk_in_sr > CISLAND_MINIMUM_ENGINE_CLOCK) ?
  2023. min_sclk_in_sr : CISLAND_MINIMUM_ENGINE_CLOCK;
  2024. if (sclk < min)
  2025. return 0;
  2026. for (i = CISLAND_MAX_DEEPSLEEP_DIVIDER_ID; ; i--) {
  2027. tmp = sclk / (1 << i);
  2028. if (tmp >= min || i == 0)
  2029. break;
  2030. }
  2031. return (u8)i;
  2032. }
  2033. static int ci_initial_switch_from_arb_f0_to_f1(struct radeon_device *rdev)
  2034. {
  2035. return ni_copy_and_switch_arb_sets(rdev, MC_CG_ARB_FREQ_F0, MC_CG_ARB_FREQ_F1);
  2036. }
  2037. static int ci_reset_to_default(struct radeon_device *rdev)
  2038. {
  2039. return (ci_send_msg_to_smc(rdev, PPSMC_MSG_ResetToDefaults) == PPSMC_Result_OK) ?
  2040. 0 : -EINVAL;
  2041. }
  2042. static int ci_force_switch_to_arb_f0(struct radeon_device *rdev)
  2043. {
  2044. u32 tmp;
  2045. tmp = (RREG32_SMC(SMC_SCRATCH9) & 0x0000ff00) >> 8;
  2046. if (tmp == MC_CG_ARB_FREQ_F0)
  2047. return 0;
  2048. return ni_copy_and_switch_arb_sets(rdev, tmp, MC_CG_ARB_FREQ_F0);
  2049. }
  2050. static void ci_register_patching_mc_arb(struct radeon_device *rdev,
  2051. const u32 engine_clock,
  2052. const u32 memory_clock,
  2053. u32 *dram_timimg2)
  2054. {
  2055. bool patch;
  2056. u32 tmp, tmp2;
  2057. tmp = RREG32(MC_SEQ_MISC0);
  2058. patch = ((tmp & 0x0000f00) == 0x300) ? true : false;
  2059. if (patch &&
  2060. ((rdev->pdev->device == 0x67B0) ||
  2061. (rdev->pdev->device == 0x67B1))) {
  2062. if ((memory_clock > 100000) && (memory_clock <= 125000)) {
  2063. tmp2 = (((0x31 * engine_clock) / 125000) - 1) & 0xff;
  2064. *dram_timimg2 &= ~0x00ff0000;
  2065. *dram_timimg2 |= tmp2 << 16;
  2066. } else if ((memory_clock > 125000) && (memory_clock <= 137500)) {
  2067. tmp2 = (((0x36 * engine_clock) / 137500) - 1) & 0xff;
  2068. *dram_timimg2 &= ~0x00ff0000;
  2069. *dram_timimg2 |= tmp2 << 16;
  2070. }
  2071. }
  2072. }
  2073. static int ci_populate_memory_timing_parameters(struct radeon_device *rdev,
  2074. u32 sclk,
  2075. u32 mclk,
  2076. SMU7_Discrete_MCArbDramTimingTableEntry *arb_regs)
  2077. {
  2078. u32 dram_timing;
  2079. u32 dram_timing2;
  2080. u32 burst_time;
  2081. radeon_atom_set_engine_dram_timings(rdev, sclk, mclk);
  2082. dram_timing = RREG32(MC_ARB_DRAM_TIMING);
  2083. dram_timing2 = RREG32(MC_ARB_DRAM_TIMING2);
  2084. burst_time = RREG32(MC_ARB_BURST_TIME) & STATE0_MASK;
  2085. ci_register_patching_mc_arb(rdev, sclk, mclk, &dram_timing2);
  2086. arb_regs->McArbDramTiming = cpu_to_be32(dram_timing);
  2087. arb_regs->McArbDramTiming2 = cpu_to_be32(dram_timing2);
  2088. arb_regs->McArbBurstTime = (u8)burst_time;
  2089. return 0;
  2090. }
  2091. static int ci_do_program_memory_timing_parameters(struct radeon_device *rdev)
  2092. {
  2093. struct ci_power_info *pi = ci_get_pi(rdev);
  2094. SMU7_Discrete_MCArbDramTimingTable arb_regs;
  2095. u32 i, j;
  2096. int ret = 0;
  2097. memset(&arb_regs, 0, sizeof(SMU7_Discrete_MCArbDramTimingTable));
  2098. for (i = 0; i < pi->dpm_table.sclk_table.count; i++) {
  2099. for (j = 0; j < pi->dpm_table.mclk_table.count; j++) {
  2100. ret = ci_populate_memory_timing_parameters(rdev,
  2101. pi->dpm_table.sclk_table.dpm_levels[i].value,
  2102. pi->dpm_table.mclk_table.dpm_levels[j].value,
  2103. &arb_regs.entries[i][j]);
  2104. if (ret)
  2105. break;
  2106. }
  2107. }
  2108. if (ret == 0)
  2109. ret = ci_copy_bytes_to_smc(rdev,
  2110. pi->arb_table_start,
  2111. (u8 *)&arb_regs,
  2112. sizeof(SMU7_Discrete_MCArbDramTimingTable),
  2113. pi->sram_end);
  2114. return ret;
  2115. }
  2116. static int ci_program_memory_timing_parameters(struct radeon_device *rdev)
  2117. {
  2118. struct ci_power_info *pi = ci_get_pi(rdev);
  2119. if (pi->need_update_smu7_dpm_table == 0)
  2120. return 0;
  2121. return ci_do_program_memory_timing_parameters(rdev);
  2122. }
  2123. static void ci_populate_smc_initial_state(struct radeon_device *rdev,
  2124. struct radeon_ps *radeon_boot_state)
  2125. {
  2126. struct ci_ps *boot_state = ci_get_ps(radeon_boot_state);
  2127. struct ci_power_info *pi = ci_get_pi(rdev);
  2128. u32 level = 0;
  2129. for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count; level++) {
  2130. if (rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[level].clk >=
  2131. boot_state->performance_levels[0].sclk) {
  2132. pi->smc_state_table.GraphicsBootLevel = level;
  2133. break;
  2134. }
  2135. }
  2136. for (level = 0; level < rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.count; level++) {
  2137. if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries[level].clk >=
  2138. boot_state->performance_levels[0].mclk) {
  2139. pi->smc_state_table.MemoryBootLevel = level;
  2140. break;
  2141. }
  2142. }
  2143. }
  2144. static u32 ci_get_dpm_level_enable_mask_value(struct ci_single_dpm_table *dpm_table)
  2145. {
  2146. u32 i;
  2147. u32 mask_value = 0;
  2148. for (i = dpm_table->count; i > 0; i--) {
  2149. mask_value = mask_value << 1;
  2150. if (dpm_table->dpm_levels[i-1].enabled)
  2151. mask_value |= 0x1;
  2152. else
  2153. mask_value &= 0xFFFFFFFE;
  2154. }
  2155. return mask_value;
  2156. }
  2157. static void ci_populate_smc_link_level(struct radeon_device *rdev,
  2158. SMU7_Discrete_DpmTable *table)
  2159. {
  2160. struct ci_power_info *pi = ci_get_pi(rdev);
  2161. struct ci_dpm_table *dpm_table = &pi->dpm_table;
  2162. u32 i;
  2163. for (i = 0; i < dpm_table->pcie_speed_table.count; i++) {
  2164. table->LinkLevel[i].PcieGenSpeed =
  2165. (u8)dpm_table->pcie_speed_table.dpm_levels[i].value;
  2166. table->LinkLevel[i].PcieLaneCount =
  2167. r600_encode_pci_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
  2168. table->LinkLevel[i].EnabledForActivity = 1;
  2169. table->LinkLevel[i].DownT = cpu_to_be32(5);
  2170. table->LinkLevel[i].UpT = cpu_to_be32(30);
  2171. }
  2172. pi->smc_state_table.LinkLevelCount = (u8)dpm_table->pcie_speed_table.count;
  2173. pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
  2174. ci_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
  2175. }
  2176. static int ci_populate_smc_uvd_level(struct radeon_device *rdev,
  2177. SMU7_Discrete_DpmTable *table)
  2178. {
  2179. u32 count;
  2180. struct atom_clock_dividers dividers;
  2181. int ret = -EINVAL;
  2182. table->UvdLevelCount =
  2183. rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count;
  2184. for (count = 0; count < table->UvdLevelCount; count++) {
  2185. table->UvdLevel[count].VclkFrequency =
  2186. rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].vclk;
  2187. table->UvdLevel[count].DclkFrequency =
  2188. rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].dclk;
  2189. table->UvdLevel[count].MinVddc =
  2190. rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
  2191. table->UvdLevel[count].MinVddcPhases = 1;
  2192. ret = radeon_atom_get_clock_dividers(rdev,
  2193. COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  2194. table->UvdLevel[count].VclkFrequency, false, &dividers);
  2195. if (ret)
  2196. return ret;
  2197. table->UvdLevel[count].VclkDivider = (u8)dividers.post_divider;
  2198. ret = radeon_atom_get_clock_dividers(rdev,
  2199. COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  2200. table->UvdLevel[count].DclkFrequency, false, &dividers);
  2201. if (ret)
  2202. return ret;
  2203. table->UvdLevel[count].DclkDivider = (u8)dividers.post_divider;
  2204. table->UvdLevel[count].VclkFrequency = cpu_to_be32(table->UvdLevel[count].VclkFrequency);
  2205. table->UvdLevel[count].DclkFrequency = cpu_to_be32(table->UvdLevel[count].DclkFrequency);
  2206. table->UvdLevel[count].MinVddc = cpu_to_be16(table->UvdLevel[count].MinVddc);
  2207. }
  2208. return ret;
  2209. }
  2210. static int ci_populate_smc_vce_level(struct radeon_device *rdev,
  2211. SMU7_Discrete_DpmTable *table)
  2212. {
  2213. u32 count;
  2214. struct atom_clock_dividers dividers;
  2215. int ret = -EINVAL;
  2216. table->VceLevelCount =
  2217. rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count;
  2218. for (count = 0; count < table->VceLevelCount; count++) {
  2219. table->VceLevel[count].Frequency =
  2220. rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].evclk;
  2221. table->VceLevel[count].MinVoltage =
  2222. (u16)rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
  2223. table->VceLevel[count].MinPhases = 1;
  2224. ret = radeon_atom_get_clock_dividers(rdev,
  2225. COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  2226. table->VceLevel[count].Frequency, false, &dividers);
  2227. if (ret)
  2228. return ret;
  2229. table->VceLevel[count].Divider = (u8)dividers.post_divider;
  2230. table->VceLevel[count].Frequency = cpu_to_be32(table->VceLevel[count].Frequency);
  2231. table->VceLevel[count].MinVoltage = cpu_to_be16(table->VceLevel[count].MinVoltage);
  2232. }
  2233. return ret;
  2234. }
  2235. static int ci_populate_smc_acp_level(struct radeon_device *rdev,
  2236. SMU7_Discrete_DpmTable *table)
  2237. {
  2238. u32 count;
  2239. struct atom_clock_dividers dividers;
  2240. int ret = -EINVAL;
  2241. table->AcpLevelCount = (u8)
  2242. (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count);
  2243. for (count = 0; count < table->AcpLevelCount; count++) {
  2244. table->AcpLevel[count].Frequency =
  2245. rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].clk;
  2246. table->AcpLevel[count].MinVoltage =
  2247. rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[count].v;
  2248. table->AcpLevel[count].MinPhases = 1;
  2249. ret = radeon_atom_get_clock_dividers(rdev,
  2250. COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  2251. table->AcpLevel[count].Frequency, false, &dividers);
  2252. if (ret)
  2253. return ret;
  2254. table->AcpLevel[count].Divider = (u8)dividers.post_divider;
  2255. table->AcpLevel[count].Frequency = cpu_to_be32(table->AcpLevel[count].Frequency);
  2256. table->AcpLevel[count].MinVoltage = cpu_to_be16(table->AcpLevel[count].MinVoltage);
  2257. }
  2258. return ret;
  2259. }
  2260. static int ci_populate_smc_samu_level(struct radeon_device *rdev,
  2261. SMU7_Discrete_DpmTable *table)
  2262. {
  2263. u32 count;
  2264. struct atom_clock_dividers dividers;
  2265. int ret = -EINVAL;
  2266. table->SamuLevelCount =
  2267. rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count;
  2268. for (count = 0; count < table->SamuLevelCount; count++) {
  2269. table->SamuLevel[count].Frequency =
  2270. rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].clk;
  2271. table->SamuLevel[count].MinVoltage =
  2272. rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[count].v * VOLTAGE_SCALE;
  2273. table->SamuLevel[count].MinPhases = 1;
  2274. ret = radeon_atom_get_clock_dividers(rdev,
  2275. COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK,
  2276. table->SamuLevel[count].Frequency, false, &dividers);
  2277. if (ret)
  2278. return ret;
  2279. table->SamuLevel[count].Divider = (u8)dividers.post_divider;
  2280. table->SamuLevel[count].Frequency = cpu_to_be32(table->SamuLevel[count].Frequency);
  2281. table->SamuLevel[count].MinVoltage = cpu_to_be16(table->SamuLevel[count].MinVoltage);
  2282. }
  2283. return ret;
  2284. }
  2285. static int ci_calculate_mclk_params(struct radeon_device *rdev,
  2286. u32 memory_clock,
  2287. SMU7_Discrete_MemoryLevel *mclk,
  2288. bool strobe_mode,
  2289. bool dll_state_on)
  2290. {
  2291. struct ci_power_info *pi = ci_get_pi(rdev);
  2292. u32 dll_cntl = pi->clock_registers.dll_cntl;
  2293. u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
  2294. u32 mpll_ad_func_cntl = pi->clock_registers.mpll_ad_func_cntl;
  2295. u32 mpll_dq_func_cntl = pi->clock_registers.mpll_dq_func_cntl;
  2296. u32 mpll_func_cntl = pi->clock_registers.mpll_func_cntl;
  2297. u32 mpll_func_cntl_1 = pi->clock_registers.mpll_func_cntl_1;
  2298. u32 mpll_func_cntl_2 = pi->clock_registers.mpll_func_cntl_2;
  2299. u32 mpll_ss1 = pi->clock_registers.mpll_ss1;
  2300. u32 mpll_ss2 = pi->clock_registers.mpll_ss2;
  2301. struct atom_mpll_param mpll_param;
  2302. int ret;
  2303. ret = radeon_atom_get_memory_pll_dividers(rdev, memory_clock, strobe_mode, &mpll_param);
  2304. if (ret)
  2305. return ret;
  2306. mpll_func_cntl &= ~BWCTRL_MASK;
  2307. mpll_func_cntl |= BWCTRL(mpll_param.bwcntl);
  2308. mpll_func_cntl_1 &= ~(CLKF_MASK | CLKFRAC_MASK | VCO_MODE_MASK);
  2309. mpll_func_cntl_1 |= CLKF(mpll_param.clkf) |
  2310. CLKFRAC(mpll_param.clkfrac) | VCO_MODE(mpll_param.vco_mode);
  2311. mpll_ad_func_cntl &= ~YCLK_POST_DIV_MASK;
  2312. mpll_ad_func_cntl |= YCLK_POST_DIV(mpll_param.post_div);
  2313. if (pi->mem_gddr5) {
  2314. mpll_dq_func_cntl &= ~(YCLK_SEL_MASK | YCLK_POST_DIV_MASK);
  2315. mpll_dq_func_cntl |= YCLK_SEL(mpll_param.yclk_sel) |
  2316. YCLK_POST_DIV(mpll_param.post_div);
  2317. }
  2318. if (pi->caps_mclk_ss_support) {
  2319. struct radeon_atom_ss ss;
  2320. u32 freq_nom;
  2321. u32 tmp;
  2322. u32 reference_clock = rdev->clock.mpll.reference_freq;
  2323. if (mpll_param.qdr == 1)
  2324. freq_nom = memory_clock * 4 * (1 << mpll_param.post_div);
  2325. else
  2326. freq_nom = memory_clock * 2 * (1 << mpll_param.post_div);
  2327. tmp = (freq_nom / reference_clock);
  2328. tmp = tmp * tmp;
  2329. if (radeon_atombios_get_asic_ss_info(rdev, &ss,
  2330. ASIC_INTERNAL_MEMORY_SS, freq_nom)) {
  2331. u32 clks = reference_clock * 5 / ss.rate;
  2332. u32 clkv = (u32)((((131 * ss.percentage * ss.rate) / 100) * tmp) / freq_nom);
  2333. mpll_ss1 &= ~CLKV_MASK;
  2334. mpll_ss1 |= CLKV(clkv);
  2335. mpll_ss2 &= ~CLKS_MASK;
  2336. mpll_ss2 |= CLKS(clks);
  2337. }
  2338. }
  2339. mclk_pwrmgt_cntl &= ~DLL_SPEED_MASK;
  2340. mclk_pwrmgt_cntl |= DLL_SPEED(mpll_param.dll_speed);
  2341. if (dll_state_on)
  2342. mclk_pwrmgt_cntl |= MRDCK0_PDNB | MRDCK1_PDNB;
  2343. else
  2344. mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
  2345. mclk->MclkFrequency = memory_clock;
  2346. mclk->MpllFuncCntl = mpll_func_cntl;
  2347. mclk->MpllFuncCntl_1 = mpll_func_cntl_1;
  2348. mclk->MpllFuncCntl_2 = mpll_func_cntl_2;
  2349. mclk->MpllAdFuncCntl = mpll_ad_func_cntl;
  2350. mclk->MpllDqFuncCntl = mpll_dq_func_cntl;
  2351. mclk->MclkPwrmgtCntl = mclk_pwrmgt_cntl;
  2352. mclk->DllCntl = dll_cntl;
  2353. mclk->MpllSs1 = mpll_ss1;
  2354. mclk->MpllSs2 = mpll_ss2;
  2355. return 0;
  2356. }
  2357. static int ci_populate_single_memory_level(struct radeon_device *rdev,
  2358. u32 memory_clock,
  2359. SMU7_Discrete_MemoryLevel *memory_level)
  2360. {
  2361. struct ci_power_info *pi = ci_get_pi(rdev);
  2362. int ret;
  2363. bool dll_state_on;
  2364. if (rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk.entries) {
  2365. ret = ci_get_dependency_volt_by_clk(rdev,
  2366. &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk,
  2367. memory_clock, &memory_level->MinVddc);
  2368. if (ret)
  2369. return ret;
  2370. }
  2371. if (rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk.entries) {
  2372. ret = ci_get_dependency_volt_by_clk(rdev,
  2373. &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk,
  2374. memory_clock, &memory_level->MinVddci);
  2375. if (ret)
  2376. return ret;
  2377. }
  2378. if (rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk.entries) {
  2379. ret = ci_get_dependency_volt_by_clk(rdev,
  2380. &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk,
  2381. memory_clock, &memory_level->MinMvdd);
  2382. if (ret)
  2383. return ret;
  2384. }
  2385. memory_level->MinVddcPhases = 1;
  2386. if (pi->vddc_phase_shed_control)
  2387. ci_populate_phase_value_based_on_mclk(rdev,
  2388. &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
  2389. memory_clock,
  2390. &memory_level->MinVddcPhases);
  2391. memory_level->EnabledForThrottle = 1;
  2392. memory_level->UpH = 0;
  2393. memory_level->DownH = 100;
  2394. memory_level->VoltageDownH = 0;
  2395. memory_level->ActivityLevel = (u16)pi->mclk_activity_target;
  2396. memory_level->StutterEnable = false;
  2397. memory_level->StrobeEnable = false;
  2398. memory_level->EdcReadEnable = false;
  2399. memory_level->EdcWriteEnable = false;
  2400. memory_level->RttEnable = false;
  2401. memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  2402. if (pi->mclk_stutter_mode_threshold &&
  2403. (memory_clock <= pi->mclk_stutter_mode_threshold) &&
  2404. (pi->uvd_enabled == false) &&
  2405. (RREG32(DPG_PIPE_STUTTER_CONTROL) & STUTTER_ENABLE) &&
  2406. (rdev->pm.dpm.new_active_crtc_count <= 2))
  2407. memory_level->StutterEnable = true;
  2408. if (pi->mclk_strobe_mode_threshold &&
  2409. (memory_clock <= pi->mclk_strobe_mode_threshold))
  2410. memory_level->StrobeEnable = 1;
  2411. if (pi->mem_gddr5) {
  2412. memory_level->StrobeRatio =
  2413. si_get_mclk_frequency_ratio(memory_clock, memory_level->StrobeEnable);
  2414. if (pi->mclk_edc_enable_threshold &&
  2415. (memory_clock > pi->mclk_edc_enable_threshold))
  2416. memory_level->EdcReadEnable = true;
  2417. if (pi->mclk_edc_wr_enable_threshold &&
  2418. (memory_clock > pi->mclk_edc_wr_enable_threshold))
  2419. memory_level->EdcWriteEnable = true;
  2420. if (memory_level->StrobeEnable) {
  2421. if (si_get_mclk_frequency_ratio(memory_clock, true) >=
  2422. ((RREG32(MC_SEQ_MISC7) >> 16) & 0xf))
  2423. dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
  2424. else
  2425. dll_state_on = ((RREG32(MC_SEQ_MISC6) >> 1) & 0x1) ? true : false;
  2426. } else {
  2427. dll_state_on = pi->dll_default_on;
  2428. }
  2429. } else {
  2430. memory_level->StrobeRatio = si_get_ddr3_mclk_frequency_ratio(memory_clock);
  2431. dll_state_on = ((RREG32(MC_SEQ_MISC5) >> 1) & 0x1) ? true : false;
  2432. }
  2433. ret = ci_calculate_mclk_params(rdev, memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
  2434. if (ret)
  2435. return ret;
  2436. memory_level->MinVddc = cpu_to_be32(memory_level->MinVddc * VOLTAGE_SCALE);
  2437. memory_level->MinVddcPhases = cpu_to_be32(memory_level->MinVddcPhases);
  2438. memory_level->MinVddci = cpu_to_be32(memory_level->MinVddci * VOLTAGE_SCALE);
  2439. memory_level->MinMvdd = cpu_to_be32(memory_level->MinMvdd * VOLTAGE_SCALE);
  2440. memory_level->MclkFrequency = cpu_to_be32(memory_level->MclkFrequency);
  2441. memory_level->ActivityLevel = cpu_to_be16(memory_level->ActivityLevel);
  2442. memory_level->MpllFuncCntl = cpu_to_be32(memory_level->MpllFuncCntl);
  2443. memory_level->MpllFuncCntl_1 = cpu_to_be32(memory_level->MpllFuncCntl_1);
  2444. memory_level->MpllFuncCntl_2 = cpu_to_be32(memory_level->MpllFuncCntl_2);
  2445. memory_level->MpllAdFuncCntl = cpu_to_be32(memory_level->MpllAdFuncCntl);
  2446. memory_level->MpllDqFuncCntl = cpu_to_be32(memory_level->MpllDqFuncCntl);
  2447. memory_level->MclkPwrmgtCntl = cpu_to_be32(memory_level->MclkPwrmgtCntl);
  2448. memory_level->DllCntl = cpu_to_be32(memory_level->DllCntl);
  2449. memory_level->MpllSs1 = cpu_to_be32(memory_level->MpllSs1);
  2450. memory_level->MpllSs2 = cpu_to_be32(memory_level->MpllSs2);
  2451. return 0;
  2452. }
  2453. static int ci_populate_smc_acpi_level(struct radeon_device *rdev,
  2454. SMU7_Discrete_DpmTable *table)
  2455. {
  2456. struct ci_power_info *pi = ci_get_pi(rdev);
  2457. struct atom_clock_dividers dividers;
  2458. SMU7_Discrete_VoltageLevel voltage_level;
  2459. u32 spll_func_cntl = pi->clock_registers.cg_spll_func_cntl;
  2460. u32 spll_func_cntl_2 = pi->clock_registers.cg_spll_func_cntl_2;
  2461. u32 dll_cntl = pi->clock_registers.dll_cntl;
  2462. u32 mclk_pwrmgt_cntl = pi->clock_registers.mclk_pwrmgt_cntl;
  2463. int ret;
  2464. table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
  2465. if (pi->acpi_vddc)
  2466. table->ACPILevel.MinVddc = cpu_to_be32(pi->acpi_vddc * VOLTAGE_SCALE);
  2467. else
  2468. table->ACPILevel.MinVddc = cpu_to_be32(pi->min_vddc_in_pp_table * VOLTAGE_SCALE);
  2469. table->ACPILevel.MinVddcPhases = pi->vddc_phase_shed_control ? 0 : 1;
  2470. table->ACPILevel.SclkFrequency = rdev->clock.spll.reference_freq;
  2471. ret = radeon_atom_get_clock_dividers(rdev,
  2472. COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
  2473. table->ACPILevel.SclkFrequency, false, &dividers);
  2474. if (ret)
  2475. return ret;
  2476. table->ACPILevel.SclkDid = (u8)dividers.post_divider;
  2477. table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  2478. table->ACPILevel.DeepSleepDivId = 0;
  2479. spll_func_cntl &= ~SPLL_PWRON;
  2480. spll_func_cntl |= SPLL_RESET;
  2481. spll_func_cntl_2 &= ~SCLK_MUX_SEL_MASK;
  2482. spll_func_cntl_2 |= SCLK_MUX_SEL(4);
  2483. table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
  2484. table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
  2485. table->ACPILevel.CgSpllFuncCntl3 = pi->clock_registers.cg_spll_func_cntl_3;
  2486. table->ACPILevel.CgSpllFuncCntl4 = pi->clock_registers.cg_spll_func_cntl_4;
  2487. table->ACPILevel.SpllSpreadSpectrum = pi->clock_registers.cg_spll_spread_spectrum;
  2488. table->ACPILevel.SpllSpreadSpectrum2 = pi->clock_registers.cg_spll_spread_spectrum_2;
  2489. table->ACPILevel.CcPwrDynRm = 0;
  2490. table->ACPILevel.CcPwrDynRm1 = 0;
  2491. table->ACPILevel.Flags = cpu_to_be32(table->ACPILevel.Flags);
  2492. table->ACPILevel.MinVddcPhases = cpu_to_be32(table->ACPILevel.MinVddcPhases);
  2493. table->ACPILevel.SclkFrequency = cpu_to_be32(table->ACPILevel.SclkFrequency);
  2494. table->ACPILevel.CgSpllFuncCntl = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl);
  2495. table->ACPILevel.CgSpllFuncCntl2 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl2);
  2496. table->ACPILevel.CgSpllFuncCntl3 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl3);
  2497. table->ACPILevel.CgSpllFuncCntl4 = cpu_to_be32(table->ACPILevel.CgSpllFuncCntl4);
  2498. table->ACPILevel.SpllSpreadSpectrum = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum);
  2499. table->ACPILevel.SpllSpreadSpectrum2 = cpu_to_be32(table->ACPILevel.SpllSpreadSpectrum2);
  2500. table->ACPILevel.CcPwrDynRm = cpu_to_be32(table->ACPILevel.CcPwrDynRm);
  2501. table->ACPILevel.CcPwrDynRm1 = cpu_to_be32(table->ACPILevel.CcPwrDynRm1);
  2502. table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
  2503. table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
  2504. if (pi->vddci_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
  2505. if (pi->acpi_vddci)
  2506. table->MemoryACPILevel.MinVddci =
  2507. cpu_to_be32(pi->acpi_vddci * VOLTAGE_SCALE);
  2508. else
  2509. table->MemoryACPILevel.MinVddci =
  2510. cpu_to_be32(pi->min_vddci_in_pp_table * VOLTAGE_SCALE);
  2511. }
  2512. if (ci_populate_mvdd_value(rdev, 0, &voltage_level))
  2513. table->MemoryACPILevel.MinMvdd = 0;
  2514. else
  2515. table->MemoryACPILevel.MinMvdd =
  2516. cpu_to_be32(voltage_level.Voltage * VOLTAGE_SCALE);
  2517. mclk_pwrmgt_cntl |= MRDCK0_RESET | MRDCK1_RESET;
  2518. mclk_pwrmgt_cntl &= ~(MRDCK0_PDNB | MRDCK1_PDNB);
  2519. dll_cntl &= ~(MRDCK0_BYPASS | MRDCK1_BYPASS);
  2520. table->MemoryACPILevel.DllCntl = cpu_to_be32(dll_cntl);
  2521. table->MemoryACPILevel.MclkPwrmgtCntl = cpu_to_be32(mclk_pwrmgt_cntl);
  2522. table->MemoryACPILevel.MpllAdFuncCntl =
  2523. cpu_to_be32(pi->clock_registers.mpll_ad_func_cntl);
  2524. table->MemoryACPILevel.MpllDqFuncCntl =
  2525. cpu_to_be32(pi->clock_registers.mpll_dq_func_cntl);
  2526. table->MemoryACPILevel.MpllFuncCntl =
  2527. cpu_to_be32(pi->clock_registers.mpll_func_cntl);
  2528. table->MemoryACPILevel.MpllFuncCntl_1 =
  2529. cpu_to_be32(pi->clock_registers.mpll_func_cntl_1);
  2530. table->MemoryACPILevel.MpllFuncCntl_2 =
  2531. cpu_to_be32(pi->clock_registers.mpll_func_cntl_2);
  2532. table->MemoryACPILevel.MpllSs1 = cpu_to_be32(pi->clock_registers.mpll_ss1);
  2533. table->MemoryACPILevel.MpllSs2 = cpu_to_be32(pi->clock_registers.mpll_ss2);
  2534. table->MemoryACPILevel.EnabledForThrottle = 0;
  2535. table->MemoryACPILevel.EnabledForActivity = 0;
  2536. table->MemoryACPILevel.UpH = 0;
  2537. table->MemoryACPILevel.DownH = 100;
  2538. table->MemoryACPILevel.VoltageDownH = 0;
  2539. table->MemoryACPILevel.ActivityLevel =
  2540. cpu_to_be16((u16)pi->mclk_activity_target);
  2541. table->MemoryACPILevel.StutterEnable = false;
  2542. table->MemoryACPILevel.StrobeEnable = false;
  2543. table->MemoryACPILevel.EdcReadEnable = false;
  2544. table->MemoryACPILevel.EdcWriteEnable = false;
  2545. table->MemoryACPILevel.RttEnable = false;
  2546. return 0;
  2547. }
  2548. static int ci_enable_ulv(struct radeon_device *rdev, bool enable)
  2549. {
  2550. struct ci_power_info *pi = ci_get_pi(rdev);
  2551. struct ci_ulv_parm *ulv = &pi->ulv;
  2552. if (ulv->supported) {
  2553. if (enable)
  2554. return (ci_send_msg_to_smc(rdev, PPSMC_MSG_EnableULV) == PPSMC_Result_OK) ?
  2555. 0 : -EINVAL;
  2556. else
  2557. return (ci_send_msg_to_smc(rdev, PPSMC_MSG_DisableULV) == PPSMC_Result_OK) ?
  2558. 0 : -EINVAL;
  2559. }
  2560. return 0;
  2561. }
  2562. static int ci_populate_ulv_level(struct radeon_device *rdev,
  2563. SMU7_Discrete_Ulv *state)
  2564. {
  2565. struct ci_power_info *pi = ci_get_pi(rdev);
  2566. u16 ulv_voltage = rdev->pm.dpm.backbias_response_time;
  2567. state->CcPwrDynRm = 0;
  2568. state->CcPwrDynRm1 = 0;
  2569. if (ulv_voltage == 0) {
  2570. pi->ulv.supported = false;
  2571. return 0;
  2572. }
  2573. if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_BY_SVID2) {
  2574. if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
  2575. state->VddcOffset = 0;
  2576. else
  2577. state->VddcOffset =
  2578. rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage;
  2579. } else {
  2580. if (ulv_voltage > rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v)
  2581. state->VddcOffsetVid = 0;
  2582. else
  2583. state->VddcOffsetVid = (u8)
  2584. ((rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[0].v - ulv_voltage) *
  2585. VOLTAGE_VID_OFFSET_SCALE2 / VOLTAGE_VID_OFFSET_SCALE1);
  2586. }
  2587. state->VddcPhase = pi->vddc_phase_shed_control ? 0 : 1;
  2588. state->CcPwrDynRm = cpu_to_be32(state->CcPwrDynRm);
  2589. state->CcPwrDynRm1 = cpu_to_be32(state->CcPwrDynRm1);
  2590. state->VddcOffset = cpu_to_be16(state->VddcOffset);
  2591. return 0;
  2592. }
  2593. static int ci_calculate_sclk_params(struct radeon_device *rdev,
  2594. u32 engine_clock,
  2595. SMU7_Discrete_GraphicsLevel *sclk)
  2596. {
  2597. struct ci_power_info *pi = ci_get_pi(rdev);
  2598. struct atom_clock_dividers dividers;
  2599. u32 spll_func_cntl_3 = pi->clock_registers.cg_spll_func_cntl_3;
  2600. u32 spll_func_cntl_4 = pi->clock_registers.cg_spll_func_cntl_4;
  2601. u32 cg_spll_spread_spectrum = pi->clock_registers.cg_spll_spread_spectrum;
  2602. u32 cg_spll_spread_spectrum_2 = pi->clock_registers.cg_spll_spread_spectrum_2;
  2603. u32 reference_clock = rdev->clock.spll.reference_freq;
  2604. u32 reference_divider;
  2605. u32 fbdiv;
  2606. int ret;
  2607. ret = radeon_atom_get_clock_dividers(rdev,
  2608. COMPUTE_GPUCLK_INPUT_FLAG_SCLK,
  2609. engine_clock, false, &dividers);
  2610. if (ret)
  2611. return ret;
  2612. reference_divider = 1 + dividers.ref_div;
  2613. fbdiv = dividers.fb_div & 0x3FFFFFF;
  2614. spll_func_cntl_3 &= ~SPLL_FB_DIV_MASK;
  2615. spll_func_cntl_3 |= SPLL_FB_DIV(fbdiv);
  2616. spll_func_cntl_3 |= SPLL_DITHEN;
  2617. if (pi->caps_sclk_ss_support) {
  2618. struct radeon_atom_ss ss;
  2619. u32 vco_freq = engine_clock * dividers.post_div;
  2620. if (radeon_atombios_get_asic_ss_info(rdev, &ss,
  2621. ASIC_INTERNAL_ENGINE_SS, vco_freq)) {
  2622. u32 clk_s = reference_clock * 5 / (reference_divider * ss.rate);
  2623. u32 clk_v = 4 * ss.percentage * fbdiv / (clk_s * 10000);
  2624. cg_spll_spread_spectrum &= ~CLK_S_MASK;
  2625. cg_spll_spread_spectrum |= CLK_S(clk_s);
  2626. cg_spll_spread_spectrum |= SSEN;
  2627. cg_spll_spread_spectrum_2 &= ~CLK_V_MASK;
  2628. cg_spll_spread_spectrum_2 |= CLK_V(clk_v);
  2629. }
  2630. }
  2631. sclk->SclkFrequency = engine_clock;
  2632. sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
  2633. sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
  2634. sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
  2635. sclk->SpllSpreadSpectrum2 = cg_spll_spread_spectrum_2;
  2636. sclk->SclkDid = (u8)dividers.post_divider;
  2637. return 0;
  2638. }
  2639. static int ci_populate_single_graphic_level(struct radeon_device *rdev,
  2640. u32 engine_clock,
  2641. u16 sclk_activity_level_t,
  2642. SMU7_Discrete_GraphicsLevel *graphic_level)
  2643. {
  2644. struct ci_power_info *pi = ci_get_pi(rdev);
  2645. int ret;
  2646. ret = ci_calculate_sclk_params(rdev, engine_clock, graphic_level);
  2647. if (ret)
  2648. return ret;
  2649. ret = ci_get_dependency_volt_by_clk(rdev,
  2650. &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk,
  2651. engine_clock, &graphic_level->MinVddc);
  2652. if (ret)
  2653. return ret;
  2654. graphic_level->SclkFrequency = engine_clock;
  2655. graphic_level->Flags = 0;
  2656. graphic_level->MinVddcPhases = 1;
  2657. if (pi->vddc_phase_shed_control)
  2658. ci_populate_phase_value_based_on_sclk(rdev,
  2659. &rdev->pm.dpm.dyn_state.phase_shedding_limits_table,
  2660. engine_clock,
  2661. &graphic_level->MinVddcPhases);
  2662. graphic_level->ActivityLevel = sclk_activity_level_t;
  2663. graphic_level->CcPwrDynRm = 0;
  2664. graphic_level->CcPwrDynRm1 = 0;
  2665. graphic_level->EnabledForThrottle = 1;
  2666. graphic_level->UpH = 0;
  2667. graphic_level->DownH = 0;
  2668. graphic_level->VoltageDownH = 0;
  2669. graphic_level->PowerThrottle = 0;
  2670. if (pi->caps_sclk_ds)
  2671. graphic_level->DeepSleepDivId = ci_get_sleep_divider_id_from_clock(rdev,
  2672. engine_clock,
  2673. CISLAND_MINIMUM_ENGINE_CLOCK);
  2674. graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  2675. graphic_level->Flags = cpu_to_be32(graphic_level->Flags);
  2676. graphic_level->MinVddc = cpu_to_be32(graphic_level->MinVddc * VOLTAGE_SCALE);
  2677. graphic_level->MinVddcPhases = cpu_to_be32(graphic_level->MinVddcPhases);
  2678. graphic_level->SclkFrequency = cpu_to_be32(graphic_level->SclkFrequency);
  2679. graphic_level->ActivityLevel = cpu_to_be16(graphic_level->ActivityLevel);
  2680. graphic_level->CgSpllFuncCntl3 = cpu_to_be32(graphic_level->CgSpllFuncCntl3);
  2681. graphic_level->CgSpllFuncCntl4 = cpu_to_be32(graphic_level->CgSpllFuncCntl4);
  2682. graphic_level->SpllSpreadSpectrum = cpu_to_be32(graphic_level->SpllSpreadSpectrum);
  2683. graphic_level->SpllSpreadSpectrum2 = cpu_to_be32(graphic_level->SpllSpreadSpectrum2);
  2684. graphic_level->CcPwrDynRm = cpu_to_be32(graphic_level->CcPwrDynRm);
  2685. graphic_level->CcPwrDynRm1 = cpu_to_be32(graphic_level->CcPwrDynRm1);
  2686. return 0;
  2687. }
  2688. static int ci_populate_all_graphic_levels(struct radeon_device *rdev)
  2689. {
  2690. struct ci_power_info *pi = ci_get_pi(rdev);
  2691. struct ci_dpm_table *dpm_table = &pi->dpm_table;
  2692. u32 level_array_address = pi->dpm_table_start +
  2693. offsetof(SMU7_Discrete_DpmTable, GraphicsLevel);
  2694. u32 level_array_size = sizeof(SMU7_Discrete_GraphicsLevel) *
  2695. SMU7_MAX_LEVELS_GRAPHICS;
  2696. SMU7_Discrete_GraphicsLevel *levels = pi->smc_state_table.GraphicsLevel;
  2697. u32 i, ret;
  2698. memset(levels, 0, level_array_size);
  2699. for (i = 0; i < dpm_table->sclk_table.count; i++) {
  2700. ret = ci_populate_single_graphic_level(rdev,
  2701. dpm_table->sclk_table.dpm_levels[i].value,
  2702. (u16)pi->activity_target[i],
  2703. &pi->smc_state_table.GraphicsLevel[i]);
  2704. if (ret)
  2705. return ret;
  2706. if (i > 1)
  2707. pi->smc_state_table.GraphicsLevel[i].DeepSleepDivId = 0;
  2708. if (i == (dpm_table->sclk_table.count - 1))
  2709. pi->smc_state_table.GraphicsLevel[i].DisplayWatermark =
  2710. PPSMC_DISPLAY_WATERMARK_HIGH;
  2711. }
  2712. pi->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
  2713. pi->smc_state_table.GraphicsDpmLevelCount = (u8)dpm_table->sclk_table.count;
  2714. pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
  2715. ci_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
  2716. ret = ci_copy_bytes_to_smc(rdev, level_array_address,
  2717. (u8 *)levels, level_array_size,
  2718. pi->sram_end);
  2719. if (ret)
  2720. return ret;
  2721. return 0;
  2722. }
  2723. static int ci_populate_ulv_state(struct radeon_device *rdev,
  2724. SMU7_Discrete_Ulv *ulv_level)
  2725. {
  2726. return ci_populate_ulv_level(rdev, ulv_level);
  2727. }
  2728. static int ci_populate_all_memory_levels(struct radeon_device *rdev)
  2729. {
  2730. struct ci_power_info *pi = ci_get_pi(rdev);
  2731. struct ci_dpm_table *dpm_table = &pi->dpm_table;
  2732. u32 level_array_address = pi->dpm_table_start +
  2733. offsetof(SMU7_Discrete_DpmTable, MemoryLevel);
  2734. u32 level_array_size = sizeof(SMU7_Discrete_MemoryLevel) *
  2735. SMU7_MAX_LEVELS_MEMORY;
  2736. SMU7_Discrete_MemoryLevel *levels = pi->smc_state_table.MemoryLevel;
  2737. u32 i, ret;
  2738. memset(levels, 0, level_array_size);
  2739. for (i = 0; i < dpm_table->mclk_table.count; i++) {
  2740. if (dpm_table->mclk_table.dpm_levels[i].value == 0)
  2741. return -EINVAL;
  2742. ret = ci_populate_single_memory_level(rdev,
  2743. dpm_table->mclk_table.dpm_levels[i].value,
  2744. &pi->smc_state_table.MemoryLevel[i]);
  2745. if (ret)
  2746. return ret;
  2747. }
  2748. pi->smc_state_table.MemoryLevel[0].EnabledForActivity = 1;
  2749. if ((dpm_table->mclk_table.count >= 2) &&
  2750. ((rdev->pdev->device == 0x67B0) || (rdev->pdev->device == 0x67B1))) {
  2751. pi->smc_state_table.MemoryLevel[1].MinVddc =
  2752. pi->smc_state_table.MemoryLevel[0].MinVddc;
  2753. pi->smc_state_table.MemoryLevel[1].MinVddcPhases =
  2754. pi->smc_state_table.MemoryLevel[0].MinVddcPhases;
  2755. }
  2756. pi->smc_state_table.MemoryLevel[0].ActivityLevel = cpu_to_be16(0x1F);
  2757. pi->smc_state_table.MemoryDpmLevelCount = (u8)dpm_table->mclk_table.count;
  2758. pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
  2759. ci_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
  2760. pi->smc_state_table.MemoryLevel[dpm_table->mclk_table.count - 1].DisplayWatermark =
  2761. PPSMC_DISPLAY_WATERMARK_HIGH;
  2762. ret = ci_copy_bytes_to_smc(rdev, level_array_address,
  2763. (u8 *)levels, level_array_size,
  2764. pi->sram_end);
  2765. if (ret)
  2766. return ret;
  2767. return 0;
  2768. }
  2769. static void ci_reset_single_dpm_table(struct radeon_device *rdev,
  2770. struct ci_single_dpm_table* dpm_table,
  2771. u32 count)
  2772. {
  2773. u32 i;
  2774. dpm_table->count = count;
  2775. for (i = 0; i < MAX_REGULAR_DPM_NUMBER; i++)
  2776. dpm_table->dpm_levels[i].enabled = false;
  2777. }
  2778. static void ci_setup_pcie_table_entry(struct ci_single_dpm_table* dpm_table,
  2779. u32 index, u32 pcie_gen, u32 pcie_lanes)
  2780. {
  2781. dpm_table->dpm_levels[index].value = pcie_gen;
  2782. dpm_table->dpm_levels[index].param1 = pcie_lanes;
  2783. dpm_table->dpm_levels[index].enabled = true;
  2784. }
  2785. static int ci_setup_default_pcie_tables(struct radeon_device *rdev)
  2786. {
  2787. struct ci_power_info *pi = ci_get_pi(rdev);
  2788. if (!pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels)
  2789. return -EINVAL;
  2790. if (pi->use_pcie_performance_levels && !pi->use_pcie_powersaving_levels) {
  2791. pi->pcie_gen_powersaving = pi->pcie_gen_performance;
  2792. pi->pcie_lane_powersaving = pi->pcie_lane_performance;
  2793. } else if (!pi->use_pcie_performance_levels && pi->use_pcie_powersaving_levels) {
  2794. pi->pcie_gen_performance = pi->pcie_gen_powersaving;
  2795. pi->pcie_lane_performance = pi->pcie_lane_powersaving;
  2796. }
  2797. ci_reset_single_dpm_table(rdev,
  2798. &pi->dpm_table.pcie_speed_table,
  2799. SMU7_MAX_LEVELS_LINK);
  2800. if (rdev->family == CHIP_BONAIRE)
  2801. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
  2802. pi->pcie_gen_powersaving.min,
  2803. pi->pcie_lane_powersaving.max);
  2804. else
  2805. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 0,
  2806. pi->pcie_gen_powersaving.min,
  2807. pi->pcie_lane_powersaving.min);
  2808. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 1,
  2809. pi->pcie_gen_performance.min,
  2810. pi->pcie_lane_performance.min);
  2811. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 2,
  2812. pi->pcie_gen_powersaving.min,
  2813. pi->pcie_lane_powersaving.max);
  2814. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 3,
  2815. pi->pcie_gen_performance.min,
  2816. pi->pcie_lane_performance.max);
  2817. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 4,
  2818. pi->pcie_gen_powersaving.max,
  2819. pi->pcie_lane_powersaving.max);
  2820. ci_setup_pcie_table_entry(&pi->dpm_table.pcie_speed_table, 5,
  2821. pi->pcie_gen_performance.max,
  2822. pi->pcie_lane_performance.max);
  2823. pi->dpm_table.pcie_speed_table.count = 6;
  2824. return 0;
  2825. }
  2826. static int ci_setup_default_dpm_tables(struct radeon_device *rdev)
  2827. {
  2828. struct ci_power_info *pi = ci_get_pi(rdev);
  2829. struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
  2830. &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  2831. struct radeon_clock_voltage_dependency_table *allowed_mclk_table =
  2832. &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
  2833. struct radeon_cac_leakage_table *std_voltage_table =
  2834. &rdev->pm.dpm.dyn_state.cac_leakage_table;
  2835. u32 i;
  2836. if (allowed_sclk_vddc_table == NULL)
  2837. return -EINVAL;
  2838. if (allowed_sclk_vddc_table->count < 1)
  2839. return -EINVAL;
  2840. if (allowed_mclk_table == NULL)
  2841. return -EINVAL;
  2842. if (allowed_mclk_table->count < 1)
  2843. return -EINVAL;
  2844. memset(&pi->dpm_table, 0, sizeof(struct ci_dpm_table));
  2845. ci_reset_single_dpm_table(rdev,
  2846. &pi->dpm_table.sclk_table,
  2847. SMU7_MAX_LEVELS_GRAPHICS);
  2848. ci_reset_single_dpm_table(rdev,
  2849. &pi->dpm_table.mclk_table,
  2850. SMU7_MAX_LEVELS_MEMORY);
  2851. ci_reset_single_dpm_table(rdev,
  2852. &pi->dpm_table.vddc_table,
  2853. SMU7_MAX_LEVELS_VDDC);
  2854. ci_reset_single_dpm_table(rdev,
  2855. &pi->dpm_table.vddci_table,
  2856. SMU7_MAX_LEVELS_VDDCI);
  2857. ci_reset_single_dpm_table(rdev,
  2858. &pi->dpm_table.mvdd_table,
  2859. SMU7_MAX_LEVELS_MVDD);
  2860. pi->dpm_table.sclk_table.count = 0;
  2861. for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
  2862. if ((i == 0) ||
  2863. (pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count-1].value !=
  2864. allowed_sclk_vddc_table->entries[i].clk)) {
  2865. pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].value =
  2866. allowed_sclk_vddc_table->entries[i].clk;
  2867. pi->dpm_table.sclk_table.dpm_levels[pi->dpm_table.sclk_table.count].enabled =
  2868. (i == 0) ? true : false;
  2869. pi->dpm_table.sclk_table.count++;
  2870. }
  2871. }
  2872. pi->dpm_table.mclk_table.count = 0;
  2873. for (i = 0; i < allowed_mclk_table->count; i++) {
  2874. if ((i == 0) ||
  2875. (pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count-1].value !=
  2876. allowed_mclk_table->entries[i].clk)) {
  2877. pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].value =
  2878. allowed_mclk_table->entries[i].clk;
  2879. pi->dpm_table.mclk_table.dpm_levels[pi->dpm_table.mclk_table.count].enabled =
  2880. (i == 0) ? true : false;
  2881. pi->dpm_table.mclk_table.count++;
  2882. }
  2883. }
  2884. for (i = 0; i < allowed_sclk_vddc_table->count; i++) {
  2885. pi->dpm_table.vddc_table.dpm_levels[i].value =
  2886. allowed_sclk_vddc_table->entries[i].v;
  2887. pi->dpm_table.vddc_table.dpm_levels[i].param1 =
  2888. std_voltage_table->entries[i].leakage;
  2889. pi->dpm_table.vddc_table.dpm_levels[i].enabled = true;
  2890. }
  2891. pi->dpm_table.vddc_table.count = allowed_sclk_vddc_table->count;
  2892. allowed_mclk_table = &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
  2893. if (allowed_mclk_table) {
  2894. for (i = 0; i < allowed_mclk_table->count; i++) {
  2895. pi->dpm_table.vddci_table.dpm_levels[i].value =
  2896. allowed_mclk_table->entries[i].v;
  2897. pi->dpm_table.vddci_table.dpm_levels[i].enabled = true;
  2898. }
  2899. pi->dpm_table.vddci_table.count = allowed_mclk_table->count;
  2900. }
  2901. allowed_mclk_table = &rdev->pm.dpm.dyn_state.mvdd_dependency_on_mclk;
  2902. if (allowed_mclk_table) {
  2903. for (i = 0; i < allowed_mclk_table->count; i++) {
  2904. pi->dpm_table.mvdd_table.dpm_levels[i].value =
  2905. allowed_mclk_table->entries[i].v;
  2906. pi->dpm_table.mvdd_table.dpm_levels[i].enabled = true;
  2907. }
  2908. pi->dpm_table.mvdd_table.count = allowed_mclk_table->count;
  2909. }
  2910. ci_setup_default_pcie_tables(rdev);
  2911. return 0;
  2912. }
  2913. static int ci_find_boot_level(struct ci_single_dpm_table *table,
  2914. u32 value, u32 *boot_level)
  2915. {
  2916. u32 i;
  2917. int ret = -EINVAL;
  2918. for(i = 0; i < table->count; i++) {
  2919. if (value == table->dpm_levels[i].value) {
  2920. *boot_level = i;
  2921. ret = 0;
  2922. }
  2923. }
  2924. return ret;
  2925. }
  2926. static int ci_init_smc_table(struct radeon_device *rdev)
  2927. {
  2928. struct ci_power_info *pi = ci_get_pi(rdev);
  2929. struct ci_ulv_parm *ulv = &pi->ulv;
  2930. struct radeon_ps *radeon_boot_state = rdev->pm.dpm.boot_ps;
  2931. SMU7_Discrete_DpmTable *table = &pi->smc_state_table;
  2932. int ret;
  2933. ret = ci_setup_default_dpm_tables(rdev);
  2934. if (ret)
  2935. return ret;
  2936. if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE)
  2937. ci_populate_smc_voltage_tables(rdev, table);
  2938. ci_init_fps_limits(rdev);
  2939. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC)
  2940. table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
  2941. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC)
  2942. table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
  2943. if (pi->mem_gddr5)
  2944. table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
  2945. if (ulv->supported) {
  2946. ret = ci_populate_ulv_state(rdev, &pi->smc_state_table.Ulv);
  2947. if (ret)
  2948. return ret;
  2949. WREG32_SMC(CG_ULV_PARAMETER, ulv->cg_ulv_parameter);
  2950. }
  2951. ret = ci_populate_all_graphic_levels(rdev);
  2952. if (ret)
  2953. return ret;
  2954. ret = ci_populate_all_memory_levels(rdev);
  2955. if (ret)
  2956. return ret;
  2957. ci_populate_smc_link_level(rdev, table);
  2958. ret = ci_populate_smc_acpi_level(rdev, table);
  2959. if (ret)
  2960. return ret;
  2961. ret = ci_populate_smc_vce_level(rdev, table);
  2962. if (ret)
  2963. return ret;
  2964. ret = ci_populate_smc_acp_level(rdev, table);
  2965. if (ret)
  2966. return ret;
  2967. ret = ci_populate_smc_samu_level(rdev, table);
  2968. if (ret)
  2969. return ret;
  2970. ret = ci_do_program_memory_timing_parameters(rdev);
  2971. if (ret)
  2972. return ret;
  2973. ret = ci_populate_smc_uvd_level(rdev, table);
  2974. if (ret)
  2975. return ret;
  2976. table->UvdBootLevel = 0;
  2977. table->VceBootLevel = 0;
  2978. table->AcpBootLevel = 0;
  2979. table->SamuBootLevel = 0;
  2980. table->GraphicsBootLevel = 0;
  2981. table->MemoryBootLevel = 0;
  2982. ret = ci_find_boot_level(&pi->dpm_table.sclk_table,
  2983. pi->vbios_boot_state.sclk_bootup_value,
  2984. (u32 *)&pi->smc_state_table.GraphicsBootLevel);
  2985. ret = ci_find_boot_level(&pi->dpm_table.mclk_table,
  2986. pi->vbios_boot_state.mclk_bootup_value,
  2987. (u32 *)&pi->smc_state_table.MemoryBootLevel);
  2988. table->BootVddc = pi->vbios_boot_state.vddc_bootup_value;
  2989. table->BootVddci = pi->vbios_boot_state.vddci_bootup_value;
  2990. table->BootMVdd = pi->vbios_boot_state.mvdd_bootup_value;
  2991. ci_populate_smc_initial_state(rdev, radeon_boot_state);
  2992. ret = ci_populate_bapm_parameters_in_dpm_table(rdev);
  2993. if (ret)
  2994. return ret;
  2995. table->UVDInterval = 1;
  2996. table->VCEInterval = 1;
  2997. table->ACPInterval = 1;
  2998. table->SAMUInterval = 1;
  2999. table->GraphicsVoltageChangeEnable = 1;
  3000. table->GraphicsThermThrottleEnable = 1;
  3001. table->GraphicsInterval = 1;
  3002. table->VoltageInterval = 1;
  3003. table->ThermalInterval = 1;
  3004. table->TemperatureLimitHigh = (u16)((pi->thermal_temp_setting.temperature_high *
  3005. CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
  3006. table->TemperatureLimitLow = (u16)((pi->thermal_temp_setting.temperature_low *
  3007. CISLANDS_Q88_FORMAT_CONVERSION_UNIT) / 1000);
  3008. table->MemoryVoltageChangeEnable = 1;
  3009. table->MemoryInterval = 1;
  3010. table->VoltageResponseTime = 0;
  3011. table->VddcVddciDelta = 4000;
  3012. table->PhaseResponseTime = 0;
  3013. table->MemoryThermThrottleEnable = 1;
  3014. table->PCIeBootLinkLevel = pi->dpm_table.pcie_speed_table.count - 1;
  3015. table->PCIeGenInterval = 1;
  3016. if (pi->voltage_control == CISLANDS_VOLTAGE_CONTROL_BY_SVID2)
  3017. table->SVI2Enable = 1;
  3018. else
  3019. table->SVI2Enable = 0;
  3020. table->ThermGpio = 17;
  3021. table->SclkStepSize = 0x4000;
  3022. table->SystemFlags = cpu_to_be32(table->SystemFlags);
  3023. table->SmioMaskVddcVid = cpu_to_be32(table->SmioMaskVddcVid);
  3024. table->SmioMaskVddcPhase = cpu_to_be32(table->SmioMaskVddcPhase);
  3025. table->SmioMaskVddciVid = cpu_to_be32(table->SmioMaskVddciVid);
  3026. table->SmioMaskMvddVid = cpu_to_be32(table->SmioMaskMvddVid);
  3027. table->SclkStepSize = cpu_to_be32(table->SclkStepSize);
  3028. table->TemperatureLimitHigh = cpu_to_be16(table->TemperatureLimitHigh);
  3029. table->TemperatureLimitLow = cpu_to_be16(table->TemperatureLimitLow);
  3030. table->VddcVddciDelta = cpu_to_be16(table->VddcVddciDelta);
  3031. table->VoltageResponseTime = cpu_to_be16(table->VoltageResponseTime);
  3032. table->PhaseResponseTime = cpu_to_be16(table->PhaseResponseTime);
  3033. table->BootVddc = cpu_to_be16(table->BootVddc * VOLTAGE_SCALE);
  3034. table->BootVddci = cpu_to_be16(table->BootVddci * VOLTAGE_SCALE);
  3035. table->BootMVdd = cpu_to_be16(table->BootMVdd * VOLTAGE_SCALE);
  3036. ret = ci_copy_bytes_to_smc(rdev,
  3037. pi->dpm_table_start +
  3038. offsetof(SMU7_Discrete_DpmTable, SystemFlags),
  3039. (u8 *)&table->SystemFlags,
  3040. sizeof(SMU7_Discrete_DpmTable) - 3 * sizeof(SMU7_PIDController),
  3041. pi->sram_end);
  3042. if (ret)
  3043. return ret;
  3044. return 0;
  3045. }
  3046. static void ci_trim_single_dpm_states(struct radeon_device *rdev,
  3047. struct ci_single_dpm_table *dpm_table,
  3048. u32 low_limit, u32 high_limit)
  3049. {
  3050. u32 i;
  3051. for (i = 0; i < dpm_table->count; i++) {
  3052. if ((dpm_table->dpm_levels[i].value < low_limit) ||
  3053. (dpm_table->dpm_levels[i].value > high_limit))
  3054. dpm_table->dpm_levels[i].enabled = false;
  3055. else
  3056. dpm_table->dpm_levels[i].enabled = true;
  3057. }
  3058. }
  3059. static void ci_trim_pcie_dpm_states(struct radeon_device *rdev,
  3060. u32 speed_low, u32 lanes_low,
  3061. u32 speed_high, u32 lanes_high)
  3062. {
  3063. struct ci_power_info *pi = ci_get_pi(rdev);
  3064. struct ci_single_dpm_table *pcie_table = &pi->dpm_table.pcie_speed_table;
  3065. u32 i, j;
  3066. for (i = 0; i < pcie_table->count; i++) {
  3067. if ((pcie_table->dpm_levels[i].value < speed_low) ||
  3068. (pcie_table->dpm_levels[i].param1 < lanes_low) ||
  3069. (pcie_table->dpm_levels[i].value > speed_high) ||
  3070. (pcie_table->dpm_levels[i].param1 > lanes_high))
  3071. pcie_table->dpm_levels[i].enabled = false;
  3072. else
  3073. pcie_table->dpm_levels[i].enabled = true;
  3074. }
  3075. for (i = 0; i < pcie_table->count; i++) {
  3076. if (pcie_table->dpm_levels[i].enabled) {
  3077. for (j = i + 1; j < pcie_table->count; j++) {
  3078. if (pcie_table->dpm_levels[j].enabled) {
  3079. if ((pcie_table->dpm_levels[i].value == pcie_table->dpm_levels[j].value) &&
  3080. (pcie_table->dpm_levels[i].param1 == pcie_table->dpm_levels[j].param1))
  3081. pcie_table->dpm_levels[j].enabled = false;
  3082. }
  3083. }
  3084. }
  3085. }
  3086. }
  3087. static int ci_trim_dpm_states(struct radeon_device *rdev,
  3088. struct radeon_ps *radeon_state)
  3089. {
  3090. struct ci_ps *state = ci_get_ps(radeon_state);
  3091. struct ci_power_info *pi = ci_get_pi(rdev);
  3092. u32 high_limit_count;
  3093. if (state->performance_level_count < 1)
  3094. return -EINVAL;
  3095. if (state->performance_level_count == 1)
  3096. high_limit_count = 0;
  3097. else
  3098. high_limit_count = 1;
  3099. ci_trim_single_dpm_states(rdev,
  3100. &pi->dpm_table.sclk_table,
  3101. state->performance_levels[0].sclk,
  3102. state->performance_levels[high_limit_count].sclk);
  3103. ci_trim_single_dpm_states(rdev,
  3104. &pi->dpm_table.mclk_table,
  3105. state->performance_levels[0].mclk,
  3106. state->performance_levels[high_limit_count].mclk);
  3107. ci_trim_pcie_dpm_states(rdev,
  3108. state->performance_levels[0].pcie_gen,
  3109. state->performance_levels[0].pcie_lane,
  3110. state->performance_levels[high_limit_count].pcie_gen,
  3111. state->performance_levels[high_limit_count].pcie_lane);
  3112. return 0;
  3113. }
  3114. static int ci_apply_disp_minimum_voltage_request(struct radeon_device *rdev)
  3115. {
  3116. struct radeon_clock_voltage_dependency_table *disp_voltage_table =
  3117. &rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk;
  3118. struct radeon_clock_voltage_dependency_table *vddc_table =
  3119. &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  3120. u32 requested_voltage = 0;
  3121. u32 i;
  3122. if (disp_voltage_table == NULL)
  3123. return -EINVAL;
  3124. if (!disp_voltage_table->count)
  3125. return -EINVAL;
  3126. for (i = 0; i < disp_voltage_table->count; i++) {
  3127. if (rdev->clock.current_dispclk == disp_voltage_table->entries[i].clk)
  3128. requested_voltage = disp_voltage_table->entries[i].v;
  3129. }
  3130. for (i = 0; i < vddc_table->count; i++) {
  3131. if (requested_voltage <= vddc_table->entries[i].v) {
  3132. requested_voltage = vddc_table->entries[i].v;
  3133. return (ci_send_msg_to_smc_with_parameter(rdev,
  3134. PPSMC_MSG_VddC_Request,
  3135. requested_voltage * VOLTAGE_SCALE) == PPSMC_Result_OK) ?
  3136. 0 : -EINVAL;
  3137. }
  3138. }
  3139. return -EINVAL;
  3140. }
  3141. static int ci_upload_dpm_level_enable_mask(struct radeon_device *rdev)
  3142. {
  3143. struct ci_power_info *pi = ci_get_pi(rdev);
  3144. PPSMC_Result result;
  3145. ci_apply_disp_minimum_voltage_request(rdev);
  3146. if (!pi->sclk_dpm_key_disabled) {
  3147. if (pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
  3148. result = ci_send_msg_to_smc_with_parameter(rdev,
  3149. PPSMC_MSG_SCLKDPM_SetEnabledMask,
  3150. pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
  3151. if (result != PPSMC_Result_OK)
  3152. return -EINVAL;
  3153. }
  3154. }
  3155. if (!pi->mclk_dpm_key_disabled) {
  3156. if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
  3157. result = ci_send_msg_to_smc_with_parameter(rdev,
  3158. PPSMC_MSG_MCLKDPM_SetEnabledMask,
  3159. pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
  3160. if (result != PPSMC_Result_OK)
  3161. return -EINVAL;
  3162. }
  3163. }
  3164. #if 0
  3165. if (!pi->pcie_dpm_key_disabled) {
  3166. if (pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
  3167. result = ci_send_msg_to_smc_with_parameter(rdev,
  3168. PPSMC_MSG_PCIeDPM_SetEnabledMask,
  3169. pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
  3170. if (result != PPSMC_Result_OK)
  3171. return -EINVAL;
  3172. }
  3173. }
  3174. #endif
  3175. return 0;
  3176. }
  3177. static void ci_find_dpm_states_clocks_in_dpm_table(struct radeon_device *rdev,
  3178. struct radeon_ps *radeon_state)
  3179. {
  3180. struct ci_power_info *pi = ci_get_pi(rdev);
  3181. struct ci_ps *state = ci_get_ps(radeon_state);
  3182. struct ci_single_dpm_table *sclk_table = &pi->dpm_table.sclk_table;
  3183. u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
  3184. struct ci_single_dpm_table *mclk_table = &pi->dpm_table.mclk_table;
  3185. u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
  3186. u32 i;
  3187. pi->need_update_smu7_dpm_table = 0;
  3188. for (i = 0; i < sclk_table->count; i++) {
  3189. if (sclk == sclk_table->dpm_levels[i].value)
  3190. break;
  3191. }
  3192. if (i >= sclk_table->count) {
  3193. pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_SCLK;
  3194. } else {
  3195. /* XXX check display min clock requirements */
  3196. if (CISLAND_MINIMUM_ENGINE_CLOCK != CISLAND_MINIMUM_ENGINE_CLOCK)
  3197. pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_SCLK;
  3198. }
  3199. for (i = 0; i < mclk_table->count; i++) {
  3200. if (mclk == mclk_table->dpm_levels[i].value)
  3201. break;
  3202. }
  3203. if (i >= mclk_table->count)
  3204. pi->need_update_smu7_dpm_table |= DPMTABLE_OD_UPDATE_MCLK;
  3205. if (rdev->pm.dpm.current_active_crtc_count !=
  3206. rdev->pm.dpm.new_active_crtc_count)
  3207. pi->need_update_smu7_dpm_table |= DPMTABLE_UPDATE_MCLK;
  3208. }
  3209. static int ci_populate_and_upload_sclk_mclk_dpm_levels(struct radeon_device *rdev,
  3210. struct radeon_ps *radeon_state)
  3211. {
  3212. struct ci_power_info *pi = ci_get_pi(rdev);
  3213. struct ci_ps *state = ci_get_ps(radeon_state);
  3214. u32 sclk = state->performance_levels[state->performance_level_count-1].sclk;
  3215. u32 mclk = state->performance_levels[state->performance_level_count-1].mclk;
  3216. struct ci_dpm_table *dpm_table = &pi->dpm_table;
  3217. int ret;
  3218. if (!pi->need_update_smu7_dpm_table)
  3219. return 0;
  3220. if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_SCLK)
  3221. dpm_table->sclk_table.dpm_levels[dpm_table->sclk_table.count-1].value = sclk;
  3222. if (pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK)
  3223. dpm_table->mclk_table.dpm_levels[dpm_table->mclk_table.count-1].value = mclk;
  3224. if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_SCLK | DPMTABLE_UPDATE_SCLK)) {
  3225. ret = ci_populate_all_graphic_levels(rdev);
  3226. if (ret)
  3227. return ret;
  3228. }
  3229. if (pi->need_update_smu7_dpm_table & (DPMTABLE_OD_UPDATE_MCLK | DPMTABLE_UPDATE_MCLK)) {
  3230. ret = ci_populate_all_memory_levels(rdev);
  3231. if (ret)
  3232. return ret;
  3233. }
  3234. return 0;
  3235. }
  3236. static int ci_enable_uvd_dpm(struct radeon_device *rdev, bool enable)
  3237. {
  3238. struct ci_power_info *pi = ci_get_pi(rdev);
  3239. const struct radeon_clock_and_voltage_limits *max_limits;
  3240. int i;
  3241. if (rdev->pm.dpm.ac_power)
  3242. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  3243. else
  3244. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
  3245. if (enable) {
  3246. pi->dpm_level_enable_mask.uvd_dpm_enable_mask = 0;
  3247. for (i = rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
  3248. if (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
  3249. pi->dpm_level_enable_mask.uvd_dpm_enable_mask |= 1 << i;
  3250. if (!pi->caps_uvd_dpm)
  3251. break;
  3252. }
  3253. }
  3254. ci_send_msg_to_smc_with_parameter(rdev,
  3255. PPSMC_MSG_UVDDPM_SetEnabledMask,
  3256. pi->dpm_level_enable_mask.uvd_dpm_enable_mask);
  3257. if (pi->last_mclk_dpm_enable_mask & 0x1) {
  3258. pi->uvd_enabled = true;
  3259. pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
  3260. ci_send_msg_to_smc_with_parameter(rdev,
  3261. PPSMC_MSG_MCLKDPM_SetEnabledMask,
  3262. pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
  3263. }
  3264. } else {
  3265. if (pi->last_mclk_dpm_enable_mask & 0x1) {
  3266. pi->uvd_enabled = false;
  3267. pi->dpm_level_enable_mask.mclk_dpm_enable_mask |= 1;
  3268. ci_send_msg_to_smc_with_parameter(rdev,
  3269. PPSMC_MSG_MCLKDPM_SetEnabledMask,
  3270. pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
  3271. }
  3272. }
  3273. return (ci_send_msg_to_smc(rdev, enable ?
  3274. PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable) == PPSMC_Result_OK) ?
  3275. 0 : -EINVAL;
  3276. }
  3277. static int ci_enable_vce_dpm(struct radeon_device *rdev, bool enable)
  3278. {
  3279. struct ci_power_info *pi = ci_get_pi(rdev);
  3280. const struct radeon_clock_and_voltage_limits *max_limits;
  3281. int i;
  3282. if (rdev->pm.dpm.ac_power)
  3283. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  3284. else
  3285. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
  3286. if (enable) {
  3287. pi->dpm_level_enable_mask.vce_dpm_enable_mask = 0;
  3288. for (i = rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
  3289. if (rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
  3290. pi->dpm_level_enable_mask.vce_dpm_enable_mask |= 1 << i;
  3291. if (!pi->caps_vce_dpm)
  3292. break;
  3293. }
  3294. }
  3295. ci_send_msg_to_smc_with_parameter(rdev,
  3296. PPSMC_MSG_VCEDPM_SetEnabledMask,
  3297. pi->dpm_level_enable_mask.vce_dpm_enable_mask);
  3298. }
  3299. return (ci_send_msg_to_smc(rdev, enable ?
  3300. PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable) == PPSMC_Result_OK) ?
  3301. 0 : -EINVAL;
  3302. }
  3303. #if 0
  3304. static int ci_enable_samu_dpm(struct radeon_device *rdev, bool enable)
  3305. {
  3306. struct ci_power_info *pi = ci_get_pi(rdev);
  3307. const struct radeon_clock_and_voltage_limits *max_limits;
  3308. int i;
  3309. if (rdev->pm.dpm.ac_power)
  3310. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  3311. else
  3312. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
  3313. if (enable) {
  3314. pi->dpm_level_enable_mask.samu_dpm_enable_mask = 0;
  3315. for (i = rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
  3316. if (rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
  3317. pi->dpm_level_enable_mask.samu_dpm_enable_mask |= 1 << i;
  3318. if (!pi->caps_samu_dpm)
  3319. break;
  3320. }
  3321. }
  3322. ci_send_msg_to_smc_with_parameter(rdev,
  3323. PPSMC_MSG_SAMUDPM_SetEnabledMask,
  3324. pi->dpm_level_enable_mask.samu_dpm_enable_mask);
  3325. }
  3326. return (ci_send_msg_to_smc(rdev, enable ?
  3327. PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable) == PPSMC_Result_OK) ?
  3328. 0 : -EINVAL;
  3329. }
  3330. static int ci_enable_acp_dpm(struct radeon_device *rdev, bool enable)
  3331. {
  3332. struct ci_power_info *pi = ci_get_pi(rdev);
  3333. const struct radeon_clock_and_voltage_limits *max_limits;
  3334. int i;
  3335. if (rdev->pm.dpm.ac_power)
  3336. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  3337. else
  3338. max_limits = &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc;
  3339. if (enable) {
  3340. pi->dpm_level_enable_mask.acp_dpm_enable_mask = 0;
  3341. for (i = rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.count - 1; i >= 0; i--) {
  3342. if (rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table.entries[i].v <= max_limits->vddc) {
  3343. pi->dpm_level_enable_mask.acp_dpm_enable_mask |= 1 << i;
  3344. if (!pi->caps_acp_dpm)
  3345. break;
  3346. }
  3347. }
  3348. ci_send_msg_to_smc_with_parameter(rdev,
  3349. PPSMC_MSG_ACPDPM_SetEnabledMask,
  3350. pi->dpm_level_enable_mask.acp_dpm_enable_mask);
  3351. }
  3352. return (ci_send_msg_to_smc(rdev, enable ?
  3353. PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable) == PPSMC_Result_OK) ?
  3354. 0 : -EINVAL;
  3355. }
  3356. #endif
  3357. static int ci_update_uvd_dpm(struct radeon_device *rdev, bool gate)
  3358. {
  3359. struct ci_power_info *pi = ci_get_pi(rdev);
  3360. u32 tmp;
  3361. if (!gate) {
  3362. if (pi->caps_uvd_dpm ||
  3363. (rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count <= 0))
  3364. pi->smc_state_table.UvdBootLevel = 0;
  3365. else
  3366. pi->smc_state_table.UvdBootLevel =
  3367. rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table.count - 1;
  3368. tmp = RREG32_SMC(DPM_TABLE_475);
  3369. tmp &= ~UvdBootLevel_MASK;
  3370. tmp |= UvdBootLevel(pi->smc_state_table.UvdBootLevel);
  3371. WREG32_SMC(DPM_TABLE_475, tmp);
  3372. }
  3373. return ci_enable_uvd_dpm(rdev, !gate);
  3374. }
  3375. static u8 ci_get_vce_boot_level(struct radeon_device *rdev)
  3376. {
  3377. u8 i;
  3378. u32 min_evclk = 30000; /* ??? */
  3379. struct radeon_vce_clock_voltage_dependency_table *table =
  3380. &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  3381. for (i = 0; i < table->count; i++) {
  3382. if (table->entries[i].evclk >= min_evclk)
  3383. return i;
  3384. }
  3385. return table->count - 1;
  3386. }
  3387. static int ci_update_vce_dpm(struct radeon_device *rdev,
  3388. struct radeon_ps *radeon_new_state,
  3389. struct radeon_ps *radeon_current_state)
  3390. {
  3391. struct ci_power_info *pi = ci_get_pi(rdev);
  3392. int ret = 0;
  3393. u32 tmp;
  3394. if (radeon_current_state->evclk != radeon_new_state->evclk) {
  3395. if (radeon_new_state->evclk) {
  3396. /* turn the clocks on when encoding */
  3397. cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, false);
  3398. pi->smc_state_table.VceBootLevel = ci_get_vce_boot_level(rdev);
  3399. tmp = RREG32_SMC(DPM_TABLE_475);
  3400. tmp &= ~VceBootLevel_MASK;
  3401. tmp |= VceBootLevel(pi->smc_state_table.VceBootLevel);
  3402. WREG32_SMC(DPM_TABLE_475, tmp);
  3403. ret = ci_enable_vce_dpm(rdev, true);
  3404. } else {
  3405. /* turn the clocks off when not encoding */
  3406. cik_update_cg(rdev, RADEON_CG_BLOCK_VCE, true);
  3407. ret = ci_enable_vce_dpm(rdev, false);
  3408. }
  3409. }
  3410. return ret;
  3411. }
  3412. #if 0
  3413. static int ci_update_samu_dpm(struct radeon_device *rdev, bool gate)
  3414. {
  3415. return ci_enable_samu_dpm(rdev, gate);
  3416. }
  3417. static int ci_update_acp_dpm(struct radeon_device *rdev, bool gate)
  3418. {
  3419. struct ci_power_info *pi = ci_get_pi(rdev);
  3420. u32 tmp;
  3421. if (!gate) {
  3422. pi->smc_state_table.AcpBootLevel = 0;
  3423. tmp = RREG32_SMC(DPM_TABLE_475);
  3424. tmp &= ~AcpBootLevel_MASK;
  3425. tmp |= AcpBootLevel(pi->smc_state_table.AcpBootLevel);
  3426. WREG32_SMC(DPM_TABLE_475, tmp);
  3427. }
  3428. return ci_enable_acp_dpm(rdev, !gate);
  3429. }
  3430. #endif
  3431. static int ci_generate_dpm_level_enable_mask(struct radeon_device *rdev,
  3432. struct radeon_ps *radeon_state)
  3433. {
  3434. struct ci_power_info *pi = ci_get_pi(rdev);
  3435. int ret;
  3436. ret = ci_trim_dpm_states(rdev, radeon_state);
  3437. if (ret)
  3438. return ret;
  3439. pi->dpm_level_enable_mask.sclk_dpm_enable_mask =
  3440. ci_get_dpm_level_enable_mask_value(&pi->dpm_table.sclk_table);
  3441. pi->dpm_level_enable_mask.mclk_dpm_enable_mask =
  3442. ci_get_dpm_level_enable_mask_value(&pi->dpm_table.mclk_table);
  3443. pi->last_mclk_dpm_enable_mask =
  3444. pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
  3445. if (pi->uvd_enabled) {
  3446. if (pi->dpm_level_enable_mask.mclk_dpm_enable_mask & 1)
  3447. pi->dpm_level_enable_mask.mclk_dpm_enable_mask &= 0xFFFFFFFE;
  3448. }
  3449. pi->dpm_level_enable_mask.pcie_dpm_enable_mask =
  3450. ci_get_dpm_level_enable_mask_value(&pi->dpm_table.pcie_speed_table);
  3451. return 0;
  3452. }
  3453. static u32 ci_get_lowest_enabled_level(struct radeon_device *rdev,
  3454. u32 level_mask)
  3455. {
  3456. u32 level = 0;
  3457. while ((level_mask & (1 << level)) == 0)
  3458. level++;
  3459. return level;
  3460. }
  3461. int ci_dpm_force_performance_level(struct radeon_device *rdev,
  3462. enum radeon_dpm_forced_level level)
  3463. {
  3464. struct ci_power_info *pi = ci_get_pi(rdev);
  3465. u32 tmp, levels, i;
  3466. int ret;
  3467. if (level == RADEON_DPM_FORCED_LEVEL_HIGH) {
  3468. if ((!pi->pcie_dpm_key_disabled) &&
  3469. pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
  3470. levels = 0;
  3471. tmp = pi->dpm_level_enable_mask.pcie_dpm_enable_mask;
  3472. while (tmp >>= 1)
  3473. levels++;
  3474. if (levels) {
  3475. ret = ci_dpm_force_state_pcie(rdev, level);
  3476. if (ret)
  3477. return ret;
  3478. for (i = 0; i < rdev->usec_timeout; i++) {
  3479. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
  3480. CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
  3481. if (tmp == levels)
  3482. break;
  3483. udelay(1);
  3484. }
  3485. }
  3486. }
  3487. if ((!pi->sclk_dpm_key_disabled) &&
  3488. pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
  3489. levels = 0;
  3490. tmp = pi->dpm_level_enable_mask.sclk_dpm_enable_mask;
  3491. while (tmp >>= 1)
  3492. levels++;
  3493. if (levels) {
  3494. ret = ci_dpm_force_state_sclk(rdev, levels);
  3495. if (ret)
  3496. return ret;
  3497. for (i = 0; i < rdev->usec_timeout; i++) {
  3498. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
  3499. CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
  3500. if (tmp == levels)
  3501. break;
  3502. udelay(1);
  3503. }
  3504. }
  3505. }
  3506. if ((!pi->mclk_dpm_key_disabled) &&
  3507. pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
  3508. levels = 0;
  3509. tmp = pi->dpm_level_enable_mask.mclk_dpm_enable_mask;
  3510. while (tmp >>= 1)
  3511. levels++;
  3512. if (levels) {
  3513. ret = ci_dpm_force_state_mclk(rdev, levels);
  3514. if (ret)
  3515. return ret;
  3516. for (i = 0; i < rdev->usec_timeout; i++) {
  3517. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
  3518. CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
  3519. if (tmp == levels)
  3520. break;
  3521. udelay(1);
  3522. }
  3523. }
  3524. }
  3525. } else if (level == RADEON_DPM_FORCED_LEVEL_LOW) {
  3526. if ((!pi->sclk_dpm_key_disabled) &&
  3527. pi->dpm_level_enable_mask.sclk_dpm_enable_mask) {
  3528. levels = ci_get_lowest_enabled_level(rdev,
  3529. pi->dpm_level_enable_mask.sclk_dpm_enable_mask);
  3530. ret = ci_dpm_force_state_sclk(rdev, levels);
  3531. if (ret)
  3532. return ret;
  3533. for (i = 0; i < rdev->usec_timeout; i++) {
  3534. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
  3535. CURR_SCLK_INDEX_MASK) >> CURR_SCLK_INDEX_SHIFT;
  3536. if (tmp == levels)
  3537. break;
  3538. udelay(1);
  3539. }
  3540. }
  3541. if ((!pi->mclk_dpm_key_disabled) &&
  3542. pi->dpm_level_enable_mask.mclk_dpm_enable_mask) {
  3543. levels = ci_get_lowest_enabled_level(rdev,
  3544. pi->dpm_level_enable_mask.mclk_dpm_enable_mask);
  3545. ret = ci_dpm_force_state_mclk(rdev, levels);
  3546. if (ret)
  3547. return ret;
  3548. for (i = 0; i < rdev->usec_timeout; i++) {
  3549. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX) &
  3550. CURR_MCLK_INDEX_MASK) >> CURR_MCLK_INDEX_SHIFT;
  3551. if (tmp == levels)
  3552. break;
  3553. udelay(1);
  3554. }
  3555. }
  3556. if ((!pi->pcie_dpm_key_disabled) &&
  3557. pi->dpm_level_enable_mask.pcie_dpm_enable_mask) {
  3558. levels = ci_get_lowest_enabled_level(rdev,
  3559. pi->dpm_level_enable_mask.pcie_dpm_enable_mask);
  3560. ret = ci_dpm_force_state_pcie(rdev, levels);
  3561. if (ret)
  3562. return ret;
  3563. for (i = 0; i < rdev->usec_timeout; i++) {
  3564. tmp = (RREG32_SMC(TARGET_AND_CURRENT_PROFILE_INDEX_1) &
  3565. CURR_PCIE_INDEX_MASK) >> CURR_PCIE_INDEX_SHIFT;
  3566. if (tmp == levels)
  3567. break;
  3568. udelay(1);
  3569. }
  3570. }
  3571. } else if (level == RADEON_DPM_FORCED_LEVEL_AUTO) {
  3572. if (!pi->pcie_dpm_key_disabled) {
  3573. PPSMC_Result smc_result;
  3574. smc_result = ci_send_msg_to_smc(rdev,
  3575. PPSMC_MSG_PCIeDPM_UnForceLevel);
  3576. if (smc_result != PPSMC_Result_OK)
  3577. return -EINVAL;
  3578. }
  3579. ret = ci_upload_dpm_level_enable_mask(rdev);
  3580. if (ret)
  3581. return ret;
  3582. }
  3583. rdev->pm.dpm.forced_level = level;
  3584. return 0;
  3585. }
  3586. static int ci_set_mc_special_registers(struct radeon_device *rdev,
  3587. struct ci_mc_reg_table *table)
  3588. {
  3589. struct ci_power_info *pi = ci_get_pi(rdev);
  3590. u8 i, j, k;
  3591. u32 temp_reg;
  3592. for (i = 0, j = table->last; i < table->last; i++) {
  3593. if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3594. return -EINVAL;
  3595. switch(table->mc_reg_address[i].s1 << 2) {
  3596. case MC_SEQ_MISC1:
  3597. temp_reg = RREG32(MC_PMG_CMD_EMRS);
  3598. table->mc_reg_address[j].s1 = MC_PMG_CMD_EMRS >> 2;
  3599. table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
  3600. for (k = 0; k < table->num_entries; k++) {
  3601. table->mc_reg_table_entry[k].mc_data[j] =
  3602. ((temp_reg & 0xffff0000)) | ((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
  3603. }
  3604. j++;
  3605. if (j >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3606. return -EINVAL;
  3607. temp_reg = RREG32(MC_PMG_CMD_MRS);
  3608. table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS >> 2;
  3609. table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS_LP >> 2;
  3610. for (k = 0; k < table->num_entries; k++) {
  3611. table->mc_reg_table_entry[k].mc_data[j] =
  3612. (temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
  3613. if (!pi->mem_gddr5)
  3614. table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
  3615. }
  3616. j++;
  3617. if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3618. return -EINVAL;
  3619. if (!pi->mem_gddr5) {
  3620. table->mc_reg_address[j].s1 = MC_PMG_AUTO_CMD >> 2;
  3621. table->mc_reg_address[j].s0 = MC_PMG_AUTO_CMD >> 2;
  3622. for (k = 0; k < table->num_entries; k++) {
  3623. table->mc_reg_table_entry[k].mc_data[j] =
  3624. (table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16;
  3625. }
  3626. j++;
  3627. if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3628. return -EINVAL;
  3629. }
  3630. break;
  3631. case MC_SEQ_RESERVE_M:
  3632. temp_reg = RREG32(MC_PMG_CMD_MRS1);
  3633. table->mc_reg_address[j].s1 = MC_PMG_CMD_MRS1 >> 2;
  3634. table->mc_reg_address[j].s0 = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
  3635. for (k = 0; k < table->num_entries; k++) {
  3636. table->mc_reg_table_entry[k].mc_data[j] =
  3637. (temp_reg & 0xffff0000) | (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
  3638. }
  3639. j++;
  3640. if (j > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3641. return -EINVAL;
  3642. break;
  3643. default:
  3644. break;
  3645. }
  3646. }
  3647. table->last = j;
  3648. return 0;
  3649. }
  3650. static bool ci_check_s0_mc_reg_index(u16 in_reg, u16 *out_reg)
  3651. {
  3652. bool result = true;
  3653. switch(in_reg) {
  3654. case MC_SEQ_RAS_TIMING >> 2:
  3655. *out_reg = MC_SEQ_RAS_TIMING_LP >> 2;
  3656. break;
  3657. case MC_SEQ_DLL_STBY >> 2:
  3658. *out_reg = MC_SEQ_DLL_STBY_LP >> 2;
  3659. break;
  3660. case MC_SEQ_G5PDX_CMD0 >> 2:
  3661. *out_reg = MC_SEQ_G5PDX_CMD0_LP >> 2;
  3662. break;
  3663. case MC_SEQ_G5PDX_CMD1 >> 2:
  3664. *out_reg = MC_SEQ_G5PDX_CMD1_LP >> 2;
  3665. break;
  3666. case MC_SEQ_G5PDX_CTRL >> 2:
  3667. *out_reg = MC_SEQ_G5PDX_CTRL_LP >> 2;
  3668. break;
  3669. case MC_SEQ_CAS_TIMING >> 2:
  3670. *out_reg = MC_SEQ_CAS_TIMING_LP >> 2;
  3671. break;
  3672. case MC_SEQ_MISC_TIMING >> 2:
  3673. *out_reg = MC_SEQ_MISC_TIMING_LP >> 2;
  3674. break;
  3675. case MC_SEQ_MISC_TIMING2 >> 2:
  3676. *out_reg = MC_SEQ_MISC_TIMING2_LP >> 2;
  3677. break;
  3678. case MC_SEQ_PMG_DVS_CMD >> 2:
  3679. *out_reg = MC_SEQ_PMG_DVS_CMD_LP >> 2;
  3680. break;
  3681. case MC_SEQ_PMG_DVS_CTL >> 2:
  3682. *out_reg = MC_SEQ_PMG_DVS_CTL_LP >> 2;
  3683. break;
  3684. case MC_SEQ_RD_CTL_D0 >> 2:
  3685. *out_reg = MC_SEQ_RD_CTL_D0_LP >> 2;
  3686. break;
  3687. case MC_SEQ_RD_CTL_D1 >> 2:
  3688. *out_reg = MC_SEQ_RD_CTL_D1_LP >> 2;
  3689. break;
  3690. case MC_SEQ_WR_CTL_D0 >> 2:
  3691. *out_reg = MC_SEQ_WR_CTL_D0_LP >> 2;
  3692. break;
  3693. case MC_SEQ_WR_CTL_D1 >> 2:
  3694. *out_reg = MC_SEQ_WR_CTL_D1_LP >> 2;
  3695. break;
  3696. case MC_PMG_CMD_EMRS >> 2:
  3697. *out_reg = MC_SEQ_PMG_CMD_EMRS_LP >> 2;
  3698. break;
  3699. case MC_PMG_CMD_MRS >> 2:
  3700. *out_reg = MC_SEQ_PMG_CMD_MRS_LP >> 2;
  3701. break;
  3702. case MC_PMG_CMD_MRS1 >> 2:
  3703. *out_reg = MC_SEQ_PMG_CMD_MRS1_LP >> 2;
  3704. break;
  3705. case MC_SEQ_PMG_TIMING >> 2:
  3706. *out_reg = MC_SEQ_PMG_TIMING_LP >> 2;
  3707. break;
  3708. case MC_PMG_CMD_MRS2 >> 2:
  3709. *out_reg = MC_SEQ_PMG_CMD_MRS2_LP >> 2;
  3710. break;
  3711. case MC_SEQ_WR_CTL_2 >> 2:
  3712. *out_reg = MC_SEQ_WR_CTL_2_LP >> 2;
  3713. break;
  3714. default:
  3715. result = false;
  3716. break;
  3717. }
  3718. return result;
  3719. }
  3720. static void ci_set_valid_flag(struct ci_mc_reg_table *table)
  3721. {
  3722. u8 i, j;
  3723. for (i = 0; i < table->last; i++) {
  3724. for (j = 1; j < table->num_entries; j++) {
  3725. if (table->mc_reg_table_entry[j-1].mc_data[i] !=
  3726. table->mc_reg_table_entry[j].mc_data[i]) {
  3727. table->valid_flag |= 1 << i;
  3728. break;
  3729. }
  3730. }
  3731. }
  3732. }
  3733. static void ci_set_s0_mc_reg_index(struct ci_mc_reg_table *table)
  3734. {
  3735. u32 i;
  3736. u16 address;
  3737. for (i = 0; i < table->last; i++) {
  3738. table->mc_reg_address[i].s0 =
  3739. ci_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address) ?
  3740. address : table->mc_reg_address[i].s1;
  3741. }
  3742. }
  3743. static int ci_copy_vbios_mc_reg_table(const struct atom_mc_reg_table *table,
  3744. struct ci_mc_reg_table *ci_table)
  3745. {
  3746. u8 i, j;
  3747. if (table->last > SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3748. return -EINVAL;
  3749. if (table->num_entries > MAX_AC_TIMING_ENTRIES)
  3750. return -EINVAL;
  3751. for (i = 0; i < table->last; i++)
  3752. ci_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
  3753. ci_table->last = table->last;
  3754. for (i = 0; i < table->num_entries; i++) {
  3755. ci_table->mc_reg_table_entry[i].mclk_max =
  3756. table->mc_reg_table_entry[i].mclk_max;
  3757. for (j = 0; j < table->last; j++)
  3758. ci_table->mc_reg_table_entry[i].mc_data[j] =
  3759. table->mc_reg_table_entry[i].mc_data[j];
  3760. }
  3761. ci_table->num_entries = table->num_entries;
  3762. return 0;
  3763. }
  3764. static int ci_register_patching_mc_seq(struct radeon_device *rdev,
  3765. struct ci_mc_reg_table *table)
  3766. {
  3767. u8 i, k;
  3768. u32 tmp;
  3769. bool patch;
  3770. tmp = RREG32(MC_SEQ_MISC0);
  3771. patch = ((tmp & 0x0000f00) == 0x300) ? true : false;
  3772. if (patch &&
  3773. ((rdev->pdev->device == 0x67B0) ||
  3774. (rdev->pdev->device == 0x67B1))) {
  3775. for (i = 0; i < table->last; i++) {
  3776. if (table->last >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3777. return -EINVAL;
  3778. switch(table->mc_reg_address[i].s1 >> 2) {
  3779. case MC_SEQ_MISC1:
  3780. for (k = 0; k < table->num_entries; k++) {
  3781. if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
  3782. (table->mc_reg_table_entry[k].mclk_max == 137500))
  3783. table->mc_reg_table_entry[k].mc_data[i] =
  3784. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFF8) |
  3785. 0x00000007;
  3786. }
  3787. break;
  3788. case MC_SEQ_WR_CTL_D0:
  3789. for (k = 0; k < table->num_entries; k++) {
  3790. if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
  3791. (table->mc_reg_table_entry[k].mclk_max == 137500))
  3792. table->mc_reg_table_entry[k].mc_data[i] =
  3793. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFFF0F00) |
  3794. 0x0000D0DD;
  3795. }
  3796. break;
  3797. case MC_SEQ_WR_CTL_D1:
  3798. for (k = 0; k < table->num_entries; k++) {
  3799. if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
  3800. (table->mc_reg_table_entry[k].mclk_max == 137500))
  3801. table->mc_reg_table_entry[k].mc_data[i] =
  3802. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFFF0F00) |
  3803. 0x0000D0DD;
  3804. }
  3805. break;
  3806. case MC_SEQ_WR_CTL_2:
  3807. for (k = 0; k < table->num_entries; k++) {
  3808. if ((table->mc_reg_table_entry[k].mclk_max == 125000) ||
  3809. (table->mc_reg_table_entry[k].mclk_max == 137500))
  3810. table->mc_reg_table_entry[k].mc_data[i] = 0;
  3811. }
  3812. break;
  3813. case MC_SEQ_CAS_TIMING:
  3814. for (k = 0; k < table->num_entries; k++) {
  3815. if (table->mc_reg_table_entry[k].mclk_max == 125000)
  3816. table->mc_reg_table_entry[k].mc_data[i] =
  3817. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFE0FE0F) |
  3818. 0x000C0140;
  3819. else if (table->mc_reg_table_entry[k].mclk_max == 137500)
  3820. table->mc_reg_table_entry[k].mc_data[i] =
  3821. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFE0FE0F) |
  3822. 0x000C0150;
  3823. }
  3824. break;
  3825. case MC_SEQ_MISC_TIMING:
  3826. for (k = 0; k < table->num_entries; k++) {
  3827. if (table->mc_reg_table_entry[k].mclk_max == 125000)
  3828. table->mc_reg_table_entry[k].mc_data[i] =
  3829. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFE0) |
  3830. 0x00000030;
  3831. else if (table->mc_reg_table_entry[k].mclk_max == 137500)
  3832. table->mc_reg_table_entry[k].mc_data[i] =
  3833. (table->mc_reg_table_entry[k].mc_data[i] & 0xFFFFFFE0) |
  3834. 0x00000035;
  3835. }
  3836. break;
  3837. default:
  3838. break;
  3839. }
  3840. }
  3841. WREG32(MC_SEQ_IO_DEBUG_INDEX, 3);
  3842. tmp = RREG32(MC_SEQ_IO_DEBUG_DATA);
  3843. tmp = (tmp & 0xFFF8FFFF) | (1 << 16);
  3844. WREG32(MC_SEQ_IO_DEBUG_INDEX, 3);
  3845. WREG32(MC_SEQ_IO_DEBUG_DATA, tmp);
  3846. }
  3847. return 0;
  3848. }
  3849. static int ci_initialize_mc_reg_table(struct radeon_device *rdev)
  3850. {
  3851. struct ci_power_info *pi = ci_get_pi(rdev);
  3852. struct atom_mc_reg_table *table;
  3853. struct ci_mc_reg_table *ci_table = &pi->mc_reg_table;
  3854. u8 module_index = rv770_get_memory_module_index(rdev);
  3855. int ret;
  3856. table = kzalloc(sizeof(struct atom_mc_reg_table), GFP_KERNEL);
  3857. if (!table)
  3858. return -ENOMEM;
  3859. WREG32(MC_SEQ_RAS_TIMING_LP, RREG32(MC_SEQ_RAS_TIMING));
  3860. WREG32(MC_SEQ_CAS_TIMING_LP, RREG32(MC_SEQ_CAS_TIMING));
  3861. WREG32(MC_SEQ_DLL_STBY_LP, RREG32(MC_SEQ_DLL_STBY));
  3862. WREG32(MC_SEQ_G5PDX_CMD0_LP, RREG32(MC_SEQ_G5PDX_CMD0));
  3863. WREG32(MC_SEQ_G5PDX_CMD1_LP, RREG32(MC_SEQ_G5PDX_CMD1));
  3864. WREG32(MC_SEQ_G5PDX_CTRL_LP, RREG32(MC_SEQ_G5PDX_CTRL));
  3865. WREG32(MC_SEQ_PMG_DVS_CMD_LP, RREG32(MC_SEQ_PMG_DVS_CMD));
  3866. WREG32(MC_SEQ_PMG_DVS_CTL_LP, RREG32(MC_SEQ_PMG_DVS_CTL));
  3867. WREG32(MC_SEQ_MISC_TIMING_LP, RREG32(MC_SEQ_MISC_TIMING));
  3868. WREG32(MC_SEQ_MISC_TIMING2_LP, RREG32(MC_SEQ_MISC_TIMING2));
  3869. WREG32(MC_SEQ_PMG_CMD_EMRS_LP, RREG32(MC_PMG_CMD_EMRS));
  3870. WREG32(MC_SEQ_PMG_CMD_MRS_LP, RREG32(MC_PMG_CMD_MRS));
  3871. WREG32(MC_SEQ_PMG_CMD_MRS1_LP, RREG32(MC_PMG_CMD_MRS1));
  3872. WREG32(MC_SEQ_WR_CTL_D0_LP, RREG32(MC_SEQ_WR_CTL_D0));
  3873. WREG32(MC_SEQ_WR_CTL_D1_LP, RREG32(MC_SEQ_WR_CTL_D1));
  3874. WREG32(MC_SEQ_RD_CTL_D0_LP, RREG32(MC_SEQ_RD_CTL_D0));
  3875. WREG32(MC_SEQ_RD_CTL_D1_LP, RREG32(MC_SEQ_RD_CTL_D1));
  3876. WREG32(MC_SEQ_PMG_TIMING_LP, RREG32(MC_SEQ_PMG_TIMING));
  3877. WREG32(MC_SEQ_PMG_CMD_MRS2_LP, RREG32(MC_PMG_CMD_MRS2));
  3878. WREG32(MC_SEQ_WR_CTL_2_LP, RREG32(MC_SEQ_WR_CTL_2));
  3879. ret = radeon_atom_init_mc_reg_table(rdev, module_index, table);
  3880. if (ret)
  3881. goto init_mc_done;
  3882. ret = ci_copy_vbios_mc_reg_table(table, ci_table);
  3883. if (ret)
  3884. goto init_mc_done;
  3885. ci_set_s0_mc_reg_index(ci_table);
  3886. ret = ci_register_patching_mc_seq(rdev, ci_table);
  3887. if (ret)
  3888. goto init_mc_done;
  3889. ret = ci_set_mc_special_registers(rdev, ci_table);
  3890. if (ret)
  3891. goto init_mc_done;
  3892. ci_set_valid_flag(ci_table);
  3893. init_mc_done:
  3894. kfree(table);
  3895. return ret;
  3896. }
  3897. static int ci_populate_mc_reg_addresses(struct radeon_device *rdev,
  3898. SMU7_Discrete_MCRegisters *mc_reg_table)
  3899. {
  3900. struct ci_power_info *pi = ci_get_pi(rdev);
  3901. u32 i, j;
  3902. for (i = 0, j = 0; j < pi->mc_reg_table.last; j++) {
  3903. if (pi->mc_reg_table.valid_flag & (1 << j)) {
  3904. if (i >= SMU7_DISCRETE_MC_REGISTER_ARRAY_SIZE)
  3905. return -EINVAL;
  3906. mc_reg_table->address[i].s0 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s0);
  3907. mc_reg_table->address[i].s1 = cpu_to_be16(pi->mc_reg_table.mc_reg_address[j].s1);
  3908. i++;
  3909. }
  3910. }
  3911. mc_reg_table->last = (u8)i;
  3912. return 0;
  3913. }
  3914. static void ci_convert_mc_registers(const struct ci_mc_reg_entry *entry,
  3915. SMU7_Discrete_MCRegisterSet *data,
  3916. u32 num_entries, u32 valid_flag)
  3917. {
  3918. u32 i, j;
  3919. for (i = 0, j = 0; j < num_entries; j++) {
  3920. if (valid_flag & (1 << j)) {
  3921. data->value[i] = cpu_to_be32(entry->mc_data[j]);
  3922. i++;
  3923. }
  3924. }
  3925. }
  3926. static void ci_convert_mc_reg_table_entry_to_smc(struct radeon_device *rdev,
  3927. const u32 memory_clock,
  3928. SMU7_Discrete_MCRegisterSet *mc_reg_table_data)
  3929. {
  3930. struct ci_power_info *pi = ci_get_pi(rdev);
  3931. u32 i = 0;
  3932. for(i = 0; i < pi->mc_reg_table.num_entries; i++) {
  3933. if (memory_clock <= pi->mc_reg_table.mc_reg_table_entry[i].mclk_max)
  3934. break;
  3935. }
  3936. if ((i == pi->mc_reg_table.num_entries) && (i > 0))
  3937. --i;
  3938. ci_convert_mc_registers(&pi->mc_reg_table.mc_reg_table_entry[i],
  3939. mc_reg_table_data, pi->mc_reg_table.last,
  3940. pi->mc_reg_table.valid_flag);
  3941. }
  3942. static void ci_convert_mc_reg_table_to_smc(struct radeon_device *rdev,
  3943. SMU7_Discrete_MCRegisters *mc_reg_table)
  3944. {
  3945. struct ci_power_info *pi = ci_get_pi(rdev);
  3946. u32 i;
  3947. for (i = 0; i < pi->dpm_table.mclk_table.count; i++)
  3948. ci_convert_mc_reg_table_entry_to_smc(rdev,
  3949. pi->dpm_table.mclk_table.dpm_levels[i].value,
  3950. &mc_reg_table->data[i]);
  3951. }
  3952. static int ci_populate_initial_mc_reg_table(struct radeon_device *rdev)
  3953. {
  3954. struct ci_power_info *pi = ci_get_pi(rdev);
  3955. int ret;
  3956. memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
  3957. ret = ci_populate_mc_reg_addresses(rdev, &pi->smc_mc_reg_table);
  3958. if (ret)
  3959. return ret;
  3960. ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
  3961. return ci_copy_bytes_to_smc(rdev,
  3962. pi->mc_reg_table_start,
  3963. (u8 *)&pi->smc_mc_reg_table,
  3964. sizeof(SMU7_Discrete_MCRegisters),
  3965. pi->sram_end);
  3966. }
  3967. static int ci_update_and_upload_mc_reg_table(struct radeon_device *rdev)
  3968. {
  3969. struct ci_power_info *pi = ci_get_pi(rdev);
  3970. if (!(pi->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK))
  3971. return 0;
  3972. memset(&pi->smc_mc_reg_table, 0, sizeof(SMU7_Discrete_MCRegisters));
  3973. ci_convert_mc_reg_table_to_smc(rdev, &pi->smc_mc_reg_table);
  3974. return ci_copy_bytes_to_smc(rdev,
  3975. pi->mc_reg_table_start +
  3976. offsetof(SMU7_Discrete_MCRegisters, data[0]),
  3977. (u8 *)&pi->smc_mc_reg_table.data[0],
  3978. sizeof(SMU7_Discrete_MCRegisterSet) *
  3979. pi->dpm_table.mclk_table.count,
  3980. pi->sram_end);
  3981. }
  3982. static void ci_enable_voltage_control(struct radeon_device *rdev)
  3983. {
  3984. u32 tmp = RREG32_SMC(GENERAL_PWRMGT);
  3985. tmp |= VOLT_PWRMGT_EN;
  3986. WREG32_SMC(GENERAL_PWRMGT, tmp);
  3987. }
  3988. static enum radeon_pcie_gen ci_get_maximum_link_speed(struct radeon_device *rdev,
  3989. struct radeon_ps *radeon_state)
  3990. {
  3991. struct ci_ps *state = ci_get_ps(radeon_state);
  3992. int i;
  3993. u16 pcie_speed, max_speed = 0;
  3994. for (i = 0; i < state->performance_level_count; i++) {
  3995. pcie_speed = state->performance_levels[i].pcie_gen;
  3996. if (max_speed < pcie_speed)
  3997. max_speed = pcie_speed;
  3998. }
  3999. return max_speed;
  4000. }
  4001. static u16 ci_get_current_pcie_speed(struct radeon_device *rdev)
  4002. {
  4003. u32 speed_cntl = 0;
  4004. speed_cntl = RREG32_PCIE_PORT(PCIE_LC_SPEED_CNTL) & LC_CURRENT_DATA_RATE_MASK;
  4005. speed_cntl >>= LC_CURRENT_DATA_RATE_SHIFT;
  4006. return (u16)speed_cntl;
  4007. }
  4008. static int ci_get_current_pcie_lane_number(struct radeon_device *rdev)
  4009. {
  4010. u32 link_width = 0;
  4011. link_width = RREG32_PCIE_PORT(PCIE_LC_LINK_WIDTH_CNTL) & LC_LINK_WIDTH_RD_MASK;
  4012. link_width >>= LC_LINK_WIDTH_RD_SHIFT;
  4013. switch (link_width) {
  4014. case RADEON_PCIE_LC_LINK_WIDTH_X1:
  4015. return 1;
  4016. case RADEON_PCIE_LC_LINK_WIDTH_X2:
  4017. return 2;
  4018. case RADEON_PCIE_LC_LINK_WIDTH_X4:
  4019. return 4;
  4020. case RADEON_PCIE_LC_LINK_WIDTH_X8:
  4021. return 8;
  4022. case RADEON_PCIE_LC_LINK_WIDTH_X12:
  4023. /* not actually supported */
  4024. return 12;
  4025. case RADEON_PCIE_LC_LINK_WIDTH_X0:
  4026. case RADEON_PCIE_LC_LINK_WIDTH_X16:
  4027. default:
  4028. return 16;
  4029. }
  4030. }
  4031. static void ci_request_link_speed_change_before_state_change(struct radeon_device *rdev,
  4032. struct radeon_ps *radeon_new_state,
  4033. struct radeon_ps *radeon_current_state)
  4034. {
  4035. struct ci_power_info *pi = ci_get_pi(rdev);
  4036. enum radeon_pcie_gen target_link_speed =
  4037. ci_get_maximum_link_speed(rdev, radeon_new_state);
  4038. enum radeon_pcie_gen current_link_speed;
  4039. if (pi->force_pcie_gen == RADEON_PCIE_GEN_INVALID)
  4040. current_link_speed = ci_get_maximum_link_speed(rdev, radeon_current_state);
  4041. else
  4042. current_link_speed = pi->force_pcie_gen;
  4043. pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
  4044. pi->pspp_notify_required = false;
  4045. if (target_link_speed > current_link_speed) {
  4046. switch (target_link_speed) {
  4047. #ifdef CONFIG_ACPI
  4048. case RADEON_PCIE_GEN3:
  4049. if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN3, false) == 0)
  4050. break;
  4051. pi->force_pcie_gen = RADEON_PCIE_GEN2;
  4052. if (current_link_speed == RADEON_PCIE_GEN2)
  4053. break;
  4054. case RADEON_PCIE_GEN2:
  4055. if (radeon_acpi_pcie_performance_request(rdev, PCIE_PERF_REQ_PECI_GEN2, false) == 0)
  4056. break;
  4057. #endif
  4058. default:
  4059. pi->force_pcie_gen = ci_get_current_pcie_speed(rdev);
  4060. break;
  4061. }
  4062. } else {
  4063. if (target_link_speed < current_link_speed)
  4064. pi->pspp_notify_required = true;
  4065. }
  4066. }
  4067. static void ci_notify_link_speed_change_after_state_change(struct radeon_device *rdev,
  4068. struct radeon_ps *radeon_new_state,
  4069. struct radeon_ps *radeon_current_state)
  4070. {
  4071. struct ci_power_info *pi = ci_get_pi(rdev);
  4072. enum radeon_pcie_gen target_link_speed =
  4073. ci_get_maximum_link_speed(rdev, radeon_new_state);
  4074. u8 request;
  4075. if (pi->pspp_notify_required) {
  4076. if (target_link_speed == RADEON_PCIE_GEN3)
  4077. request = PCIE_PERF_REQ_PECI_GEN3;
  4078. else if (target_link_speed == RADEON_PCIE_GEN2)
  4079. request = PCIE_PERF_REQ_PECI_GEN2;
  4080. else
  4081. request = PCIE_PERF_REQ_PECI_GEN1;
  4082. if ((request == PCIE_PERF_REQ_PECI_GEN1) &&
  4083. (ci_get_current_pcie_speed(rdev) > 0))
  4084. return;
  4085. #ifdef CONFIG_ACPI
  4086. radeon_acpi_pcie_performance_request(rdev, request, false);
  4087. #endif
  4088. }
  4089. }
  4090. static int ci_set_private_data_variables_based_on_pptable(struct radeon_device *rdev)
  4091. {
  4092. struct ci_power_info *pi = ci_get_pi(rdev);
  4093. struct radeon_clock_voltage_dependency_table *allowed_sclk_vddc_table =
  4094. &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  4095. struct radeon_clock_voltage_dependency_table *allowed_mclk_vddc_table =
  4096. &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk;
  4097. struct radeon_clock_voltage_dependency_table *allowed_mclk_vddci_table =
  4098. &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk;
  4099. if (allowed_sclk_vddc_table == NULL)
  4100. return -EINVAL;
  4101. if (allowed_sclk_vddc_table->count < 1)
  4102. return -EINVAL;
  4103. if (allowed_mclk_vddc_table == NULL)
  4104. return -EINVAL;
  4105. if (allowed_mclk_vddc_table->count < 1)
  4106. return -EINVAL;
  4107. if (allowed_mclk_vddci_table == NULL)
  4108. return -EINVAL;
  4109. if (allowed_mclk_vddci_table->count < 1)
  4110. return -EINVAL;
  4111. pi->min_vddc_in_pp_table = allowed_sclk_vddc_table->entries[0].v;
  4112. pi->max_vddc_in_pp_table =
  4113. allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
  4114. pi->min_vddci_in_pp_table = allowed_mclk_vddci_table->entries[0].v;
  4115. pi->max_vddci_in_pp_table =
  4116. allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
  4117. rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk =
  4118. allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
  4119. rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.mclk =
  4120. allowed_mclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].clk;
  4121. rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddc =
  4122. allowed_sclk_vddc_table->entries[allowed_sclk_vddc_table->count - 1].v;
  4123. rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.vddci =
  4124. allowed_mclk_vddci_table->entries[allowed_mclk_vddci_table->count - 1].v;
  4125. return 0;
  4126. }
  4127. static void ci_patch_with_vddc_leakage(struct radeon_device *rdev, u16 *vddc)
  4128. {
  4129. struct ci_power_info *pi = ci_get_pi(rdev);
  4130. struct ci_leakage_voltage *leakage_table = &pi->vddc_leakage;
  4131. u32 leakage_index;
  4132. for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
  4133. if (leakage_table->leakage_id[leakage_index] == *vddc) {
  4134. *vddc = leakage_table->actual_voltage[leakage_index];
  4135. break;
  4136. }
  4137. }
  4138. }
  4139. static void ci_patch_with_vddci_leakage(struct radeon_device *rdev, u16 *vddci)
  4140. {
  4141. struct ci_power_info *pi = ci_get_pi(rdev);
  4142. struct ci_leakage_voltage *leakage_table = &pi->vddci_leakage;
  4143. u32 leakage_index;
  4144. for (leakage_index = 0; leakage_index < leakage_table->count; leakage_index++) {
  4145. if (leakage_table->leakage_id[leakage_index] == *vddci) {
  4146. *vddci = leakage_table->actual_voltage[leakage_index];
  4147. break;
  4148. }
  4149. }
  4150. }
  4151. static void ci_patch_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
  4152. struct radeon_clock_voltage_dependency_table *table)
  4153. {
  4154. u32 i;
  4155. if (table) {
  4156. for (i = 0; i < table->count; i++)
  4157. ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
  4158. }
  4159. }
  4160. static void ci_patch_clock_voltage_dependency_table_with_vddci_leakage(struct radeon_device *rdev,
  4161. struct radeon_clock_voltage_dependency_table *table)
  4162. {
  4163. u32 i;
  4164. if (table) {
  4165. for (i = 0; i < table->count; i++)
  4166. ci_patch_with_vddci_leakage(rdev, &table->entries[i].v);
  4167. }
  4168. }
  4169. static void ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
  4170. struct radeon_vce_clock_voltage_dependency_table *table)
  4171. {
  4172. u32 i;
  4173. if (table) {
  4174. for (i = 0; i < table->count; i++)
  4175. ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
  4176. }
  4177. }
  4178. static void ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(struct radeon_device *rdev,
  4179. struct radeon_uvd_clock_voltage_dependency_table *table)
  4180. {
  4181. u32 i;
  4182. if (table) {
  4183. for (i = 0; i < table->count; i++)
  4184. ci_patch_with_vddc_leakage(rdev, &table->entries[i].v);
  4185. }
  4186. }
  4187. static void ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(struct radeon_device *rdev,
  4188. struct radeon_phase_shedding_limits_table *table)
  4189. {
  4190. u32 i;
  4191. if (table) {
  4192. for (i = 0; i < table->count; i++)
  4193. ci_patch_with_vddc_leakage(rdev, &table->entries[i].voltage);
  4194. }
  4195. }
  4196. static void ci_patch_clock_voltage_limits_with_vddc_leakage(struct radeon_device *rdev,
  4197. struct radeon_clock_and_voltage_limits *table)
  4198. {
  4199. if (table) {
  4200. ci_patch_with_vddc_leakage(rdev, (u16 *)&table->vddc);
  4201. ci_patch_with_vddci_leakage(rdev, (u16 *)&table->vddci);
  4202. }
  4203. }
  4204. static void ci_patch_cac_leakage_table_with_vddc_leakage(struct radeon_device *rdev,
  4205. struct radeon_cac_leakage_table *table)
  4206. {
  4207. u32 i;
  4208. if (table) {
  4209. for (i = 0; i < table->count; i++)
  4210. ci_patch_with_vddc_leakage(rdev, &table->entries[i].vddc);
  4211. }
  4212. }
  4213. static void ci_patch_dependency_tables_with_leakage(struct radeon_device *rdev)
  4214. {
  4215. ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4216. &rdev->pm.dpm.dyn_state.vddc_dependency_on_sclk);
  4217. ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4218. &rdev->pm.dpm.dyn_state.vddc_dependency_on_mclk);
  4219. ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4220. &rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk);
  4221. ci_patch_clock_voltage_dependency_table_with_vddci_leakage(rdev,
  4222. &rdev->pm.dpm.dyn_state.vddci_dependency_on_mclk);
  4223. ci_patch_vce_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4224. &rdev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table);
  4225. ci_patch_uvd_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4226. &rdev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table);
  4227. ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4228. &rdev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table);
  4229. ci_patch_clock_voltage_dependency_table_with_vddc_leakage(rdev,
  4230. &rdev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table);
  4231. ci_patch_vddc_phase_shed_limit_table_with_vddc_leakage(rdev,
  4232. &rdev->pm.dpm.dyn_state.phase_shedding_limits_table);
  4233. ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
  4234. &rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
  4235. ci_patch_clock_voltage_limits_with_vddc_leakage(rdev,
  4236. &rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc);
  4237. ci_patch_cac_leakage_table_with_vddc_leakage(rdev,
  4238. &rdev->pm.dpm.dyn_state.cac_leakage_table);
  4239. }
  4240. static void ci_get_memory_type(struct radeon_device *rdev)
  4241. {
  4242. struct ci_power_info *pi = ci_get_pi(rdev);
  4243. u32 tmp;
  4244. tmp = RREG32(MC_SEQ_MISC0);
  4245. if (((tmp & MC_SEQ_MISC0_GDDR5_MASK) >> MC_SEQ_MISC0_GDDR5_SHIFT) ==
  4246. MC_SEQ_MISC0_GDDR5_VALUE)
  4247. pi->mem_gddr5 = true;
  4248. else
  4249. pi->mem_gddr5 = false;
  4250. }
  4251. static void ci_update_current_ps(struct radeon_device *rdev,
  4252. struct radeon_ps *rps)
  4253. {
  4254. struct ci_ps *new_ps = ci_get_ps(rps);
  4255. struct ci_power_info *pi = ci_get_pi(rdev);
  4256. pi->current_rps = *rps;
  4257. pi->current_ps = *new_ps;
  4258. pi->current_rps.ps_priv = &pi->current_ps;
  4259. }
  4260. static void ci_update_requested_ps(struct radeon_device *rdev,
  4261. struct radeon_ps *rps)
  4262. {
  4263. struct ci_ps *new_ps = ci_get_ps(rps);
  4264. struct ci_power_info *pi = ci_get_pi(rdev);
  4265. pi->requested_rps = *rps;
  4266. pi->requested_ps = *new_ps;
  4267. pi->requested_rps.ps_priv = &pi->requested_ps;
  4268. }
  4269. int ci_dpm_pre_set_power_state(struct radeon_device *rdev)
  4270. {
  4271. struct ci_power_info *pi = ci_get_pi(rdev);
  4272. struct radeon_ps requested_ps = *rdev->pm.dpm.requested_ps;
  4273. struct radeon_ps *new_ps = &requested_ps;
  4274. ci_update_requested_ps(rdev, new_ps);
  4275. ci_apply_state_adjust_rules(rdev, &pi->requested_rps);
  4276. return 0;
  4277. }
  4278. void ci_dpm_post_set_power_state(struct radeon_device *rdev)
  4279. {
  4280. struct ci_power_info *pi = ci_get_pi(rdev);
  4281. struct radeon_ps *new_ps = &pi->requested_rps;
  4282. ci_update_current_ps(rdev, new_ps);
  4283. }
  4284. void ci_dpm_setup_asic(struct radeon_device *rdev)
  4285. {
  4286. int r;
  4287. r = ci_mc_load_microcode(rdev);
  4288. if (r)
  4289. DRM_ERROR("Failed to load MC firmware!\n");
  4290. ci_read_clock_registers(rdev);
  4291. ci_get_memory_type(rdev);
  4292. ci_enable_acpi_power_management(rdev);
  4293. ci_init_sclk_t(rdev);
  4294. }
  4295. int ci_dpm_enable(struct radeon_device *rdev)
  4296. {
  4297. struct ci_power_info *pi = ci_get_pi(rdev);
  4298. struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
  4299. int ret;
  4300. if (ci_is_smc_running(rdev))
  4301. return -EINVAL;
  4302. if (pi->voltage_control != CISLANDS_VOLTAGE_CONTROL_NONE) {
  4303. ci_enable_voltage_control(rdev);
  4304. ret = ci_construct_voltage_tables(rdev);
  4305. if (ret) {
  4306. DRM_ERROR("ci_construct_voltage_tables failed\n");
  4307. return ret;
  4308. }
  4309. }
  4310. if (pi->caps_dynamic_ac_timing) {
  4311. ret = ci_initialize_mc_reg_table(rdev);
  4312. if (ret)
  4313. pi->caps_dynamic_ac_timing = false;
  4314. }
  4315. if (pi->dynamic_ss)
  4316. ci_enable_spread_spectrum(rdev, true);
  4317. if (pi->thermal_protection)
  4318. ci_enable_thermal_protection(rdev, true);
  4319. ci_program_sstp(rdev);
  4320. ci_enable_display_gap(rdev);
  4321. ci_program_vc(rdev);
  4322. ret = ci_upload_firmware(rdev);
  4323. if (ret) {
  4324. DRM_ERROR("ci_upload_firmware failed\n");
  4325. return ret;
  4326. }
  4327. ret = ci_process_firmware_header(rdev);
  4328. if (ret) {
  4329. DRM_ERROR("ci_process_firmware_header failed\n");
  4330. return ret;
  4331. }
  4332. ret = ci_initial_switch_from_arb_f0_to_f1(rdev);
  4333. if (ret) {
  4334. DRM_ERROR("ci_initial_switch_from_arb_f0_to_f1 failed\n");
  4335. return ret;
  4336. }
  4337. ret = ci_init_smc_table(rdev);
  4338. if (ret) {
  4339. DRM_ERROR("ci_init_smc_table failed\n");
  4340. return ret;
  4341. }
  4342. ret = ci_init_arb_table_index(rdev);
  4343. if (ret) {
  4344. DRM_ERROR("ci_init_arb_table_index failed\n");
  4345. return ret;
  4346. }
  4347. if (pi->caps_dynamic_ac_timing) {
  4348. ret = ci_populate_initial_mc_reg_table(rdev);
  4349. if (ret) {
  4350. DRM_ERROR("ci_populate_initial_mc_reg_table failed\n");
  4351. return ret;
  4352. }
  4353. }
  4354. ret = ci_populate_pm_base(rdev);
  4355. if (ret) {
  4356. DRM_ERROR("ci_populate_pm_base failed\n");
  4357. return ret;
  4358. }
  4359. ci_dpm_start_smc(rdev);
  4360. ci_enable_vr_hot_gpio_interrupt(rdev);
  4361. ret = ci_notify_smc_display_change(rdev, false);
  4362. if (ret) {
  4363. DRM_ERROR("ci_notify_smc_display_change failed\n");
  4364. return ret;
  4365. }
  4366. ci_enable_sclk_control(rdev, true);
  4367. ret = ci_enable_ulv(rdev, true);
  4368. if (ret) {
  4369. DRM_ERROR("ci_enable_ulv failed\n");
  4370. return ret;
  4371. }
  4372. ret = ci_enable_ds_master_switch(rdev, true);
  4373. if (ret) {
  4374. DRM_ERROR("ci_enable_ds_master_switch failed\n");
  4375. return ret;
  4376. }
  4377. ret = ci_start_dpm(rdev);
  4378. if (ret) {
  4379. DRM_ERROR("ci_start_dpm failed\n");
  4380. return ret;
  4381. }
  4382. ret = ci_enable_didt(rdev, true);
  4383. if (ret) {
  4384. DRM_ERROR("ci_enable_didt failed\n");
  4385. return ret;
  4386. }
  4387. ret = ci_enable_smc_cac(rdev, true);
  4388. if (ret) {
  4389. DRM_ERROR("ci_enable_smc_cac failed\n");
  4390. return ret;
  4391. }
  4392. ret = ci_enable_power_containment(rdev, true);
  4393. if (ret) {
  4394. DRM_ERROR("ci_enable_power_containment failed\n");
  4395. return ret;
  4396. }
  4397. ret = ci_power_control_set_level(rdev);
  4398. if (ret) {
  4399. DRM_ERROR("ci_power_control_set_level failed\n");
  4400. return ret;
  4401. }
  4402. ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, true);
  4403. ret = ci_enable_thermal_based_sclk_dpm(rdev, true);
  4404. if (ret) {
  4405. DRM_ERROR("ci_enable_thermal_based_sclk_dpm failed\n");
  4406. return ret;
  4407. }
  4408. ci_thermal_start_thermal_controller(rdev);
  4409. ci_update_current_ps(rdev, boot_ps);
  4410. return 0;
  4411. }
  4412. static int ci_set_temperature_range(struct radeon_device *rdev)
  4413. {
  4414. int ret;
  4415. ret = ci_thermal_enable_alert(rdev, false);
  4416. if (ret)
  4417. return ret;
  4418. ret = ci_thermal_set_temperature_range(rdev, R600_TEMP_RANGE_MIN, R600_TEMP_RANGE_MAX);
  4419. if (ret)
  4420. return ret;
  4421. ret = ci_thermal_enable_alert(rdev, true);
  4422. if (ret)
  4423. return ret;
  4424. return ret;
  4425. }
  4426. int ci_dpm_late_enable(struct radeon_device *rdev)
  4427. {
  4428. int ret;
  4429. ret = ci_set_temperature_range(rdev);
  4430. if (ret)
  4431. return ret;
  4432. ci_dpm_powergate_uvd(rdev, true);
  4433. return 0;
  4434. }
  4435. void ci_dpm_disable(struct radeon_device *rdev)
  4436. {
  4437. struct ci_power_info *pi = ci_get_pi(rdev);
  4438. struct radeon_ps *boot_ps = rdev->pm.dpm.boot_ps;
  4439. ci_dpm_powergate_uvd(rdev, false);
  4440. if (!ci_is_smc_running(rdev))
  4441. return;
  4442. ci_thermal_stop_thermal_controller(rdev);
  4443. if (pi->thermal_protection)
  4444. ci_enable_thermal_protection(rdev, false);
  4445. ci_enable_power_containment(rdev, false);
  4446. ci_enable_smc_cac(rdev, false);
  4447. ci_enable_didt(rdev, false);
  4448. ci_enable_spread_spectrum(rdev, false);
  4449. ci_enable_auto_throttle_source(rdev, RADEON_DPM_AUTO_THROTTLE_SRC_THERMAL, false);
  4450. ci_stop_dpm(rdev);
  4451. ci_enable_ds_master_switch(rdev, false);
  4452. ci_enable_ulv(rdev, false);
  4453. ci_clear_vc(rdev);
  4454. ci_reset_to_default(rdev);
  4455. ci_dpm_stop_smc(rdev);
  4456. ci_force_switch_to_arb_f0(rdev);
  4457. ci_enable_thermal_based_sclk_dpm(rdev, false);
  4458. ci_update_current_ps(rdev, boot_ps);
  4459. }
  4460. int ci_dpm_set_power_state(struct radeon_device *rdev)
  4461. {
  4462. struct ci_power_info *pi = ci_get_pi(rdev);
  4463. struct radeon_ps *new_ps = &pi->requested_rps;
  4464. struct radeon_ps *old_ps = &pi->current_rps;
  4465. int ret;
  4466. ci_find_dpm_states_clocks_in_dpm_table(rdev, new_ps);
  4467. if (pi->pcie_performance_request)
  4468. ci_request_link_speed_change_before_state_change(rdev, new_ps, old_ps);
  4469. ret = ci_freeze_sclk_mclk_dpm(rdev);
  4470. if (ret) {
  4471. DRM_ERROR("ci_freeze_sclk_mclk_dpm failed\n");
  4472. return ret;
  4473. }
  4474. ret = ci_populate_and_upload_sclk_mclk_dpm_levels(rdev, new_ps);
  4475. if (ret) {
  4476. DRM_ERROR("ci_populate_and_upload_sclk_mclk_dpm_levels failed\n");
  4477. return ret;
  4478. }
  4479. ret = ci_generate_dpm_level_enable_mask(rdev, new_ps);
  4480. if (ret) {
  4481. DRM_ERROR("ci_generate_dpm_level_enable_mask failed\n");
  4482. return ret;
  4483. }
  4484. ret = ci_update_vce_dpm(rdev, new_ps, old_ps);
  4485. if (ret) {
  4486. DRM_ERROR("ci_update_vce_dpm failed\n");
  4487. return ret;
  4488. }
  4489. ret = ci_update_sclk_t(rdev);
  4490. if (ret) {
  4491. DRM_ERROR("ci_update_sclk_t failed\n");
  4492. return ret;
  4493. }
  4494. if (pi->caps_dynamic_ac_timing) {
  4495. ret = ci_update_and_upload_mc_reg_table(rdev);
  4496. if (ret) {
  4497. DRM_ERROR("ci_update_and_upload_mc_reg_table failed\n");
  4498. return ret;
  4499. }
  4500. }
  4501. ret = ci_program_memory_timing_parameters(rdev);
  4502. if (ret) {
  4503. DRM_ERROR("ci_program_memory_timing_parameters failed\n");
  4504. return ret;
  4505. }
  4506. ret = ci_unfreeze_sclk_mclk_dpm(rdev);
  4507. if (ret) {
  4508. DRM_ERROR("ci_unfreeze_sclk_mclk_dpm failed\n");
  4509. return ret;
  4510. }
  4511. ret = ci_upload_dpm_level_enable_mask(rdev);
  4512. if (ret) {
  4513. DRM_ERROR("ci_upload_dpm_level_enable_mask failed\n");
  4514. return ret;
  4515. }
  4516. if (pi->pcie_performance_request)
  4517. ci_notify_link_speed_change_after_state_change(rdev, new_ps, old_ps);
  4518. return 0;
  4519. }
  4520. #if 0
  4521. void ci_dpm_reset_asic(struct radeon_device *rdev)
  4522. {
  4523. ci_set_boot_state(rdev);
  4524. }
  4525. #endif
  4526. void ci_dpm_display_configuration_changed(struct radeon_device *rdev)
  4527. {
  4528. ci_program_display_gap(rdev);
  4529. }
  4530. union power_info {
  4531. struct _ATOM_POWERPLAY_INFO info;
  4532. struct _ATOM_POWERPLAY_INFO_V2 info_2;
  4533. struct _ATOM_POWERPLAY_INFO_V3 info_3;
  4534. struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
  4535. struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
  4536. struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
  4537. };
  4538. union pplib_clock_info {
  4539. struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
  4540. struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
  4541. struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
  4542. struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
  4543. struct _ATOM_PPLIB_SI_CLOCK_INFO si;
  4544. struct _ATOM_PPLIB_CI_CLOCK_INFO ci;
  4545. };
  4546. union pplib_power_state {
  4547. struct _ATOM_PPLIB_STATE v1;
  4548. struct _ATOM_PPLIB_STATE_V2 v2;
  4549. };
  4550. static void ci_parse_pplib_non_clock_info(struct radeon_device *rdev,
  4551. struct radeon_ps *rps,
  4552. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
  4553. u8 table_rev)
  4554. {
  4555. rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
  4556. rps->class = le16_to_cpu(non_clock_info->usClassification);
  4557. rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
  4558. if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
  4559. rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
  4560. rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
  4561. } else {
  4562. rps->vclk = 0;
  4563. rps->dclk = 0;
  4564. }
  4565. if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT)
  4566. rdev->pm.dpm.boot_ps = rps;
  4567. if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
  4568. rdev->pm.dpm.uvd_ps = rps;
  4569. }
  4570. static void ci_parse_pplib_clock_info(struct radeon_device *rdev,
  4571. struct radeon_ps *rps, int index,
  4572. union pplib_clock_info *clock_info)
  4573. {
  4574. struct ci_power_info *pi = ci_get_pi(rdev);
  4575. struct ci_ps *ps = ci_get_ps(rps);
  4576. struct ci_pl *pl = &ps->performance_levels[index];
  4577. ps->performance_level_count = index + 1;
  4578. pl->sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
  4579. pl->sclk |= clock_info->ci.ucEngineClockHigh << 16;
  4580. pl->mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
  4581. pl->mclk |= clock_info->ci.ucMemoryClockHigh << 16;
  4582. pl->pcie_gen = r600_get_pcie_gen_support(rdev,
  4583. pi->sys_pcie_mask,
  4584. pi->vbios_boot_state.pcie_gen_bootup_value,
  4585. clock_info->ci.ucPCIEGen);
  4586. pl->pcie_lane = r600_get_pcie_lane_support(rdev,
  4587. pi->vbios_boot_state.pcie_lane_bootup_value,
  4588. le16_to_cpu(clock_info->ci.usPCIELane));
  4589. if (rps->class & ATOM_PPLIB_CLASSIFICATION_ACPI) {
  4590. pi->acpi_pcie_gen = pl->pcie_gen;
  4591. }
  4592. if (rps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV) {
  4593. pi->ulv.supported = true;
  4594. pi->ulv.pl = *pl;
  4595. pi->ulv.cg_ulv_parameter = CISLANDS_CGULVPARAMETER_DFLT;
  4596. }
  4597. /* patch up boot state */
  4598. if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
  4599. pl->mclk = pi->vbios_boot_state.mclk_bootup_value;
  4600. pl->sclk = pi->vbios_boot_state.sclk_bootup_value;
  4601. pl->pcie_gen = pi->vbios_boot_state.pcie_gen_bootup_value;
  4602. pl->pcie_lane = pi->vbios_boot_state.pcie_lane_bootup_value;
  4603. }
  4604. switch (rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) {
  4605. case ATOM_PPLIB_CLASSIFICATION_UI_BATTERY:
  4606. pi->use_pcie_powersaving_levels = true;
  4607. if (pi->pcie_gen_powersaving.max < pl->pcie_gen)
  4608. pi->pcie_gen_powersaving.max = pl->pcie_gen;
  4609. if (pi->pcie_gen_powersaving.min > pl->pcie_gen)
  4610. pi->pcie_gen_powersaving.min = pl->pcie_gen;
  4611. if (pi->pcie_lane_powersaving.max < pl->pcie_lane)
  4612. pi->pcie_lane_powersaving.max = pl->pcie_lane;
  4613. if (pi->pcie_lane_powersaving.min > pl->pcie_lane)
  4614. pi->pcie_lane_powersaving.min = pl->pcie_lane;
  4615. break;
  4616. case ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE:
  4617. pi->use_pcie_performance_levels = true;
  4618. if (pi->pcie_gen_performance.max < pl->pcie_gen)
  4619. pi->pcie_gen_performance.max = pl->pcie_gen;
  4620. if (pi->pcie_gen_performance.min > pl->pcie_gen)
  4621. pi->pcie_gen_performance.min = pl->pcie_gen;
  4622. if (pi->pcie_lane_performance.max < pl->pcie_lane)
  4623. pi->pcie_lane_performance.max = pl->pcie_lane;
  4624. if (pi->pcie_lane_performance.min > pl->pcie_lane)
  4625. pi->pcie_lane_performance.min = pl->pcie_lane;
  4626. break;
  4627. default:
  4628. break;
  4629. }
  4630. }
  4631. static int ci_parse_power_table(struct radeon_device *rdev)
  4632. {
  4633. struct radeon_mode_info *mode_info = &rdev->mode_info;
  4634. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
  4635. union pplib_power_state *power_state;
  4636. int i, j, k, non_clock_array_index, clock_array_index;
  4637. union pplib_clock_info *clock_info;
  4638. struct _StateArray *state_array;
  4639. struct _ClockInfoArray *clock_info_array;
  4640. struct _NonClockInfoArray *non_clock_info_array;
  4641. union power_info *power_info;
  4642. int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
  4643. u16 data_offset;
  4644. u8 frev, crev;
  4645. u8 *power_state_offset;
  4646. struct ci_ps *ps;
  4647. if (!atom_parse_data_header(mode_info->atom_context, index, NULL,
  4648. &frev, &crev, &data_offset))
  4649. return -EINVAL;
  4650. power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
  4651. state_array = (struct _StateArray *)
  4652. (mode_info->atom_context->bios + data_offset +
  4653. le16_to_cpu(power_info->pplib.usStateArrayOffset));
  4654. clock_info_array = (struct _ClockInfoArray *)
  4655. (mode_info->atom_context->bios + data_offset +
  4656. le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
  4657. non_clock_info_array = (struct _NonClockInfoArray *)
  4658. (mode_info->atom_context->bios + data_offset +
  4659. le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
  4660. rdev->pm.dpm.ps = kzalloc(sizeof(struct radeon_ps) *
  4661. state_array->ucNumEntries, GFP_KERNEL);
  4662. if (!rdev->pm.dpm.ps)
  4663. return -ENOMEM;
  4664. power_state_offset = (u8 *)state_array->states;
  4665. for (i = 0; i < state_array->ucNumEntries; i++) {
  4666. u8 *idx;
  4667. power_state = (union pplib_power_state *)power_state_offset;
  4668. non_clock_array_index = power_state->v2.nonClockInfoIndex;
  4669. non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
  4670. &non_clock_info_array->nonClockInfo[non_clock_array_index];
  4671. if (!rdev->pm.power_state[i].clock_info)
  4672. return -EINVAL;
  4673. ps = kzalloc(sizeof(struct ci_ps), GFP_KERNEL);
  4674. if (ps == NULL) {
  4675. kfree(rdev->pm.dpm.ps);
  4676. return -ENOMEM;
  4677. }
  4678. rdev->pm.dpm.ps[i].ps_priv = ps;
  4679. ci_parse_pplib_non_clock_info(rdev, &rdev->pm.dpm.ps[i],
  4680. non_clock_info,
  4681. non_clock_info_array->ucEntrySize);
  4682. k = 0;
  4683. idx = (u8 *)&power_state->v2.clockInfoIndex[0];
  4684. for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
  4685. clock_array_index = idx[j];
  4686. if (clock_array_index >= clock_info_array->ucNumEntries)
  4687. continue;
  4688. if (k >= CISLANDS_MAX_HARDWARE_POWERLEVELS)
  4689. break;
  4690. clock_info = (union pplib_clock_info *)
  4691. ((u8 *)&clock_info_array->clockInfo[0] +
  4692. (clock_array_index * clock_info_array->ucEntrySize));
  4693. ci_parse_pplib_clock_info(rdev,
  4694. &rdev->pm.dpm.ps[i], k,
  4695. clock_info);
  4696. k++;
  4697. }
  4698. power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
  4699. }
  4700. rdev->pm.dpm.num_ps = state_array->ucNumEntries;
  4701. /* fill in the vce power states */
  4702. for (i = 0; i < RADEON_MAX_VCE_LEVELS; i++) {
  4703. u32 sclk, mclk;
  4704. clock_array_index = rdev->pm.dpm.vce_states[i].clk_idx;
  4705. clock_info = (union pplib_clock_info *)
  4706. &clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
  4707. sclk = le16_to_cpu(clock_info->ci.usEngineClockLow);
  4708. sclk |= clock_info->ci.ucEngineClockHigh << 16;
  4709. mclk = le16_to_cpu(clock_info->ci.usMemoryClockLow);
  4710. mclk |= clock_info->ci.ucMemoryClockHigh << 16;
  4711. rdev->pm.dpm.vce_states[i].sclk = sclk;
  4712. rdev->pm.dpm.vce_states[i].mclk = mclk;
  4713. }
  4714. return 0;
  4715. }
  4716. static int ci_get_vbios_boot_values(struct radeon_device *rdev,
  4717. struct ci_vbios_boot_state *boot_state)
  4718. {
  4719. struct radeon_mode_info *mode_info = &rdev->mode_info;
  4720. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  4721. ATOM_FIRMWARE_INFO_V2_2 *firmware_info;
  4722. u8 frev, crev;
  4723. u16 data_offset;
  4724. if (atom_parse_data_header(mode_info->atom_context, index, NULL,
  4725. &frev, &crev, &data_offset)) {
  4726. firmware_info =
  4727. (ATOM_FIRMWARE_INFO_V2_2 *)(mode_info->atom_context->bios +
  4728. data_offset);
  4729. boot_state->mvdd_bootup_value = le16_to_cpu(firmware_info->usBootUpMVDDCVoltage);
  4730. boot_state->vddc_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCVoltage);
  4731. boot_state->vddci_bootup_value = le16_to_cpu(firmware_info->usBootUpVDDCIVoltage);
  4732. boot_state->pcie_gen_bootup_value = ci_get_current_pcie_speed(rdev);
  4733. boot_state->pcie_lane_bootup_value = ci_get_current_pcie_lane_number(rdev);
  4734. boot_state->sclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultEngineClock);
  4735. boot_state->mclk_bootup_value = le32_to_cpu(firmware_info->ulDefaultMemoryClock);
  4736. return 0;
  4737. }
  4738. return -EINVAL;
  4739. }
  4740. void ci_dpm_fini(struct radeon_device *rdev)
  4741. {
  4742. int i;
  4743. for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
  4744. kfree(rdev->pm.dpm.ps[i].ps_priv);
  4745. }
  4746. kfree(rdev->pm.dpm.ps);
  4747. kfree(rdev->pm.dpm.priv);
  4748. kfree(rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries);
  4749. r600_free_extended_power_table(rdev);
  4750. }
  4751. int ci_dpm_init(struct radeon_device *rdev)
  4752. {
  4753. int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
  4754. SMU7_Discrete_DpmTable *dpm_table;
  4755. struct radeon_gpio_rec gpio;
  4756. u16 data_offset, size;
  4757. u8 frev, crev;
  4758. struct ci_power_info *pi;
  4759. int ret;
  4760. u32 mask;
  4761. pi = kzalloc(sizeof(struct ci_power_info), GFP_KERNEL);
  4762. if (pi == NULL)
  4763. return -ENOMEM;
  4764. rdev->pm.dpm.priv = pi;
  4765. ret = drm_pcie_get_speed_cap_mask(rdev->ddev, &mask);
  4766. if (ret)
  4767. pi->sys_pcie_mask = 0;
  4768. else
  4769. pi->sys_pcie_mask = mask;
  4770. pi->force_pcie_gen = RADEON_PCIE_GEN_INVALID;
  4771. pi->pcie_gen_performance.max = RADEON_PCIE_GEN1;
  4772. pi->pcie_gen_performance.min = RADEON_PCIE_GEN3;
  4773. pi->pcie_gen_powersaving.max = RADEON_PCIE_GEN1;
  4774. pi->pcie_gen_powersaving.min = RADEON_PCIE_GEN3;
  4775. pi->pcie_lane_performance.max = 0;
  4776. pi->pcie_lane_performance.min = 16;
  4777. pi->pcie_lane_powersaving.max = 0;
  4778. pi->pcie_lane_powersaving.min = 16;
  4779. ret = ci_get_vbios_boot_values(rdev, &pi->vbios_boot_state);
  4780. if (ret) {
  4781. ci_dpm_fini(rdev);
  4782. return ret;
  4783. }
  4784. ret = r600_get_platform_caps(rdev);
  4785. if (ret) {
  4786. ci_dpm_fini(rdev);
  4787. return ret;
  4788. }
  4789. ret = r600_parse_extended_power_table(rdev);
  4790. if (ret) {
  4791. ci_dpm_fini(rdev);
  4792. return ret;
  4793. }
  4794. ret = ci_parse_power_table(rdev);
  4795. if (ret) {
  4796. ci_dpm_fini(rdev);
  4797. return ret;
  4798. }
  4799. pi->dll_default_on = false;
  4800. pi->sram_end = SMC_RAM_END;
  4801. pi->activity_target[0] = CISLAND_TARGETACTIVITY_DFLT;
  4802. pi->activity_target[1] = CISLAND_TARGETACTIVITY_DFLT;
  4803. pi->activity_target[2] = CISLAND_TARGETACTIVITY_DFLT;
  4804. pi->activity_target[3] = CISLAND_TARGETACTIVITY_DFLT;
  4805. pi->activity_target[4] = CISLAND_TARGETACTIVITY_DFLT;
  4806. pi->activity_target[5] = CISLAND_TARGETACTIVITY_DFLT;
  4807. pi->activity_target[6] = CISLAND_TARGETACTIVITY_DFLT;
  4808. pi->activity_target[7] = CISLAND_TARGETACTIVITY_DFLT;
  4809. pi->mclk_activity_target = CISLAND_MCLK_TARGETACTIVITY_DFLT;
  4810. pi->sclk_dpm_key_disabled = 0;
  4811. pi->mclk_dpm_key_disabled = 0;
  4812. pi->pcie_dpm_key_disabled = 0;
  4813. pi->thermal_sclk_dpm_enabled = 0;
  4814. /* mclk dpm is unstable on some R7 260X cards with the old mc ucode */
  4815. if ((rdev->pdev->device == 0x6658) &&
  4816. (rdev->mc_fw->size == (BONAIRE_MC_UCODE_SIZE * 4))) {
  4817. pi->mclk_dpm_key_disabled = 1;
  4818. }
  4819. pi->caps_sclk_ds = true;
  4820. pi->mclk_strobe_mode_threshold = 40000;
  4821. pi->mclk_stutter_mode_threshold = 40000;
  4822. pi->mclk_edc_enable_threshold = 40000;
  4823. pi->mclk_edc_wr_enable_threshold = 40000;
  4824. ci_initialize_powertune_defaults(rdev);
  4825. pi->caps_fps = false;
  4826. pi->caps_sclk_throttle_low_notification = false;
  4827. pi->caps_uvd_dpm = true;
  4828. pi->caps_vce_dpm = true;
  4829. ci_get_leakage_voltages(rdev);
  4830. ci_patch_dependency_tables_with_leakage(rdev);
  4831. ci_set_private_data_variables_based_on_pptable(rdev);
  4832. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries =
  4833. kzalloc(4 * sizeof(struct radeon_clock_voltage_dependency_entry), GFP_KERNEL);
  4834. if (!rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries) {
  4835. ci_dpm_fini(rdev);
  4836. return -ENOMEM;
  4837. }
  4838. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.count = 4;
  4839. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].clk = 0;
  4840. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[0].v = 0;
  4841. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].clk = 36000;
  4842. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[1].v = 720;
  4843. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].clk = 54000;
  4844. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[2].v = 810;
  4845. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].clk = 72000;
  4846. rdev->pm.dpm.dyn_state.vddc_dependency_on_dispclk.entries[3].v = 900;
  4847. rdev->pm.dpm.dyn_state.mclk_sclk_ratio = 4;
  4848. rdev->pm.dpm.dyn_state.sclk_mclk_delta = 15000;
  4849. rdev->pm.dpm.dyn_state.vddc_vddci_delta = 200;
  4850. rdev->pm.dpm.dyn_state.valid_sclk_values.count = 0;
  4851. rdev->pm.dpm.dyn_state.valid_sclk_values.values = NULL;
  4852. rdev->pm.dpm.dyn_state.valid_mclk_values.count = 0;
  4853. rdev->pm.dpm.dyn_state.valid_mclk_values.values = NULL;
  4854. if (rdev->family == CHIP_HAWAII) {
  4855. pi->thermal_temp_setting.temperature_low = 94500;
  4856. pi->thermal_temp_setting.temperature_high = 95000;
  4857. pi->thermal_temp_setting.temperature_shutdown = 104000;
  4858. } else {
  4859. pi->thermal_temp_setting.temperature_low = 99500;
  4860. pi->thermal_temp_setting.temperature_high = 100000;
  4861. pi->thermal_temp_setting.temperature_shutdown = 104000;
  4862. }
  4863. pi->uvd_enabled = false;
  4864. dpm_table = &pi->smc_state_table;
  4865. gpio = radeon_atombios_lookup_gpio(rdev, VDDC_VRHOT_GPIO_PINID);
  4866. if (gpio.valid) {
  4867. dpm_table->VRHotGpio = gpio.shift;
  4868. rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
  4869. } else {
  4870. dpm_table->VRHotGpio = CISLANDS_UNUSED_GPIO_PIN;
  4871. rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_REGULATOR_HOT;
  4872. }
  4873. gpio = radeon_atombios_lookup_gpio(rdev, PP_AC_DC_SWITCH_GPIO_PINID);
  4874. if (gpio.valid) {
  4875. dpm_table->AcDcGpio = gpio.shift;
  4876. rdev->pm.dpm.platform_caps |= ATOM_PP_PLATFORM_CAP_HARDWAREDC;
  4877. } else {
  4878. dpm_table->AcDcGpio = CISLANDS_UNUSED_GPIO_PIN;
  4879. rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_HARDWAREDC;
  4880. }
  4881. gpio = radeon_atombios_lookup_gpio(rdev, VDDC_PCC_GPIO_PINID);
  4882. if (gpio.valid) {
  4883. u32 tmp = RREG32_SMC(CNB_PWRMGT_CNTL);
  4884. switch (gpio.shift) {
  4885. case 0:
  4886. tmp &= ~GNB_SLOW_MODE_MASK;
  4887. tmp |= GNB_SLOW_MODE(1);
  4888. break;
  4889. case 1:
  4890. tmp &= ~GNB_SLOW_MODE_MASK;
  4891. tmp |= GNB_SLOW_MODE(2);
  4892. break;
  4893. case 2:
  4894. tmp |= GNB_SLOW;
  4895. break;
  4896. case 3:
  4897. tmp |= FORCE_NB_PS1;
  4898. break;
  4899. case 4:
  4900. tmp |= DPM_ENABLED;
  4901. break;
  4902. default:
  4903. DRM_DEBUG("Invalid PCC GPIO: %u!\n", gpio.shift);
  4904. break;
  4905. }
  4906. WREG32_SMC(CNB_PWRMGT_CNTL, tmp);
  4907. }
  4908. pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_NONE;
  4909. pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_NONE;
  4910. pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_NONE;
  4911. if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_GPIO_LUT))
  4912. pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
  4913. else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDC, VOLTAGE_OBJ_SVID2))
  4914. pi->voltage_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
  4915. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL) {
  4916. if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_GPIO_LUT))
  4917. pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
  4918. else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_VDDCI, VOLTAGE_OBJ_SVID2))
  4919. pi->vddci_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
  4920. else
  4921. rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL;
  4922. }
  4923. if (rdev->pm.dpm.platform_caps & ATOM_PP_PLATFORM_CAP_MVDDCONTROL) {
  4924. if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_GPIO_LUT))
  4925. pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_GPIO;
  4926. else if (radeon_atom_is_voltage_gpio(rdev, VOLTAGE_TYPE_MVDDC, VOLTAGE_OBJ_SVID2))
  4927. pi->mvdd_control = CISLANDS_VOLTAGE_CONTROL_BY_SVID2;
  4928. else
  4929. rdev->pm.dpm.platform_caps &= ~ATOM_PP_PLATFORM_CAP_MVDDCONTROL;
  4930. }
  4931. pi->vddc_phase_shed_control = true;
  4932. #if defined(CONFIG_ACPI)
  4933. pi->pcie_performance_request =
  4934. radeon_acpi_is_pcie_performance_request_supported(rdev);
  4935. #else
  4936. pi->pcie_performance_request = false;
  4937. #endif
  4938. if (atom_parse_data_header(rdev->mode_info.atom_context, index, &size,
  4939. &frev, &crev, &data_offset)) {
  4940. pi->caps_sclk_ss_support = true;
  4941. pi->caps_mclk_ss_support = true;
  4942. pi->dynamic_ss = true;
  4943. } else {
  4944. pi->caps_sclk_ss_support = false;
  4945. pi->caps_mclk_ss_support = false;
  4946. pi->dynamic_ss = true;
  4947. }
  4948. if (rdev->pm.int_thermal_type != THERMAL_TYPE_NONE)
  4949. pi->thermal_protection = true;
  4950. else
  4951. pi->thermal_protection = false;
  4952. pi->caps_dynamic_ac_timing = true;
  4953. pi->uvd_power_gated = false;
  4954. /* make sure dc limits are valid */
  4955. if ((rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.sclk == 0) ||
  4956. (rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc.mclk == 0))
  4957. rdev->pm.dpm.dyn_state.max_clock_voltage_on_dc =
  4958. rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  4959. pi->fan_ctrl_is_in_default_mode = true;
  4960. return 0;
  4961. }
  4962. void ci_dpm_debugfs_print_current_performance_level(struct radeon_device *rdev,
  4963. struct seq_file *m)
  4964. {
  4965. struct ci_power_info *pi = ci_get_pi(rdev);
  4966. struct radeon_ps *rps = &pi->current_rps;
  4967. u32 sclk = ci_get_average_sclk_freq(rdev);
  4968. u32 mclk = ci_get_average_mclk_freq(rdev);
  4969. seq_printf(m, "uvd %sabled\n", pi->uvd_enabled ? "en" : "dis");
  4970. seq_printf(m, "vce %sabled\n", rps->vce_active ? "en" : "dis");
  4971. seq_printf(m, "power level avg sclk: %u mclk: %u\n",
  4972. sclk, mclk);
  4973. }
  4974. void ci_dpm_print_power_state(struct radeon_device *rdev,
  4975. struct radeon_ps *rps)
  4976. {
  4977. struct ci_ps *ps = ci_get_ps(rps);
  4978. struct ci_pl *pl;
  4979. int i;
  4980. r600_dpm_print_class_info(rps->class, rps->class2);
  4981. r600_dpm_print_cap_info(rps->caps);
  4982. printk("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
  4983. for (i = 0; i < ps->performance_level_count; i++) {
  4984. pl = &ps->performance_levels[i];
  4985. printk("\t\tpower level %d sclk: %u mclk: %u pcie gen: %u pcie lanes: %u\n",
  4986. i, pl->sclk, pl->mclk, pl->pcie_gen + 1, pl->pcie_lane);
  4987. }
  4988. r600_dpm_print_ps_status(rdev, rps);
  4989. }
  4990. u32 ci_dpm_get_current_sclk(struct radeon_device *rdev)
  4991. {
  4992. u32 sclk = ci_get_average_sclk_freq(rdev);
  4993. return sclk;
  4994. }
  4995. u32 ci_dpm_get_current_mclk(struct radeon_device *rdev)
  4996. {
  4997. u32 mclk = ci_get_average_mclk_freq(rdev);
  4998. return mclk;
  4999. }
  5000. u32 ci_dpm_get_sclk(struct radeon_device *rdev, bool low)
  5001. {
  5002. struct ci_power_info *pi = ci_get_pi(rdev);
  5003. struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
  5004. if (low)
  5005. return requested_state->performance_levels[0].sclk;
  5006. else
  5007. return requested_state->performance_levels[requested_state->performance_level_count - 1].sclk;
  5008. }
  5009. u32 ci_dpm_get_mclk(struct radeon_device *rdev, bool low)
  5010. {
  5011. struct ci_power_info *pi = ci_get_pi(rdev);
  5012. struct ci_ps *requested_state = ci_get_ps(&pi->requested_rps);
  5013. if (low)
  5014. return requested_state->performance_levels[0].mclk;
  5015. else
  5016. return requested_state->performance_levels[requested_state->performance_level_count - 1].mclk;
  5017. }