vega12_processpptables.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright 2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/fb.h>
  26. #include "vega12/smu9_driver_if.h"
  27. #include "vega12_processpptables.h"
  28. #include "ppatomfwctrl.h"
  29. #include "atomfirmware.h"
  30. #include "pp_debug.h"
  31. #include "cgs_common.h"
  32. #include "vega12_pptable.h"
  33. static void set_hw_cap(struct pp_hwmgr *hwmgr, bool enable,
  34. enum phm_platform_caps cap)
  35. {
  36. if (enable)
  37. phm_cap_set(hwmgr->platform_descriptor.platformCaps, cap);
  38. else
  39. phm_cap_unset(hwmgr->platform_descriptor.platformCaps, cap);
  40. }
  41. static const void *get_powerplay_table(struct pp_hwmgr *hwmgr)
  42. {
  43. int index = GetIndexIntoMasterDataTable(powerplayinfo);
  44. u16 size;
  45. u8 frev, crev;
  46. const void *table_address = hwmgr->soft_pp_table;
  47. if (!table_address) {
  48. table_address = (ATOM_Vega12_POWERPLAYTABLE *)
  49. smu_atom_get_data_table(hwmgr->adev, index,
  50. &size, &frev, &crev);
  51. hwmgr->soft_pp_table = table_address; /*Cache the result in RAM.*/
  52. hwmgr->soft_pp_table_size = size;
  53. }
  54. return table_address;
  55. }
  56. static int check_powerplay_tables(
  57. struct pp_hwmgr *hwmgr,
  58. const ATOM_Vega12_POWERPLAYTABLE *powerplay_table)
  59. {
  60. PP_ASSERT_WITH_CODE((powerplay_table->sHeader.format_revision >=
  61. ATOM_VEGA12_TABLE_REVISION_VEGA12),
  62. "Unsupported PPTable format!", return -1);
  63. PP_ASSERT_WITH_CODE(powerplay_table->sHeader.structuresize > 0,
  64. "Invalid PowerPlay Table!", return -1);
  65. return 0;
  66. }
  67. static int set_platform_caps(struct pp_hwmgr *hwmgr, uint32_t powerplay_caps)
  68. {
  69. set_hw_cap(
  70. hwmgr,
  71. 0 != (powerplay_caps & ATOM_VEGA12_PP_PLATFORM_CAP_POWERPLAY),
  72. PHM_PlatformCaps_PowerPlaySupport);
  73. set_hw_cap(
  74. hwmgr,
  75. 0 != (powerplay_caps & ATOM_VEGA12_PP_PLATFORM_CAP_SBIOSPOWERSOURCE),
  76. PHM_PlatformCaps_BiosPowerSourceControl);
  77. set_hw_cap(
  78. hwmgr,
  79. 0 != (powerplay_caps & ATOM_VEGA12_PP_PLATFORM_CAP_BACO),
  80. PHM_PlatformCaps_BACO);
  81. set_hw_cap(
  82. hwmgr,
  83. 0 != (powerplay_caps & ATOM_VEGA12_PP_PLATFORM_CAP_BAMACO),
  84. PHM_PlatformCaps_BAMACO);
  85. return 0;
  86. }
  87. static int copy_clock_limits_array(
  88. struct pp_hwmgr *hwmgr,
  89. uint32_t **pptable_info_array,
  90. const uint32_t *pptable_array)
  91. {
  92. uint32_t array_size, i;
  93. uint32_t *table;
  94. array_size = sizeof(uint32_t) * ATOM_VEGA12_PPCLOCK_COUNT;
  95. table = kzalloc(array_size, GFP_KERNEL);
  96. if (NULL == table)
  97. return -ENOMEM;
  98. for (i = 0; i < ATOM_VEGA12_PPCLOCK_COUNT; i++)
  99. table[i] = pptable_array[i];
  100. *pptable_info_array = table;
  101. return 0;
  102. }
  103. static int copy_overdrive_settings_limits_array(
  104. struct pp_hwmgr *hwmgr,
  105. uint32_t **pptable_info_array,
  106. const uint32_t *pptable_array)
  107. {
  108. uint32_t array_size, i;
  109. uint32_t *table;
  110. array_size = sizeof(uint32_t) * ATOM_VEGA12_ODSETTING_COUNT;
  111. table = kzalloc(array_size, GFP_KERNEL);
  112. if (NULL == table)
  113. return -ENOMEM;
  114. for (i = 0; i < ATOM_VEGA12_ODSETTING_COUNT; i++)
  115. table[i] = pptable_array[i];
  116. *pptable_info_array = table;
  117. return 0;
  118. }
  119. static int append_vbios_pptable(struct pp_hwmgr *hwmgr, PPTable_t *ppsmc_pptable)
  120. {
  121. struct pp_atomfwctrl_smc_dpm_parameters smc_dpm_table;
  122. PP_ASSERT_WITH_CODE(
  123. pp_atomfwctrl_get_smc_dpm_information(hwmgr, &smc_dpm_table) == 0,
  124. "[appendVbiosPPTable] Failed to retrieve Smc Dpm Table from VBIOS!",
  125. return -1);
  126. ppsmc_pptable->Liquid1_I2C_address = smc_dpm_table.liquid1_i2c_address;
  127. ppsmc_pptable->Liquid2_I2C_address = smc_dpm_table.liquid2_i2c_address;
  128. ppsmc_pptable->Vr_I2C_address = smc_dpm_table.vr_i2c_address;
  129. ppsmc_pptable->Plx_I2C_address = smc_dpm_table.plx_i2c_address;
  130. ppsmc_pptable->Liquid_I2C_LineSCL = smc_dpm_table.liquid_i2c_linescl;
  131. ppsmc_pptable->Liquid_I2C_LineSDA = smc_dpm_table.liquid_i2c_linesda;
  132. ppsmc_pptable->Vr_I2C_LineSCL = smc_dpm_table.vr_i2c_linescl;
  133. ppsmc_pptable->Vr_I2C_LineSDA = smc_dpm_table.vr_i2c_linesda;
  134. ppsmc_pptable->Plx_I2C_LineSCL = smc_dpm_table.plx_i2c_linescl;
  135. ppsmc_pptable->Plx_I2C_LineSDA = smc_dpm_table.plx_i2c_linesda;
  136. ppsmc_pptable->VrSensorPresent = smc_dpm_table.vrsensorpresent;
  137. ppsmc_pptable->LiquidSensorPresent = smc_dpm_table.liquidsensorpresent;
  138. ppsmc_pptable->MaxVoltageStepGfx = smc_dpm_table.maxvoltagestepgfx;
  139. ppsmc_pptable->MaxVoltageStepSoc = smc_dpm_table.maxvoltagestepsoc;
  140. ppsmc_pptable->VddGfxVrMapping = smc_dpm_table.vddgfxvrmapping;
  141. ppsmc_pptable->VddSocVrMapping = smc_dpm_table.vddsocvrmapping;
  142. ppsmc_pptable->VddMem0VrMapping = smc_dpm_table.vddmem0vrmapping;
  143. ppsmc_pptable->VddMem1VrMapping = smc_dpm_table.vddmem1vrmapping;
  144. ppsmc_pptable->GfxUlvPhaseSheddingMask = smc_dpm_table.gfxulvphasesheddingmask;
  145. ppsmc_pptable->SocUlvPhaseSheddingMask = smc_dpm_table.soculvphasesheddingmask;
  146. ppsmc_pptable->GfxMaxCurrent = smc_dpm_table.gfxmaxcurrent;
  147. ppsmc_pptable->GfxOffset = smc_dpm_table.gfxoffset;
  148. ppsmc_pptable->Padding_TelemetryGfx = smc_dpm_table.padding_telemetrygfx;
  149. ppsmc_pptable->SocMaxCurrent = smc_dpm_table.socmaxcurrent;
  150. ppsmc_pptable->SocOffset = smc_dpm_table.socoffset;
  151. ppsmc_pptable->Padding_TelemetrySoc = smc_dpm_table.padding_telemetrysoc;
  152. ppsmc_pptable->Mem0MaxCurrent = smc_dpm_table.mem0maxcurrent;
  153. ppsmc_pptable->Mem0Offset = smc_dpm_table.mem0offset;
  154. ppsmc_pptable->Padding_TelemetryMem0 = smc_dpm_table.padding_telemetrymem0;
  155. ppsmc_pptable->Mem1MaxCurrent = smc_dpm_table.mem1maxcurrent;
  156. ppsmc_pptable->Mem1Offset = smc_dpm_table.mem1offset;
  157. ppsmc_pptable->Padding_TelemetryMem1 = smc_dpm_table.padding_telemetrymem1;
  158. ppsmc_pptable->AcDcGpio = smc_dpm_table.acdcgpio;
  159. ppsmc_pptable->AcDcPolarity = smc_dpm_table.acdcpolarity;
  160. ppsmc_pptable->VR0HotGpio = smc_dpm_table.vr0hotgpio;
  161. ppsmc_pptable->VR0HotPolarity = smc_dpm_table.vr0hotpolarity;
  162. ppsmc_pptable->VR1HotGpio = smc_dpm_table.vr1hotgpio;
  163. ppsmc_pptable->VR1HotPolarity = smc_dpm_table.vr1hotpolarity;
  164. ppsmc_pptable->Padding1 = smc_dpm_table.padding1;
  165. ppsmc_pptable->Padding2 = smc_dpm_table.padding2;
  166. ppsmc_pptable->LedPin0 = smc_dpm_table.ledpin0;
  167. ppsmc_pptable->LedPin1 = smc_dpm_table.ledpin1;
  168. ppsmc_pptable->LedPin2 = smc_dpm_table.ledpin2;
  169. ppsmc_pptable->PllGfxclkSpreadEnabled = smc_dpm_table.pllgfxclkspreadenabled;
  170. ppsmc_pptable->PllGfxclkSpreadPercent = smc_dpm_table.pllgfxclkspreadpercent;
  171. ppsmc_pptable->PllGfxclkSpreadFreq = smc_dpm_table.pllgfxclkspreadfreq;
  172. ppsmc_pptable->UclkSpreadEnabled = 0;
  173. ppsmc_pptable->UclkSpreadPercent = smc_dpm_table.uclkspreadpercent;
  174. ppsmc_pptable->UclkSpreadFreq = smc_dpm_table.uclkspreadfreq;
  175. ppsmc_pptable->SocclkSpreadEnabled = 0;
  176. ppsmc_pptable->SocclkSpreadPercent = smc_dpm_table.socclkspreadpercent;
  177. ppsmc_pptable->SocclkSpreadFreq = smc_dpm_table.socclkspreadfreq;
  178. ppsmc_pptable->AcgGfxclkSpreadEnabled = smc_dpm_table.acggfxclkspreadenabled;
  179. ppsmc_pptable->AcgGfxclkSpreadPercent = smc_dpm_table.acggfxclkspreadpercent;
  180. ppsmc_pptable->AcgGfxclkSpreadFreq = smc_dpm_table.acggfxclkspreadfreq;
  181. ppsmc_pptable->Vr2_I2C_address = smc_dpm_table.Vr2_I2C_address;
  182. ppsmc_pptable->Vr2_I2C_address = smc_dpm_table.Vr2_I2C_address;
  183. return 0;
  184. }
  185. #define VEGA12_ENGINECLOCK_HARDMAX 198000
  186. static int init_powerplay_table_information(
  187. struct pp_hwmgr *hwmgr,
  188. const ATOM_Vega12_POWERPLAYTABLE *powerplay_table)
  189. {
  190. struct phm_ppt_v3_information *pptable_information =
  191. (struct phm_ppt_v3_information *)hwmgr->pptable;
  192. uint32_t disable_power_control = 0;
  193. int result;
  194. hwmgr->thermal_controller.ucType = powerplay_table->ucThermalControllerType;
  195. pptable_information->uc_thermal_controller_type = powerplay_table->ucThermalControllerType;
  196. set_hw_cap(hwmgr,
  197. ATOM_VEGA12_PP_THERMALCONTROLLER_NONE != hwmgr->thermal_controller.ucType,
  198. PHM_PlatformCaps_ThermalController);
  199. phm_cap_set(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_MicrocodeFanControl);
  200. if (powerplay_table->ODSettingsMax[ATOM_VEGA12_ODSETTING_GFXCLKFMAX] > VEGA12_ENGINECLOCK_HARDMAX)
  201. hwmgr->platform_descriptor.overdriveLimit.engineClock = VEGA12_ENGINECLOCK_HARDMAX;
  202. else
  203. hwmgr->platform_descriptor.overdriveLimit.engineClock = powerplay_table->ODSettingsMax[ATOM_VEGA12_ODSETTING_GFXCLKFMAX];
  204. hwmgr->platform_descriptor.overdriveLimit.memoryClock = powerplay_table->ODSettingsMax[ATOM_VEGA12_ODSETTING_UCLKFMAX];
  205. copy_overdrive_settings_limits_array(hwmgr, &pptable_information->od_settings_max, powerplay_table->ODSettingsMax);
  206. copy_overdrive_settings_limits_array(hwmgr, &pptable_information->od_settings_min, powerplay_table->ODSettingsMin);
  207. /* hwmgr->platformDescriptor.minOverdriveVDDC = 0;
  208. hwmgr->platformDescriptor.maxOverdriveVDDC = 0;
  209. hwmgr->platformDescriptor.overdriveVDDCStep = 0; */
  210. if (hwmgr->platform_descriptor.overdriveLimit.engineClock > 0
  211. && hwmgr->platform_descriptor.overdriveLimit.memoryClock > 0)
  212. phm_cap_set(hwmgr->platform_descriptor.platformCaps, PHM_PlatformCaps_ACOverdriveSupport);
  213. pptable_information->us_small_power_limit1 = powerplay_table->usSmallPowerLimit1;
  214. pptable_information->us_small_power_limit2 = powerplay_table->usSmallPowerLimit2;
  215. pptable_information->us_boost_power_limit = powerplay_table->usBoostPowerLimit;
  216. pptable_information->us_od_turbo_power_limit = powerplay_table->usODTurboPowerLimit;
  217. pptable_information->us_od_powersave_power_limit = powerplay_table->usODPowerSavePowerLimit;
  218. pptable_information->us_software_shutdown_temp = powerplay_table->usSoftwareShutdownTemp;
  219. hwmgr->platform_descriptor.TDPODLimit = (uint16_t)powerplay_table->ODSettingsMax[ATOM_VEGA12_ODSETTING_POWERPERCENTAGE];
  220. disable_power_control = 0;
  221. if (!disable_power_control) {
  222. /* enable TDP overdrive (PowerControl) feature as well if supported */
  223. if (hwmgr->platform_descriptor.TDPODLimit)
  224. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  225. PHM_PlatformCaps_PowerControl);
  226. }
  227. copy_clock_limits_array(hwmgr, &pptable_information->power_saving_clock_max, powerplay_table->PowerSavingClockMax);
  228. copy_clock_limits_array(hwmgr, &pptable_information->power_saving_clock_min, powerplay_table->PowerSavingClockMin);
  229. pptable_information->smc_pptable = (PPTable_t *)kmalloc(sizeof(PPTable_t), GFP_KERNEL);
  230. if (pptable_information->smc_pptable == NULL)
  231. return -ENOMEM;
  232. memcpy(pptable_information->smc_pptable, &(powerplay_table->smcPPTable), sizeof(PPTable_t));
  233. result = append_vbios_pptable(hwmgr, (pptable_information->smc_pptable));
  234. return result;
  235. }
  236. int vega12_pp_tables_initialize(struct pp_hwmgr *hwmgr)
  237. {
  238. int result = 0;
  239. const ATOM_Vega12_POWERPLAYTABLE *powerplay_table;
  240. hwmgr->pptable = kzalloc(sizeof(struct phm_ppt_v3_information), GFP_KERNEL);
  241. PP_ASSERT_WITH_CODE((hwmgr->pptable != NULL),
  242. "Failed to allocate hwmgr->pptable!", return -ENOMEM);
  243. powerplay_table = get_powerplay_table(hwmgr);
  244. PP_ASSERT_WITH_CODE((powerplay_table != NULL),
  245. "Missing PowerPlay Table!", return -1);
  246. result = check_powerplay_tables(hwmgr, powerplay_table);
  247. PP_ASSERT_WITH_CODE((result == 0),
  248. "check_powerplay_tables failed", return result);
  249. result = set_platform_caps(hwmgr,
  250. le32_to_cpu(powerplay_table->ulPlatformCaps));
  251. PP_ASSERT_WITH_CODE((result == 0),
  252. "set_platform_caps failed", return result);
  253. result = init_powerplay_table_information(hwmgr, powerplay_table);
  254. PP_ASSERT_WITH_CODE((result == 0),
  255. "init_powerplay_table_information failed", return result);
  256. return result;
  257. }
  258. static int vega12_pp_tables_uninitialize(struct pp_hwmgr *hwmgr)
  259. {
  260. struct phm_ppt_v3_information *pp_table_info =
  261. (struct phm_ppt_v3_information *)(hwmgr->pptable);
  262. kfree(pp_table_info->power_saving_clock_max);
  263. pp_table_info->power_saving_clock_max = NULL;
  264. kfree(pp_table_info->power_saving_clock_min);
  265. pp_table_info->power_saving_clock_min = NULL;
  266. kfree(pp_table_info->od_settings_max);
  267. pp_table_info->od_settings_max = NULL;
  268. kfree(pp_table_info->od_settings_min);
  269. pp_table_info->od_settings_min = NULL;
  270. kfree(pp_table_info->smc_pptable);
  271. pp_table_info->smc_pptable = NULL;
  272. kfree(hwmgr->pptable);
  273. hwmgr->pptable = NULL;
  274. return 0;
  275. }
  276. const struct pp_table_func vega12_pptable_funcs = {
  277. .pptable_init = vega12_pp_tables_initialize,
  278. .pptable_fini = vega12_pp_tables_uninitialize,
  279. };
  280. #if 0
  281. static uint32_t make_classification_flags(struct pp_hwmgr *hwmgr,
  282. uint16_t classification, uint16_t classification2)
  283. {
  284. uint32_t result = 0;
  285. if (classification & ATOM_PPLIB_CLASSIFICATION_BOOT)
  286. result |= PP_StateClassificationFlag_Boot;
  287. if (classification & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  288. result |= PP_StateClassificationFlag_Thermal;
  289. if (classification & ATOM_PPLIB_CLASSIFICATION_LIMITEDPOWERSOURCE)
  290. result |= PP_StateClassificationFlag_LimitedPowerSource;
  291. if (classification & ATOM_PPLIB_CLASSIFICATION_REST)
  292. result |= PP_StateClassificationFlag_Rest;
  293. if (classification & ATOM_PPLIB_CLASSIFICATION_FORCED)
  294. result |= PP_StateClassificationFlag_Forced;
  295. if (classification & ATOM_PPLIB_CLASSIFICATION_ACPI)
  296. result |= PP_StateClassificationFlag_ACPI;
  297. if (classification2 & ATOM_PPLIB_CLASSIFICATION2_LIMITEDPOWERSOURCE_2)
  298. result |= PP_StateClassificationFlag_LimitedPowerSource_2;
  299. return result;
  300. }
  301. int vega12_get_powerplay_table_entry(struct pp_hwmgr *hwmgr,
  302. uint32_t entry_index, struct pp_power_state *power_state,
  303. int (*call_back_func)(struct pp_hwmgr *, void *,
  304. struct pp_power_state *, void *, uint32_t))
  305. {
  306. int result = 0;
  307. const ATOM_Vega12_State_Array *state_arrays;
  308. const ATOM_Vega12_State *state_entry;
  309. const ATOM_Vega12_POWERPLAYTABLE *pp_table =
  310. get_powerplay_table(hwmgr);
  311. PP_ASSERT_WITH_CODE(pp_table, "Missing PowerPlay Table!",
  312. return -1;);
  313. power_state->classification.bios_index = entry_index;
  314. if (pp_table->sHeader.format_revision >=
  315. ATOM_Vega12_TABLE_REVISION_VEGA12) {
  316. state_arrays = (ATOM_Vega12_State_Array *)
  317. (((unsigned long)pp_table) +
  318. le16_to_cpu(pp_table->usStateArrayOffset));
  319. PP_ASSERT_WITH_CODE(pp_table->usStateArrayOffset > 0,
  320. "Invalid PowerPlay Table State Array Offset.",
  321. return -1);
  322. PP_ASSERT_WITH_CODE(state_arrays->ucNumEntries > 0,
  323. "Invalid PowerPlay Table State Array.",
  324. return -1);
  325. PP_ASSERT_WITH_CODE((entry_index <= state_arrays->ucNumEntries),
  326. "Invalid PowerPlay Table State Array Entry.",
  327. return -1);
  328. state_entry = &(state_arrays->states[entry_index]);
  329. result = call_back_func(hwmgr, (void *)state_entry, power_state,
  330. (void *)pp_table,
  331. make_classification_flags(hwmgr,
  332. le16_to_cpu(state_entry->usClassification),
  333. le16_to_cpu(state_entry->usClassification2)));
  334. }
  335. if (!result && (power_state->classification.flags &
  336. PP_StateClassificationFlag_Boot))
  337. result = hwmgr->hwmgr_func->patch_boot_state(hwmgr, &(power_state->hardware));
  338. return result;
  339. }
  340. #endif