amdgpu_pm.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859
  1. /*
  2. * Copyright 2017 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. * Authors: Rafał Miłecki <zajec5@gmail.com>
  23. * Alex Deucher <alexdeucher@gmail.com>
  24. */
  25. #include "amdgpu.h"
  26. #include "amdgpu_drv.h"
  27. #include "amdgpu_pm.h"
  28. #include "amdgpu_dpm.h"
  29. #include "atom.h"
  30. #include <linux/pci.h>
  31. #include <linux/hwmon.h>
  32. #include <linux/hwmon-sysfs.h>
  33. #include <linux/nospec.h>
  34. #include <linux/pm_runtime.h>
  35. #include <asm/processor.h>
  36. #define MAX_NUM_OF_FEATURES_PER_SUBSET 8
  37. #define MAX_NUM_OF_SUBSETS 8
  38. #define DEVICE_ATTR_IS(_name) (attr_id == device_attr_id__##_name)
  39. struct od_attribute {
  40. struct kobj_attribute attribute;
  41. struct list_head entry;
  42. };
  43. struct od_kobj {
  44. struct kobject kobj;
  45. struct list_head entry;
  46. struct list_head attribute;
  47. void *priv;
  48. };
  49. struct od_feature_ops {
  50. umode_t (*is_visible)(struct amdgpu_device *adev);
  51. ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
  52. char *buf);
  53. ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
  54. const char *buf, size_t count);
  55. };
  56. struct od_feature_item {
  57. const char *name;
  58. struct od_feature_ops ops;
  59. };
  60. struct od_feature_container {
  61. char *name;
  62. struct od_feature_ops ops;
  63. struct od_feature_item sub_feature[MAX_NUM_OF_FEATURES_PER_SUBSET];
  64. };
  65. struct od_feature_set {
  66. struct od_feature_container containers[MAX_NUM_OF_SUBSETS];
  67. };
  68. static const struct hwmon_temp_label {
  69. enum PP_HWMON_TEMP channel;
  70. const char *label;
  71. } temp_label[] = {
  72. {PP_TEMP_EDGE, "edge"},
  73. {PP_TEMP_JUNCTION, "junction"},
  74. {PP_TEMP_MEM, "mem"},
  75. };
  76. const char * const amdgpu_pp_profile_name[] = {
  77. "BOOTUP_DEFAULT",
  78. "3D_FULL_SCREEN",
  79. "POWER_SAVING",
  80. "VIDEO",
  81. "VR",
  82. "COMPUTE",
  83. "CUSTOM",
  84. "WINDOW_3D",
  85. "CAPPED",
  86. "UNCAPPED",
  87. };
  88. /**
  89. * DOC: power_dpm_state
  90. *
  91. * The power_dpm_state file is a legacy interface and is only provided for
  92. * backwards compatibility. The amdgpu driver provides a sysfs API for adjusting
  93. * certain power related parameters. The file power_dpm_state is used for this.
  94. * It accepts the following arguments:
  95. *
  96. * - battery
  97. *
  98. * - balanced
  99. *
  100. * - performance
  101. *
  102. * battery
  103. *
  104. * On older GPUs, the vbios provided a special power state for battery
  105. * operation. Selecting battery switched to this state. This is no
  106. * longer provided on newer GPUs so the option does nothing in that case.
  107. *
  108. * balanced
  109. *
  110. * On older GPUs, the vbios provided a special power state for balanced
  111. * operation. Selecting balanced switched to this state. This is no
  112. * longer provided on newer GPUs so the option does nothing in that case.
  113. *
  114. * performance
  115. *
  116. * On older GPUs, the vbios provided a special power state for performance
  117. * operation. Selecting performance switched to this state. This is no
  118. * longer provided on newer GPUs so the option does nothing in that case.
  119. *
  120. */
  121. static ssize_t amdgpu_get_power_dpm_state(struct device *dev,
  122. struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct drm_device *ddev = dev_get_drvdata(dev);
  126. struct amdgpu_device *adev = drm_to_adev(ddev);
  127. enum amd_pm_state_type pm;
  128. int ret;
  129. if (amdgpu_in_reset(adev))
  130. return -EPERM;
  131. if (adev->in_suspend && !adev->in_runpm)
  132. return -EPERM;
  133. ret = pm_runtime_get_sync(ddev->dev);
  134. if (ret < 0) {
  135. pm_runtime_put_autosuspend(ddev->dev);
  136. return ret;
  137. }
  138. amdgpu_dpm_get_current_power_state(adev, &pm);
  139. pm_runtime_mark_last_busy(ddev->dev);
  140. pm_runtime_put_autosuspend(ddev->dev);
  141. return sysfs_emit(buf, "%s\n",
  142. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  143. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  144. }
  145. static ssize_t amdgpu_set_power_dpm_state(struct device *dev,
  146. struct device_attribute *attr,
  147. const char *buf,
  148. size_t count)
  149. {
  150. struct drm_device *ddev = dev_get_drvdata(dev);
  151. struct amdgpu_device *adev = drm_to_adev(ddev);
  152. enum amd_pm_state_type state;
  153. int ret;
  154. if (amdgpu_in_reset(adev))
  155. return -EPERM;
  156. if (adev->in_suspend && !adev->in_runpm)
  157. return -EPERM;
  158. if (strncmp("battery", buf, strlen("battery")) == 0)
  159. state = POWER_STATE_TYPE_BATTERY;
  160. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  161. state = POWER_STATE_TYPE_BALANCED;
  162. else if (strncmp("performance", buf, strlen("performance")) == 0)
  163. state = POWER_STATE_TYPE_PERFORMANCE;
  164. else
  165. return -EINVAL;
  166. ret = pm_runtime_get_sync(ddev->dev);
  167. if (ret < 0) {
  168. pm_runtime_put_autosuspend(ddev->dev);
  169. return ret;
  170. }
  171. amdgpu_dpm_set_power_state(adev, state);
  172. pm_runtime_mark_last_busy(ddev->dev);
  173. pm_runtime_put_autosuspend(ddev->dev);
  174. return count;
  175. }
  176. /**
  177. * DOC: power_dpm_force_performance_level
  178. *
  179. * The amdgpu driver provides a sysfs API for adjusting certain power
  180. * related parameters. The file power_dpm_force_performance_level is
  181. * used for this. It accepts the following arguments:
  182. *
  183. * - auto
  184. *
  185. * - low
  186. *
  187. * - high
  188. *
  189. * - manual
  190. *
  191. * - profile_standard
  192. *
  193. * - profile_min_sclk
  194. *
  195. * - profile_min_mclk
  196. *
  197. * - profile_peak
  198. *
  199. * auto
  200. *
  201. * When auto is selected, the driver will attempt to dynamically select
  202. * the optimal power profile for current conditions in the driver.
  203. *
  204. * low
  205. *
  206. * When low is selected, the clocks are forced to the lowest power state.
  207. *
  208. * high
  209. *
  210. * When high is selected, the clocks are forced to the highest power state.
  211. *
  212. * manual
  213. *
  214. * When manual is selected, the user can manually adjust which power states
  215. * are enabled for each clock domain via the sysfs pp_dpm_mclk, pp_dpm_sclk,
  216. * and pp_dpm_pcie files and adjust the power state transition heuristics
  217. * via the pp_power_profile_mode sysfs file.
  218. *
  219. * profile_standard
  220. * profile_min_sclk
  221. * profile_min_mclk
  222. * profile_peak
  223. *
  224. * When the profiling modes are selected, clock and power gating are
  225. * disabled and the clocks are set for different profiling cases. This
  226. * mode is recommended for profiling specific work loads where you do
  227. * not want clock or power gating for clock fluctuation to interfere
  228. * with your results. profile_standard sets the clocks to a fixed clock
  229. * level which varies from asic to asic. profile_min_sclk forces the sclk
  230. * to the lowest level. profile_min_mclk forces the mclk to the lowest level.
  231. * profile_peak sets all clocks (mclk, sclk, pcie) to the highest levels.
  232. *
  233. */
  234. static ssize_t amdgpu_get_power_dpm_force_performance_level(struct device *dev,
  235. struct device_attribute *attr,
  236. char *buf)
  237. {
  238. struct drm_device *ddev = dev_get_drvdata(dev);
  239. struct amdgpu_device *adev = drm_to_adev(ddev);
  240. enum amd_dpm_forced_level level = 0xff;
  241. int ret;
  242. if (amdgpu_in_reset(adev))
  243. return -EPERM;
  244. if (adev->in_suspend && !adev->in_runpm)
  245. return -EPERM;
  246. ret = pm_runtime_get_sync(ddev->dev);
  247. if (ret < 0) {
  248. pm_runtime_put_autosuspend(ddev->dev);
  249. return ret;
  250. }
  251. level = amdgpu_dpm_get_performance_level(adev);
  252. pm_runtime_mark_last_busy(ddev->dev);
  253. pm_runtime_put_autosuspend(ddev->dev);
  254. return sysfs_emit(buf, "%s\n",
  255. (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  256. (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  257. (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  258. (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
  259. (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
  260. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
  261. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
  262. (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
  263. (level == AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM) ? "perf_determinism" :
  264. "unknown");
  265. }
  266. static ssize_t amdgpu_set_power_dpm_force_performance_level(struct device *dev,
  267. struct device_attribute *attr,
  268. const char *buf,
  269. size_t count)
  270. {
  271. struct drm_device *ddev = dev_get_drvdata(dev);
  272. struct amdgpu_device *adev = drm_to_adev(ddev);
  273. enum amd_dpm_forced_level level;
  274. int ret = 0;
  275. if (amdgpu_in_reset(adev))
  276. return -EPERM;
  277. if (adev->in_suspend && !adev->in_runpm)
  278. return -EPERM;
  279. if (strncmp("low", buf, strlen("low")) == 0) {
  280. level = AMD_DPM_FORCED_LEVEL_LOW;
  281. } else if (strncmp("high", buf, strlen("high")) == 0) {
  282. level = AMD_DPM_FORCED_LEVEL_HIGH;
  283. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  284. level = AMD_DPM_FORCED_LEVEL_AUTO;
  285. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  286. level = AMD_DPM_FORCED_LEVEL_MANUAL;
  287. } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
  288. level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
  289. } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
  290. level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
  291. } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
  292. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
  293. } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
  294. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
  295. } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
  296. level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  297. } else if (strncmp("perf_determinism", buf, strlen("perf_determinism")) == 0) {
  298. level = AMD_DPM_FORCED_LEVEL_PERF_DETERMINISM;
  299. } else {
  300. return -EINVAL;
  301. }
  302. ret = pm_runtime_get_sync(ddev->dev);
  303. if (ret < 0) {
  304. pm_runtime_put_autosuspend(ddev->dev);
  305. return ret;
  306. }
  307. mutex_lock(&adev->pm.stable_pstate_ctx_lock);
  308. if (amdgpu_dpm_force_performance_level(adev, level)) {
  309. pm_runtime_mark_last_busy(ddev->dev);
  310. pm_runtime_put_autosuspend(ddev->dev);
  311. mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
  312. return -EINVAL;
  313. }
  314. /* override whatever a user ctx may have set */
  315. adev->pm.stable_pstate_ctx = NULL;
  316. mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
  317. pm_runtime_mark_last_busy(ddev->dev);
  318. pm_runtime_put_autosuspend(ddev->dev);
  319. return count;
  320. }
  321. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  322. struct device_attribute *attr,
  323. char *buf)
  324. {
  325. struct drm_device *ddev = dev_get_drvdata(dev);
  326. struct amdgpu_device *adev = drm_to_adev(ddev);
  327. struct pp_states_info data;
  328. uint32_t i;
  329. int buf_len, ret;
  330. if (amdgpu_in_reset(adev))
  331. return -EPERM;
  332. if (adev->in_suspend && !adev->in_runpm)
  333. return -EPERM;
  334. ret = pm_runtime_get_sync(ddev->dev);
  335. if (ret < 0) {
  336. pm_runtime_put_autosuspend(ddev->dev);
  337. return ret;
  338. }
  339. if (amdgpu_dpm_get_pp_num_states(adev, &data))
  340. memset(&data, 0, sizeof(data));
  341. pm_runtime_mark_last_busy(ddev->dev);
  342. pm_runtime_put_autosuspend(ddev->dev);
  343. buf_len = sysfs_emit(buf, "states: %d\n", data.nums);
  344. for (i = 0; i < data.nums; i++)
  345. buf_len += sysfs_emit_at(buf, buf_len, "%d %s\n", i,
  346. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  347. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  348. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  349. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  350. return buf_len;
  351. }
  352. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf)
  355. {
  356. struct drm_device *ddev = dev_get_drvdata(dev);
  357. struct amdgpu_device *adev = drm_to_adev(ddev);
  358. struct pp_states_info data = {0};
  359. enum amd_pm_state_type pm = 0;
  360. int i = 0, ret = 0;
  361. if (amdgpu_in_reset(adev))
  362. return -EPERM;
  363. if (adev->in_suspend && !adev->in_runpm)
  364. return -EPERM;
  365. ret = pm_runtime_get_sync(ddev->dev);
  366. if (ret < 0) {
  367. pm_runtime_put_autosuspend(ddev->dev);
  368. return ret;
  369. }
  370. amdgpu_dpm_get_current_power_state(adev, &pm);
  371. ret = amdgpu_dpm_get_pp_num_states(adev, &data);
  372. pm_runtime_mark_last_busy(ddev->dev);
  373. pm_runtime_put_autosuspend(ddev->dev);
  374. if (ret)
  375. return ret;
  376. for (i = 0; i < data.nums; i++) {
  377. if (pm == data.states[i])
  378. break;
  379. }
  380. if (i == data.nums)
  381. i = -EINVAL;
  382. return sysfs_emit(buf, "%d\n", i);
  383. }
  384. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  385. struct device_attribute *attr,
  386. char *buf)
  387. {
  388. struct drm_device *ddev = dev_get_drvdata(dev);
  389. struct amdgpu_device *adev = drm_to_adev(ddev);
  390. if (amdgpu_in_reset(adev))
  391. return -EPERM;
  392. if (adev->in_suspend && !adev->in_runpm)
  393. return -EPERM;
  394. if (adev->pm.pp_force_state_enabled)
  395. return amdgpu_get_pp_cur_state(dev, attr, buf);
  396. else
  397. return sysfs_emit(buf, "\n");
  398. }
  399. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  400. struct device_attribute *attr,
  401. const char *buf,
  402. size_t count)
  403. {
  404. struct drm_device *ddev = dev_get_drvdata(dev);
  405. struct amdgpu_device *adev = drm_to_adev(ddev);
  406. enum amd_pm_state_type state = 0;
  407. struct pp_states_info data;
  408. unsigned long idx;
  409. int ret;
  410. if (amdgpu_in_reset(adev))
  411. return -EPERM;
  412. if (adev->in_suspend && !adev->in_runpm)
  413. return -EPERM;
  414. adev->pm.pp_force_state_enabled = false;
  415. if (strlen(buf) == 1)
  416. return count;
  417. ret = kstrtoul(buf, 0, &idx);
  418. if (ret || idx >= ARRAY_SIZE(data.states))
  419. return -EINVAL;
  420. idx = array_index_nospec(idx, ARRAY_SIZE(data.states));
  421. ret = pm_runtime_get_sync(ddev->dev);
  422. if (ret < 0) {
  423. pm_runtime_put_autosuspend(ddev->dev);
  424. return ret;
  425. }
  426. ret = amdgpu_dpm_get_pp_num_states(adev, &data);
  427. if (ret)
  428. goto err_out;
  429. state = data.states[idx];
  430. /* only set user selected power states */
  431. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  432. state != POWER_STATE_TYPE_DEFAULT) {
  433. ret = amdgpu_dpm_dispatch_task(adev,
  434. AMD_PP_TASK_ENABLE_USER_STATE, &state);
  435. if (ret)
  436. goto err_out;
  437. adev->pm.pp_force_state_enabled = true;
  438. }
  439. pm_runtime_mark_last_busy(ddev->dev);
  440. pm_runtime_put_autosuspend(ddev->dev);
  441. return count;
  442. err_out:
  443. pm_runtime_mark_last_busy(ddev->dev);
  444. pm_runtime_put_autosuspend(ddev->dev);
  445. return ret;
  446. }
  447. /**
  448. * DOC: pp_table
  449. *
  450. * The amdgpu driver provides a sysfs API for uploading new powerplay
  451. * tables. The file pp_table is used for this. Reading the file
  452. * will dump the current power play table. Writing to the file
  453. * will attempt to upload a new powerplay table and re-initialize
  454. * powerplay using that new table.
  455. *
  456. */
  457. static ssize_t amdgpu_get_pp_table(struct device *dev,
  458. struct device_attribute *attr,
  459. char *buf)
  460. {
  461. struct drm_device *ddev = dev_get_drvdata(dev);
  462. struct amdgpu_device *adev = drm_to_adev(ddev);
  463. char *table = NULL;
  464. int size, ret;
  465. if (amdgpu_in_reset(adev))
  466. return -EPERM;
  467. if (adev->in_suspend && !adev->in_runpm)
  468. return -EPERM;
  469. ret = pm_runtime_get_sync(ddev->dev);
  470. if (ret < 0) {
  471. pm_runtime_put_autosuspend(ddev->dev);
  472. return ret;
  473. }
  474. size = amdgpu_dpm_get_pp_table(adev, &table);
  475. pm_runtime_mark_last_busy(ddev->dev);
  476. pm_runtime_put_autosuspend(ddev->dev);
  477. if (size <= 0)
  478. return size;
  479. if (size >= PAGE_SIZE)
  480. size = PAGE_SIZE - 1;
  481. memcpy(buf, table, size);
  482. return size;
  483. }
  484. static ssize_t amdgpu_set_pp_table(struct device *dev,
  485. struct device_attribute *attr,
  486. const char *buf,
  487. size_t count)
  488. {
  489. struct drm_device *ddev = dev_get_drvdata(dev);
  490. struct amdgpu_device *adev = drm_to_adev(ddev);
  491. int ret = 0;
  492. if (amdgpu_in_reset(adev))
  493. return -EPERM;
  494. if (adev->in_suspend && !adev->in_runpm)
  495. return -EPERM;
  496. ret = pm_runtime_get_sync(ddev->dev);
  497. if (ret < 0) {
  498. pm_runtime_put_autosuspend(ddev->dev);
  499. return ret;
  500. }
  501. ret = amdgpu_dpm_set_pp_table(adev, buf, count);
  502. pm_runtime_mark_last_busy(ddev->dev);
  503. pm_runtime_put_autosuspend(ddev->dev);
  504. if (ret)
  505. return ret;
  506. return count;
  507. }
  508. /**
  509. * DOC: pp_od_clk_voltage
  510. *
  511. * The amdgpu driver provides a sysfs API for adjusting the clocks and voltages
  512. * in each power level within a power state. The pp_od_clk_voltage is used for
  513. * this.
  514. *
  515. * Note that the actual memory controller clock rate are exposed, not
  516. * the effective memory clock of the DRAMs. To translate it, use the
  517. * following formula:
  518. *
  519. * Clock conversion (Mhz):
  520. *
  521. * HBM: effective_memory_clock = memory_controller_clock * 1
  522. *
  523. * G5: effective_memory_clock = memory_controller_clock * 1
  524. *
  525. * G6: effective_memory_clock = memory_controller_clock * 2
  526. *
  527. * DRAM data rate (MT/s):
  528. *
  529. * HBM: effective_memory_clock * 2 = data_rate
  530. *
  531. * G5: effective_memory_clock * 4 = data_rate
  532. *
  533. * G6: effective_memory_clock * 8 = data_rate
  534. *
  535. * Bandwidth (MB/s):
  536. *
  537. * data_rate * vram_bit_width / 8 = memory_bandwidth
  538. *
  539. * Some examples:
  540. *
  541. * G5 on RX460:
  542. *
  543. * memory_controller_clock = 1750 Mhz
  544. *
  545. * effective_memory_clock = 1750 Mhz * 1 = 1750 Mhz
  546. *
  547. * data rate = 1750 * 4 = 7000 MT/s
  548. *
  549. * memory_bandwidth = 7000 * 128 bits / 8 = 112000 MB/s
  550. *
  551. * G6 on RX5700:
  552. *
  553. * memory_controller_clock = 875 Mhz
  554. *
  555. * effective_memory_clock = 875 Mhz * 2 = 1750 Mhz
  556. *
  557. * data rate = 1750 * 8 = 14000 MT/s
  558. *
  559. * memory_bandwidth = 14000 * 256 bits / 8 = 448000 MB/s
  560. *
  561. * < For Vega10 and previous ASICs >
  562. *
  563. * Reading the file will display:
  564. *
  565. * - a list of engine clock levels and voltages labeled OD_SCLK
  566. *
  567. * - a list of memory clock levels and voltages labeled OD_MCLK
  568. *
  569. * - a list of valid ranges for sclk, mclk, and voltage labeled OD_RANGE
  570. *
  571. * To manually adjust these settings, first select manual using
  572. * power_dpm_force_performance_level. Enter a new value for each
  573. * level by writing a string that contains "s/m level clock voltage" to
  574. * the file. E.g., "s 1 500 820" will update sclk level 1 to be 500 MHz
  575. * at 820 mV; "m 0 350 810" will update mclk level 0 to be 350 MHz at
  576. * 810 mV. When you have edited all of the states as needed, write
  577. * "c" (commit) to the file to commit your changes. If you want to reset to the
  578. * default power levels, write "r" (reset) to the file to reset them.
  579. *
  580. *
  581. * < For Vega20 and newer ASICs >
  582. *
  583. * Reading the file will display:
  584. *
  585. * - minimum and maximum engine clock labeled OD_SCLK
  586. *
  587. * - minimum(not available for Vega20 and Navi1x) and maximum memory
  588. * clock labeled OD_MCLK
  589. *
  590. * - three <frequency, voltage> points labeled OD_VDDC_CURVE.
  591. * They can be used to calibrate the sclk voltage curve. This is
  592. * available for Vega20 and NV1X.
  593. *
  594. * - voltage offset(in mV) applied on target voltage calculation.
  595. * This is available for Sienna Cichlid, Navy Flounder, Dimgrey
  596. * Cavefish and some later SMU13 ASICs. For these ASICs, the target
  597. * voltage calculation can be illustrated by "voltage = voltage
  598. * calculated from v/f curve + overdrive vddgfx offset"
  599. *
  600. * - a list of valid ranges for sclk, mclk, voltage curve points
  601. * or voltage offset labeled OD_RANGE
  602. *
  603. * < For APUs >
  604. *
  605. * Reading the file will display:
  606. *
  607. * - minimum and maximum engine clock labeled OD_SCLK
  608. *
  609. * - a list of valid ranges for sclk labeled OD_RANGE
  610. *
  611. * < For VanGogh >
  612. *
  613. * Reading the file will display:
  614. *
  615. * - minimum and maximum engine clock labeled OD_SCLK
  616. * - minimum and maximum core clocks labeled OD_CCLK
  617. *
  618. * - a list of valid ranges for sclk and cclk labeled OD_RANGE
  619. *
  620. * To manually adjust these settings:
  621. *
  622. * - First select manual using power_dpm_force_performance_level
  623. *
  624. * - For clock frequency setting, enter a new value by writing a
  625. * string that contains "s/m index clock" to the file. The index
  626. * should be 0 if to set minimum clock. And 1 if to set maximum
  627. * clock. E.g., "s 0 500" will update minimum sclk to be 500 MHz.
  628. * "m 1 800" will update maximum mclk to be 800Mhz. For core
  629. * clocks on VanGogh, the string contains "p core index clock".
  630. * E.g., "p 2 0 800" would set the minimum core clock on core
  631. * 2 to 800Mhz.
  632. *
  633. * For sclk voltage curve supported by Vega20 and NV1X, enter the new
  634. * values by writing a string that contains "vc point clock voltage"
  635. * to the file. The points are indexed by 0, 1 and 2. E.g., "vc 0 300
  636. * 600" will update point1 with clock set as 300Mhz and voltage as 600mV.
  637. * "vc 2 1000 1000" will update point3 with clock set as 1000Mhz and
  638. * voltage 1000mV.
  639. *
  640. * For voltage offset supported by Sienna Cichlid, Navy Flounder, Dimgrey
  641. * Cavefish and some later SMU13 ASICs, enter the new value by writing a
  642. * string that contains "vo offset". E.g., "vo -10" will update the extra
  643. * voltage offset applied to the whole v/f curve line as -10mv.
  644. *
  645. * - When you have edited all of the states as needed, write "c" (commit)
  646. * to the file to commit your changes
  647. *
  648. * - If you want to reset to the default power levels, write "r" (reset)
  649. * to the file to reset them
  650. *
  651. */
  652. static ssize_t amdgpu_set_pp_od_clk_voltage(struct device *dev,
  653. struct device_attribute *attr,
  654. const char *buf,
  655. size_t count)
  656. {
  657. struct drm_device *ddev = dev_get_drvdata(dev);
  658. struct amdgpu_device *adev = drm_to_adev(ddev);
  659. int ret;
  660. uint32_t parameter_size = 0;
  661. long parameter[64];
  662. char buf_cpy[128];
  663. char *tmp_str;
  664. char *sub_str;
  665. const char delimiter[3] = {' ', '\n', '\0'};
  666. uint32_t type;
  667. if (amdgpu_in_reset(adev))
  668. return -EPERM;
  669. if (adev->in_suspend && !adev->in_runpm)
  670. return -EPERM;
  671. if (count > 127 || count == 0)
  672. return -EINVAL;
  673. if (*buf == 's')
  674. type = PP_OD_EDIT_SCLK_VDDC_TABLE;
  675. else if (*buf == 'p')
  676. type = PP_OD_EDIT_CCLK_VDDC_TABLE;
  677. else if (*buf == 'm')
  678. type = PP_OD_EDIT_MCLK_VDDC_TABLE;
  679. else if (*buf == 'r')
  680. type = PP_OD_RESTORE_DEFAULT_TABLE;
  681. else if (*buf == 'c')
  682. type = PP_OD_COMMIT_DPM_TABLE;
  683. else if (!strncmp(buf, "vc", 2))
  684. type = PP_OD_EDIT_VDDC_CURVE;
  685. else if (!strncmp(buf, "vo", 2))
  686. type = PP_OD_EDIT_VDDGFX_OFFSET;
  687. else
  688. return -EINVAL;
  689. memcpy(buf_cpy, buf, count);
  690. buf_cpy[count] = 0;
  691. tmp_str = buf_cpy;
  692. if ((type == PP_OD_EDIT_VDDC_CURVE) ||
  693. (type == PP_OD_EDIT_VDDGFX_OFFSET))
  694. tmp_str++;
  695. while (isspace(*++tmp_str));
  696. while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
  697. if (strlen(sub_str) == 0)
  698. continue;
  699. ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
  700. if (ret)
  701. return -EINVAL;
  702. parameter_size++;
  703. if (!tmp_str)
  704. break;
  705. while (isspace(*tmp_str))
  706. tmp_str++;
  707. }
  708. ret = pm_runtime_get_sync(ddev->dev);
  709. if (ret < 0) {
  710. pm_runtime_put_autosuspend(ddev->dev);
  711. return ret;
  712. }
  713. if (amdgpu_dpm_set_fine_grain_clk_vol(adev,
  714. type,
  715. parameter,
  716. parameter_size))
  717. goto err_out;
  718. if (amdgpu_dpm_odn_edit_dpm_table(adev, type,
  719. parameter, parameter_size))
  720. goto err_out;
  721. if (type == PP_OD_COMMIT_DPM_TABLE) {
  722. if (amdgpu_dpm_dispatch_task(adev,
  723. AMD_PP_TASK_READJUST_POWER_STATE,
  724. NULL))
  725. goto err_out;
  726. }
  727. pm_runtime_mark_last_busy(ddev->dev);
  728. pm_runtime_put_autosuspend(ddev->dev);
  729. return count;
  730. err_out:
  731. pm_runtime_mark_last_busy(ddev->dev);
  732. pm_runtime_put_autosuspend(ddev->dev);
  733. return -EINVAL;
  734. }
  735. static ssize_t amdgpu_get_pp_od_clk_voltage(struct device *dev,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. struct drm_device *ddev = dev_get_drvdata(dev);
  740. struct amdgpu_device *adev = drm_to_adev(ddev);
  741. int size = 0;
  742. int ret;
  743. enum pp_clock_type od_clocks[6] = {
  744. OD_SCLK,
  745. OD_MCLK,
  746. OD_VDDC_CURVE,
  747. OD_RANGE,
  748. OD_VDDGFX_OFFSET,
  749. OD_CCLK,
  750. };
  751. uint clk_index;
  752. if (amdgpu_in_reset(adev))
  753. return -EPERM;
  754. if (adev->in_suspend && !adev->in_runpm)
  755. return -EPERM;
  756. ret = pm_runtime_get_sync(ddev->dev);
  757. if (ret < 0) {
  758. pm_runtime_put_autosuspend(ddev->dev);
  759. return ret;
  760. }
  761. for (clk_index = 0 ; clk_index < 6 ; clk_index++) {
  762. ret = amdgpu_dpm_emit_clock_levels(adev, od_clocks[clk_index], buf, &size);
  763. if (ret)
  764. break;
  765. }
  766. if (ret == -ENOENT) {
  767. size = amdgpu_dpm_print_clock_levels(adev, OD_SCLK, buf);
  768. size += amdgpu_dpm_print_clock_levels(adev, OD_MCLK, buf + size);
  769. size += amdgpu_dpm_print_clock_levels(adev, OD_VDDC_CURVE, buf + size);
  770. size += amdgpu_dpm_print_clock_levels(adev, OD_VDDGFX_OFFSET, buf + size);
  771. size += amdgpu_dpm_print_clock_levels(adev, OD_RANGE, buf + size);
  772. size += amdgpu_dpm_print_clock_levels(adev, OD_CCLK, buf + size);
  773. }
  774. if (size == 0)
  775. size = sysfs_emit(buf, "\n");
  776. pm_runtime_mark_last_busy(ddev->dev);
  777. pm_runtime_put_autosuspend(ddev->dev);
  778. return size;
  779. }
  780. /**
  781. * DOC: pp_features
  782. *
  783. * The amdgpu driver provides a sysfs API for adjusting what powerplay
  784. * features to be enabled. The file pp_features is used for this. And
  785. * this is only available for Vega10 and later dGPUs.
  786. *
  787. * Reading back the file will show you the followings:
  788. * - Current ppfeature masks
  789. * - List of the all supported powerplay features with their naming,
  790. * bitmasks and enablement status('Y'/'N' means "enabled"/"disabled").
  791. *
  792. * To manually enable or disable a specific feature, just set or clear
  793. * the corresponding bit from original ppfeature masks and input the
  794. * new ppfeature masks.
  795. */
  796. static ssize_t amdgpu_set_pp_features(struct device *dev,
  797. struct device_attribute *attr,
  798. const char *buf,
  799. size_t count)
  800. {
  801. struct drm_device *ddev = dev_get_drvdata(dev);
  802. struct amdgpu_device *adev = drm_to_adev(ddev);
  803. uint64_t featuremask;
  804. int ret;
  805. if (amdgpu_in_reset(adev))
  806. return -EPERM;
  807. if (adev->in_suspend && !adev->in_runpm)
  808. return -EPERM;
  809. ret = kstrtou64(buf, 0, &featuremask);
  810. if (ret)
  811. return -EINVAL;
  812. ret = pm_runtime_get_sync(ddev->dev);
  813. if (ret < 0) {
  814. pm_runtime_put_autosuspend(ddev->dev);
  815. return ret;
  816. }
  817. ret = amdgpu_dpm_set_ppfeature_status(adev, featuremask);
  818. pm_runtime_mark_last_busy(ddev->dev);
  819. pm_runtime_put_autosuspend(ddev->dev);
  820. if (ret)
  821. return -EINVAL;
  822. return count;
  823. }
  824. static ssize_t amdgpu_get_pp_features(struct device *dev,
  825. struct device_attribute *attr,
  826. char *buf)
  827. {
  828. struct drm_device *ddev = dev_get_drvdata(dev);
  829. struct amdgpu_device *adev = drm_to_adev(ddev);
  830. ssize_t size;
  831. int ret;
  832. if (amdgpu_in_reset(adev))
  833. return -EPERM;
  834. if (adev->in_suspend && !adev->in_runpm)
  835. return -EPERM;
  836. ret = pm_runtime_get_sync(ddev->dev);
  837. if (ret < 0) {
  838. pm_runtime_put_autosuspend(ddev->dev);
  839. return ret;
  840. }
  841. size = amdgpu_dpm_get_ppfeature_status(adev, buf);
  842. if (size <= 0)
  843. size = sysfs_emit(buf, "\n");
  844. pm_runtime_mark_last_busy(ddev->dev);
  845. pm_runtime_put_autosuspend(ddev->dev);
  846. return size;
  847. }
  848. /**
  849. * DOC: pp_dpm_sclk pp_dpm_mclk pp_dpm_socclk pp_dpm_fclk pp_dpm_dcefclk pp_dpm_pcie
  850. *
  851. * The amdgpu driver provides a sysfs API for adjusting what power levels
  852. * are enabled for a given power state. The files pp_dpm_sclk, pp_dpm_mclk,
  853. * pp_dpm_socclk, pp_dpm_fclk, pp_dpm_dcefclk and pp_dpm_pcie are used for
  854. * this.
  855. *
  856. * pp_dpm_socclk and pp_dpm_dcefclk interfaces are only available for
  857. * Vega10 and later ASICs.
  858. * pp_dpm_fclk interface is only available for Vega20 and later ASICs.
  859. *
  860. * Reading back the files will show you the available power levels within
  861. * the power state and the clock information for those levels. If deep sleep is
  862. * applied to a clock, the level will be denoted by a special level 'S:'
  863. * E.g., ::
  864. *
  865. * S: 19Mhz *
  866. * 0: 615Mhz
  867. * 1: 800Mhz
  868. * 2: 888Mhz
  869. * 3: 1000Mhz
  870. *
  871. *
  872. * To manually adjust these states, first select manual using
  873. * power_dpm_force_performance_level.
  874. * Secondly, enter a new value for each level by inputing a string that
  875. * contains " echo xx xx xx > pp_dpm_sclk/mclk/pcie"
  876. * E.g.,
  877. *
  878. * .. code-block:: bash
  879. *
  880. * echo "4 5 6" > pp_dpm_sclk
  881. *
  882. * will enable sclk levels 4, 5, and 6.
  883. *
  884. * NOTE: change to the dcefclk max dpm level is not supported now
  885. */
  886. static ssize_t amdgpu_get_pp_dpm_clock(struct device *dev,
  887. enum pp_clock_type type,
  888. char *buf)
  889. {
  890. struct drm_device *ddev = dev_get_drvdata(dev);
  891. struct amdgpu_device *adev = drm_to_adev(ddev);
  892. int size = 0;
  893. int ret = 0;
  894. if (amdgpu_in_reset(adev))
  895. return -EPERM;
  896. if (adev->in_suspend && !adev->in_runpm)
  897. return -EPERM;
  898. ret = pm_runtime_get_sync(ddev->dev);
  899. if (ret < 0) {
  900. pm_runtime_put_autosuspend(ddev->dev);
  901. return ret;
  902. }
  903. ret = amdgpu_dpm_emit_clock_levels(adev, type, buf, &size);
  904. if (ret == -ENOENT)
  905. size = amdgpu_dpm_print_clock_levels(adev, type, buf);
  906. if (size == 0)
  907. size = sysfs_emit(buf, "\n");
  908. pm_runtime_mark_last_busy(ddev->dev);
  909. pm_runtime_put_autosuspend(ddev->dev);
  910. return size;
  911. }
  912. /*
  913. * Worst case: 32 bits individually specified, in octal at 12 characters
  914. * per line (+1 for \n).
  915. */
  916. #define AMDGPU_MASK_BUF_MAX (32 * 13)
  917. static ssize_t amdgpu_read_mask(const char *buf, size_t count, uint32_t *mask)
  918. {
  919. int ret;
  920. unsigned long level;
  921. char *sub_str = NULL;
  922. char *tmp;
  923. char buf_cpy[AMDGPU_MASK_BUF_MAX + 1];
  924. const char delimiter[3] = {' ', '\n', '\0'};
  925. size_t bytes;
  926. *mask = 0;
  927. bytes = min(count, sizeof(buf_cpy) - 1);
  928. memcpy(buf_cpy, buf, bytes);
  929. buf_cpy[bytes] = '\0';
  930. tmp = buf_cpy;
  931. while ((sub_str = strsep(&tmp, delimiter)) != NULL) {
  932. if (strlen(sub_str)) {
  933. ret = kstrtoul(sub_str, 0, &level);
  934. if (ret || level > 31)
  935. return -EINVAL;
  936. *mask |= 1 << level;
  937. } else
  938. break;
  939. }
  940. return 0;
  941. }
  942. static ssize_t amdgpu_set_pp_dpm_clock(struct device *dev,
  943. enum pp_clock_type type,
  944. const char *buf,
  945. size_t count)
  946. {
  947. struct drm_device *ddev = dev_get_drvdata(dev);
  948. struct amdgpu_device *adev = drm_to_adev(ddev);
  949. int ret;
  950. uint32_t mask = 0;
  951. if (amdgpu_in_reset(adev))
  952. return -EPERM;
  953. if (adev->in_suspend && !adev->in_runpm)
  954. return -EPERM;
  955. ret = amdgpu_read_mask(buf, count, &mask);
  956. if (ret)
  957. return ret;
  958. ret = pm_runtime_get_sync(ddev->dev);
  959. if (ret < 0) {
  960. pm_runtime_put_autosuspend(ddev->dev);
  961. return ret;
  962. }
  963. ret = amdgpu_dpm_force_clock_level(adev, type, mask);
  964. pm_runtime_mark_last_busy(ddev->dev);
  965. pm_runtime_put_autosuspend(ddev->dev);
  966. if (ret)
  967. return -EINVAL;
  968. return count;
  969. }
  970. static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
  971. struct device_attribute *attr,
  972. char *buf)
  973. {
  974. return amdgpu_get_pp_dpm_clock(dev, PP_SCLK, buf);
  975. }
  976. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  977. struct device_attribute *attr,
  978. const char *buf,
  979. size_t count)
  980. {
  981. return amdgpu_set_pp_dpm_clock(dev, PP_SCLK, buf, count);
  982. }
  983. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  984. struct device_attribute *attr,
  985. char *buf)
  986. {
  987. return amdgpu_get_pp_dpm_clock(dev, PP_MCLK, buf);
  988. }
  989. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  990. struct device_attribute *attr,
  991. const char *buf,
  992. size_t count)
  993. {
  994. return amdgpu_set_pp_dpm_clock(dev, PP_MCLK, buf, count);
  995. }
  996. static ssize_t amdgpu_get_pp_dpm_socclk(struct device *dev,
  997. struct device_attribute *attr,
  998. char *buf)
  999. {
  1000. return amdgpu_get_pp_dpm_clock(dev, PP_SOCCLK, buf);
  1001. }
  1002. static ssize_t amdgpu_set_pp_dpm_socclk(struct device *dev,
  1003. struct device_attribute *attr,
  1004. const char *buf,
  1005. size_t count)
  1006. {
  1007. return amdgpu_set_pp_dpm_clock(dev, PP_SOCCLK, buf, count);
  1008. }
  1009. static ssize_t amdgpu_get_pp_dpm_fclk(struct device *dev,
  1010. struct device_attribute *attr,
  1011. char *buf)
  1012. {
  1013. return amdgpu_get_pp_dpm_clock(dev, PP_FCLK, buf);
  1014. }
  1015. static ssize_t amdgpu_set_pp_dpm_fclk(struct device *dev,
  1016. struct device_attribute *attr,
  1017. const char *buf,
  1018. size_t count)
  1019. {
  1020. return amdgpu_set_pp_dpm_clock(dev, PP_FCLK, buf, count);
  1021. }
  1022. static ssize_t amdgpu_get_pp_dpm_vclk(struct device *dev,
  1023. struct device_attribute *attr,
  1024. char *buf)
  1025. {
  1026. return amdgpu_get_pp_dpm_clock(dev, PP_VCLK, buf);
  1027. }
  1028. static ssize_t amdgpu_set_pp_dpm_vclk(struct device *dev,
  1029. struct device_attribute *attr,
  1030. const char *buf,
  1031. size_t count)
  1032. {
  1033. return amdgpu_set_pp_dpm_clock(dev, PP_VCLK, buf, count);
  1034. }
  1035. static ssize_t amdgpu_get_pp_dpm_vclk1(struct device *dev,
  1036. struct device_attribute *attr,
  1037. char *buf)
  1038. {
  1039. return amdgpu_get_pp_dpm_clock(dev, PP_VCLK1, buf);
  1040. }
  1041. static ssize_t amdgpu_set_pp_dpm_vclk1(struct device *dev,
  1042. struct device_attribute *attr,
  1043. const char *buf,
  1044. size_t count)
  1045. {
  1046. return amdgpu_set_pp_dpm_clock(dev, PP_VCLK1, buf, count);
  1047. }
  1048. static ssize_t amdgpu_get_pp_dpm_dclk(struct device *dev,
  1049. struct device_attribute *attr,
  1050. char *buf)
  1051. {
  1052. return amdgpu_get_pp_dpm_clock(dev, PP_DCLK, buf);
  1053. }
  1054. static ssize_t amdgpu_set_pp_dpm_dclk(struct device *dev,
  1055. struct device_attribute *attr,
  1056. const char *buf,
  1057. size_t count)
  1058. {
  1059. return amdgpu_set_pp_dpm_clock(dev, PP_DCLK, buf, count);
  1060. }
  1061. static ssize_t amdgpu_get_pp_dpm_dclk1(struct device *dev,
  1062. struct device_attribute *attr,
  1063. char *buf)
  1064. {
  1065. return amdgpu_get_pp_dpm_clock(dev, PP_DCLK1, buf);
  1066. }
  1067. static ssize_t amdgpu_set_pp_dpm_dclk1(struct device *dev,
  1068. struct device_attribute *attr,
  1069. const char *buf,
  1070. size_t count)
  1071. {
  1072. return amdgpu_set_pp_dpm_clock(dev, PP_DCLK1, buf, count);
  1073. }
  1074. static ssize_t amdgpu_get_pp_dpm_dcefclk(struct device *dev,
  1075. struct device_attribute *attr,
  1076. char *buf)
  1077. {
  1078. return amdgpu_get_pp_dpm_clock(dev, PP_DCEFCLK, buf);
  1079. }
  1080. static ssize_t amdgpu_set_pp_dpm_dcefclk(struct device *dev,
  1081. struct device_attribute *attr,
  1082. const char *buf,
  1083. size_t count)
  1084. {
  1085. return amdgpu_set_pp_dpm_clock(dev, PP_DCEFCLK, buf, count);
  1086. }
  1087. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  1088. struct device_attribute *attr,
  1089. char *buf)
  1090. {
  1091. return amdgpu_get_pp_dpm_clock(dev, PP_PCIE, buf);
  1092. }
  1093. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  1094. struct device_attribute *attr,
  1095. const char *buf,
  1096. size_t count)
  1097. {
  1098. return amdgpu_set_pp_dpm_clock(dev, PP_PCIE, buf, count);
  1099. }
  1100. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  1101. struct device_attribute *attr,
  1102. char *buf)
  1103. {
  1104. struct drm_device *ddev = dev_get_drvdata(dev);
  1105. struct amdgpu_device *adev = drm_to_adev(ddev);
  1106. uint32_t value = 0;
  1107. int ret;
  1108. if (amdgpu_in_reset(adev))
  1109. return -EPERM;
  1110. if (adev->in_suspend && !adev->in_runpm)
  1111. return -EPERM;
  1112. ret = pm_runtime_get_sync(ddev->dev);
  1113. if (ret < 0) {
  1114. pm_runtime_put_autosuspend(ddev->dev);
  1115. return ret;
  1116. }
  1117. value = amdgpu_dpm_get_sclk_od(adev);
  1118. pm_runtime_mark_last_busy(ddev->dev);
  1119. pm_runtime_put_autosuspend(ddev->dev);
  1120. return sysfs_emit(buf, "%d\n", value);
  1121. }
  1122. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  1123. struct device_attribute *attr,
  1124. const char *buf,
  1125. size_t count)
  1126. {
  1127. struct drm_device *ddev = dev_get_drvdata(dev);
  1128. struct amdgpu_device *adev = drm_to_adev(ddev);
  1129. int ret;
  1130. long int value;
  1131. if (amdgpu_in_reset(adev))
  1132. return -EPERM;
  1133. if (adev->in_suspend && !adev->in_runpm)
  1134. return -EPERM;
  1135. ret = kstrtol(buf, 0, &value);
  1136. if (ret)
  1137. return -EINVAL;
  1138. ret = pm_runtime_get_sync(ddev->dev);
  1139. if (ret < 0) {
  1140. pm_runtime_put_autosuspend(ddev->dev);
  1141. return ret;
  1142. }
  1143. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  1144. pm_runtime_mark_last_busy(ddev->dev);
  1145. pm_runtime_put_autosuspend(ddev->dev);
  1146. return count;
  1147. }
  1148. static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
  1149. struct device_attribute *attr,
  1150. char *buf)
  1151. {
  1152. struct drm_device *ddev = dev_get_drvdata(dev);
  1153. struct amdgpu_device *adev = drm_to_adev(ddev);
  1154. uint32_t value = 0;
  1155. int ret;
  1156. if (amdgpu_in_reset(adev))
  1157. return -EPERM;
  1158. if (adev->in_suspend && !adev->in_runpm)
  1159. return -EPERM;
  1160. ret = pm_runtime_get_sync(ddev->dev);
  1161. if (ret < 0) {
  1162. pm_runtime_put_autosuspend(ddev->dev);
  1163. return ret;
  1164. }
  1165. value = amdgpu_dpm_get_mclk_od(adev);
  1166. pm_runtime_mark_last_busy(ddev->dev);
  1167. pm_runtime_put_autosuspend(ddev->dev);
  1168. return sysfs_emit(buf, "%d\n", value);
  1169. }
  1170. static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
  1171. struct device_attribute *attr,
  1172. const char *buf,
  1173. size_t count)
  1174. {
  1175. struct drm_device *ddev = dev_get_drvdata(dev);
  1176. struct amdgpu_device *adev = drm_to_adev(ddev);
  1177. int ret;
  1178. long int value;
  1179. if (amdgpu_in_reset(adev))
  1180. return -EPERM;
  1181. if (adev->in_suspend && !adev->in_runpm)
  1182. return -EPERM;
  1183. ret = kstrtol(buf, 0, &value);
  1184. if (ret)
  1185. return -EINVAL;
  1186. ret = pm_runtime_get_sync(ddev->dev);
  1187. if (ret < 0) {
  1188. pm_runtime_put_autosuspend(ddev->dev);
  1189. return ret;
  1190. }
  1191. amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
  1192. pm_runtime_mark_last_busy(ddev->dev);
  1193. pm_runtime_put_autosuspend(ddev->dev);
  1194. return count;
  1195. }
  1196. /**
  1197. * DOC: pp_power_profile_mode
  1198. *
  1199. * The amdgpu driver provides a sysfs API for adjusting the heuristics
  1200. * related to switching between power levels in a power state. The file
  1201. * pp_power_profile_mode is used for this.
  1202. *
  1203. * Reading this file outputs a list of all of the predefined power profiles
  1204. * and the relevant heuristics settings for that profile.
  1205. *
  1206. * To select a profile or create a custom profile, first select manual using
  1207. * power_dpm_force_performance_level. Writing the number of a predefined
  1208. * profile to pp_power_profile_mode will enable those heuristics. To
  1209. * create a custom set of heuristics, write a string of numbers to the file
  1210. * starting with the number of the custom profile along with a setting
  1211. * for each heuristic parameter. Due to differences across asic families
  1212. * the heuristic parameters vary from family to family. Additionally,
  1213. * you can apply the custom heuristics to different clock domains. Each
  1214. * clock domain is considered a distinct operation so if you modify the
  1215. * gfxclk heuristics and then the memclk heuristics, the all of the
  1216. * custom heuristics will be retained until you switch to another profile.
  1217. *
  1218. */
  1219. static ssize_t amdgpu_get_pp_power_profile_mode(struct device *dev,
  1220. struct device_attribute *attr,
  1221. char *buf)
  1222. {
  1223. struct drm_device *ddev = dev_get_drvdata(dev);
  1224. struct amdgpu_device *adev = drm_to_adev(ddev);
  1225. ssize_t size;
  1226. int ret;
  1227. if (amdgpu_in_reset(adev))
  1228. return -EPERM;
  1229. if (adev->in_suspend && !adev->in_runpm)
  1230. return -EPERM;
  1231. ret = pm_runtime_get_sync(ddev->dev);
  1232. if (ret < 0) {
  1233. pm_runtime_put_autosuspend(ddev->dev);
  1234. return ret;
  1235. }
  1236. size = amdgpu_dpm_get_power_profile_mode(adev, buf);
  1237. if (size <= 0)
  1238. size = sysfs_emit(buf, "\n");
  1239. pm_runtime_mark_last_busy(ddev->dev);
  1240. pm_runtime_put_autosuspend(ddev->dev);
  1241. return size;
  1242. }
  1243. static ssize_t amdgpu_set_pp_power_profile_mode(struct device *dev,
  1244. struct device_attribute *attr,
  1245. const char *buf,
  1246. size_t count)
  1247. {
  1248. int ret;
  1249. struct drm_device *ddev = dev_get_drvdata(dev);
  1250. struct amdgpu_device *adev = drm_to_adev(ddev);
  1251. uint32_t parameter_size = 0;
  1252. long parameter[64];
  1253. char *sub_str, buf_cpy[128];
  1254. char *tmp_str;
  1255. uint32_t i = 0;
  1256. char tmp[2];
  1257. long int profile_mode = 0;
  1258. const char delimiter[3] = {' ', '\n', '\0'};
  1259. if (amdgpu_in_reset(adev))
  1260. return -EPERM;
  1261. if (adev->in_suspend && !adev->in_runpm)
  1262. return -EPERM;
  1263. tmp[0] = *(buf);
  1264. tmp[1] = '\0';
  1265. ret = kstrtol(tmp, 0, &profile_mode);
  1266. if (ret)
  1267. return -EINVAL;
  1268. if (profile_mode == PP_SMC_POWER_PROFILE_CUSTOM) {
  1269. if (count < 2 || count > 127)
  1270. return -EINVAL;
  1271. while (isspace(*++buf))
  1272. i++;
  1273. memcpy(buf_cpy, buf, count-i);
  1274. tmp_str = buf_cpy;
  1275. while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
  1276. if (strlen(sub_str) == 0)
  1277. continue;
  1278. ret = kstrtol(sub_str, 0, &parameter[parameter_size]);
  1279. if (ret)
  1280. return -EINVAL;
  1281. parameter_size++;
  1282. if (!tmp_str)
  1283. break;
  1284. while (isspace(*tmp_str))
  1285. tmp_str++;
  1286. }
  1287. }
  1288. parameter[parameter_size] = profile_mode;
  1289. ret = pm_runtime_get_sync(ddev->dev);
  1290. if (ret < 0) {
  1291. pm_runtime_put_autosuspend(ddev->dev);
  1292. return ret;
  1293. }
  1294. ret = amdgpu_dpm_set_power_profile_mode(adev, parameter, parameter_size);
  1295. pm_runtime_mark_last_busy(ddev->dev);
  1296. pm_runtime_put_autosuspend(ddev->dev);
  1297. if (!ret)
  1298. return count;
  1299. return -EINVAL;
  1300. }
  1301. static int amdgpu_hwmon_get_sensor_generic(struct amdgpu_device *adev,
  1302. enum amd_pp_sensors sensor,
  1303. void *query)
  1304. {
  1305. int r, size = sizeof(uint32_t);
  1306. if (amdgpu_in_reset(adev))
  1307. return -EPERM;
  1308. if (adev->in_suspend && !adev->in_runpm)
  1309. return -EPERM;
  1310. r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  1311. if (r < 0) {
  1312. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  1313. return r;
  1314. }
  1315. /* get the sensor value */
  1316. r = amdgpu_dpm_read_sensor(adev, sensor, query, &size);
  1317. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  1318. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  1319. return r;
  1320. }
  1321. /**
  1322. * DOC: gpu_busy_percent
  1323. *
  1324. * The amdgpu driver provides a sysfs API for reading how busy the GPU
  1325. * is as a percentage. The file gpu_busy_percent is used for this.
  1326. * The SMU firmware computes a percentage of load based on the
  1327. * aggregate activity level in the IP cores.
  1328. */
  1329. static ssize_t amdgpu_get_gpu_busy_percent(struct device *dev,
  1330. struct device_attribute *attr,
  1331. char *buf)
  1332. {
  1333. struct drm_device *ddev = dev_get_drvdata(dev);
  1334. struct amdgpu_device *adev = drm_to_adev(ddev);
  1335. unsigned int value;
  1336. int r;
  1337. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value);
  1338. if (r)
  1339. return r;
  1340. return sysfs_emit(buf, "%d\n", value);
  1341. }
  1342. /**
  1343. * DOC: mem_busy_percent
  1344. *
  1345. * The amdgpu driver provides a sysfs API for reading how busy the VRAM
  1346. * is as a percentage. The file mem_busy_percent is used for this.
  1347. * The SMU firmware computes a percentage of load based on the
  1348. * aggregate activity level in the IP cores.
  1349. */
  1350. static ssize_t amdgpu_get_mem_busy_percent(struct device *dev,
  1351. struct device_attribute *attr,
  1352. char *buf)
  1353. {
  1354. struct drm_device *ddev = dev_get_drvdata(dev);
  1355. struct amdgpu_device *adev = drm_to_adev(ddev);
  1356. unsigned int value;
  1357. int r;
  1358. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_LOAD, &value);
  1359. if (r)
  1360. return r;
  1361. return sysfs_emit(buf, "%d\n", value);
  1362. }
  1363. /**
  1364. * DOC: vcn_busy_percent
  1365. *
  1366. * The amdgpu driver provides a sysfs API for reading how busy the VCN
  1367. * is as a percentage. The file vcn_busy_percent is used for this.
  1368. * The SMU firmware computes a percentage of load based on the
  1369. * aggregate activity level in the IP cores.
  1370. */
  1371. static ssize_t amdgpu_get_vcn_busy_percent(struct device *dev,
  1372. struct device_attribute *attr,
  1373. char *buf)
  1374. {
  1375. struct drm_device *ddev = dev_get_drvdata(dev);
  1376. struct amdgpu_device *adev = drm_to_adev(ddev);
  1377. unsigned int value;
  1378. int r;
  1379. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VCN_LOAD, &value);
  1380. if (r)
  1381. return r;
  1382. return sysfs_emit(buf, "%d\n", value);
  1383. }
  1384. /**
  1385. * DOC: pcie_bw
  1386. *
  1387. * The amdgpu driver provides a sysfs API for estimating how much data
  1388. * has been received and sent by the GPU in the last second through PCIe.
  1389. * The file pcie_bw is used for this.
  1390. * The Perf counters count the number of received and sent messages and return
  1391. * those values, as well as the maximum payload size of a PCIe packet (mps).
  1392. * Note that it is not possible to easily and quickly obtain the size of each
  1393. * packet transmitted, so we output the max payload size (mps) to allow for
  1394. * quick estimation of the PCIe bandwidth usage
  1395. */
  1396. static ssize_t amdgpu_get_pcie_bw(struct device *dev,
  1397. struct device_attribute *attr,
  1398. char *buf)
  1399. {
  1400. struct drm_device *ddev = dev_get_drvdata(dev);
  1401. struct amdgpu_device *adev = drm_to_adev(ddev);
  1402. uint64_t count0 = 0, count1 = 0;
  1403. int ret;
  1404. if (amdgpu_in_reset(adev))
  1405. return -EPERM;
  1406. if (adev->in_suspend && !adev->in_runpm)
  1407. return -EPERM;
  1408. if (adev->flags & AMD_IS_APU)
  1409. return -ENODATA;
  1410. if (!adev->asic_funcs->get_pcie_usage)
  1411. return -ENODATA;
  1412. ret = pm_runtime_get_sync(ddev->dev);
  1413. if (ret < 0) {
  1414. pm_runtime_put_autosuspend(ddev->dev);
  1415. return ret;
  1416. }
  1417. amdgpu_asic_get_pcie_usage(adev, &count0, &count1);
  1418. pm_runtime_mark_last_busy(ddev->dev);
  1419. pm_runtime_put_autosuspend(ddev->dev);
  1420. return sysfs_emit(buf, "%llu %llu %i\n",
  1421. count0, count1, pcie_get_mps(adev->pdev));
  1422. }
  1423. /**
  1424. * DOC: unique_id
  1425. *
  1426. * The amdgpu driver provides a sysfs API for providing a unique ID for the GPU
  1427. * The file unique_id is used for this.
  1428. * This will provide a Unique ID that will persist from machine to machine
  1429. *
  1430. * NOTE: This will only work for GFX9 and newer. This file will be absent
  1431. * on unsupported ASICs (GFX8 and older)
  1432. */
  1433. static ssize_t amdgpu_get_unique_id(struct device *dev,
  1434. struct device_attribute *attr,
  1435. char *buf)
  1436. {
  1437. struct drm_device *ddev = dev_get_drvdata(dev);
  1438. struct amdgpu_device *adev = drm_to_adev(ddev);
  1439. if (amdgpu_in_reset(adev))
  1440. return -EPERM;
  1441. if (adev->in_suspend && !adev->in_runpm)
  1442. return -EPERM;
  1443. if (adev->unique_id)
  1444. return sysfs_emit(buf, "%016llx\n", adev->unique_id);
  1445. return 0;
  1446. }
  1447. /**
  1448. * DOC: thermal_throttling_logging
  1449. *
  1450. * Thermal throttling pulls down the clock frequency and thus the performance.
  1451. * It's an useful mechanism to protect the chip from overheating. Since it
  1452. * impacts performance, the user controls whether it is enabled and if so,
  1453. * the log frequency.
  1454. *
  1455. * Reading back the file shows you the status(enabled or disabled) and
  1456. * the interval(in seconds) between each thermal logging.
  1457. *
  1458. * Writing an integer to the file, sets a new logging interval, in seconds.
  1459. * The value should be between 1 and 3600. If the value is less than 1,
  1460. * thermal logging is disabled. Values greater than 3600 are ignored.
  1461. */
  1462. static ssize_t amdgpu_get_thermal_throttling_logging(struct device *dev,
  1463. struct device_attribute *attr,
  1464. char *buf)
  1465. {
  1466. struct drm_device *ddev = dev_get_drvdata(dev);
  1467. struct amdgpu_device *adev = drm_to_adev(ddev);
  1468. return sysfs_emit(buf, "%s: thermal throttling logging %s, with interval %d seconds\n",
  1469. adev_to_drm(adev)->unique,
  1470. atomic_read(&adev->throttling_logging_enabled) ? "enabled" : "disabled",
  1471. adev->throttling_logging_rs.interval / HZ + 1);
  1472. }
  1473. static ssize_t amdgpu_set_thermal_throttling_logging(struct device *dev,
  1474. struct device_attribute *attr,
  1475. const char *buf,
  1476. size_t count)
  1477. {
  1478. struct drm_device *ddev = dev_get_drvdata(dev);
  1479. struct amdgpu_device *adev = drm_to_adev(ddev);
  1480. long throttling_logging_interval;
  1481. unsigned long flags;
  1482. int ret = 0;
  1483. ret = kstrtol(buf, 0, &throttling_logging_interval);
  1484. if (ret)
  1485. return ret;
  1486. if (throttling_logging_interval > 3600)
  1487. return -EINVAL;
  1488. if (throttling_logging_interval > 0) {
  1489. raw_spin_lock_irqsave(&adev->throttling_logging_rs.lock, flags);
  1490. /*
  1491. * Reset the ratelimit timer internals.
  1492. * This can effectively restart the timer.
  1493. */
  1494. adev->throttling_logging_rs.interval =
  1495. (throttling_logging_interval - 1) * HZ;
  1496. adev->throttling_logging_rs.begin = 0;
  1497. adev->throttling_logging_rs.printed = 0;
  1498. adev->throttling_logging_rs.missed = 0;
  1499. raw_spin_unlock_irqrestore(&adev->throttling_logging_rs.lock, flags);
  1500. atomic_set(&adev->throttling_logging_enabled, 1);
  1501. } else {
  1502. atomic_set(&adev->throttling_logging_enabled, 0);
  1503. }
  1504. return count;
  1505. }
  1506. /**
  1507. * DOC: apu_thermal_cap
  1508. *
  1509. * The amdgpu driver provides a sysfs API for retrieving/updating thermal
  1510. * limit temperature in millidegrees Celsius
  1511. *
  1512. * Reading back the file shows you core limit value
  1513. *
  1514. * Writing an integer to the file, sets a new thermal limit. The value
  1515. * should be between 0 and 100. If the value is less than 0 or greater
  1516. * than 100, then the write request will be ignored.
  1517. */
  1518. static ssize_t amdgpu_get_apu_thermal_cap(struct device *dev,
  1519. struct device_attribute *attr,
  1520. char *buf)
  1521. {
  1522. int ret, size;
  1523. u32 limit;
  1524. struct drm_device *ddev = dev_get_drvdata(dev);
  1525. struct amdgpu_device *adev = drm_to_adev(ddev);
  1526. ret = pm_runtime_get_sync(ddev->dev);
  1527. if (ret < 0) {
  1528. pm_runtime_put_autosuspend(ddev->dev);
  1529. return ret;
  1530. }
  1531. ret = amdgpu_dpm_get_apu_thermal_limit(adev, &limit);
  1532. if (!ret)
  1533. size = sysfs_emit(buf, "%u\n", limit);
  1534. else
  1535. size = sysfs_emit(buf, "failed to get thermal limit\n");
  1536. pm_runtime_mark_last_busy(ddev->dev);
  1537. pm_runtime_put_autosuspend(ddev->dev);
  1538. return size;
  1539. }
  1540. static ssize_t amdgpu_set_apu_thermal_cap(struct device *dev,
  1541. struct device_attribute *attr,
  1542. const char *buf,
  1543. size_t count)
  1544. {
  1545. int ret;
  1546. u32 value;
  1547. struct drm_device *ddev = dev_get_drvdata(dev);
  1548. struct amdgpu_device *adev = drm_to_adev(ddev);
  1549. ret = kstrtou32(buf, 10, &value);
  1550. if (ret)
  1551. return ret;
  1552. if (value > 100) {
  1553. dev_err(dev, "Invalid argument !\n");
  1554. return -EINVAL;
  1555. }
  1556. ret = pm_runtime_get_sync(ddev->dev);
  1557. if (ret < 0) {
  1558. pm_runtime_put_autosuspend(ddev->dev);
  1559. return ret;
  1560. }
  1561. ret = amdgpu_dpm_set_apu_thermal_limit(adev, value);
  1562. if (ret) {
  1563. dev_err(dev, "failed to update thermal limit\n");
  1564. return ret;
  1565. }
  1566. pm_runtime_mark_last_busy(ddev->dev);
  1567. pm_runtime_put_autosuspend(ddev->dev);
  1568. return count;
  1569. }
  1570. static int amdgpu_pm_metrics_attr_update(struct amdgpu_device *adev,
  1571. struct amdgpu_device_attr *attr,
  1572. uint32_t mask,
  1573. enum amdgpu_device_attr_states *states)
  1574. {
  1575. if (amdgpu_dpm_get_pm_metrics(adev, NULL, 0) == -EOPNOTSUPP)
  1576. *states = ATTR_STATE_UNSUPPORTED;
  1577. return 0;
  1578. }
  1579. static ssize_t amdgpu_get_pm_metrics(struct device *dev,
  1580. struct device_attribute *attr, char *buf)
  1581. {
  1582. struct drm_device *ddev = dev_get_drvdata(dev);
  1583. struct amdgpu_device *adev = drm_to_adev(ddev);
  1584. ssize_t size = 0;
  1585. int ret;
  1586. if (amdgpu_in_reset(adev))
  1587. return -EPERM;
  1588. if (adev->in_suspend && !adev->in_runpm)
  1589. return -EPERM;
  1590. ret = pm_runtime_get_sync(ddev->dev);
  1591. if (ret < 0) {
  1592. pm_runtime_put_autosuspend(ddev->dev);
  1593. return ret;
  1594. }
  1595. size = amdgpu_dpm_get_pm_metrics(adev, buf, PAGE_SIZE);
  1596. pm_runtime_mark_last_busy(ddev->dev);
  1597. pm_runtime_put_autosuspend(ddev->dev);
  1598. return size;
  1599. }
  1600. /**
  1601. * DOC: gpu_metrics
  1602. *
  1603. * The amdgpu driver provides a sysfs API for retrieving current gpu
  1604. * metrics data. The file gpu_metrics is used for this. Reading the
  1605. * file will dump all the current gpu metrics data.
  1606. *
  1607. * These data include temperature, frequency, engines utilization,
  1608. * power consume, throttler status, fan speed and cpu core statistics(
  1609. * available for APU only). That's it will give a snapshot of all sensors
  1610. * at the same time.
  1611. */
  1612. static ssize_t amdgpu_get_gpu_metrics(struct device *dev,
  1613. struct device_attribute *attr,
  1614. char *buf)
  1615. {
  1616. struct drm_device *ddev = dev_get_drvdata(dev);
  1617. struct amdgpu_device *adev = drm_to_adev(ddev);
  1618. void *gpu_metrics;
  1619. ssize_t size = 0;
  1620. int ret;
  1621. if (amdgpu_in_reset(adev))
  1622. return -EPERM;
  1623. if (adev->in_suspend && !adev->in_runpm)
  1624. return -EPERM;
  1625. ret = pm_runtime_get_sync(ddev->dev);
  1626. if (ret < 0) {
  1627. pm_runtime_put_autosuspend(ddev->dev);
  1628. return ret;
  1629. }
  1630. size = amdgpu_dpm_get_gpu_metrics(adev, &gpu_metrics);
  1631. if (size <= 0)
  1632. goto out;
  1633. if (size >= PAGE_SIZE)
  1634. size = PAGE_SIZE - 1;
  1635. memcpy(buf, gpu_metrics, size);
  1636. out:
  1637. pm_runtime_mark_last_busy(ddev->dev);
  1638. pm_runtime_put_autosuspend(ddev->dev);
  1639. return size;
  1640. }
  1641. static int amdgpu_show_powershift_percent(struct device *dev,
  1642. char *buf, enum amd_pp_sensors sensor)
  1643. {
  1644. struct drm_device *ddev = dev_get_drvdata(dev);
  1645. struct amdgpu_device *adev = drm_to_adev(ddev);
  1646. uint32_t ss_power;
  1647. int r = 0, i;
  1648. r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
  1649. if (r == -EOPNOTSUPP) {
  1650. /* sensor not available on dGPU, try to read from APU */
  1651. adev = NULL;
  1652. mutex_lock(&mgpu_info.mutex);
  1653. for (i = 0; i < mgpu_info.num_gpu; i++) {
  1654. if (mgpu_info.gpu_ins[i].adev->flags & AMD_IS_APU) {
  1655. adev = mgpu_info.gpu_ins[i].adev;
  1656. break;
  1657. }
  1658. }
  1659. mutex_unlock(&mgpu_info.mutex);
  1660. if (adev)
  1661. r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&ss_power);
  1662. }
  1663. if (r)
  1664. return r;
  1665. return sysfs_emit(buf, "%u%%\n", ss_power);
  1666. }
  1667. /**
  1668. * DOC: smartshift_apu_power
  1669. *
  1670. * The amdgpu driver provides a sysfs API for reporting APU power
  1671. * shift in percentage if platform supports smartshift. Value 0 means that
  1672. * there is no powershift and values between [1-100] means that the power
  1673. * is shifted to APU, the percentage of boost is with respect to APU power
  1674. * limit on the platform.
  1675. */
  1676. static ssize_t amdgpu_get_smartshift_apu_power(struct device *dev, struct device_attribute *attr,
  1677. char *buf)
  1678. {
  1679. return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_APU_SHARE);
  1680. }
  1681. /**
  1682. * DOC: smartshift_dgpu_power
  1683. *
  1684. * The amdgpu driver provides a sysfs API for reporting dGPU power
  1685. * shift in percentage if platform supports smartshift. Value 0 means that
  1686. * there is no powershift and values between [1-100] means that the power is
  1687. * shifted to dGPU, the percentage of boost is with respect to dGPU power
  1688. * limit on the platform.
  1689. */
  1690. static ssize_t amdgpu_get_smartshift_dgpu_power(struct device *dev, struct device_attribute *attr,
  1691. char *buf)
  1692. {
  1693. return amdgpu_show_powershift_percent(dev, buf, AMDGPU_PP_SENSOR_SS_DGPU_SHARE);
  1694. }
  1695. /**
  1696. * DOC: smartshift_bias
  1697. *
  1698. * The amdgpu driver provides a sysfs API for reporting the
  1699. * smartshift(SS2.0) bias level. The value ranges from -100 to 100
  1700. * and the default is 0. -100 sets maximum preference to APU
  1701. * and 100 sets max perference to dGPU.
  1702. */
  1703. static ssize_t amdgpu_get_smartshift_bias(struct device *dev,
  1704. struct device_attribute *attr,
  1705. char *buf)
  1706. {
  1707. int r = 0;
  1708. r = sysfs_emit(buf, "%d\n", amdgpu_smartshift_bias);
  1709. return r;
  1710. }
  1711. static ssize_t amdgpu_set_smartshift_bias(struct device *dev,
  1712. struct device_attribute *attr,
  1713. const char *buf, size_t count)
  1714. {
  1715. struct drm_device *ddev = dev_get_drvdata(dev);
  1716. struct amdgpu_device *adev = drm_to_adev(ddev);
  1717. int r = 0;
  1718. int bias = 0;
  1719. if (amdgpu_in_reset(adev))
  1720. return -EPERM;
  1721. if (adev->in_suspend && !adev->in_runpm)
  1722. return -EPERM;
  1723. r = pm_runtime_get_sync(ddev->dev);
  1724. if (r < 0) {
  1725. pm_runtime_put_autosuspend(ddev->dev);
  1726. return r;
  1727. }
  1728. r = kstrtoint(buf, 10, &bias);
  1729. if (r)
  1730. goto out;
  1731. if (bias > AMDGPU_SMARTSHIFT_MAX_BIAS)
  1732. bias = AMDGPU_SMARTSHIFT_MAX_BIAS;
  1733. else if (bias < AMDGPU_SMARTSHIFT_MIN_BIAS)
  1734. bias = AMDGPU_SMARTSHIFT_MIN_BIAS;
  1735. amdgpu_smartshift_bias = bias;
  1736. r = count;
  1737. /* TODO: update bias level with SMU message */
  1738. out:
  1739. pm_runtime_mark_last_busy(ddev->dev);
  1740. pm_runtime_put_autosuspend(ddev->dev);
  1741. return r;
  1742. }
  1743. static int ss_power_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  1744. uint32_t mask, enum amdgpu_device_attr_states *states)
  1745. {
  1746. if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
  1747. *states = ATTR_STATE_UNSUPPORTED;
  1748. return 0;
  1749. }
  1750. static int ss_bias_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  1751. uint32_t mask, enum amdgpu_device_attr_states *states)
  1752. {
  1753. uint32_t ss_power;
  1754. if (!amdgpu_device_supports_smart_shift(adev_to_drm(adev)))
  1755. *states = ATTR_STATE_UNSUPPORTED;
  1756. else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_APU_SHARE,
  1757. (void *)&ss_power))
  1758. *states = ATTR_STATE_UNSUPPORTED;
  1759. else if (amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_SS_DGPU_SHARE,
  1760. (void *)&ss_power))
  1761. *states = ATTR_STATE_UNSUPPORTED;
  1762. return 0;
  1763. }
  1764. static int pp_od_clk_voltage_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  1765. uint32_t mask, enum amdgpu_device_attr_states *states)
  1766. {
  1767. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  1768. *states = ATTR_STATE_SUPPORTED;
  1769. if (!amdgpu_dpm_is_overdrive_supported(adev)) {
  1770. *states = ATTR_STATE_UNSUPPORTED;
  1771. return 0;
  1772. }
  1773. /* Enable pp_od_clk_voltage node for gc 9.4.3 SRIOV/BM support */
  1774. if (gc_ver == IP_VERSION(9, 4, 3) ||
  1775. gc_ver == IP_VERSION(9, 4, 4)) {
  1776. if (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))
  1777. *states = ATTR_STATE_UNSUPPORTED;
  1778. return 0;
  1779. }
  1780. if (!(attr->flags & mask))
  1781. *states = ATTR_STATE_UNSUPPORTED;
  1782. return 0;
  1783. }
  1784. static int pp_dpm_dcefclk_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  1785. uint32_t mask, enum amdgpu_device_attr_states *states)
  1786. {
  1787. struct device_attribute *dev_attr = &attr->dev_attr;
  1788. uint32_t gc_ver;
  1789. *states = ATTR_STATE_SUPPORTED;
  1790. if (!(attr->flags & mask)) {
  1791. *states = ATTR_STATE_UNSUPPORTED;
  1792. return 0;
  1793. }
  1794. gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  1795. /* dcefclk node is not available on gfx 11.0.3 sriov */
  1796. if ((gc_ver == IP_VERSION(11, 0, 3) && amdgpu_sriov_is_pp_one_vf(adev)) ||
  1797. gc_ver < IP_VERSION(9, 0, 0) ||
  1798. !amdgpu_device_has_display_hardware(adev))
  1799. *states = ATTR_STATE_UNSUPPORTED;
  1800. /* SMU MP1 does not support dcefclk level setting,
  1801. * setting should not be allowed from VF if not in one VF mode.
  1802. */
  1803. if (gc_ver >= IP_VERSION(10, 0, 0) ||
  1804. (amdgpu_sriov_vf(adev) && !amdgpu_sriov_is_pp_one_vf(adev))) {
  1805. dev_attr->attr.mode &= ~S_IWUGO;
  1806. dev_attr->store = NULL;
  1807. }
  1808. return 0;
  1809. }
  1810. static int pp_dpm_clk_default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  1811. uint32_t mask, enum amdgpu_device_attr_states *states)
  1812. {
  1813. struct device_attribute *dev_attr = &attr->dev_attr;
  1814. enum amdgpu_device_attr_id attr_id = attr->attr_id;
  1815. uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
  1816. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  1817. *states = ATTR_STATE_SUPPORTED;
  1818. if (!(attr->flags & mask)) {
  1819. *states = ATTR_STATE_UNSUPPORTED;
  1820. return 0;
  1821. }
  1822. if (DEVICE_ATTR_IS(pp_dpm_socclk)) {
  1823. if (gc_ver < IP_VERSION(9, 0, 0))
  1824. *states = ATTR_STATE_UNSUPPORTED;
  1825. } else if (DEVICE_ATTR_IS(pp_dpm_fclk)) {
  1826. if (mp1_ver < IP_VERSION(10, 0, 0))
  1827. *states = ATTR_STATE_UNSUPPORTED;
  1828. } else if (DEVICE_ATTR_IS(pp_dpm_vclk)) {
  1829. if (!(gc_ver == IP_VERSION(10, 3, 1) ||
  1830. gc_ver == IP_VERSION(10, 3, 3) ||
  1831. gc_ver == IP_VERSION(10, 3, 6) ||
  1832. gc_ver == IP_VERSION(10, 3, 7) ||
  1833. gc_ver == IP_VERSION(10, 3, 0) ||
  1834. gc_ver == IP_VERSION(10, 1, 2) ||
  1835. gc_ver == IP_VERSION(11, 0, 0) ||
  1836. gc_ver == IP_VERSION(11, 0, 1) ||
  1837. gc_ver == IP_VERSION(11, 0, 4) ||
  1838. gc_ver == IP_VERSION(11, 5, 0) ||
  1839. gc_ver == IP_VERSION(11, 0, 2) ||
  1840. gc_ver == IP_VERSION(11, 0, 3) ||
  1841. gc_ver == IP_VERSION(9, 4, 3) ||
  1842. gc_ver == IP_VERSION(9, 4, 4)))
  1843. *states = ATTR_STATE_UNSUPPORTED;
  1844. } else if (DEVICE_ATTR_IS(pp_dpm_vclk1)) {
  1845. if (!((gc_ver == IP_VERSION(10, 3, 1) ||
  1846. gc_ver == IP_VERSION(10, 3, 0) ||
  1847. gc_ver == IP_VERSION(11, 0, 2) ||
  1848. gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
  1849. *states = ATTR_STATE_UNSUPPORTED;
  1850. } else if (DEVICE_ATTR_IS(pp_dpm_dclk)) {
  1851. if (!(gc_ver == IP_VERSION(10, 3, 1) ||
  1852. gc_ver == IP_VERSION(10, 3, 3) ||
  1853. gc_ver == IP_VERSION(10, 3, 6) ||
  1854. gc_ver == IP_VERSION(10, 3, 7) ||
  1855. gc_ver == IP_VERSION(10, 3, 0) ||
  1856. gc_ver == IP_VERSION(10, 1, 2) ||
  1857. gc_ver == IP_VERSION(11, 0, 0) ||
  1858. gc_ver == IP_VERSION(11, 0, 1) ||
  1859. gc_ver == IP_VERSION(11, 0, 4) ||
  1860. gc_ver == IP_VERSION(11, 5, 0) ||
  1861. gc_ver == IP_VERSION(11, 0, 2) ||
  1862. gc_ver == IP_VERSION(11, 0, 3) ||
  1863. gc_ver == IP_VERSION(9, 4, 3) ||
  1864. gc_ver == IP_VERSION(9, 4, 4)))
  1865. *states = ATTR_STATE_UNSUPPORTED;
  1866. } else if (DEVICE_ATTR_IS(pp_dpm_dclk1)) {
  1867. if (!((gc_ver == IP_VERSION(10, 3, 1) ||
  1868. gc_ver == IP_VERSION(10, 3, 0) ||
  1869. gc_ver == IP_VERSION(11, 0, 2) ||
  1870. gc_ver == IP_VERSION(11, 0, 3)) && adev->vcn.num_vcn_inst >= 2))
  1871. *states = ATTR_STATE_UNSUPPORTED;
  1872. } else if (DEVICE_ATTR_IS(pp_dpm_pcie)) {
  1873. if (gc_ver == IP_VERSION(9, 4, 2) ||
  1874. gc_ver == IP_VERSION(9, 4, 3) ||
  1875. gc_ver == IP_VERSION(9, 4, 4))
  1876. *states = ATTR_STATE_UNSUPPORTED;
  1877. }
  1878. switch (gc_ver) {
  1879. case IP_VERSION(9, 4, 1):
  1880. case IP_VERSION(9, 4, 2):
  1881. /* the Mi series card does not support standalone mclk/socclk/fclk level setting */
  1882. if (DEVICE_ATTR_IS(pp_dpm_mclk) ||
  1883. DEVICE_ATTR_IS(pp_dpm_socclk) ||
  1884. DEVICE_ATTR_IS(pp_dpm_fclk)) {
  1885. dev_attr->attr.mode &= ~S_IWUGO;
  1886. dev_attr->store = NULL;
  1887. }
  1888. break;
  1889. default:
  1890. break;
  1891. }
  1892. /* setting should not be allowed from VF if not in one VF mode */
  1893. if (amdgpu_sriov_vf(adev) && amdgpu_sriov_is_pp_one_vf(adev)) {
  1894. dev_attr->attr.mode &= ~S_IWUGO;
  1895. dev_attr->store = NULL;
  1896. }
  1897. return 0;
  1898. }
  1899. /* pm policy attributes */
  1900. struct amdgpu_pm_policy_attr {
  1901. struct device_attribute dev_attr;
  1902. enum pp_pm_policy id;
  1903. };
  1904. /**
  1905. * DOC: pm_policy
  1906. *
  1907. * Certain SOCs can support different power policies to optimize application
  1908. * performance. However, this policy is provided only at SOC level and not at a
  1909. * per-process level. This is useful especially when entire SOC is utilized for
  1910. * dedicated workload.
  1911. *
  1912. * The amdgpu driver provides a sysfs API for selecting the policy. Presently,
  1913. * only two types of policies are supported through this interface.
  1914. *
  1915. * Pstate Policy Selection - This is to select different Pstate profiles which
  1916. * decides clock/throttling preferences.
  1917. *
  1918. * XGMI PLPD Policy Selection - When multiple devices are connected over XGMI,
  1919. * this helps to select policy to be applied for per link power down.
  1920. *
  1921. * The list of available policies and policy levels vary between SOCs. They can
  1922. * be viewed under pm_policy node directory. If SOC doesn't support any policy,
  1923. * this node won't be available. The different policies supported will be
  1924. * available as separate nodes under pm_policy.
  1925. *
  1926. * cat /sys/bus/pci/devices/.../pm_policy/<policy_type>
  1927. *
  1928. * Reading the policy file shows the different levels supported. The level which
  1929. * is applied presently is denoted by * (asterisk). E.g.,
  1930. *
  1931. * .. code-block:: console
  1932. *
  1933. * cat /sys/bus/pci/devices/.../pm_policy/soc_pstate
  1934. * 0 : soc_pstate_default
  1935. * 1 : soc_pstate_0
  1936. * 2 : soc_pstate_1*
  1937. * 3 : soc_pstate_2
  1938. *
  1939. * cat /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
  1940. * 0 : plpd_disallow
  1941. * 1 : plpd_default
  1942. * 2 : plpd_optimized*
  1943. *
  1944. * To apply a specific policy
  1945. *
  1946. * "echo <level> > /sys/bus/pci/devices/.../pm_policy/<policy_type>"
  1947. *
  1948. * For the levels listed in the example above, to select "plpd_optimized" for
  1949. * XGMI and "soc_pstate_2" for soc pstate policy -
  1950. *
  1951. * .. code-block:: console
  1952. *
  1953. * echo "2" > /sys/bus/pci/devices/.../pm_policy/xgmi_plpd
  1954. * echo "3" > /sys/bus/pci/devices/.../pm_policy/soc_pstate
  1955. *
  1956. */
  1957. static ssize_t amdgpu_get_pm_policy_attr(struct device *dev,
  1958. struct device_attribute *attr,
  1959. char *buf)
  1960. {
  1961. struct drm_device *ddev = dev_get_drvdata(dev);
  1962. struct amdgpu_device *adev = drm_to_adev(ddev);
  1963. struct amdgpu_pm_policy_attr *policy_attr;
  1964. policy_attr =
  1965. container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
  1966. if (amdgpu_in_reset(adev))
  1967. return -EPERM;
  1968. if (adev->in_suspend && !adev->in_runpm)
  1969. return -EPERM;
  1970. return amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, buf);
  1971. }
  1972. static ssize_t amdgpu_set_pm_policy_attr(struct device *dev,
  1973. struct device_attribute *attr,
  1974. const char *buf, size_t count)
  1975. {
  1976. struct drm_device *ddev = dev_get_drvdata(dev);
  1977. struct amdgpu_device *adev = drm_to_adev(ddev);
  1978. struct amdgpu_pm_policy_attr *policy_attr;
  1979. int ret, num_params = 0;
  1980. char delimiter[] = " \n\t";
  1981. char tmp_buf[128];
  1982. char *tmp, *param;
  1983. long val;
  1984. if (amdgpu_in_reset(adev))
  1985. return -EPERM;
  1986. if (adev->in_suspend && !adev->in_runpm)
  1987. return -EPERM;
  1988. count = min(count, sizeof(tmp_buf));
  1989. memcpy(tmp_buf, buf, count);
  1990. tmp_buf[count - 1] = '\0';
  1991. tmp = tmp_buf;
  1992. tmp = skip_spaces(tmp);
  1993. while ((param = strsep(&tmp, delimiter))) {
  1994. if (!strlen(param)) {
  1995. tmp = skip_spaces(tmp);
  1996. continue;
  1997. }
  1998. ret = kstrtol(param, 0, &val);
  1999. if (ret)
  2000. return -EINVAL;
  2001. num_params++;
  2002. if (num_params > 1)
  2003. return -EINVAL;
  2004. }
  2005. if (num_params != 1)
  2006. return -EINVAL;
  2007. policy_attr =
  2008. container_of(attr, struct amdgpu_pm_policy_attr, dev_attr);
  2009. ret = pm_runtime_get_sync(ddev->dev);
  2010. if (ret < 0) {
  2011. pm_runtime_put_autosuspend(ddev->dev);
  2012. return ret;
  2013. }
  2014. ret = amdgpu_dpm_set_pm_policy(adev, policy_attr->id, val);
  2015. pm_runtime_mark_last_busy(ddev->dev);
  2016. pm_runtime_put_autosuspend(ddev->dev);
  2017. if (ret)
  2018. return ret;
  2019. return count;
  2020. }
  2021. #define AMDGPU_PM_POLICY_ATTR(_name, _id) \
  2022. static struct amdgpu_pm_policy_attr pm_policy_attr_##_name = { \
  2023. .dev_attr = __ATTR(_name, 0644, amdgpu_get_pm_policy_attr, \
  2024. amdgpu_set_pm_policy_attr), \
  2025. .id = PP_PM_POLICY_##_id, \
  2026. };
  2027. #define AMDGPU_PM_POLICY_ATTR_VAR(_name) pm_policy_attr_##_name.dev_attr.attr
  2028. AMDGPU_PM_POLICY_ATTR(soc_pstate, SOC_PSTATE)
  2029. AMDGPU_PM_POLICY_ATTR(xgmi_plpd, XGMI_PLPD)
  2030. static struct attribute *pm_policy_attrs[] = {
  2031. &AMDGPU_PM_POLICY_ATTR_VAR(soc_pstate),
  2032. &AMDGPU_PM_POLICY_ATTR_VAR(xgmi_plpd),
  2033. NULL
  2034. };
  2035. static umode_t amdgpu_pm_policy_attr_visible(struct kobject *kobj,
  2036. struct attribute *attr, int n)
  2037. {
  2038. struct device *dev = kobj_to_dev(kobj);
  2039. struct drm_device *ddev = dev_get_drvdata(dev);
  2040. struct amdgpu_device *adev = drm_to_adev(ddev);
  2041. struct amdgpu_pm_policy_attr *policy_attr;
  2042. policy_attr =
  2043. container_of(attr, struct amdgpu_pm_policy_attr, dev_attr.attr);
  2044. if (amdgpu_dpm_get_pm_policy_info(adev, policy_attr->id, NULL) ==
  2045. -ENOENT)
  2046. return 0;
  2047. return attr->mode;
  2048. }
  2049. const struct attribute_group amdgpu_pm_policy_attr_group = {
  2050. .name = "pm_policy",
  2051. .attrs = pm_policy_attrs,
  2052. .is_visible = amdgpu_pm_policy_attr_visible,
  2053. };
  2054. static struct amdgpu_device_attr amdgpu_device_attrs[] = {
  2055. AMDGPU_DEVICE_ATTR_RW(power_dpm_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2056. AMDGPU_DEVICE_ATTR_RW(power_dpm_force_performance_level, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2057. AMDGPU_DEVICE_ATTR_RO(pp_num_states, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2058. AMDGPU_DEVICE_ATTR_RO(pp_cur_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2059. AMDGPU_DEVICE_ATTR_RW(pp_force_state, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2060. AMDGPU_DEVICE_ATTR_RW(pp_table, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2061. AMDGPU_DEVICE_ATTR_RW(pp_dpm_sclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2062. .attr_update = pp_dpm_clk_default_attr_update),
  2063. AMDGPU_DEVICE_ATTR_RW(pp_dpm_mclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2064. .attr_update = pp_dpm_clk_default_attr_update),
  2065. AMDGPU_DEVICE_ATTR_RW(pp_dpm_socclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2066. .attr_update = pp_dpm_clk_default_attr_update),
  2067. AMDGPU_DEVICE_ATTR_RW(pp_dpm_fclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2068. .attr_update = pp_dpm_clk_default_attr_update),
  2069. AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2070. .attr_update = pp_dpm_clk_default_attr_update),
  2071. AMDGPU_DEVICE_ATTR_RW(pp_dpm_vclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2072. .attr_update = pp_dpm_clk_default_attr_update),
  2073. AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2074. .attr_update = pp_dpm_clk_default_attr_update),
  2075. AMDGPU_DEVICE_ATTR_RW(pp_dpm_dclk1, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2076. .attr_update = pp_dpm_clk_default_attr_update),
  2077. AMDGPU_DEVICE_ATTR_RW(pp_dpm_dcefclk, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2078. .attr_update = pp_dpm_dcefclk_attr_update),
  2079. AMDGPU_DEVICE_ATTR_RW(pp_dpm_pcie, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF,
  2080. .attr_update = pp_dpm_clk_default_attr_update),
  2081. AMDGPU_DEVICE_ATTR_RW(pp_sclk_od, ATTR_FLAG_BASIC),
  2082. AMDGPU_DEVICE_ATTR_RW(pp_mclk_od, ATTR_FLAG_BASIC),
  2083. AMDGPU_DEVICE_ATTR_RW(pp_power_profile_mode, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2084. AMDGPU_DEVICE_ATTR_RW(pp_od_clk_voltage, ATTR_FLAG_BASIC,
  2085. .attr_update = pp_od_clk_voltage_attr_update),
  2086. AMDGPU_DEVICE_ATTR_RO(gpu_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2087. AMDGPU_DEVICE_ATTR_RO(mem_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2088. AMDGPU_DEVICE_ATTR_RO(vcn_busy_percent, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2089. AMDGPU_DEVICE_ATTR_RO(pcie_bw, ATTR_FLAG_BASIC),
  2090. AMDGPU_DEVICE_ATTR_RW(pp_features, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2091. AMDGPU_DEVICE_ATTR_RO(unique_id, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2092. AMDGPU_DEVICE_ATTR_RW(thermal_throttling_logging, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2093. AMDGPU_DEVICE_ATTR_RW(apu_thermal_cap, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2094. AMDGPU_DEVICE_ATTR_RO(gpu_metrics, ATTR_FLAG_BASIC|ATTR_FLAG_ONEVF),
  2095. AMDGPU_DEVICE_ATTR_RO(smartshift_apu_power, ATTR_FLAG_BASIC,
  2096. .attr_update = ss_power_attr_update),
  2097. AMDGPU_DEVICE_ATTR_RO(smartshift_dgpu_power, ATTR_FLAG_BASIC,
  2098. .attr_update = ss_power_attr_update),
  2099. AMDGPU_DEVICE_ATTR_RW(smartshift_bias, ATTR_FLAG_BASIC,
  2100. .attr_update = ss_bias_attr_update),
  2101. AMDGPU_DEVICE_ATTR_RO(pm_metrics, ATTR_FLAG_BASIC,
  2102. .attr_update = amdgpu_pm_metrics_attr_update),
  2103. };
  2104. static int default_attr_update(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  2105. uint32_t mask, enum amdgpu_device_attr_states *states)
  2106. {
  2107. struct device_attribute *dev_attr = &attr->dev_attr;
  2108. enum amdgpu_device_attr_id attr_id = attr->attr_id;
  2109. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  2110. if (!(attr->flags & mask)) {
  2111. *states = ATTR_STATE_UNSUPPORTED;
  2112. return 0;
  2113. }
  2114. if (DEVICE_ATTR_IS(mem_busy_percent)) {
  2115. if ((adev->flags & AMD_IS_APU &&
  2116. gc_ver != IP_VERSION(9, 4, 3)) ||
  2117. gc_ver == IP_VERSION(9, 0, 1))
  2118. *states = ATTR_STATE_UNSUPPORTED;
  2119. } else if (DEVICE_ATTR_IS(vcn_busy_percent)) {
  2120. if (!(gc_ver == IP_VERSION(10, 3, 1) ||
  2121. gc_ver == IP_VERSION(10, 3, 3) ||
  2122. gc_ver == IP_VERSION(10, 3, 6) ||
  2123. gc_ver == IP_VERSION(10, 3, 7) ||
  2124. gc_ver == IP_VERSION(11, 0, 1) ||
  2125. gc_ver == IP_VERSION(11, 0, 4) ||
  2126. gc_ver == IP_VERSION(11, 5, 0)))
  2127. *states = ATTR_STATE_UNSUPPORTED;
  2128. } else if (DEVICE_ATTR_IS(pcie_bw)) {
  2129. /* PCIe Perf counters won't work on APU nodes */
  2130. if (adev->flags & AMD_IS_APU ||
  2131. !adev->asic_funcs->get_pcie_usage)
  2132. *states = ATTR_STATE_UNSUPPORTED;
  2133. } else if (DEVICE_ATTR_IS(unique_id)) {
  2134. switch (gc_ver) {
  2135. case IP_VERSION(9, 0, 1):
  2136. case IP_VERSION(9, 4, 0):
  2137. case IP_VERSION(9, 4, 1):
  2138. case IP_VERSION(9, 4, 2):
  2139. case IP_VERSION(9, 4, 3):
  2140. case IP_VERSION(9, 4, 4):
  2141. case IP_VERSION(10, 3, 0):
  2142. case IP_VERSION(11, 0, 0):
  2143. case IP_VERSION(11, 0, 1):
  2144. case IP_VERSION(11, 0, 2):
  2145. case IP_VERSION(11, 0, 3):
  2146. case IP_VERSION(12, 0, 0):
  2147. case IP_VERSION(12, 0, 1):
  2148. *states = ATTR_STATE_SUPPORTED;
  2149. break;
  2150. default:
  2151. *states = ATTR_STATE_UNSUPPORTED;
  2152. }
  2153. } else if (DEVICE_ATTR_IS(pp_features)) {
  2154. if ((adev->flags & AMD_IS_APU &&
  2155. gc_ver != IP_VERSION(9, 4, 3)) ||
  2156. gc_ver < IP_VERSION(9, 0, 0))
  2157. *states = ATTR_STATE_UNSUPPORTED;
  2158. } else if (DEVICE_ATTR_IS(gpu_metrics)) {
  2159. if (gc_ver < IP_VERSION(9, 1, 0))
  2160. *states = ATTR_STATE_UNSUPPORTED;
  2161. } else if (DEVICE_ATTR_IS(pp_power_profile_mode)) {
  2162. if (amdgpu_dpm_get_power_profile_mode(adev, NULL) == -EOPNOTSUPP)
  2163. *states = ATTR_STATE_UNSUPPORTED;
  2164. else if ((gc_ver == IP_VERSION(10, 3, 0) ||
  2165. gc_ver == IP_VERSION(11, 0, 3)) && amdgpu_sriov_vf(adev))
  2166. *states = ATTR_STATE_UNSUPPORTED;
  2167. } else if (DEVICE_ATTR_IS(pp_mclk_od)) {
  2168. if (amdgpu_dpm_get_mclk_od(adev) == -EOPNOTSUPP)
  2169. *states = ATTR_STATE_UNSUPPORTED;
  2170. } else if (DEVICE_ATTR_IS(pp_sclk_od)) {
  2171. if (amdgpu_dpm_get_sclk_od(adev) == -EOPNOTSUPP)
  2172. *states = ATTR_STATE_UNSUPPORTED;
  2173. } else if (DEVICE_ATTR_IS(apu_thermal_cap)) {
  2174. u32 limit;
  2175. if (amdgpu_dpm_get_apu_thermal_limit(adev, &limit) ==
  2176. -EOPNOTSUPP)
  2177. *states = ATTR_STATE_UNSUPPORTED;
  2178. }
  2179. switch (gc_ver) {
  2180. case IP_VERSION(10, 3, 0):
  2181. if (DEVICE_ATTR_IS(power_dpm_force_performance_level) &&
  2182. amdgpu_sriov_vf(adev)) {
  2183. dev_attr->attr.mode &= ~0222;
  2184. dev_attr->store = NULL;
  2185. }
  2186. break;
  2187. default:
  2188. break;
  2189. }
  2190. return 0;
  2191. }
  2192. static int amdgpu_device_attr_create(struct amdgpu_device *adev,
  2193. struct amdgpu_device_attr *attr,
  2194. uint32_t mask, struct list_head *attr_list)
  2195. {
  2196. int ret = 0;
  2197. enum amdgpu_device_attr_states attr_states = ATTR_STATE_SUPPORTED;
  2198. struct amdgpu_device_attr_entry *attr_entry;
  2199. struct device_attribute *dev_attr;
  2200. const char *name;
  2201. int (*attr_update)(struct amdgpu_device *adev, struct amdgpu_device_attr *attr,
  2202. uint32_t mask, enum amdgpu_device_attr_states *states) = default_attr_update;
  2203. if (!attr)
  2204. return -EINVAL;
  2205. dev_attr = &attr->dev_attr;
  2206. name = dev_attr->attr.name;
  2207. attr_update = attr->attr_update ? attr->attr_update : default_attr_update;
  2208. ret = attr_update(adev, attr, mask, &attr_states);
  2209. if (ret) {
  2210. dev_err(adev->dev, "failed to update device file %s, ret = %d\n",
  2211. name, ret);
  2212. return ret;
  2213. }
  2214. if (attr_states == ATTR_STATE_UNSUPPORTED)
  2215. return 0;
  2216. ret = device_create_file(adev->dev, dev_attr);
  2217. if (ret) {
  2218. dev_err(adev->dev, "failed to create device file %s, ret = %d\n",
  2219. name, ret);
  2220. }
  2221. attr_entry = kmalloc(sizeof(*attr_entry), GFP_KERNEL);
  2222. if (!attr_entry)
  2223. return -ENOMEM;
  2224. attr_entry->attr = attr;
  2225. INIT_LIST_HEAD(&attr_entry->entry);
  2226. list_add_tail(&attr_entry->entry, attr_list);
  2227. return ret;
  2228. }
  2229. static void amdgpu_device_attr_remove(struct amdgpu_device *adev, struct amdgpu_device_attr *attr)
  2230. {
  2231. struct device_attribute *dev_attr = &attr->dev_attr;
  2232. device_remove_file(adev->dev, dev_attr);
  2233. }
  2234. static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
  2235. struct list_head *attr_list);
  2236. static int amdgpu_device_attr_create_groups(struct amdgpu_device *adev,
  2237. struct amdgpu_device_attr *attrs,
  2238. uint32_t counts,
  2239. uint32_t mask,
  2240. struct list_head *attr_list)
  2241. {
  2242. int ret = 0;
  2243. uint32_t i = 0;
  2244. for (i = 0; i < counts; i++) {
  2245. ret = amdgpu_device_attr_create(adev, &attrs[i], mask, attr_list);
  2246. if (ret)
  2247. goto failed;
  2248. }
  2249. return 0;
  2250. failed:
  2251. amdgpu_device_attr_remove_groups(adev, attr_list);
  2252. return ret;
  2253. }
  2254. static void amdgpu_device_attr_remove_groups(struct amdgpu_device *adev,
  2255. struct list_head *attr_list)
  2256. {
  2257. struct amdgpu_device_attr_entry *entry, *entry_tmp;
  2258. if (list_empty(attr_list))
  2259. return ;
  2260. list_for_each_entry_safe(entry, entry_tmp, attr_list, entry) {
  2261. amdgpu_device_attr_remove(adev, entry->attr);
  2262. list_del(&entry->entry);
  2263. kfree(entry);
  2264. }
  2265. }
  2266. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  2267. struct device_attribute *attr,
  2268. char *buf)
  2269. {
  2270. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2271. int channel = to_sensor_dev_attr(attr)->index;
  2272. int r, temp = 0;
  2273. if (channel >= PP_TEMP_MAX)
  2274. return -EINVAL;
  2275. switch (channel) {
  2276. case PP_TEMP_JUNCTION:
  2277. /* get current junction temperature */
  2278. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_HOTSPOT_TEMP,
  2279. (void *)&temp);
  2280. break;
  2281. case PP_TEMP_EDGE:
  2282. /* get current edge temperature */
  2283. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_EDGE_TEMP,
  2284. (void *)&temp);
  2285. break;
  2286. case PP_TEMP_MEM:
  2287. /* get current memory temperature */
  2288. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MEM_TEMP,
  2289. (void *)&temp);
  2290. break;
  2291. default:
  2292. r = -EINVAL;
  2293. break;
  2294. }
  2295. if (r)
  2296. return r;
  2297. return sysfs_emit(buf, "%d\n", temp);
  2298. }
  2299. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  2300. struct device_attribute *attr,
  2301. char *buf)
  2302. {
  2303. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2304. int hyst = to_sensor_dev_attr(attr)->index;
  2305. int temp;
  2306. if (hyst)
  2307. temp = adev->pm.dpm.thermal.min_temp;
  2308. else
  2309. temp = adev->pm.dpm.thermal.max_temp;
  2310. return sysfs_emit(buf, "%d\n", temp);
  2311. }
  2312. static ssize_t amdgpu_hwmon_show_hotspot_temp_thresh(struct device *dev,
  2313. struct device_attribute *attr,
  2314. char *buf)
  2315. {
  2316. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2317. int hyst = to_sensor_dev_attr(attr)->index;
  2318. int temp;
  2319. if (hyst)
  2320. temp = adev->pm.dpm.thermal.min_hotspot_temp;
  2321. else
  2322. temp = adev->pm.dpm.thermal.max_hotspot_crit_temp;
  2323. return sysfs_emit(buf, "%d\n", temp);
  2324. }
  2325. static ssize_t amdgpu_hwmon_show_mem_temp_thresh(struct device *dev,
  2326. struct device_attribute *attr,
  2327. char *buf)
  2328. {
  2329. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2330. int hyst = to_sensor_dev_attr(attr)->index;
  2331. int temp;
  2332. if (hyst)
  2333. temp = adev->pm.dpm.thermal.min_mem_temp;
  2334. else
  2335. temp = adev->pm.dpm.thermal.max_mem_crit_temp;
  2336. return sysfs_emit(buf, "%d\n", temp);
  2337. }
  2338. static ssize_t amdgpu_hwmon_show_temp_label(struct device *dev,
  2339. struct device_attribute *attr,
  2340. char *buf)
  2341. {
  2342. int channel = to_sensor_dev_attr(attr)->index;
  2343. if (channel >= PP_TEMP_MAX)
  2344. return -EINVAL;
  2345. return sysfs_emit(buf, "%s\n", temp_label[channel].label);
  2346. }
  2347. static ssize_t amdgpu_hwmon_show_temp_emergency(struct device *dev,
  2348. struct device_attribute *attr,
  2349. char *buf)
  2350. {
  2351. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2352. int channel = to_sensor_dev_attr(attr)->index;
  2353. int temp = 0;
  2354. if (channel >= PP_TEMP_MAX)
  2355. return -EINVAL;
  2356. switch (channel) {
  2357. case PP_TEMP_JUNCTION:
  2358. temp = adev->pm.dpm.thermal.max_hotspot_emergency_temp;
  2359. break;
  2360. case PP_TEMP_EDGE:
  2361. temp = adev->pm.dpm.thermal.max_edge_emergency_temp;
  2362. break;
  2363. case PP_TEMP_MEM:
  2364. temp = adev->pm.dpm.thermal.max_mem_emergency_temp;
  2365. break;
  2366. }
  2367. return sysfs_emit(buf, "%d\n", temp);
  2368. }
  2369. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  2370. struct device_attribute *attr,
  2371. char *buf)
  2372. {
  2373. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2374. u32 pwm_mode = 0;
  2375. int ret;
  2376. if (amdgpu_in_reset(adev))
  2377. return -EPERM;
  2378. if (adev->in_suspend && !adev->in_runpm)
  2379. return -EPERM;
  2380. ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2381. if (ret < 0) {
  2382. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2383. return ret;
  2384. }
  2385. ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
  2386. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2387. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2388. if (ret)
  2389. return -EINVAL;
  2390. return sysfs_emit(buf, "%u\n", pwm_mode);
  2391. }
  2392. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  2393. struct device_attribute *attr,
  2394. const char *buf,
  2395. size_t count)
  2396. {
  2397. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2398. int err, ret;
  2399. u32 pwm_mode;
  2400. int value;
  2401. if (amdgpu_in_reset(adev))
  2402. return -EPERM;
  2403. if (adev->in_suspend && !adev->in_runpm)
  2404. return -EPERM;
  2405. err = kstrtoint(buf, 10, &value);
  2406. if (err)
  2407. return err;
  2408. if (value == 0)
  2409. pwm_mode = AMD_FAN_CTRL_NONE;
  2410. else if (value == 1)
  2411. pwm_mode = AMD_FAN_CTRL_MANUAL;
  2412. else if (value == 2)
  2413. pwm_mode = AMD_FAN_CTRL_AUTO;
  2414. else
  2415. return -EINVAL;
  2416. ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2417. if (ret < 0) {
  2418. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2419. return ret;
  2420. }
  2421. ret = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
  2422. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2423. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2424. if (ret)
  2425. return -EINVAL;
  2426. return count;
  2427. }
  2428. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  2429. struct device_attribute *attr,
  2430. char *buf)
  2431. {
  2432. return sysfs_emit(buf, "%i\n", 0);
  2433. }
  2434. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  2435. struct device_attribute *attr,
  2436. char *buf)
  2437. {
  2438. return sysfs_emit(buf, "%i\n", 255);
  2439. }
  2440. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  2441. struct device_attribute *attr,
  2442. const char *buf, size_t count)
  2443. {
  2444. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2445. int err;
  2446. u32 value;
  2447. u32 pwm_mode;
  2448. if (amdgpu_in_reset(adev))
  2449. return -EPERM;
  2450. if (adev->in_suspend && !adev->in_runpm)
  2451. return -EPERM;
  2452. err = kstrtou32(buf, 10, &value);
  2453. if (err)
  2454. return err;
  2455. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2456. if (err < 0) {
  2457. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2458. return err;
  2459. }
  2460. err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
  2461. if (err)
  2462. goto out;
  2463. if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
  2464. pr_info("manual fan speed control should be enabled first\n");
  2465. err = -EINVAL;
  2466. goto out;
  2467. }
  2468. err = amdgpu_dpm_set_fan_speed_pwm(adev, value);
  2469. out:
  2470. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2471. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2472. if (err)
  2473. return err;
  2474. return count;
  2475. }
  2476. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  2477. struct device_attribute *attr,
  2478. char *buf)
  2479. {
  2480. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2481. int err;
  2482. u32 speed = 0;
  2483. if (amdgpu_in_reset(adev))
  2484. return -EPERM;
  2485. if (adev->in_suspend && !adev->in_runpm)
  2486. return -EPERM;
  2487. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2488. if (err < 0) {
  2489. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2490. return err;
  2491. }
  2492. err = amdgpu_dpm_get_fan_speed_pwm(adev, &speed);
  2493. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2494. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2495. if (err)
  2496. return err;
  2497. return sysfs_emit(buf, "%i\n", speed);
  2498. }
  2499. static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
  2500. struct device_attribute *attr,
  2501. char *buf)
  2502. {
  2503. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2504. int err;
  2505. u32 speed = 0;
  2506. if (amdgpu_in_reset(adev))
  2507. return -EPERM;
  2508. if (adev->in_suspend && !adev->in_runpm)
  2509. return -EPERM;
  2510. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2511. if (err < 0) {
  2512. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2513. return err;
  2514. }
  2515. err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
  2516. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2517. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2518. if (err)
  2519. return err;
  2520. return sysfs_emit(buf, "%i\n", speed);
  2521. }
  2522. static ssize_t amdgpu_hwmon_get_fan1_min(struct device *dev,
  2523. struct device_attribute *attr,
  2524. char *buf)
  2525. {
  2526. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2527. u32 min_rpm = 0;
  2528. int r;
  2529. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MIN_FAN_RPM,
  2530. (void *)&min_rpm);
  2531. if (r)
  2532. return r;
  2533. return sysfs_emit(buf, "%d\n", min_rpm);
  2534. }
  2535. static ssize_t amdgpu_hwmon_get_fan1_max(struct device *dev,
  2536. struct device_attribute *attr,
  2537. char *buf)
  2538. {
  2539. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2540. u32 max_rpm = 0;
  2541. int r;
  2542. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_MAX_FAN_RPM,
  2543. (void *)&max_rpm);
  2544. if (r)
  2545. return r;
  2546. return sysfs_emit(buf, "%d\n", max_rpm);
  2547. }
  2548. static ssize_t amdgpu_hwmon_get_fan1_target(struct device *dev,
  2549. struct device_attribute *attr,
  2550. char *buf)
  2551. {
  2552. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2553. int err;
  2554. u32 rpm = 0;
  2555. if (amdgpu_in_reset(adev))
  2556. return -EPERM;
  2557. if (adev->in_suspend && !adev->in_runpm)
  2558. return -EPERM;
  2559. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2560. if (err < 0) {
  2561. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2562. return err;
  2563. }
  2564. err = amdgpu_dpm_get_fan_speed_rpm(adev, &rpm);
  2565. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2566. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2567. if (err)
  2568. return err;
  2569. return sysfs_emit(buf, "%i\n", rpm);
  2570. }
  2571. static ssize_t amdgpu_hwmon_set_fan1_target(struct device *dev,
  2572. struct device_attribute *attr,
  2573. const char *buf, size_t count)
  2574. {
  2575. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2576. int err;
  2577. u32 value;
  2578. u32 pwm_mode;
  2579. if (amdgpu_in_reset(adev))
  2580. return -EPERM;
  2581. if (adev->in_suspend && !adev->in_runpm)
  2582. return -EPERM;
  2583. err = kstrtou32(buf, 10, &value);
  2584. if (err)
  2585. return err;
  2586. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2587. if (err < 0) {
  2588. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2589. return err;
  2590. }
  2591. err = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
  2592. if (err)
  2593. goto out;
  2594. if (pwm_mode != AMD_FAN_CTRL_MANUAL) {
  2595. err = -ENODATA;
  2596. goto out;
  2597. }
  2598. err = amdgpu_dpm_set_fan_speed_rpm(adev, value);
  2599. out:
  2600. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2601. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2602. if (err)
  2603. return err;
  2604. return count;
  2605. }
  2606. static ssize_t amdgpu_hwmon_get_fan1_enable(struct device *dev,
  2607. struct device_attribute *attr,
  2608. char *buf)
  2609. {
  2610. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2611. u32 pwm_mode = 0;
  2612. int ret;
  2613. if (amdgpu_in_reset(adev))
  2614. return -EPERM;
  2615. if (adev->in_suspend && !adev->in_runpm)
  2616. return -EPERM;
  2617. ret = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2618. if (ret < 0) {
  2619. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2620. return ret;
  2621. }
  2622. ret = amdgpu_dpm_get_fan_control_mode(adev, &pwm_mode);
  2623. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2624. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2625. if (ret)
  2626. return -EINVAL;
  2627. return sysfs_emit(buf, "%i\n", pwm_mode == AMD_FAN_CTRL_AUTO ? 0 : 1);
  2628. }
  2629. static ssize_t amdgpu_hwmon_set_fan1_enable(struct device *dev,
  2630. struct device_attribute *attr,
  2631. const char *buf,
  2632. size_t count)
  2633. {
  2634. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2635. int err;
  2636. int value;
  2637. u32 pwm_mode;
  2638. if (amdgpu_in_reset(adev))
  2639. return -EPERM;
  2640. if (adev->in_suspend && !adev->in_runpm)
  2641. return -EPERM;
  2642. err = kstrtoint(buf, 10, &value);
  2643. if (err)
  2644. return err;
  2645. if (value == 0)
  2646. pwm_mode = AMD_FAN_CTRL_AUTO;
  2647. else if (value == 1)
  2648. pwm_mode = AMD_FAN_CTRL_MANUAL;
  2649. else
  2650. return -EINVAL;
  2651. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2652. if (err < 0) {
  2653. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2654. return err;
  2655. }
  2656. err = amdgpu_dpm_set_fan_control_mode(adev, pwm_mode);
  2657. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2658. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2659. if (err)
  2660. return -EINVAL;
  2661. return count;
  2662. }
  2663. static ssize_t amdgpu_hwmon_show_vddgfx(struct device *dev,
  2664. struct device_attribute *attr,
  2665. char *buf)
  2666. {
  2667. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2668. u32 vddgfx;
  2669. int r;
  2670. /* get the voltage */
  2671. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDGFX,
  2672. (void *)&vddgfx);
  2673. if (r)
  2674. return r;
  2675. return sysfs_emit(buf, "%d\n", vddgfx);
  2676. }
  2677. static ssize_t amdgpu_hwmon_show_vddgfx_label(struct device *dev,
  2678. struct device_attribute *attr,
  2679. char *buf)
  2680. {
  2681. return sysfs_emit(buf, "vddgfx\n");
  2682. }
  2683. static ssize_t amdgpu_hwmon_show_vddnb(struct device *dev,
  2684. struct device_attribute *attr,
  2685. char *buf)
  2686. {
  2687. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2688. u32 vddnb;
  2689. int r;
  2690. /* only APUs have vddnb */
  2691. if (!(adev->flags & AMD_IS_APU))
  2692. return -EINVAL;
  2693. /* get the voltage */
  2694. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_VDDNB,
  2695. (void *)&vddnb);
  2696. if (r)
  2697. return r;
  2698. return sysfs_emit(buf, "%d\n", vddnb);
  2699. }
  2700. static ssize_t amdgpu_hwmon_show_vddnb_label(struct device *dev,
  2701. struct device_attribute *attr,
  2702. char *buf)
  2703. {
  2704. return sysfs_emit(buf, "vddnb\n");
  2705. }
  2706. static int amdgpu_hwmon_get_power(struct device *dev,
  2707. enum amd_pp_sensors sensor)
  2708. {
  2709. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2710. unsigned int uw;
  2711. u32 query = 0;
  2712. int r;
  2713. r = amdgpu_hwmon_get_sensor_generic(adev, sensor, (void *)&query);
  2714. if (r)
  2715. return r;
  2716. /* convert to microwatts */
  2717. uw = (query >> 8) * 1000000 + (query & 0xff) * 1000;
  2718. return uw;
  2719. }
  2720. static ssize_t amdgpu_hwmon_show_power_avg(struct device *dev,
  2721. struct device_attribute *attr,
  2722. char *buf)
  2723. {
  2724. ssize_t val;
  2725. val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_AVG_POWER);
  2726. if (val < 0)
  2727. return val;
  2728. return sysfs_emit(buf, "%zd\n", val);
  2729. }
  2730. static ssize_t amdgpu_hwmon_show_power_input(struct device *dev,
  2731. struct device_attribute *attr,
  2732. char *buf)
  2733. {
  2734. ssize_t val;
  2735. val = amdgpu_hwmon_get_power(dev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER);
  2736. if (val < 0)
  2737. return val;
  2738. return sysfs_emit(buf, "%zd\n", val);
  2739. }
  2740. static ssize_t amdgpu_hwmon_show_power_cap_generic(struct device *dev,
  2741. struct device_attribute *attr,
  2742. char *buf,
  2743. enum pp_power_limit_level pp_limit_level)
  2744. {
  2745. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2746. enum pp_power_type power_type = to_sensor_dev_attr(attr)->index;
  2747. uint32_t limit;
  2748. ssize_t size;
  2749. int r;
  2750. if (amdgpu_in_reset(adev))
  2751. return -EPERM;
  2752. if (adev->in_suspend && !adev->in_runpm)
  2753. return -EPERM;
  2754. r = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2755. if (r < 0) {
  2756. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2757. return r;
  2758. }
  2759. r = amdgpu_dpm_get_power_limit(adev, &limit,
  2760. pp_limit_level, power_type);
  2761. if (!r)
  2762. size = sysfs_emit(buf, "%u\n", limit * 1000000);
  2763. else
  2764. size = sysfs_emit(buf, "\n");
  2765. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2766. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2767. return size;
  2768. }
  2769. static ssize_t amdgpu_hwmon_show_power_cap_min(struct device *dev,
  2770. struct device_attribute *attr,
  2771. char *buf)
  2772. {
  2773. return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MIN);
  2774. }
  2775. static ssize_t amdgpu_hwmon_show_power_cap_max(struct device *dev,
  2776. struct device_attribute *attr,
  2777. char *buf)
  2778. {
  2779. return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_MAX);
  2780. }
  2781. static ssize_t amdgpu_hwmon_show_power_cap(struct device *dev,
  2782. struct device_attribute *attr,
  2783. char *buf)
  2784. {
  2785. return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_CURRENT);
  2786. }
  2787. static ssize_t amdgpu_hwmon_show_power_cap_default(struct device *dev,
  2788. struct device_attribute *attr,
  2789. char *buf)
  2790. {
  2791. return amdgpu_hwmon_show_power_cap_generic(dev, attr, buf, PP_PWR_LIMIT_DEFAULT);
  2792. }
  2793. static ssize_t amdgpu_hwmon_show_power_label(struct device *dev,
  2794. struct device_attribute *attr,
  2795. char *buf)
  2796. {
  2797. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2798. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  2799. if (gc_ver == IP_VERSION(10, 3, 1))
  2800. return sysfs_emit(buf, "%s\n",
  2801. to_sensor_dev_attr(attr)->index == PP_PWR_TYPE_FAST ?
  2802. "fastPPT" : "slowPPT");
  2803. else
  2804. return sysfs_emit(buf, "PPT\n");
  2805. }
  2806. static ssize_t amdgpu_hwmon_set_power_cap(struct device *dev,
  2807. struct device_attribute *attr,
  2808. const char *buf,
  2809. size_t count)
  2810. {
  2811. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2812. int limit_type = to_sensor_dev_attr(attr)->index;
  2813. int err;
  2814. u32 value;
  2815. if (amdgpu_in_reset(adev))
  2816. return -EPERM;
  2817. if (adev->in_suspend && !adev->in_runpm)
  2818. return -EPERM;
  2819. if (amdgpu_sriov_vf(adev))
  2820. return -EINVAL;
  2821. err = kstrtou32(buf, 10, &value);
  2822. if (err)
  2823. return err;
  2824. value = value / 1000000; /* convert to Watt */
  2825. value |= limit_type << 24;
  2826. err = pm_runtime_get_sync(adev_to_drm(adev)->dev);
  2827. if (err < 0) {
  2828. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2829. return err;
  2830. }
  2831. err = amdgpu_dpm_set_power_limit(adev, value);
  2832. pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
  2833. pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
  2834. if (err)
  2835. return err;
  2836. return count;
  2837. }
  2838. static ssize_t amdgpu_hwmon_show_sclk(struct device *dev,
  2839. struct device_attribute *attr,
  2840. char *buf)
  2841. {
  2842. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2843. uint32_t sclk;
  2844. int r;
  2845. /* get the sclk */
  2846. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_SCLK,
  2847. (void *)&sclk);
  2848. if (r)
  2849. return r;
  2850. return sysfs_emit(buf, "%u\n", sclk * 10 * 1000);
  2851. }
  2852. static ssize_t amdgpu_hwmon_show_sclk_label(struct device *dev,
  2853. struct device_attribute *attr,
  2854. char *buf)
  2855. {
  2856. return sysfs_emit(buf, "sclk\n");
  2857. }
  2858. static ssize_t amdgpu_hwmon_show_mclk(struct device *dev,
  2859. struct device_attribute *attr,
  2860. char *buf)
  2861. {
  2862. struct amdgpu_device *adev = dev_get_drvdata(dev);
  2863. uint32_t mclk;
  2864. int r;
  2865. /* get the sclk */
  2866. r = amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GFX_MCLK,
  2867. (void *)&mclk);
  2868. if (r)
  2869. return r;
  2870. return sysfs_emit(buf, "%u\n", mclk * 10 * 1000);
  2871. }
  2872. static ssize_t amdgpu_hwmon_show_mclk_label(struct device *dev,
  2873. struct device_attribute *attr,
  2874. char *buf)
  2875. {
  2876. return sysfs_emit(buf, "mclk\n");
  2877. }
  2878. /**
  2879. * DOC: hwmon
  2880. *
  2881. * The amdgpu driver exposes the following sensor interfaces:
  2882. *
  2883. * - GPU temperature (via the on-die sensor)
  2884. *
  2885. * - GPU voltage
  2886. *
  2887. * - Northbridge voltage (APUs only)
  2888. *
  2889. * - GPU power
  2890. *
  2891. * - GPU fan
  2892. *
  2893. * - GPU gfx/compute engine clock
  2894. *
  2895. * - GPU memory clock (dGPU only)
  2896. *
  2897. * hwmon interfaces for GPU temperature:
  2898. *
  2899. * - temp[1-3]_input: the on die GPU temperature in millidegrees Celsius
  2900. * - temp2_input and temp3_input are supported on SOC15 dGPUs only
  2901. *
  2902. * - temp[1-3]_label: temperature channel label
  2903. * - temp2_label and temp3_label are supported on SOC15 dGPUs only
  2904. *
  2905. * - temp[1-3]_crit: temperature critical max value in millidegrees Celsius
  2906. * - temp2_crit and temp3_crit are supported on SOC15 dGPUs only
  2907. *
  2908. * - temp[1-3]_crit_hyst: temperature hysteresis for critical limit in millidegrees Celsius
  2909. * - temp2_crit_hyst and temp3_crit_hyst are supported on SOC15 dGPUs only
  2910. *
  2911. * - temp[1-3]_emergency: temperature emergency max value(asic shutdown) in millidegrees Celsius
  2912. * - these are supported on SOC15 dGPUs only
  2913. *
  2914. * hwmon interfaces for GPU voltage:
  2915. *
  2916. * - in0_input: the voltage on the GPU in millivolts
  2917. *
  2918. * - in1_input: the voltage on the Northbridge in millivolts
  2919. *
  2920. * hwmon interfaces for GPU power:
  2921. *
  2922. * - power1_average: average power used by the SoC in microWatts. On APUs this includes the CPU.
  2923. *
  2924. * - power1_input: instantaneous power used by the SoC in microWatts. On APUs this includes the CPU.
  2925. *
  2926. * - power1_cap_min: minimum cap supported in microWatts
  2927. *
  2928. * - power1_cap_max: maximum cap supported in microWatts
  2929. *
  2930. * - power1_cap: selected power cap in microWatts
  2931. *
  2932. * hwmon interfaces for GPU fan:
  2933. *
  2934. * - pwm1: pulse width modulation fan level (0-255)
  2935. *
  2936. * - pwm1_enable: pulse width modulation fan control method (0: no fan speed control, 1: manual fan speed control using pwm interface, 2: automatic fan speed control)
  2937. *
  2938. * - pwm1_min: pulse width modulation fan control minimum level (0)
  2939. *
  2940. * - pwm1_max: pulse width modulation fan control maximum level (255)
  2941. *
  2942. * - fan1_min: a minimum value Unit: revolution/min (RPM)
  2943. *
  2944. * - fan1_max: a maximum value Unit: revolution/max (RPM)
  2945. *
  2946. * - fan1_input: fan speed in RPM
  2947. *
  2948. * - fan[1-\*]_target: Desired fan speed Unit: revolution/min (RPM)
  2949. *
  2950. * - fan[1-\*]_enable: Enable or disable the sensors.1: Enable 0: Disable
  2951. *
  2952. * NOTE: DO NOT set the fan speed via "pwm1" and "fan[1-\*]_target" interfaces at the same time.
  2953. * That will get the former one overridden.
  2954. *
  2955. * hwmon interfaces for GPU clocks:
  2956. *
  2957. * - freq1_input: the gfx/compute clock in hertz
  2958. *
  2959. * - freq2_input: the memory clock in hertz
  2960. *
  2961. * You can use hwmon tools like sensors to view this information on your system.
  2962. *
  2963. */
  2964. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_EDGE);
  2965. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  2966. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  2967. static SENSOR_DEVICE_ATTR(temp1_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_EDGE);
  2968. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_JUNCTION);
  2969. static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 0);
  2970. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, amdgpu_hwmon_show_hotspot_temp_thresh, NULL, 1);
  2971. static SENSOR_DEVICE_ATTR(temp2_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_JUNCTION);
  2972. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, PP_TEMP_MEM);
  2973. static SENSOR_DEVICE_ATTR(temp3_crit, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 0);
  2974. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, amdgpu_hwmon_show_mem_temp_thresh, NULL, 1);
  2975. static SENSOR_DEVICE_ATTR(temp3_emergency, S_IRUGO, amdgpu_hwmon_show_temp_emergency, NULL, PP_TEMP_MEM);
  2976. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_EDGE);
  2977. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_JUNCTION);
  2978. static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, amdgpu_hwmon_show_temp_label, NULL, PP_TEMP_MEM);
  2979. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  2980. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  2981. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  2982. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  2983. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
  2984. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, amdgpu_hwmon_get_fan1_min, NULL, 0);
  2985. static SENSOR_DEVICE_ATTR(fan1_max, S_IRUGO, amdgpu_hwmon_get_fan1_max, NULL, 0);
  2986. static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_target, amdgpu_hwmon_set_fan1_target, 0);
  2987. static SENSOR_DEVICE_ATTR(fan1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_fan1_enable, amdgpu_hwmon_set_fan1_enable, 0);
  2988. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, amdgpu_hwmon_show_vddgfx, NULL, 0);
  2989. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, amdgpu_hwmon_show_vddgfx_label, NULL, 0);
  2990. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, amdgpu_hwmon_show_vddnb, NULL, 0);
  2991. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, amdgpu_hwmon_show_vddnb_label, NULL, 0);
  2992. static SENSOR_DEVICE_ATTR(power1_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 0);
  2993. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, amdgpu_hwmon_show_power_input, NULL, 0);
  2994. static SENSOR_DEVICE_ATTR(power1_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 0);
  2995. static SENSOR_DEVICE_ATTR(power1_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 0);
  2996. static SENSOR_DEVICE_ATTR(power1_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 0);
  2997. static SENSOR_DEVICE_ATTR(power1_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 0);
  2998. static SENSOR_DEVICE_ATTR(power1_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 0);
  2999. static SENSOR_DEVICE_ATTR(power2_average, S_IRUGO, amdgpu_hwmon_show_power_avg, NULL, 1);
  3000. static SENSOR_DEVICE_ATTR(power2_cap_max, S_IRUGO, amdgpu_hwmon_show_power_cap_max, NULL, 1);
  3001. static SENSOR_DEVICE_ATTR(power2_cap_min, S_IRUGO, amdgpu_hwmon_show_power_cap_min, NULL, 1);
  3002. static SENSOR_DEVICE_ATTR(power2_cap, S_IRUGO | S_IWUSR, amdgpu_hwmon_show_power_cap, amdgpu_hwmon_set_power_cap, 1);
  3003. static SENSOR_DEVICE_ATTR(power2_cap_default, S_IRUGO, amdgpu_hwmon_show_power_cap_default, NULL, 1);
  3004. static SENSOR_DEVICE_ATTR(power2_label, S_IRUGO, amdgpu_hwmon_show_power_label, NULL, 1);
  3005. static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, amdgpu_hwmon_show_sclk, NULL, 0);
  3006. static SENSOR_DEVICE_ATTR(freq1_label, S_IRUGO, amdgpu_hwmon_show_sclk_label, NULL, 0);
  3007. static SENSOR_DEVICE_ATTR(freq2_input, S_IRUGO, amdgpu_hwmon_show_mclk, NULL, 0);
  3008. static SENSOR_DEVICE_ATTR(freq2_label, S_IRUGO, amdgpu_hwmon_show_mclk_label, NULL, 0);
  3009. static struct attribute *hwmon_attributes[] = {
  3010. &sensor_dev_attr_temp1_input.dev_attr.attr,
  3011. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  3012. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  3013. &sensor_dev_attr_temp2_input.dev_attr.attr,
  3014. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  3015. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  3016. &sensor_dev_attr_temp3_input.dev_attr.attr,
  3017. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  3018. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  3019. &sensor_dev_attr_temp1_emergency.dev_attr.attr,
  3020. &sensor_dev_attr_temp2_emergency.dev_attr.attr,
  3021. &sensor_dev_attr_temp3_emergency.dev_attr.attr,
  3022. &sensor_dev_attr_temp1_label.dev_attr.attr,
  3023. &sensor_dev_attr_temp2_label.dev_attr.attr,
  3024. &sensor_dev_attr_temp3_label.dev_attr.attr,
  3025. &sensor_dev_attr_pwm1.dev_attr.attr,
  3026. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  3027. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  3028. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  3029. &sensor_dev_attr_fan1_input.dev_attr.attr,
  3030. &sensor_dev_attr_fan1_min.dev_attr.attr,
  3031. &sensor_dev_attr_fan1_max.dev_attr.attr,
  3032. &sensor_dev_attr_fan1_target.dev_attr.attr,
  3033. &sensor_dev_attr_fan1_enable.dev_attr.attr,
  3034. &sensor_dev_attr_in0_input.dev_attr.attr,
  3035. &sensor_dev_attr_in0_label.dev_attr.attr,
  3036. &sensor_dev_attr_in1_input.dev_attr.attr,
  3037. &sensor_dev_attr_in1_label.dev_attr.attr,
  3038. &sensor_dev_attr_power1_average.dev_attr.attr,
  3039. &sensor_dev_attr_power1_input.dev_attr.attr,
  3040. &sensor_dev_attr_power1_cap_max.dev_attr.attr,
  3041. &sensor_dev_attr_power1_cap_min.dev_attr.attr,
  3042. &sensor_dev_attr_power1_cap.dev_attr.attr,
  3043. &sensor_dev_attr_power1_cap_default.dev_attr.attr,
  3044. &sensor_dev_attr_power1_label.dev_attr.attr,
  3045. &sensor_dev_attr_power2_average.dev_attr.attr,
  3046. &sensor_dev_attr_power2_cap_max.dev_attr.attr,
  3047. &sensor_dev_attr_power2_cap_min.dev_attr.attr,
  3048. &sensor_dev_attr_power2_cap.dev_attr.attr,
  3049. &sensor_dev_attr_power2_cap_default.dev_attr.attr,
  3050. &sensor_dev_attr_power2_label.dev_attr.attr,
  3051. &sensor_dev_attr_freq1_input.dev_attr.attr,
  3052. &sensor_dev_attr_freq1_label.dev_attr.attr,
  3053. &sensor_dev_attr_freq2_input.dev_attr.attr,
  3054. &sensor_dev_attr_freq2_label.dev_attr.attr,
  3055. NULL
  3056. };
  3057. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  3058. struct attribute *attr, int index)
  3059. {
  3060. struct device *dev = kobj_to_dev(kobj);
  3061. struct amdgpu_device *adev = dev_get_drvdata(dev);
  3062. umode_t effective_mode = attr->mode;
  3063. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  3064. uint32_t tmp;
  3065. /* under pp one vf mode manage of hwmon attributes is not supported */
  3066. if (amdgpu_sriov_is_pp_one_vf(adev))
  3067. effective_mode &= ~S_IWUSR;
  3068. /* Skip fan attributes if fan is not present */
  3069. if (adev->pm.no_fan && (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  3070. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  3071. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  3072. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
  3073. attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
  3074. attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
  3075. attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
  3076. attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
  3077. attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
  3078. return 0;
  3079. /* Skip fan attributes on APU */
  3080. if ((adev->flags & AMD_IS_APU) &&
  3081. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  3082. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  3083. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  3084. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
  3085. attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
  3086. attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
  3087. attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
  3088. attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
  3089. attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
  3090. return 0;
  3091. /* Skip crit temp on APU */
  3092. if ((((adev->flags & AMD_IS_APU) && (adev->family >= AMDGPU_FAMILY_CZ)) ||
  3093. (gc_ver == IP_VERSION(9, 4, 3) || gc_ver == IP_VERSION(9, 4, 4))) &&
  3094. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  3095. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
  3096. return 0;
  3097. /* Skip limit attributes if DPM is not enabled */
  3098. if (!adev->pm.dpm_enabled &&
  3099. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  3100. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  3101. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  3102. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  3103. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  3104. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
  3105. attr == &sensor_dev_attr_fan1_input.dev_attr.attr ||
  3106. attr == &sensor_dev_attr_fan1_min.dev_attr.attr ||
  3107. attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
  3108. attr == &sensor_dev_attr_fan1_target.dev_attr.attr ||
  3109. attr == &sensor_dev_attr_fan1_enable.dev_attr.attr))
  3110. return 0;
  3111. /* mask fan attributes if we have no bindings for this asic to expose */
  3112. if (((amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
  3113. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  3114. ((amdgpu_dpm_get_fan_control_mode(adev, NULL) == -EOPNOTSUPP) &&
  3115. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  3116. effective_mode &= ~S_IRUGO;
  3117. if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
  3118. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  3119. ((amdgpu_dpm_set_fan_control_mode(adev, U32_MAX) == -EOPNOTSUPP) &&
  3120. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  3121. effective_mode &= ~S_IWUSR;
  3122. /* not implemented yet for APUs other than GC 10.3.1 (vangogh) and 9.4.3 */
  3123. if (attr == &sensor_dev_attr_power1_cap_max.dev_attr.attr ||
  3124. attr == &sensor_dev_attr_power1_cap_min.dev_attr.attr ||
  3125. attr == &sensor_dev_attr_power1_cap.dev_attr.attr ||
  3126. attr == &sensor_dev_attr_power1_cap_default.dev_attr.attr) {
  3127. if (adev->family == AMDGPU_FAMILY_SI ||
  3128. ((adev->flags & AMD_IS_APU) && gc_ver != IP_VERSION(10, 3, 1) &&
  3129. (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4))) ||
  3130. (amdgpu_sriov_vf(adev) && gc_ver == IP_VERSION(11, 0, 3)))
  3131. return 0;
  3132. }
  3133. /* not implemented yet for APUs having < GC 9.3.0 (Renoir) */
  3134. if (((adev->family == AMDGPU_FAMILY_SI) ||
  3135. ((adev->flags & AMD_IS_APU) && (gc_ver < IP_VERSION(9, 3, 0)))) &&
  3136. (attr == &sensor_dev_attr_power1_average.dev_attr.attr))
  3137. return 0;
  3138. /* not all products support both average and instantaneous */
  3139. if (attr == &sensor_dev_attr_power1_average.dev_attr.attr &&
  3140. amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&tmp) == -EOPNOTSUPP)
  3141. return 0;
  3142. if (attr == &sensor_dev_attr_power1_input.dev_attr.attr &&
  3143. amdgpu_hwmon_get_sensor_generic(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&tmp) == -EOPNOTSUPP)
  3144. return 0;
  3145. /* hide max/min values if we can't both query and manage the fan */
  3146. if (((amdgpu_dpm_set_fan_speed_pwm(adev, U32_MAX) == -EOPNOTSUPP) &&
  3147. (amdgpu_dpm_get_fan_speed_pwm(adev, NULL) == -EOPNOTSUPP) &&
  3148. (amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
  3149. (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP)) &&
  3150. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  3151. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  3152. return 0;
  3153. if ((amdgpu_dpm_set_fan_speed_rpm(adev, U32_MAX) == -EOPNOTSUPP) &&
  3154. (amdgpu_dpm_get_fan_speed_rpm(adev, NULL) == -EOPNOTSUPP) &&
  3155. (attr == &sensor_dev_attr_fan1_max.dev_attr.attr ||
  3156. attr == &sensor_dev_attr_fan1_min.dev_attr.attr))
  3157. return 0;
  3158. if ((adev->family == AMDGPU_FAMILY_SI || /* not implemented yet */
  3159. adev->family == AMDGPU_FAMILY_KV || /* not implemented yet */
  3160. (gc_ver == IP_VERSION(9, 4, 3) ||
  3161. gc_ver == IP_VERSION(9, 4, 4))) &&
  3162. (attr == &sensor_dev_attr_in0_input.dev_attr.attr ||
  3163. attr == &sensor_dev_attr_in0_label.dev_attr.attr))
  3164. return 0;
  3165. /* only APUs other than gc 9,4,3 have vddnb */
  3166. if ((!(adev->flags & AMD_IS_APU) ||
  3167. (gc_ver == IP_VERSION(9, 4, 3) ||
  3168. gc_ver == IP_VERSION(9, 4, 4))) &&
  3169. (attr == &sensor_dev_attr_in1_input.dev_attr.attr ||
  3170. attr == &sensor_dev_attr_in1_label.dev_attr.attr))
  3171. return 0;
  3172. /* no mclk on APUs other than gc 9,4,3*/
  3173. if (((adev->flags & AMD_IS_APU) && (gc_ver != IP_VERSION(9, 4, 3))) &&
  3174. (attr == &sensor_dev_attr_freq2_input.dev_attr.attr ||
  3175. attr == &sensor_dev_attr_freq2_label.dev_attr.attr))
  3176. return 0;
  3177. if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
  3178. (gc_ver != IP_VERSION(9, 4, 3) && gc_ver != IP_VERSION(9, 4, 4)) &&
  3179. (attr == &sensor_dev_attr_temp2_input.dev_attr.attr ||
  3180. attr == &sensor_dev_attr_temp2_label.dev_attr.attr ||
  3181. attr == &sensor_dev_attr_temp2_crit.dev_attr.attr ||
  3182. attr == &sensor_dev_attr_temp3_input.dev_attr.attr ||
  3183. attr == &sensor_dev_attr_temp3_label.dev_attr.attr ||
  3184. attr == &sensor_dev_attr_temp3_crit.dev_attr.attr))
  3185. return 0;
  3186. /* hotspot temperature for gc 9,4,3*/
  3187. if (gc_ver == IP_VERSION(9, 4, 3) ||
  3188. gc_ver == IP_VERSION(9, 4, 4)) {
  3189. if (attr == &sensor_dev_attr_temp1_input.dev_attr.attr ||
  3190. attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
  3191. attr == &sensor_dev_attr_temp1_label.dev_attr.attr)
  3192. return 0;
  3193. if (attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
  3194. attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr)
  3195. return attr->mode;
  3196. }
  3197. /* only SOC15 dGPUs support hotspot and mem temperatures */
  3198. if (((adev->flags & AMD_IS_APU) || gc_ver < IP_VERSION(9, 0, 0)) &&
  3199. (attr == &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr ||
  3200. attr == &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr ||
  3201. attr == &sensor_dev_attr_temp1_emergency.dev_attr.attr ||
  3202. attr == &sensor_dev_attr_temp2_emergency.dev_attr.attr ||
  3203. attr == &sensor_dev_attr_temp3_emergency.dev_attr.attr))
  3204. return 0;
  3205. /* only Vangogh has fast PPT limit and power labels */
  3206. if (!(gc_ver == IP_VERSION(10, 3, 1)) &&
  3207. (attr == &sensor_dev_attr_power2_average.dev_attr.attr ||
  3208. attr == &sensor_dev_attr_power2_cap_max.dev_attr.attr ||
  3209. attr == &sensor_dev_attr_power2_cap_min.dev_attr.attr ||
  3210. attr == &sensor_dev_attr_power2_cap.dev_attr.attr ||
  3211. attr == &sensor_dev_attr_power2_cap_default.dev_attr.attr ||
  3212. attr == &sensor_dev_attr_power2_label.dev_attr.attr))
  3213. return 0;
  3214. return effective_mode;
  3215. }
  3216. static const struct attribute_group hwmon_attrgroup = {
  3217. .attrs = hwmon_attributes,
  3218. .is_visible = hwmon_attributes_visible,
  3219. };
  3220. static const struct attribute_group *hwmon_groups[] = {
  3221. &hwmon_attrgroup,
  3222. NULL
  3223. };
  3224. static int amdgpu_retrieve_od_settings(struct amdgpu_device *adev,
  3225. enum pp_clock_type od_type,
  3226. char *buf)
  3227. {
  3228. int size = 0;
  3229. int ret;
  3230. if (amdgpu_in_reset(adev))
  3231. return -EPERM;
  3232. if (adev->in_suspend && !adev->in_runpm)
  3233. return -EPERM;
  3234. ret = pm_runtime_get_sync(adev->dev);
  3235. if (ret < 0) {
  3236. pm_runtime_put_autosuspend(adev->dev);
  3237. return ret;
  3238. }
  3239. size = amdgpu_dpm_print_clock_levels(adev, od_type, buf);
  3240. if (size == 0)
  3241. size = sysfs_emit(buf, "\n");
  3242. pm_runtime_mark_last_busy(adev->dev);
  3243. pm_runtime_put_autosuspend(adev->dev);
  3244. return size;
  3245. }
  3246. static int parse_input_od_command_lines(const char *buf,
  3247. size_t count,
  3248. u32 *type,
  3249. long *params,
  3250. uint32_t *num_of_params)
  3251. {
  3252. const char delimiter[3] = {' ', '\n', '\0'};
  3253. uint32_t parameter_size = 0;
  3254. char buf_cpy[128] = {0};
  3255. char *tmp_str, *sub_str;
  3256. int ret;
  3257. if (count > sizeof(buf_cpy) - 1)
  3258. return -EINVAL;
  3259. memcpy(buf_cpy, buf, count);
  3260. tmp_str = buf_cpy;
  3261. /* skip heading spaces */
  3262. while (isspace(*tmp_str))
  3263. tmp_str++;
  3264. switch (*tmp_str) {
  3265. case 'c':
  3266. *type = PP_OD_COMMIT_DPM_TABLE;
  3267. return 0;
  3268. case 'r':
  3269. params[parameter_size] = *type;
  3270. *num_of_params = 1;
  3271. *type = PP_OD_RESTORE_DEFAULT_TABLE;
  3272. return 0;
  3273. default:
  3274. break;
  3275. }
  3276. while ((sub_str = strsep(&tmp_str, delimiter)) != NULL) {
  3277. if (strlen(sub_str) == 0)
  3278. continue;
  3279. ret = kstrtol(sub_str, 0, &params[parameter_size]);
  3280. if (ret)
  3281. return -EINVAL;
  3282. parameter_size++;
  3283. if (!tmp_str)
  3284. break;
  3285. while (isspace(*tmp_str))
  3286. tmp_str++;
  3287. }
  3288. *num_of_params = parameter_size;
  3289. return 0;
  3290. }
  3291. static int
  3292. amdgpu_distribute_custom_od_settings(struct amdgpu_device *adev,
  3293. enum PP_OD_DPM_TABLE_COMMAND cmd_type,
  3294. const char *in_buf,
  3295. size_t count)
  3296. {
  3297. uint32_t parameter_size = 0;
  3298. long parameter[64];
  3299. int ret;
  3300. if (amdgpu_in_reset(adev))
  3301. return -EPERM;
  3302. if (adev->in_suspend && !adev->in_runpm)
  3303. return -EPERM;
  3304. ret = parse_input_od_command_lines(in_buf,
  3305. count,
  3306. &cmd_type,
  3307. parameter,
  3308. &parameter_size);
  3309. if (ret)
  3310. return ret;
  3311. ret = pm_runtime_get_sync(adev->dev);
  3312. if (ret < 0)
  3313. goto err_out0;
  3314. ret = amdgpu_dpm_odn_edit_dpm_table(adev,
  3315. cmd_type,
  3316. parameter,
  3317. parameter_size);
  3318. if (ret)
  3319. goto err_out1;
  3320. if (cmd_type == PP_OD_COMMIT_DPM_TABLE) {
  3321. ret = amdgpu_dpm_dispatch_task(adev,
  3322. AMD_PP_TASK_READJUST_POWER_STATE,
  3323. NULL);
  3324. if (ret)
  3325. goto err_out1;
  3326. }
  3327. pm_runtime_mark_last_busy(adev->dev);
  3328. pm_runtime_put_autosuspend(adev->dev);
  3329. return count;
  3330. err_out1:
  3331. pm_runtime_mark_last_busy(adev->dev);
  3332. err_out0:
  3333. pm_runtime_put_autosuspend(adev->dev);
  3334. return ret;
  3335. }
  3336. /**
  3337. * DOC: fan_curve
  3338. *
  3339. * The amdgpu driver provides a sysfs API for checking and adjusting the fan
  3340. * control curve line.
  3341. *
  3342. * Reading back the file shows you the current settings(temperature in Celsius
  3343. * degree and fan speed in pwm) applied to every anchor point of the curve line
  3344. * and their permitted ranges if changable.
  3345. *
  3346. * Writing a desired string(with the format like "anchor_point_index temperature
  3347. * fan_speed_in_pwm") to the file, change the settings for the specific anchor
  3348. * point accordingly.
  3349. *
  3350. * When you have finished the editing, write "c" (commit) to the file to commit
  3351. * your changes.
  3352. *
  3353. * If you want to reset to the default value, write "r" (reset) to the file to
  3354. * reset them
  3355. *
  3356. * There are two fan control modes supported: auto and manual. With auto mode,
  3357. * PMFW handles the fan speed control(how fan speed reacts to ASIC temperature).
  3358. * While with manual mode, users can set their own fan curve line as what
  3359. * described here. Normally the ASIC is booted up with auto mode. Any
  3360. * settings via this interface will switch the fan control to manual mode
  3361. * implicitly.
  3362. */
  3363. static ssize_t fan_curve_show(struct kobject *kobj,
  3364. struct kobj_attribute *attr,
  3365. char *buf)
  3366. {
  3367. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3368. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3369. return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_CURVE, buf);
  3370. }
  3371. static ssize_t fan_curve_store(struct kobject *kobj,
  3372. struct kobj_attribute *attr,
  3373. const char *buf,
  3374. size_t count)
  3375. {
  3376. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3377. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3378. return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
  3379. PP_OD_EDIT_FAN_CURVE,
  3380. buf,
  3381. count);
  3382. }
  3383. static umode_t fan_curve_visible(struct amdgpu_device *adev)
  3384. {
  3385. umode_t umode = 0000;
  3386. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_RETRIEVE)
  3387. umode |= S_IRUSR | S_IRGRP | S_IROTH;
  3388. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_CURVE_SET)
  3389. umode |= S_IWUSR;
  3390. return umode;
  3391. }
  3392. /**
  3393. * DOC: acoustic_limit_rpm_threshold
  3394. *
  3395. * The amdgpu driver provides a sysfs API for checking and adjusting the
  3396. * acoustic limit in RPM for fan control.
  3397. *
  3398. * Reading back the file shows you the current setting and the permitted
  3399. * ranges if changable.
  3400. *
  3401. * Writing an integer to the file, change the setting accordingly.
  3402. *
  3403. * When you have finished the editing, write "c" (commit) to the file to commit
  3404. * your changes.
  3405. *
  3406. * If you want to reset to the default value, write "r" (reset) to the file to
  3407. * reset them
  3408. *
  3409. * This setting works under auto fan control mode only. It adjusts the PMFW's
  3410. * behavior about the maximum speed in RPM the fan can spin. Setting via this
  3411. * interface will switch the fan control to auto mode implicitly.
  3412. */
  3413. static ssize_t acoustic_limit_threshold_show(struct kobject *kobj,
  3414. struct kobj_attribute *attr,
  3415. char *buf)
  3416. {
  3417. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3418. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3419. return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_LIMIT, buf);
  3420. }
  3421. static ssize_t acoustic_limit_threshold_store(struct kobject *kobj,
  3422. struct kobj_attribute *attr,
  3423. const char *buf,
  3424. size_t count)
  3425. {
  3426. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3427. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3428. return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
  3429. PP_OD_EDIT_ACOUSTIC_LIMIT,
  3430. buf,
  3431. count);
  3432. }
  3433. static umode_t acoustic_limit_threshold_visible(struct amdgpu_device *adev)
  3434. {
  3435. umode_t umode = 0000;
  3436. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_RETRIEVE)
  3437. umode |= S_IRUSR | S_IRGRP | S_IROTH;
  3438. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_LIMIT_THRESHOLD_SET)
  3439. umode |= S_IWUSR;
  3440. return umode;
  3441. }
  3442. /**
  3443. * DOC: acoustic_target_rpm_threshold
  3444. *
  3445. * The amdgpu driver provides a sysfs API for checking and adjusting the
  3446. * acoustic target in RPM for fan control.
  3447. *
  3448. * Reading back the file shows you the current setting and the permitted
  3449. * ranges if changable.
  3450. *
  3451. * Writing an integer to the file, change the setting accordingly.
  3452. *
  3453. * When you have finished the editing, write "c" (commit) to the file to commit
  3454. * your changes.
  3455. *
  3456. * If you want to reset to the default value, write "r" (reset) to the file to
  3457. * reset them
  3458. *
  3459. * This setting works under auto fan control mode only. It can co-exist with
  3460. * other settings which can work also under auto mode. It adjusts the PMFW's
  3461. * behavior about the maximum speed in RPM the fan can spin when ASIC
  3462. * temperature is not greater than target temperature. Setting via this
  3463. * interface will switch the fan control to auto mode implicitly.
  3464. */
  3465. static ssize_t acoustic_target_threshold_show(struct kobject *kobj,
  3466. struct kobj_attribute *attr,
  3467. char *buf)
  3468. {
  3469. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3470. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3471. return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_ACOUSTIC_TARGET, buf);
  3472. }
  3473. static ssize_t acoustic_target_threshold_store(struct kobject *kobj,
  3474. struct kobj_attribute *attr,
  3475. const char *buf,
  3476. size_t count)
  3477. {
  3478. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3479. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3480. return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
  3481. PP_OD_EDIT_ACOUSTIC_TARGET,
  3482. buf,
  3483. count);
  3484. }
  3485. static umode_t acoustic_target_threshold_visible(struct amdgpu_device *adev)
  3486. {
  3487. umode_t umode = 0000;
  3488. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_RETRIEVE)
  3489. umode |= S_IRUSR | S_IRGRP | S_IROTH;
  3490. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_ACOUSTIC_TARGET_THRESHOLD_SET)
  3491. umode |= S_IWUSR;
  3492. return umode;
  3493. }
  3494. /**
  3495. * DOC: fan_target_temperature
  3496. *
  3497. * The amdgpu driver provides a sysfs API for checking and adjusting the
  3498. * target tempeature in Celsius degree for fan control.
  3499. *
  3500. * Reading back the file shows you the current setting and the permitted
  3501. * ranges if changable.
  3502. *
  3503. * Writing an integer to the file, change the setting accordingly.
  3504. *
  3505. * When you have finished the editing, write "c" (commit) to the file to commit
  3506. * your changes.
  3507. *
  3508. * If you want to reset to the default value, write "r" (reset) to the file to
  3509. * reset them
  3510. *
  3511. * This setting works under auto fan control mode only. It can co-exist with
  3512. * other settings which can work also under auto mode. Paring with the
  3513. * acoustic_target_rpm_threshold setting, they define the maximum speed in
  3514. * RPM the fan can spin when ASIC temperature is not greater than target
  3515. * temperature. Setting via this interface will switch the fan control to
  3516. * auto mode implicitly.
  3517. */
  3518. static ssize_t fan_target_temperature_show(struct kobject *kobj,
  3519. struct kobj_attribute *attr,
  3520. char *buf)
  3521. {
  3522. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3523. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3524. return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_TARGET_TEMPERATURE, buf);
  3525. }
  3526. static ssize_t fan_target_temperature_store(struct kobject *kobj,
  3527. struct kobj_attribute *attr,
  3528. const char *buf,
  3529. size_t count)
  3530. {
  3531. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3532. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3533. return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
  3534. PP_OD_EDIT_FAN_TARGET_TEMPERATURE,
  3535. buf,
  3536. count);
  3537. }
  3538. static umode_t fan_target_temperature_visible(struct amdgpu_device *adev)
  3539. {
  3540. umode_t umode = 0000;
  3541. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_RETRIEVE)
  3542. umode |= S_IRUSR | S_IRGRP | S_IROTH;
  3543. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_TARGET_TEMPERATURE_SET)
  3544. umode |= S_IWUSR;
  3545. return umode;
  3546. }
  3547. /**
  3548. * DOC: fan_minimum_pwm
  3549. *
  3550. * The amdgpu driver provides a sysfs API for checking and adjusting the
  3551. * minimum fan speed in PWM.
  3552. *
  3553. * Reading back the file shows you the current setting and the permitted
  3554. * ranges if changable.
  3555. *
  3556. * Writing an integer to the file, change the setting accordingly.
  3557. *
  3558. * When you have finished the editing, write "c" (commit) to the file to commit
  3559. * your changes.
  3560. *
  3561. * If you want to reset to the default value, write "r" (reset) to the file to
  3562. * reset them
  3563. *
  3564. * This setting works under auto fan control mode only. It can co-exist with
  3565. * other settings which can work also under auto mode. It adjusts the PMFW's
  3566. * behavior about the minimum fan speed in PWM the fan should spin. Setting
  3567. * via this interface will switch the fan control to auto mode implicitly.
  3568. */
  3569. static ssize_t fan_minimum_pwm_show(struct kobject *kobj,
  3570. struct kobj_attribute *attr,
  3571. char *buf)
  3572. {
  3573. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3574. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3575. return (ssize_t)amdgpu_retrieve_od_settings(adev, OD_FAN_MINIMUM_PWM, buf);
  3576. }
  3577. static ssize_t fan_minimum_pwm_store(struct kobject *kobj,
  3578. struct kobj_attribute *attr,
  3579. const char *buf,
  3580. size_t count)
  3581. {
  3582. struct od_kobj *container = container_of(kobj, struct od_kobj, kobj);
  3583. struct amdgpu_device *adev = (struct amdgpu_device *)container->priv;
  3584. return (ssize_t)amdgpu_distribute_custom_od_settings(adev,
  3585. PP_OD_EDIT_FAN_MINIMUM_PWM,
  3586. buf,
  3587. count);
  3588. }
  3589. static umode_t fan_minimum_pwm_visible(struct amdgpu_device *adev)
  3590. {
  3591. umode_t umode = 0000;
  3592. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE)
  3593. umode |= S_IRUSR | S_IRGRP | S_IROTH;
  3594. if (adev->pm.od_feature_mask & OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET)
  3595. umode |= S_IWUSR;
  3596. return umode;
  3597. }
  3598. static struct od_feature_set amdgpu_od_set = {
  3599. .containers = {
  3600. [0] = {
  3601. .name = "fan_ctrl",
  3602. .sub_feature = {
  3603. [0] = {
  3604. .name = "fan_curve",
  3605. .ops = {
  3606. .is_visible = fan_curve_visible,
  3607. .show = fan_curve_show,
  3608. .store = fan_curve_store,
  3609. },
  3610. },
  3611. [1] = {
  3612. .name = "acoustic_limit_rpm_threshold",
  3613. .ops = {
  3614. .is_visible = acoustic_limit_threshold_visible,
  3615. .show = acoustic_limit_threshold_show,
  3616. .store = acoustic_limit_threshold_store,
  3617. },
  3618. },
  3619. [2] = {
  3620. .name = "acoustic_target_rpm_threshold",
  3621. .ops = {
  3622. .is_visible = acoustic_target_threshold_visible,
  3623. .show = acoustic_target_threshold_show,
  3624. .store = acoustic_target_threshold_store,
  3625. },
  3626. },
  3627. [3] = {
  3628. .name = "fan_target_temperature",
  3629. .ops = {
  3630. .is_visible = fan_target_temperature_visible,
  3631. .show = fan_target_temperature_show,
  3632. .store = fan_target_temperature_store,
  3633. },
  3634. },
  3635. [4] = {
  3636. .name = "fan_minimum_pwm",
  3637. .ops = {
  3638. .is_visible = fan_minimum_pwm_visible,
  3639. .show = fan_minimum_pwm_show,
  3640. .store = fan_minimum_pwm_store,
  3641. },
  3642. },
  3643. },
  3644. },
  3645. },
  3646. };
  3647. static void od_kobj_release(struct kobject *kobj)
  3648. {
  3649. struct od_kobj *od_kobj = container_of(kobj, struct od_kobj, kobj);
  3650. kfree(od_kobj);
  3651. }
  3652. static const struct kobj_type od_ktype = {
  3653. .release = od_kobj_release,
  3654. .sysfs_ops = &kobj_sysfs_ops,
  3655. };
  3656. static void amdgpu_od_set_fini(struct amdgpu_device *adev)
  3657. {
  3658. struct od_kobj *container, *container_next;
  3659. struct od_attribute *attribute, *attribute_next;
  3660. if (list_empty(&adev->pm.od_kobj_list))
  3661. return;
  3662. list_for_each_entry_safe(container, container_next,
  3663. &adev->pm.od_kobj_list, entry) {
  3664. list_del(&container->entry);
  3665. list_for_each_entry_safe(attribute, attribute_next,
  3666. &container->attribute, entry) {
  3667. list_del(&attribute->entry);
  3668. sysfs_remove_file(&container->kobj,
  3669. &attribute->attribute.attr);
  3670. kfree(attribute);
  3671. }
  3672. kobject_put(&container->kobj);
  3673. }
  3674. }
  3675. static bool amdgpu_is_od_feature_supported(struct amdgpu_device *adev,
  3676. struct od_feature_ops *feature_ops)
  3677. {
  3678. umode_t mode;
  3679. if (!feature_ops->is_visible)
  3680. return false;
  3681. /*
  3682. * If the feature has no user read and write mode set,
  3683. * we can assume the feature is actually not supported.(?)
  3684. * And the revelant sysfs interface should not be exposed.
  3685. */
  3686. mode = feature_ops->is_visible(adev);
  3687. if (mode & (S_IRUSR | S_IWUSR))
  3688. return true;
  3689. return false;
  3690. }
  3691. static bool amdgpu_od_is_self_contained(struct amdgpu_device *adev,
  3692. struct od_feature_container *container)
  3693. {
  3694. int i;
  3695. /*
  3696. * If there is no valid entry within the container, the container
  3697. * is recognized as a self contained container. And the valid entry
  3698. * here means it has a valid naming and it is visible/supported by
  3699. * the ASIC.
  3700. */
  3701. for (i = 0; i < ARRAY_SIZE(container->sub_feature); i++) {
  3702. if (container->sub_feature[i].name &&
  3703. amdgpu_is_od_feature_supported(adev,
  3704. &container->sub_feature[i].ops))
  3705. return false;
  3706. }
  3707. return true;
  3708. }
  3709. static int amdgpu_od_set_init(struct amdgpu_device *adev)
  3710. {
  3711. struct od_kobj *top_set, *sub_set;
  3712. struct od_attribute *attribute;
  3713. struct od_feature_container *container;
  3714. struct od_feature_item *feature;
  3715. int i, j;
  3716. int ret;
  3717. /* Setup the top `gpu_od` directory which holds all other OD interfaces */
  3718. top_set = kzalloc(sizeof(*top_set), GFP_KERNEL);
  3719. if (!top_set)
  3720. return -ENOMEM;
  3721. list_add(&top_set->entry, &adev->pm.od_kobj_list);
  3722. ret = kobject_init_and_add(&top_set->kobj,
  3723. &od_ktype,
  3724. &adev->dev->kobj,
  3725. "%s",
  3726. "gpu_od");
  3727. if (ret)
  3728. goto err_out;
  3729. INIT_LIST_HEAD(&top_set->attribute);
  3730. top_set->priv = adev;
  3731. for (i = 0; i < ARRAY_SIZE(amdgpu_od_set.containers); i++) {
  3732. container = &amdgpu_od_set.containers[i];
  3733. if (!container->name)
  3734. continue;
  3735. /*
  3736. * If there is valid entries within the container, the container
  3737. * will be presented as a sub directory and all its holding entries
  3738. * will be presented as plain files under it.
  3739. * While if there is no valid entry within the container, the container
  3740. * itself will be presented as a plain file under top `gpu_od` directory.
  3741. */
  3742. if (amdgpu_od_is_self_contained(adev, container)) {
  3743. if (!amdgpu_is_od_feature_supported(adev,
  3744. &container->ops))
  3745. continue;
  3746. /*
  3747. * The container is presented as a plain file under top `gpu_od`
  3748. * directory.
  3749. */
  3750. attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
  3751. if (!attribute) {
  3752. ret = -ENOMEM;
  3753. goto err_out;
  3754. }
  3755. list_add(&attribute->entry, &top_set->attribute);
  3756. attribute->attribute.attr.mode =
  3757. container->ops.is_visible(adev);
  3758. attribute->attribute.attr.name = container->name;
  3759. attribute->attribute.show =
  3760. container->ops.show;
  3761. attribute->attribute.store =
  3762. container->ops.store;
  3763. ret = sysfs_create_file(&top_set->kobj,
  3764. &attribute->attribute.attr);
  3765. if (ret)
  3766. goto err_out;
  3767. } else {
  3768. /* The container is presented as a sub directory. */
  3769. sub_set = kzalloc(sizeof(*sub_set), GFP_KERNEL);
  3770. if (!sub_set) {
  3771. ret = -ENOMEM;
  3772. goto err_out;
  3773. }
  3774. list_add(&sub_set->entry, &adev->pm.od_kobj_list);
  3775. ret = kobject_init_and_add(&sub_set->kobj,
  3776. &od_ktype,
  3777. &top_set->kobj,
  3778. "%s",
  3779. container->name);
  3780. if (ret)
  3781. goto err_out;
  3782. INIT_LIST_HEAD(&sub_set->attribute);
  3783. sub_set->priv = adev;
  3784. for (j = 0; j < ARRAY_SIZE(container->sub_feature); j++) {
  3785. feature = &container->sub_feature[j];
  3786. if (!feature->name)
  3787. continue;
  3788. if (!amdgpu_is_od_feature_supported(adev,
  3789. &feature->ops))
  3790. continue;
  3791. /*
  3792. * With the container presented as a sub directory, the entry within
  3793. * it is presented as a plain file under the sub directory.
  3794. */
  3795. attribute = kzalloc(sizeof(*attribute), GFP_KERNEL);
  3796. if (!attribute) {
  3797. ret = -ENOMEM;
  3798. goto err_out;
  3799. }
  3800. list_add(&attribute->entry, &sub_set->attribute);
  3801. attribute->attribute.attr.mode =
  3802. feature->ops.is_visible(adev);
  3803. attribute->attribute.attr.name = feature->name;
  3804. attribute->attribute.show =
  3805. feature->ops.show;
  3806. attribute->attribute.store =
  3807. feature->ops.store;
  3808. ret = sysfs_create_file(&sub_set->kobj,
  3809. &attribute->attribute.attr);
  3810. if (ret)
  3811. goto err_out;
  3812. }
  3813. }
  3814. }
  3815. /*
  3816. * If gpu_od is the only member in the list, that means gpu_od is an
  3817. * empty directory, so remove it.
  3818. */
  3819. if (list_is_singular(&adev->pm.od_kobj_list))
  3820. goto err_out;
  3821. return 0;
  3822. err_out:
  3823. amdgpu_od_set_fini(adev);
  3824. return ret;
  3825. }
  3826. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  3827. {
  3828. enum amdgpu_sriov_vf_mode mode;
  3829. uint32_t mask = 0;
  3830. int ret;
  3831. if (adev->pm.sysfs_initialized)
  3832. return 0;
  3833. INIT_LIST_HEAD(&adev->pm.pm_attr_list);
  3834. if (adev->pm.dpm_enabled == 0)
  3835. return 0;
  3836. mode = amdgpu_virt_get_sriov_vf_mode(adev);
  3837. /* under multi-vf mode, the hwmon attributes are all not supported */
  3838. if (mode != SRIOV_VF_MODE_MULTI_VF) {
  3839. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  3840. DRIVER_NAME, adev,
  3841. hwmon_groups);
  3842. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  3843. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  3844. dev_err(adev->dev, "Unable to register hwmon device: %d\n", ret);
  3845. return ret;
  3846. }
  3847. }
  3848. switch (mode) {
  3849. case SRIOV_VF_MODE_ONE_VF:
  3850. mask = ATTR_FLAG_ONEVF;
  3851. break;
  3852. case SRIOV_VF_MODE_MULTI_VF:
  3853. mask = 0;
  3854. break;
  3855. case SRIOV_VF_MODE_BARE_METAL:
  3856. default:
  3857. mask = ATTR_FLAG_MASK_ALL;
  3858. break;
  3859. }
  3860. ret = amdgpu_device_attr_create_groups(adev,
  3861. amdgpu_device_attrs,
  3862. ARRAY_SIZE(amdgpu_device_attrs),
  3863. mask,
  3864. &adev->pm.pm_attr_list);
  3865. if (ret)
  3866. goto err_out0;
  3867. if (amdgpu_dpm_is_overdrive_supported(adev)) {
  3868. ret = amdgpu_od_set_init(adev);
  3869. if (ret)
  3870. goto err_out1;
  3871. } else if (adev->pm.pp_feature & PP_OVERDRIVE_MASK) {
  3872. dev_info(adev->dev, "overdrive feature is not supported\n");
  3873. }
  3874. if (amdgpu_dpm_get_pm_policy_info(adev, PP_PM_POLICY_NONE, NULL) !=
  3875. -EOPNOTSUPP) {
  3876. ret = devm_device_add_group(adev->dev,
  3877. &amdgpu_pm_policy_attr_group);
  3878. if (ret)
  3879. goto err_out0;
  3880. }
  3881. adev->pm.sysfs_initialized = true;
  3882. return 0;
  3883. err_out1:
  3884. amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
  3885. err_out0:
  3886. if (adev->pm.int_hwmon_dev)
  3887. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  3888. return ret;
  3889. }
  3890. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  3891. {
  3892. amdgpu_od_set_fini(adev);
  3893. if (adev->pm.int_hwmon_dev)
  3894. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  3895. amdgpu_device_attr_remove_groups(adev, &adev->pm.pm_attr_list);
  3896. }
  3897. /*
  3898. * Debugfs info
  3899. */
  3900. #if defined(CONFIG_DEBUG_FS)
  3901. static void amdgpu_debugfs_prints_cpu_info(struct seq_file *m,
  3902. struct amdgpu_device *adev)
  3903. {
  3904. uint16_t *p_val;
  3905. uint32_t size;
  3906. int i;
  3907. uint32_t num_cpu_cores = amdgpu_dpm_get_num_cpu_cores(adev);
  3908. if (amdgpu_dpm_is_cclk_dpm_supported(adev)) {
  3909. p_val = kcalloc(num_cpu_cores, sizeof(uint16_t),
  3910. GFP_KERNEL);
  3911. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_CPU_CLK,
  3912. (void *)p_val, &size)) {
  3913. for (i = 0; i < num_cpu_cores; i++)
  3914. seq_printf(m, "\t%u MHz (CPU%d)\n",
  3915. *(p_val + i), i);
  3916. }
  3917. kfree(p_val);
  3918. }
  3919. }
  3920. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  3921. {
  3922. uint32_t mp1_ver = amdgpu_ip_version(adev, MP1_HWIP, 0);
  3923. uint32_t gc_ver = amdgpu_ip_version(adev, GC_HWIP, 0);
  3924. uint32_t value;
  3925. uint64_t value64 = 0;
  3926. uint32_t query = 0;
  3927. int size;
  3928. /* GPU Clocks */
  3929. size = sizeof(value);
  3930. seq_printf(m, "GFX Clocks and Power:\n");
  3931. amdgpu_debugfs_prints_cpu_info(m, adev);
  3932. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
  3933. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  3934. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
  3935. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  3936. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK, (void *)&value, &size))
  3937. seq_printf(m, "\t%u MHz (PSTATE_SCLK)\n", value/100);
  3938. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK, (void *)&value, &size))
  3939. seq_printf(m, "\t%u MHz (PSTATE_MCLK)\n", value/100);
  3940. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
  3941. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  3942. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
  3943. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  3944. size = sizeof(uint32_t);
  3945. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_AVG_POWER, (void *)&query, &size)) {
  3946. if (adev->flags & AMD_IS_APU)
  3947. seq_printf(m, "\t%u.%02u W (average SoC including CPU)\n", query >> 8, query & 0xff);
  3948. else
  3949. seq_printf(m, "\t%u.%02u W (average SoC)\n", query >> 8, query & 0xff);
  3950. }
  3951. size = sizeof(uint32_t);
  3952. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_INPUT_POWER, (void *)&query, &size)) {
  3953. if (adev->flags & AMD_IS_APU)
  3954. seq_printf(m, "\t%u.%02u W (current SoC including CPU)\n", query >> 8, query & 0xff);
  3955. else
  3956. seq_printf(m, "\t%u.%02u W (current SoC)\n", query >> 8, query & 0xff);
  3957. }
  3958. size = sizeof(value);
  3959. seq_printf(m, "\n");
  3960. /* GPU Temp */
  3961. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
  3962. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  3963. /* GPU Load */
  3964. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
  3965. seq_printf(m, "GPU Load: %u %%\n", value);
  3966. /* MEM Load */
  3967. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_MEM_LOAD, (void *)&value, &size))
  3968. seq_printf(m, "MEM Load: %u %%\n", value);
  3969. /* VCN Load */
  3970. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_LOAD, (void *)&value, &size))
  3971. seq_printf(m, "VCN Load: %u %%\n", value);
  3972. seq_printf(m, "\n");
  3973. /* SMC feature mask */
  3974. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_ENABLED_SMC_FEATURES_MASK, (void *)&value64, &size))
  3975. seq_printf(m, "SMC Feature Mask: 0x%016llx\n", value64);
  3976. /* ASICs greater than CHIP_VEGA20 supports these sensors */
  3977. if (gc_ver != IP_VERSION(9, 4, 0) && mp1_ver > IP_VERSION(9, 0, 0)) {
  3978. /* VCN clocks */
  3979. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCN_POWER_STATE, (void *)&value, &size)) {
  3980. if (!value) {
  3981. seq_printf(m, "VCN: Powered down\n");
  3982. } else {
  3983. seq_printf(m, "VCN: Powered up\n");
  3984. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
  3985. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  3986. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
  3987. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  3988. }
  3989. }
  3990. seq_printf(m, "\n");
  3991. } else {
  3992. /* UVD clocks */
  3993. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
  3994. if (!value) {
  3995. seq_printf(m, "UVD: Powered down\n");
  3996. } else {
  3997. seq_printf(m, "UVD: Powered up\n");
  3998. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
  3999. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  4000. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
  4001. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  4002. }
  4003. }
  4004. seq_printf(m, "\n");
  4005. /* VCE clocks */
  4006. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
  4007. if (!value) {
  4008. seq_printf(m, "VCE: Powered down\n");
  4009. } else {
  4010. seq_printf(m, "VCE: Powered up\n");
  4011. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
  4012. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  4013. }
  4014. }
  4015. }
  4016. return 0;
  4017. }
  4018. static const struct cg_flag_name clocks[] = {
  4019. {AMD_CG_SUPPORT_GFX_FGCG, "Graphics Fine Grain Clock Gating"},
  4020. {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
  4021. {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
  4022. {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
  4023. {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
  4024. {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
  4025. {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
  4026. {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
  4027. {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
  4028. {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
  4029. {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
  4030. {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
  4031. {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
  4032. {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
  4033. {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
  4034. {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
  4035. {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
  4036. {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
  4037. {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
  4038. {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
  4039. {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
  4040. {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
  4041. {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
  4042. {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
  4043. {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
  4044. {AMD_CG_SUPPORT_VCN_MGCG, "VCN Medium Grain Clock Gating"},
  4045. {AMD_CG_SUPPORT_HDP_DS, "Host Data Path Deep Sleep"},
  4046. {AMD_CG_SUPPORT_HDP_SD, "Host Data Path Shutdown"},
  4047. {AMD_CG_SUPPORT_IH_CG, "Interrupt Handler Clock Gating"},
  4048. {AMD_CG_SUPPORT_JPEG_MGCG, "JPEG Medium Grain Clock Gating"},
  4049. {AMD_CG_SUPPORT_REPEATER_FGCG, "Repeater Fine Grain Clock Gating"},
  4050. {AMD_CG_SUPPORT_GFX_PERF_CLK, "Perfmon Clock Gating"},
  4051. {AMD_CG_SUPPORT_ATHUB_MGCG, "Address Translation Hub Medium Grain Clock Gating"},
  4052. {AMD_CG_SUPPORT_ATHUB_LS, "Address Translation Hub Light Sleep"},
  4053. {0, NULL},
  4054. };
  4055. static void amdgpu_parse_cg_state(struct seq_file *m, u64 flags)
  4056. {
  4057. int i;
  4058. for (i = 0; clocks[i].flag; i++)
  4059. seq_printf(m, "\t%s: %s\n", clocks[i].name,
  4060. (flags & clocks[i].flag) ? "On" : "Off");
  4061. }
  4062. static int amdgpu_debugfs_pm_info_show(struct seq_file *m, void *unused)
  4063. {
  4064. struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
  4065. struct drm_device *dev = adev_to_drm(adev);
  4066. u64 flags = 0;
  4067. int r;
  4068. if (amdgpu_in_reset(adev))
  4069. return -EPERM;
  4070. if (adev->in_suspend && !adev->in_runpm)
  4071. return -EPERM;
  4072. r = pm_runtime_get_sync(dev->dev);
  4073. if (r < 0) {
  4074. pm_runtime_put_autosuspend(dev->dev);
  4075. return r;
  4076. }
  4077. if (amdgpu_dpm_debugfs_print_current_performance_level(adev, m)) {
  4078. r = amdgpu_debugfs_pm_info_pp(m, adev);
  4079. if (r)
  4080. goto out;
  4081. }
  4082. amdgpu_device_ip_get_clockgating_state(adev, &flags);
  4083. seq_printf(m, "Clock Gating Flags Mask: 0x%llx\n", flags);
  4084. amdgpu_parse_cg_state(m, flags);
  4085. seq_printf(m, "\n");
  4086. out:
  4087. pm_runtime_mark_last_busy(dev->dev);
  4088. pm_runtime_put_autosuspend(dev->dev);
  4089. return r;
  4090. }
  4091. DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_pm_info);
  4092. /*
  4093. * amdgpu_pm_priv_buffer_read - Read memory region allocated to FW
  4094. *
  4095. * Reads debug memory region allocated to PMFW
  4096. */
  4097. static ssize_t amdgpu_pm_prv_buffer_read(struct file *f, char __user *buf,
  4098. size_t size, loff_t *pos)
  4099. {
  4100. struct amdgpu_device *adev = file_inode(f)->i_private;
  4101. size_t smu_prv_buf_size;
  4102. void *smu_prv_buf;
  4103. int ret = 0;
  4104. if (amdgpu_in_reset(adev))
  4105. return -EPERM;
  4106. if (adev->in_suspend && !adev->in_runpm)
  4107. return -EPERM;
  4108. ret = amdgpu_dpm_get_smu_prv_buf_details(adev, &smu_prv_buf, &smu_prv_buf_size);
  4109. if (ret)
  4110. return ret;
  4111. if (!smu_prv_buf || !smu_prv_buf_size)
  4112. return -EINVAL;
  4113. return simple_read_from_buffer(buf, size, pos, smu_prv_buf,
  4114. smu_prv_buf_size);
  4115. }
  4116. static const struct file_operations amdgpu_debugfs_pm_prv_buffer_fops = {
  4117. .owner = THIS_MODULE,
  4118. .open = simple_open,
  4119. .read = amdgpu_pm_prv_buffer_read,
  4120. .llseek = default_llseek,
  4121. };
  4122. #endif
  4123. void amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  4124. {
  4125. #if defined(CONFIG_DEBUG_FS)
  4126. struct drm_minor *minor = adev_to_drm(adev)->primary;
  4127. struct dentry *root = minor->debugfs_root;
  4128. if (!adev->pm.dpm_enabled)
  4129. return;
  4130. debugfs_create_file("amdgpu_pm_info", 0444, root, adev,
  4131. &amdgpu_debugfs_pm_info_fops);
  4132. if (adev->pm.smu_prv_buffer_size > 0)
  4133. debugfs_create_file_size("amdgpu_pm_prv_buffer", 0444, root,
  4134. adev,
  4135. &amdgpu_debugfs_pm_prv_buffer_fops,
  4136. adev->pm.smu_prv_buffer_size);
  4137. amdgpu_dpm_stb_debug_fs_init(adev);
  4138. #endif
  4139. }