processpptables.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  1. /*
  2. * Copyright 2015 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 "pp_debug.h"
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <drm/amdgpu_drm.h>
  28. #include "processpptables.h"
  29. #include <atom-types.h>
  30. #include <atombios.h>
  31. #include "pptable.h"
  32. #include "power_state.h"
  33. #include "hwmgr.h"
  34. #include "hardwaremanager.h"
  35. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V2 12
  36. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V3 14
  37. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V4 16
  38. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V5 18
  39. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V6 20
  40. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V7 22
  41. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V8 24
  42. #define SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V9 26
  43. #define NUM_BITS_CLOCK_INFO_ARRAY_INDEX 6
  44. static uint16_t get_vce_table_offset(struct pp_hwmgr *hwmgr,
  45. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  46. {
  47. uint16_t vce_table_offset = 0;
  48. if (le16_to_cpu(powerplay_table->usTableSize) >=
  49. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  50. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  51. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  52. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  53. const ATOM_PPLIB_EXTENDEDHEADER *extended_header =
  54. (const ATOM_PPLIB_EXTENDEDHEADER *)
  55. (((unsigned long)powerplay_table3) +
  56. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  57. if (le16_to_cpu(extended_header->usSize) >=
  58. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V2)
  59. vce_table_offset = le16_to_cpu(extended_header->usVCETableOffset);
  60. }
  61. }
  62. return vce_table_offset;
  63. }
  64. static uint16_t get_vce_clock_info_array_offset(struct pp_hwmgr *hwmgr,
  65. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  66. {
  67. uint16_t table_offset = get_vce_table_offset(hwmgr,
  68. powerplay_table);
  69. if (table_offset > 0)
  70. return table_offset + 1;
  71. return 0;
  72. }
  73. static uint16_t get_vce_clock_info_array_size(struct pp_hwmgr *hwmgr,
  74. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  75. {
  76. uint16_t table_offset = get_vce_clock_info_array_offset(hwmgr,
  77. powerplay_table);
  78. uint16_t table_size = 0;
  79. if (table_offset > 0) {
  80. const VCEClockInfoArray *p = (const VCEClockInfoArray *)
  81. (((unsigned long) powerplay_table) + table_offset);
  82. table_size = sizeof(uint8_t) + p->ucNumEntries * sizeof(VCEClockInfo);
  83. }
  84. return table_size;
  85. }
  86. static uint16_t get_vce_clock_voltage_limit_table_offset(struct pp_hwmgr *hwmgr,
  87. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  88. {
  89. uint16_t table_offset = get_vce_clock_info_array_offset(hwmgr,
  90. powerplay_table);
  91. if (table_offset > 0)
  92. return table_offset + get_vce_clock_info_array_size(hwmgr,
  93. powerplay_table);
  94. return 0;
  95. }
  96. static uint16_t get_vce_clock_voltage_limit_table_size(struct pp_hwmgr *hwmgr,
  97. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  98. {
  99. uint16_t table_offset = get_vce_clock_voltage_limit_table_offset(hwmgr, powerplay_table);
  100. uint16_t table_size = 0;
  101. if (table_offset > 0) {
  102. const ATOM_PPLIB_VCE_Clock_Voltage_Limit_Table *ptable =
  103. (const ATOM_PPLIB_VCE_Clock_Voltage_Limit_Table *)(((unsigned long) powerplay_table) + table_offset);
  104. table_size = sizeof(uint8_t) + ptable->numEntries * sizeof(ATOM_PPLIB_VCE_Clock_Voltage_Limit_Record);
  105. }
  106. return table_size;
  107. }
  108. static uint16_t get_vce_state_table_offset(struct pp_hwmgr *hwmgr, const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  109. {
  110. uint16_t table_offset = get_vce_clock_voltage_limit_table_offset(hwmgr, powerplay_table);
  111. if (table_offset > 0)
  112. return table_offset + get_vce_clock_voltage_limit_table_size(hwmgr, powerplay_table);
  113. return 0;
  114. }
  115. static const ATOM_PPLIB_VCE_State_Table *get_vce_state_table(
  116. struct pp_hwmgr *hwmgr,
  117. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  118. {
  119. uint16_t table_offset = get_vce_state_table_offset(hwmgr, powerplay_table);
  120. if (table_offset > 0)
  121. return (const ATOM_PPLIB_VCE_State_Table *)(((unsigned long) powerplay_table) + table_offset);
  122. return NULL;
  123. }
  124. static uint16_t get_uvd_table_offset(struct pp_hwmgr *hwmgr,
  125. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  126. {
  127. uint16_t uvd_table_offset = 0;
  128. if (le16_to_cpu(powerplay_table->usTableSize) >=
  129. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  130. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  131. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  132. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  133. const ATOM_PPLIB_EXTENDEDHEADER *extended_header =
  134. (const ATOM_PPLIB_EXTENDEDHEADER *)
  135. (((unsigned long)powerplay_table3) +
  136. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  137. if (le16_to_cpu(extended_header->usSize) >=
  138. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V3)
  139. uvd_table_offset = le16_to_cpu(extended_header->usUVDTableOffset);
  140. }
  141. }
  142. return uvd_table_offset;
  143. }
  144. static uint16_t get_uvd_clock_info_array_offset(struct pp_hwmgr *hwmgr,
  145. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  146. {
  147. uint16_t table_offset = get_uvd_table_offset(hwmgr,
  148. powerplay_table);
  149. if (table_offset > 0)
  150. return table_offset + 1;
  151. return 0;
  152. }
  153. static uint16_t get_uvd_clock_info_array_size(struct pp_hwmgr *hwmgr,
  154. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  155. {
  156. uint16_t table_offset = get_uvd_clock_info_array_offset(hwmgr,
  157. powerplay_table);
  158. uint16_t table_size = 0;
  159. if (table_offset > 0) {
  160. const UVDClockInfoArray *p = (const UVDClockInfoArray *)
  161. (((unsigned long) powerplay_table)
  162. + table_offset);
  163. table_size = sizeof(UCHAR) +
  164. p->ucNumEntries * sizeof(UVDClockInfo);
  165. }
  166. return table_size;
  167. }
  168. static uint16_t get_uvd_clock_voltage_limit_table_offset(
  169. struct pp_hwmgr *hwmgr,
  170. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  171. {
  172. uint16_t table_offset = get_uvd_clock_info_array_offset(hwmgr,
  173. powerplay_table);
  174. if (table_offset > 0)
  175. return table_offset +
  176. get_uvd_clock_info_array_size(hwmgr, powerplay_table);
  177. return 0;
  178. }
  179. static uint16_t get_samu_table_offset(struct pp_hwmgr *hwmgr,
  180. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  181. {
  182. uint16_t samu_table_offset = 0;
  183. if (le16_to_cpu(powerplay_table->usTableSize) >=
  184. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  185. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  186. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  187. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  188. const ATOM_PPLIB_EXTENDEDHEADER *extended_header =
  189. (const ATOM_PPLIB_EXTENDEDHEADER *)
  190. (((unsigned long)powerplay_table3) +
  191. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  192. if (le16_to_cpu(extended_header->usSize) >=
  193. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V4)
  194. samu_table_offset = le16_to_cpu(extended_header->usSAMUTableOffset);
  195. }
  196. }
  197. return samu_table_offset;
  198. }
  199. static uint16_t get_samu_clock_voltage_limit_table_offset(
  200. struct pp_hwmgr *hwmgr,
  201. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  202. {
  203. uint16_t table_offset = get_samu_table_offset(hwmgr,
  204. powerplay_table);
  205. if (table_offset > 0)
  206. return table_offset + 1;
  207. return 0;
  208. }
  209. static uint16_t get_acp_table_offset(struct pp_hwmgr *hwmgr,
  210. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  211. {
  212. uint16_t acp_table_offset = 0;
  213. if (le16_to_cpu(powerplay_table->usTableSize) >=
  214. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  215. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  216. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  217. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  218. const ATOM_PPLIB_EXTENDEDHEADER *pExtendedHeader =
  219. (const ATOM_PPLIB_EXTENDEDHEADER *)
  220. (((unsigned long)powerplay_table3) +
  221. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  222. if (le16_to_cpu(pExtendedHeader->usSize) >=
  223. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V6)
  224. acp_table_offset = le16_to_cpu(pExtendedHeader->usACPTableOffset);
  225. }
  226. }
  227. return acp_table_offset;
  228. }
  229. static uint16_t get_acp_clock_voltage_limit_table_offset(
  230. struct pp_hwmgr *hwmgr,
  231. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  232. {
  233. uint16_t tableOffset = get_acp_table_offset(hwmgr, powerplay_table);
  234. if (tableOffset > 0)
  235. return tableOffset + 1;
  236. return 0;
  237. }
  238. static uint16_t get_cacp_tdp_table_offset(
  239. struct pp_hwmgr *hwmgr,
  240. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  241. {
  242. uint16_t cacTdpTableOffset = 0;
  243. if (le16_to_cpu(powerplay_table->usTableSize) >=
  244. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  245. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  246. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  247. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  248. const ATOM_PPLIB_EXTENDEDHEADER *pExtendedHeader =
  249. (const ATOM_PPLIB_EXTENDEDHEADER *)
  250. (((unsigned long)powerplay_table3) +
  251. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  252. if (le16_to_cpu(pExtendedHeader->usSize) >=
  253. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V7)
  254. cacTdpTableOffset = le16_to_cpu(pExtendedHeader->usPowerTuneTableOffset);
  255. }
  256. }
  257. return cacTdpTableOffset;
  258. }
  259. static int get_cac_tdp_table(struct pp_hwmgr *hwmgr,
  260. struct phm_cac_tdp_table **ptable,
  261. const ATOM_PowerTune_Table *table,
  262. uint16_t us_maximum_power_delivery_limit)
  263. {
  264. unsigned long table_size;
  265. struct phm_cac_tdp_table *tdp_table;
  266. table_size = sizeof(unsigned long) + sizeof(struct phm_cac_tdp_table);
  267. tdp_table = kzalloc(table_size, GFP_KERNEL);
  268. if (NULL == tdp_table)
  269. return -ENOMEM;
  270. tdp_table->usTDP = le16_to_cpu(table->usTDP);
  271. tdp_table->usConfigurableTDP = le16_to_cpu(table->usConfigurableTDP);
  272. tdp_table->usTDC = le16_to_cpu(table->usTDC);
  273. tdp_table->usBatteryPowerLimit = le16_to_cpu(table->usBatteryPowerLimit);
  274. tdp_table->usSmallPowerLimit = le16_to_cpu(table->usSmallPowerLimit);
  275. tdp_table->usLowCACLeakage = le16_to_cpu(table->usLowCACLeakage);
  276. tdp_table->usHighCACLeakage = le16_to_cpu(table->usHighCACLeakage);
  277. tdp_table->usMaximumPowerDeliveryLimit = us_maximum_power_delivery_limit;
  278. *ptable = tdp_table;
  279. return 0;
  280. }
  281. static uint16_t get_sclk_vdd_gfx_table_offset(struct pp_hwmgr *hwmgr,
  282. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  283. {
  284. uint16_t sclk_vdd_gfx_table_offset = 0;
  285. if (le16_to_cpu(powerplay_table->usTableSize) >=
  286. sizeof(ATOM_PPLIB_POWERPLAYTABLE3)) {
  287. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3 =
  288. (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  289. if (powerplay_table3->usExtendendedHeaderOffset > 0) {
  290. const ATOM_PPLIB_EXTENDEDHEADER *pExtendedHeader =
  291. (const ATOM_PPLIB_EXTENDEDHEADER *)
  292. (((unsigned long)powerplay_table3) +
  293. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  294. if (le16_to_cpu(pExtendedHeader->usSize) >=
  295. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V8)
  296. sclk_vdd_gfx_table_offset =
  297. le16_to_cpu(pExtendedHeader->usSclkVddgfxTableOffset);
  298. }
  299. }
  300. return sclk_vdd_gfx_table_offset;
  301. }
  302. static uint16_t get_sclk_vdd_gfx_clock_voltage_dependency_table_offset(
  303. struct pp_hwmgr *hwmgr,
  304. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  305. {
  306. uint16_t tableOffset = get_sclk_vdd_gfx_table_offset(hwmgr, powerplay_table);
  307. if (tableOffset > 0)
  308. return tableOffset;
  309. return 0;
  310. }
  311. static int get_clock_voltage_dependency_table(struct pp_hwmgr *hwmgr,
  312. struct phm_clock_voltage_dependency_table **ptable,
  313. const ATOM_PPLIB_Clock_Voltage_Dependency_Table *table)
  314. {
  315. unsigned long table_size, i;
  316. struct phm_clock_voltage_dependency_table *dep_table;
  317. table_size = sizeof(unsigned long) +
  318. sizeof(struct phm_clock_voltage_dependency_table)
  319. * table->ucNumEntries;
  320. dep_table = kzalloc(table_size, GFP_KERNEL);
  321. if (NULL == dep_table)
  322. return -ENOMEM;
  323. dep_table->count = (unsigned long)table->ucNumEntries;
  324. for (i = 0; i < dep_table->count; i++) {
  325. dep_table->entries[i].clk =
  326. ((unsigned long)table->entries[i].ucClockHigh << 16) |
  327. le16_to_cpu(table->entries[i].usClockLow);
  328. dep_table->entries[i].v =
  329. (unsigned long)le16_to_cpu(table->entries[i].usVoltage);
  330. }
  331. *ptable = dep_table;
  332. return 0;
  333. }
  334. static int get_valid_clk(struct pp_hwmgr *hwmgr,
  335. struct phm_clock_array **ptable,
  336. const struct phm_clock_voltage_dependency_table *table)
  337. {
  338. unsigned long table_size, i;
  339. struct phm_clock_array *clock_table;
  340. table_size = sizeof(unsigned long) + sizeof(unsigned long) * table->count;
  341. clock_table = kzalloc(table_size, GFP_KERNEL);
  342. if (NULL == clock_table)
  343. return -ENOMEM;
  344. clock_table->count = (unsigned long)table->count;
  345. for (i = 0; i < clock_table->count; i++)
  346. clock_table->values[i] = (unsigned long)table->entries[i].clk;
  347. *ptable = clock_table;
  348. return 0;
  349. }
  350. static int get_clock_voltage_limit(struct pp_hwmgr *hwmgr,
  351. struct phm_clock_and_voltage_limits *limits,
  352. const ATOM_PPLIB_Clock_Voltage_Limit_Table *table)
  353. {
  354. limits->sclk = ((unsigned long)table->entries[0].ucSclkHigh << 16) |
  355. le16_to_cpu(table->entries[0].usSclkLow);
  356. limits->mclk = ((unsigned long)table->entries[0].ucMclkHigh << 16) |
  357. le16_to_cpu(table->entries[0].usMclkLow);
  358. limits->vddc = (unsigned long)le16_to_cpu(table->entries[0].usVddc);
  359. limits->vddci = (unsigned long)le16_to_cpu(table->entries[0].usVddci);
  360. return 0;
  361. }
  362. static void set_hw_cap(struct pp_hwmgr *hwmgr, bool enable,
  363. enum phm_platform_caps cap)
  364. {
  365. if (enable)
  366. phm_cap_set(hwmgr->platform_descriptor.platformCaps, cap);
  367. else
  368. phm_cap_unset(hwmgr->platform_descriptor.platformCaps, cap);
  369. }
  370. static int set_platform_caps(struct pp_hwmgr *hwmgr,
  371. unsigned long powerplay_caps)
  372. {
  373. set_hw_cap(
  374. hwmgr,
  375. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_POWERPLAY),
  376. PHM_PlatformCaps_PowerPlaySupport
  377. );
  378. set_hw_cap(
  379. hwmgr,
  380. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_SBIOSPOWERSOURCE),
  381. PHM_PlatformCaps_BiosPowerSourceControl
  382. );
  383. set_hw_cap(
  384. hwmgr,
  385. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_ASPM_L0s),
  386. PHM_PlatformCaps_EnableASPML0s
  387. );
  388. set_hw_cap(
  389. hwmgr,
  390. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_ASPM_L1),
  391. PHM_PlatformCaps_EnableASPML1
  392. );
  393. set_hw_cap(
  394. hwmgr,
  395. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_BACKBIAS),
  396. PHM_PlatformCaps_EnableBackbias
  397. );
  398. set_hw_cap(
  399. hwmgr,
  400. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_HARDWAREDC),
  401. PHM_PlatformCaps_AutomaticDCTransition
  402. );
  403. set_hw_cap(
  404. hwmgr,
  405. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_GEMINIPRIMARY),
  406. PHM_PlatformCaps_GeminiPrimary
  407. );
  408. set_hw_cap(
  409. hwmgr,
  410. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_STEPVDDC),
  411. PHM_PlatformCaps_StepVddc
  412. );
  413. set_hw_cap(
  414. hwmgr,
  415. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_VOLTAGECONTROL),
  416. PHM_PlatformCaps_EnableVoltageControl
  417. );
  418. set_hw_cap(
  419. hwmgr,
  420. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_SIDEPORTCONTROL),
  421. PHM_PlatformCaps_EnableSideportControl
  422. );
  423. set_hw_cap(
  424. hwmgr,
  425. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_TURNOFFPLL_ASPML1),
  426. PHM_PlatformCaps_TurnOffPll_ASPML1
  427. );
  428. set_hw_cap(
  429. hwmgr,
  430. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_HTLINKCONTROL),
  431. PHM_PlatformCaps_EnableHTLinkControl
  432. );
  433. set_hw_cap(
  434. hwmgr,
  435. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_MVDDCONTROL),
  436. PHM_PlatformCaps_EnableMVDDControl
  437. );
  438. set_hw_cap(
  439. hwmgr,
  440. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_VDDCI_CONTROL),
  441. PHM_PlatformCaps_ControlVDDCI
  442. );
  443. set_hw_cap(
  444. hwmgr,
  445. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_REGULATOR_HOT),
  446. PHM_PlatformCaps_RegulatorHot
  447. );
  448. set_hw_cap(
  449. hwmgr,
  450. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_GOTO_BOOT_ON_ALERT),
  451. PHM_PlatformCaps_BootStateOnAlert
  452. );
  453. set_hw_cap(
  454. hwmgr,
  455. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_DONT_WAIT_FOR_VBLANK_ON_ALERT),
  456. PHM_PlatformCaps_DontWaitForVBlankOnAlert
  457. );
  458. set_hw_cap(
  459. hwmgr,
  460. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_BACO),
  461. PHM_PlatformCaps_BACO
  462. );
  463. set_hw_cap(
  464. hwmgr,
  465. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_NEW_CAC_VOLTAGE),
  466. PHM_PlatformCaps_NewCACVoltage
  467. );
  468. set_hw_cap(
  469. hwmgr,
  470. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_REVERT_GPIO5_POLARITY),
  471. PHM_PlatformCaps_RevertGPIO5Polarity
  472. );
  473. set_hw_cap(
  474. hwmgr,
  475. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_OUTPUT_THERMAL2GPIO17),
  476. PHM_PlatformCaps_Thermal2GPIO17
  477. );
  478. set_hw_cap(
  479. hwmgr,
  480. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_VRHOT_GPIO_CONFIGURABLE),
  481. PHM_PlatformCaps_VRHotGPIOConfigurable
  482. );
  483. set_hw_cap(
  484. hwmgr,
  485. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_TEMP_INVERSION),
  486. PHM_PlatformCaps_TempInversion
  487. );
  488. set_hw_cap(
  489. hwmgr,
  490. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_EVV),
  491. PHM_PlatformCaps_EVV
  492. );
  493. set_hw_cap(
  494. hwmgr,
  495. 0 != (powerplay_caps & ATOM_PP_PLATFORM_COMBINE_PCC_WITH_THERMAL_SIGNAL),
  496. PHM_PlatformCaps_CombinePCCWithThermalSignal
  497. );
  498. set_hw_cap(
  499. hwmgr,
  500. 0 != (powerplay_caps & ATOM_PP_PLATFORM_LOAD_POST_PRODUCTION_FIRMWARE),
  501. PHM_PlatformCaps_LoadPostProductionFirmware
  502. );
  503. set_hw_cap(
  504. hwmgr,
  505. 0 != (powerplay_caps & ATOM_PP_PLATFORM_CAP_DISABLE_USING_ACTUAL_TEMPERATURE_FOR_POWER_CALC),
  506. PHM_PlatformCaps_DisableUsingActualTemperatureForPowerCalc
  507. );
  508. return 0;
  509. }
  510. static PP_StateClassificationFlags make_classification_flags(
  511. struct pp_hwmgr *hwmgr,
  512. USHORT classification,
  513. USHORT classification2)
  514. {
  515. PP_StateClassificationFlags result = 0;
  516. if (classification & ATOM_PPLIB_CLASSIFICATION_BOOT)
  517. result |= PP_StateClassificationFlag_Boot;
  518. if (classification & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  519. result |= PP_StateClassificationFlag_Thermal;
  520. if (classification &
  521. ATOM_PPLIB_CLASSIFICATION_LIMITEDPOWERSOURCE)
  522. result |= PP_StateClassificationFlag_LimitedPowerSource;
  523. if (classification & ATOM_PPLIB_CLASSIFICATION_REST)
  524. result |= PP_StateClassificationFlag_Rest;
  525. if (classification & ATOM_PPLIB_CLASSIFICATION_FORCED)
  526. result |= PP_StateClassificationFlag_Forced;
  527. if (classification & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  528. result |= PP_StateClassificationFlag_3DPerformance;
  529. if (classification & ATOM_PPLIB_CLASSIFICATION_OVERDRIVETEMPLATE)
  530. result |= PP_StateClassificationFlag_ACOverdriveTemplate;
  531. if (classification & ATOM_PPLIB_CLASSIFICATION_UVDSTATE)
  532. result |= PP_StateClassificationFlag_Uvd;
  533. if (classification & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  534. result |= PP_StateClassificationFlag_UvdHD;
  535. if (classification & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  536. result |= PP_StateClassificationFlag_UvdSD;
  537. if (classification & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  538. result |= PP_StateClassificationFlag_HD2;
  539. if (classification & ATOM_PPLIB_CLASSIFICATION_ACPI)
  540. result |= PP_StateClassificationFlag_ACPI;
  541. if (classification2 & ATOM_PPLIB_CLASSIFICATION2_LIMITEDPOWERSOURCE_2)
  542. result |= PP_StateClassificationFlag_LimitedPowerSource_2;
  543. if (classification2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  544. result |= PP_StateClassificationFlag_ULV;
  545. if (classification2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  546. result |= PP_StateClassificationFlag_UvdMVC;
  547. return result;
  548. }
  549. static int init_non_clock_fields(struct pp_hwmgr *hwmgr,
  550. struct pp_power_state *ps,
  551. uint8_t version,
  552. const ATOM_PPLIB_NONCLOCK_INFO *pnon_clock_info) {
  553. unsigned long rrr_index;
  554. unsigned long tmp;
  555. ps->classification.ui_label = (le16_to_cpu(pnon_clock_info->usClassification) &
  556. ATOM_PPLIB_CLASSIFICATION_UI_MASK) >> ATOM_PPLIB_CLASSIFICATION_UI_SHIFT;
  557. ps->classification.flags = make_classification_flags(hwmgr,
  558. le16_to_cpu(pnon_clock_info->usClassification),
  559. le16_to_cpu(pnon_clock_info->usClassification2));
  560. ps->classification.temporary_state = false;
  561. ps->classification.to_be_deleted = false;
  562. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  563. ATOM_PPLIB_SINGLE_DISPLAY_ONLY;
  564. ps->validation.singleDisplayOnly = (0 != tmp);
  565. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  566. ATOM_PPLIB_DISALLOW_ON_DC;
  567. ps->validation.disallowOnDC = (0 != tmp);
  568. ps->pcie.lanes = ((le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  569. ATOM_PPLIB_PCIE_LINK_WIDTH_MASK) >>
  570. ATOM_PPLIB_PCIE_LINK_WIDTH_SHIFT) + 1;
  571. ps->pcie.lanes = 0;
  572. ps->display.disableFrameModulation = false;
  573. rrr_index = (le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  574. ATOM_PPLIB_LIMITED_REFRESHRATE_VALUE_MASK) >>
  575. ATOM_PPLIB_LIMITED_REFRESHRATE_VALUE_SHIFT;
  576. if (rrr_index != ATOM_PPLIB_LIMITED_REFRESHRATE_UNLIMITED) {
  577. static const uint8_t look_up[(ATOM_PPLIB_LIMITED_REFRESHRATE_VALUE_MASK >> ATOM_PPLIB_LIMITED_REFRESHRATE_VALUE_SHIFT) + 1] = \
  578. { 0, 50, 0 };
  579. ps->display.refreshrateSource = PP_RefreshrateSource_Explicit;
  580. ps->display.explicitRefreshrate = look_up[rrr_index];
  581. ps->display.limitRefreshrate = true;
  582. if (ps->display.explicitRefreshrate == 0)
  583. ps->display.limitRefreshrate = false;
  584. } else
  585. ps->display.limitRefreshrate = false;
  586. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  587. ATOM_PPLIB_ENABLE_VARIBRIGHT;
  588. ps->display.enableVariBright = (0 != tmp);
  589. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  590. ATOM_PPLIB_SWSTATE_MEMORY_DLL_OFF;
  591. ps->memory.dllOff = (0 != tmp);
  592. ps->memory.m3arb = (le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  593. ATOM_PPLIB_M3ARB_MASK) >> ATOM_PPLIB_M3ARB_SHIFT;
  594. ps->temperatures.min = PP_TEMPERATURE_UNITS_PER_CENTIGRADES *
  595. pnon_clock_info->ucMinTemperature;
  596. ps->temperatures.max = PP_TEMPERATURE_UNITS_PER_CENTIGRADES *
  597. pnon_clock_info->ucMaxTemperature;
  598. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  599. ATOM_PPLIB_SOFTWARE_DISABLE_LOADBALANCING;
  600. ps->software.disableLoadBalancing = tmp;
  601. tmp = le32_to_cpu(pnon_clock_info->ulCapsAndSettings) &
  602. ATOM_PPLIB_SOFTWARE_ENABLE_SLEEP_FOR_TIMESTAMPS;
  603. ps->software.enableSleepForTimestamps = (0 != tmp);
  604. ps->validation.supportedPowerLevels = pnon_clock_info->ucRequiredPower;
  605. if (ATOM_PPLIB_NONCLOCKINFO_VER1 < version) {
  606. ps->uvd_clocks.VCLK = pnon_clock_info->ulVCLK;
  607. ps->uvd_clocks.DCLK = pnon_clock_info->ulDCLK;
  608. } else {
  609. ps->uvd_clocks.VCLK = 0;
  610. ps->uvd_clocks.DCLK = 0;
  611. }
  612. return 0;
  613. }
  614. static ULONG size_of_entry_v2(ULONG num_dpm_levels)
  615. {
  616. return (sizeof(UCHAR) + sizeof(UCHAR) +
  617. (num_dpm_levels * sizeof(UCHAR)));
  618. }
  619. static const ATOM_PPLIB_STATE_V2 *get_state_entry_v2(
  620. const StateArray * pstate_arrays,
  621. ULONG entry_index)
  622. {
  623. ULONG i;
  624. const ATOM_PPLIB_STATE_V2 *pstate;
  625. pstate = pstate_arrays->states;
  626. if (entry_index <= pstate_arrays->ucNumEntries) {
  627. for (i = 0; i < entry_index; i++)
  628. pstate = (ATOM_PPLIB_STATE_V2 *)(
  629. (unsigned long)pstate +
  630. size_of_entry_v2(pstate->ucNumDPMLevels));
  631. }
  632. return pstate;
  633. }
  634. static const unsigned char soft_dummy_pp_table[] = {
  635. 0xe1, 0x01, 0x06, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x4a, 0x00, 0x6c, 0x00, 0x00,
  636. 0x00, 0x00, 0x00, 0x42, 0x00, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
  637. 0x00, 0x4e, 0x00, 0x88, 0x00, 0x00, 0x9e, 0x00, 0x17, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x00, 0x00,
  638. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  639. 0x00, 0x00, 0x02, 0x02, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
  640. 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  641. 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x18, 0x05, 0x00,
  642. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  643. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  644. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1a, 0x00,
  645. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0x00, 0x43, 0x01, 0x00, 0x00, 0x00, 0x00,
  646. 0x8e, 0x01, 0x00, 0x00, 0xb8, 0x01, 0x00, 0x00, 0x08, 0x30, 0x75, 0x00, 0x80, 0x00, 0xa0, 0x8c,
  647. 0x00, 0x7e, 0x00, 0x71, 0xa5, 0x00, 0x7c, 0x00, 0xe5, 0xc8, 0x00, 0x70, 0x00, 0x91, 0xf4, 0x00,
  648. 0x64, 0x00, 0x40, 0x19, 0x01, 0x5a, 0x00, 0x0e, 0x28, 0x01, 0x52, 0x00, 0x80, 0x38, 0x01, 0x4a,
  649. 0x00, 0x00, 0x09, 0x30, 0x75, 0x00, 0x30, 0x75, 0x00, 0x40, 0x9c, 0x00, 0x40, 0x9c, 0x00, 0x59,
  650. 0xd8, 0x00, 0x59, 0xd8, 0x00, 0x91, 0xf4, 0x00, 0x91, 0xf4, 0x00, 0x0e, 0x28, 0x01, 0x0e, 0x28,
  651. 0x01, 0x90, 0x5f, 0x01, 0x90, 0x5f, 0x01, 0x00, 0x77, 0x01, 0x00, 0x77, 0x01, 0xca, 0x91, 0x01,
  652. 0xca, 0x91, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x80, 0x00, 0x00, 0x7e, 0x00, 0x01,
  653. 0x7c, 0x00, 0x02, 0x70, 0x00, 0x03, 0x64, 0x00, 0x04, 0x5a, 0x00, 0x05, 0x52, 0x00, 0x06, 0x4a,
  654. 0x00, 0x07, 0x08, 0x08, 0x00, 0x08, 0x00, 0x01, 0x02, 0x02, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03,
  655. 0x02, 0x04, 0x02, 0x00, 0x08, 0x40, 0x9c, 0x00, 0x30, 0x75, 0x00, 0x74, 0xb5, 0x00, 0xa0, 0x8c,
  656. 0x00, 0x60, 0xea, 0x00, 0x74, 0xb5, 0x00, 0x0e, 0x28, 0x01, 0x60, 0xea, 0x00, 0x90, 0x5f, 0x01,
  657. 0x40, 0x19, 0x01, 0xb2, 0xb0, 0x01, 0x90, 0x5f, 0x01, 0xc0, 0xd4, 0x01, 0x00, 0x77, 0x01, 0x5e,
  658. 0xff, 0x01, 0xca, 0x91, 0x01, 0x08, 0x80, 0x00, 0x00, 0x7e, 0x00, 0x01, 0x7c, 0x00, 0x02, 0x70,
  659. 0x00, 0x03, 0x64, 0x00, 0x04, 0x5a, 0x00, 0x05, 0x52, 0x00, 0x06, 0x4a, 0x00, 0x07, 0x00, 0x08,
  660. 0x80, 0x00, 0x30, 0x75, 0x00, 0x7e, 0x00, 0x40, 0x9c, 0x00, 0x7c, 0x00, 0x59, 0xd8, 0x00, 0x70,
  661. 0x00, 0xdc, 0x0b, 0x01, 0x64, 0x00, 0x80, 0x38, 0x01, 0x5a, 0x00, 0x80, 0x38, 0x01, 0x52, 0x00,
  662. 0x80, 0x38, 0x01, 0x4a, 0x00, 0x80, 0x38, 0x01, 0x08, 0x30, 0x75, 0x00, 0x80, 0x00, 0xa0, 0x8c,
  663. 0x00, 0x7e, 0x00, 0x71, 0xa5, 0x00, 0x7c, 0x00, 0xe5, 0xc8, 0x00, 0x74, 0x00, 0x91, 0xf4, 0x00,
  664. 0x66, 0x00, 0x40, 0x19, 0x01, 0x58, 0x00, 0x0e, 0x28, 0x01, 0x52, 0x00, 0x80, 0x38, 0x01, 0x4a,
  665. 0x00
  666. };
  667. static const ATOM_PPLIB_POWERPLAYTABLE *get_powerplay_table(
  668. struct pp_hwmgr *hwmgr)
  669. {
  670. const void *table_addr = hwmgr->soft_pp_table;
  671. uint8_t frev, crev;
  672. uint16_t size;
  673. if (!table_addr) {
  674. if (hwmgr->chip_id == CHIP_RAVEN) {
  675. table_addr = &soft_dummy_pp_table[0];
  676. hwmgr->soft_pp_table = &soft_dummy_pp_table[0];
  677. hwmgr->soft_pp_table_size = sizeof(soft_dummy_pp_table);
  678. } else {
  679. table_addr = smu_atom_get_data_table(hwmgr->adev,
  680. GetIndexIntoMasterTable(DATA, PowerPlayInfo),
  681. &size, &frev, &crev);
  682. hwmgr->soft_pp_table = table_addr;
  683. hwmgr->soft_pp_table_size = size;
  684. }
  685. }
  686. return (const ATOM_PPLIB_POWERPLAYTABLE *)table_addr;
  687. }
  688. int pp_tables_get_response_times(struct pp_hwmgr *hwmgr,
  689. uint32_t *vol_rep_time, uint32_t *bb_rep_time)
  690. {
  691. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_tab = get_powerplay_table(hwmgr);
  692. PP_ASSERT_WITH_CODE(NULL != powerplay_tab,
  693. "Missing PowerPlay Table!", return -EINVAL);
  694. *vol_rep_time = (uint32_t)le16_to_cpu(powerplay_tab->usVoltageTime);
  695. *bb_rep_time = (uint32_t)le16_to_cpu(powerplay_tab->usBackbiasTime);
  696. return 0;
  697. }
  698. int pp_tables_get_num_of_entries(struct pp_hwmgr *hwmgr,
  699. unsigned long *num_of_entries)
  700. {
  701. const StateArray *pstate_arrays;
  702. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table = get_powerplay_table(hwmgr);
  703. if (powerplay_table == NULL)
  704. return -1;
  705. if (powerplay_table->sHeader.ucTableFormatRevision >= 6) {
  706. pstate_arrays = (StateArray *)(((unsigned long)powerplay_table) +
  707. le16_to_cpu(powerplay_table->usStateArrayOffset));
  708. *num_of_entries = (unsigned long)(pstate_arrays->ucNumEntries);
  709. } else
  710. *num_of_entries = (unsigned long)(powerplay_table->ucNumStates);
  711. return 0;
  712. }
  713. int pp_tables_get_entry(struct pp_hwmgr *hwmgr,
  714. unsigned long entry_index,
  715. struct pp_power_state *ps,
  716. pp_tables_hw_clock_info_callback func)
  717. {
  718. int i;
  719. const StateArray *pstate_arrays;
  720. const ATOM_PPLIB_STATE_V2 *pstate_entry_v2;
  721. const ATOM_PPLIB_NONCLOCK_INFO *pnon_clock_info;
  722. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table = get_powerplay_table(hwmgr);
  723. int result = 0;
  724. int res = 0;
  725. const ClockInfoArray *pclock_arrays;
  726. const NonClockInfoArray *pnon_clock_arrays;
  727. const ATOM_PPLIB_STATE *pstate_entry;
  728. if (powerplay_table == NULL)
  729. return -1;
  730. ps->classification.bios_index = entry_index;
  731. if (powerplay_table->sHeader.ucTableFormatRevision >= 6) {
  732. pstate_arrays = (StateArray *)(((unsigned long)powerplay_table) +
  733. le16_to_cpu(powerplay_table->usStateArrayOffset));
  734. if (entry_index > pstate_arrays->ucNumEntries)
  735. return -1;
  736. pstate_entry_v2 = get_state_entry_v2(pstate_arrays, entry_index);
  737. pclock_arrays = (ClockInfoArray *)(((unsigned long)powerplay_table) +
  738. le16_to_cpu(powerplay_table->usClockInfoArrayOffset));
  739. pnon_clock_arrays = (NonClockInfoArray *)(((unsigned long)powerplay_table) +
  740. le16_to_cpu(powerplay_table->usNonClockInfoArrayOffset));
  741. pnon_clock_info = (ATOM_PPLIB_NONCLOCK_INFO *)((unsigned long)(pnon_clock_arrays->nonClockInfo) +
  742. (pstate_entry_v2->nonClockInfoIndex * pnon_clock_arrays->ucEntrySize));
  743. result = init_non_clock_fields(hwmgr, ps, pnon_clock_arrays->ucEntrySize, pnon_clock_info);
  744. for (i = 0; i < pstate_entry_v2->ucNumDPMLevels; i++) {
  745. const void *pclock_info = (const void *)(
  746. (unsigned long)(pclock_arrays->clockInfo) +
  747. (pstate_entry_v2->clockInfoIndex[i] * pclock_arrays->ucEntrySize));
  748. res = func(hwmgr, &ps->hardware, i, pclock_info);
  749. if ((0 == result) && (0 != res))
  750. result = res;
  751. }
  752. } else {
  753. if (entry_index > powerplay_table->ucNumStates)
  754. return -1;
  755. pstate_entry = (ATOM_PPLIB_STATE *)((unsigned long)powerplay_table + powerplay_table->usStateArrayOffset +
  756. entry_index * powerplay_table->ucStateEntrySize);
  757. pnon_clock_info = (ATOM_PPLIB_NONCLOCK_INFO *)((unsigned long)powerplay_table +
  758. le16_to_cpu(powerplay_table->usNonClockInfoArrayOffset) +
  759. pstate_entry->ucNonClockStateIndex *
  760. powerplay_table->ucNonClockSize);
  761. result = init_non_clock_fields(hwmgr, ps,
  762. powerplay_table->ucNonClockSize,
  763. pnon_clock_info);
  764. for (i = 0; i < powerplay_table->ucStateEntrySize-1; i++) {
  765. const void *pclock_info = (const void *)((unsigned long)powerplay_table +
  766. le16_to_cpu(powerplay_table->usClockInfoArrayOffset) +
  767. pstate_entry->ucClockStateIndices[i] *
  768. powerplay_table->ucClockInfoSize);
  769. int res = func(hwmgr, &ps->hardware, i, pclock_info);
  770. if ((0 == result) && (0 != res))
  771. result = res;
  772. }
  773. }
  774. if ((0 == result) && (0 != (ps->classification.flags & PP_StateClassificationFlag_Boot))) {
  775. if (hwmgr->chip_family < AMDGPU_FAMILY_RV)
  776. result = hwmgr->hwmgr_func->patch_boot_state(hwmgr, &(ps->hardware));
  777. }
  778. return result;
  779. }
  780. static int init_powerplay_tables(
  781. struct pp_hwmgr *hwmgr,
  782. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table
  783. )
  784. {
  785. return 0;
  786. }
  787. static int init_thermal_controller(
  788. struct pp_hwmgr *hwmgr,
  789. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  790. {
  791. return 0;
  792. }
  793. static int init_overdrive_limits_V1_4(struct pp_hwmgr *hwmgr,
  794. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table,
  795. const ATOM_FIRMWARE_INFO_V1_4 *fw_info)
  796. {
  797. hwmgr->platform_descriptor.overdriveLimit.engineClock =
  798. le32_to_cpu(fw_info->ulASICMaxEngineClock);
  799. hwmgr->platform_descriptor.overdriveLimit.memoryClock =
  800. le32_to_cpu(fw_info->ulASICMaxMemoryClock);
  801. hwmgr->platform_descriptor.maxOverdriveVDDC =
  802. le32_to_cpu(fw_info->ul3DAccelerationEngineClock) & 0x7FF;
  803. hwmgr->platform_descriptor.minOverdriveVDDC =
  804. le16_to_cpu(fw_info->usBootUpVDDCVoltage);
  805. hwmgr->platform_descriptor.maxOverdriveVDDC =
  806. le16_to_cpu(fw_info->usBootUpVDDCVoltage);
  807. hwmgr->platform_descriptor.overdriveVDDCStep = 0;
  808. return 0;
  809. }
  810. static int init_overdrive_limits_V2_1(struct pp_hwmgr *hwmgr,
  811. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table,
  812. const ATOM_FIRMWARE_INFO_V2_1 *fw_info)
  813. {
  814. const ATOM_PPLIB_POWERPLAYTABLE3 *powerplay_table3;
  815. const ATOM_PPLIB_EXTENDEDHEADER *header;
  816. if (le16_to_cpu(powerplay_table->usTableSize) <
  817. sizeof(ATOM_PPLIB_POWERPLAYTABLE3))
  818. return 0;
  819. powerplay_table3 = (const ATOM_PPLIB_POWERPLAYTABLE3 *)powerplay_table;
  820. if (0 == powerplay_table3->usExtendendedHeaderOffset)
  821. return 0;
  822. header = (ATOM_PPLIB_EXTENDEDHEADER *)(((unsigned long) powerplay_table) +
  823. le16_to_cpu(powerplay_table3->usExtendendedHeaderOffset));
  824. hwmgr->platform_descriptor.overdriveLimit.engineClock = le32_to_cpu(header->ulMaxEngineClock);
  825. hwmgr->platform_descriptor.overdriveLimit.memoryClock = le32_to_cpu(header->ulMaxMemoryClock);
  826. hwmgr->platform_descriptor.minOverdriveVDDC = 0;
  827. hwmgr->platform_descriptor.maxOverdriveVDDC = 0;
  828. hwmgr->platform_descriptor.overdriveVDDCStep = 0;
  829. return 0;
  830. }
  831. static int init_overdrive_limits(struct pp_hwmgr *hwmgr,
  832. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  833. {
  834. int result = 0;
  835. uint8_t frev, crev;
  836. uint16_t size;
  837. const ATOM_COMMON_TABLE_HEADER *fw_info = NULL;
  838. hwmgr->platform_descriptor.overdriveLimit.engineClock = 0;
  839. hwmgr->platform_descriptor.overdriveLimit.memoryClock = 0;
  840. hwmgr->platform_descriptor.minOverdriveVDDC = 0;
  841. hwmgr->platform_descriptor.maxOverdriveVDDC = 0;
  842. hwmgr->platform_descriptor.overdriveVDDCStep = 0;
  843. if (hwmgr->chip_id == CHIP_RAVEN)
  844. return 0;
  845. /* We assume here that fw_info is unchanged if this call fails.*/
  846. fw_info = smu_atom_get_data_table(hwmgr->adev,
  847. GetIndexIntoMasterTable(DATA, FirmwareInfo),
  848. &size, &frev, &crev);
  849. if ((fw_info->ucTableFormatRevision == 1)
  850. && (fw_info->usStructureSize >= sizeof(ATOM_FIRMWARE_INFO_V1_4)))
  851. result = init_overdrive_limits_V1_4(hwmgr,
  852. powerplay_table,
  853. (const ATOM_FIRMWARE_INFO_V1_4 *)fw_info);
  854. else if ((fw_info->ucTableFormatRevision == 2)
  855. && (fw_info->usStructureSize >= sizeof(ATOM_FIRMWARE_INFO_V2_1)))
  856. result = init_overdrive_limits_V2_1(hwmgr,
  857. powerplay_table,
  858. (const ATOM_FIRMWARE_INFO_V2_1 *)fw_info);
  859. return result;
  860. }
  861. static int get_uvd_clock_voltage_limit_table(struct pp_hwmgr *hwmgr,
  862. struct phm_uvd_clock_voltage_dependency_table **ptable,
  863. const ATOM_PPLIB_UVD_Clock_Voltage_Limit_Table *table,
  864. const UVDClockInfoArray *array)
  865. {
  866. unsigned long table_size, i;
  867. struct phm_uvd_clock_voltage_dependency_table *uvd_table;
  868. table_size = sizeof(unsigned long) +
  869. sizeof(struct phm_uvd_clock_voltage_dependency_table) *
  870. table->numEntries;
  871. uvd_table = kzalloc(table_size, GFP_KERNEL);
  872. if (NULL == uvd_table)
  873. return -ENOMEM;
  874. uvd_table->count = table->numEntries;
  875. for (i = 0; i < table->numEntries; i++) {
  876. const UVDClockInfo *entry =
  877. &array->entries[table->entries[i].ucUVDClockInfoIndex];
  878. uvd_table->entries[i].v = (unsigned long)le16_to_cpu(table->entries[i].usVoltage);
  879. uvd_table->entries[i].vclk = ((unsigned long)entry->ucVClkHigh << 16)
  880. | le16_to_cpu(entry->usVClkLow);
  881. uvd_table->entries[i].dclk = ((unsigned long)entry->ucDClkHigh << 16)
  882. | le16_to_cpu(entry->usDClkLow);
  883. }
  884. *ptable = uvd_table;
  885. return 0;
  886. }
  887. static int get_vce_clock_voltage_limit_table(struct pp_hwmgr *hwmgr,
  888. struct phm_vce_clock_voltage_dependency_table **ptable,
  889. const ATOM_PPLIB_VCE_Clock_Voltage_Limit_Table *table,
  890. const VCEClockInfoArray *array)
  891. {
  892. unsigned long table_size, i;
  893. struct phm_vce_clock_voltage_dependency_table *vce_table = NULL;
  894. table_size = sizeof(unsigned long) +
  895. sizeof(struct phm_vce_clock_voltage_dependency_table)
  896. * table->numEntries;
  897. vce_table = kzalloc(table_size, GFP_KERNEL);
  898. if (NULL == vce_table)
  899. return -ENOMEM;
  900. vce_table->count = table->numEntries;
  901. for (i = 0; i < table->numEntries; i++) {
  902. const VCEClockInfo *entry = &array->entries[table->entries[i].ucVCEClockInfoIndex];
  903. vce_table->entries[i].v = (unsigned long)le16_to_cpu(table->entries[i].usVoltage);
  904. vce_table->entries[i].evclk = ((unsigned long)entry->ucEVClkHigh << 16)
  905. | le16_to_cpu(entry->usEVClkLow);
  906. vce_table->entries[i].ecclk = ((unsigned long)entry->ucECClkHigh << 16)
  907. | le16_to_cpu(entry->usECClkLow);
  908. }
  909. *ptable = vce_table;
  910. return 0;
  911. }
  912. static int get_samu_clock_voltage_limit_table(struct pp_hwmgr *hwmgr,
  913. struct phm_samu_clock_voltage_dependency_table **ptable,
  914. const ATOM_PPLIB_SAMClk_Voltage_Limit_Table *table)
  915. {
  916. unsigned long table_size, i;
  917. struct phm_samu_clock_voltage_dependency_table *samu_table;
  918. table_size = sizeof(unsigned long) +
  919. sizeof(struct phm_samu_clock_voltage_dependency_table) *
  920. table->numEntries;
  921. samu_table = kzalloc(table_size, GFP_KERNEL);
  922. if (NULL == samu_table)
  923. return -ENOMEM;
  924. samu_table->count = table->numEntries;
  925. for (i = 0; i < table->numEntries; i++) {
  926. samu_table->entries[i].v = (unsigned long)le16_to_cpu(table->entries[i].usVoltage);
  927. samu_table->entries[i].samclk = ((unsigned long)table->entries[i].ucSAMClockHigh << 16)
  928. | le16_to_cpu(table->entries[i].usSAMClockLow);
  929. }
  930. *ptable = samu_table;
  931. return 0;
  932. }
  933. static int get_acp_clock_voltage_limit_table(struct pp_hwmgr *hwmgr,
  934. struct phm_acp_clock_voltage_dependency_table **ptable,
  935. const ATOM_PPLIB_ACPClk_Voltage_Limit_Table *table)
  936. {
  937. unsigned table_size, i;
  938. struct phm_acp_clock_voltage_dependency_table *acp_table;
  939. table_size = sizeof(unsigned long) +
  940. sizeof(struct phm_acp_clock_voltage_dependency_table) *
  941. table->numEntries;
  942. acp_table = kzalloc(table_size, GFP_KERNEL);
  943. if (NULL == acp_table)
  944. return -ENOMEM;
  945. acp_table->count = (unsigned long)table->numEntries;
  946. for (i = 0; i < table->numEntries; i++) {
  947. acp_table->entries[i].v = (unsigned long)le16_to_cpu(table->entries[i].usVoltage);
  948. acp_table->entries[i].acpclk = ((unsigned long)table->entries[i].ucACPClockHigh << 16)
  949. | le16_to_cpu(table->entries[i].usACPClockLow);
  950. }
  951. *ptable = acp_table;
  952. return 0;
  953. }
  954. static int init_clock_voltage_dependency(struct pp_hwmgr *hwmgr,
  955. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  956. {
  957. ATOM_PPLIB_Clock_Voltage_Dependency_Table *table;
  958. ATOM_PPLIB_Clock_Voltage_Limit_Table *limit_table;
  959. int result = 0;
  960. uint16_t vce_clock_info_array_offset;
  961. uint16_t uvd_clock_info_array_offset;
  962. uint16_t table_offset;
  963. hwmgr->dyn_state.vddc_dependency_on_sclk = NULL;
  964. hwmgr->dyn_state.vddci_dependency_on_mclk = NULL;
  965. hwmgr->dyn_state.vddc_dependency_on_mclk = NULL;
  966. hwmgr->dyn_state.vddc_dep_on_dal_pwrl = NULL;
  967. hwmgr->dyn_state.mvdd_dependency_on_mclk = NULL;
  968. hwmgr->dyn_state.vce_clock_voltage_dependency_table = NULL;
  969. hwmgr->dyn_state.uvd_clock_voltage_dependency_table = NULL;
  970. hwmgr->dyn_state.samu_clock_voltage_dependency_table = NULL;
  971. hwmgr->dyn_state.acp_clock_voltage_dependency_table = NULL;
  972. hwmgr->dyn_state.ppm_parameter_table = NULL;
  973. hwmgr->dyn_state.vdd_gfx_dependency_on_sclk = NULL;
  974. vce_clock_info_array_offset = get_vce_clock_info_array_offset(
  975. hwmgr, powerplay_table);
  976. table_offset = get_vce_clock_voltage_limit_table_offset(hwmgr,
  977. powerplay_table);
  978. if (vce_clock_info_array_offset > 0 && table_offset > 0) {
  979. const VCEClockInfoArray *array = (const VCEClockInfoArray *)
  980. (((unsigned long) powerplay_table) +
  981. vce_clock_info_array_offset);
  982. const ATOM_PPLIB_VCE_Clock_Voltage_Limit_Table *table =
  983. (const ATOM_PPLIB_VCE_Clock_Voltage_Limit_Table *)
  984. (((unsigned long) powerplay_table) + table_offset);
  985. result = get_vce_clock_voltage_limit_table(hwmgr,
  986. &hwmgr->dyn_state.vce_clock_voltage_dependency_table,
  987. table, array);
  988. }
  989. uvd_clock_info_array_offset = get_uvd_clock_info_array_offset(hwmgr, powerplay_table);
  990. table_offset = get_uvd_clock_voltage_limit_table_offset(hwmgr, powerplay_table);
  991. if (uvd_clock_info_array_offset > 0 && table_offset > 0) {
  992. const UVDClockInfoArray *array = (const UVDClockInfoArray *)
  993. (((unsigned long) powerplay_table) +
  994. uvd_clock_info_array_offset);
  995. const ATOM_PPLIB_UVD_Clock_Voltage_Limit_Table *ptable =
  996. (const ATOM_PPLIB_UVD_Clock_Voltage_Limit_Table *)
  997. (((unsigned long) powerplay_table) + table_offset);
  998. result = get_uvd_clock_voltage_limit_table(hwmgr,
  999. &hwmgr->dyn_state.uvd_clock_voltage_dependency_table, ptable, array);
  1000. }
  1001. table_offset = get_samu_clock_voltage_limit_table_offset(hwmgr,
  1002. powerplay_table);
  1003. if (table_offset > 0) {
  1004. const ATOM_PPLIB_SAMClk_Voltage_Limit_Table *ptable =
  1005. (const ATOM_PPLIB_SAMClk_Voltage_Limit_Table *)
  1006. (((unsigned long) powerplay_table) + table_offset);
  1007. result = get_samu_clock_voltage_limit_table(hwmgr,
  1008. &hwmgr->dyn_state.samu_clock_voltage_dependency_table, ptable);
  1009. }
  1010. table_offset = get_acp_clock_voltage_limit_table_offset(hwmgr,
  1011. powerplay_table);
  1012. if (table_offset > 0) {
  1013. const ATOM_PPLIB_ACPClk_Voltage_Limit_Table *ptable =
  1014. (const ATOM_PPLIB_ACPClk_Voltage_Limit_Table *)
  1015. (((unsigned long) powerplay_table) + table_offset);
  1016. result = get_acp_clock_voltage_limit_table(hwmgr,
  1017. &hwmgr->dyn_state.acp_clock_voltage_dependency_table, ptable);
  1018. }
  1019. table_offset = get_cacp_tdp_table_offset(hwmgr, powerplay_table);
  1020. if (table_offset > 0) {
  1021. UCHAR rev_id = *(UCHAR *)(((unsigned long)powerplay_table) + table_offset);
  1022. if (rev_id > 0) {
  1023. const ATOM_PPLIB_POWERTUNE_Table_V1 *tune_table =
  1024. (const ATOM_PPLIB_POWERTUNE_Table_V1 *)
  1025. (((unsigned long) powerplay_table) + table_offset);
  1026. result = get_cac_tdp_table(hwmgr, &hwmgr->dyn_state.cac_dtp_table,
  1027. &tune_table->power_tune_table,
  1028. le16_to_cpu(tune_table->usMaximumPowerDeliveryLimit));
  1029. hwmgr->dyn_state.cac_dtp_table->usDefaultTargetOperatingTemp =
  1030. le16_to_cpu(tune_table->usTjMax);
  1031. } else {
  1032. const ATOM_PPLIB_POWERTUNE_Table *tune_table =
  1033. (const ATOM_PPLIB_POWERTUNE_Table *)
  1034. (((unsigned long) powerplay_table) + table_offset);
  1035. result = get_cac_tdp_table(hwmgr,
  1036. &hwmgr->dyn_state.cac_dtp_table,
  1037. &tune_table->power_tune_table, 255);
  1038. }
  1039. }
  1040. if (le16_to_cpu(powerplay_table->usTableSize) >=
  1041. sizeof(ATOM_PPLIB_POWERPLAYTABLE4)) {
  1042. const ATOM_PPLIB_POWERPLAYTABLE4 *powerplay_table4 =
  1043. (const ATOM_PPLIB_POWERPLAYTABLE4 *)powerplay_table;
  1044. if (0 != powerplay_table4->usVddcDependencyOnSCLKOffset) {
  1045. table = (ATOM_PPLIB_Clock_Voltage_Dependency_Table *)
  1046. (((unsigned long) powerplay_table4) +
  1047. powerplay_table4->usVddcDependencyOnSCLKOffset);
  1048. result = get_clock_voltage_dependency_table(hwmgr,
  1049. &hwmgr->dyn_state.vddc_dependency_on_sclk, table);
  1050. }
  1051. if (result == 0 && (0 != powerplay_table4->usVddciDependencyOnMCLKOffset)) {
  1052. table = (ATOM_PPLIB_Clock_Voltage_Dependency_Table *)
  1053. (((unsigned long) powerplay_table4) +
  1054. powerplay_table4->usVddciDependencyOnMCLKOffset);
  1055. result = get_clock_voltage_dependency_table(hwmgr,
  1056. &hwmgr->dyn_state.vddci_dependency_on_mclk, table);
  1057. }
  1058. if (result == 0 && (0 != powerplay_table4->usVddcDependencyOnMCLKOffset)) {
  1059. table = (ATOM_PPLIB_Clock_Voltage_Dependency_Table *)
  1060. (((unsigned long) powerplay_table4) +
  1061. powerplay_table4->usVddcDependencyOnMCLKOffset);
  1062. result = get_clock_voltage_dependency_table(hwmgr,
  1063. &hwmgr->dyn_state.vddc_dependency_on_mclk, table);
  1064. }
  1065. if (result == 0 && (0 != powerplay_table4->usMaxClockVoltageOnDCOffset)) {
  1066. limit_table = (ATOM_PPLIB_Clock_Voltage_Limit_Table *)
  1067. (((unsigned long) powerplay_table4) +
  1068. powerplay_table4->usMaxClockVoltageOnDCOffset);
  1069. result = get_clock_voltage_limit(hwmgr,
  1070. &hwmgr->dyn_state.max_clock_voltage_on_dc, limit_table);
  1071. }
  1072. if (result == 0 && (NULL != hwmgr->dyn_state.vddc_dependency_on_mclk) &&
  1073. (0 != hwmgr->dyn_state.vddc_dependency_on_mclk->count))
  1074. result = get_valid_clk(hwmgr, &hwmgr->dyn_state.valid_mclk_values,
  1075. hwmgr->dyn_state.vddc_dependency_on_mclk);
  1076. if(result == 0 && (NULL != hwmgr->dyn_state.vddc_dependency_on_sclk) &&
  1077. (0 != hwmgr->dyn_state.vddc_dependency_on_sclk->count))
  1078. result = get_valid_clk(hwmgr,
  1079. &hwmgr->dyn_state.valid_sclk_values,
  1080. hwmgr->dyn_state.vddc_dependency_on_sclk);
  1081. if (result == 0 && (0 != powerplay_table4->usMvddDependencyOnMCLKOffset)) {
  1082. table = (ATOM_PPLIB_Clock_Voltage_Dependency_Table *)
  1083. (((unsigned long) powerplay_table4) +
  1084. powerplay_table4->usMvddDependencyOnMCLKOffset);
  1085. result = get_clock_voltage_dependency_table(hwmgr,
  1086. &hwmgr->dyn_state.mvdd_dependency_on_mclk, table);
  1087. }
  1088. }
  1089. table_offset = get_sclk_vdd_gfx_clock_voltage_dependency_table_offset(hwmgr,
  1090. powerplay_table);
  1091. if (table_offset > 0) {
  1092. table = (ATOM_PPLIB_Clock_Voltage_Dependency_Table *)
  1093. (((unsigned long) powerplay_table) + table_offset);
  1094. result = get_clock_voltage_dependency_table(hwmgr,
  1095. &hwmgr->dyn_state.vdd_gfx_dependency_on_sclk, table);
  1096. }
  1097. return result;
  1098. }
  1099. static int get_cac_leakage_table(struct pp_hwmgr *hwmgr,
  1100. struct phm_cac_leakage_table **ptable,
  1101. const ATOM_PPLIB_CAC_Leakage_Table *table)
  1102. {
  1103. struct phm_cac_leakage_table *cac_leakage_table;
  1104. unsigned long table_size, i;
  1105. if (hwmgr == NULL || table == NULL || ptable == NULL)
  1106. return -EINVAL;
  1107. table_size = sizeof(ULONG) +
  1108. (sizeof(struct phm_cac_leakage_table) * table->ucNumEntries);
  1109. cac_leakage_table = kzalloc(table_size, GFP_KERNEL);
  1110. if (cac_leakage_table == NULL)
  1111. return -ENOMEM;
  1112. cac_leakage_table->count = (ULONG)table->ucNumEntries;
  1113. for (i = 0; i < cac_leakage_table->count; i++) {
  1114. if (phm_cap_enabled(hwmgr->platform_descriptor.platformCaps,
  1115. PHM_PlatformCaps_EVV)) {
  1116. cac_leakage_table->entries[i].Vddc1 = le16_to_cpu(table->entries[i].usVddc1);
  1117. cac_leakage_table->entries[i].Vddc2 = le16_to_cpu(table->entries[i].usVddc2);
  1118. cac_leakage_table->entries[i].Vddc3 = le16_to_cpu(table->entries[i].usVddc3);
  1119. } else {
  1120. cac_leakage_table->entries[i].Vddc = le16_to_cpu(table->entries[i].usVddc);
  1121. cac_leakage_table->entries[i].Leakage = le32_to_cpu(table->entries[i].ulLeakageValue);
  1122. }
  1123. }
  1124. *ptable = cac_leakage_table;
  1125. return 0;
  1126. }
  1127. static int get_platform_power_management_table(struct pp_hwmgr *hwmgr,
  1128. ATOM_PPLIB_PPM_Table *atom_ppm_table)
  1129. {
  1130. struct phm_ppm_table *ptr = kzalloc(sizeof(struct phm_ppm_table), GFP_KERNEL);
  1131. if (NULL == ptr)
  1132. return -ENOMEM;
  1133. ptr->ppm_design = atom_ppm_table->ucPpmDesign;
  1134. ptr->cpu_core_number = le16_to_cpu(atom_ppm_table->usCpuCoreNumber);
  1135. ptr->platform_tdp = le32_to_cpu(atom_ppm_table->ulPlatformTDP);
  1136. ptr->small_ac_platform_tdp = le32_to_cpu(atom_ppm_table->ulSmallACPlatformTDP);
  1137. ptr->platform_tdc = le32_to_cpu(atom_ppm_table->ulPlatformTDC);
  1138. ptr->small_ac_platform_tdc = le32_to_cpu(atom_ppm_table->ulSmallACPlatformTDC);
  1139. ptr->apu_tdp = le32_to_cpu(atom_ppm_table->ulApuTDP);
  1140. ptr->dgpu_tdp = le32_to_cpu(atom_ppm_table->ulDGpuTDP);
  1141. ptr->dgpu_ulv_power = le32_to_cpu(atom_ppm_table->ulDGpuUlvPower);
  1142. ptr->tj_max = le32_to_cpu(atom_ppm_table->ulTjmax);
  1143. hwmgr->dyn_state.ppm_parameter_table = ptr;
  1144. return 0;
  1145. }
  1146. static int init_dpm2_parameters(struct pp_hwmgr *hwmgr,
  1147. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  1148. {
  1149. int result = 0;
  1150. if (le16_to_cpu(powerplay_table->usTableSize) >=
  1151. sizeof(ATOM_PPLIB_POWERPLAYTABLE5)) {
  1152. const ATOM_PPLIB_POWERPLAYTABLE5 *ptable5 =
  1153. (const ATOM_PPLIB_POWERPLAYTABLE5 *)powerplay_table;
  1154. const ATOM_PPLIB_POWERPLAYTABLE4 *ptable4 =
  1155. (const ATOM_PPLIB_POWERPLAYTABLE4 *)
  1156. (&ptable5->basicTable4);
  1157. const ATOM_PPLIB_POWERPLAYTABLE3 *ptable3 =
  1158. (const ATOM_PPLIB_POWERPLAYTABLE3 *)
  1159. (&ptable4->basicTable3);
  1160. const ATOM_PPLIB_EXTENDEDHEADER *extended_header;
  1161. uint16_t table_offset;
  1162. ATOM_PPLIB_PPM_Table *atom_ppm_table;
  1163. hwmgr->platform_descriptor.TDPLimit = le32_to_cpu(ptable5->ulTDPLimit);
  1164. hwmgr->platform_descriptor.nearTDPLimit = le32_to_cpu(ptable5->ulNearTDPLimit);
  1165. hwmgr->platform_descriptor.TDPODLimit = le16_to_cpu(ptable5->usTDPODLimit);
  1166. hwmgr->platform_descriptor.TDPAdjustment = 0;
  1167. hwmgr->platform_descriptor.VidAdjustment = 0;
  1168. hwmgr->platform_descriptor.VidAdjustmentPolarity = 0;
  1169. hwmgr->platform_descriptor.VidMinLimit = 0;
  1170. hwmgr->platform_descriptor.VidMaxLimit = 1500000;
  1171. hwmgr->platform_descriptor.VidStep = 6250;
  1172. hwmgr->platform_descriptor.nearTDPLimitAdjusted = le32_to_cpu(ptable5->ulNearTDPLimit);
  1173. if (hwmgr->platform_descriptor.TDPODLimit != 0)
  1174. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  1175. PHM_PlatformCaps_PowerControl);
  1176. hwmgr->platform_descriptor.SQRampingThreshold = le32_to_cpu(ptable5->ulSQRampingThreshold);
  1177. hwmgr->platform_descriptor.CACLeakage = le32_to_cpu(ptable5->ulCACLeakage);
  1178. hwmgr->dyn_state.cac_leakage_table = NULL;
  1179. if (0 != ptable5->usCACLeakageTableOffset) {
  1180. const ATOM_PPLIB_CAC_Leakage_Table *pCAC_leakage_table =
  1181. (ATOM_PPLIB_CAC_Leakage_Table *)(((unsigned long)ptable5) +
  1182. le16_to_cpu(ptable5->usCACLeakageTableOffset));
  1183. result = get_cac_leakage_table(hwmgr,
  1184. &hwmgr->dyn_state.cac_leakage_table, pCAC_leakage_table);
  1185. }
  1186. hwmgr->platform_descriptor.LoadLineSlope = le16_to_cpu(ptable5->usLoadLineSlope);
  1187. hwmgr->dyn_state.ppm_parameter_table = NULL;
  1188. if (0 != ptable3->usExtendendedHeaderOffset) {
  1189. extended_header = (const ATOM_PPLIB_EXTENDEDHEADER *)
  1190. (((unsigned long)powerplay_table) +
  1191. le16_to_cpu(ptable3->usExtendendedHeaderOffset));
  1192. if ((extended_header->usPPMTableOffset > 0) &&
  1193. le16_to_cpu(extended_header->usSize) >=
  1194. SIZE_OF_ATOM_PPLIB_EXTENDEDHEADER_V5) {
  1195. table_offset = le16_to_cpu(extended_header->usPPMTableOffset);
  1196. atom_ppm_table = (ATOM_PPLIB_PPM_Table *)
  1197. (((unsigned long)powerplay_table) + table_offset);
  1198. if (0 == get_platform_power_management_table(hwmgr, atom_ppm_table))
  1199. phm_cap_set(hwmgr->platform_descriptor.platformCaps,
  1200. PHM_PlatformCaps_EnablePlatformPowerManagement);
  1201. }
  1202. }
  1203. }
  1204. return result;
  1205. }
  1206. static int init_phase_shedding_table(struct pp_hwmgr *hwmgr,
  1207. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table)
  1208. {
  1209. if (le16_to_cpu(powerplay_table->usTableSize) >=
  1210. sizeof(ATOM_PPLIB_POWERPLAYTABLE4)) {
  1211. const ATOM_PPLIB_POWERPLAYTABLE4 *powerplay_table4 =
  1212. (const ATOM_PPLIB_POWERPLAYTABLE4 *)powerplay_table;
  1213. if (0 != powerplay_table4->usVddcPhaseShedLimitsTableOffset) {
  1214. const ATOM_PPLIB_PhaseSheddingLimits_Table *ptable =
  1215. (ATOM_PPLIB_PhaseSheddingLimits_Table *)
  1216. (((unsigned long)powerplay_table4) +
  1217. le16_to_cpu(powerplay_table4->usVddcPhaseShedLimitsTableOffset));
  1218. struct phm_phase_shedding_limits_table *table;
  1219. unsigned long size, i;
  1220. size = sizeof(unsigned long) +
  1221. (sizeof(struct phm_phase_shedding_limits_table) *
  1222. ptable->ucNumEntries);
  1223. table = kzalloc(size, GFP_KERNEL);
  1224. if (table == NULL)
  1225. return -ENOMEM;
  1226. table->count = (unsigned long)ptable->ucNumEntries;
  1227. for (i = 0; i < table->count; i++) {
  1228. table->entries[i].Voltage = (unsigned long)le16_to_cpu(ptable->entries[i].usVoltage);
  1229. table->entries[i].Sclk = ((unsigned long)ptable->entries[i].ucSclkHigh << 16)
  1230. | le16_to_cpu(ptable->entries[i].usSclkLow);
  1231. table->entries[i].Mclk = ((unsigned long)ptable->entries[i].ucMclkHigh << 16)
  1232. | le16_to_cpu(ptable->entries[i].usMclkLow);
  1233. }
  1234. hwmgr->dyn_state.vddc_phase_shed_limits_table = table;
  1235. }
  1236. }
  1237. return 0;
  1238. }
  1239. static int get_number_of_vce_state_table_entries(
  1240. struct pp_hwmgr *hwmgr)
  1241. {
  1242. const ATOM_PPLIB_POWERPLAYTABLE *table =
  1243. get_powerplay_table(hwmgr);
  1244. const ATOM_PPLIB_VCE_State_Table *vce_table =
  1245. get_vce_state_table(hwmgr, table);
  1246. if (vce_table)
  1247. return vce_table->numEntries;
  1248. return 0;
  1249. }
  1250. static int get_vce_state_table_entry(struct pp_hwmgr *hwmgr,
  1251. unsigned long i,
  1252. struct amd_vce_state *vce_state,
  1253. void **clock_info,
  1254. unsigned long *flag)
  1255. {
  1256. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table = get_powerplay_table(hwmgr);
  1257. const ATOM_PPLIB_VCE_State_Table *vce_state_table = get_vce_state_table(hwmgr, powerplay_table);
  1258. unsigned short vce_clock_info_array_offset = get_vce_clock_info_array_offset(hwmgr, powerplay_table);
  1259. const VCEClockInfoArray *vce_clock_info_array = (const VCEClockInfoArray *)(((unsigned long) powerplay_table) + vce_clock_info_array_offset);
  1260. const ClockInfoArray *clock_arrays = (ClockInfoArray *)(((unsigned long)powerplay_table) + powerplay_table->usClockInfoArrayOffset);
  1261. const ATOM_PPLIB_VCE_State_Record *record = &vce_state_table->entries[i];
  1262. const VCEClockInfo *vce_clock_info = &vce_clock_info_array->entries[record->ucVCEClockInfoIndex];
  1263. unsigned long clockInfoIndex = record->ucClockInfoIndex & 0x3F;
  1264. *flag = (record->ucClockInfoIndex >> NUM_BITS_CLOCK_INFO_ARRAY_INDEX);
  1265. vce_state->evclk = ((uint32_t)vce_clock_info->ucEVClkHigh << 16) | vce_clock_info->usEVClkLow;
  1266. vce_state->ecclk = ((uint32_t)vce_clock_info->ucECClkHigh << 16) | vce_clock_info->usECClkLow;
  1267. *clock_info = (void *)((unsigned long)(clock_arrays->clockInfo) + (clockInfoIndex * clock_arrays->ucEntrySize));
  1268. return 0;
  1269. }
  1270. static int pp_tables_initialize(struct pp_hwmgr *hwmgr)
  1271. {
  1272. int result;
  1273. const ATOM_PPLIB_POWERPLAYTABLE *powerplay_table;
  1274. if (hwmgr->chip_id == CHIP_RAVEN)
  1275. return 0;
  1276. hwmgr->need_pp_table_upload = true;
  1277. powerplay_table = get_powerplay_table(hwmgr);
  1278. result = init_powerplay_tables(hwmgr, powerplay_table);
  1279. PP_ASSERT_WITH_CODE((result == 0),
  1280. "init_powerplay_tables failed", return result);
  1281. result = set_platform_caps(hwmgr,
  1282. le32_to_cpu(powerplay_table->ulPlatformCaps));
  1283. PP_ASSERT_WITH_CODE((result == 0),
  1284. "set_platform_caps failed", return result);
  1285. result = init_thermal_controller(hwmgr, powerplay_table);
  1286. PP_ASSERT_WITH_CODE((result == 0),
  1287. "init_thermal_controller failed", return result);
  1288. result = init_overdrive_limits(hwmgr, powerplay_table);
  1289. PP_ASSERT_WITH_CODE((result == 0),
  1290. "init_overdrive_limits failed", return result);
  1291. result = init_clock_voltage_dependency(hwmgr,
  1292. powerplay_table);
  1293. PP_ASSERT_WITH_CODE((result == 0),
  1294. "init_clock_voltage_dependency failed", return result);
  1295. result = init_dpm2_parameters(hwmgr, powerplay_table);
  1296. PP_ASSERT_WITH_CODE((result == 0),
  1297. "init_dpm2_parameters failed", return result);
  1298. result = init_phase_shedding_table(hwmgr, powerplay_table);
  1299. PP_ASSERT_WITH_CODE((result == 0),
  1300. "init_phase_shedding_table failed", return result);
  1301. return result;
  1302. }
  1303. static int pp_tables_uninitialize(struct pp_hwmgr *hwmgr)
  1304. {
  1305. if (hwmgr->chip_id == CHIP_RAVEN)
  1306. return 0;
  1307. kfree(hwmgr->dyn_state.vddc_dependency_on_sclk);
  1308. hwmgr->dyn_state.vddc_dependency_on_sclk = NULL;
  1309. kfree(hwmgr->dyn_state.vddci_dependency_on_mclk);
  1310. hwmgr->dyn_state.vddci_dependency_on_mclk = NULL;
  1311. kfree(hwmgr->dyn_state.vddc_dependency_on_mclk);
  1312. hwmgr->dyn_state.vddc_dependency_on_mclk = NULL;
  1313. kfree(hwmgr->dyn_state.mvdd_dependency_on_mclk);
  1314. hwmgr->dyn_state.mvdd_dependency_on_mclk = NULL;
  1315. kfree(hwmgr->dyn_state.valid_mclk_values);
  1316. hwmgr->dyn_state.valid_mclk_values = NULL;
  1317. kfree(hwmgr->dyn_state.valid_sclk_values);
  1318. hwmgr->dyn_state.valid_sclk_values = NULL;
  1319. kfree(hwmgr->dyn_state.cac_leakage_table);
  1320. hwmgr->dyn_state.cac_leakage_table = NULL;
  1321. kfree(hwmgr->dyn_state.vddc_phase_shed_limits_table);
  1322. hwmgr->dyn_state.vddc_phase_shed_limits_table = NULL;
  1323. kfree(hwmgr->dyn_state.vce_clock_voltage_dependency_table);
  1324. hwmgr->dyn_state.vce_clock_voltage_dependency_table = NULL;
  1325. kfree(hwmgr->dyn_state.uvd_clock_voltage_dependency_table);
  1326. hwmgr->dyn_state.uvd_clock_voltage_dependency_table = NULL;
  1327. kfree(hwmgr->dyn_state.samu_clock_voltage_dependency_table);
  1328. hwmgr->dyn_state.samu_clock_voltage_dependency_table = NULL;
  1329. kfree(hwmgr->dyn_state.acp_clock_voltage_dependency_table);
  1330. hwmgr->dyn_state.acp_clock_voltage_dependency_table = NULL;
  1331. kfree(hwmgr->dyn_state.cac_dtp_table);
  1332. hwmgr->dyn_state.cac_dtp_table = NULL;
  1333. kfree(hwmgr->dyn_state.ppm_parameter_table);
  1334. hwmgr->dyn_state.ppm_parameter_table = NULL;
  1335. kfree(hwmgr->dyn_state.vdd_gfx_dependency_on_sclk);
  1336. hwmgr->dyn_state.vdd_gfx_dependency_on_sclk = NULL;
  1337. return 0;
  1338. }
  1339. const struct pp_table_func pptable_funcs = {
  1340. .pptable_init = pp_tables_initialize,
  1341. .pptable_fini = pp_tables_uninitialize,
  1342. .pptable_get_number_of_vce_state_table_entries =
  1343. get_number_of_vce_state_table_entries,
  1344. .pptable_get_vce_state_table_entry =
  1345. get_vce_state_table_entry,
  1346. };