kv_dpm.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include "drmP.h"
  24. #include "amdgpu.h"
  25. #include "amdgpu_pm.h"
  26. #include "cikd.h"
  27. #include "atom.h"
  28. #include "amdgpu_atombios.h"
  29. #include "amdgpu_dpm.h"
  30. #include "kv_dpm.h"
  31. #include "gfx_v7_0.h"
  32. #include <linux/seq_file.h>
  33. #include "smu/smu_7_0_0_d.h"
  34. #include "smu/smu_7_0_0_sh_mask.h"
  35. #include "gca/gfx_7_2_d.h"
  36. #include "gca/gfx_7_2_sh_mask.h"
  37. #define KV_MAX_DEEPSLEEP_DIVIDER_ID 5
  38. #define KV_MINIMUM_ENGINE_CLOCK 800
  39. #define SMC_RAM_END 0x40000
  40. static void kv_dpm_set_dpm_funcs(struct amdgpu_device *adev);
  41. static void kv_dpm_set_irq_funcs(struct amdgpu_device *adev);
  42. static int kv_enable_nb_dpm(struct amdgpu_device *adev,
  43. bool enable);
  44. static void kv_init_graphics_levels(struct amdgpu_device *adev);
  45. static int kv_calculate_ds_divider(struct amdgpu_device *adev);
  46. static int kv_calculate_nbps_level_settings(struct amdgpu_device *adev);
  47. static int kv_calculate_dpm_settings(struct amdgpu_device *adev);
  48. static void kv_enable_new_levels(struct amdgpu_device *adev);
  49. static void kv_program_nbps_index_settings(struct amdgpu_device *adev,
  50. struct amdgpu_ps *new_rps);
  51. static int kv_set_enabled_level(struct amdgpu_device *adev, u32 level);
  52. static int kv_set_enabled_levels(struct amdgpu_device *adev);
  53. static int kv_force_dpm_highest(struct amdgpu_device *adev);
  54. static int kv_force_dpm_lowest(struct amdgpu_device *adev);
  55. static void kv_apply_state_adjust_rules(struct amdgpu_device *adev,
  56. struct amdgpu_ps *new_rps,
  57. struct amdgpu_ps *old_rps);
  58. static int kv_set_thermal_temperature_range(struct amdgpu_device *adev,
  59. int min_temp, int max_temp);
  60. static int kv_init_fps_limits(struct amdgpu_device *adev);
  61. static void kv_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate);
  62. static void kv_dpm_powergate_vce(struct amdgpu_device *adev, bool gate);
  63. static void kv_dpm_powergate_samu(struct amdgpu_device *adev, bool gate);
  64. static void kv_dpm_powergate_acp(struct amdgpu_device *adev, bool gate);
  65. static u32 kv_convert_vid2_to_vid7(struct amdgpu_device *adev,
  66. struct sumo_vid_mapping_table *vid_mapping_table,
  67. u32 vid_2bit)
  68. {
  69. struct amdgpu_clock_voltage_dependency_table *vddc_sclk_table =
  70. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  71. u32 i;
  72. if (vddc_sclk_table && vddc_sclk_table->count) {
  73. if (vid_2bit < vddc_sclk_table->count)
  74. return vddc_sclk_table->entries[vid_2bit].v;
  75. else
  76. return vddc_sclk_table->entries[vddc_sclk_table->count - 1].v;
  77. } else {
  78. for (i = 0; i < vid_mapping_table->num_entries; i++) {
  79. if (vid_mapping_table->entries[i].vid_2bit == vid_2bit)
  80. return vid_mapping_table->entries[i].vid_7bit;
  81. }
  82. return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_7bit;
  83. }
  84. }
  85. static u32 kv_convert_vid7_to_vid2(struct amdgpu_device *adev,
  86. struct sumo_vid_mapping_table *vid_mapping_table,
  87. u32 vid_7bit)
  88. {
  89. struct amdgpu_clock_voltage_dependency_table *vddc_sclk_table =
  90. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  91. u32 i;
  92. if (vddc_sclk_table && vddc_sclk_table->count) {
  93. for (i = 0; i < vddc_sclk_table->count; i++) {
  94. if (vddc_sclk_table->entries[i].v == vid_7bit)
  95. return i;
  96. }
  97. return vddc_sclk_table->count - 1;
  98. } else {
  99. for (i = 0; i < vid_mapping_table->num_entries; i++) {
  100. if (vid_mapping_table->entries[i].vid_7bit == vid_7bit)
  101. return vid_mapping_table->entries[i].vid_2bit;
  102. }
  103. return vid_mapping_table->entries[vid_mapping_table->num_entries - 1].vid_2bit;
  104. }
  105. }
  106. static void sumo_take_smu_control(struct amdgpu_device *adev, bool enable)
  107. {
  108. /* This bit selects who handles display phy powergating.
  109. * Clear the bit to let atom handle it.
  110. * Set it to let the driver handle it.
  111. * For now we just let atom handle it.
  112. */
  113. #if 0
  114. u32 v = RREG32(mmDOUT_SCRATCH3);
  115. if (enable)
  116. v |= 0x4;
  117. else
  118. v &= 0xFFFFFFFB;
  119. WREG32(mmDOUT_SCRATCH3, v);
  120. #endif
  121. }
  122. static u32 sumo_get_sleep_divider_from_id(u32 id)
  123. {
  124. return 1 << id;
  125. }
  126. static void sumo_construct_sclk_voltage_mapping_table(struct amdgpu_device *adev,
  127. struct sumo_sclk_voltage_mapping_table *sclk_voltage_mapping_table,
  128. ATOM_AVAILABLE_SCLK_LIST *table)
  129. {
  130. u32 i;
  131. u32 n = 0;
  132. u32 prev_sclk = 0;
  133. for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++) {
  134. if (table[i].ulSupportedSCLK > prev_sclk) {
  135. sclk_voltage_mapping_table->entries[n].sclk_frequency =
  136. table[i].ulSupportedSCLK;
  137. sclk_voltage_mapping_table->entries[n].vid_2bit =
  138. table[i].usVoltageIndex;
  139. prev_sclk = table[i].ulSupportedSCLK;
  140. n++;
  141. }
  142. }
  143. sclk_voltage_mapping_table->num_max_dpm_entries = n;
  144. }
  145. static void sumo_construct_vid_mapping_table(struct amdgpu_device *adev,
  146. struct sumo_vid_mapping_table *vid_mapping_table,
  147. ATOM_AVAILABLE_SCLK_LIST *table)
  148. {
  149. u32 i, j;
  150. for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++) {
  151. if (table[i].ulSupportedSCLK != 0) {
  152. vid_mapping_table->entries[table[i].usVoltageIndex].vid_7bit =
  153. table[i].usVoltageID;
  154. vid_mapping_table->entries[table[i].usVoltageIndex].vid_2bit =
  155. table[i].usVoltageIndex;
  156. }
  157. }
  158. for (i = 0; i < SUMO_MAX_NUMBER_VOLTAGES; i++) {
  159. if (vid_mapping_table->entries[i].vid_7bit == 0) {
  160. for (j = i + 1; j < SUMO_MAX_NUMBER_VOLTAGES; j++) {
  161. if (vid_mapping_table->entries[j].vid_7bit != 0) {
  162. vid_mapping_table->entries[i] =
  163. vid_mapping_table->entries[j];
  164. vid_mapping_table->entries[j].vid_7bit = 0;
  165. break;
  166. }
  167. }
  168. if (j == SUMO_MAX_NUMBER_VOLTAGES)
  169. break;
  170. }
  171. }
  172. vid_mapping_table->num_entries = i;
  173. }
  174. static const struct kv_lcac_config_values sx_local_cac_cfg_kv[] =
  175. {
  176. { 0, 4, 1 },
  177. { 1, 4, 1 },
  178. { 2, 5, 1 },
  179. { 3, 4, 2 },
  180. { 4, 1, 1 },
  181. { 5, 5, 2 },
  182. { 6, 6, 1 },
  183. { 7, 9, 2 },
  184. { 0xffffffff }
  185. };
  186. static const struct kv_lcac_config_values mc0_local_cac_cfg_kv[] =
  187. {
  188. { 0, 4, 1 },
  189. { 0xffffffff }
  190. };
  191. static const struct kv_lcac_config_values mc1_local_cac_cfg_kv[] =
  192. {
  193. { 0, 4, 1 },
  194. { 0xffffffff }
  195. };
  196. static const struct kv_lcac_config_values mc2_local_cac_cfg_kv[] =
  197. {
  198. { 0, 4, 1 },
  199. { 0xffffffff }
  200. };
  201. static const struct kv_lcac_config_values mc3_local_cac_cfg_kv[] =
  202. {
  203. { 0, 4, 1 },
  204. { 0xffffffff }
  205. };
  206. static const struct kv_lcac_config_values cpl_local_cac_cfg_kv[] =
  207. {
  208. { 0, 4, 1 },
  209. { 1, 4, 1 },
  210. { 2, 5, 1 },
  211. { 3, 4, 1 },
  212. { 4, 1, 1 },
  213. { 5, 5, 1 },
  214. { 6, 6, 1 },
  215. { 7, 9, 1 },
  216. { 8, 4, 1 },
  217. { 9, 2, 1 },
  218. { 10, 3, 1 },
  219. { 11, 6, 1 },
  220. { 12, 8, 2 },
  221. { 13, 1, 1 },
  222. { 14, 2, 1 },
  223. { 15, 3, 1 },
  224. { 16, 1, 1 },
  225. { 17, 4, 1 },
  226. { 18, 3, 1 },
  227. { 19, 1, 1 },
  228. { 20, 8, 1 },
  229. { 21, 5, 1 },
  230. { 22, 1, 1 },
  231. { 23, 1, 1 },
  232. { 24, 4, 1 },
  233. { 27, 6, 1 },
  234. { 28, 1, 1 },
  235. { 0xffffffff }
  236. };
  237. static const struct kv_lcac_config_reg sx0_cac_config_reg[] =
  238. {
  239. { 0xc0400d00, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  240. };
  241. static const struct kv_lcac_config_reg mc0_cac_config_reg[] =
  242. {
  243. { 0xc0400d30, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  244. };
  245. static const struct kv_lcac_config_reg mc1_cac_config_reg[] =
  246. {
  247. { 0xc0400d3c, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  248. };
  249. static const struct kv_lcac_config_reg mc2_cac_config_reg[] =
  250. {
  251. { 0xc0400d48, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  252. };
  253. static const struct kv_lcac_config_reg mc3_cac_config_reg[] =
  254. {
  255. { 0xc0400d54, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  256. };
  257. static const struct kv_lcac_config_reg cpl_cac_config_reg[] =
  258. {
  259. { 0xc0400d80, 0x003e0000, 17, 0x3fc00000, 22, 0x0001fffe, 1, 0x00000001, 0 }
  260. };
  261. static const struct kv_pt_config_reg didt_config_kv[] =
  262. {
  263. { 0x10, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  264. { 0x10, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  265. { 0x10, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  266. { 0x10, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  267. { 0x11, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  268. { 0x11, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  269. { 0x11, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  270. { 0x11, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  271. { 0x12, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  272. { 0x12, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  273. { 0x12, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  274. { 0x12, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  275. { 0x2, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
  276. { 0x2, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
  277. { 0x2, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
  278. { 0x1, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  279. { 0x1, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  280. { 0x0, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  281. { 0x30, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  282. { 0x30, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  283. { 0x30, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  284. { 0x30, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  285. { 0x31, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  286. { 0x31, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  287. { 0x31, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  288. { 0x31, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  289. { 0x32, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  290. { 0x32, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  291. { 0x32, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  292. { 0x32, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  293. { 0x22, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
  294. { 0x22, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
  295. { 0x22, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
  296. { 0x21, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  297. { 0x21, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  298. { 0x20, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  299. { 0x50, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  300. { 0x50, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  301. { 0x50, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  302. { 0x50, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  303. { 0x51, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  304. { 0x51, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  305. { 0x51, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  306. { 0x51, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  307. { 0x52, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  308. { 0x52, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  309. { 0x52, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  310. { 0x52, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  311. { 0x42, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
  312. { 0x42, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
  313. { 0x42, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
  314. { 0x41, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  315. { 0x41, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  316. { 0x40, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  317. { 0x70, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  318. { 0x70, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  319. { 0x70, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  320. { 0x70, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  321. { 0x71, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  322. { 0x71, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  323. { 0x71, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  324. { 0x71, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  325. { 0x72, 0x000000ff, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  326. { 0x72, 0x0000ff00, 8, 0x0, KV_CONFIGREG_DIDT_IND },
  327. { 0x72, 0x00ff0000, 16, 0x0, KV_CONFIGREG_DIDT_IND },
  328. { 0x72, 0xff000000, 24, 0x0, KV_CONFIGREG_DIDT_IND },
  329. { 0x62, 0x00003fff, 0, 0x4, KV_CONFIGREG_DIDT_IND },
  330. { 0x62, 0x03ff0000, 16, 0x80, KV_CONFIGREG_DIDT_IND },
  331. { 0x62, 0x78000000, 27, 0x3, KV_CONFIGREG_DIDT_IND },
  332. { 0x61, 0x0000ffff, 0, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  333. { 0x61, 0xffff0000, 16, 0x3FFF, KV_CONFIGREG_DIDT_IND },
  334. { 0x60, 0x00000001, 0, 0x0, KV_CONFIGREG_DIDT_IND },
  335. { 0xFFFFFFFF }
  336. };
  337. static struct kv_ps *kv_get_ps(struct amdgpu_ps *rps)
  338. {
  339. struct kv_ps *ps = rps->ps_priv;
  340. return ps;
  341. }
  342. static struct kv_power_info *kv_get_pi(struct amdgpu_device *adev)
  343. {
  344. struct kv_power_info *pi = adev->pm.dpm.priv;
  345. return pi;
  346. }
  347. #if 0
  348. static void kv_program_local_cac_table(struct amdgpu_device *adev,
  349. const struct kv_lcac_config_values *local_cac_table,
  350. const struct kv_lcac_config_reg *local_cac_reg)
  351. {
  352. u32 i, count, data;
  353. const struct kv_lcac_config_values *values = local_cac_table;
  354. while (values->block_id != 0xffffffff) {
  355. count = values->signal_id;
  356. for (i = 0; i < count; i++) {
  357. data = ((values->block_id << local_cac_reg->block_shift) &
  358. local_cac_reg->block_mask);
  359. data |= ((i << local_cac_reg->signal_shift) &
  360. local_cac_reg->signal_mask);
  361. data |= ((values->t << local_cac_reg->t_shift) &
  362. local_cac_reg->t_mask);
  363. data |= ((1 << local_cac_reg->enable_shift) &
  364. local_cac_reg->enable_mask);
  365. WREG32_SMC(local_cac_reg->cntl, data);
  366. }
  367. values++;
  368. }
  369. }
  370. #endif
  371. static int kv_program_pt_config_registers(struct amdgpu_device *adev,
  372. const struct kv_pt_config_reg *cac_config_regs)
  373. {
  374. const struct kv_pt_config_reg *config_regs = cac_config_regs;
  375. u32 data;
  376. u32 cache = 0;
  377. if (config_regs == NULL)
  378. return -EINVAL;
  379. while (config_regs->offset != 0xFFFFFFFF) {
  380. if (config_regs->type == KV_CONFIGREG_CACHE) {
  381. cache |= ((config_regs->value << config_regs->shift) & config_regs->mask);
  382. } else {
  383. switch (config_regs->type) {
  384. case KV_CONFIGREG_SMC_IND:
  385. data = RREG32_SMC(config_regs->offset);
  386. break;
  387. case KV_CONFIGREG_DIDT_IND:
  388. data = RREG32_DIDT(config_regs->offset);
  389. break;
  390. default:
  391. data = RREG32(config_regs->offset);
  392. break;
  393. }
  394. data &= ~config_regs->mask;
  395. data |= ((config_regs->value << config_regs->shift) & config_regs->mask);
  396. data |= cache;
  397. cache = 0;
  398. switch (config_regs->type) {
  399. case KV_CONFIGREG_SMC_IND:
  400. WREG32_SMC(config_regs->offset, data);
  401. break;
  402. case KV_CONFIGREG_DIDT_IND:
  403. WREG32_DIDT(config_regs->offset, data);
  404. break;
  405. default:
  406. WREG32(config_regs->offset, data);
  407. break;
  408. }
  409. }
  410. config_regs++;
  411. }
  412. return 0;
  413. }
  414. static void kv_do_enable_didt(struct amdgpu_device *adev, bool enable)
  415. {
  416. struct kv_power_info *pi = kv_get_pi(adev);
  417. u32 data;
  418. if (pi->caps_sq_ramping) {
  419. data = RREG32_DIDT(ixDIDT_SQ_CTRL0);
  420. if (enable)
  421. data |= DIDT_SQ_CTRL0__DIDT_CTRL_EN_MASK;
  422. else
  423. data &= ~DIDT_SQ_CTRL0__DIDT_CTRL_EN_MASK;
  424. WREG32_DIDT(ixDIDT_SQ_CTRL0, data);
  425. }
  426. if (pi->caps_db_ramping) {
  427. data = RREG32_DIDT(ixDIDT_DB_CTRL0);
  428. if (enable)
  429. data |= DIDT_DB_CTRL0__DIDT_CTRL_EN_MASK;
  430. else
  431. data &= ~DIDT_DB_CTRL0__DIDT_CTRL_EN_MASK;
  432. WREG32_DIDT(ixDIDT_DB_CTRL0, data);
  433. }
  434. if (pi->caps_td_ramping) {
  435. data = RREG32_DIDT(ixDIDT_TD_CTRL0);
  436. if (enable)
  437. data |= DIDT_TD_CTRL0__DIDT_CTRL_EN_MASK;
  438. else
  439. data &= ~DIDT_TD_CTRL0__DIDT_CTRL_EN_MASK;
  440. WREG32_DIDT(ixDIDT_TD_CTRL0, data);
  441. }
  442. if (pi->caps_tcp_ramping) {
  443. data = RREG32_DIDT(ixDIDT_TCP_CTRL0);
  444. if (enable)
  445. data |= DIDT_TCP_CTRL0__DIDT_CTRL_EN_MASK;
  446. else
  447. data &= ~DIDT_TCP_CTRL0__DIDT_CTRL_EN_MASK;
  448. WREG32_DIDT(ixDIDT_TCP_CTRL0, data);
  449. }
  450. }
  451. static int kv_enable_didt(struct amdgpu_device *adev, bool enable)
  452. {
  453. struct kv_power_info *pi = kv_get_pi(adev);
  454. int ret;
  455. if (pi->caps_sq_ramping ||
  456. pi->caps_db_ramping ||
  457. pi->caps_td_ramping ||
  458. pi->caps_tcp_ramping) {
  459. gfx_v7_0_enter_rlc_safe_mode(adev);
  460. if (enable) {
  461. ret = kv_program_pt_config_registers(adev, didt_config_kv);
  462. if (ret) {
  463. gfx_v7_0_exit_rlc_safe_mode(adev);
  464. return ret;
  465. }
  466. }
  467. kv_do_enable_didt(adev, enable);
  468. gfx_v7_0_exit_rlc_safe_mode(adev);
  469. }
  470. return 0;
  471. }
  472. #if 0
  473. static void kv_initialize_hardware_cac_manager(struct amdgpu_device *adev)
  474. {
  475. struct kv_power_info *pi = kv_get_pi(adev);
  476. if (pi->caps_cac) {
  477. WREG32_SMC(ixLCAC_SX0_OVR_SEL, 0);
  478. WREG32_SMC(ixLCAC_SX0_OVR_VAL, 0);
  479. kv_program_local_cac_table(adev, sx_local_cac_cfg_kv, sx0_cac_config_reg);
  480. WREG32_SMC(ixLCAC_MC0_OVR_SEL, 0);
  481. WREG32_SMC(ixLCAC_MC0_OVR_VAL, 0);
  482. kv_program_local_cac_table(adev, mc0_local_cac_cfg_kv, mc0_cac_config_reg);
  483. WREG32_SMC(ixLCAC_MC1_OVR_SEL, 0);
  484. WREG32_SMC(ixLCAC_MC1_OVR_VAL, 0);
  485. kv_program_local_cac_table(adev, mc1_local_cac_cfg_kv, mc1_cac_config_reg);
  486. WREG32_SMC(ixLCAC_MC2_OVR_SEL, 0);
  487. WREG32_SMC(ixLCAC_MC2_OVR_VAL, 0);
  488. kv_program_local_cac_table(adev, mc2_local_cac_cfg_kv, mc2_cac_config_reg);
  489. WREG32_SMC(ixLCAC_MC3_OVR_SEL, 0);
  490. WREG32_SMC(ixLCAC_MC3_OVR_VAL, 0);
  491. kv_program_local_cac_table(adev, mc3_local_cac_cfg_kv, mc3_cac_config_reg);
  492. WREG32_SMC(ixLCAC_CPL_OVR_SEL, 0);
  493. WREG32_SMC(ixLCAC_CPL_OVR_VAL, 0);
  494. kv_program_local_cac_table(adev, cpl_local_cac_cfg_kv, cpl_cac_config_reg);
  495. }
  496. }
  497. #endif
  498. static int kv_enable_smc_cac(struct amdgpu_device *adev, bool enable)
  499. {
  500. struct kv_power_info *pi = kv_get_pi(adev);
  501. int ret = 0;
  502. if (pi->caps_cac) {
  503. if (enable) {
  504. ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_EnableCac);
  505. if (ret)
  506. pi->cac_enabled = false;
  507. else
  508. pi->cac_enabled = true;
  509. } else if (pi->cac_enabled) {
  510. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_DisableCac);
  511. pi->cac_enabled = false;
  512. }
  513. }
  514. return ret;
  515. }
  516. static int kv_process_firmware_header(struct amdgpu_device *adev)
  517. {
  518. struct kv_power_info *pi = kv_get_pi(adev);
  519. u32 tmp;
  520. int ret;
  521. ret = amdgpu_kv_read_smc_sram_dword(adev, SMU7_FIRMWARE_HEADER_LOCATION +
  522. offsetof(SMU7_Firmware_Header, DpmTable),
  523. &tmp, pi->sram_end);
  524. if (ret == 0)
  525. pi->dpm_table_start = tmp;
  526. ret = amdgpu_kv_read_smc_sram_dword(adev, SMU7_FIRMWARE_HEADER_LOCATION +
  527. offsetof(SMU7_Firmware_Header, SoftRegisters),
  528. &tmp, pi->sram_end);
  529. if (ret == 0)
  530. pi->soft_regs_start = tmp;
  531. return ret;
  532. }
  533. static int kv_enable_dpm_voltage_scaling(struct amdgpu_device *adev)
  534. {
  535. struct kv_power_info *pi = kv_get_pi(adev);
  536. int ret;
  537. pi->graphics_voltage_change_enable = 1;
  538. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  539. pi->dpm_table_start +
  540. offsetof(SMU7_Fusion_DpmTable, GraphicsVoltageChangeEnable),
  541. &pi->graphics_voltage_change_enable,
  542. sizeof(u8), pi->sram_end);
  543. return ret;
  544. }
  545. static int kv_set_dpm_interval(struct amdgpu_device *adev)
  546. {
  547. struct kv_power_info *pi = kv_get_pi(adev);
  548. int ret;
  549. pi->graphics_interval = 1;
  550. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  551. pi->dpm_table_start +
  552. offsetof(SMU7_Fusion_DpmTable, GraphicsInterval),
  553. &pi->graphics_interval,
  554. sizeof(u8), pi->sram_end);
  555. return ret;
  556. }
  557. static int kv_set_dpm_boot_state(struct amdgpu_device *adev)
  558. {
  559. struct kv_power_info *pi = kv_get_pi(adev);
  560. int ret;
  561. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  562. pi->dpm_table_start +
  563. offsetof(SMU7_Fusion_DpmTable, GraphicsBootLevel),
  564. &pi->graphics_boot_level,
  565. sizeof(u8), pi->sram_end);
  566. return ret;
  567. }
  568. static void kv_program_vc(struct amdgpu_device *adev)
  569. {
  570. WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0x3FFFC100);
  571. }
  572. static void kv_clear_vc(struct amdgpu_device *adev)
  573. {
  574. WREG32_SMC(ixCG_FREQ_TRAN_VOTING_0, 0);
  575. }
  576. static int kv_set_divider_value(struct amdgpu_device *adev,
  577. u32 index, u32 sclk)
  578. {
  579. struct kv_power_info *pi = kv_get_pi(adev);
  580. struct atom_clock_dividers dividers;
  581. int ret;
  582. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  583. sclk, false, &dividers);
  584. if (ret)
  585. return ret;
  586. pi->graphics_level[index].SclkDid = (u8)dividers.post_div;
  587. pi->graphics_level[index].SclkFrequency = cpu_to_be32(sclk);
  588. return 0;
  589. }
  590. static u16 kv_convert_8bit_index_to_voltage(struct amdgpu_device *adev,
  591. u16 voltage)
  592. {
  593. return 6200 - (voltage * 25);
  594. }
  595. static u16 kv_convert_2bit_index_to_voltage(struct amdgpu_device *adev,
  596. u32 vid_2bit)
  597. {
  598. struct kv_power_info *pi = kv_get_pi(adev);
  599. u32 vid_8bit = kv_convert_vid2_to_vid7(adev,
  600. &pi->sys_info.vid_mapping_table,
  601. vid_2bit);
  602. return kv_convert_8bit_index_to_voltage(adev, (u16)vid_8bit);
  603. }
  604. static int kv_set_vid(struct amdgpu_device *adev, u32 index, u32 vid)
  605. {
  606. struct kv_power_info *pi = kv_get_pi(adev);
  607. pi->graphics_level[index].VoltageDownH = (u8)pi->voltage_drop_t;
  608. pi->graphics_level[index].MinVddNb =
  609. cpu_to_be32(kv_convert_2bit_index_to_voltage(adev, vid));
  610. return 0;
  611. }
  612. static int kv_set_at(struct amdgpu_device *adev, u32 index, u32 at)
  613. {
  614. struct kv_power_info *pi = kv_get_pi(adev);
  615. pi->graphics_level[index].AT = cpu_to_be16((u16)at);
  616. return 0;
  617. }
  618. static void kv_dpm_power_level_enable(struct amdgpu_device *adev,
  619. u32 index, bool enable)
  620. {
  621. struct kv_power_info *pi = kv_get_pi(adev);
  622. pi->graphics_level[index].EnabledForActivity = enable ? 1 : 0;
  623. }
  624. static void kv_start_dpm(struct amdgpu_device *adev)
  625. {
  626. u32 tmp = RREG32_SMC(ixGENERAL_PWRMGT);
  627. tmp |= GENERAL_PWRMGT__GLOBAL_PWRMGT_EN_MASK;
  628. WREG32_SMC(ixGENERAL_PWRMGT, tmp);
  629. amdgpu_kv_smc_dpm_enable(adev, true);
  630. }
  631. static void kv_stop_dpm(struct amdgpu_device *adev)
  632. {
  633. amdgpu_kv_smc_dpm_enable(adev, false);
  634. }
  635. static void kv_start_am(struct amdgpu_device *adev)
  636. {
  637. u32 sclk_pwrmgt_cntl = RREG32_SMC(ixSCLK_PWRMGT_CNTL);
  638. sclk_pwrmgt_cntl &= ~(SCLK_PWRMGT_CNTL__RESET_SCLK_CNT_MASK |
  639. SCLK_PWRMGT_CNTL__RESET_BUSY_CNT_MASK);
  640. sclk_pwrmgt_cntl |= SCLK_PWRMGT_CNTL__DYNAMIC_PM_EN_MASK;
  641. WREG32_SMC(ixSCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
  642. }
  643. static void kv_reset_am(struct amdgpu_device *adev)
  644. {
  645. u32 sclk_pwrmgt_cntl = RREG32_SMC(ixSCLK_PWRMGT_CNTL);
  646. sclk_pwrmgt_cntl |= (SCLK_PWRMGT_CNTL__RESET_SCLK_CNT_MASK |
  647. SCLK_PWRMGT_CNTL__RESET_BUSY_CNT_MASK);
  648. WREG32_SMC(ixSCLK_PWRMGT_CNTL, sclk_pwrmgt_cntl);
  649. }
  650. static int kv_freeze_sclk_dpm(struct amdgpu_device *adev, bool freeze)
  651. {
  652. return amdgpu_kv_notify_message_to_smu(adev, freeze ?
  653. PPSMC_MSG_SCLKDPM_FreezeLevel : PPSMC_MSG_SCLKDPM_UnfreezeLevel);
  654. }
  655. static int kv_force_lowest_valid(struct amdgpu_device *adev)
  656. {
  657. return kv_force_dpm_lowest(adev);
  658. }
  659. static int kv_unforce_levels(struct amdgpu_device *adev)
  660. {
  661. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
  662. return amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NoForcedLevel);
  663. else
  664. return kv_set_enabled_levels(adev);
  665. }
  666. static int kv_update_sclk_t(struct amdgpu_device *adev)
  667. {
  668. struct kv_power_info *pi = kv_get_pi(adev);
  669. u32 low_sclk_interrupt_t = 0;
  670. int ret = 0;
  671. if (pi->caps_sclk_throttle_low_notification) {
  672. low_sclk_interrupt_t = cpu_to_be32(pi->low_sclk_interrupt_t);
  673. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  674. pi->dpm_table_start +
  675. offsetof(SMU7_Fusion_DpmTable, LowSclkInterruptT),
  676. (u8 *)&low_sclk_interrupt_t,
  677. sizeof(u32), pi->sram_end);
  678. }
  679. return ret;
  680. }
  681. static int kv_program_bootup_state(struct amdgpu_device *adev)
  682. {
  683. struct kv_power_info *pi = kv_get_pi(adev);
  684. u32 i;
  685. struct amdgpu_clock_voltage_dependency_table *table =
  686. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  687. if (table && table->count) {
  688. for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
  689. if (table->entries[i].clk == pi->boot_pl.sclk)
  690. break;
  691. }
  692. pi->graphics_boot_level = (u8)i;
  693. kv_dpm_power_level_enable(adev, i, true);
  694. } else {
  695. struct sumo_sclk_voltage_mapping_table *table =
  696. &pi->sys_info.sclk_voltage_mapping_table;
  697. if (table->num_max_dpm_entries == 0)
  698. return -EINVAL;
  699. for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
  700. if (table->entries[i].sclk_frequency == pi->boot_pl.sclk)
  701. break;
  702. }
  703. pi->graphics_boot_level = (u8)i;
  704. kv_dpm_power_level_enable(adev, i, true);
  705. }
  706. return 0;
  707. }
  708. static int kv_enable_auto_thermal_throttling(struct amdgpu_device *adev)
  709. {
  710. struct kv_power_info *pi = kv_get_pi(adev);
  711. int ret;
  712. pi->graphics_therm_throttle_enable = 1;
  713. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  714. pi->dpm_table_start +
  715. offsetof(SMU7_Fusion_DpmTable, GraphicsThermThrottleEnable),
  716. &pi->graphics_therm_throttle_enable,
  717. sizeof(u8), pi->sram_end);
  718. return ret;
  719. }
  720. static int kv_upload_dpm_settings(struct amdgpu_device *adev)
  721. {
  722. struct kv_power_info *pi = kv_get_pi(adev);
  723. int ret;
  724. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  725. pi->dpm_table_start +
  726. offsetof(SMU7_Fusion_DpmTable, GraphicsLevel),
  727. (u8 *)&pi->graphics_level,
  728. sizeof(SMU7_Fusion_GraphicsLevel) * SMU7_MAX_LEVELS_GRAPHICS,
  729. pi->sram_end);
  730. if (ret)
  731. return ret;
  732. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  733. pi->dpm_table_start +
  734. offsetof(SMU7_Fusion_DpmTable, GraphicsDpmLevelCount),
  735. &pi->graphics_dpm_level_count,
  736. sizeof(u8), pi->sram_end);
  737. return ret;
  738. }
  739. static u32 kv_get_clock_difference(u32 a, u32 b)
  740. {
  741. return (a >= b) ? a - b : b - a;
  742. }
  743. static u32 kv_get_clk_bypass(struct amdgpu_device *adev, u32 clk)
  744. {
  745. struct kv_power_info *pi = kv_get_pi(adev);
  746. u32 value;
  747. if (pi->caps_enable_dfs_bypass) {
  748. if (kv_get_clock_difference(clk, 40000) < 200)
  749. value = 3;
  750. else if (kv_get_clock_difference(clk, 30000) < 200)
  751. value = 2;
  752. else if (kv_get_clock_difference(clk, 20000) < 200)
  753. value = 7;
  754. else if (kv_get_clock_difference(clk, 15000) < 200)
  755. value = 6;
  756. else if (kv_get_clock_difference(clk, 10000) < 200)
  757. value = 8;
  758. else
  759. value = 0;
  760. } else {
  761. value = 0;
  762. }
  763. return value;
  764. }
  765. static int kv_populate_uvd_table(struct amdgpu_device *adev)
  766. {
  767. struct kv_power_info *pi = kv_get_pi(adev);
  768. struct amdgpu_uvd_clock_voltage_dependency_table *table =
  769. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  770. struct atom_clock_dividers dividers;
  771. int ret;
  772. u32 i;
  773. if (table == NULL || table->count == 0)
  774. return 0;
  775. pi->uvd_level_count = 0;
  776. for (i = 0; i < table->count; i++) {
  777. if (pi->high_voltage_t &&
  778. (pi->high_voltage_t < table->entries[i].v))
  779. break;
  780. pi->uvd_level[i].VclkFrequency = cpu_to_be32(table->entries[i].vclk);
  781. pi->uvd_level[i].DclkFrequency = cpu_to_be32(table->entries[i].dclk);
  782. pi->uvd_level[i].MinVddNb = cpu_to_be16(table->entries[i].v);
  783. pi->uvd_level[i].VClkBypassCntl =
  784. (u8)kv_get_clk_bypass(adev, table->entries[i].vclk);
  785. pi->uvd_level[i].DClkBypassCntl =
  786. (u8)kv_get_clk_bypass(adev, table->entries[i].dclk);
  787. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  788. table->entries[i].vclk, false, &dividers);
  789. if (ret)
  790. return ret;
  791. pi->uvd_level[i].VclkDivider = (u8)dividers.post_div;
  792. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  793. table->entries[i].dclk, false, &dividers);
  794. if (ret)
  795. return ret;
  796. pi->uvd_level[i].DclkDivider = (u8)dividers.post_div;
  797. pi->uvd_level_count++;
  798. }
  799. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  800. pi->dpm_table_start +
  801. offsetof(SMU7_Fusion_DpmTable, UvdLevelCount),
  802. (u8 *)&pi->uvd_level_count,
  803. sizeof(u8), pi->sram_end);
  804. if (ret)
  805. return ret;
  806. pi->uvd_interval = 1;
  807. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  808. pi->dpm_table_start +
  809. offsetof(SMU7_Fusion_DpmTable, UVDInterval),
  810. &pi->uvd_interval,
  811. sizeof(u8), pi->sram_end);
  812. if (ret)
  813. return ret;
  814. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  815. pi->dpm_table_start +
  816. offsetof(SMU7_Fusion_DpmTable, UvdLevel),
  817. (u8 *)&pi->uvd_level,
  818. sizeof(SMU7_Fusion_UvdLevel) * SMU7_MAX_LEVELS_UVD,
  819. pi->sram_end);
  820. return ret;
  821. }
  822. static int kv_populate_vce_table(struct amdgpu_device *adev)
  823. {
  824. struct kv_power_info *pi = kv_get_pi(adev);
  825. int ret;
  826. u32 i;
  827. struct amdgpu_vce_clock_voltage_dependency_table *table =
  828. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  829. struct atom_clock_dividers dividers;
  830. if (table == NULL || table->count == 0)
  831. return 0;
  832. pi->vce_level_count = 0;
  833. for (i = 0; i < table->count; i++) {
  834. if (pi->high_voltage_t &&
  835. pi->high_voltage_t < table->entries[i].v)
  836. break;
  837. pi->vce_level[i].Frequency = cpu_to_be32(table->entries[i].evclk);
  838. pi->vce_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
  839. pi->vce_level[i].ClkBypassCntl =
  840. (u8)kv_get_clk_bypass(adev, table->entries[i].evclk);
  841. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  842. table->entries[i].evclk, false, &dividers);
  843. if (ret)
  844. return ret;
  845. pi->vce_level[i].Divider = (u8)dividers.post_div;
  846. pi->vce_level_count++;
  847. }
  848. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  849. pi->dpm_table_start +
  850. offsetof(SMU7_Fusion_DpmTable, VceLevelCount),
  851. (u8 *)&pi->vce_level_count,
  852. sizeof(u8),
  853. pi->sram_end);
  854. if (ret)
  855. return ret;
  856. pi->vce_interval = 1;
  857. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  858. pi->dpm_table_start +
  859. offsetof(SMU7_Fusion_DpmTable, VCEInterval),
  860. (u8 *)&pi->vce_interval,
  861. sizeof(u8),
  862. pi->sram_end);
  863. if (ret)
  864. return ret;
  865. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  866. pi->dpm_table_start +
  867. offsetof(SMU7_Fusion_DpmTable, VceLevel),
  868. (u8 *)&pi->vce_level,
  869. sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_VCE,
  870. pi->sram_end);
  871. return ret;
  872. }
  873. static int kv_populate_samu_table(struct amdgpu_device *adev)
  874. {
  875. struct kv_power_info *pi = kv_get_pi(adev);
  876. struct amdgpu_clock_voltage_dependency_table *table =
  877. &adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
  878. struct atom_clock_dividers dividers;
  879. int ret;
  880. u32 i;
  881. if (table == NULL || table->count == 0)
  882. return 0;
  883. pi->samu_level_count = 0;
  884. for (i = 0; i < table->count; i++) {
  885. if (pi->high_voltage_t &&
  886. pi->high_voltage_t < table->entries[i].v)
  887. break;
  888. pi->samu_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
  889. pi->samu_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
  890. pi->samu_level[i].ClkBypassCntl =
  891. (u8)kv_get_clk_bypass(adev, table->entries[i].clk);
  892. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  893. table->entries[i].clk, false, &dividers);
  894. if (ret)
  895. return ret;
  896. pi->samu_level[i].Divider = (u8)dividers.post_div;
  897. pi->samu_level_count++;
  898. }
  899. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  900. pi->dpm_table_start +
  901. offsetof(SMU7_Fusion_DpmTable, SamuLevelCount),
  902. (u8 *)&pi->samu_level_count,
  903. sizeof(u8),
  904. pi->sram_end);
  905. if (ret)
  906. return ret;
  907. pi->samu_interval = 1;
  908. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  909. pi->dpm_table_start +
  910. offsetof(SMU7_Fusion_DpmTable, SAMUInterval),
  911. (u8 *)&pi->samu_interval,
  912. sizeof(u8),
  913. pi->sram_end);
  914. if (ret)
  915. return ret;
  916. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  917. pi->dpm_table_start +
  918. offsetof(SMU7_Fusion_DpmTable, SamuLevel),
  919. (u8 *)&pi->samu_level,
  920. sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_SAMU,
  921. pi->sram_end);
  922. if (ret)
  923. return ret;
  924. return ret;
  925. }
  926. static int kv_populate_acp_table(struct amdgpu_device *adev)
  927. {
  928. struct kv_power_info *pi = kv_get_pi(adev);
  929. struct amdgpu_clock_voltage_dependency_table *table =
  930. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  931. struct atom_clock_dividers dividers;
  932. int ret;
  933. u32 i;
  934. if (table == NULL || table->count == 0)
  935. return 0;
  936. pi->acp_level_count = 0;
  937. for (i = 0; i < table->count; i++) {
  938. pi->acp_level[i].Frequency = cpu_to_be32(table->entries[i].clk);
  939. pi->acp_level[i].MinVoltage = cpu_to_be16(table->entries[i].v);
  940. ret = amdgpu_atombios_get_clock_dividers(adev, COMPUTE_ENGINE_PLL_PARAM,
  941. table->entries[i].clk, false, &dividers);
  942. if (ret)
  943. return ret;
  944. pi->acp_level[i].Divider = (u8)dividers.post_div;
  945. pi->acp_level_count++;
  946. }
  947. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  948. pi->dpm_table_start +
  949. offsetof(SMU7_Fusion_DpmTable, AcpLevelCount),
  950. (u8 *)&pi->acp_level_count,
  951. sizeof(u8),
  952. pi->sram_end);
  953. if (ret)
  954. return ret;
  955. pi->acp_interval = 1;
  956. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  957. pi->dpm_table_start +
  958. offsetof(SMU7_Fusion_DpmTable, ACPInterval),
  959. (u8 *)&pi->acp_interval,
  960. sizeof(u8),
  961. pi->sram_end);
  962. if (ret)
  963. return ret;
  964. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  965. pi->dpm_table_start +
  966. offsetof(SMU7_Fusion_DpmTable, AcpLevel),
  967. (u8 *)&pi->acp_level,
  968. sizeof(SMU7_Fusion_ExtClkLevel) * SMU7_MAX_LEVELS_ACP,
  969. pi->sram_end);
  970. if (ret)
  971. return ret;
  972. return ret;
  973. }
  974. static void kv_calculate_dfs_bypass_settings(struct amdgpu_device *adev)
  975. {
  976. struct kv_power_info *pi = kv_get_pi(adev);
  977. u32 i;
  978. struct amdgpu_clock_voltage_dependency_table *table =
  979. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  980. if (table && table->count) {
  981. for (i = 0; i < pi->graphics_dpm_level_count; i++) {
  982. if (pi->caps_enable_dfs_bypass) {
  983. if (kv_get_clock_difference(table->entries[i].clk, 40000) < 200)
  984. pi->graphics_level[i].ClkBypassCntl = 3;
  985. else if (kv_get_clock_difference(table->entries[i].clk, 30000) < 200)
  986. pi->graphics_level[i].ClkBypassCntl = 2;
  987. else if (kv_get_clock_difference(table->entries[i].clk, 26600) < 200)
  988. pi->graphics_level[i].ClkBypassCntl = 7;
  989. else if (kv_get_clock_difference(table->entries[i].clk , 20000) < 200)
  990. pi->graphics_level[i].ClkBypassCntl = 6;
  991. else if (kv_get_clock_difference(table->entries[i].clk , 10000) < 200)
  992. pi->graphics_level[i].ClkBypassCntl = 8;
  993. else
  994. pi->graphics_level[i].ClkBypassCntl = 0;
  995. } else {
  996. pi->graphics_level[i].ClkBypassCntl = 0;
  997. }
  998. }
  999. } else {
  1000. struct sumo_sclk_voltage_mapping_table *table =
  1001. &pi->sys_info.sclk_voltage_mapping_table;
  1002. for (i = 0; i < pi->graphics_dpm_level_count; i++) {
  1003. if (pi->caps_enable_dfs_bypass) {
  1004. if (kv_get_clock_difference(table->entries[i].sclk_frequency, 40000) < 200)
  1005. pi->graphics_level[i].ClkBypassCntl = 3;
  1006. else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 30000) < 200)
  1007. pi->graphics_level[i].ClkBypassCntl = 2;
  1008. else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 26600) < 200)
  1009. pi->graphics_level[i].ClkBypassCntl = 7;
  1010. else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 20000) < 200)
  1011. pi->graphics_level[i].ClkBypassCntl = 6;
  1012. else if (kv_get_clock_difference(table->entries[i].sclk_frequency, 10000) < 200)
  1013. pi->graphics_level[i].ClkBypassCntl = 8;
  1014. else
  1015. pi->graphics_level[i].ClkBypassCntl = 0;
  1016. } else {
  1017. pi->graphics_level[i].ClkBypassCntl = 0;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. static int kv_enable_ulv(struct amdgpu_device *adev, bool enable)
  1023. {
  1024. return amdgpu_kv_notify_message_to_smu(adev, enable ?
  1025. PPSMC_MSG_EnableULV : PPSMC_MSG_DisableULV);
  1026. }
  1027. static void kv_reset_acp_boot_level(struct amdgpu_device *adev)
  1028. {
  1029. struct kv_power_info *pi = kv_get_pi(adev);
  1030. pi->acp_boot_level = 0xff;
  1031. }
  1032. static void kv_update_current_ps(struct amdgpu_device *adev,
  1033. struct amdgpu_ps *rps)
  1034. {
  1035. struct kv_ps *new_ps = kv_get_ps(rps);
  1036. struct kv_power_info *pi = kv_get_pi(adev);
  1037. pi->current_rps = *rps;
  1038. pi->current_ps = *new_ps;
  1039. pi->current_rps.ps_priv = &pi->current_ps;
  1040. }
  1041. static void kv_update_requested_ps(struct amdgpu_device *adev,
  1042. struct amdgpu_ps *rps)
  1043. {
  1044. struct kv_ps *new_ps = kv_get_ps(rps);
  1045. struct kv_power_info *pi = kv_get_pi(adev);
  1046. pi->requested_rps = *rps;
  1047. pi->requested_ps = *new_ps;
  1048. pi->requested_rps.ps_priv = &pi->requested_ps;
  1049. }
  1050. static void kv_dpm_enable_bapm(struct amdgpu_device *adev, bool enable)
  1051. {
  1052. struct kv_power_info *pi = kv_get_pi(adev);
  1053. int ret;
  1054. if (pi->bapm_enable) {
  1055. ret = amdgpu_kv_smc_bapm_enable(adev, enable);
  1056. if (ret)
  1057. DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
  1058. }
  1059. }
  1060. static int kv_dpm_enable(struct amdgpu_device *adev)
  1061. {
  1062. struct kv_power_info *pi = kv_get_pi(adev);
  1063. int ret;
  1064. ret = kv_process_firmware_header(adev);
  1065. if (ret) {
  1066. DRM_ERROR("kv_process_firmware_header failed\n");
  1067. return ret;
  1068. }
  1069. kv_init_fps_limits(adev);
  1070. kv_init_graphics_levels(adev);
  1071. ret = kv_program_bootup_state(adev);
  1072. if (ret) {
  1073. DRM_ERROR("kv_program_bootup_state failed\n");
  1074. return ret;
  1075. }
  1076. kv_calculate_dfs_bypass_settings(adev);
  1077. ret = kv_upload_dpm_settings(adev);
  1078. if (ret) {
  1079. DRM_ERROR("kv_upload_dpm_settings failed\n");
  1080. return ret;
  1081. }
  1082. ret = kv_populate_uvd_table(adev);
  1083. if (ret) {
  1084. DRM_ERROR("kv_populate_uvd_table failed\n");
  1085. return ret;
  1086. }
  1087. ret = kv_populate_vce_table(adev);
  1088. if (ret) {
  1089. DRM_ERROR("kv_populate_vce_table failed\n");
  1090. return ret;
  1091. }
  1092. ret = kv_populate_samu_table(adev);
  1093. if (ret) {
  1094. DRM_ERROR("kv_populate_samu_table failed\n");
  1095. return ret;
  1096. }
  1097. ret = kv_populate_acp_table(adev);
  1098. if (ret) {
  1099. DRM_ERROR("kv_populate_acp_table failed\n");
  1100. return ret;
  1101. }
  1102. kv_program_vc(adev);
  1103. #if 0
  1104. kv_initialize_hardware_cac_manager(adev);
  1105. #endif
  1106. kv_start_am(adev);
  1107. if (pi->enable_auto_thermal_throttling) {
  1108. ret = kv_enable_auto_thermal_throttling(adev);
  1109. if (ret) {
  1110. DRM_ERROR("kv_enable_auto_thermal_throttling failed\n");
  1111. return ret;
  1112. }
  1113. }
  1114. ret = kv_enable_dpm_voltage_scaling(adev);
  1115. if (ret) {
  1116. DRM_ERROR("kv_enable_dpm_voltage_scaling failed\n");
  1117. return ret;
  1118. }
  1119. ret = kv_set_dpm_interval(adev);
  1120. if (ret) {
  1121. DRM_ERROR("kv_set_dpm_interval failed\n");
  1122. return ret;
  1123. }
  1124. ret = kv_set_dpm_boot_state(adev);
  1125. if (ret) {
  1126. DRM_ERROR("kv_set_dpm_boot_state failed\n");
  1127. return ret;
  1128. }
  1129. ret = kv_enable_ulv(adev, true);
  1130. if (ret) {
  1131. DRM_ERROR("kv_enable_ulv failed\n");
  1132. return ret;
  1133. }
  1134. kv_start_dpm(adev);
  1135. ret = kv_enable_didt(adev, true);
  1136. if (ret) {
  1137. DRM_ERROR("kv_enable_didt failed\n");
  1138. return ret;
  1139. }
  1140. ret = kv_enable_smc_cac(adev, true);
  1141. if (ret) {
  1142. DRM_ERROR("kv_enable_smc_cac failed\n");
  1143. return ret;
  1144. }
  1145. kv_reset_acp_boot_level(adev);
  1146. ret = amdgpu_kv_smc_bapm_enable(adev, false);
  1147. if (ret) {
  1148. DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
  1149. return ret;
  1150. }
  1151. kv_update_current_ps(adev, adev->pm.dpm.boot_ps);
  1152. if (adev->irq.installed &&
  1153. amdgpu_is_internal_thermal_sensor(adev->pm.int_thermal_type)) {
  1154. ret = kv_set_thermal_temperature_range(adev, KV_TEMP_RANGE_MIN, KV_TEMP_RANGE_MAX);
  1155. if (ret) {
  1156. DRM_ERROR("kv_set_thermal_temperature_range failed\n");
  1157. return ret;
  1158. }
  1159. amdgpu_irq_get(adev, &adev->pm.dpm.thermal.irq,
  1160. AMDGPU_THERMAL_IRQ_LOW_TO_HIGH);
  1161. amdgpu_irq_get(adev, &adev->pm.dpm.thermal.irq,
  1162. AMDGPU_THERMAL_IRQ_HIGH_TO_LOW);
  1163. }
  1164. return ret;
  1165. }
  1166. static void kv_dpm_disable(struct amdgpu_device *adev)
  1167. {
  1168. amdgpu_irq_put(adev, &adev->pm.dpm.thermal.irq,
  1169. AMDGPU_THERMAL_IRQ_LOW_TO_HIGH);
  1170. amdgpu_irq_put(adev, &adev->pm.dpm.thermal.irq,
  1171. AMDGPU_THERMAL_IRQ_HIGH_TO_LOW);
  1172. amdgpu_kv_smc_bapm_enable(adev, false);
  1173. if (adev->asic_type == CHIP_MULLINS)
  1174. kv_enable_nb_dpm(adev, false);
  1175. /* powerup blocks */
  1176. kv_dpm_powergate_acp(adev, false);
  1177. kv_dpm_powergate_samu(adev, false);
  1178. kv_dpm_powergate_vce(adev, false);
  1179. kv_dpm_powergate_uvd(adev, false);
  1180. kv_enable_smc_cac(adev, false);
  1181. kv_enable_didt(adev, false);
  1182. kv_clear_vc(adev);
  1183. kv_stop_dpm(adev);
  1184. kv_enable_ulv(adev, false);
  1185. kv_reset_am(adev);
  1186. kv_update_current_ps(adev, adev->pm.dpm.boot_ps);
  1187. }
  1188. #if 0
  1189. static int kv_write_smc_soft_register(struct amdgpu_device *adev,
  1190. u16 reg_offset, u32 value)
  1191. {
  1192. struct kv_power_info *pi = kv_get_pi(adev);
  1193. return amdgpu_kv_copy_bytes_to_smc(adev, pi->soft_regs_start + reg_offset,
  1194. (u8 *)&value, sizeof(u16), pi->sram_end);
  1195. }
  1196. static int kv_read_smc_soft_register(struct amdgpu_device *adev,
  1197. u16 reg_offset, u32 *value)
  1198. {
  1199. struct kv_power_info *pi = kv_get_pi(adev);
  1200. return amdgpu_kv_read_smc_sram_dword(adev, pi->soft_regs_start + reg_offset,
  1201. value, pi->sram_end);
  1202. }
  1203. #endif
  1204. static void kv_init_sclk_t(struct amdgpu_device *adev)
  1205. {
  1206. struct kv_power_info *pi = kv_get_pi(adev);
  1207. pi->low_sclk_interrupt_t = 0;
  1208. }
  1209. static int kv_init_fps_limits(struct amdgpu_device *adev)
  1210. {
  1211. struct kv_power_info *pi = kv_get_pi(adev);
  1212. int ret = 0;
  1213. if (pi->caps_fps) {
  1214. u16 tmp;
  1215. tmp = 45;
  1216. pi->fps_high_t = cpu_to_be16(tmp);
  1217. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1218. pi->dpm_table_start +
  1219. offsetof(SMU7_Fusion_DpmTable, FpsHighT),
  1220. (u8 *)&pi->fps_high_t,
  1221. sizeof(u16), pi->sram_end);
  1222. tmp = 30;
  1223. pi->fps_low_t = cpu_to_be16(tmp);
  1224. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1225. pi->dpm_table_start +
  1226. offsetof(SMU7_Fusion_DpmTable, FpsLowT),
  1227. (u8 *)&pi->fps_low_t,
  1228. sizeof(u16), pi->sram_end);
  1229. }
  1230. return ret;
  1231. }
  1232. static void kv_init_powergate_state(struct amdgpu_device *adev)
  1233. {
  1234. struct kv_power_info *pi = kv_get_pi(adev);
  1235. pi->uvd_power_gated = false;
  1236. pi->vce_power_gated = false;
  1237. pi->samu_power_gated = false;
  1238. pi->acp_power_gated = false;
  1239. }
  1240. static int kv_enable_uvd_dpm(struct amdgpu_device *adev, bool enable)
  1241. {
  1242. return amdgpu_kv_notify_message_to_smu(adev, enable ?
  1243. PPSMC_MSG_UVDDPM_Enable : PPSMC_MSG_UVDDPM_Disable);
  1244. }
  1245. static int kv_enable_vce_dpm(struct amdgpu_device *adev, bool enable)
  1246. {
  1247. return amdgpu_kv_notify_message_to_smu(adev, enable ?
  1248. PPSMC_MSG_VCEDPM_Enable : PPSMC_MSG_VCEDPM_Disable);
  1249. }
  1250. static int kv_enable_samu_dpm(struct amdgpu_device *adev, bool enable)
  1251. {
  1252. return amdgpu_kv_notify_message_to_smu(adev, enable ?
  1253. PPSMC_MSG_SAMUDPM_Enable : PPSMC_MSG_SAMUDPM_Disable);
  1254. }
  1255. static int kv_enable_acp_dpm(struct amdgpu_device *adev, bool enable)
  1256. {
  1257. return amdgpu_kv_notify_message_to_smu(adev, enable ?
  1258. PPSMC_MSG_ACPDPM_Enable : PPSMC_MSG_ACPDPM_Disable);
  1259. }
  1260. static int kv_update_uvd_dpm(struct amdgpu_device *adev, bool gate)
  1261. {
  1262. struct kv_power_info *pi = kv_get_pi(adev);
  1263. struct amdgpu_uvd_clock_voltage_dependency_table *table =
  1264. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  1265. int ret;
  1266. u32 mask;
  1267. if (!gate) {
  1268. if (table->count)
  1269. pi->uvd_boot_level = table->count - 1;
  1270. else
  1271. pi->uvd_boot_level = 0;
  1272. if (!pi->caps_uvd_dpm || pi->caps_stable_p_state) {
  1273. mask = 1 << pi->uvd_boot_level;
  1274. } else {
  1275. mask = 0x1f;
  1276. }
  1277. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1278. pi->dpm_table_start +
  1279. offsetof(SMU7_Fusion_DpmTable, UvdBootLevel),
  1280. (uint8_t *)&pi->uvd_boot_level,
  1281. sizeof(u8), pi->sram_end);
  1282. if (ret)
  1283. return ret;
  1284. amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  1285. PPSMC_MSG_UVDDPM_SetEnabledMask,
  1286. mask);
  1287. }
  1288. return kv_enable_uvd_dpm(adev, !gate);
  1289. }
  1290. static u8 kv_get_vce_boot_level(struct amdgpu_device *adev, u32 evclk)
  1291. {
  1292. u8 i;
  1293. struct amdgpu_vce_clock_voltage_dependency_table *table =
  1294. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  1295. for (i = 0; i < table->count; i++) {
  1296. if (table->entries[i].evclk >= evclk)
  1297. break;
  1298. }
  1299. return i;
  1300. }
  1301. static int kv_update_vce_dpm(struct amdgpu_device *adev,
  1302. struct amdgpu_ps *amdgpu_new_state,
  1303. struct amdgpu_ps *amdgpu_current_state)
  1304. {
  1305. struct kv_power_info *pi = kv_get_pi(adev);
  1306. struct amdgpu_vce_clock_voltage_dependency_table *table =
  1307. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  1308. int ret;
  1309. if (amdgpu_new_state->evclk > 0 && amdgpu_current_state->evclk == 0) {
  1310. kv_dpm_powergate_vce(adev, false);
  1311. /* turn the clocks on when encoding */
  1312. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1313. AMD_CG_STATE_UNGATE);
  1314. if (ret)
  1315. return ret;
  1316. if (pi->caps_stable_p_state)
  1317. pi->vce_boot_level = table->count - 1;
  1318. else
  1319. pi->vce_boot_level = kv_get_vce_boot_level(adev, amdgpu_new_state->evclk);
  1320. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1321. pi->dpm_table_start +
  1322. offsetof(SMU7_Fusion_DpmTable, VceBootLevel),
  1323. (u8 *)&pi->vce_boot_level,
  1324. sizeof(u8),
  1325. pi->sram_end);
  1326. if (ret)
  1327. return ret;
  1328. if (pi->caps_stable_p_state)
  1329. amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  1330. PPSMC_MSG_VCEDPM_SetEnabledMask,
  1331. (1 << pi->vce_boot_level));
  1332. kv_enable_vce_dpm(adev, true);
  1333. } else if (amdgpu_new_state->evclk == 0 && amdgpu_current_state->evclk > 0) {
  1334. kv_enable_vce_dpm(adev, false);
  1335. /* turn the clocks off when not encoding */
  1336. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1337. AMD_CG_STATE_GATE);
  1338. if (ret)
  1339. return ret;
  1340. kv_dpm_powergate_vce(adev, true);
  1341. }
  1342. return 0;
  1343. }
  1344. static int kv_update_samu_dpm(struct amdgpu_device *adev, bool gate)
  1345. {
  1346. struct kv_power_info *pi = kv_get_pi(adev);
  1347. struct amdgpu_clock_voltage_dependency_table *table =
  1348. &adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
  1349. int ret;
  1350. if (!gate) {
  1351. if (pi->caps_stable_p_state)
  1352. pi->samu_boot_level = table->count - 1;
  1353. else
  1354. pi->samu_boot_level = 0;
  1355. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1356. pi->dpm_table_start +
  1357. offsetof(SMU7_Fusion_DpmTable, SamuBootLevel),
  1358. (u8 *)&pi->samu_boot_level,
  1359. sizeof(u8),
  1360. pi->sram_end);
  1361. if (ret)
  1362. return ret;
  1363. if (pi->caps_stable_p_state)
  1364. amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  1365. PPSMC_MSG_SAMUDPM_SetEnabledMask,
  1366. (1 << pi->samu_boot_level));
  1367. }
  1368. return kv_enable_samu_dpm(adev, !gate);
  1369. }
  1370. static u8 kv_get_acp_boot_level(struct amdgpu_device *adev)
  1371. {
  1372. u8 i;
  1373. struct amdgpu_clock_voltage_dependency_table *table =
  1374. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  1375. for (i = 0; i < table->count; i++) {
  1376. if (table->entries[i].clk >= 0) /* XXX */
  1377. break;
  1378. }
  1379. if (i >= table->count)
  1380. i = table->count - 1;
  1381. return i;
  1382. }
  1383. static void kv_update_acp_boot_level(struct amdgpu_device *adev)
  1384. {
  1385. struct kv_power_info *pi = kv_get_pi(adev);
  1386. u8 acp_boot_level;
  1387. if (!pi->caps_stable_p_state) {
  1388. acp_boot_level = kv_get_acp_boot_level(adev);
  1389. if (acp_boot_level != pi->acp_boot_level) {
  1390. pi->acp_boot_level = acp_boot_level;
  1391. amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  1392. PPSMC_MSG_ACPDPM_SetEnabledMask,
  1393. (1 << pi->acp_boot_level));
  1394. }
  1395. }
  1396. }
  1397. static int kv_update_acp_dpm(struct amdgpu_device *adev, bool gate)
  1398. {
  1399. struct kv_power_info *pi = kv_get_pi(adev);
  1400. struct amdgpu_clock_voltage_dependency_table *table =
  1401. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  1402. int ret;
  1403. if (!gate) {
  1404. if (pi->caps_stable_p_state)
  1405. pi->acp_boot_level = table->count - 1;
  1406. else
  1407. pi->acp_boot_level = kv_get_acp_boot_level(adev);
  1408. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1409. pi->dpm_table_start +
  1410. offsetof(SMU7_Fusion_DpmTable, AcpBootLevel),
  1411. (u8 *)&pi->acp_boot_level,
  1412. sizeof(u8),
  1413. pi->sram_end);
  1414. if (ret)
  1415. return ret;
  1416. if (pi->caps_stable_p_state)
  1417. amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  1418. PPSMC_MSG_ACPDPM_SetEnabledMask,
  1419. (1 << pi->acp_boot_level));
  1420. }
  1421. return kv_enable_acp_dpm(adev, !gate);
  1422. }
  1423. static void kv_dpm_powergate_uvd(struct amdgpu_device *adev, bool gate)
  1424. {
  1425. struct kv_power_info *pi = kv_get_pi(adev);
  1426. int ret;
  1427. if (pi->uvd_power_gated == gate)
  1428. return;
  1429. pi->uvd_power_gated = gate;
  1430. if (gate) {
  1431. if (pi->caps_uvd_pg) {
  1432. /* disable clockgating so we can properly shut down the block */
  1433. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1434. AMD_CG_STATE_UNGATE);
  1435. /* shutdown the UVD block */
  1436. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1437. AMD_PG_STATE_GATE);
  1438. /* XXX: check for errors */
  1439. }
  1440. kv_update_uvd_dpm(adev, gate);
  1441. if (pi->caps_uvd_pg)
  1442. /* power off the UVD block */
  1443. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_UVDPowerOFF);
  1444. } else {
  1445. if (pi->caps_uvd_pg) {
  1446. /* power on the UVD block */
  1447. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_UVDPowerON);
  1448. /* re-init the UVD block */
  1449. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1450. AMD_PG_STATE_UNGATE);
  1451. /* enable clockgating. hw will dynamically gate/ungate clocks on the fly */
  1452. ret = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1453. AMD_CG_STATE_GATE);
  1454. /* XXX: check for errors */
  1455. }
  1456. kv_update_uvd_dpm(adev, gate);
  1457. }
  1458. }
  1459. static void kv_dpm_powergate_vce(struct amdgpu_device *adev, bool gate)
  1460. {
  1461. struct kv_power_info *pi = kv_get_pi(adev);
  1462. int ret;
  1463. if (pi->vce_power_gated == gate)
  1464. return;
  1465. pi->vce_power_gated = gate;
  1466. if (gate) {
  1467. if (pi->caps_vce_pg) {
  1468. /* shutdown the VCE block */
  1469. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1470. AMD_PG_STATE_GATE);
  1471. /* XXX: check for errors */
  1472. /* power off the VCE block */
  1473. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_VCEPowerOFF);
  1474. }
  1475. } else {
  1476. if (pi->caps_vce_pg) {
  1477. /* power on the VCE block */
  1478. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_VCEPowerON);
  1479. /* re-init the VCE block */
  1480. ret = amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1481. AMD_PG_STATE_UNGATE);
  1482. /* XXX: check for errors */
  1483. }
  1484. }
  1485. }
  1486. static void kv_dpm_powergate_samu(struct amdgpu_device *adev, bool gate)
  1487. {
  1488. struct kv_power_info *pi = kv_get_pi(adev);
  1489. if (pi->samu_power_gated == gate)
  1490. return;
  1491. pi->samu_power_gated = gate;
  1492. if (gate) {
  1493. kv_update_samu_dpm(adev, true);
  1494. if (pi->caps_samu_pg)
  1495. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_SAMPowerOFF);
  1496. } else {
  1497. if (pi->caps_samu_pg)
  1498. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_SAMPowerON);
  1499. kv_update_samu_dpm(adev, false);
  1500. }
  1501. }
  1502. static void kv_dpm_powergate_acp(struct amdgpu_device *adev, bool gate)
  1503. {
  1504. struct kv_power_info *pi = kv_get_pi(adev);
  1505. if (pi->acp_power_gated == gate)
  1506. return;
  1507. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
  1508. return;
  1509. pi->acp_power_gated = gate;
  1510. if (gate) {
  1511. kv_update_acp_dpm(adev, true);
  1512. if (pi->caps_acp_pg)
  1513. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_ACPPowerOFF);
  1514. } else {
  1515. if (pi->caps_acp_pg)
  1516. amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_ACPPowerON);
  1517. kv_update_acp_dpm(adev, false);
  1518. }
  1519. }
  1520. static void kv_set_valid_clock_range(struct amdgpu_device *adev,
  1521. struct amdgpu_ps *new_rps)
  1522. {
  1523. struct kv_ps *new_ps = kv_get_ps(new_rps);
  1524. struct kv_power_info *pi = kv_get_pi(adev);
  1525. u32 i;
  1526. struct amdgpu_clock_voltage_dependency_table *table =
  1527. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  1528. if (table && table->count) {
  1529. for (i = 0; i < pi->graphics_dpm_level_count; i++) {
  1530. if ((table->entries[i].clk >= new_ps->levels[0].sclk) ||
  1531. (i == (pi->graphics_dpm_level_count - 1))) {
  1532. pi->lowest_valid = i;
  1533. break;
  1534. }
  1535. }
  1536. for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
  1537. if (table->entries[i].clk <= new_ps->levels[new_ps->num_levels - 1].sclk)
  1538. break;
  1539. }
  1540. pi->highest_valid = i;
  1541. if (pi->lowest_valid > pi->highest_valid) {
  1542. if ((new_ps->levels[0].sclk - table->entries[pi->highest_valid].clk) >
  1543. (table->entries[pi->lowest_valid].clk - new_ps->levels[new_ps->num_levels - 1].sclk))
  1544. pi->highest_valid = pi->lowest_valid;
  1545. else
  1546. pi->lowest_valid = pi->highest_valid;
  1547. }
  1548. } else {
  1549. struct sumo_sclk_voltage_mapping_table *table =
  1550. &pi->sys_info.sclk_voltage_mapping_table;
  1551. for (i = 0; i < (int)pi->graphics_dpm_level_count; i++) {
  1552. if (table->entries[i].sclk_frequency >= new_ps->levels[0].sclk ||
  1553. i == (int)(pi->graphics_dpm_level_count - 1)) {
  1554. pi->lowest_valid = i;
  1555. break;
  1556. }
  1557. }
  1558. for (i = pi->graphics_dpm_level_count - 1; i > 0; i--) {
  1559. if (table->entries[i].sclk_frequency <=
  1560. new_ps->levels[new_ps->num_levels - 1].sclk)
  1561. break;
  1562. }
  1563. pi->highest_valid = i;
  1564. if (pi->lowest_valid > pi->highest_valid) {
  1565. if ((new_ps->levels[0].sclk -
  1566. table->entries[pi->highest_valid].sclk_frequency) >
  1567. (table->entries[pi->lowest_valid].sclk_frequency -
  1568. new_ps->levels[new_ps->num_levels -1].sclk))
  1569. pi->highest_valid = pi->lowest_valid;
  1570. else
  1571. pi->lowest_valid = pi->highest_valid;
  1572. }
  1573. }
  1574. }
  1575. static int kv_update_dfs_bypass_settings(struct amdgpu_device *adev,
  1576. struct amdgpu_ps *new_rps)
  1577. {
  1578. struct kv_ps *new_ps = kv_get_ps(new_rps);
  1579. struct kv_power_info *pi = kv_get_pi(adev);
  1580. int ret = 0;
  1581. u8 clk_bypass_cntl;
  1582. if (pi->caps_enable_dfs_bypass) {
  1583. clk_bypass_cntl = new_ps->need_dfs_bypass ?
  1584. pi->graphics_level[pi->graphics_boot_level].ClkBypassCntl : 0;
  1585. ret = amdgpu_kv_copy_bytes_to_smc(adev,
  1586. (pi->dpm_table_start +
  1587. offsetof(SMU7_Fusion_DpmTable, GraphicsLevel) +
  1588. (pi->graphics_boot_level * sizeof(SMU7_Fusion_GraphicsLevel)) +
  1589. offsetof(SMU7_Fusion_GraphicsLevel, ClkBypassCntl)),
  1590. &clk_bypass_cntl,
  1591. sizeof(u8), pi->sram_end);
  1592. }
  1593. return ret;
  1594. }
  1595. static int kv_enable_nb_dpm(struct amdgpu_device *adev,
  1596. bool enable)
  1597. {
  1598. struct kv_power_info *pi = kv_get_pi(adev);
  1599. int ret = 0;
  1600. if (enable) {
  1601. if (pi->enable_nb_dpm && !pi->nb_dpm_enabled) {
  1602. ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NBDPM_Enable);
  1603. if (ret == 0)
  1604. pi->nb_dpm_enabled = true;
  1605. }
  1606. } else {
  1607. if (pi->enable_nb_dpm && pi->nb_dpm_enabled) {
  1608. ret = amdgpu_kv_notify_message_to_smu(adev, PPSMC_MSG_NBDPM_Disable);
  1609. if (ret == 0)
  1610. pi->nb_dpm_enabled = false;
  1611. }
  1612. }
  1613. return ret;
  1614. }
  1615. static int kv_dpm_force_performance_level(struct amdgpu_device *adev,
  1616. enum amdgpu_dpm_forced_level level)
  1617. {
  1618. int ret;
  1619. if (level == AMDGPU_DPM_FORCED_LEVEL_HIGH) {
  1620. ret = kv_force_dpm_highest(adev);
  1621. if (ret)
  1622. return ret;
  1623. } else if (level == AMDGPU_DPM_FORCED_LEVEL_LOW) {
  1624. ret = kv_force_dpm_lowest(adev);
  1625. if (ret)
  1626. return ret;
  1627. } else if (level == AMDGPU_DPM_FORCED_LEVEL_AUTO) {
  1628. ret = kv_unforce_levels(adev);
  1629. if (ret)
  1630. return ret;
  1631. }
  1632. adev->pm.dpm.forced_level = level;
  1633. return 0;
  1634. }
  1635. static int kv_dpm_pre_set_power_state(struct amdgpu_device *adev)
  1636. {
  1637. struct kv_power_info *pi = kv_get_pi(adev);
  1638. struct amdgpu_ps requested_ps = *adev->pm.dpm.requested_ps;
  1639. struct amdgpu_ps *new_ps = &requested_ps;
  1640. kv_update_requested_ps(adev, new_ps);
  1641. kv_apply_state_adjust_rules(adev,
  1642. &pi->requested_rps,
  1643. &pi->current_rps);
  1644. return 0;
  1645. }
  1646. static int kv_dpm_set_power_state(struct amdgpu_device *adev)
  1647. {
  1648. struct kv_power_info *pi = kv_get_pi(adev);
  1649. struct amdgpu_ps *new_ps = &pi->requested_rps;
  1650. struct amdgpu_ps *old_ps = &pi->current_rps;
  1651. int ret;
  1652. if (pi->bapm_enable) {
  1653. ret = amdgpu_kv_smc_bapm_enable(adev, adev->pm.dpm.ac_power);
  1654. if (ret) {
  1655. DRM_ERROR("amdgpu_kv_smc_bapm_enable failed\n");
  1656. return ret;
  1657. }
  1658. }
  1659. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
  1660. if (pi->enable_dpm) {
  1661. kv_set_valid_clock_range(adev, new_ps);
  1662. kv_update_dfs_bypass_settings(adev, new_ps);
  1663. ret = kv_calculate_ds_divider(adev);
  1664. if (ret) {
  1665. DRM_ERROR("kv_calculate_ds_divider failed\n");
  1666. return ret;
  1667. }
  1668. kv_calculate_nbps_level_settings(adev);
  1669. kv_calculate_dpm_settings(adev);
  1670. kv_force_lowest_valid(adev);
  1671. kv_enable_new_levels(adev);
  1672. kv_upload_dpm_settings(adev);
  1673. kv_program_nbps_index_settings(adev, new_ps);
  1674. kv_unforce_levels(adev);
  1675. kv_set_enabled_levels(adev);
  1676. kv_force_lowest_valid(adev);
  1677. kv_unforce_levels(adev);
  1678. ret = kv_update_vce_dpm(adev, new_ps, old_ps);
  1679. if (ret) {
  1680. DRM_ERROR("kv_update_vce_dpm failed\n");
  1681. return ret;
  1682. }
  1683. kv_update_sclk_t(adev);
  1684. if (adev->asic_type == CHIP_MULLINS)
  1685. kv_enable_nb_dpm(adev, true);
  1686. }
  1687. } else {
  1688. if (pi->enable_dpm) {
  1689. kv_set_valid_clock_range(adev, new_ps);
  1690. kv_update_dfs_bypass_settings(adev, new_ps);
  1691. ret = kv_calculate_ds_divider(adev);
  1692. if (ret) {
  1693. DRM_ERROR("kv_calculate_ds_divider failed\n");
  1694. return ret;
  1695. }
  1696. kv_calculate_nbps_level_settings(adev);
  1697. kv_calculate_dpm_settings(adev);
  1698. kv_freeze_sclk_dpm(adev, true);
  1699. kv_upload_dpm_settings(adev);
  1700. kv_program_nbps_index_settings(adev, new_ps);
  1701. kv_freeze_sclk_dpm(adev, false);
  1702. kv_set_enabled_levels(adev);
  1703. ret = kv_update_vce_dpm(adev, new_ps, old_ps);
  1704. if (ret) {
  1705. DRM_ERROR("kv_update_vce_dpm failed\n");
  1706. return ret;
  1707. }
  1708. kv_update_acp_boot_level(adev);
  1709. kv_update_sclk_t(adev);
  1710. kv_enable_nb_dpm(adev, true);
  1711. }
  1712. }
  1713. return 0;
  1714. }
  1715. static void kv_dpm_post_set_power_state(struct amdgpu_device *adev)
  1716. {
  1717. struct kv_power_info *pi = kv_get_pi(adev);
  1718. struct amdgpu_ps *new_ps = &pi->requested_rps;
  1719. kv_update_current_ps(adev, new_ps);
  1720. }
  1721. static void kv_dpm_setup_asic(struct amdgpu_device *adev)
  1722. {
  1723. sumo_take_smu_control(adev, true);
  1724. kv_init_powergate_state(adev);
  1725. kv_init_sclk_t(adev);
  1726. }
  1727. #if 0
  1728. static void kv_dpm_reset_asic(struct amdgpu_device *adev)
  1729. {
  1730. struct kv_power_info *pi = kv_get_pi(adev);
  1731. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
  1732. kv_force_lowest_valid(adev);
  1733. kv_init_graphics_levels(adev);
  1734. kv_program_bootup_state(adev);
  1735. kv_upload_dpm_settings(adev);
  1736. kv_force_lowest_valid(adev);
  1737. kv_unforce_levels(adev);
  1738. } else {
  1739. kv_init_graphics_levels(adev);
  1740. kv_program_bootup_state(adev);
  1741. kv_freeze_sclk_dpm(adev, true);
  1742. kv_upload_dpm_settings(adev);
  1743. kv_freeze_sclk_dpm(adev, false);
  1744. kv_set_enabled_level(adev, pi->graphics_boot_level);
  1745. }
  1746. }
  1747. #endif
  1748. static void kv_construct_max_power_limits_table(struct amdgpu_device *adev,
  1749. struct amdgpu_clock_and_voltage_limits *table)
  1750. {
  1751. struct kv_power_info *pi = kv_get_pi(adev);
  1752. if (pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries > 0) {
  1753. int idx = pi->sys_info.sclk_voltage_mapping_table.num_max_dpm_entries - 1;
  1754. table->sclk =
  1755. pi->sys_info.sclk_voltage_mapping_table.entries[idx].sclk_frequency;
  1756. table->vddc =
  1757. kv_convert_2bit_index_to_voltage(adev,
  1758. pi->sys_info.sclk_voltage_mapping_table.entries[idx].vid_2bit);
  1759. }
  1760. table->mclk = pi->sys_info.nbp_memory_clock[0];
  1761. }
  1762. static void kv_patch_voltage_values(struct amdgpu_device *adev)
  1763. {
  1764. int i;
  1765. struct amdgpu_uvd_clock_voltage_dependency_table *uvd_table =
  1766. &adev->pm.dpm.dyn_state.uvd_clock_voltage_dependency_table;
  1767. struct amdgpu_vce_clock_voltage_dependency_table *vce_table =
  1768. &adev->pm.dpm.dyn_state.vce_clock_voltage_dependency_table;
  1769. struct amdgpu_clock_voltage_dependency_table *samu_table =
  1770. &adev->pm.dpm.dyn_state.samu_clock_voltage_dependency_table;
  1771. struct amdgpu_clock_voltage_dependency_table *acp_table =
  1772. &adev->pm.dpm.dyn_state.acp_clock_voltage_dependency_table;
  1773. if (uvd_table->count) {
  1774. for (i = 0; i < uvd_table->count; i++)
  1775. uvd_table->entries[i].v =
  1776. kv_convert_8bit_index_to_voltage(adev,
  1777. uvd_table->entries[i].v);
  1778. }
  1779. if (vce_table->count) {
  1780. for (i = 0; i < vce_table->count; i++)
  1781. vce_table->entries[i].v =
  1782. kv_convert_8bit_index_to_voltage(adev,
  1783. vce_table->entries[i].v);
  1784. }
  1785. if (samu_table->count) {
  1786. for (i = 0; i < samu_table->count; i++)
  1787. samu_table->entries[i].v =
  1788. kv_convert_8bit_index_to_voltage(adev,
  1789. samu_table->entries[i].v);
  1790. }
  1791. if (acp_table->count) {
  1792. for (i = 0; i < acp_table->count; i++)
  1793. acp_table->entries[i].v =
  1794. kv_convert_8bit_index_to_voltage(adev,
  1795. acp_table->entries[i].v);
  1796. }
  1797. }
  1798. static void kv_construct_boot_state(struct amdgpu_device *adev)
  1799. {
  1800. struct kv_power_info *pi = kv_get_pi(adev);
  1801. pi->boot_pl.sclk = pi->sys_info.bootup_sclk;
  1802. pi->boot_pl.vddc_index = pi->sys_info.bootup_nb_voltage_index;
  1803. pi->boot_pl.ds_divider_index = 0;
  1804. pi->boot_pl.ss_divider_index = 0;
  1805. pi->boot_pl.allow_gnb_slow = 1;
  1806. pi->boot_pl.force_nbp_state = 0;
  1807. pi->boot_pl.display_wm = 0;
  1808. pi->boot_pl.vce_wm = 0;
  1809. }
  1810. static int kv_force_dpm_highest(struct amdgpu_device *adev)
  1811. {
  1812. int ret;
  1813. u32 enable_mask, i;
  1814. ret = amdgpu_kv_dpm_get_enable_mask(adev, &enable_mask);
  1815. if (ret)
  1816. return ret;
  1817. for (i = SMU7_MAX_LEVELS_GRAPHICS - 1; i > 0; i--) {
  1818. if (enable_mask & (1 << i))
  1819. break;
  1820. }
  1821. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
  1822. return amdgpu_kv_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DPM_ForceState, i);
  1823. else
  1824. return kv_set_enabled_level(adev, i);
  1825. }
  1826. static int kv_force_dpm_lowest(struct amdgpu_device *adev)
  1827. {
  1828. int ret;
  1829. u32 enable_mask, i;
  1830. ret = amdgpu_kv_dpm_get_enable_mask(adev, &enable_mask);
  1831. if (ret)
  1832. return ret;
  1833. for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
  1834. if (enable_mask & (1 << i))
  1835. break;
  1836. }
  1837. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
  1838. return amdgpu_kv_send_msg_to_smc_with_parameter(adev, PPSMC_MSG_DPM_ForceState, i);
  1839. else
  1840. return kv_set_enabled_level(adev, i);
  1841. }
  1842. static u8 kv_get_sleep_divider_id_from_clock(struct amdgpu_device *adev,
  1843. u32 sclk, u32 min_sclk_in_sr)
  1844. {
  1845. struct kv_power_info *pi = kv_get_pi(adev);
  1846. u32 i;
  1847. u32 temp;
  1848. u32 min = (min_sclk_in_sr > KV_MINIMUM_ENGINE_CLOCK) ?
  1849. min_sclk_in_sr : KV_MINIMUM_ENGINE_CLOCK;
  1850. if (sclk < min)
  1851. return 0;
  1852. if (!pi->caps_sclk_ds)
  1853. return 0;
  1854. for (i = KV_MAX_DEEPSLEEP_DIVIDER_ID; i > 0; i--) {
  1855. temp = sclk / sumo_get_sleep_divider_from_id(i);
  1856. if (temp >= min)
  1857. break;
  1858. }
  1859. return (u8)i;
  1860. }
  1861. static int kv_get_high_voltage_limit(struct amdgpu_device *adev, int *limit)
  1862. {
  1863. struct kv_power_info *pi = kv_get_pi(adev);
  1864. struct amdgpu_clock_voltage_dependency_table *table =
  1865. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  1866. int i;
  1867. if (table && table->count) {
  1868. for (i = table->count - 1; i >= 0; i--) {
  1869. if (pi->high_voltage_t &&
  1870. (kv_convert_8bit_index_to_voltage(adev, table->entries[i].v) <=
  1871. pi->high_voltage_t)) {
  1872. *limit = i;
  1873. return 0;
  1874. }
  1875. }
  1876. } else {
  1877. struct sumo_sclk_voltage_mapping_table *table =
  1878. &pi->sys_info.sclk_voltage_mapping_table;
  1879. for (i = table->num_max_dpm_entries - 1; i >= 0; i--) {
  1880. if (pi->high_voltage_t &&
  1881. (kv_convert_2bit_index_to_voltage(adev, table->entries[i].vid_2bit) <=
  1882. pi->high_voltage_t)) {
  1883. *limit = i;
  1884. return 0;
  1885. }
  1886. }
  1887. }
  1888. *limit = 0;
  1889. return 0;
  1890. }
  1891. static void kv_apply_state_adjust_rules(struct amdgpu_device *adev,
  1892. struct amdgpu_ps *new_rps,
  1893. struct amdgpu_ps *old_rps)
  1894. {
  1895. struct kv_ps *ps = kv_get_ps(new_rps);
  1896. struct kv_power_info *pi = kv_get_pi(adev);
  1897. u32 min_sclk = 10000; /* ??? */
  1898. u32 sclk, mclk = 0;
  1899. int i, limit;
  1900. bool force_high;
  1901. struct amdgpu_clock_voltage_dependency_table *table =
  1902. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  1903. u32 stable_p_state_sclk = 0;
  1904. struct amdgpu_clock_and_voltage_limits *max_limits =
  1905. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  1906. if (new_rps->vce_active) {
  1907. new_rps->evclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].evclk;
  1908. new_rps->ecclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].ecclk;
  1909. } else {
  1910. new_rps->evclk = 0;
  1911. new_rps->ecclk = 0;
  1912. }
  1913. mclk = max_limits->mclk;
  1914. sclk = min_sclk;
  1915. if (pi->caps_stable_p_state) {
  1916. stable_p_state_sclk = (max_limits->sclk * 75) / 100;
  1917. for (i = table->count - 1; i >= 0; i--) {
  1918. if (stable_p_state_sclk >= table->entries[i].clk) {
  1919. stable_p_state_sclk = table->entries[i].clk;
  1920. break;
  1921. }
  1922. }
  1923. if (i > 0)
  1924. stable_p_state_sclk = table->entries[0].clk;
  1925. sclk = stable_p_state_sclk;
  1926. }
  1927. if (new_rps->vce_active) {
  1928. if (sclk < adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].sclk)
  1929. sclk = adev->pm.dpm.vce_states[adev->pm.dpm.vce_level].sclk;
  1930. }
  1931. ps->need_dfs_bypass = true;
  1932. for (i = 0; i < ps->num_levels; i++) {
  1933. if (ps->levels[i].sclk < sclk)
  1934. ps->levels[i].sclk = sclk;
  1935. }
  1936. if (table && table->count) {
  1937. for (i = 0; i < ps->num_levels; i++) {
  1938. if (pi->high_voltage_t &&
  1939. (pi->high_voltage_t <
  1940. kv_convert_8bit_index_to_voltage(adev, ps->levels[i].vddc_index))) {
  1941. kv_get_high_voltage_limit(adev, &limit);
  1942. ps->levels[i].sclk = table->entries[limit].clk;
  1943. }
  1944. }
  1945. } else {
  1946. struct sumo_sclk_voltage_mapping_table *table =
  1947. &pi->sys_info.sclk_voltage_mapping_table;
  1948. for (i = 0; i < ps->num_levels; i++) {
  1949. if (pi->high_voltage_t &&
  1950. (pi->high_voltage_t <
  1951. kv_convert_8bit_index_to_voltage(adev, ps->levels[i].vddc_index))) {
  1952. kv_get_high_voltage_limit(adev, &limit);
  1953. ps->levels[i].sclk = table->entries[limit].sclk_frequency;
  1954. }
  1955. }
  1956. }
  1957. if (pi->caps_stable_p_state) {
  1958. for (i = 0; i < ps->num_levels; i++) {
  1959. ps->levels[i].sclk = stable_p_state_sclk;
  1960. }
  1961. }
  1962. pi->video_start = new_rps->dclk || new_rps->vclk ||
  1963. new_rps->evclk || new_rps->ecclk;
  1964. if ((new_rps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK) ==
  1965. ATOM_PPLIB_CLASSIFICATION_UI_BATTERY)
  1966. pi->battery_state = true;
  1967. else
  1968. pi->battery_state = false;
  1969. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
  1970. ps->dpm0_pg_nb_ps_lo = 0x1;
  1971. ps->dpm0_pg_nb_ps_hi = 0x0;
  1972. ps->dpmx_nb_ps_lo = 0x1;
  1973. ps->dpmx_nb_ps_hi = 0x0;
  1974. } else {
  1975. ps->dpm0_pg_nb_ps_lo = 0x3;
  1976. ps->dpm0_pg_nb_ps_hi = 0x0;
  1977. ps->dpmx_nb_ps_lo = 0x3;
  1978. ps->dpmx_nb_ps_hi = 0x0;
  1979. if (pi->sys_info.nb_dpm_enable) {
  1980. force_high = (mclk >= pi->sys_info.nbp_memory_clock[3]) ||
  1981. pi->video_start || (adev->pm.dpm.new_active_crtc_count >= 3) ||
  1982. pi->disable_nb_ps3_in_battery;
  1983. ps->dpm0_pg_nb_ps_lo = force_high ? 0x2 : 0x3;
  1984. ps->dpm0_pg_nb_ps_hi = 0x2;
  1985. ps->dpmx_nb_ps_lo = force_high ? 0x2 : 0x3;
  1986. ps->dpmx_nb_ps_hi = 0x2;
  1987. }
  1988. }
  1989. }
  1990. static void kv_dpm_power_level_enabled_for_throttle(struct amdgpu_device *adev,
  1991. u32 index, bool enable)
  1992. {
  1993. struct kv_power_info *pi = kv_get_pi(adev);
  1994. pi->graphics_level[index].EnabledForThrottle = enable ? 1 : 0;
  1995. }
  1996. static int kv_calculate_ds_divider(struct amdgpu_device *adev)
  1997. {
  1998. struct kv_power_info *pi = kv_get_pi(adev);
  1999. u32 sclk_in_sr = 10000; /* ??? */
  2000. u32 i;
  2001. if (pi->lowest_valid > pi->highest_valid)
  2002. return -EINVAL;
  2003. for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
  2004. pi->graphics_level[i].DeepSleepDivId =
  2005. kv_get_sleep_divider_id_from_clock(adev,
  2006. be32_to_cpu(pi->graphics_level[i].SclkFrequency),
  2007. sclk_in_sr);
  2008. }
  2009. return 0;
  2010. }
  2011. static int kv_calculate_nbps_level_settings(struct amdgpu_device *adev)
  2012. {
  2013. struct kv_power_info *pi = kv_get_pi(adev);
  2014. u32 i;
  2015. bool force_high;
  2016. struct amdgpu_clock_and_voltage_limits *max_limits =
  2017. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac;
  2018. u32 mclk = max_limits->mclk;
  2019. if (pi->lowest_valid > pi->highest_valid)
  2020. return -EINVAL;
  2021. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS) {
  2022. for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
  2023. pi->graphics_level[i].GnbSlow = 1;
  2024. pi->graphics_level[i].ForceNbPs1 = 0;
  2025. pi->graphics_level[i].UpH = 0;
  2026. }
  2027. if (!pi->sys_info.nb_dpm_enable)
  2028. return 0;
  2029. force_high = ((mclk >= pi->sys_info.nbp_memory_clock[3]) ||
  2030. (adev->pm.dpm.new_active_crtc_count >= 3) || pi->video_start);
  2031. if (force_high) {
  2032. for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
  2033. pi->graphics_level[i].GnbSlow = 0;
  2034. } else {
  2035. if (pi->battery_state)
  2036. pi->graphics_level[0].ForceNbPs1 = 1;
  2037. pi->graphics_level[1].GnbSlow = 0;
  2038. pi->graphics_level[2].GnbSlow = 0;
  2039. pi->graphics_level[3].GnbSlow = 0;
  2040. pi->graphics_level[4].GnbSlow = 0;
  2041. }
  2042. } else {
  2043. for (i = pi->lowest_valid; i <= pi->highest_valid; i++) {
  2044. pi->graphics_level[i].GnbSlow = 1;
  2045. pi->graphics_level[i].ForceNbPs1 = 0;
  2046. pi->graphics_level[i].UpH = 0;
  2047. }
  2048. if (pi->sys_info.nb_dpm_enable && pi->battery_state) {
  2049. pi->graphics_level[pi->lowest_valid].UpH = 0x28;
  2050. pi->graphics_level[pi->lowest_valid].GnbSlow = 0;
  2051. if (pi->lowest_valid != pi->highest_valid)
  2052. pi->graphics_level[pi->lowest_valid].ForceNbPs1 = 1;
  2053. }
  2054. }
  2055. return 0;
  2056. }
  2057. static int kv_calculate_dpm_settings(struct amdgpu_device *adev)
  2058. {
  2059. struct kv_power_info *pi = kv_get_pi(adev);
  2060. u32 i;
  2061. if (pi->lowest_valid > pi->highest_valid)
  2062. return -EINVAL;
  2063. for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
  2064. pi->graphics_level[i].DisplayWatermark = (i == pi->highest_valid) ? 1 : 0;
  2065. return 0;
  2066. }
  2067. static void kv_init_graphics_levels(struct amdgpu_device *adev)
  2068. {
  2069. struct kv_power_info *pi = kv_get_pi(adev);
  2070. u32 i;
  2071. struct amdgpu_clock_voltage_dependency_table *table =
  2072. &adev->pm.dpm.dyn_state.vddc_dependency_on_sclk;
  2073. if (table && table->count) {
  2074. u32 vid_2bit;
  2075. pi->graphics_dpm_level_count = 0;
  2076. for (i = 0; i < table->count; i++) {
  2077. if (pi->high_voltage_t &&
  2078. (pi->high_voltage_t <
  2079. kv_convert_8bit_index_to_voltage(adev, table->entries[i].v)))
  2080. break;
  2081. kv_set_divider_value(adev, i, table->entries[i].clk);
  2082. vid_2bit = kv_convert_vid7_to_vid2(adev,
  2083. &pi->sys_info.vid_mapping_table,
  2084. table->entries[i].v);
  2085. kv_set_vid(adev, i, vid_2bit);
  2086. kv_set_at(adev, i, pi->at[i]);
  2087. kv_dpm_power_level_enabled_for_throttle(adev, i, true);
  2088. pi->graphics_dpm_level_count++;
  2089. }
  2090. } else {
  2091. struct sumo_sclk_voltage_mapping_table *table =
  2092. &pi->sys_info.sclk_voltage_mapping_table;
  2093. pi->graphics_dpm_level_count = 0;
  2094. for (i = 0; i < table->num_max_dpm_entries; i++) {
  2095. if (pi->high_voltage_t &&
  2096. pi->high_voltage_t <
  2097. kv_convert_2bit_index_to_voltage(adev, table->entries[i].vid_2bit))
  2098. break;
  2099. kv_set_divider_value(adev, i, table->entries[i].sclk_frequency);
  2100. kv_set_vid(adev, i, table->entries[i].vid_2bit);
  2101. kv_set_at(adev, i, pi->at[i]);
  2102. kv_dpm_power_level_enabled_for_throttle(adev, i, true);
  2103. pi->graphics_dpm_level_count++;
  2104. }
  2105. }
  2106. for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++)
  2107. kv_dpm_power_level_enable(adev, i, false);
  2108. }
  2109. static void kv_enable_new_levels(struct amdgpu_device *adev)
  2110. {
  2111. struct kv_power_info *pi = kv_get_pi(adev);
  2112. u32 i;
  2113. for (i = 0; i < SMU7_MAX_LEVELS_GRAPHICS; i++) {
  2114. if (i >= pi->lowest_valid && i <= pi->highest_valid)
  2115. kv_dpm_power_level_enable(adev, i, true);
  2116. }
  2117. }
  2118. static int kv_set_enabled_level(struct amdgpu_device *adev, u32 level)
  2119. {
  2120. u32 new_mask = (1 << level);
  2121. return amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  2122. PPSMC_MSG_SCLKDPM_SetEnabledMask,
  2123. new_mask);
  2124. }
  2125. static int kv_set_enabled_levels(struct amdgpu_device *adev)
  2126. {
  2127. struct kv_power_info *pi = kv_get_pi(adev);
  2128. u32 i, new_mask = 0;
  2129. for (i = pi->lowest_valid; i <= pi->highest_valid; i++)
  2130. new_mask |= (1 << i);
  2131. return amdgpu_kv_send_msg_to_smc_with_parameter(adev,
  2132. PPSMC_MSG_SCLKDPM_SetEnabledMask,
  2133. new_mask);
  2134. }
  2135. static void kv_program_nbps_index_settings(struct amdgpu_device *adev,
  2136. struct amdgpu_ps *new_rps)
  2137. {
  2138. struct kv_ps *new_ps = kv_get_ps(new_rps);
  2139. struct kv_power_info *pi = kv_get_pi(adev);
  2140. u32 nbdpmconfig1;
  2141. if (adev->asic_type == CHIP_KABINI || adev->asic_type == CHIP_MULLINS)
  2142. return;
  2143. if (pi->sys_info.nb_dpm_enable) {
  2144. nbdpmconfig1 = RREG32_SMC(ixNB_DPM_CONFIG_1);
  2145. nbdpmconfig1 &= ~(NB_DPM_CONFIG_1__Dpm0PgNbPsLo_MASK |
  2146. NB_DPM_CONFIG_1__Dpm0PgNbPsHi_MASK |
  2147. NB_DPM_CONFIG_1__DpmXNbPsLo_MASK |
  2148. NB_DPM_CONFIG_1__DpmXNbPsHi_MASK);
  2149. nbdpmconfig1 |= (new_ps->dpm0_pg_nb_ps_lo << NB_DPM_CONFIG_1__Dpm0PgNbPsLo__SHIFT) |
  2150. (new_ps->dpm0_pg_nb_ps_hi << NB_DPM_CONFIG_1__Dpm0PgNbPsHi__SHIFT) |
  2151. (new_ps->dpmx_nb_ps_lo << NB_DPM_CONFIG_1__DpmXNbPsLo__SHIFT) |
  2152. (new_ps->dpmx_nb_ps_hi << NB_DPM_CONFIG_1__DpmXNbPsHi__SHIFT);
  2153. WREG32_SMC(ixNB_DPM_CONFIG_1, nbdpmconfig1);
  2154. }
  2155. }
  2156. static int kv_set_thermal_temperature_range(struct amdgpu_device *adev,
  2157. int min_temp, int max_temp)
  2158. {
  2159. int low_temp = 0 * 1000;
  2160. int high_temp = 255 * 1000;
  2161. u32 tmp;
  2162. if (low_temp < min_temp)
  2163. low_temp = min_temp;
  2164. if (high_temp > max_temp)
  2165. high_temp = max_temp;
  2166. if (high_temp < low_temp) {
  2167. DRM_ERROR("invalid thermal range: %d - %d\n", low_temp, high_temp);
  2168. return -EINVAL;
  2169. }
  2170. tmp = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
  2171. tmp &= ~(CG_THERMAL_INT_CTRL__DIG_THERM_INTH_MASK |
  2172. CG_THERMAL_INT_CTRL__DIG_THERM_INTL_MASK);
  2173. tmp |= ((49 + (high_temp / 1000)) << CG_THERMAL_INT_CTRL__DIG_THERM_INTH__SHIFT) |
  2174. ((49 + (low_temp / 1000)) << CG_THERMAL_INT_CTRL__DIG_THERM_INTL__SHIFT);
  2175. WREG32_SMC(ixCG_THERMAL_INT_CTRL, tmp);
  2176. adev->pm.dpm.thermal.min_temp = low_temp;
  2177. adev->pm.dpm.thermal.max_temp = high_temp;
  2178. return 0;
  2179. }
  2180. union igp_info {
  2181. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  2182. struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
  2183. struct _ATOM_INTEGRATED_SYSTEM_INFO_V5 info_5;
  2184. struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
  2185. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  2186. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  2187. };
  2188. static int kv_parse_sys_info_table(struct amdgpu_device *adev)
  2189. {
  2190. struct kv_power_info *pi = kv_get_pi(adev);
  2191. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  2192. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  2193. union igp_info *igp_info;
  2194. u8 frev, crev;
  2195. u16 data_offset;
  2196. int i;
  2197. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  2198. &frev, &crev, &data_offset)) {
  2199. igp_info = (union igp_info *)(mode_info->atom_context->bios +
  2200. data_offset);
  2201. if (crev != 8) {
  2202. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  2203. return -EINVAL;
  2204. }
  2205. pi->sys_info.bootup_sclk = le32_to_cpu(igp_info->info_8.ulBootUpEngineClock);
  2206. pi->sys_info.bootup_uma_clk = le32_to_cpu(igp_info->info_8.ulBootUpUMAClock);
  2207. pi->sys_info.bootup_nb_voltage_index =
  2208. le16_to_cpu(igp_info->info_8.usBootUpNBVoltage);
  2209. if (igp_info->info_8.ucHtcTmpLmt == 0)
  2210. pi->sys_info.htc_tmp_lmt = 203;
  2211. else
  2212. pi->sys_info.htc_tmp_lmt = igp_info->info_8.ucHtcTmpLmt;
  2213. if (igp_info->info_8.ucHtcHystLmt == 0)
  2214. pi->sys_info.htc_hyst_lmt = 5;
  2215. else
  2216. pi->sys_info.htc_hyst_lmt = igp_info->info_8.ucHtcHystLmt;
  2217. if (pi->sys_info.htc_tmp_lmt <= pi->sys_info.htc_hyst_lmt) {
  2218. DRM_ERROR("The htcTmpLmt should be larger than htcHystLmt.\n");
  2219. }
  2220. if (le32_to_cpu(igp_info->info_8.ulSystemConfig) & (1 << 3))
  2221. pi->sys_info.nb_dpm_enable = true;
  2222. else
  2223. pi->sys_info.nb_dpm_enable = false;
  2224. for (i = 0; i < KV_NUM_NBPSTATES; i++) {
  2225. pi->sys_info.nbp_memory_clock[i] =
  2226. le32_to_cpu(igp_info->info_8.ulNbpStateMemclkFreq[i]);
  2227. pi->sys_info.nbp_n_clock[i] =
  2228. le32_to_cpu(igp_info->info_8.ulNbpStateNClkFreq[i]);
  2229. }
  2230. if (le32_to_cpu(igp_info->info_8.ulGPUCapInfo) &
  2231. SYS_INFO_GPUCAPS__ENABEL_DFS_BYPASS)
  2232. pi->caps_enable_dfs_bypass = true;
  2233. sumo_construct_sclk_voltage_mapping_table(adev,
  2234. &pi->sys_info.sclk_voltage_mapping_table,
  2235. igp_info->info_8.sAvail_SCLK);
  2236. sumo_construct_vid_mapping_table(adev,
  2237. &pi->sys_info.vid_mapping_table,
  2238. igp_info->info_8.sAvail_SCLK);
  2239. kv_construct_max_power_limits_table(adev,
  2240. &adev->pm.dpm.dyn_state.max_clock_voltage_on_ac);
  2241. }
  2242. return 0;
  2243. }
  2244. union power_info {
  2245. struct _ATOM_POWERPLAY_INFO info;
  2246. struct _ATOM_POWERPLAY_INFO_V2 info_2;
  2247. struct _ATOM_POWERPLAY_INFO_V3 info_3;
  2248. struct _ATOM_PPLIB_POWERPLAYTABLE pplib;
  2249. struct _ATOM_PPLIB_POWERPLAYTABLE2 pplib2;
  2250. struct _ATOM_PPLIB_POWERPLAYTABLE3 pplib3;
  2251. };
  2252. union pplib_clock_info {
  2253. struct _ATOM_PPLIB_R600_CLOCK_INFO r600;
  2254. struct _ATOM_PPLIB_RS780_CLOCK_INFO rs780;
  2255. struct _ATOM_PPLIB_EVERGREEN_CLOCK_INFO evergreen;
  2256. struct _ATOM_PPLIB_SUMO_CLOCK_INFO sumo;
  2257. };
  2258. union pplib_power_state {
  2259. struct _ATOM_PPLIB_STATE v1;
  2260. struct _ATOM_PPLIB_STATE_V2 v2;
  2261. };
  2262. static void kv_patch_boot_state(struct amdgpu_device *adev,
  2263. struct kv_ps *ps)
  2264. {
  2265. struct kv_power_info *pi = kv_get_pi(adev);
  2266. ps->num_levels = 1;
  2267. ps->levels[0] = pi->boot_pl;
  2268. }
  2269. static void kv_parse_pplib_non_clock_info(struct amdgpu_device *adev,
  2270. struct amdgpu_ps *rps,
  2271. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info,
  2272. u8 table_rev)
  2273. {
  2274. struct kv_ps *ps = kv_get_ps(rps);
  2275. rps->caps = le32_to_cpu(non_clock_info->ulCapsAndSettings);
  2276. rps->class = le16_to_cpu(non_clock_info->usClassification);
  2277. rps->class2 = le16_to_cpu(non_clock_info->usClassification2);
  2278. if (ATOM_PPLIB_NONCLOCKINFO_VER1 < table_rev) {
  2279. rps->vclk = le32_to_cpu(non_clock_info->ulVCLK);
  2280. rps->dclk = le32_to_cpu(non_clock_info->ulDCLK);
  2281. } else {
  2282. rps->vclk = 0;
  2283. rps->dclk = 0;
  2284. }
  2285. if (rps->class & ATOM_PPLIB_CLASSIFICATION_BOOT) {
  2286. adev->pm.dpm.boot_ps = rps;
  2287. kv_patch_boot_state(adev, ps);
  2288. }
  2289. if (rps->class & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
  2290. adev->pm.dpm.uvd_ps = rps;
  2291. }
  2292. static void kv_parse_pplib_clock_info(struct amdgpu_device *adev,
  2293. struct amdgpu_ps *rps, int index,
  2294. union pplib_clock_info *clock_info)
  2295. {
  2296. struct kv_power_info *pi = kv_get_pi(adev);
  2297. struct kv_ps *ps = kv_get_ps(rps);
  2298. struct kv_pl *pl = &ps->levels[index];
  2299. u32 sclk;
  2300. sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
  2301. sclk |= clock_info->sumo.ucEngineClockHigh << 16;
  2302. pl->sclk = sclk;
  2303. pl->vddc_index = clock_info->sumo.vddcIndex;
  2304. ps->num_levels = index + 1;
  2305. if (pi->caps_sclk_ds) {
  2306. pl->ds_divider_index = 5;
  2307. pl->ss_divider_index = 5;
  2308. }
  2309. }
  2310. static int kv_parse_power_table(struct amdgpu_device *adev)
  2311. {
  2312. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  2313. struct _ATOM_PPLIB_NONCLOCK_INFO *non_clock_info;
  2314. union pplib_power_state *power_state;
  2315. int i, j, k, non_clock_array_index, clock_array_index;
  2316. union pplib_clock_info *clock_info;
  2317. struct _StateArray *state_array;
  2318. struct _ClockInfoArray *clock_info_array;
  2319. struct _NonClockInfoArray *non_clock_info_array;
  2320. union power_info *power_info;
  2321. int index = GetIndexIntoMasterTable(DATA, PowerPlayInfo);
  2322. u16 data_offset;
  2323. u8 frev, crev;
  2324. u8 *power_state_offset;
  2325. struct kv_ps *ps;
  2326. if (!amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  2327. &frev, &crev, &data_offset))
  2328. return -EINVAL;
  2329. power_info = (union power_info *)(mode_info->atom_context->bios + data_offset);
  2330. amdgpu_add_thermal_controller(adev);
  2331. state_array = (struct _StateArray *)
  2332. (mode_info->atom_context->bios + data_offset +
  2333. le16_to_cpu(power_info->pplib.usStateArrayOffset));
  2334. clock_info_array = (struct _ClockInfoArray *)
  2335. (mode_info->atom_context->bios + data_offset +
  2336. le16_to_cpu(power_info->pplib.usClockInfoArrayOffset));
  2337. non_clock_info_array = (struct _NonClockInfoArray *)
  2338. (mode_info->atom_context->bios + data_offset +
  2339. le16_to_cpu(power_info->pplib.usNonClockInfoArrayOffset));
  2340. adev->pm.dpm.ps = kzalloc(sizeof(struct amdgpu_ps) *
  2341. state_array->ucNumEntries, GFP_KERNEL);
  2342. if (!adev->pm.dpm.ps)
  2343. return -ENOMEM;
  2344. power_state_offset = (u8 *)state_array->states;
  2345. for (i = 0; i < state_array->ucNumEntries; i++) {
  2346. u8 *idx;
  2347. power_state = (union pplib_power_state *)power_state_offset;
  2348. non_clock_array_index = power_state->v2.nonClockInfoIndex;
  2349. non_clock_info = (struct _ATOM_PPLIB_NONCLOCK_INFO *)
  2350. &non_clock_info_array->nonClockInfo[non_clock_array_index];
  2351. ps = kzalloc(sizeof(struct kv_ps), GFP_KERNEL);
  2352. if (ps == NULL) {
  2353. kfree(adev->pm.dpm.ps);
  2354. return -ENOMEM;
  2355. }
  2356. adev->pm.dpm.ps[i].ps_priv = ps;
  2357. k = 0;
  2358. idx = (u8 *)&power_state->v2.clockInfoIndex[0];
  2359. for (j = 0; j < power_state->v2.ucNumDPMLevels; j++) {
  2360. clock_array_index = idx[j];
  2361. if (clock_array_index >= clock_info_array->ucNumEntries)
  2362. continue;
  2363. if (k >= SUMO_MAX_HARDWARE_POWERLEVELS)
  2364. break;
  2365. clock_info = (union pplib_clock_info *)
  2366. ((u8 *)&clock_info_array->clockInfo[0] +
  2367. (clock_array_index * clock_info_array->ucEntrySize));
  2368. kv_parse_pplib_clock_info(adev,
  2369. &adev->pm.dpm.ps[i], k,
  2370. clock_info);
  2371. k++;
  2372. }
  2373. kv_parse_pplib_non_clock_info(adev, &adev->pm.dpm.ps[i],
  2374. non_clock_info,
  2375. non_clock_info_array->ucEntrySize);
  2376. power_state_offset += 2 + power_state->v2.ucNumDPMLevels;
  2377. }
  2378. adev->pm.dpm.num_ps = state_array->ucNumEntries;
  2379. /* fill in the vce power states */
  2380. for (i = 0; i < AMDGPU_MAX_VCE_LEVELS; i++) {
  2381. u32 sclk;
  2382. clock_array_index = adev->pm.dpm.vce_states[i].clk_idx;
  2383. clock_info = (union pplib_clock_info *)
  2384. &clock_info_array->clockInfo[clock_array_index * clock_info_array->ucEntrySize];
  2385. sclk = le16_to_cpu(clock_info->sumo.usEngineClockLow);
  2386. sclk |= clock_info->sumo.ucEngineClockHigh << 16;
  2387. adev->pm.dpm.vce_states[i].sclk = sclk;
  2388. adev->pm.dpm.vce_states[i].mclk = 0;
  2389. }
  2390. return 0;
  2391. }
  2392. static int kv_dpm_init(struct amdgpu_device *adev)
  2393. {
  2394. struct kv_power_info *pi;
  2395. int ret, i;
  2396. pi = kzalloc(sizeof(struct kv_power_info), GFP_KERNEL);
  2397. if (pi == NULL)
  2398. return -ENOMEM;
  2399. adev->pm.dpm.priv = pi;
  2400. ret = amdgpu_get_platform_caps(adev);
  2401. if (ret)
  2402. return ret;
  2403. ret = amdgpu_parse_extended_power_table(adev);
  2404. if (ret)
  2405. return ret;
  2406. for (i = 0; i < SUMO_MAX_HARDWARE_POWERLEVELS; i++)
  2407. pi->at[i] = TRINITY_AT_DFLT;
  2408. pi->sram_end = SMC_RAM_END;
  2409. pi->enable_nb_dpm = true;
  2410. pi->caps_power_containment = true;
  2411. pi->caps_cac = true;
  2412. pi->enable_didt = false;
  2413. if (pi->enable_didt) {
  2414. pi->caps_sq_ramping = true;
  2415. pi->caps_db_ramping = true;
  2416. pi->caps_td_ramping = true;
  2417. pi->caps_tcp_ramping = true;
  2418. }
  2419. pi->caps_sclk_ds = true;
  2420. pi->enable_auto_thermal_throttling = true;
  2421. pi->disable_nb_ps3_in_battery = false;
  2422. if (amdgpu_bapm == 0)
  2423. pi->bapm_enable = false;
  2424. else
  2425. pi->bapm_enable = true;
  2426. pi->voltage_drop_t = 0;
  2427. pi->caps_sclk_throttle_low_notification = false;
  2428. pi->caps_fps = false; /* true? */
  2429. pi->caps_uvd_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_UVD) ? true : false;
  2430. pi->caps_uvd_dpm = true;
  2431. pi->caps_vce_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_VCE) ? true : false;
  2432. pi->caps_samu_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_SAMU) ? true : false;
  2433. pi->caps_acp_pg = (adev->pg_flags & AMDGPU_PG_SUPPORT_ACP) ? true : false;
  2434. pi->caps_stable_p_state = false;
  2435. ret = kv_parse_sys_info_table(adev);
  2436. if (ret)
  2437. return ret;
  2438. kv_patch_voltage_values(adev);
  2439. kv_construct_boot_state(adev);
  2440. ret = kv_parse_power_table(adev);
  2441. if (ret)
  2442. return ret;
  2443. pi->enable_dpm = true;
  2444. return 0;
  2445. }
  2446. static void
  2447. kv_dpm_debugfs_print_current_performance_level(struct amdgpu_device *adev,
  2448. struct seq_file *m)
  2449. {
  2450. struct kv_power_info *pi = kv_get_pi(adev);
  2451. u32 current_index =
  2452. (RREG32_SMC(ixTARGET_AND_CURRENT_PROFILE_INDEX) &
  2453. TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX_MASK) >>
  2454. TARGET_AND_CURRENT_PROFILE_INDEX__CURR_SCLK_INDEX__SHIFT;
  2455. u32 sclk, tmp;
  2456. u16 vddc;
  2457. if (current_index >= SMU__NUM_SCLK_DPM_STATE) {
  2458. seq_printf(m, "invalid dpm profile %d\n", current_index);
  2459. } else {
  2460. sclk = be32_to_cpu(pi->graphics_level[current_index].SclkFrequency);
  2461. tmp = (RREG32_SMC(ixSMU_VOLTAGE_STATUS) &
  2462. SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL_MASK) >>
  2463. SMU_VOLTAGE_STATUS__SMU_VOLTAGE_CURRENT_LEVEL__SHIFT;
  2464. vddc = kv_convert_8bit_index_to_voltage(adev, (u16)tmp);
  2465. seq_printf(m, "uvd %sabled\n", pi->uvd_power_gated ? "dis" : "en");
  2466. seq_printf(m, "vce %sabled\n", pi->vce_power_gated ? "dis" : "en");
  2467. seq_printf(m, "power level %d sclk: %u vddc: %u\n",
  2468. current_index, sclk, vddc);
  2469. }
  2470. }
  2471. static void
  2472. kv_dpm_print_power_state(struct amdgpu_device *adev,
  2473. struct amdgpu_ps *rps)
  2474. {
  2475. int i;
  2476. struct kv_ps *ps = kv_get_ps(rps);
  2477. amdgpu_dpm_print_class_info(rps->class, rps->class2);
  2478. amdgpu_dpm_print_cap_info(rps->caps);
  2479. printk("\tuvd vclk: %d dclk: %d\n", rps->vclk, rps->dclk);
  2480. for (i = 0; i < ps->num_levels; i++) {
  2481. struct kv_pl *pl = &ps->levels[i];
  2482. printk("\t\tpower level %d sclk: %u vddc: %u\n",
  2483. i, pl->sclk,
  2484. kv_convert_8bit_index_to_voltage(adev, pl->vddc_index));
  2485. }
  2486. amdgpu_dpm_print_ps_status(adev, rps);
  2487. }
  2488. static void kv_dpm_fini(struct amdgpu_device *adev)
  2489. {
  2490. int i;
  2491. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  2492. kfree(adev->pm.dpm.ps[i].ps_priv);
  2493. }
  2494. kfree(adev->pm.dpm.ps);
  2495. kfree(adev->pm.dpm.priv);
  2496. amdgpu_free_extended_power_table(adev);
  2497. }
  2498. static void kv_dpm_display_configuration_changed(struct amdgpu_device *adev)
  2499. {
  2500. }
  2501. static u32 kv_dpm_get_sclk(struct amdgpu_device *adev, bool low)
  2502. {
  2503. struct kv_power_info *pi = kv_get_pi(adev);
  2504. struct kv_ps *requested_state = kv_get_ps(&pi->requested_rps);
  2505. if (low)
  2506. return requested_state->levels[0].sclk;
  2507. else
  2508. return requested_state->levels[requested_state->num_levels - 1].sclk;
  2509. }
  2510. static u32 kv_dpm_get_mclk(struct amdgpu_device *adev, bool low)
  2511. {
  2512. struct kv_power_info *pi = kv_get_pi(adev);
  2513. return pi->sys_info.bootup_uma_clk;
  2514. }
  2515. /* get temperature in millidegrees */
  2516. static int kv_dpm_get_temp(struct amdgpu_device *adev)
  2517. {
  2518. u32 temp;
  2519. int actual_temp = 0;
  2520. temp = RREG32_SMC(0xC0300E0C);
  2521. if (temp)
  2522. actual_temp = (temp / 8) - 49;
  2523. else
  2524. actual_temp = 0;
  2525. actual_temp = actual_temp * 1000;
  2526. return actual_temp;
  2527. }
  2528. static int kv_dpm_early_init(void *handle)
  2529. {
  2530. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2531. kv_dpm_set_dpm_funcs(adev);
  2532. kv_dpm_set_irq_funcs(adev);
  2533. return 0;
  2534. }
  2535. static int kv_dpm_late_init(void *handle)
  2536. {
  2537. /* powerdown unused blocks for now */
  2538. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2539. int ret;
  2540. if (!amdgpu_dpm)
  2541. return 0;
  2542. /* init the sysfs and debugfs files late */
  2543. ret = amdgpu_pm_sysfs_init(adev);
  2544. if (ret)
  2545. return ret;
  2546. kv_dpm_powergate_acp(adev, true);
  2547. kv_dpm_powergate_samu(adev, true);
  2548. kv_dpm_powergate_vce(adev, true);
  2549. kv_dpm_powergate_uvd(adev, true);
  2550. return 0;
  2551. }
  2552. static int kv_dpm_sw_init(void *handle)
  2553. {
  2554. int ret;
  2555. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2556. ret = amdgpu_irq_add_id(adev, 230, &adev->pm.dpm.thermal.irq);
  2557. if (ret)
  2558. return ret;
  2559. ret = amdgpu_irq_add_id(adev, 231, &adev->pm.dpm.thermal.irq);
  2560. if (ret)
  2561. return ret;
  2562. /* default to balanced state */
  2563. adev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
  2564. adev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
  2565. adev->pm.dpm.forced_level = AMDGPU_DPM_FORCED_LEVEL_AUTO;
  2566. adev->pm.default_sclk = adev->clock.default_sclk;
  2567. adev->pm.default_mclk = adev->clock.default_mclk;
  2568. adev->pm.current_sclk = adev->clock.default_sclk;
  2569. adev->pm.current_mclk = adev->clock.default_mclk;
  2570. adev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  2571. if (amdgpu_dpm == 0)
  2572. return 0;
  2573. INIT_WORK(&adev->pm.dpm.thermal.work, amdgpu_dpm_thermal_work_handler);
  2574. mutex_lock(&adev->pm.mutex);
  2575. ret = kv_dpm_init(adev);
  2576. if (ret)
  2577. goto dpm_failed;
  2578. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
  2579. if (amdgpu_dpm == 1)
  2580. amdgpu_pm_print_power_states(adev);
  2581. mutex_unlock(&adev->pm.mutex);
  2582. DRM_INFO("amdgpu: dpm initialized\n");
  2583. return 0;
  2584. dpm_failed:
  2585. kv_dpm_fini(adev);
  2586. mutex_unlock(&adev->pm.mutex);
  2587. DRM_ERROR("amdgpu: dpm initialization failed\n");
  2588. return ret;
  2589. }
  2590. static int kv_dpm_sw_fini(void *handle)
  2591. {
  2592. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2593. mutex_lock(&adev->pm.mutex);
  2594. amdgpu_pm_sysfs_fini(adev);
  2595. kv_dpm_fini(adev);
  2596. mutex_unlock(&adev->pm.mutex);
  2597. return 0;
  2598. }
  2599. static int kv_dpm_hw_init(void *handle)
  2600. {
  2601. int ret;
  2602. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2603. mutex_lock(&adev->pm.mutex);
  2604. kv_dpm_setup_asic(adev);
  2605. ret = kv_dpm_enable(adev);
  2606. if (ret)
  2607. adev->pm.dpm_enabled = false;
  2608. else
  2609. adev->pm.dpm_enabled = true;
  2610. mutex_unlock(&adev->pm.mutex);
  2611. return ret;
  2612. }
  2613. static int kv_dpm_hw_fini(void *handle)
  2614. {
  2615. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2616. if (adev->pm.dpm_enabled) {
  2617. mutex_lock(&adev->pm.mutex);
  2618. kv_dpm_disable(adev);
  2619. mutex_unlock(&adev->pm.mutex);
  2620. }
  2621. return 0;
  2622. }
  2623. static int kv_dpm_suspend(void *handle)
  2624. {
  2625. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2626. if (adev->pm.dpm_enabled) {
  2627. mutex_lock(&adev->pm.mutex);
  2628. /* disable dpm */
  2629. kv_dpm_disable(adev);
  2630. /* reset the power state */
  2631. adev->pm.dpm.current_ps = adev->pm.dpm.requested_ps = adev->pm.dpm.boot_ps;
  2632. mutex_unlock(&adev->pm.mutex);
  2633. }
  2634. return 0;
  2635. }
  2636. static int kv_dpm_resume(void *handle)
  2637. {
  2638. int ret;
  2639. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2640. if (adev->pm.dpm_enabled) {
  2641. /* asic init will reset to the boot state */
  2642. mutex_lock(&adev->pm.mutex);
  2643. kv_dpm_setup_asic(adev);
  2644. ret = kv_dpm_enable(adev);
  2645. if (ret)
  2646. adev->pm.dpm_enabled = false;
  2647. else
  2648. adev->pm.dpm_enabled = true;
  2649. mutex_unlock(&adev->pm.mutex);
  2650. if (adev->pm.dpm_enabled)
  2651. amdgpu_pm_compute_clocks(adev);
  2652. }
  2653. return 0;
  2654. }
  2655. static bool kv_dpm_is_idle(void *handle)
  2656. {
  2657. return true;
  2658. }
  2659. static int kv_dpm_wait_for_idle(void *handle)
  2660. {
  2661. return 0;
  2662. }
  2663. static void kv_dpm_print_status(void *handle)
  2664. {
  2665. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  2666. dev_info(adev->dev, "KV/KB DPM registers\n");
  2667. dev_info(adev->dev, " DIDT_SQ_CTRL0=0x%08X\n",
  2668. RREG32_DIDT(ixDIDT_SQ_CTRL0));
  2669. dev_info(adev->dev, " DIDT_DB_CTRL0=0x%08X\n",
  2670. RREG32_DIDT(ixDIDT_DB_CTRL0));
  2671. dev_info(adev->dev, " DIDT_TD_CTRL0=0x%08X\n",
  2672. RREG32_DIDT(ixDIDT_TD_CTRL0));
  2673. dev_info(adev->dev, " DIDT_TCP_CTRL0=0x%08X\n",
  2674. RREG32_DIDT(ixDIDT_TCP_CTRL0));
  2675. dev_info(adev->dev, " LCAC_SX0_OVR_SEL=0x%08X\n",
  2676. RREG32_SMC(ixLCAC_SX0_OVR_SEL));
  2677. dev_info(adev->dev, " LCAC_SX0_OVR_VAL=0x%08X\n",
  2678. RREG32_SMC(ixLCAC_SX0_OVR_VAL));
  2679. dev_info(adev->dev, " LCAC_MC0_OVR_SEL=0x%08X\n",
  2680. RREG32_SMC(ixLCAC_MC0_OVR_SEL));
  2681. dev_info(adev->dev, " LCAC_MC0_OVR_VAL=0x%08X\n",
  2682. RREG32_SMC(ixLCAC_MC0_OVR_VAL));
  2683. dev_info(adev->dev, " LCAC_MC1_OVR_SEL=0x%08X\n",
  2684. RREG32_SMC(ixLCAC_MC1_OVR_SEL));
  2685. dev_info(adev->dev, " LCAC_MC1_OVR_VAL=0x%08X\n",
  2686. RREG32_SMC(ixLCAC_MC1_OVR_VAL));
  2687. dev_info(adev->dev, " LCAC_MC2_OVR_SEL=0x%08X\n",
  2688. RREG32_SMC(ixLCAC_MC2_OVR_SEL));
  2689. dev_info(adev->dev, " LCAC_MC2_OVR_VAL=0x%08X\n",
  2690. RREG32_SMC(ixLCAC_MC2_OVR_VAL));
  2691. dev_info(adev->dev, " LCAC_MC3_OVR_SEL=0x%08X\n",
  2692. RREG32_SMC(ixLCAC_MC3_OVR_SEL));
  2693. dev_info(adev->dev, " LCAC_MC3_OVR_VAL=0x%08X\n",
  2694. RREG32_SMC(ixLCAC_MC3_OVR_VAL));
  2695. dev_info(adev->dev, " LCAC_CPL_OVR_SEL=0x%08X\n",
  2696. RREG32_SMC(ixLCAC_CPL_OVR_SEL));
  2697. dev_info(adev->dev, " LCAC_CPL_OVR_VAL=0x%08X\n",
  2698. RREG32_SMC(ixLCAC_CPL_OVR_VAL));
  2699. dev_info(adev->dev, " CG_FREQ_TRAN_VOTING_0=0x%08X\n",
  2700. RREG32_SMC(ixCG_FREQ_TRAN_VOTING_0));
  2701. dev_info(adev->dev, " GENERAL_PWRMGT=0x%08X\n",
  2702. RREG32_SMC(ixGENERAL_PWRMGT));
  2703. dev_info(adev->dev, " SCLK_PWRMGT_CNTL=0x%08X\n",
  2704. RREG32_SMC(ixSCLK_PWRMGT_CNTL));
  2705. dev_info(adev->dev, " SMC_MESSAGE_0=0x%08X\n",
  2706. RREG32(mmSMC_MESSAGE_0));
  2707. dev_info(adev->dev, " SMC_RESP_0=0x%08X\n",
  2708. RREG32(mmSMC_RESP_0));
  2709. dev_info(adev->dev, " SMC_MSG_ARG_0=0x%08X\n",
  2710. RREG32(mmSMC_MSG_ARG_0));
  2711. dev_info(adev->dev, " SMC_IND_INDEX_0=0x%08X\n",
  2712. RREG32(mmSMC_IND_INDEX_0));
  2713. dev_info(adev->dev, " SMC_IND_DATA_0=0x%08X\n",
  2714. RREG32(mmSMC_IND_DATA_0));
  2715. dev_info(adev->dev, " SMC_IND_ACCESS_CNTL=0x%08X\n",
  2716. RREG32(mmSMC_IND_ACCESS_CNTL));
  2717. }
  2718. static int kv_dpm_soft_reset(void *handle)
  2719. {
  2720. return 0;
  2721. }
  2722. static int kv_dpm_set_interrupt_state(struct amdgpu_device *adev,
  2723. struct amdgpu_irq_src *src,
  2724. unsigned type,
  2725. enum amdgpu_interrupt_state state)
  2726. {
  2727. u32 cg_thermal_int;
  2728. switch (type) {
  2729. case AMDGPU_THERMAL_IRQ_LOW_TO_HIGH:
  2730. switch (state) {
  2731. case AMDGPU_IRQ_STATE_DISABLE:
  2732. cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
  2733. cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
  2734. WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
  2735. break;
  2736. case AMDGPU_IRQ_STATE_ENABLE:
  2737. cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
  2738. cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTH_MASK_MASK;
  2739. WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
  2740. break;
  2741. default:
  2742. break;
  2743. }
  2744. break;
  2745. case AMDGPU_THERMAL_IRQ_HIGH_TO_LOW:
  2746. switch (state) {
  2747. case AMDGPU_IRQ_STATE_DISABLE:
  2748. cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
  2749. cg_thermal_int &= ~CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
  2750. WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
  2751. break;
  2752. case AMDGPU_IRQ_STATE_ENABLE:
  2753. cg_thermal_int = RREG32_SMC(ixCG_THERMAL_INT_CTRL);
  2754. cg_thermal_int |= CG_THERMAL_INT_CTRL__THERM_INTL_MASK_MASK;
  2755. WREG32_SMC(ixCG_THERMAL_INT_CTRL, cg_thermal_int);
  2756. break;
  2757. default:
  2758. break;
  2759. }
  2760. break;
  2761. default:
  2762. break;
  2763. }
  2764. return 0;
  2765. }
  2766. static int kv_dpm_process_interrupt(struct amdgpu_device *adev,
  2767. struct amdgpu_irq_src *source,
  2768. struct amdgpu_iv_entry *entry)
  2769. {
  2770. bool queue_thermal = false;
  2771. if (entry == NULL)
  2772. return -EINVAL;
  2773. switch (entry->src_id) {
  2774. case 230: /* thermal low to high */
  2775. DRM_DEBUG("IH: thermal low to high\n");
  2776. adev->pm.dpm.thermal.high_to_low = false;
  2777. queue_thermal = true;
  2778. break;
  2779. case 231: /* thermal high to low */
  2780. DRM_DEBUG("IH: thermal high to low\n");
  2781. adev->pm.dpm.thermal.high_to_low = true;
  2782. queue_thermal = true;
  2783. break;
  2784. default:
  2785. break;
  2786. }
  2787. if (queue_thermal)
  2788. schedule_work(&adev->pm.dpm.thermal.work);
  2789. return 0;
  2790. }
  2791. static int kv_dpm_set_clockgating_state(void *handle,
  2792. enum amd_clockgating_state state)
  2793. {
  2794. return 0;
  2795. }
  2796. static int kv_dpm_set_powergating_state(void *handle,
  2797. enum amd_powergating_state state)
  2798. {
  2799. return 0;
  2800. }
  2801. const struct amd_ip_funcs kv_dpm_ip_funcs = {
  2802. .early_init = kv_dpm_early_init,
  2803. .late_init = kv_dpm_late_init,
  2804. .sw_init = kv_dpm_sw_init,
  2805. .sw_fini = kv_dpm_sw_fini,
  2806. .hw_init = kv_dpm_hw_init,
  2807. .hw_fini = kv_dpm_hw_fini,
  2808. .suspend = kv_dpm_suspend,
  2809. .resume = kv_dpm_resume,
  2810. .is_idle = kv_dpm_is_idle,
  2811. .wait_for_idle = kv_dpm_wait_for_idle,
  2812. .soft_reset = kv_dpm_soft_reset,
  2813. .print_status = kv_dpm_print_status,
  2814. .set_clockgating_state = kv_dpm_set_clockgating_state,
  2815. .set_powergating_state = kv_dpm_set_powergating_state,
  2816. };
  2817. static const struct amdgpu_dpm_funcs kv_dpm_funcs = {
  2818. .get_temperature = &kv_dpm_get_temp,
  2819. .pre_set_power_state = &kv_dpm_pre_set_power_state,
  2820. .set_power_state = &kv_dpm_set_power_state,
  2821. .post_set_power_state = &kv_dpm_post_set_power_state,
  2822. .display_configuration_changed = &kv_dpm_display_configuration_changed,
  2823. .get_sclk = &kv_dpm_get_sclk,
  2824. .get_mclk = &kv_dpm_get_mclk,
  2825. .print_power_state = &kv_dpm_print_power_state,
  2826. .debugfs_print_current_performance_level = &kv_dpm_debugfs_print_current_performance_level,
  2827. .force_performance_level = &kv_dpm_force_performance_level,
  2828. .powergate_uvd = &kv_dpm_powergate_uvd,
  2829. .enable_bapm = &kv_dpm_enable_bapm,
  2830. };
  2831. static void kv_dpm_set_dpm_funcs(struct amdgpu_device *adev)
  2832. {
  2833. if (adev->pm.funcs == NULL)
  2834. adev->pm.funcs = &kv_dpm_funcs;
  2835. }
  2836. static const struct amdgpu_irq_src_funcs kv_dpm_irq_funcs = {
  2837. .set = kv_dpm_set_interrupt_state,
  2838. .process = kv_dpm_process_interrupt,
  2839. };
  2840. static void kv_dpm_set_irq_funcs(struct amdgpu_device *adev)
  2841. {
  2842. adev->pm.dpm.thermal.irq.num_types = AMDGPU_THERMAL_IRQ_LAST;
  2843. adev->pm.dpm.thermal.irq.funcs = &kv_dpm_irq_funcs;
  2844. }