iceland_smumgr.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  1. /*
  2. * Copyright 2016 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. * Author: Huang Rui <ray.huang@amd.com>
  23. *
  24. */
  25. #include "pp_debug.h"
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/gfp.h>
  30. #include "smumgr.h"
  31. #include "iceland_smumgr.h"
  32. #include "ppsmc.h"
  33. #include "cgs_common.h"
  34. #include "smu7_dyn_defaults.h"
  35. #include "smu7_hwmgr.h"
  36. #include "hardwaremanager.h"
  37. #include "ppatomctrl.h"
  38. #include "atombios.h"
  39. #include "pppcielanes.h"
  40. #include "pp_endian.h"
  41. #include "processpptables.h"
  42. #include "smu/smu_7_1_1_d.h"
  43. #include "smu/smu_7_1_1_sh_mask.h"
  44. #include "smu71_discrete.h"
  45. #include "smu_ucode_xfer_vi.h"
  46. #include "gmc/gmc_8_1_d.h"
  47. #include "gmc/gmc_8_1_sh_mask.h"
  48. #include "bif/bif_5_0_d.h"
  49. #include "bif/bif_5_0_sh_mask.h"
  50. #include "dce/dce_10_0_d.h"
  51. #include "dce/dce_10_0_sh_mask.h"
  52. #define ICELAND_SMC_SIZE 0x20000
  53. #define POWERTUNE_DEFAULT_SET_MAX 1
  54. #define MC_CG_ARB_FREQ_F1 0x0b
  55. #define VDDC_VDDCI_DELTA 200
  56. #define DEVICE_ID_VI_ICELAND_M_6900 0x6900
  57. #define DEVICE_ID_VI_ICELAND_M_6901 0x6901
  58. #define DEVICE_ID_VI_ICELAND_M_6902 0x6902
  59. #define DEVICE_ID_VI_ICELAND_M_6903 0x6903
  60. static const struct iceland_pt_defaults defaults_iceland = {
  61. /*
  62. * sviLoadLIneEn, SviLoadLineVddC, TDC_VDDC_ThrottleReleaseLimitPerc,
  63. * TDC_MAWt, TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac, BAPM_TEMP_GRADIENT
  64. */
  65. 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0xB0000,
  66. { 0x79, 0x253, 0x25D, 0xAE, 0x72, 0x80, 0x83, 0x86, 0x6F, 0xC8, 0xC9, 0xC9, 0x2F, 0x4D, 0x61 },
  67. { 0x17C, 0x172, 0x180, 0x1BC, 0x1B3, 0x1BD, 0x206, 0x200, 0x203, 0x25D, 0x25A, 0x255, 0x2C3, 0x2C5, 0x2B4 }
  68. };
  69. /* 35W - XT, XTL */
  70. static const struct iceland_pt_defaults defaults_icelandxt = {
  71. /*
  72. * sviLoadLIneEn, SviLoadLineVddC,
  73. * TDC_VDDC_ThrottleReleaseLimitPerc, TDC_MAWt,
  74. * TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac,
  75. * BAPM_TEMP_GRADIENT
  76. */
  77. 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x0,
  78. { 0xA7, 0x0, 0x0, 0xB5, 0x0, 0x0, 0x9F, 0x0, 0x0, 0xD6, 0x0, 0x0, 0xD7, 0x0, 0x0},
  79. { 0x1EA, 0x0, 0x0, 0x224, 0x0, 0x0, 0x25E, 0x0, 0x0, 0x28E, 0x0, 0x0, 0x2AB, 0x0, 0x0}
  80. };
  81. /* 25W - PRO, LE */
  82. static const struct iceland_pt_defaults defaults_icelandpro = {
  83. /*
  84. * sviLoadLIneEn, SviLoadLineVddC,
  85. * TDC_VDDC_ThrottleReleaseLimitPerc, TDC_MAWt,
  86. * TdcWaterfallCtl, DTEAmbientTempBase, DisplayCac,
  87. * BAPM_TEMP_GRADIENT
  88. */
  89. 1, 0xF, 0xFD, 0x19, 5, 45, 0, 0x0,
  90. { 0xB7, 0x0, 0x0, 0xC3, 0x0, 0x0, 0xB5, 0x0, 0x0, 0xEA, 0x0, 0x0, 0xE6, 0x0, 0x0},
  91. { 0x1EA, 0x0, 0x0, 0x224, 0x0, 0x0, 0x25E, 0x0, 0x0, 0x28E, 0x0, 0x0, 0x2AB, 0x0, 0x0}
  92. };
  93. static int iceland_start_smc(struct pp_hwmgr *hwmgr)
  94. {
  95. PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
  96. SMC_SYSCON_RESET_CNTL, rst_reg, 0);
  97. return 0;
  98. }
  99. static void iceland_reset_smc(struct pp_hwmgr *hwmgr)
  100. {
  101. PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
  102. SMC_SYSCON_RESET_CNTL,
  103. rst_reg, 1);
  104. }
  105. static void iceland_stop_smc_clock(struct pp_hwmgr *hwmgr)
  106. {
  107. PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
  108. SMC_SYSCON_CLOCK_CNTL_0,
  109. ck_disable, 1);
  110. }
  111. static void iceland_start_smc_clock(struct pp_hwmgr *hwmgr)
  112. {
  113. PHM_WRITE_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC,
  114. SMC_SYSCON_CLOCK_CNTL_0,
  115. ck_disable, 0);
  116. }
  117. static int iceland_smu_start_smc(struct pp_hwmgr *hwmgr)
  118. {
  119. /* set smc instruct start point at 0x0 */
  120. smu7_program_jump_on_start(hwmgr);
  121. /* enable smc clock */
  122. iceland_start_smc_clock(hwmgr);
  123. /* de-assert reset */
  124. iceland_start_smc(hwmgr);
  125. PHM_WAIT_INDIRECT_FIELD(hwmgr, SMC_IND, FIRMWARE_FLAGS,
  126. INTERRUPTS_ENABLED, 1);
  127. return 0;
  128. }
  129. static int iceland_upload_smc_firmware_data(struct pp_hwmgr *hwmgr,
  130. uint32_t length, const uint8_t *src,
  131. uint32_t limit, uint32_t start_addr)
  132. {
  133. uint32_t byte_count = length;
  134. uint32_t data;
  135. PP_ASSERT_WITH_CODE((limit >= byte_count), "SMC address is beyond the SMC RAM area.", return -EINVAL);
  136. cgs_write_register(hwmgr->device, mmSMC_IND_INDEX_0, start_addr);
  137. PHM_WRITE_FIELD(hwmgr->device, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 1);
  138. while (byte_count >= 4) {
  139. data = src[0] * 0x1000000 + src[1] * 0x10000 + src[2] * 0x100 + src[3];
  140. cgs_write_register(hwmgr->device, mmSMC_IND_DATA_0, data);
  141. src += 4;
  142. byte_count -= 4;
  143. }
  144. PHM_WRITE_FIELD(hwmgr->device, SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, 0);
  145. PP_ASSERT_WITH_CODE((0 == byte_count), "SMC size must be divisible by 4.", return -EINVAL);
  146. return 0;
  147. }
  148. static int iceland_smu_upload_firmware_image(struct pp_hwmgr *hwmgr)
  149. {
  150. uint32_t val;
  151. struct cgs_firmware_info info = {0};
  152. if (hwmgr == NULL || hwmgr->device == NULL)
  153. return -EINVAL;
  154. /* load SMC firmware */
  155. cgs_get_firmware_info(hwmgr->device,
  156. smu7_convert_fw_type_to_cgs(UCODE_ID_SMU), &info);
  157. if (info.image_size & 3) {
  158. pr_err("[ powerplay ] SMC ucode is not 4 bytes aligned\n");
  159. return -EINVAL;
  160. }
  161. if (info.image_size > ICELAND_SMC_SIZE) {
  162. pr_err("[ powerplay ] SMC address is beyond the SMC RAM area\n");
  163. return -EINVAL;
  164. }
  165. hwmgr->smu_version = info.version;
  166. /* wait for smc boot up */
  167. PHM_WAIT_INDIRECT_FIELD_UNEQUAL(hwmgr, SMC_IND,
  168. RCU_UC_EVENTS, boot_seq_done, 0);
  169. /* clear firmware interrupt enable flag */
  170. val = cgs_read_ind_register(hwmgr->device, CGS_IND_REG__SMC,
  171. ixSMC_SYSCON_MISC_CNTL);
  172. cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
  173. ixSMC_SYSCON_MISC_CNTL, val | 1);
  174. /* stop smc clock */
  175. iceland_stop_smc_clock(hwmgr);
  176. /* reset smc */
  177. iceland_reset_smc(hwmgr);
  178. iceland_upload_smc_firmware_data(hwmgr, info.image_size,
  179. (uint8_t *)info.kptr, ICELAND_SMC_SIZE,
  180. info.ucode_start_address);
  181. return 0;
  182. }
  183. static int iceland_request_smu_load_specific_fw(struct pp_hwmgr *hwmgr,
  184. uint32_t firmwareType)
  185. {
  186. return 0;
  187. }
  188. static int iceland_start_smu(struct pp_hwmgr *hwmgr)
  189. {
  190. int result;
  191. result = iceland_smu_upload_firmware_image(hwmgr);
  192. if (result)
  193. return result;
  194. result = iceland_smu_start_smc(hwmgr);
  195. if (result)
  196. return result;
  197. if (!smu7_is_smc_ram_running(hwmgr)) {
  198. pr_info("smu not running, upload firmware again \n");
  199. result = iceland_smu_upload_firmware_image(hwmgr);
  200. if (result)
  201. return result;
  202. result = iceland_smu_start_smc(hwmgr);
  203. if (result)
  204. return result;
  205. }
  206. result = smu7_request_smu_load_fw(hwmgr);
  207. return result;
  208. }
  209. static int iceland_smu_init(struct pp_hwmgr *hwmgr)
  210. {
  211. struct iceland_smumgr *iceland_priv = NULL;
  212. iceland_priv = kzalloc(sizeof(struct iceland_smumgr), GFP_KERNEL);
  213. if (iceland_priv == NULL)
  214. return -ENOMEM;
  215. hwmgr->smu_backend = iceland_priv;
  216. if (smu7_init(hwmgr)) {
  217. kfree(iceland_priv);
  218. return -EINVAL;
  219. }
  220. return 0;
  221. }
  222. static void iceland_initialize_power_tune_defaults(struct pp_hwmgr *hwmgr)
  223. {
  224. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  225. struct amdgpu_device *adev = hwmgr->adev;
  226. uint32_t dev_id;
  227. dev_id = adev->pdev->device;
  228. switch (dev_id) {
  229. case DEVICE_ID_VI_ICELAND_M_6900:
  230. case DEVICE_ID_VI_ICELAND_M_6903:
  231. smu_data->power_tune_defaults = &defaults_icelandxt;
  232. break;
  233. case DEVICE_ID_VI_ICELAND_M_6901:
  234. case DEVICE_ID_VI_ICELAND_M_6902:
  235. smu_data->power_tune_defaults = &defaults_icelandpro;
  236. break;
  237. default:
  238. smu_data->power_tune_defaults = &defaults_iceland;
  239. pr_warn("Unknown V.I. Device ID.\n");
  240. break;
  241. }
  242. return;
  243. }
  244. static int iceland_populate_svi_load_line(struct pp_hwmgr *hwmgr)
  245. {
  246. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  247. const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
  248. smu_data->power_tune_table.SviLoadLineEn = defaults->svi_load_line_en;
  249. smu_data->power_tune_table.SviLoadLineVddC = defaults->svi_load_line_vddc;
  250. smu_data->power_tune_table.SviLoadLineTrimVddC = 3;
  251. smu_data->power_tune_table.SviLoadLineOffsetVddC = 0;
  252. return 0;
  253. }
  254. static int iceland_populate_tdc_limit(struct pp_hwmgr *hwmgr)
  255. {
  256. uint16_t tdc_limit;
  257. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  258. const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
  259. tdc_limit = (uint16_t)(hwmgr->dyn_state.cac_dtp_table->usTDC * 256);
  260. smu_data->power_tune_table.TDC_VDDC_PkgLimit =
  261. CONVERT_FROM_HOST_TO_SMC_US(tdc_limit);
  262. smu_data->power_tune_table.TDC_VDDC_ThrottleReleaseLimitPerc =
  263. defaults->tdc_vddc_throttle_release_limit_perc;
  264. smu_data->power_tune_table.TDC_MAWt = defaults->tdc_mawt;
  265. return 0;
  266. }
  267. static int iceland_populate_dw8(struct pp_hwmgr *hwmgr, uint32_t fuse_table_offset)
  268. {
  269. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  270. const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
  271. uint32_t temp;
  272. if (smu7_read_smc_sram_dword(hwmgr,
  273. fuse_table_offset +
  274. offsetof(SMU71_Discrete_PmFuses, TdcWaterfallCtl),
  275. (uint32_t *)&temp, SMC_RAM_END))
  276. PP_ASSERT_WITH_CODE(false,
  277. "Attempt to read PmFuses.DW6 (SviLoadLineEn) from SMC Failed!",
  278. return -EINVAL);
  279. else
  280. smu_data->power_tune_table.TdcWaterfallCtl = defaults->tdc_waterfall_ctl;
  281. return 0;
  282. }
  283. static int iceland_populate_temperature_scaler(struct pp_hwmgr *hwmgr)
  284. {
  285. return 0;
  286. }
  287. static int iceland_populate_gnb_lpml(struct pp_hwmgr *hwmgr)
  288. {
  289. int i;
  290. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  291. /* Currently not used. Set all to zero. */
  292. for (i = 0; i < 8; i++)
  293. smu_data->power_tune_table.GnbLPML[i] = 0;
  294. return 0;
  295. }
  296. static int iceland_populate_bapm_vddc_base_leakage_sidd(struct pp_hwmgr *hwmgr)
  297. {
  298. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  299. uint16_t HiSidd = smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd;
  300. uint16_t LoSidd = smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd;
  301. struct phm_cac_tdp_table *cac_table = hwmgr->dyn_state.cac_dtp_table;
  302. HiSidd = (uint16_t)(cac_table->usHighCACLeakage / 100 * 256);
  303. LoSidd = (uint16_t)(cac_table->usLowCACLeakage / 100 * 256);
  304. smu_data->power_tune_table.BapmVddCBaseLeakageHiSidd =
  305. CONVERT_FROM_HOST_TO_SMC_US(HiSidd);
  306. smu_data->power_tune_table.BapmVddCBaseLeakageLoSidd =
  307. CONVERT_FROM_HOST_TO_SMC_US(LoSidd);
  308. return 0;
  309. }
  310. static int iceland_populate_bapm_vddc_vid_sidd(struct pp_hwmgr *hwmgr)
  311. {
  312. int i;
  313. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  314. uint8_t *hi_vid = smu_data->power_tune_table.BapmVddCVidHiSidd;
  315. uint8_t *lo_vid = smu_data->power_tune_table.BapmVddCVidLoSidd;
  316. PP_ASSERT_WITH_CODE(NULL != hwmgr->dyn_state.cac_leakage_table,
  317. "The CAC Leakage table does not exist!", return -EINVAL);
  318. PP_ASSERT_WITH_CODE(hwmgr->dyn_state.cac_leakage_table->count <= 8,
  319. "There should never be more than 8 entries for BapmVddcVid!!!", return -EINVAL);
  320. PP_ASSERT_WITH_CODE(hwmgr->dyn_state.cac_leakage_table->count == hwmgr->dyn_state.vddc_dependency_on_sclk->count,
  321. "CACLeakageTable->count and VddcDependencyOnSCLk->count not equal", return -EINVAL);
  322. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_EVV)) {
  323. for (i = 0; (uint32_t) i < hwmgr->dyn_state.cac_leakage_table->count; i++) {
  324. lo_vid[i] = convert_to_vid(hwmgr->dyn_state.cac_leakage_table->entries[i].Vddc1);
  325. hi_vid[i] = convert_to_vid(hwmgr->dyn_state.cac_leakage_table->entries[i].Vddc2);
  326. }
  327. } else {
  328. PP_ASSERT_WITH_CODE(false, "Iceland should always support EVV", return -EINVAL);
  329. }
  330. return 0;
  331. }
  332. static int iceland_populate_vddc_vid(struct pp_hwmgr *hwmgr)
  333. {
  334. int i;
  335. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  336. uint8_t *vid = smu_data->power_tune_table.VddCVid;
  337. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  338. PP_ASSERT_WITH_CODE(data->vddc_voltage_table.count <= 8,
  339. "There should never be more than 8 entries for VddcVid!!!",
  340. return -EINVAL);
  341. for (i = 0; i < (int)data->vddc_voltage_table.count; i++) {
  342. vid[i] = convert_to_vid(data->vddc_voltage_table.entries[i].value);
  343. }
  344. return 0;
  345. }
  346. static int iceland_populate_pm_fuses(struct pp_hwmgr *hwmgr)
  347. {
  348. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  349. uint32_t pm_fuse_table_offset;
  350. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  351. PHM_PlatformCaps_PowerContainment)) {
  352. if (smu7_read_smc_sram_dword(hwmgr,
  353. SMU71_FIRMWARE_HEADER_LOCATION +
  354. offsetof(SMU71_Firmware_Header, PmFuseTable),
  355. &pm_fuse_table_offset, SMC_RAM_END))
  356. PP_ASSERT_WITH_CODE(false,
  357. "Attempt to get pm_fuse_table_offset Failed!",
  358. return -EINVAL);
  359. /* DW0 - DW3 */
  360. if (iceland_populate_bapm_vddc_vid_sidd(hwmgr))
  361. PP_ASSERT_WITH_CODE(false,
  362. "Attempt to populate bapm vddc vid Failed!",
  363. return -EINVAL);
  364. /* DW4 - DW5 */
  365. if (iceland_populate_vddc_vid(hwmgr))
  366. PP_ASSERT_WITH_CODE(false,
  367. "Attempt to populate vddc vid Failed!",
  368. return -EINVAL);
  369. /* DW6 */
  370. if (iceland_populate_svi_load_line(hwmgr))
  371. PP_ASSERT_WITH_CODE(false,
  372. "Attempt to populate SviLoadLine Failed!",
  373. return -EINVAL);
  374. /* DW7 */
  375. if (iceland_populate_tdc_limit(hwmgr))
  376. PP_ASSERT_WITH_CODE(false,
  377. "Attempt to populate TDCLimit Failed!", return -EINVAL);
  378. /* DW8 */
  379. if (iceland_populate_dw8(hwmgr, pm_fuse_table_offset))
  380. PP_ASSERT_WITH_CODE(false,
  381. "Attempt to populate TdcWaterfallCtl, "
  382. "LPMLTemperature Min and Max Failed!",
  383. return -EINVAL);
  384. /* DW9-DW12 */
  385. if (0 != iceland_populate_temperature_scaler(hwmgr))
  386. PP_ASSERT_WITH_CODE(false,
  387. "Attempt to populate LPMLTemperatureScaler Failed!",
  388. return -EINVAL);
  389. /* DW13-DW16 */
  390. if (iceland_populate_gnb_lpml(hwmgr))
  391. PP_ASSERT_WITH_CODE(false,
  392. "Attempt to populate GnbLPML Failed!",
  393. return -EINVAL);
  394. /* DW18 */
  395. if (iceland_populate_bapm_vddc_base_leakage_sidd(hwmgr))
  396. PP_ASSERT_WITH_CODE(false,
  397. "Attempt to populate BapmVddCBaseLeakage Hi and Lo Sidd Failed!",
  398. return -EINVAL);
  399. if (smu7_copy_bytes_to_smc(hwmgr, pm_fuse_table_offset,
  400. (uint8_t *)&smu_data->power_tune_table,
  401. sizeof(struct SMU71_Discrete_PmFuses), SMC_RAM_END))
  402. PP_ASSERT_WITH_CODE(false,
  403. "Attempt to download PmFuseTable Failed!",
  404. return -EINVAL);
  405. }
  406. return 0;
  407. }
  408. static int iceland_get_dependency_volt_by_clk(struct pp_hwmgr *hwmgr,
  409. struct phm_clock_voltage_dependency_table *allowed_clock_voltage_table,
  410. uint32_t clock, uint32_t *vol)
  411. {
  412. uint32_t i = 0;
  413. /* clock - voltage dependency table is empty table */
  414. if (allowed_clock_voltage_table->count == 0)
  415. return -EINVAL;
  416. for (i = 0; i < allowed_clock_voltage_table->count; i++) {
  417. /* find first sclk bigger than request */
  418. if (allowed_clock_voltage_table->entries[i].clk >= clock) {
  419. *vol = allowed_clock_voltage_table->entries[i].v;
  420. return 0;
  421. }
  422. }
  423. /* sclk is bigger than max sclk in the dependence table */
  424. *vol = allowed_clock_voltage_table->entries[i - 1].v;
  425. return 0;
  426. }
  427. static int iceland_get_std_voltage_value_sidd(struct pp_hwmgr *hwmgr,
  428. pp_atomctrl_voltage_table_entry *tab, uint16_t *hi,
  429. uint16_t *lo)
  430. {
  431. uint16_t v_index;
  432. bool vol_found = false;
  433. *hi = tab->value * VOLTAGE_SCALE;
  434. *lo = tab->value * VOLTAGE_SCALE;
  435. /* SCLK/VDDC Dependency Table has to exist. */
  436. PP_ASSERT_WITH_CODE(NULL != hwmgr->dyn_state.vddc_dependency_on_sclk,
  437. "The SCLK/VDDC Dependency Table does not exist.",
  438. return -EINVAL);
  439. if (NULL == hwmgr->dyn_state.cac_leakage_table) {
  440. pr_warn("CAC Leakage Table does not exist, using vddc.\n");
  441. return 0;
  442. }
  443. /*
  444. * Since voltage in the sclk/vddc dependency table is not
  445. * necessarily in ascending order because of ELB voltage
  446. * patching, loop through entire list to find exact voltage.
  447. */
  448. for (v_index = 0; (uint32_t)v_index < hwmgr->dyn_state.vddc_dependency_on_sclk->count; v_index++) {
  449. if (tab->value == hwmgr->dyn_state.vddc_dependency_on_sclk->entries[v_index].v) {
  450. vol_found = true;
  451. if ((uint32_t)v_index < hwmgr->dyn_state.cac_leakage_table->count) {
  452. *lo = hwmgr->dyn_state.cac_leakage_table->entries[v_index].Vddc * VOLTAGE_SCALE;
  453. *hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[v_index].Leakage * VOLTAGE_SCALE);
  454. } else {
  455. pr_warn("Index from SCLK/VDDC Dependency Table exceeds the CAC Leakage Table index, using maximum index from CAC table.\n");
  456. *lo = hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Vddc * VOLTAGE_SCALE;
  457. *hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Leakage * VOLTAGE_SCALE);
  458. }
  459. break;
  460. }
  461. }
  462. /*
  463. * If voltage is not found in the first pass, loop again to
  464. * find the best match, equal or higher value.
  465. */
  466. if (!vol_found) {
  467. for (v_index = 0; (uint32_t)v_index < hwmgr->dyn_state.vddc_dependency_on_sclk->count; v_index++) {
  468. if (tab->value <= hwmgr->dyn_state.vddc_dependency_on_sclk->entries[v_index].v) {
  469. vol_found = true;
  470. if ((uint32_t)v_index < hwmgr->dyn_state.cac_leakage_table->count) {
  471. *lo = hwmgr->dyn_state.cac_leakage_table->entries[v_index].Vddc * VOLTAGE_SCALE;
  472. *hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[v_index].Leakage) * VOLTAGE_SCALE;
  473. } else {
  474. pr_warn("Index from SCLK/VDDC Dependency Table exceeds the CAC Leakage Table index in second look up, using maximum index from CAC table.");
  475. *lo = hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Vddc * VOLTAGE_SCALE;
  476. *hi = (uint16_t)(hwmgr->dyn_state.cac_leakage_table->entries[hwmgr->dyn_state.cac_leakage_table->count - 1].Leakage * VOLTAGE_SCALE);
  477. }
  478. break;
  479. }
  480. }
  481. if (!vol_found)
  482. pr_warn("Unable to get std_vddc from SCLK/VDDC Dependency Table, using vddc.\n");
  483. }
  484. return 0;
  485. }
  486. static int iceland_populate_smc_voltage_table(struct pp_hwmgr *hwmgr,
  487. pp_atomctrl_voltage_table_entry *tab,
  488. SMU71_Discrete_VoltageLevel *smc_voltage_tab)
  489. {
  490. int result;
  491. result = iceland_get_std_voltage_value_sidd(hwmgr, tab,
  492. &smc_voltage_tab->StdVoltageHiSidd,
  493. &smc_voltage_tab->StdVoltageLoSidd);
  494. if (0 != result) {
  495. smc_voltage_tab->StdVoltageHiSidd = tab->value * VOLTAGE_SCALE;
  496. smc_voltage_tab->StdVoltageLoSidd = tab->value * VOLTAGE_SCALE;
  497. }
  498. smc_voltage_tab->Voltage = PP_HOST_TO_SMC_US(tab->value * VOLTAGE_SCALE);
  499. CONVERT_FROM_HOST_TO_SMC_US(smc_voltage_tab->StdVoltageHiSidd);
  500. CONVERT_FROM_HOST_TO_SMC_US(smc_voltage_tab->StdVoltageHiSidd);
  501. return 0;
  502. }
  503. static int iceland_populate_smc_vddc_table(struct pp_hwmgr *hwmgr,
  504. SMU71_Discrete_DpmTable *table)
  505. {
  506. unsigned int count;
  507. int result;
  508. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  509. table->VddcLevelCount = data->vddc_voltage_table.count;
  510. for (count = 0; count < table->VddcLevelCount; count++) {
  511. result = iceland_populate_smc_voltage_table(hwmgr,
  512. &(data->vddc_voltage_table.entries[count]),
  513. &(table->VddcLevel[count]));
  514. PP_ASSERT_WITH_CODE(0 == result, "do not populate SMC VDDC voltage table", return -EINVAL);
  515. /* GPIO voltage control */
  516. if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->voltage_control)
  517. table->VddcLevel[count].Smio |= data->vddc_voltage_table.entries[count].smio_low;
  518. else if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control)
  519. table->VddcLevel[count].Smio = 0;
  520. }
  521. CONVERT_FROM_HOST_TO_SMC_UL(table->VddcLevelCount);
  522. return 0;
  523. }
  524. static int iceland_populate_smc_vdd_ci_table(struct pp_hwmgr *hwmgr,
  525. SMU71_Discrete_DpmTable *table)
  526. {
  527. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  528. uint32_t count;
  529. int result;
  530. table->VddciLevelCount = data->vddci_voltage_table.count;
  531. for (count = 0; count < table->VddciLevelCount; count++) {
  532. result = iceland_populate_smc_voltage_table(hwmgr,
  533. &(data->vddci_voltage_table.entries[count]),
  534. &(table->VddciLevel[count]));
  535. PP_ASSERT_WITH_CODE(result == 0, "do not populate SMC VDDCI voltage table", return -EINVAL);
  536. if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->vddci_control)
  537. table->VddciLevel[count].Smio |= data->vddci_voltage_table.entries[count].smio_low;
  538. else
  539. table->VddciLevel[count].Smio |= 0;
  540. }
  541. CONVERT_FROM_HOST_TO_SMC_UL(table->VddciLevelCount);
  542. return 0;
  543. }
  544. static int iceland_populate_smc_mvdd_table(struct pp_hwmgr *hwmgr,
  545. SMU71_Discrete_DpmTable *table)
  546. {
  547. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  548. uint32_t count;
  549. int result;
  550. table->MvddLevelCount = data->mvdd_voltage_table.count;
  551. for (count = 0; count < table->VddciLevelCount; count++) {
  552. result = iceland_populate_smc_voltage_table(hwmgr,
  553. &(data->mvdd_voltage_table.entries[count]),
  554. &table->MvddLevel[count]);
  555. PP_ASSERT_WITH_CODE(result == 0, "do not populate SMC mvdd voltage table", return -EINVAL);
  556. if (SMU7_VOLTAGE_CONTROL_BY_GPIO == data->mvdd_control)
  557. table->MvddLevel[count].Smio |= data->mvdd_voltage_table.entries[count].smio_low;
  558. else
  559. table->MvddLevel[count].Smio |= 0;
  560. }
  561. CONVERT_FROM_HOST_TO_SMC_UL(table->MvddLevelCount);
  562. return 0;
  563. }
  564. static int iceland_populate_smc_voltage_tables(struct pp_hwmgr *hwmgr,
  565. SMU71_Discrete_DpmTable *table)
  566. {
  567. int result;
  568. result = iceland_populate_smc_vddc_table(hwmgr, table);
  569. PP_ASSERT_WITH_CODE(0 == result,
  570. "can not populate VDDC voltage table to SMC", return -EINVAL);
  571. result = iceland_populate_smc_vdd_ci_table(hwmgr, table);
  572. PP_ASSERT_WITH_CODE(0 == result,
  573. "can not populate VDDCI voltage table to SMC", return -EINVAL);
  574. result = iceland_populate_smc_mvdd_table(hwmgr, table);
  575. PP_ASSERT_WITH_CODE(0 == result,
  576. "can not populate MVDD voltage table to SMC", return -EINVAL);
  577. return 0;
  578. }
  579. static int iceland_populate_ulv_level(struct pp_hwmgr *hwmgr,
  580. struct SMU71_Discrete_Ulv *state)
  581. {
  582. uint32_t voltage_response_time, ulv_voltage;
  583. int result;
  584. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  585. state->CcPwrDynRm = 0;
  586. state->CcPwrDynRm1 = 0;
  587. result = pp_tables_get_response_times(hwmgr, &voltage_response_time, &ulv_voltage);
  588. PP_ASSERT_WITH_CODE((0 == result), "can not get ULV voltage value", return result;);
  589. if (ulv_voltage == 0) {
  590. data->ulv_supported = false;
  591. return 0;
  592. }
  593. if (data->voltage_control != SMU7_VOLTAGE_CONTROL_BY_SVID2) {
  594. /* use minimum voltage if ulv voltage in pptable is bigger than minimum voltage */
  595. if (ulv_voltage > hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v)
  596. state->VddcOffset = 0;
  597. else
  598. /* used in SMIO Mode. not implemented for now. this is backup only for CI. */
  599. state->VddcOffset = (uint16_t)(hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v - ulv_voltage);
  600. } else {
  601. /* use minimum voltage if ulv voltage in pptable is bigger than minimum voltage */
  602. if (ulv_voltage > hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v)
  603. state->VddcOffsetVid = 0;
  604. else /* used in SVI2 Mode */
  605. state->VddcOffsetVid = (uint8_t)(
  606. (hwmgr->dyn_state.vddc_dependency_on_sclk->entries[0].v - ulv_voltage)
  607. * VOLTAGE_VID_OFFSET_SCALE2
  608. / VOLTAGE_VID_OFFSET_SCALE1);
  609. }
  610. state->VddcPhase = 1;
  611. CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm);
  612. CONVERT_FROM_HOST_TO_SMC_UL(state->CcPwrDynRm1);
  613. CONVERT_FROM_HOST_TO_SMC_US(state->VddcOffset);
  614. return 0;
  615. }
  616. static int iceland_populate_ulv_state(struct pp_hwmgr *hwmgr,
  617. SMU71_Discrete_Ulv *ulv_level)
  618. {
  619. return iceland_populate_ulv_level(hwmgr, ulv_level);
  620. }
  621. static int iceland_populate_smc_link_level(struct pp_hwmgr *hwmgr, SMU71_Discrete_DpmTable *table)
  622. {
  623. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  624. struct smu7_dpm_table *dpm_table = &data->dpm_table;
  625. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  626. uint32_t i;
  627. /* Index (dpm_table->pcie_speed_table.count) is reserved for PCIE boot level. */
  628. for (i = 0; i <= dpm_table->pcie_speed_table.count; i++) {
  629. table->LinkLevel[i].PcieGenSpeed =
  630. (uint8_t)dpm_table->pcie_speed_table.dpm_levels[i].value;
  631. table->LinkLevel[i].PcieLaneCount =
  632. (uint8_t)encode_pcie_lane_width(dpm_table->pcie_speed_table.dpm_levels[i].param1);
  633. table->LinkLevel[i].EnabledForActivity =
  634. 1;
  635. table->LinkLevel[i].SPC =
  636. (uint8_t)(data->pcie_spc_cap & 0xff);
  637. table->LinkLevel[i].DownThreshold =
  638. PP_HOST_TO_SMC_UL(5);
  639. table->LinkLevel[i].UpThreshold =
  640. PP_HOST_TO_SMC_UL(30);
  641. }
  642. smu_data->smc_state_table.LinkLevelCount =
  643. (uint8_t)dpm_table->pcie_speed_table.count;
  644. data->dpm_level_enable_mask.pcie_dpm_enable_mask =
  645. phm_get_dpm_level_enable_mask_value(&dpm_table->pcie_speed_table);
  646. return 0;
  647. }
  648. static int iceland_calculate_sclk_params(struct pp_hwmgr *hwmgr,
  649. uint32_t engine_clock, SMU71_Discrete_GraphicsLevel *sclk)
  650. {
  651. const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  652. pp_atomctrl_clock_dividers_vi dividers;
  653. uint32_t spll_func_cntl = data->clock_registers.vCG_SPLL_FUNC_CNTL;
  654. uint32_t spll_func_cntl_3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
  655. uint32_t spll_func_cntl_4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
  656. uint32_t cg_spll_spread_spectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
  657. uint32_t cg_spll_spread_spectrum_2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
  658. uint32_t reference_clock;
  659. uint32_t reference_divider;
  660. uint32_t fbdiv;
  661. int result;
  662. /* get the engine clock dividers for this clock value*/
  663. result = atomctrl_get_engine_pll_dividers_vi(hwmgr, engine_clock, &dividers);
  664. PP_ASSERT_WITH_CODE(result == 0,
  665. "Error retrieving Engine Clock dividers from VBIOS.", return result);
  666. /* To get FBDIV we need to multiply this by 16384 and divide it by Fref.*/
  667. reference_clock = atomctrl_get_reference_clock(hwmgr);
  668. reference_divider = 1 + dividers.uc_pll_ref_div;
  669. /* low 14 bits is fraction and high 12 bits is divider*/
  670. fbdiv = dividers.ul_fb_div.ul_fb_divider & 0x3FFFFFF;
  671. /* SPLL_FUNC_CNTL setup*/
  672. spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
  673. CG_SPLL_FUNC_CNTL, SPLL_REF_DIV, dividers.uc_pll_ref_div);
  674. spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
  675. CG_SPLL_FUNC_CNTL, SPLL_PDIV_A, dividers.uc_pll_post_div);
  676. /* SPLL_FUNC_CNTL_3 setup*/
  677. spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3,
  678. CG_SPLL_FUNC_CNTL_3, SPLL_FB_DIV, fbdiv);
  679. /* set to use fractional accumulation*/
  680. spll_func_cntl_3 = PHM_SET_FIELD(spll_func_cntl_3,
  681. CG_SPLL_FUNC_CNTL_3, SPLL_DITHEN, 1);
  682. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  683. PHM_PlatformCaps_EngineSpreadSpectrumSupport)) {
  684. pp_atomctrl_internal_ss_info ss_info;
  685. uint32_t vcoFreq = engine_clock * dividers.uc_pll_post_div;
  686. if (0 == atomctrl_get_engine_clock_spread_spectrum(hwmgr, vcoFreq, &ss_info)) {
  687. /*
  688. * ss_info.speed_spectrum_percentage -- in unit of 0.01%
  689. * ss_info.speed_spectrum_rate -- in unit of khz
  690. */
  691. /* clks = reference_clock * 10 / (REFDIV + 1) / speed_spectrum_rate / 2 */
  692. uint32_t clkS = reference_clock * 5 / (reference_divider * ss_info.speed_spectrum_rate);
  693. /* clkv = 2 * D * fbdiv / NS */
  694. uint32_t clkV = 4 * ss_info.speed_spectrum_percentage * fbdiv / (clkS * 10000);
  695. cg_spll_spread_spectrum =
  696. PHM_SET_FIELD(cg_spll_spread_spectrum, CG_SPLL_SPREAD_SPECTRUM, CLKS, clkS);
  697. cg_spll_spread_spectrum =
  698. PHM_SET_FIELD(cg_spll_spread_spectrum, CG_SPLL_SPREAD_SPECTRUM, SSEN, 1);
  699. cg_spll_spread_spectrum_2 =
  700. PHM_SET_FIELD(cg_spll_spread_spectrum_2, CG_SPLL_SPREAD_SPECTRUM_2, CLKV, clkV);
  701. }
  702. }
  703. sclk->SclkFrequency = engine_clock;
  704. sclk->CgSpllFuncCntl3 = spll_func_cntl_3;
  705. sclk->CgSpllFuncCntl4 = spll_func_cntl_4;
  706. sclk->SpllSpreadSpectrum = cg_spll_spread_spectrum;
  707. sclk->SpllSpreadSpectrum2 = cg_spll_spread_spectrum_2;
  708. sclk->SclkDid = (uint8_t)dividers.pll_post_divider;
  709. return 0;
  710. }
  711. static int iceland_populate_phase_value_based_on_sclk(struct pp_hwmgr *hwmgr,
  712. const struct phm_phase_shedding_limits_table *pl,
  713. uint32_t sclk, uint32_t *p_shed)
  714. {
  715. unsigned int i;
  716. /* use the minimum phase shedding */
  717. *p_shed = 1;
  718. for (i = 0; i < pl->count; i++) {
  719. if (sclk < pl->entries[i].Sclk) {
  720. *p_shed = i;
  721. break;
  722. }
  723. }
  724. return 0;
  725. }
  726. static int iceland_populate_single_graphic_level(struct pp_hwmgr *hwmgr,
  727. uint32_t engine_clock,
  728. SMU71_Discrete_GraphicsLevel *graphic_level)
  729. {
  730. int result;
  731. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  732. result = iceland_calculate_sclk_params(hwmgr, engine_clock, graphic_level);
  733. /* populate graphics levels*/
  734. result = iceland_get_dependency_volt_by_clk(hwmgr,
  735. hwmgr->dyn_state.vddc_dependency_on_sclk, engine_clock,
  736. &graphic_level->MinVddc);
  737. PP_ASSERT_WITH_CODE((0 == result),
  738. "can not find VDDC voltage value for VDDC engine clock dependency table", return result);
  739. /* SCLK frequency in units of 10KHz*/
  740. graphic_level->SclkFrequency = engine_clock;
  741. graphic_level->MinVddcPhases = 1;
  742. if (data->vddc_phase_shed_control)
  743. iceland_populate_phase_value_based_on_sclk(hwmgr,
  744. hwmgr->dyn_state.vddc_phase_shed_limits_table,
  745. engine_clock,
  746. &graphic_level->MinVddcPhases);
  747. /* Indicates maximum activity level for this performance level. 50% for now*/
  748. graphic_level->ActivityLevel = data->current_profile_setting.sclk_activity;
  749. graphic_level->CcPwrDynRm = 0;
  750. graphic_level->CcPwrDynRm1 = 0;
  751. /* this level can be used if activity is high enough.*/
  752. graphic_level->EnabledForActivity = 0;
  753. /* this level can be used for throttling.*/
  754. graphic_level->EnabledForThrottle = 1;
  755. graphic_level->UpHyst = data->current_profile_setting.sclk_up_hyst;
  756. graphic_level->DownHyst = data->current_profile_setting.sclk_down_hyst;
  757. graphic_level->VoltageDownHyst = 0;
  758. graphic_level->PowerThrottle = 0;
  759. data->display_timing.min_clock_in_sr =
  760. hwmgr->display_config->min_core_set_clock_in_sr;
  761. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  762. PHM_PlatformCaps_SclkDeepSleep))
  763. graphic_level->DeepSleepDivId =
  764. smu7_get_sleep_divider_id_from_clock(engine_clock,
  765. data->display_timing.min_clock_in_sr);
  766. /* Default to slow, highest DPM level will be set to PPSMC_DISPLAY_WATERMARK_LOW later.*/
  767. graphic_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  768. if (0 == result) {
  769. graphic_level->MinVddc = PP_HOST_TO_SMC_UL(graphic_level->MinVddc * VOLTAGE_SCALE);
  770. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->MinVddcPhases);
  771. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SclkFrequency);
  772. CONVERT_FROM_HOST_TO_SMC_US(graphic_level->ActivityLevel);
  773. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CgSpllFuncCntl3);
  774. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CgSpllFuncCntl4);
  775. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SpllSpreadSpectrum);
  776. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->SpllSpreadSpectrum2);
  777. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CcPwrDynRm);
  778. CONVERT_FROM_HOST_TO_SMC_UL(graphic_level->CcPwrDynRm1);
  779. }
  780. return result;
  781. }
  782. static int iceland_populate_all_graphic_levels(struct pp_hwmgr *hwmgr)
  783. {
  784. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  785. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  786. struct smu7_dpm_table *dpm_table = &data->dpm_table;
  787. uint32_t level_array_adress = smu_data->smu7_data.dpm_table_start +
  788. offsetof(SMU71_Discrete_DpmTable, GraphicsLevel);
  789. uint32_t level_array_size = sizeof(SMU71_Discrete_GraphicsLevel) *
  790. SMU71_MAX_LEVELS_GRAPHICS;
  791. SMU71_Discrete_GraphicsLevel *levels = smu_data->smc_state_table.GraphicsLevel;
  792. uint32_t i;
  793. uint8_t highest_pcie_level_enabled = 0;
  794. uint8_t lowest_pcie_level_enabled = 0, mid_pcie_level_enabled = 0;
  795. uint8_t count = 0;
  796. int result = 0;
  797. memset(levels, 0x00, level_array_size);
  798. for (i = 0; i < dpm_table->sclk_table.count; i++) {
  799. result = iceland_populate_single_graphic_level(hwmgr,
  800. dpm_table->sclk_table.dpm_levels[i].value,
  801. &(smu_data->smc_state_table.GraphicsLevel[i]));
  802. if (result != 0)
  803. return result;
  804. /* Making sure only DPM level 0-1 have Deep Sleep Div ID populated. */
  805. if (i > 1)
  806. smu_data->smc_state_table.GraphicsLevel[i].DeepSleepDivId = 0;
  807. }
  808. /* Only enable level 0 for now. */
  809. smu_data->smc_state_table.GraphicsLevel[0].EnabledForActivity = 1;
  810. /* set highest level watermark to high */
  811. if (dpm_table->sclk_table.count > 1)
  812. smu_data->smc_state_table.GraphicsLevel[dpm_table->sclk_table.count-1].DisplayWatermark =
  813. PPSMC_DISPLAY_WATERMARK_HIGH;
  814. smu_data->smc_state_table.GraphicsDpmLevelCount =
  815. (uint8_t)dpm_table->sclk_table.count;
  816. data->dpm_level_enable_mask.sclk_dpm_enable_mask =
  817. phm_get_dpm_level_enable_mask_value(&dpm_table->sclk_table);
  818. while ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
  819. (1 << (highest_pcie_level_enabled + 1))) != 0) {
  820. highest_pcie_level_enabled++;
  821. }
  822. while ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
  823. (1 << lowest_pcie_level_enabled)) == 0) {
  824. lowest_pcie_level_enabled++;
  825. }
  826. while ((count < highest_pcie_level_enabled) &&
  827. ((data->dpm_level_enable_mask.pcie_dpm_enable_mask &
  828. (1 << (lowest_pcie_level_enabled + 1 + count))) == 0)) {
  829. count++;
  830. }
  831. mid_pcie_level_enabled = (lowest_pcie_level_enabled+1+count) < highest_pcie_level_enabled ?
  832. (lowest_pcie_level_enabled+1+count) : highest_pcie_level_enabled;
  833. /* set pcieDpmLevel to highest_pcie_level_enabled*/
  834. for (i = 2; i < dpm_table->sclk_table.count; i++) {
  835. smu_data->smc_state_table.GraphicsLevel[i].pcieDpmLevel = highest_pcie_level_enabled;
  836. }
  837. /* set pcieDpmLevel to lowest_pcie_level_enabled*/
  838. smu_data->smc_state_table.GraphicsLevel[0].pcieDpmLevel = lowest_pcie_level_enabled;
  839. /* set pcieDpmLevel to mid_pcie_level_enabled*/
  840. smu_data->smc_state_table.GraphicsLevel[1].pcieDpmLevel = mid_pcie_level_enabled;
  841. /* level count will send to smc once at init smc table and never change*/
  842. result = smu7_copy_bytes_to_smc(hwmgr, level_array_adress,
  843. (uint8_t *)levels, (uint32_t)level_array_size,
  844. SMC_RAM_END);
  845. return result;
  846. }
  847. static int iceland_calculate_mclk_params(
  848. struct pp_hwmgr *hwmgr,
  849. uint32_t memory_clock,
  850. SMU71_Discrete_MemoryLevel *mclk,
  851. bool strobe_mode,
  852. bool dllStateOn
  853. )
  854. {
  855. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  856. uint32_t dll_cntl = data->clock_registers.vDLL_CNTL;
  857. uint32_t mclk_pwrmgt_cntl = data->clock_registers.vMCLK_PWRMGT_CNTL;
  858. uint32_t mpll_ad_func_cntl = data->clock_registers.vMPLL_AD_FUNC_CNTL;
  859. uint32_t mpll_dq_func_cntl = data->clock_registers.vMPLL_DQ_FUNC_CNTL;
  860. uint32_t mpll_func_cntl = data->clock_registers.vMPLL_FUNC_CNTL;
  861. uint32_t mpll_func_cntl_1 = data->clock_registers.vMPLL_FUNC_CNTL_1;
  862. uint32_t mpll_func_cntl_2 = data->clock_registers.vMPLL_FUNC_CNTL_2;
  863. uint32_t mpll_ss1 = data->clock_registers.vMPLL_SS1;
  864. uint32_t mpll_ss2 = data->clock_registers.vMPLL_SS2;
  865. pp_atomctrl_memory_clock_param mpll_param;
  866. int result;
  867. result = atomctrl_get_memory_pll_dividers_si(hwmgr,
  868. memory_clock, &mpll_param, strobe_mode);
  869. PP_ASSERT_WITH_CODE(0 == result,
  870. "Error retrieving Memory Clock Parameters from VBIOS.", return result);
  871. /* MPLL_FUNC_CNTL setup*/
  872. mpll_func_cntl = PHM_SET_FIELD(mpll_func_cntl, MPLL_FUNC_CNTL, BWCTRL, mpll_param.bw_ctrl);
  873. /* MPLL_FUNC_CNTL_1 setup*/
  874. mpll_func_cntl_1 = PHM_SET_FIELD(mpll_func_cntl_1,
  875. MPLL_FUNC_CNTL_1, CLKF, mpll_param.mpll_fb_divider.cl_kf);
  876. mpll_func_cntl_1 = PHM_SET_FIELD(mpll_func_cntl_1,
  877. MPLL_FUNC_CNTL_1, CLKFRAC, mpll_param.mpll_fb_divider.clk_frac);
  878. mpll_func_cntl_1 = PHM_SET_FIELD(mpll_func_cntl_1,
  879. MPLL_FUNC_CNTL_1, VCO_MODE, mpll_param.vco_mode);
  880. /* MPLL_AD_FUNC_CNTL setup*/
  881. mpll_ad_func_cntl = PHM_SET_FIELD(mpll_ad_func_cntl,
  882. MPLL_AD_FUNC_CNTL, YCLK_POST_DIV, mpll_param.mpll_post_divider);
  883. if (data->is_memory_gddr5) {
  884. /* MPLL_DQ_FUNC_CNTL setup*/
  885. mpll_dq_func_cntl = PHM_SET_FIELD(mpll_dq_func_cntl,
  886. MPLL_DQ_FUNC_CNTL, YCLK_SEL, mpll_param.yclk_sel);
  887. mpll_dq_func_cntl = PHM_SET_FIELD(mpll_dq_func_cntl,
  888. MPLL_DQ_FUNC_CNTL, YCLK_POST_DIV, mpll_param.mpll_post_divider);
  889. }
  890. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  891. PHM_PlatformCaps_MemorySpreadSpectrumSupport)) {
  892. /*
  893. ************************************
  894. Fref = Reference Frequency
  895. NF = Feedback divider ratio
  896. NR = Reference divider ratio
  897. Fnom = Nominal VCO output frequency = Fref * NF / NR
  898. Fs = Spreading Rate
  899. D = Percentage down-spread / 2
  900. Fint = Reference input frequency to PFD = Fref / NR
  901. NS = Spreading rate divider ratio = int(Fint / (2 * Fs))
  902. CLKS = NS - 1 = ISS_STEP_NUM[11:0]
  903. NV = D * Fs / Fnom * 4 * ((Fnom/Fref * NR) ^ 2)
  904. CLKV = 65536 * NV = ISS_STEP_SIZE[25:0]
  905. *************************************
  906. */
  907. pp_atomctrl_internal_ss_info ss_info;
  908. uint32_t freq_nom;
  909. uint32_t tmp;
  910. uint32_t reference_clock = atomctrl_get_mpll_reference_clock(hwmgr);
  911. /* for GDDR5 for all modes and DDR3 */
  912. if (1 == mpll_param.qdr)
  913. freq_nom = memory_clock * 4 * (1 << mpll_param.mpll_post_divider);
  914. else
  915. freq_nom = memory_clock * 2 * (1 << mpll_param.mpll_post_divider);
  916. /* tmp = (freq_nom / reference_clock * reference_divider) ^ 2 Note: S.I. reference_divider = 1*/
  917. tmp = (freq_nom / reference_clock);
  918. tmp = tmp * tmp;
  919. if (0 == atomctrl_get_memory_clock_spread_spectrum(hwmgr, freq_nom, &ss_info)) {
  920. /* ss_info.speed_spectrum_percentage -- in unit of 0.01% */
  921. /* ss.Info.speed_spectrum_rate -- in unit of khz */
  922. /* CLKS = reference_clock / (2 * speed_spectrum_rate * reference_divider) * 10 */
  923. /* = reference_clock * 5 / speed_spectrum_rate */
  924. uint32_t clks = reference_clock * 5 / ss_info.speed_spectrum_rate;
  925. /* CLKV = 65536 * speed_spectrum_percentage / 2 * spreadSpecrumRate / freq_nom * 4 / 100000 * ((freq_nom / reference_clock) ^ 2) */
  926. /* = 131 * speed_spectrum_percentage * speed_spectrum_rate / 100 * ((freq_nom / reference_clock) ^ 2) / freq_nom */
  927. uint32_t clkv =
  928. (uint32_t)((((131 * ss_info.speed_spectrum_percentage *
  929. ss_info.speed_spectrum_rate) / 100) * tmp) / freq_nom);
  930. mpll_ss1 = PHM_SET_FIELD(mpll_ss1, MPLL_SS1, CLKV, clkv);
  931. mpll_ss2 = PHM_SET_FIELD(mpll_ss2, MPLL_SS2, CLKS, clks);
  932. }
  933. }
  934. /* MCLK_PWRMGT_CNTL setup */
  935. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  936. MCLK_PWRMGT_CNTL, DLL_SPEED, mpll_param.dll_speed);
  937. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  938. MCLK_PWRMGT_CNTL, MRDCK0_PDNB, dllStateOn);
  939. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  940. MCLK_PWRMGT_CNTL, MRDCK1_PDNB, dllStateOn);
  941. /* Save the result data to outpupt memory level structure */
  942. mclk->MclkFrequency = memory_clock;
  943. mclk->MpllFuncCntl = mpll_func_cntl;
  944. mclk->MpllFuncCntl_1 = mpll_func_cntl_1;
  945. mclk->MpllFuncCntl_2 = mpll_func_cntl_2;
  946. mclk->MpllAdFuncCntl = mpll_ad_func_cntl;
  947. mclk->MpllDqFuncCntl = mpll_dq_func_cntl;
  948. mclk->MclkPwrmgtCntl = mclk_pwrmgt_cntl;
  949. mclk->DllCntl = dll_cntl;
  950. mclk->MpllSs1 = mpll_ss1;
  951. mclk->MpllSs2 = mpll_ss2;
  952. return 0;
  953. }
  954. static uint8_t iceland_get_mclk_frequency_ratio(uint32_t memory_clock,
  955. bool strobe_mode)
  956. {
  957. uint8_t mc_para_index;
  958. if (strobe_mode) {
  959. if (memory_clock < 12500) {
  960. mc_para_index = 0x00;
  961. } else if (memory_clock > 47500) {
  962. mc_para_index = 0x0f;
  963. } else {
  964. mc_para_index = (uint8_t)((memory_clock - 10000) / 2500);
  965. }
  966. } else {
  967. if (memory_clock < 65000) {
  968. mc_para_index = 0x00;
  969. } else if (memory_clock > 135000) {
  970. mc_para_index = 0x0f;
  971. } else {
  972. mc_para_index = (uint8_t)((memory_clock - 60000) / 5000);
  973. }
  974. }
  975. return mc_para_index;
  976. }
  977. static uint8_t iceland_get_ddr3_mclk_frequency_ratio(uint32_t memory_clock)
  978. {
  979. uint8_t mc_para_index;
  980. if (memory_clock < 10000) {
  981. mc_para_index = 0;
  982. } else if (memory_clock >= 80000) {
  983. mc_para_index = 0x0f;
  984. } else {
  985. mc_para_index = (uint8_t)((memory_clock - 10000) / 5000 + 1);
  986. }
  987. return mc_para_index;
  988. }
  989. static int iceland_populate_phase_value_based_on_mclk(struct pp_hwmgr *hwmgr, const struct phm_phase_shedding_limits_table *pl,
  990. uint32_t memory_clock, uint32_t *p_shed)
  991. {
  992. unsigned int i;
  993. *p_shed = 1;
  994. for (i = 0; i < pl->count; i++) {
  995. if (memory_clock < pl->entries[i].Mclk) {
  996. *p_shed = i;
  997. break;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. static int iceland_populate_single_memory_level(
  1003. struct pp_hwmgr *hwmgr,
  1004. uint32_t memory_clock,
  1005. SMU71_Discrete_MemoryLevel *memory_level
  1006. )
  1007. {
  1008. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1009. int result = 0;
  1010. bool dll_state_on;
  1011. uint32_t mclk_edc_wr_enable_threshold = 40000;
  1012. uint32_t mclk_edc_enable_threshold = 40000;
  1013. uint32_t mclk_strobe_mode_threshold = 40000;
  1014. if (hwmgr->dyn_state.vddc_dependency_on_mclk != NULL) {
  1015. result = iceland_get_dependency_volt_by_clk(hwmgr,
  1016. hwmgr->dyn_state.vddc_dependency_on_mclk, memory_clock, &memory_level->MinVddc);
  1017. PP_ASSERT_WITH_CODE((0 == result),
  1018. "can not find MinVddc voltage value from memory VDDC voltage dependency table", return result);
  1019. }
  1020. if (data->vddci_control == SMU7_VOLTAGE_CONTROL_NONE) {
  1021. memory_level->MinVddci = memory_level->MinVddc;
  1022. } else if (NULL != hwmgr->dyn_state.vddci_dependency_on_mclk) {
  1023. result = iceland_get_dependency_volt_by_clk(hwmgr,
  1024. hwmgr->dyn_state.vddci_dependency_on_mclk,
  1025. memory_clock,
  1026. &memory_level->MinVddci);
  1027. PP_ASSERT_WITH_CODE((0 == result),
  1028. "can not find MinVddci voltage value from memory VDDCI voltage dependency table", return result);
  1029. }
  1030. memory_level->MinVddcPhases = 1;
  1031. if (data->vddc_phase_shed_control) {
  1032. iceland_populate_phase_value_based_on_mclk(hwmgr, hwmgr->dyn_state.vddc_phase_shed_limits_table,
  1033. memory_clock, &memory_level->MinVddcPhases);
  1034. }
  1035. memory_level->EnabledForThrottle = 1;
  1036. memory_level->EnabledForActivity = 0;
  1037. memory_level->UpHyst = data->current_profile_setting.mclk_up_hyst;
  1038. memory_level->DownHyst = data->current_profile_setting.mclk_down_hyst;
  1039. memory_level->VoltageDownHyst = 0;
  1040. /* Indicates maximum activity level for this performance level.*/
  1041. memory_level->ActivityLevel = data->current_profile_setting.mclk_activity;
  1042. memory_level->StutterEnable = 0;
  1043. memory_level->StrobeEnable = 0;
  1044. memory_level->EdcReadEnable = 0;
  1045. memory_level->EdcWriteEnable = 0;
  1046. memory_level->RttEnable = 0;
  1047. /* default set to low watermark. Highest level will be set to high later.*/
  1048. memory_level->DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  1049. data->display_timing.num_existing_displays = hwmgr->display_config->num_display;
  1050. /* stutter mode not support on iceland */
  1051. /* decide strobe mode*/
  1052. memory_level->StrobeEnable = (mclk_strobe_mode_threshold != 0) &&
  1053. (memory_clock <= mclk_strobe_mode_threshold);
  1054. /* decide EDC mode and memory clock ratio*/
  1055. if (data->is_memory_gddr5) {
  1056. memory_level->StrobeRatio = iceland_get_mclk_frequency_ratio(memory_clock,
  1057. memory_level->StrobeEnable);
  1058. if ((mclk_edc_enable_threshold != 0) &&
  1059. (memory_clock > mclk_edc_enable_threshold)) {
  1060. memory_level->EdcReadEnable = 1;
  1061. }
  1062. if ((mclk_edc_wr_enable_threshold != 0) &&
  1063. (memory_clock > mclk_edc_wr_enable_threshold)) {
  1064. memory_level->EdcWriteEnable = 1;
  1065. }
  1066. if (memory_level->StrobeEnable) {
  1067. if (iceland_get_mclk_frequency_ratio(memory_clock, 1) >=
  1068. ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC7) >> 16) & 0xf))
  1069. dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC5) >> 1) & 0x1) ? 1 : 0;
  1070. else
  1071. dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC6) >> 1) & 0x1) ? 1 : 0;
  1072. } else
  1073. dll_state_on = data->dll_default_on;
  1074. } else {
  1075. memory_level->StrobeRatio =
  1076. iceland_get_ddr3_mclk_frequency_ratio(memory_clock);
  1077. dll_state_on = ((cgs_read_register(hwmgr->device, mmMC_SEQ_MISC5) >> 1) & 0x1) ? 1 : 0;
  1078. }
  1079. result = iceland_calculate_mclk_params(hwmgr,
  1080. memory_clock, memory_level, memory_level->StrobeEnable, dll_state_on);
  1081. if (0 == result) {
  1082. memory_level->MinVddc = PP_HOST_TO_SMC_UL(memory_level->MinVddc * VOLTAGE_SCALE);
  1083. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MinVddcPhases);
  1084. memory_level->MinVddci = PP_HOST_TO_SMC_UL(memory_level->MinVddci * VOLTAGE_SCALE);
  1085. memory_level->MinMvdd = PP_HOST_TO_SMC_UL(memory_level->MinMvdd * VOLTAGE_SCALE);
  1086. /* MCLK frequency in units of 10KHz*/
  1087. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MclkFrequency);
  1088. /* Indicates maximum activity level for this performance level.*/
  1089. CONVERT_FROM_HOST_TO_SMC_US(memory_level->ActivityLevel);
  1090. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl);
  1091. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl_1);
  1092. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllFuncCntl_2);
  1093. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllAdFuncCntl);
  1094. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllDqFuncCntl);
  1095. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MclkPwrmgtCntl);
  1096. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->DllCntl);
  1097. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllSs1);
  1098. CONVERT_FROM_HOST_TO_SMC_UL(memory_level->MpllSs2);
  1099. }
  1100. return result;
  1101. }
  1102. static int iceland_populate_all_memory_levels(struct pp_hwmgr *hwmgr)
  1103. {
  1104. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1105. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1106. struct smu7_dpm_table *dpm_table = &data->dpm_table;
  1107. int result;
  1108. /* populate MCLK dpm table to SMU7 */
  1109. uint32_t level_array_adress = smu_data->smu7_data.dpm_table_start + offsetof(SMU71_Discrete_DpmTable, MemoryLevel);
  1110. uint32_t level_array_size = sizeof(SMU71_Discrete_MemoryLevel) * SMU71_MAX_LEVELS_MEMORY;
  1111. SMU71_Discrete_MemoryLevel *levels = smu_data->smc_state_table.MemoryLevel;
  1112. uint32_t i;
  1113. memset(levels, 0x00, level_array_size);
  1114. for (i = 0; i < dpm_table->mclk_table.count; i++) {
  1115. PP_ASSERT_WITH_CODE((0 != dpm_table->mclk_table.dpm_levels[i].value),
  1116. "can not populate memory level as memory clock is zero", return -EINVAL);
  1117. result = iceland_populate_single_memory_level(hwmgr, dpm_table->mclk_table.dpm_levels[i].value,
  1118. &(smu_data->smc_state_table.MemoryLevel[i]));
  1119. if (0 != result) {
  1120. return result;
  1121. }
  1122. }
  1123. /* Only enable level 0 for now.*/
  1124. smu_data->smc_state_table.MemoryLevel[0].EnabledForActivity = 1;
  1125. /*
  1126. * in order to prevent MC activity from stutter mode to push DPM up.
  1127. * the UVD change complements this by putting the MCLK in a higher state
  1128. * by default such that we are not effected by up threshold or and MCLK DPM latency.
  1129. */
  1130. smu_data->smc_state_table.MemoryLevel[0].ActivityLevel = 0x1F;
  1131. CONVERT_FROM_HOST_TO_SMC_US(smu_data->smc_state_table.MemoryLevel[0].ActivityLevel);
  1132. smu_data->smc_state_table.MemoryDpmLevelCount = (uint8_t)dpm_table->mclk_table.count;
  1133. data->dpm_level_enable_mask.mclk_dpm_enable_mask = phm_get_dpm_level_enable_mask_value(&dpm_table->mclk_table);
  1134. /* set highest level watermark to high*/
  1135. smu_data->smc_state_table.MemoryLevel[dpm_table->mclk_table.count-1].DisplayWatermark = PPSMC_DISPLAY_WATERMARK_HIGH;
  1136. /* level count will send to smc once at init smc table and never change*/
  1137. result = smu7_copy_bytes_to_smc(hwmgr,
  1138. level_array_adress, (uint8_t *)levels, (uint32_t)level_array_size,
  1139. SMC_RAM_END);
  1140. return result;
  1141. }
  1142. static int iceland_populate_mvdd_value(struct pp_hwmgr *hwmgr, uint32_t mclk,
  1143. SMU71_Discrete_VoltageLevel *voltage)
  1144. {
  1145. const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1146. uint32_t i = 0;
  1147. if (SMU7_VOLTAGE_CONTROL_NONE != data->mvdd_control) {
  1148. /* find mvdd value which clock is more than request */
  1149. for (i = 0; i < hwmgr->dyn_state.mvdd_dependency_on_mclk->count; i++) {
  1150. if (mclk <= hwmgr->dyn_state.mvdd_dependency_on_mclk->entries[i].clk) {
  1151. /* Always round to higher voltage. */
  1152. voltage->Voltage = data->mvdd_voltage_table.entries[i].value;
  1153. break;
  1154. }
  1155. }
  1156. PP_ASSERT_WITH_CODE(i < hwmgr->dyn_state.mvdd_dependency_on_mclk->count,
  1157. "MVDD Voltage is outside the supported range.", return -EINVAL);
  1158. } else {
  1159. return -EINVAL;
  1160. }
  1161. return 0;
  1162. }
  1163. static int iceland_populate_smc_acpi_level(struct pp_hwmgr *hwmgr,
  1164. SMU71_Discrete_DpmTable *table)
  1165. {
  1166. int result = 0;
  1167. const struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1168. struct pp_atomctrl_clock_dividers_vi dividers;
  1169. uint32_t vddc_phase_shed_control = 0;
  1170. SMU71_Discrete_VoltageLevel voltage_level;
  1171. uint32_t spll_func_cntl = data->clock_registers.vCG_SPLL_FUNC_CNTL;
  1172. uint32_t spll_func_cntl_2 = data->clock_registers.vCG_SPLL_FUNC_CNTL_2;
  1173. uint32_t dll_cntl = data->clock_registers.vDLL_CNTL;
  1174. uint32_t mclk_pwrmgt_cntl = data->clock_registers.vMCLK_PWRMGT_CNTL;
  1175. /* The ACPI state should not do DPM on DC (or ever).*/
  1176. table->ACPILevel.Flags &= ~PPSMC_SWSTATE_FLAG_DC;
  1177. if (data->acpi_vddc)
  1178. table->ACPILevel.MinVddc = PP_HOST_TO_SMC_UL(data->acpi_vddc * VOLTAGE_SCALE);
  1179. else
  1180. table->ACPILevel.MinVddc = PP_HOST_TO_SMC_UL(data->min_vddc_in_pptable * VOLTAGE_SCALE);
  1181. table->ACPILevel.MinVddcPhases = vddc_phase_shed_control ? 0 : 1;
  1182. /* assign zero for now*/
  1183. table->ACPILevel.SclkFrequency = atomctrl_get_reference_clock(hwmgr);
  1184. /* get the engine clock dividers for this clock value*/
  1185. result = atomctrl_get_engine_pll_dividers_vi(hwmgr,
  1186. table->ACPILevel.SclkFrequency, &dividers);
  1187. PP_ASSERT_WITH_CODE(result == 0,
  1188. "Error retrieving Engine Clock dividers from VBIOS.", return result);
  1189. /* divider ID for required SCLK*/
  1190. table->ACPILevel.SclkDid = (uint8_t)dividers.pll_post_divider;
  1191. table->ACPILevel.DisplayWatermark = PPSMC_DISPLAY_WATERMARK_LOW;
  1192. table->ACPILevel.DeepSleepDivId = 0;
  1193. spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
  1194. CG_SPLL_FUNC_CNTL, SPLL_PWRON, 0);
  1195. spll_func_cntl = PHM_SET_FIELD(spll_func_cntl,
  1196. CG_SPLL_FUNC_CNTL, SPLL_RESET, 1);
  1197. spll_func_cntl_2 = PHM_SET_FIELD(spll_func_cntl_2,
  1198. CG_SPLL_FUNC_CNTL_2, SCLK_MUX_SEL, 4);
  1199. table->ACPILevel.CgSpllFuncCntl = spll_func_cntl;
  1200. table->ACPILevel.CgSpllFuncCntl2 = spll_func_cntl_2;
  1201. table->ACPILevel.CgSpllFuncCntl3 = data->clock_registers.vCG_SPLL_FUNC_CNTL_3;
  1202. table->ACPILevel.CgSpllFuncCntl4 = data->clock_registers.vCG_SPLL_FUNC_CNTL_4;
  1203. table->ACPILevel.SpllSpreadSpectrum = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM;
  1204. table->ACPILevel.SpllSpreadSpectrum2 = data->clock_registers.vCG_SPLL_SPREAD_SPECTRUM_2;
  1205. table->ACPILevel.CcPwrDynRm = 0;
  1206. table->ACPILevel.CcPwrDynRm1 = 0;
  1207. /* For various features to be enabled/disabled while this level is active.*/
  1208. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.Flags);
  1209. /* SCLK frequency in units of 10KHz*/
  1210. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SclkFrequency);
  1211. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl);
  1212. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl2);
  1213. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl3);
  1214. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CgSpllFuncCntl4);
  1215. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum);
  1216. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.SpllSpreadSpectrum2);
  1217. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm);
  1218. CONVERT_FROM_HOST_TO_SMC_UL(table->ACPILevel.CcPwrDynRm1);
  1219. /* table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;*/
  1220. table->MemoryACPILevel.MinVddc = table->ACPILevel.MinVddc;
  1221. table->MemoryACPILevel.MinVddcPhases = table->ACPILevel.MinVddcPhases;
  1222. if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
  1223. table->MemoryACPILevel.MinVddci = table->MemoryACPILevel.MinVddc;
  1224. else {
  1225. if (data->acpi_vddci != 0)
  1226. table->MemoryACPILevel.MinVddci = PP_HOST_TO_SMC_UL(data->acpi_vddci * VOLTAGE_SCALE);
  1227. else
  1228. table->MemoryACPILevel.MinVddci = PP_HOST_TO_SMC_UL(data->min_vddci_in_pptable * VOLTAGE_SCALE);
  1229. }
  1230. if (0 == iceland_populate_mvdd_value(hwmgr, 0, &voltage_level))
  1231. table->MemoryACPILevel.MinMvdd =
  1232. PP_HOST_TO_SMC_UL(voltage_level.Voltage * VOLTAGE_SCALE);
  1233. else
  1234. table->MemoryACPILevel.MinMvdd = 0;
  1235. /* Force reset on DLL*/
  1236. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  1237. MCLK_PWRMGT_CNTL, MRDCK0_RESET, 0x1);
  1238. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  1239. MCLK_PWRMGT_CNTL, MRDCK1_RESET, 0x1);
  1240. /* Disable DLL in ACPIState*/
  1241. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  1242. MCLK_PWRMGT_CNTL, MRDCK0_PDNB, 0);
  1243. mclk_pwrmgt_cntl = PHM_SET_FIELD(mclk_pwrmgt_cntl,
  1244. MCLK_PWRMGT_CNTL, MRDCK1_PDNB, 0);
  1245. /* Enable DLL bypass signal*/
  1246. dll_cntl = PHM_SET_FIELD(dll_cntl,
  1247. DLL_CNTL, MRDCK0_BYPASS, 0);
  1248. dll_cntl = PHM_SET_FIELD(dll_cntl,
  1249. DLL_CNTL, MRDCK1_BYPASS, 0);
  1250. table->MemoryACPILevel.DllCntl =
  1251. PP_HOST_TO_SMC_UL(dll_cntl);
  1252. table->MemoryACPILevel.MclkPwrmgtCntl =
  1253. PP_HOST_TO_SMC_UL(mclk_pwrmgt_cntl);
  1254. table->MemoryACPILevel.MpllAdFuncCntl =
  1255. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_AD_FUNC_CNTL);
  1256. table->MemoryACPILevel.MpllDqFuncCntl =
  1257. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_DQ_FUNC_CNTL);
  1258. table->MemoryACPILevel.MpllFuncCntl =
  1259. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL);
  1260. table->MemoryACPILevel.MpllFuncCntl_1 =
  1261. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL_1);
  1262. table->MemoryACPILevel.MpllFuncCntl_2 =
  1263. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_FUNC_CNTL_2);
  1264. table->MemoryACPILevel.MpllSs1 =
  1265. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_SS1);
  1266. table->MemoryACPILevel.MpllSs2 =
  1267. PP_HOST_TO_SMC_UL(data->clock_registers.vMPLL_SS2);
  1268. table->MemoryACPILevel.EnabledForThrottle = 0;
  1269. table->MemoryACPILevel.EnabledForActivity = 0;
  1270. table->MemoryACPILevel.UpHyst = 0;
  1271. table->MemoryACPILevel.DownHyst = 100;
  1272. table->MemoryACPILevel.VoltageDownHyst = 0;
  1273. /* Indicates maximum activity level for this performance level.*/
  1274. table->MemoryACPILevel.ActivityLevel = PP_HOST_TO_SMC_US(data->current_profile_setting.mclk_activity);
  1275. table->MemoryACPILevel.StutterEnable = 0;
  1276. table->MemoryACPILevel.StrobeEnable = 0;
  1277. table->MemoryACPILevel.EdcReadEnable = 0;
  1278. table->MemoryACPILevel.EdcWriteEnable = 0;
  1279. table->MemoryACPILevel.RttEnable = 0;
  1280. return result;
  1281. }
  1282. static int iceland_populate_smc_uvd_level(struct pp_hwmgr *hwmgr,
  1283. SMU71_Discrete_DpmTable *table)
  1284. {
  1285. return 0;
  1286. }
  1287. static int iceland_populate_smc_vce_level(struct pp_hwmgr *hwmgr,
  1288. SMU71_Discrete_DpmTable *table)
  1289. {
  1290. return 0;
  1291. }
  1292. static int iceland_populate_smc_acp_level(struct pp_hwmgr *hwmgr,
  1293. SMU71_Discrete_DpmTable *table)
  1294. {
  1295. return 0;
  1296. }
  1297. static int iceland_populate_memory_timing_parameters(
  1298. struct pp_hwmgr *hwmgr,
  1299. uint32_t engine_clock,
  1300. uint32_t memory_clock,
  1301. struct SMU71_Discrete_MCArbDramTimingTableEntry *arb_regs
  1302. )
  1303. {
  1304. uint32_t dramTiming;
  1305. uint32_t dramTiming2;
  1306. uint32_t burstTime;
  1307. int result;
  1308. result = atomctrl_set_engine_dram_timings_rv770(hwmgr,
  1309. engine_clock, memory_clock);
  1310. PP_ASSERT_WITH_CODE(result == 0,
  1311. "Error calling VBIOS to set DRAM_TIMING.", return result);
  1312. dramTiming = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING);
  1313. dramTiming2 = cgs_read_register(hwmgr->device, mmMC_ARB_DRAM_TIMING2);
  1314. burstTime = PHM_READ_FIELD(hwmgr->device, MC_ARB_BURST_TIME, STATE0);
  1315. arb_regs->McArbDramTiming = PP_HOST_TO_SMC_UL(dramTiming);
  1316. arb_regs->McArbDramTiming2 = PP_HOST_TO_SMC_UL(dramTiming2);
  1317. arb_regs->McArbBurstTime = (uint8_t)burstTime;
  1318. return 0;
  1319. }
  1320. static int iceland_program_memory_timing_parameters(struct pp_hwmgr *hwmgr)
  1321. {
  1322. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1323. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1324. int result = 0;
  1325. SMU71_Discrete_MCArbDramTimingTable arb_regs;
  1326. uint32_t i, j;
  1327. memset(&arb_regs, 0x00, sizeof(SMU71_Discrete_MCArbDramTimingTable));
  1328. for (i = 0; i < data->dpm_table.sclk_table.count; i++) {
  1329. for (j = 0; j < data->dpm_table.mclk_table.count; j++) {
  1330. result = iceland_populate_memory_timing_parameters
  1331. (hwmgr, data->dpm_table.sclk_table.dpm_levels[i].value,
  1332. data->dpm_table.mclk_table.dpm_levels[j].value,
  1333. &arb_regs.entries[i][j]);
  1334. if (0 != result) {
  1335. break;
  1336. }
  1337. }
  1338. }
  1339. if (0 == result) {
  1340. result = smu7_copy_bytes_to_smc(
  1341. hwmgr,
  1342. smu_data->smu7_data.arb_table_start,
  1343. (uint8_t *)&arb_regs,
  1344. sizeof(SMU71_Discrete_MCArbDramTimingTable),
  1345. SMC_RAM_END
  1346. );
  1347. }
  1348. return result;
  1349. }
  1350. static int iceland_populate_smc_boot_level(struct pp_hwmgr *hwmgr,
  1351. SMU71_Discrete_DpmTable *table)
  1352. {
  1353. int result = 0;
  1354. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1355. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1356. table->GraphicsBootLevel = 0;
  1357. table->MemoryBootLevel = 0;
  1358. /* find boot level from dpm table*/
  1359. result = phm_find_boot_level(&(data->dpm_table.sclk_table),
  1360. data->vbios_boot_state.sclk_bootup_value,
  1361. (uint32_t *)&(smu_data->smc_state_table.GraphicsBootLevel));
  1362. if (0 != result) {
  1363. smu_data->smc_state_table.GraphicsBootLevel = 0;
  1364. pr_err("VBIOS did not find boot engine clock value in dependency table. Using Graphics DPM level 0!\n");
  1365. result = 0;
  1366. }
  1367. result = phm_find_boot_level(&(data->dpm_table.mclk_table),
  1368. data->vbios_boot_state.mclk_bootup_value,
  1369. (uint32_t *)&(smu_data->smc_state_table.MemoryBootLevel));
  1370. if (0 != result) {
  1371. smu_data->smc_state_table.MemoryBootLevel = 0;
  1372. pr_err("VBIOS did not find boot engine clock value in dependency table. Using Memory DPM level 0!\n");
  1373. result = 0;
  1374. }
  1375. table->BootVddc = data->vbios_boot_state.vddc_bootup_value;
  1376. if (SMU7_VOLTAGE_CONTROL_NONE == data->vddci_control)
  1377. table->BootVddci = table->BootVddc;
  1378. else
  1379. table->BootVddci = data->vbios_boot_state.vddci_bootup_value;
  1380. table->BootMVdd = data->vbios_boot_state.mvdd_bootup_value;
  1381. return result;
  1382. }
  1383. static int iceland_populate_mc_reg_address(struct pp_hwmgr *hwmgr,
  1384. SMU71_Discrete_MCRegisters *mc_reg_table)
  1385. {
  1386. const struct iceland_smumgr *smu_data = (struct iceland_smumgr *)hwmgr->smu_backend;
  1387. uint32_t i, j;
  1388. for (i = 0, j = 0; j < smu_data->mc_reg_table.last; j++) {
  1389. if (smu_data->mc_reg_table.validflag & 1<<j) {
  1390. PP_ASSERT_WITH_CODE(i < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE,
  1391. "Index of mc_reg_table->address[] array out of boundary", return -EINVAL);
  1392. mc_reg_table->address[i].s0 =
  1393. PP_HOST_TO_SMC_US(smu_data->mc_reg_table.mc_reg_address[j].s0);
  1394. mc_reg_table->address[i].s1 =
  1395. PP_HOST_TO_SMC_US(smu_data->mc_reg_table.mc_reg_address[j].s1);
  1396. i++;
  1397. }
  1398. }
  1399. mc_reg_table->last = (uint8_t)i;
  1400. return 0;
  1401. }
  1402. /*convert register values from driver to SMC format */
  1403. static void iceland_convert_mc_registers(
  1404. const struct iceland_mc_reg_entry *entry,
  1405. SMU71_Discrete_MCRegisterSet *data,
  1406. uint32_t num_entries, uint32_t valid_flag)
  1407. {
  1408. uint32_t i, j;
  1409. for (i = 0, j = 0; j < num_entries; j++) {
  1410. if (valid_flag & 1<<j) {
  1411. data->value[i] = PP_HOST_TO_SMC_UL(entry->mc_data[j]);
  1412. i++;
  1413. }
  1414. }
  1415. }
  1416. static int iceland_convert_mc_reg_table_entry_to_smc(struct pp_hwmgr *hwmgr,
  1417. const uint32_t memory_clock,
  1418. SMU71_Discrete_MCRegisterSet *mc_reg_table_data
  1419. )
  1420. {
  1421. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1422. uint32_t i = 0;
  1423. for (i = 0; i < smu_data->mc_reg_table.num_entries; i++) {
  1424. if (memory_clock <=
  1425. smu_data->mc_reg_table.mc_reg_table_entry[i].mclk_max) {
  1426. break;
  1427. }
  1428. }
  1429. if ((i == smu_data->mc_reg_table.num_entries) && (i > 0))
  1430. --i;
  1431. iceland_convert_mc_registers(&smu_data->mc_reg_table.mc_reg_table_entry[i],
  1432. mc_reg_table_data, smu_data->mc_reg_table.last,
  1433. smu_data->mc_reg_table.validflag);
  1434. return 0;
  1435. }
  1436. static int iceland_convert_mc_reg_table_to_smc(struct pp_hwmgr *hwmgr,
  1437. SMU71_Discrete_MCRegisters *mc_regs)
  1438. {
  1439. int result = 0;
  1440. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1441. int res;
  1442. uint32_t i;
  1443. for (i = 0; i < data->dpm_table.mclk_table.count; i++) {
  1444. res = iceland_convert_mc_reg_table_entry_to_smc(
  1445. hwmgr,
  1446. data->dpm_table.mclk_table.dpm_levels[i].value,
  1447. &mc_regs->data[i]
  1448. );
  1449. if (0 != res)
  1450. result = res;
  1451. }
  1452. return result;
  1453. }
  1454. static int iceland_update_and_upload_mc_reg_table(struct pp_hwmgr *hwmgr)
  1455. {
  1456. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1457. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1458. uint32_t address;
  1459. int32_t result;
  1460. if (0 == (data->need_update_smu7_dpm_table & DPMTABLE_OD_UPDATE_MCLK))
  1461. return 0;
  1462. memset(&smu_data->mc_regs, 0, sizeof(SMU71_Discrete_MCRegisters));
  1463. result = iceland_convert_mc_reg_table_to_smc(hwmgr, &(smu_data->mc_regs));
  1464. if (result != 0)
  1465. return result;
  1466. address = smu_data->smu7_data.mc_reg_table_start + (uint32_t)offsetof(SMU71_Discrete_MCRegisters, data[0]);
  1467. return smu7_copy_bytes_to_smc(hwmgr, address,
  1468. (uint8_t *)&smu_data->mc_regs.data[0],
  1469. sizeof(SMU71_Discrete_MCRegisterSet) * data->dpm_table.mclk_table.count,
  1470. SMC_RAM_END);
  1471. }
  1472. static int iceland_populate_initial_mc_reg_table(struct pp_hwmgr *hwmgr)
  1473. {
  1474. int result;
  1475. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1476. memset(&smu_data->mc_regs, 0x00, sizeof(SMU71_Discrete_MCRegisters));
  1477. result = iceland_populate_mc_reg_address(hwmgr, &(smu_data->mc_regs));
  1478. PP_ASSERT_WITH_CODE(0 == result,
  1479. "Failed to initialize MCRegTable for the MC register addresses!", return result;);
  1480. result = iceland_convert_mc_reg_table_to_smc(hwmgr, &smu_data->mc_regs);
  1481. PP_ASSERT_WITH_CODE(0 == result,
  1482. "Failed to initialize MCRegTable for driver state!", return result;);
  1483. return smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.mc_reg_table_start,
  1484. (uint8_t *)&smu_data->mc_regs, sizeof(SMU71_Discrete_MCRegisters), SMC_RAM_END);
  1485. }
  1486. static int iceland_populate_smc_initial_state(struct pp_hwmgr *hwmgr)
  1487. {
  1488. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1489. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1490. uint8_t count, level;
  1491. count = (uint8_t)(hwmgr->dyn_state.vddc_dependency_on_sclk->count);
  1492. for (level = 0; level < count; level++) {
  1493. if (hwmgr->dyn_state.vddc_dependency_on_sclk->entries[level].clk
  1494. >= data->vbios_boot_state.sclk_bootup_value) {
  1495. smu_data->smc_state_table.GraphicsBootLevel = level;
  1496. break;
  1497. }
  1498. }
  1499. count = (uint8_t)(hwmgr->dyn_state.vddc_dependency_on_mclk->count);
  1500. for (level = 0; level < count; level++) {
  1501. if (hwmgr->dyn_state.vddc_dependency_on_mclk->entries[level].clk
  1502. >= data->vbios_boot_state.mclk_bootup_value) {
  1503. smu_data->smc_state_table.MemoryBootLevel = level;
  1504. break;
  1505. }
  1506. }
  1507. return 0;
  1508. }
  1509. static int iceland_populate_bapm_parameters_in_dpm_table(struct pp_hwmgr *hwmgr)
  1510. {
  1511. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1512. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1513. const struct iceland_pt_defaults *defaults = smu_data->power_tune_defaults;
  1514. SMU71_Discrete_DpmTable *dpm_table = &(smu_data->smc_state_table);
  1515. struct phm_cac_tdp_table *cac_dtp_table = hwmgr->dyn_state.cac_dtp_table;
  1516. struct phm_ppm_table *ppm = hwmgr->dyn_state.ppm_parameter_table;
  1517. const uint16_t *def1, *def2;
  1518. int i, j, k;
  1519. /*
  1520. * TDP number of fraction bits are changed from 8 to 7 for Iceland
  1521. * as requested by SMC team
  1522. */
  1523. dpm_table->DefaultTdp = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usTDP * 256));
  1524. dpm_table->TargetTdp = PP_HOST_TO_SMC_US((uint16_t)(cac_dtp_table->usConfigurableTDP * 256));
  1525. dpm_table->DTETjOffset = 0;
  1526. dpm_table->GpuTjMax = (uint8_t)(data->thermal_temp_setting.temperature_high / PP_TEMPERATURE_UNITS_PER_CENTIGRADES);
  1527. dpm_table->GpuTjHyst = 8;
  1528. dpm_table->DTEAmbientTempBase = defaults->dte_ambient_temp_base;
  1529. /* The following are for new Iceland Multi-input fan/thermal control */
  1530. if (NULL != ppm) {
  1531. dpm_table->PPM_PkgPwrLimit = (uint16_t)ppm->dgpu_tdp * 256 / 1000;
  1532. dpm_table->PPM_TemperatureLimit = (uint16_t)ppm->tj_max * 256;
  1533. } else {
  1534. dpm_table->PPM_PkgPwrLimit = 0;
  1535. dpm_table->PPM_TemperatureLimit = 0;
  1536. }
  1537. CONVERT_FROM_HOST_TO_SMC_US(dpm_table->PPM_PkgPwrLimit);
  1538. CONVERT_FROM_HOST_TO_SMC_US(dpm_table->PPM_TemperatureLimit);
  1539. dpm_table->BAPM_TEMP_GRADIENT = PP_HOST_TO_SMC_UL(defaults->bapm_temp_gradient);
  1540. def1 = defaults->bapmti_r;
  1541. def2 = defaults->bapmti_rc;
  1542. for (i = 0; i < SMU71_DTE_ITERATIONS; i++) {
  1543. for (j = 0; j < SMU71_DTE_SOURCES; j++) {
  1544. for (k = 0; k < SMU71_DTE_SINKS; k++) {
  1545. dpm_table->BAPMTI_R[i][j][k] = PP_HOST_TO_SMC_US(*def1);
  1546. dpm_table->BAPMTI_RC[i][j][k] = PP_HOST_TO_SMC_US(*def2);
  1547. def1++;
  1548. def2++;
  1549. }
  1550. }
  1551. }
  1552. return 0;
  1553. }
  1554. static int iceland_populate_smc_svi2_config(struct pp_hwmgr *hwmgr,
  1555. SMU71_Discrete_DpmTable *tab)
  1556. {
  1557. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1558. if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->voltage_control)
  1559. tab->SVI2Enable |= VDDC_ON_SVI2;
  1560. if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->vddci_control)
  1561. tab->SVI2Enable |= VDDCI_ON_SVI2;
  1562. else
  1563. tab->MergedVddci = 1;
  1564. if (SMU7_VOLTAGE_CONTROL_BY_SVID2 == data->mvdd_control)
  1565. tab->SVI2Enable |= MVDD_ON_SVI2;
  1566. PP_ASSERT_WITH_CODE(tab->SVI2Enable != (VDDC_ON_SVI2 | VDDCI_ON_SVI2 | MVDD_ON_SVI2) &&
  1567. (tab->SVI2Enable & VDDC_ON_SVI2), "SVI2 domain configuration is incorrect!", return -EINVAL);
  1568. return 0;
  1569. }
  1570. static int iceland_init_smc_table(struct pp_hwmgr *hwmgr)
  1571. {
  1572. int result;
  1573. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1574. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1575. SMU71_Discrete_DpmTable *table = &(smu_data->smc_state_table);
  1576. iceland_initialize_power_tune_defaults(hwmgr);
  1577. memset(&(smu_data->smc_state_table), 0x00, sizeof(smu_data->smc_state_table));
  1578. if (SMU7_VOLTAGE_CONTROL_NONE != data->voltage_control) {
  1579. iceland_populate_smc_voltage_tables(hwmgr, table);
  1580. }
  1581. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1582. PHM_PlatformCaps_AutomaticDCTransition))
  1583. table->SystemFlags |= PPSMC_SYSTEMFLAG_GPIO_DC;
  1584. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1585. PHM_PlatformCaps_StepVddc))
  1586. table->SystemFlags |= PPSMC_SYSTEMFLAG_STEPVDDC;
  1587. if (data->is_memory_gddr5)
  1588. table->SystemFlags |= PPSMC_SYSTEMFLAG_GDDR5;
  1589. if (data->ulv_supported) {
  1590. result = iceland_populate_ulv_state(hwmgr, &(smu_data->ulv_setting));
  1591. PP_ASSERT_WITH_CODE(0 == result,
  1592. "Failed to initialize ULV state!", return result;);
  1593. cgs_write_ind_register(hwmgr->device, CGS_IND_REG__SMC,
  1594. ixCG_ULV_PARAMETER, 0x40035);
  1595. }
  1596. result = iceland_populate_smc_link_level(hwmgr, table);
  1597. PP_ASSERT_WITH_CODE(0 == result,
  1598. "Failed to initialize Link Level!", return result;);
  1599. result = iceland_populate_all_graphic_levels(hwmgr);
  1600. PP_ASSERT_WITH_CODE(0 == result,
  1601. "Failed to initialize Graphics Level!", return result;);
  1602. result = iceland_populate_all_memory_levels(hwmgr);
  1603. PP_ASSERT_WITH_CODE(0 == result,
  1604. "Failed to initialize Memory Level!", return result;);
  1605. result = iceland_populate_smc_acpi_level(hwmgr, table);
  1606. PP_ASSERT_WITH_CODE(0 == result,
  1607. "Failed to initialize ACPI Level!", return result;);
  1608. result = iceland_populate_smc_vce_level(hwmgr, table);
  1609. PP_ASSERT_WITH_CODE(0 == result,
  1610. "Failed to initialize VCE Level!", return result;);
  1611. result = iceland_populate_smc_acp_level(hwmgr, table);
  1612. PP_ASSERT_WITH_CODE(0 == result,
  1613. "Failed to initialize ACP Level!", return result;);
  1614. /* Since only the initial state is completely set up at this point (the other states are just copies of the boot state) we only */
  1615. /* need to populate the ARB settings for the initial state. */
  1616. result = iceland_program_memory_timing_parameters(hwmgr);
  1617. PP_ASSERT_WITH_CODE(0 == result,
  1618. "Failed to Write ARB settings for the initial state.", return result;);
  1619. result = iceland_populate_smc_uvd_level(hwmgr, table);
  1620. PP_ASSERT_WITH_CODE(0 == result,
  1621. "Failed to initialize UVD Level!", return result;);
  1622. table->GraphicsBootLevel = 0;
  1623. table->MemoryBootLevel = 0;
  1624. result = iceland_populate_smc_boot_level(hwmgr, table);
  1625. PP_ASSERT_WITH_CODE(0 == result,
  1626. "Failed to initialize Boot Level!", return result;);
  1627. result = iceland_populate_smc_initial_state(hwmgr);
  1628. PP_ASSERT_WITH_CODE(0 == result, "Failed to initialize Boot State!", return result);
  1629. result = iceland_populate_bapm_parameters_in_dpm_table(hwmgr);
  1630. PP_ASSERT_WITH_CODE(0 == result, "Failed to populate BAPM Parameters!", return result);
  1631. table->GraphicsVoltageChangeEnable = 1;
  1632. table->GraphicsThermThrottleEnable = 1;
  1633. table->GraphicsInterval = 1;
  1634. table->VoltageInterval = 1;
  1635. table->ThermalInterval = 1;
  1636. table->TemperatureLimitHigh =
  1637. (data->thermal_temp_setting.temperature_high *
  1638. SMU7_Q88_FORMAT_CONVERSION_UNIT) / PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  1639. table->TemperatureLimitLow =
  1640. (data->thermal_temp_setting.temperature_low *
  1641. SMU7_Q88_FORMAT_CONVERSION_UNIT) / PP_TEMPERATURE_UNITS_PER_CENTIGRADES;
  1642. table->MemoryVoltageChangeEnable = 1;
  1643. table->MemoryInterval = 1;
  1644. table->VoltageResponseTime = 0;
  1645. table->PhaseResponseTime = 0;
  1646. table->MemoryThermThrottleEnable = 1;
  1647. table->PCIeBootLinkLevel = 0;
  1648. table->PCIeGenInterval = 1;
  1649. result = iceland_populate_smc_svi2_config(hwmgr, table);
  1650. PP_ASSERT_WITH_CODE(0 == result,
  1651. "Failed to populate SVI2 setting!", return result);
  1652. table->ThermGpio = 17;
  1653. table->SclkStepSize = 0x4000;
  1654. CONVERT_FROM_HOST_TO_SMC_UL(table->SystemFlags);
  1655. CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddcVid);
  1656. CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddcPhase);
  1657. CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskVddciVid);
  1658. CONVERT_FROM_HOST_TO_SMC_UL(table->SmioMaskMvddVid);
  1659. CONVERT_FROM_HOST_TO_SMC_UL(table->SclkStepSize);
  1660. CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitHigh);
  1661. CONVERT_FROM_HOST_TO_SMC_US(table->TemperatureLimitLow);
  1662. CONVERT_FROM_HOST_TO_SMC_US(table->VoltageResponseTime);
  1663. CONVERT_FROM_HOST_TO_SMC_US(table->PhaseResponseTime);
  1664. table->BootVddc = PP_HOST_TO_SMC_US(table->BootVddc * VOLTAGE_SCALE);
  1665. table->BootVddci = PP_HOST_TO_SMC_US(table->BootVddci * VOLTAGE_SCALE);
  1666. table->BootMVdd = PP_HOST_TO_SMC_US(table->BootMVdd * VOLTAGE_SCALE);
  1667. /* Upload all dpm data to SMC memory.(dpm level, dpm level count etc) */
  1668. result = smu7_copy_bytes_to_smc(hwmgr, smu_data->smu7_data.dpm_table_start +
  1669. offsetof(SMU71_Discrete_DpmTable, SystemFlags),
  1670. (uint8_t *)&(table->SystemFlags),
  1671. sizeof(SMU71_Discrete_DpmTable)-3 * sizeof(SMU71_PIDController),
  1672. SMC_RAM_END);
  1673. PP_ASSERT_WITH_CODE(0 == result,
  1674. "Failed to upload dpm data to SMC memory!", return result;);
  1675. /* Upload all ulv setting to SMC memory.(dpm level, dpm level count etc) */
  1676. result = smu7_copy_bytes_to_smc(hwmgr,
  1677. smu_data->smu7_data.ulv_setting_starts,
  1678. (uint8_t *)&(smu_data->ulv_setting),
  1679. sizeof(SMU71_Discrete_Ulv),
  1680. SMC_RAM_END);
  1681. result = iceland_populate_initial_mc_reg_table(hwmgr);
  1682. PP_ASSERT_WITH_CODE((0 == result),
  1683. "Failed to populate initialize MC Reg table!", return result);
  1684. result = iceland_populate_pm_fuses(hwmgr);
  1685. PP_ASSERT_WITH_CODE(0 == result,
  1686. "Failed to populate PM fuses to SMC memory!", return result);
  1687. return 0;
  1688. }
  1689. int iceland_thermal_setup_fan_table(struct pp_hwmgr *hwmgr)
  1690. {
  1691. struct smu7_smumgr *smu7_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
  1692. SMU71_Discrete_FanTable fan_table = { FDO_MODE_HARDWARE };
  1693. uint32_t duty100;
  1694. uint32_t t_diff1, t_diff2, pwm_diff1, pwm_diff2;
  1695. uint16_t fdo_min, slope1, slope2;
  1696. uint32_t reference_clock;
  1697. int res;
  1698. uint64_t tmp64;
  1699. if (!phm_cap_enabled(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl))
  1700. return 0;
  1701. if (hwmgr->thermal_controller.fanInfo.bNoFan) {
  1702. phm_cap_unset(hwmgr->platform_descriptor.platformCaps,
  1703. PHM_PlatformCaps_MicrocodeFanControl);
  1704. return 0;
  1705. }
  1706. if (0 == smu7_data->fan_table_start) {
  1707. phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl);
  1708. return 0;
  1709. }
  1710. duty100 = PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_FDO_CTRL1, FMAX_DUTY100);
  1711. if (0 == duty100) {
  1712. phm_cap_unset(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl);
  1713. return 0;
  1714. }
  1715. tmp64 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin * duty100;
  1716. do_div(tmp64, 10000);
  1717. fdo_min = (uint16_t)tmp64;
  1718. t_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usTMed - hwmgr->thermal_controller.advanceFanControlParameters.usTMin;
  1719. t_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usTHigh - hwmgr->thermal_controller.advanceFanControlParameters.usTMed;
  1720. pwm_diff1 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed - hwmgr->thermal_controller.advanceFanControlParameters.usPWMMin;
  1721. pwm_diff2 = hwmgr->thermal_controller.advanceFanControlParameters.usPWMHigh - hwmgr->thermal_controller.advanceFanControlParameters.usPWMMed;
  1722. slope1 = (uint16_t)((50 + ((16 * duty100 * pwm_diff1) / t_diff1)) / 100);
  1723. slope2 = (uint16_t)((50 + ((16 * duty100 * pwm_diff2) / t_diff2)) / 100);
  1724. fan_table.TempMin = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMin) / 100);
  1725. fan_table.TempMed = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMed) / 100);
  1726. fan_table.TempMax = cpu_to_be16((50 + hwmgr->thermal_controller.advanceFanControlParameters.usTMax) / 100);
  1727. fan_table.Slope1 = cpu_to_be16(slope1);
  1728. fan_table.Slope2 = cpu_to_be16(slope2);
  1729. fan_table.FdoMin = cpu_to_be16(fdo_min);
  1730. fan_table.HystDown = cpu_to_be16(hwmgr->thermal_controller.advanceFanControlParameters.ucTHyst);
  1731. fan_table.HystUp = cpu_to_be16(1);
  1732. fan_table.HystSlope = cpu_to_be16(1);
  1733. fan_table.TempRespLim = cpu_to_be16(5);
  1734. reference_clock = amdgpu_asic_get_xclk((struct amdgpu_device *)hwmgr->adev);
  1735. fan_table.RefreshPeriod = cpu_to_be32((hwmgr->thermal_controller.advanceFanControlParameters.ulCycleDelay * reference_clock) / 1600);
  1736. fan_table.FdoMax = cpu_to_be16((uint16_t)duty100);
  1737. fan_table.TempSrc = (uint8_t)PHM_READ_VFPF_INDIRECT_FIELD(hwmgr->device, CGS_IND_REG__SMC, CG_MULT_THERMAL_CTRL, TEMP_SEL);
  1738. /* fan_table.FanControl_GL_Flag = 1; */
  1739. res = smu7_copy_bytes_to_smc(hwmgr, smu7_data->fan_table_start, (uint8_t *)&fan_table, (uint32_t)sizeof(fan_table), SMC_RAM_END);
  1740. return 0;
  1741. }
  1742. static int iceland_program_mem_timing_parameters(struct pp_hwmgr *hwmgr)
  1743. {
  1744. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1745. if (data->need_update_smu7_dpm_table &
  1746. (DPMTABLE_OD_UPDATE_SCLK + DPMTABLE_OD_UPDATE_MCLK))
  1747. return iceland_program_memory_timing_parameters(hwmgr);
  1748. return 0;
  1749. }
  1750. static int iceland_update_sclk_threshold(struct pp_hwmgr *hwmgr)
  1751. {
  1752. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1753. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  1754. int result = 0;
  1755. uint32_t low_sclk_interrupt_threshold = 0;
  1756. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1757. PHM_PlatformCaps_SclkThrottleLowNotification)
  1758. && (data->low_sclk_interrupt_threshold != 0)) {
  1759. low_sclk_interrupt_threshold =
  1760. data->low_sclk_interrupt_threshold;
  1761. CONVERT_FROM_HOST_TO_SMC_UL(low_sclk_interrupt_threshold);
  1762. result = smu7_copy_bytes_to_smc(
  1763. hwmgr,
  1764. smu_data->smu7_data.dpm_table_start +
  1765. offsetof(SMU71_Discrete_DpmTable,
  1766. LowSclkInterruptThreshold),
  1767. (uint8_t *)&low_sclk_interrupt_threshold,
  1768. sizeof(uint32_t),
  1769. SMC_RAM_END);
  1770. }
  1771. result = iceland_update_and_upload_mc_reg_table(hwmgr);
  1772. PP_ASSERT_WITH_CODE((0 == result), "Failed to upload MC reg table!", return result);
  1773. result = iceland_program_mem_timing_parameters(hwmgr);
  1774. PP_ASSERT_WITH_CODE((result == 0),
  1775. "Failed to program memory timing parameters!",
  1776. );
  1777. return result;
  1778. }
  1779. static uint32_t iceland_get_offsetof(uint32_t type, uint32_t member)
  1780. {
  1781. switch (type) {
  1782. case SMU_SoftRegisters:
  1783. switch (member) {
  1784. case HandshakeDisables:
  1785. return offsetof(SMU71_SoftRegisters, HandshakeDisables);
  1786. case VoltageChangeTimeout:
  1787. return offsetof(SMU71_SoftRegisters, VoltageChangeTimeout);
  1788. case AverageGraphicsActivity:
  1789. return offsetof(SMU71_SoftRegisters, AverageGraphicsActivity);
  1790. case PreVBlankGap:
  1791. return offsetof(SMU71_SoftRegisters, PreVBlankGap);
  1792. case VBlankTimeout:
  1793. return offsetof(SMU71_SoftRegisters, VBlankTimeout);
  1794. case UcodeLoadStatus:
  1795. return offsetof(SMU71_SoftRegisters, UcodeLoadStatus);
  1796. case DRAM_LOG_ADDR_H:
  1797. return offsetof(SMU71_SoftRegisters, DRAM_LOG_ADDR_H);
  1798. case DRAM_LOG_ADDR_L:
  1799. return offsetof(SMU71_SoftRegisters, DRAM_LOG_ADDR_L);
  1800. case DRAM_LOG_PHY_ADDR_H:
  1801. return offsetof(SMU71_SoftRegisters, DRAM_LOG_PHY_ADDR_H);
  1802. case DRAM_LOG_PHY_ADDR_L:
  1803. return offsetof(SMU71_SoftRegisters, DRAM_LOG_PHY_ADDR_L);
  1804. case DRAM_LOG_BUFF_SIZE:
  1805. return offsetof(SMU71_SoftRegisters, DRAM_LOG_BUFF_SIZE);
  1806. }
  1807. break;
  1808. case SMU_Discrete_DpmTable:
  1809. switch (member) {
  1810. case LowSclkInterruptThreshold:
  1811. return offsetof(SMU71_Discrete_DpmTable, LowSclkInterruptThreshold);
  1812. }
  1813. break;
  1814. }
  1815. pr_warn("can't get the offset of type %x member %x\n", type, member);
  1816. return 0;
  1817. }
  1818. static uint32_t iceland_get_mac_definition(uint32_t value)
  1819. {
  1820. switch (value) {
  1821. case SMU_MAX_LEVELS_GRAPHICS:
  1822. return SMU71_MAX_LEVELS_GRAPHICS;
  1823. case SMU_MAX_LEVELS_MEMORY:
  1824. return SMU71_MAX_LEVELS_MEMORY;
  1825. case SMU_MAX_LEVELS_LINK:
  1826. return SMU71_MAX_LEVELS_LINK;
  1827. case SMU_MAX_ENTRIES_SMIO:
  1828. return SMU71_MAX_ENTRIES_SMIO;
  1829. case SMU_MAX_LEVELS_VDDC:
  1830. return SMU71_MAX_LEVELS_VDDC;
  1831. case SMU_MAX_LEVELS_VDDCI:
  1832. return SMU71_MAX_LEVELS_VDDCI;
  1833. case SMU_MAX_LEVELS_MVDD:
  1834. return SMU71_MAX_LEVELS_MVDD;
  1835. }
  1836. pr_warn("can't get the mac of %x\n", value);
  1837. return 0;
  1838. }
  1839. static int iceland_process_firmware_header(struct pp_hwmgr *hwmgr)
  1840. {
  1841. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  1842. struct smu7_smumgr *smu7_data = (struct smu7_smumgr *)(hwmgr->smu_backend);
  1843. uint32_t tmp;
  1844. int result;
  1845. bool error = false;
  1846. result = smu7_read_smc_sram_dword(hwmgr,
  1847. SMU71_FIRMWARE_HEADER_LOCATION +
  1848. offsetof(SMU71_Firmware_Header, DpmTable),
  1849. &tmp, SMC_RAM_END);
  1850. if (0 == result) {
  1851. smu7_data->dpm_table_start = tmp;
  1852. }
  1853. error |= (0 != result);
  1854. result = smu7_read_smc_sram_dword(hwmgr,
  1855. SMU71_FIRMWARE_HEADER_LOCATION +
  1856. offsetof(SMU71_Firmware_Header, SoftRegisters),
  1857. &tmp, SMC_RAM_END);
  1858. if (0 == result) {
  1859. data->soft_regs_start = tmp;
  1860. smu7_data->soft_regs_start = tmp;
  1861. }
  1862. error |= (0 != result);
  1863. result = smu7_read_smc_sram_dword(hwmgr,
  1864. SMU71_FIRMWARE_HEADER_LOCATION +
  1865. offsetof(SMU71_Firmware_Header, mcRegisterTable),
  1866. &tmp, SMC_RAM_END);
  1867. if (0 == result) {
  1868. smu7_data->mc_reg_table_start = tmp;
  1869. }
  1870. result = smu7_read_smc_sram_dword(hwmgr,
  1871. SMU71_FIRMWARE_HEADER_LOCATION +
  1872. offsetof(SMU71_Firmware_Header, FanTable),
  1873. &tmp, SMC_RAM_END);
  1874. if (0 == result) {
  1875. smu7_data->fan_table_start = tmp;
  1876. }
  1877. error |= (0 != result);
  1878. result = smu7_read_smc_sram_dword(hwmgr,
  1879. SMU71_FIRMWARE_HEADER_LOCATION +
  1880. offsetof(SMU71_Firmware_Header, mcArbDramTimingTable),
  1881. &tmp, SMC_RAM_END);
  1882. if (0 == result) {
  1883. smu7_data->arb_table_start = tmp;
  1884. }
  1885. error |= (0 != result);
  1886. result = smu7_read_smc_sram_dword(hwmgr,
  1887. SMU71_FIRMWARE_HEADER_LOCATION +
  1888. offsetof(SMU71_Firmware_Header, Version),
  1889. &tmp, SMC_RAM_END);
  1890. if (0 == result) {
  1891. hwmgr->microcode_version_info.SMC = tmp;
  1892. }
  1893. error |= (0 != result);
  1894. result = smu7_read_smc_sram_dword(hwmgr,
  1895. SMU71_FIRMWARE_HEADER_LOCATION +
  1896. offsetof(SMU71_Firmware_Header, UlvSettings),
  1897. &tmp, SMC_RAM_END);
  1898. if (0 == result) {
  1899. smu7_data->ulv_setting_starts = tmp;
  1900. }
  1901. error |= (0 != result);
  1902. return error ? 1 : 0;
  1903. }
  1904. /*---------------------------MC----------------------------*/
  1905. static uint8_t iceland_get_memory_modile_index(struct pp_hwmgr *hwmgr)
  1906. {
  1907. return (uint8_t) (0xFF & (cgs_read_register(hwmgr->device, mmBIOS_SCRATCH_4) >> 16));
  1908. }
  1909. static bool iceland_check_s0_mc_reg_index(uint16_t in_reg, uint16_t *out_reg)
  1910. {
  1911. bool result = true;
  1912. switch (in_reg) {
  1913. case mmMC_SEQ_RAS_TIMING:
  1914. *out_reg = mmMC_SEQ_RAS_TIMING_LP;
  1915. break;
  1916. case mmMC_SEQ_DLL_STBY:
  1917. *out_reg = mmMC_SEQ_DLL_STBY_LP;
  1918. break;
  1919. case mmMC_SEQ_G5PDX_CMD0:
  1920. *out_reg = mmMC_SEQ_G5PDX_CMD0_LP;
  1921. break;
  1922. case mmMC_SEQ_G5PDX_CMD1:
  1923. *out_reg = mmMC_SEQ_G5PDX_CMD1_LP;
  1924. break;
  1925. case mmMC_SEQ_G5PDX_CTRL:
  1926. *out_reg = mmMC_SEQ_G5PDX_CTRL_LP;
  1927. break;
  1928. case mmMC_SEQ_CAS_TIMING:
  1929. *out_reg = mmMC_SEQ_CAS_TIMING_LP;
  1930. break;
  1931. case mmMC_SEQ_MISC_TIMING:
  1932. *out_reg = mmMC_SEQ_MISC_TIMING_LP;
  1933. break;
  1934. case mmMC_SEQ_MISC_TIMING2:
  1935. *out_reg = mmMC_SEQ_MISC_TIMING2_LP;
  1936. break;
  1937. case mmMC_SEQ_PMG_DVS_CMD:
  1938. *out_reg = mmMC_SEQ_PMG_DVS_CMD_LP;
  1939. break;
  1940. case mmMC_SEQ_PMG_DVS_CTL:
  1941. *out_reg = mmMC_SEQ_PMG_DVS_CTL_LP;
  1942. break;
  1943. case mmMC_SEQ_RD_CTL_D0:
  1944. *out_reg = mmMC_SEQ_RD_CTL_D0_LP;
  1945. break;
  1946. case mmMC_SEQ_RD_CTL_D1:
  1947. *out_reg = mmMC_SEQ_RD_CTL_D1_LP;
  1948. break;
  1949. case mmMC_SEQ_WR_CTL_D0:
  1950. *out_reg = mmMC_SEQ_WR_CTL_D0_LP;
  1951. break;
  1952. case mmMC_SEQ_WR_CTL_D1:
  1953. *out_reg = mmMC_SEQ_WR_CTL_D1_LP;
  1954. break;
  1955. case mmMC_PMG_CMD_EMRS:
  1956. *out_reg = mmMC_SEQ_PMG_CMD_EMRS_LP;
  1957. break;
  1958. case mmMC_PMG_CMD_MRS:
  1959. *out_reg = mmMC_SEQ_PMG_CMD_MRS_LP;
  1960. break;
  1961. case mmMC_PMG_CMD_MRS1:
  1962. *out_reg = mmMC_SEQ_PMG_CMD_MRS1_LP;
  1963. break;
  1964. case mmMC_SEQ_PMG_TIMING:
  1965. *out_reg = mmMC_SEQ_PMG_TIMING_LP;
  1966. break;
  1967. case mmMC_PMG_CMD_MRS2:
  1968. *out_reg = mmMC_SEQ_PMG_CMD_MRS2_LP;
  1969. break;
  1970. case mmMC_SEQ_WR_CTL_2:
  1971. *out_reg = mmMC_SEQ_WR_CTL_2_LP;
  1972. break;
  1973. default:
  1974. result = false;
  1975. break;
  1976. }
  1977. return result;
  1978. }
  1979. static int iceland_set_s0_mc_reg_index(struct iceland_mc_reg_table *table)
  1980. {
  1981. uint32_t i;
  1982. uint16_t address;
  1983. for (i = 0; i < table->last; i++) {
  1984. table->mc_reg_address[i].s0 =
  1985. iceland_check_s0_mc_reg_index(table->mc_reg_address[i].s1, &address)
  1986. ? address : table->mc_reg_address[i].s1;
  1987. }
  1988. return 0;
  1989. }
  1990. static int iceland_copy_vbios_smc_reg_table(const pp_atomctrl_mc_reg_table *table,
  1991. struct iceland_mc_reg_table *ni_table)
  1992. {
  1993. uint8_t i, j;
  1994. PP_ASSERT_WITH_CODE((table->last <= SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
  1995. "Invalid VramInfo table.", return -EINVAL);
  1996. PP_ASSERT_WITH_CODE((table->num_entries <= MAX_AC_TIMING_ENTRIES),
  1997. "Invalid VramInfo table.", return -EINVAL);
  1998. for (i = 0; i < table->last; i++) {
  1999. ni_table->mc_reg_address[i].s1 = table->mc_reg_address[i].s1;
  2000. }
  2001. ni_table->last = table->last;
  2002. for (i = 0; i < table->num_entries; i++) {
  2003. ni_table->mc_reg_table_entry[i].mclk_max =
  2004. table->mc_reg_table_entry[i].mclk_max;
  2005. for (j = 0; j < table->last; j++) {
  2006. ni_table->mc_reg_table_entry[i].mc_data[j] =
  2007. table->mc_reg_table_entry[i].mc_data[j];
  2008. }
  2009. }
  2010. ni_table->num_entries = table->num_entries;
  2011. return 0;
  2012. }
  2013. static int iceland_set_mc_special_registers(struct pp_hwmgr *hwmgr,
  2014. struct iceland_mc_reg_table *table)
  2015. {
  2016. uint8_t i, j, k;
  2017. uint32_t temp_reg;
  2018. struct smu7_hwmgr *data = (struct smu7_hwmgr *)(hwmgr->backend);
  2019. for (i = 0, j = table->last; i < table->last; i++) {
  2020. PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
  2021. "Invalid VramInfo table.", return -EINVAL);
  2022. switch (table->mc_reg_address[i].s1) {
  2023. case mmMC_SEQ_MISC1:
  2024. temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_EMRS);
  2025. table->mc_reg_address[j].s1 = mmMC_PMG_CMD_EMRS;
  2026. table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_EMRS_LP;
  2027. for (k = 0; k < table->num_entries; k++) {
  2028. table->mc_reg_table_entry[k].mc_data[j] =
  2029. ((temp_reg & 0xffff0000)) |
  2030. ((table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16);
  2031. }
  2032. j++;
  2033. PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
  2034. "Invalid VramInfo table.", return -EINVAL);
  2035. temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS);
  2036. table->mc_reg_address[j].s1 = mmMC_PMG_CMD_MRS;
  2037. table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_MRS_LP;
  2038. for (k = 0; k < table->num_entries; k++) {
  2039. table->mc_reg_table_entry[k].mc_data[j] =
  2040. (temp_reg & 0xffff0000) |
  2041. (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
  2042. if (!data->is_memory_gddr5) {
  2043. table->mc_reg_table_entry[k].mc_data[j] |= 0x100;
  2044. }
  2045. }
  2046. j++;
  2047. if (!data->is_memory_gddr5) {
  2048. PP_ASSERT_WITH_CODE((j < SMU71_DISCRETE_MC_REGISTER_ARRAY_SIZE),
  2049. "Invalid VramInfo table.", return -EINVAL);
  2050. table->mc_reg_address[j].s1 = mmMC_PMG_AUTO_CMD;
  2051. table->mc_reg_address[j].s0 = mmMC_PMG_AUTO_CMD;
  2052. for (k = 0; k < table->num_entries; k++) {
  2053. table->mc_reg_table_entry[k].mc_data[j] =
  2054. (table->mc_reg_table_entry[k].mc_data[i] & 0xffff0000) >> 16;
  2055. }
  2056. j++;
  2057. }
  2058. break;
  2059. case mmMC_SEQ_RESERVE_M:
  2060. temp_reg = cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS1);
  2061. table->mc_reg_address[j].s1 = mmMC_PMG_CMD_MRS1;
  2062. table->mc_reg_address[j].s0 = mmMC_SEQ_PMG_CMD_MRS1_LP;
  2063. for (k = 0; k < table->num_entries; k++) {
  2064. table->mc_reg_table_entry[k].mc_data[j] =
  2065. (temp_reg & 0xffff0000) |
  2066. (table->mc_reg_table_entry[k].mc_data[i] & 0x0000ffff);
  2067. }
  2068. j++;
  2069. break;
  2070. default:
  2071. break;
  2072. }
  2073. }
  2074. table->last = j;
  2075. return 0;
  2076. }
  2077. static int iceland_set_valid_flag(struct iceland_mc_reg_table *table)
  2078. {
  2079. uint8_t i, j;
  2080. for (i = 0; i < table->last; i++) {
  2081. for (j = 1; j < table->num_entries; j++) {
  2082. if (table->mc_reg_table_entry[j-1].mc_data[i] !=
  2083. table->mc_reg_table_entry[j].mc_data[i]) {
  2084. table->validflag |= (1<<i);
  2085. break;
  2086. }
  2087. }
  2088. }
  2089. return 0;
  2090. }
  2091. static int iceland_initialize_mc_reg_table(struct pp_hwmgr *hwmgr)
  2092. {
  2093. int result;
  2094. struct iceland_smumgr *smu_data = (struct iceland_smumgr *)(hwmgr->smu_backend);
  2095. pp_atomctrl_mc_reg_table *table;
  2096. struct iceland_mc_reg_table *ni_table = &smu_data->mc_reg_table;
  2097. uint8_t module_index = iceland_get_memory_modile_index(hwmgr);
  2098. table = kzalloc(sizeof(pp_atomctrl_mc_reg_table), GFP_KERNEL);
  2099. if (NULL == table)
  2100. return -ENOMEM;
  2101. /* Program additional LP registers that are no longer programmed by VBIOS */
  2102. cgs_write_register(hwmgr->device, mmMC_SEQ_RAS_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RAS_TIMING));
  2103. cgs_write_register(hwmgr->device, mmMC_SEQ_CAS_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_CAS_TIMING));
  2104. cgs_write_register(hwmgr->device, mmMC_SEQ_DLL_STBY_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_DLL_STBY));
  2105. cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD0));
  2106. cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CMD1));
  2107. cgs_write_register(hwmgr->device, mmMC_SEQ_G5PDX_CTRL_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_G5PDX_CTRL));
  2108. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CMD_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CMD));
  2109. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CTL_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_DVS_CTL));
  2110. cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING));
  2111. cgs_write_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_MISC_TIMING2));
  2112. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_EMRS_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_EMRS));
  2113. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS));
  2114. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS1_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS1));
  2115. cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D0));
  2116. cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_D1));
  2117. cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D0));
  2118. cgs_write_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_RD_CTL_D1));
  2119. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_TIMING_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_PMG_TIMING));
  2120. cgs_write_register(hwmgr->device, mmMC_SEQ_PMG_CMD_MRS2_LP, cgs_read_register(hwmgr->device, mmMC_PMG_CMD_MRS2));
  2121. cgs_write_register(hwmgr->device, mmMC_SEQ_WR_CTL_2_LP, cgs_read_register(hwmgr->device, mmMC_SEQ_WR_CTL_2));
  2122. memset(table, 0x00, sizeof(pp_atomctrl_mc_reg_table));
  2123. result = atomctrl_initialize_mc_reg_table(hwmgr, module_index, table);
  2124. if (0 == result)
  2125. result = iceland_copy_vbios_smc_reg_table(table, ni_table);
  2126. if (0 == result) {
  2127. iceland_set_s0_mc_reg_index(ni_table);
  2128. result = iceland_set_mc_special_registers(hwmgr, ni_table);
  2129. }
  2130. if (0 == result)
  2131. iceland_set_valid_flag(ni_table);
  2132. kfree(table);
  2133. return result;
  2134. }
  2135. static bool iceland_is_dpm_running(struct pp_hwmgr *hwmgr)
  2136. {
  2137. return (1 == PHM_READ_INDIRECT_FIELD(hwmgr->device,
  2138. CGS_IND_REG__SMC, FEATURE_STATUS, VOLTAGE_CONTROLLER_ON))
  2139. ? true : false;
  2140. }
  2141. const struct pp_smumgr_func iceland_smu_funcs = {
  2142. .smu_init = &iceland_smu_init,
  2143. .smu_fini = &smu7_smu_fini,
  2144. .start_smu = &iceland_start_smu,
  2145. .check_fw_load_finish = &smu7_check_fw_load_finish,
  2146. .request_smu_load_fw = &smu7_reload_firmware,
  2147. .request_smu_load_specific_fw = &iceland_request_smu_load_specific_fw,
  2148. .send_msg_to_smc = &smu7_send_msg_to_smc,
  2149. .send_msg_to_smc_with_parameter = &smu7_send_msg_to_smc_with_parameter,
  2150. .download_pptable_settings = NULL,
  2151. .upload_pptable_settings = NULL,
  2152. .get_offsetof = iceland_get_offsetof,
  2153. .process_firmware_header = iceland_process_firmware_header,
  2154. .init_smc_table = iceland_init_smc_table,
  2155. .update_sclk_threshold = iceland_update_sclk_threshold,
  2156. .thermal_setup_fan_table = iceland_thermal_setup_fan_table,
  2157. .populate_all_graphic_levels = iceland_populate_all_graphic_levels,
  2158. .populate_all_memory_levels = iceland_populate_all_memory_levels,
  2159. .get_mac_definition = iceland_get_mac_definition,
  2160. .initialize_mc_reg_table = iceland_initialize_mc_reg_table,
  2161. .is_dpm_running = iceland_is_dpm_running,
  2162. };