ci_dpm.c 200 KB

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