amdgpu_pm.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a
  3. * copy of this software and associated documentation files (the "Software"),
  4. * to deal in the Software without restriction, including without limitation
  5. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. * and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. * OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * Authors: Rafał Miłecki <zajec5@gmail.com>
  21. * Alex Deucher <alexdeucher@gmail.com>
  22. */
  23. #include <drm/drmP.h>
  24. #include "amdgpu.h"
  25. #include "amdgpu_drv.h"
  26. #include "amdgpu_pm.h"
  27. #include "amdgpu_dpm.h"
  28. #include "atom.h"
  29. #include <linux/power_supply.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include "amd_powerplay.h"
  33. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev);
  34. static const struct cg_flag_name clocks[] = {
  35. {AMD_CG_SUPPORT_GFX_MGCG, "Graphics Medium Grain Clock Gating"},
  36. {AMD_CG_SUPPORT_GFX_MGLS, "Graphics Medium Grain memory Light Sleep"},
  37. {AMD_CG_SUPPORT_GFX_CGCG, "Graphics Coarse Grain Clock Gating"},
  38. {AMD_CG_SUPPORT_GFX_CGLS, "Graphics Coarse Grain memory Light Sleep"},
  39. {AMD_CG_SUPPORT_GFX_CGTS, "Graphics Coarse Grain Tree Shader Clock Gating"},
  40. {AMD_CG_SUPPORT_GFX_CGTS_LS, "Graphics Coarse Grain Tree Shader Light Sleep"},
  41. {AMD_CG_SUPPORT_GFX_CP_LS, "Graphics Command Processor Light Sleep"},
  42. {AMD_CG_SUPPORT_GFX_RLC_LS, "Graphics Run List Controller Light Sleep"},
  43. {AMD_CG_SUPPORT_GFX_3D_CGCG, "Graphics 3D Coarse Grain Clock Gating"},
  44. {AMD_CG_SUPPORT_GFX_3D_CGLS, "Graphics 3D Coarse Grain memory Light Sleep"},
  45. {AMD_CG_SUPPORT_MC_LS, "Memory Controller Light Sleep"},
  46. {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
  47. {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
  48. {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
  49. {AMD_CG_SUPPORT_BIF_MGCG, "Bus Interface Medium Grain Clock Gating"},
  50. {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
  51. {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
  52. {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
  53. {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
  54. {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
  55. {AMD_CG_SUPPORT_DRM_MGCG, "Digital Right Management Medium Grain Clock Gating"},
  56. {AMD_CG_SUPPORT_DRM_LS, "Digital Right Management Light Sleep"},
  57. {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
  58. {AMD_CG_SUPPORT_DF_MGCG, "Data Fabric Medium Grain Clock Gating"},
  59. {0, NULL},
  60. };
  61. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  62. {
  63. if (adev->pp_enabled)
  64. /* TODO */
  65. return;
  66. if (adev->pm.dpm_enabled) {
  67. mutex_lock(&adev->pm.mutex);
  68. if (power_supply_is_system_supplied() > 0)
  69. adev->pm.dpm.ac_power = true;
  70. else
  71. adev->pm.dpm.ac_power = false;
  72. if (adev->pm.funcs->enable_bapm)
  73. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  74. mutex_unlock(&adev->pm.mutex);
  75. }
  76. }
  77. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  78. struct device_attribute *attr,
  79. char *buf)
  80. {
  81. struct drm_device *ddev = dev_get_drvdata(dev);
  82. struct amdgpu_device *adev = ddev->dev_private;
  83. enum amd_pm_state_type pm;
  84. if (adev->pp_enabled) {
  85. pm = amdgpu_dpm_get_current_power_state(adev);
  86. } else
  87. pm = adev->pm.dpm.user_state;
  88. return snprintf(buf, PAGE_SIZE, "%s\n",
  89. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  90. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  91. }
  92. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  93. struct device_attribute *attr,
  94. const char *buf,
  95. size_t count)
  96. {
  97. struct drm_device *ddev = dev_get_drvdata(dev);
  98. struct amdgpu_device *adev = ddev->dev_private;
  99. enum amd_pm_state_type state;
  100. if (strncmp("battery", buf, strlen("battery")) == 0)
  101. state = POWER_STATE_TYPE_BATTERY;
  102. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  103. state = POWER_STATE_TYPE_BALANCED;
  104. else if (strncmp("performance", buf, strlen("performance")) == 0)
  105. state = POWER_STATE_TYPE_PERFORMANCE;
  106. else {
  107. count = -EINVAL;
  108. goto fail;
  109. }
  110. if (adev->pp_enabled) {
  111. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  112. } else {
  113. mutex_lock(&adev->pm.mutex);
  114. adev->pm.dpm.user_state = state;
  115. mutex_unlock(&adev->pm.mutex);
  116. /* Can't set dpm state when the card is off */
  117. if (!(adev->flags & AMD_IS_PX) ||
  118. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  119. amdgpu_pm_compute_clocks(adev);
  120. }
  121. fail:
  122. return count;
  123. }
  124. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  125. struct device_attribute *attr,
  126. char *buf)
  127. {
  128. struct drm_device *ddev = dev_get_drvdata(dev);
  129. struct amdgpu_device *adev = ddev->dev_private;
  130. enum amd_dpm_forced_level level;
  131. if ((adev->flags & AMD_IS_PX) &&
  132. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  133. return snprintf(buf, PAGE_SIZE, "off\n");
  134. level = amdgpu_dpm_get_performance_level(adev);
  135. return snprintf(buf, PAGE_SIZE, "%s\n",
  136. (level == AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  137. (level == AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  138. (level == AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  139. (level == AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
  140. (level == AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD) ? "profile_standard" :
  141. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK) ? "profile_min_sclk" :
  142. (level == AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK) ? "profile_min_mclk" :
  143. (level == AMD_DPM_FORCED_LEVEL_PROFILE_PEAK) ? "profile_peak" :
  144. "unknown");
  145. }
  146. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  147. struct device_attribute *attr,
  148. const char *buf,
  149. size_t count)
  150. {
  151. struct drm_device *ddev = dev_get_drvdata(dev);
  152. struct amdgpu_device *adev = ddev->dev_private;
  153. enum amd_dpm_forced_level level;
  154. enum amd_dpm_forced_level current_level;
  155. int ret = 0;
  156. /* Can't force performance level when the card is off */
  157. if ((adev->flags & AMD_IS_PX) &&
  158. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  159. return -EINVAL;
  160. current_level = amdgpu_dpm_get_performance_level(adev);
  161. if (strncmp("low", buf, strlen("low")) == 0) {
  162. level = AMD_DPM_FORCED_LEVEL_LOW;
  163. } else if (strncmp("high", buf, strlen("high")) == 0) {
  164. level = AMD_DPM_FORCED_LEVEL_HIGH;
  165. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  166. level = AMD_DPM_FORCED_LEVEL_AUTO;
  167. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  168. level = AMD_DPM_FORCED_LEVEL_MANUAL;
  169. } else if (strncmp("profile_exit", buf, strlen("profile_exit")) == 0) {
  170. level = AMD_DPM_FORCED_LEVEL_PROFILE_EXIT;
  171. } else if (strncmp("profile_standard", buf, strlen("profile_standard")) == 0) {
  172. level = AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD;
  173. } else if (strncmp("profile_min_sclk", buf, strlen("profile_min_sclk")) == 0) {
  174. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK;
  175. } else if (strncmp("profile_min_mclk", buf, strlen("profile_min_mclk")) == 0) {
  176. level = AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK;
  177. } else if (strncmp("profile_peak", buf, strlen("profile_peak")) == 0) {
  178. level = AMD_DPM_FORCED_LEVEL_PROFILE_PEAK;
  179. } else {
  180. count = -EINVAL;
  181. goto fail;
  182. }
  183. if (current_level == level)
  184. return count;
  185. if (adev->pp_enabled)
  186. amdgpu_dpm_force_performance_level(adev, level);
  187. else {
  188. mutex_lock(&adev->pm.mutex);
  189. if (adev->pm.dpm.thermal_active) {
  190. count = -EINVAL;
  191. mutex_unlock(&adev->pm.mutex);
  192. goto fail;
  193. }
  194. ret = amdgpu_dpm_force_performance_level(adev, level);
  195. if (ret)
  196. count = -EINVAL;
  197. else
  198. adev->pm.dpm.forced_level = level;
  199. mutex_unlock(&adev->pm.mutex);
  200. }
  201. fail:
  202. return count;
  203. }
  204. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  205. struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct drm_device *ddev = dev_get_drvdata(dev);
  209. struct amdgpu_device *adev = ddev->dev_private;
  210. struct pp_states_info data;
  211. int i, buf_len;
  212. if (adev->pp_enabled)
  213. amdgpu_dpm_get_pp_num_states(adev, &data);
  214. buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
  215. for (i = 0; i < data.nums; i++)
  216. buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
  217. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  218. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  219. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  220. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  221. return buf_len;
  222. }
  223. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  224. struct device_attribute *attr,
  225. char *buf)
  226. {
  227. struct drm_device *ddev = dev_get_drvdata(dev);
  228. struct amdgpu_device *adev = ddev->dev_private;
  229. struct pp_states_info data;
  230. enum amd_pm_state_type pm = 0;
  231. int i = 0;
  232. if (adev->pp_enabled) {
  233. pm = amdgpu_dpm_get_current_power_state(adev);
  234. amdgpu_dpm_get_pp_num_states(adev, &data);
  235. for (i = 0; i < data.nums; i++) {
  236. if (pm == data.states[i])
  237. break;
  238. }
  239. if (i == data.nums)
  240. i = -EINVAL;
  241. }
  242. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  243. }
  244. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  245. struct device_attribute *attr,
  246. char *buf)
  247. {
  248. struct drm_device *ddev = dev_get_drvdata(dev);
  249. struct amdgpu_device *adev = ddev->dev_private;
  250. struct pp_states_info data;
  251. enum amd_pm_state_type pm = 0;
  252. int i;
  253. if (adev->pp_force_state_enabled && adev->pp_enabled) {
  254. pm = amdgpu_dpm_get_current_power_state(adev);
  255. amdgpu_dpm_get_pp_num_states(adev, &data);
  256. for (i = 0; i < data.nums; i++) {
  257. if (pm == data.states[i])
  258. break;
  259. }
  260. if (i == data.nums)
  261. i = -EINVAL;
  262. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  263. } else
  264. return snprintf(buf, PAGE_SIZE, "\n");
  265. }
  266. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  267. struct device_attribute *attr,
  268. const char *buf,
  269. size_t count)
  270. {
  271. struct drm_device *ddev = dev_get_drvdata(dev);
  272. struct amdgpu_device *adev = ddev->dev_private;
  273. enum amd_pm_state_type state = 0;
  274. unsigned long idx;
  275. int ret;
  276. if (strlen(buf) == 1)
  277. adev->pp_force_state_enabled = false;
  278. else if (adev->pp_enabled) {
  279. struct pp_states_info data;
  280. ret = kstrtoul(buf, 0, &idx);
  281. if (ret || idx >= ARRAY_SIZE(data.states)) {
  282. count = -EINVAL;
  283. goto fail;
  284. }
  285. amdgpu_dpm_get_pp_num_states(adev, &data);
  286. state = data.states[idx];
  287. /* only set user selected power states */
  288. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  289. state != POWER_STATE_TYPE_DEFAULT) {
  290. amdgpu_dpm_dispatch_task(adev,
  291. AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  292. adev->pp_force_state_enabled = true;
  293. }
  294. }
  295. fail:
  296. return count;
  297. }
  298. static ssize_t amdgpu_get_pp_table(struct device *dev,
  299. struct device_attribute *attr,
  300. char *buf)
  301. {
  302. struct drm_device *ddev = dev_get_drvdata(dev);
  303. struct amdgpu_device *adev = ddev->dev_private;
  304. char *table = NULL;
  305. int size;
  306. if (adev->pp_enabled)
  307. size = amdgpu_dpm_get_pp_table(adev, &table);
  308. else
  309. return 0;
  310. if (size >= PAGE_SIZE)
  311. size = PAGE_SIZE - 1;
  312. memcpy(buf, table, size);
  313. return size;
  314. }
  315. static ssize_t amdgpu_set_pp_table(struct device *dev,
  316. struct device_attribute *attr,
  317. const char *buf,
  318. size_t count)
  319. {
  320. struct drm_device *ddev = dev_get_drvdata(dev);
  321. struct amdgpu_device *adev = ddev->dev_private;
  322. if (adev->pp_enabled)
  323. amdgpu_dpm_set_pp_table(adev, buf, count);
  324. return count;
  325. }
  326. static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
  327. struct device_attribute *attr,
  328. char *buf)
  329. {
  330. struct drm_device *ddev = dev_get_drvdata(dev);
  331. struct amdgpu_device *adev = ddev->dev_private;
  332. ssize_t size = 0;
  333. if (adev->pp_enabled)
  334. size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
  335. else if (adev->pm.funcs->print_clock_levels)
  336. size = adev->pm.funcs->print_clock_levels(adev, PP_SCLK, buf);
  337. return size;
  338. }
  339. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  340. struct device_attribute *attr,
  341. const char *buf,
  342. size_t count)
  343. {
  344. struct drm_device *ddev = dev_get_drvdata(dev);
  345. struct amdgpu_device *adev = ddev->dev_private;
  346. int ret;
  347. long level;
  348. uint32_t i, mask = 0;
  349. char sub_str[2];
  350. for (i = 0; i < strlen(buf); i++) {
  351. if (*(buf + i) == '\n')
  352. continue;
  353. sub_str[0] = *(buf + i);
  354. sub_str[1] = '\0';
  355. ret = kstrtol(sub_str, 0, &level);
  356. if (ret) {
  357. count = -EINVAL;
  358. goto fail;
  359. }
  360. mask |= 1 << level;
  361. }
  362. if (adev->pp_enabled)
  363. amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
  364. else if (adev->pm.funcs->force_clock_level)
  365. adev->pm.funcs->force_clock_level(adev, PP_SCLK, mask);
  366. fail:
  367. return count;
  368. }
  369. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  370. struct device_attribute *attr,
  371. char *buf)
  372. {
  373. struct drm_device *ddev = dev_get_drvdata(dev);
  374. struct amdgpu_device *adev = ddev->dev_private;
  375. ssize_t size = 0;
  376. if (adev->pp_enabled)
  377. size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
  378. else if (adev->pm.funcs->print_clock_levels)
  379. size = adev->pm.funcs->print_clock_levels(adev, PP_MCLK, buf);
  380. return size;
  381. }
  382. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  383. struct device_attribute *attr,
  384. const char *buf,
  385. size_t count)
  386. {
  387. struct drm_device *ddev = dev_get_drvdata(dev);
  388. struct amdgpu_device *adev = ddev->dev_private;
  389. int ret;
  390. long level;
  391. uint32_t i, mask = 0;
  392. char sub_str[2];
  393. for (i = 0; i < strlen(buf); i++) {
  394. if (*(buf + i) == '\n')
  395. continue;
  396. sub_str[0] = *(buf + i);
  397. sub_str[1] = '\0';
  398. ret = kstrtol(sub_str, 0, &level);
  399. if (ret) {
  400. count = -EINVAL;
  401. goto fail;
  402. }
  403. mask |= 1 << level;
  404. }
  405. if (adev->pp_enabled)
  406. amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
  407. else if (adev->pm.funcs->force_clock_level)
  408. adev->pm.funcs->force_clock_level(adev, PP_MCLK, mask);
  409. fail:
  410. return count;
  411. }
  412. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  413. struct device_attribute *attr,
  414. char *buf)
  415. {
  416. struct drm_device *ddev = dev_get_drvdata(dev);
  417. struct amdgpu_device *adev = ddev->dev_private;
  418. ssize_t size = 0;
  419. if (adev->pp_enabled)
  420. size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
  421. else if (adev->pm.funcs->print_clock_levels)
  422. size = adev->pm.funcs->print_clock_levels(adev, PP_PCIE, buf);
  423. return size;
  424. }
  425. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  426. struct device_attribute *attr,
  427. const char *buf,
  428. size_t count)
  429. {
  430. struct drm_device *ddev = dev_get_drvdata(dev);
  431. struct amdgpu_device *adev = ddev->dev_private;
  432. int ret;
  433. long level;
  434. uint32_t i, mask = 0;
  435. char sub_str[2];
  436. for (i = 0; i < strlen(buf); i++) {
  437. if (*(buf + i) == '\n')
  438. continue;
  439. sub_str[0] = *(buf + i);
  440. sub_str[1] = '\0';
  441. ret = kstrtol(sub_str, 0, &level);
  442. if (ret) {
  443. count = -EINVAL;
  444. goto fail;
  445. }
  446. mask |= 1 << level;
  447. }
  448. if (adev->pp_enabled)
  449. amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
  450. else if (adev->pm.funcs->force_clock_level)
  451. adev->pm.funcs->force_clock_level(adev, PP_PCIE, mask);
  452. fail:
  453. return count;
  454. }
  455. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  456. struct device_attribute *attr,
  457. char *buf)
  458. {
  459. struct drm_device *ddev = dev_get_drvdata(dev);
  460. struct amdgpu_device *adev = ddev->dev_private;
  461. uint32_t value = 0;
  462. if (adev->pp_enabled)
  463. value = amdgpu_dpm_get_sclk_od(adev);
  464. else if (adev->pm.funcs->get_sclk_od)
  465. value = adev->pm.funcs->get_sclk_od(adev);
  466. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  467. }
  468. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  469. struct device_attribute *attr,
  470. const char *buf,
  471. size_t count)
  472. {
  473. struct drm_device *ddev = dev_get_drvdata(dev);
  474. struct amdgpu_device *adev = ddev->dev_private;
  475. int ret;
  476. long int value;
  477. ret = kstrtol(buf, 0, &value);
  478. if (ret) {
  479. count = -EINVAL;
  480. goto fail;
  481. }
  482. if (adev->pp_enabled) {
  483. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  484. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  485. } else if (adev->pm.funcs->set_sclk_od) {
  486. adev->pm.funcs->set_sclk_od(adev, (uint32_t)value);
  487. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  488. amdgpu_pm_compute_clocks(adev);
  489. }
  490. fail:
  491. return count;
  492. }
  493. static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
  494. struct device_attribute *attr,
  495. char *buf)
  496. {
  497. struct drm_device *ddev = dev_get_drvdata(dev);
  498. struct amdgpu_device *adev = ddev->dev_private;
  499. uint32_t value = 0;
  500. if (adev->pp_enabled)
  501. value = amdgpu_dpm_get_mclk_od(adev);
  502. else if (adev->pm.funcs->get_mclk_od)
  503. value = adev->pm.funcs->get_mclk_od(adev);
  504. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  505. }
  506. static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
  507. struct device_attribute *attr,
  508. const char *buf,
  509. size_t count)
  510. {
  511. struct drm_device *ddev = dev_get_drvdata(dev);
  512. struct amdgpu_device *adev = ddev->dev_private;
  513. int ret;
  514. long int value;
  515. ret = kstrtol(buf, 0, &value);
  516. if (ret) {
  517. count = -EINVAL;
  518. goto fail;
  519. }
  520. if (adev->pp_enabled) {
  521. amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
  522. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  523. } else if (adev->pm.funcs->set_mclk_od) {
  524. adev->pm.funcs->set_mclk_od(adev, (uint32_t)value);
  525. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  526. amdgpu_pm_compute_clocks(adev);
  527. }
  528. fail:
  529. return count;
  530. }
  531. static ssize_t amdgpu_get_pp_power_profile(struct device *dev,
  532. char *buf, struct amd_pp_profile *query)
  533. {
  534. struct drm_device *ddev = dev_get_drvdata(dev);
  535. struct amdgpu_device *adev = ddev->dev_private;
  536. int ret = 0;
  537. if (adev->pp_enabled)
  538. ret = amdgpu_dpm_get_power_profile_state(
  539. adev, query);
  540. else if (adev->pm.funcs->get_power_profile_state)
  541. ret = adev->pm.funcs->get_power_profile_state(
  542. adev, query);
  543. if (ret)
  544. return ret;
  545. return snprintf(buf, PAGE_SIZE,
  546. "%d %d %d %d %d\n",
  547. query->min_sclk / 100,
  548. query->min_mclk / 100,
  549. query->activity_threshold,
  550. query->up_hyst,
  551. query->down_hyst);
  552. }
  553. static ssize_t amdgpu_get_pp_gfx_power_profile(struct device *dev,
  554. struct device_attribute *attr,
  555. char *buf)
  556. {
  557. struct amd_pp_profile query = {0};
  558. query.type = AMD_PP_GFX_PROFILE;
  559. return amdgpu_get_pp_power_profile(dev, buf, &query);
  560. }
  561. static ssize_t amdgpu_get_pp_compute_power_profile(struct device *dev,
  562. struct device_attribute *attr,
  563. char *buf)
  564. {
  565. struct amd_pp_profile query = {0};
  566. query.type = AMD_PP_COMPUTE_PROFILE;
  567. return amdgpu_get_pp_power_profile(dev, buf, &query);
  568. }
  569. static ssize_t amdgpu_set_pp_power_profile(struct device *dev,
  570. const char *buf,
  571. size_t count,
  572. struct amd_pp_profile *request)
  573. {
  574. struct drm_device *ddev = dev_get_drvdata(dev);
  575. struct amdgpu_device *adev = ddev->dev_private;
  576. uint32_t loop = 0;
  577. char *sub_str, buf_cpy[128], *tmp_str;
  578. const char delimiter[3] = {' ', '\n', '\0'};
  579. long int value;
  580. int ret = 0;
  581. if (strncmp("reset", buf, strlen("reset")) == 0) {
  582. if (adev->pp_enabled)
  583. ret = amdgpu_dpm_reset_power_profile_state(
  584. adev, request);
  585. else if (adev->pm.funcs->reset_power_profile_state)
  586. ret = adev->pm.funcs->reset_power_profile_state(
  587. adev, request);
  588. if (ret) {
  589. count = -EINVAL;
  590. goto fail;
  591. }
  592. return count;
  593. }
  594. if (strncmp("set", buf, strlen("set")) == 0) {
  595. if (adev->pp_enabled)
  596. ret = amdgpu_dpm_set_power_profile_state(
  597. adev, request);
  598. else if (adev->pm.funcs->set_power_profile_state)
  599. ret = adev->pm.funcs->set_power_profile_state(
  600. adev, request);
  601. if (ret) {
  602. count = -EINVAL;
  603. goto fail;
  604. }
  605. return count;
  606. }
  607. if (count + 1 >= 128) {
  608. count = -EINVAL;
  609. goto fail;
  610. }
  611. memcpy(buf_cpy, buf, count + 1);
  612. tmp_str = buf_cpy;
  613. while (tmp_str[0]) {
  614. sub_str = strsep(&tmp_str, delimiter);
  615. ret = kstrtol(sub_str, 0, &value);
  616. if (ret) {
  617. count = -EINVAL;
  618. goto fail;
  619. }
  620. switch (loop) {
  621. case 0:
  622. /* input unit MHz convert to dpm table unit 10KHz*/
  623. request->min_sclk = (uint32_t)value * 100;
  624. break;
  625. case 1:
  626. /* input unit MHz convert to dpm table unit 10KHz*/
  627. request->min_mclk = (uint32_t)value * 100;
  628. break;
  629. case 2:
  630. request->activity_threshold = (uint16_t)value;
  631. break;
  632. case 3:
  633. request->up_hyst = (uint8_t)value;
  634. break;
  635. case 4:
  636. request->down_hyst = (uint8_t)value;
  637. break;
  638. default:
  639. break;
  640. }
  641. loop++;
  642. }
  643. if (adev->pp_enabled)
  644. ret = amdgpu_dpm_set_power_profile_state(
  645. adev, request);
  646. else if (adev->pm.funcs->set_power_profile_state)
  647. ret = adev->pm.funcs->set_power_profile_state(
  648. adev, request);
  649. if (ret)
  650. count = -EINVAL;
  651. fail:
  652. return count;
  653. }
  654. static ssize_t amdgpu_set_pp_gfx_power_profile(struct device *dev,
  655. struct device_attribute *attr,
  656. const char *buf,
  657. size_t count)
  658. {
  659. struct amd_pp_profile request = {0};
  660. request.type = AMD_PP_GFX_PROFILE;
  661. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  662. }
  663. static ssize_t amdgpu_set_pp_compute_power_profile(struct device *dev,
  664. struct device_attribute *attr,
  665. const char *buf,
  666. size_t count)
  667. {
  668. struct amd_pp_profile request = {0};
  669. request.type = AMD_PP_COMPUTE_PROFILE;
  670. return amdgpu_set_pp_power_profile(dev, buf, count, &request);
  671. }
  672. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  673. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  674. amdgpu_get_dpm_forced_performance_level,
  675. amdgpu_set_dpm_forced_performance_level);
  676. static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
  677. static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
  678. static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
  679. amdgpu_get_pp_force_state,
  680. amdgpu_set_pp_force_state);
  681. static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
  682. amdgpu_get_pp_table,
  683. amdgpu_set_pp_table);
  684. static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
  685. amdgpu_get_pp_dpm_sclk,
  686. amdgpu_set_pp_dpm_sclk);
  687. static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
  688. amdgpu_get_pp_dpm_mclk,
  689. amdgpu_set_pp_dpm_mclk);
  690. static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
  691. amdgpu_get_pp_dpm_pcie,
  692. amdgpu_set_pp_dpm_pcie);
  693. static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
  694. amdgpu_get_pp_sclk_od,
  695. amdgpu_set_pp_sclk_od);
  696. static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
  697. amdgpu_get_pp_mclk_od,
  698. amdgpu_set_pp_mclk_od);
  699. static DEVICE_ATTR(pp_gfx_power_profile, S_IRUGO | S_IWUSR,
  700. amdgpu_get_pp_gfx_power_profile,
  701. amdgpu_set_pp_gfx_power_profile);
  702. static DEVICE_ATTR(pp_compute_power_profile, S_IRUGO | S_IWUSR,
  703. amdgpu_get_pp_compute_power_profile,
  704. amdgpu_set_pp_compute_power_profile);
  705. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  706. struct device_attribute *attr,
  707. char *buf)
  708. {
  709. struct amdgpu_device *adev = dev_get_drvdata(dev);
  710. struct drm_device *ddev = adev->ddev;
  711. int temp;
  712. /* Can't get temperature when the card is off */
  713. if ((adev->flags & AMD_IS_PX) &&
  714. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  715. return -EINVAL;
  716. if (!adev->pp_enabled && !adev->pm.funcs->get_temperature)
  717. temp = 0;
  718. else
  719. temp = amdgpu_dpm_get_temperature(adev);
  720. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  721. }
  722. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  723. struct device_attribute *attr,
  724. char *buf)
  725. {
  726. struct amdgpu_device *adev = dev_get_drvdata(dev);
  727. int hyst = to_sensor_dev_attr(attr)->index;
  728. int temp;
  729. if (hyst)
  730. temp = adev->pm.dpm.thermal.min_temp;
  731. else
  732. temp = adev->pm.dpm.thermal.max_temp;
  733. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  734. }
  735. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. struct amdgpu_device *adev = dev_get_drvdata(dev);
  740. u32 pwm_mode = 0;
  741. if (!adev->pp_enabled && !adev->pm.funcs->get_fan_control_mode)
  742. return -EINVAL;
  743. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  744. /* never 0 (full-speed), fuse or smc-controlled always */
  745. return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
  746. }
  747. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  748. struct device_attribute *attr,
  749. const char *buf,
  750. size_t count)
  751. {
  752. struct amdgpu_device *adev = dev_get_drvdata(dev);
  753. int err;
  754. int value;
  755. if (!adev->pp_enabled && !adev->pm.funcs->set_fan_control_mode)
  756. return -EINVAL;
  757. err = kstrtoint(buf, 10, &value);
  758. if (err)
  759. return err;
  760. switch (value) {
  761. case 1: /* manual, percent-based */
  762. amdgpu_dpm_set_fan_control_mode(adev, FDO_PWM_MODE_STATIC);
  763. break;
  764. default: /* disable */
  765. amdgpu_dpm_set_fan_control_mode(adev, 0);
  766. break;
  767. }
  768. return count;
  769. }
  770. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  771. struct device_attribute *attr,
  772. char *buf)
  773. {
  774. return sprintf(buf, "%i\n", 0);
  775. }
  776. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  777. struct device_attribute *attr,
  778. char *buf)
  779. {
  780. return sprintf(buf, "%i\n", 255);
  781. }
  782. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  783. struct device_attribute *attr,
  784. const char *buf, size_t count)
  785. {
  786. struct amdgpu_device *adev = dev_get_drvdata(dev);
  787. int err;
  788. u32 value;
  789. err = kstrtou32(buf, 10, &value);
  790. if (err)
  791. return err;
  792. value = (value * 100) / 255;
  793. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  794. if (err)
  795. return err;
  796. return count;
  797. }
  798. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  799. struct device_attribute *attr,
  800. char *buf)
  801. {
  802. struct amdgpu_device *adev = dev_get_drvdata(dev);
  803. int err;
  804. u32 speed;
  805. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  806. if (err)
  807. return err;
  808. speed = (speed * 255) / 100;
  809. return sprintf(buf, "%i\n", speed);
  810. }
  811. static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
  812. struct device_attribute *attr,
  813. char *buf)
  814. {
  815. struct amdgpu_device *adev = dev_get_drvdata(dev);
  816. int err;
  817. u32 speed;
  818. err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
  819. if (err)
  820. return err;
  821. return sprintf(buf, "%i\n", speed);
  822. }
  823. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  824. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  825. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  826. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  827. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  828. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  829. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  830. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
  831. static struct attribute *hwmon_attributes[] = {
  832. &sensor_dev_attr_temp1_input.dev_attr.attr,
  833. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  834. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  835. &sensor_dev_attr_pwm1.dev_attr.attr,
  836. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  837. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  838. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  839. &sensor_dev_attr_fan1_input.dev_attr.attr,
  840. NULL
  841. };
  842. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  843. struct attribute *attr, int index)
  844. {
  845. struct device *dev = kobj_to_dev(kobj);
  846. struct amdgpu_device *adev = dev_get_drvdata(dev);
  847. umode_t effective_mode = attr->mode;
  848. /* Skip limit attributes if DPM is not enabled */
  849. if (!adev->pm.dpm_enabled &&
  850. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  851. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  852. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  853. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  854. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  855. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  856. return 0;
  857. if (adev->pp_enabled)
  858. return effective_mode;
  859. /* Skip fan attributes if fan is not present */
  860. if (adev->pm.no_fan &&
  861. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  862. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  863. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  864. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  865. return 0;
  866. /* mask fan attributes if we have no bindings for this asic to expose */
  867. if ((!adev->pm.funcs->get_fan_speed_percent &&
  868. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  869. (!adev->pm.funcs->get_fan_control_mode &&
  870. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  871. effective_mode &= ~S_IRUGO;
  872. if ((!adev->pm.funcs->set_fan_speed_percent &&
  873. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  874. (!adev->pm.funcs->set_fan_control_mode &&
  875. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  876. effective_mode &= ~S_IWUSR;
  877. /* hide max/min values if we can't both query and manage the fan */
  878. if ((!adev->pm.funcs->set_fan_speed_percent &&
  879. !adev->pm.funcs->get_fan_speed_percent) &&
  880. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  881. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  882. return 0;
  883. /* requires powerplay */
  884. if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
  885. return 0;
  886. return effective_mode;
  887. }
  888. static const struct attribute_group hwmon_attrgroup = {
  889. .attrs = hwmon_attributes,
  890. .is_visible = hwmon_attributes_visible,
  891. };
  892. static const struct attribute_group *hwmon_groups[] = {
  893. &hwmon_attrgroup,
  894. NULL
  895. };
  896. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  897. {
  898. struct amdgpu_device *adev =
  899. container_of(work, struct amdgpu_device,
  900. pm.dpm.thermal.work);
  901. /* switch to the thermal state */
  902. enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  903. if (!adev->pm.dpm_enabled)
  904. return;
  905. if (adev->pm.funcs->get_temperature) {
  906. int temp = amdgpu_dpm_get_temperature(adev);
  907. if (temp < adev->pm.dpm.thermal.min_temp)
  908. /* switch back the user state */
  909. dpm_state = adev->pm.dpm.user_state;
  910. } else {
  911. if (adev->pm.dpm.thermal.high_to_low)
  912. /* switch back the user state */
  913. dpm_state = adev->pm.dpm.user_state;
  914. }
  915. mutex_lock(&adev->pm.mutex);
  916. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  917. adev->pm.dpm.thermal_active = true;
  918. else
  919. adev->pm.dpm.thermal_active = false;
  920. adev->pm.dpm.state = dpm_state;
  921. mutex_unlock(&adev->pm.mutex);
  922. amdgpu_pm_compute_clocks(adev);
  923. }
  924. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  925. enum amd_pm_state_type dpm_state)
  926. {
  927. int i;
  928. struct amdgpu_ps *ps;
  929. u32 ui_class;
  930. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  931. true : false;
  932. /* check if the vblank period is too short to adjust the mclk */
  933. if (single_display && adev->pm.funcs->vblank_too_short) {
  934. if (amdgpu_dpm_vblank_too_short(adev))
  935. single_display = false;
  936. }
  937. /* certain older asics have a separare 3D performance state,
  938. * so try that first if the user selected performance
  939. */
  940. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  941. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  942. /* balanced states don't exist at the moment */
  943. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  944. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  945. restart_search:
  946. /* Pick the best power state based on current conditions */
  947. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  948. ps = &adev->pm.dpm.ps[i];
  949. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  950. switch (dpm_state) {
  951. /* user states */
  952. case POWER_STATE_TYPE_BATTERY:
  953. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  954. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  955. if (single_display)
  956. return ps;
  957. } else
  958. return ps;
  959. }
  960. break;
  961. case POWER_STATE_TYPE_BALANCED:
  962. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  963. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  964. if (single_display)
  965. return ps;
  966. } else
  967. return ps;
  968. }
  969. break;
  970. case POWER_STATE_TYPE_PERFORMANCE:
  971. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  972. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  973. if (single_display)
  974. return ps;
  975. } else
  976. return ps;
  977. }
  978. break;
  979. /* internal states */
  980. case POWER_STATE_TYPE_INTERNAL_UVD:
  981. if (adev->pm.dpm.uvd_ps)
  982. return adev->pm.dpm.uvd_ps;
  983. else
  984. break;
  985. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  986. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  987. return ps;
  988. break;
  989. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  990. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  991. return ps;
  992. break;
  993. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  994. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  995. return ps;
  996. break;
  997. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  998. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  999. return ps;
  1000. break;
  1001. case POWER_STATE_TYPE_INTERNAL_BOOT:
  1002. return adev->pm.dpm.boot_ps;
  1003. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  1004. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  1005. return ps;
  1006. break;
  1007. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1008. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  1009. return ps;
  1010. break;
  1011. case POWER_STATE_TYPE_INTERNAL_ULV:
  1012. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  1013. return ps;
  1014. break;
  1015. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1016. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  1017. return ps;
  1018. break;
  1019. default:
  1020. break;
  1021. }
  1022. }
  1023. /* use a fallback state if we didn't match */
  1024. switch (dpm_state) {
  1025. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  1026. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  1027. goto restart_search;
  1028. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  1029. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  1030. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  1031. if (adev->pm.dpm.uvd_ps) {
  1032. return adev->pm.dpm.uvd_ps;
  1033. } else {
  1034. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1035. goto restart_search;
  1036. }
  1037. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  1038. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  1039. goto restart_search;
  1040. case POWER_STATE_TYPE_INTERNAL_ACPI:
  1041. dpm_state = POWER_STATE_TYPE_BATTERY;
  1042. goto restart_search;
  1043. case POWER_STATE_TYPE_BATTERY:
  1044. case POWER_STATE_TYPE_BALANCED:
  1045. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  1046. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  1047. goto restart_search;
  1048. default:
  1049. break;
  1050. }
  1051. return NULL;
  1052. }
  1053. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  1054. {
  1055. struct amdgpu_ps *ps;
  1056. enum amd_pm_state_type dpm_state;
  1057. int ret;
  1058. bool equal;
  1059. /* if dpm init failed */
  1060. if (!adev->pm.dpm_enabled)
  1061. return;
  1062. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  1063. /* add other state override checks here */
  1064. if ((!adev->pm.dpm.thermal_active) &&
  1065. (!adev->pm.dpm.uvd_active))
  1066. adev->pm.dpm.state = adev->pm.dpm.user_state;
  1067. }
  1068. dpm_state = adev->pm.dpm.state;
  1069. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  1070. if (ps)
  1071. adev->pm.dpm.requested_ps = ps;
  1072. else
  1073. return;
  1074. if (amdgpu_dpm == 1) {
  1075. printk("switching from power state:\n");
  1076. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  1077. printk("switching to power state:\n");
  1078. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  1079. }
  1080. /* update whether vce is active */
  1081. ps->vce_active = adev->pm.dpm.vce_active;
  1082. amdgpu_dpm_display_configuration_changed(adev);
  1083. ret = amdgpu_dpm_pre_set_power_state(adev);
  1084. if (ret)
  1085. return;
  1086. if ((0 != amgdpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal)))
  1087. equal = false;
  1088. if (equal)
  1089. return;
  1090. amdgpu_dpm_set_power_state(adev);
  1091. amdgpu_dpm_post_set_power_state(adev);
  1092. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  1093. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  1094. if (adev->pm.funcs->force_performance_level) {
  1095. if (adev->pm.dpm.thermal_active) {
  1096. enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
  1097. /* force low perf level for thermal */
  1098. amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
  1099. /* save the user's level */
  1100. adev->pm.dpm.forced_level = level;
  1101. } else {
  1102. /* otherwise, user selected level */
  1103. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  1104. }
  1105. }
  1106. }
  1107. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  1108. {
  1109. if (adev->pp_enabled || adev->pm.funcs->powergate_uvd) {
  1110. /* enable/disable UVD */
  1111. mutex_lock(&adev->pm.mutex);
  1112. amdgpu_dpm_powergate_uvd(adev, !enable);
  1113. mutex_unlock(&adev->pm.mutex);
  1114. } else {
  1115. if (enable) {
  1116. mutex_lock(&adev->pm.mutex);
  1117. adev->pm.dpm.uvd_active = true;
  1118. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  1119. mutex_unlock(&adev->pm.mutex);
  1120. } else {
  1121. mutex_lock(&adev->pm.mutex);
  1122. adev->pm.dpm.uvd_active = false;
  1123. mutex_unlock(&adev->pm.mutex);
  1124. }
  1125. amdgpu_pm_compute_clocks(adev);
  1126. }
  1127. }
  1128. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  1129. {
  1130. if (adev->pp_enabled || adev->pm.funcs->powergate_vce) {
  1131. /* enable/disable VCE */
  1132. mutex_lock(&adev->pm.mutex);
  1133. amdgpu_dpm_powergate_vce(adev, !enable);
  1134. mutex_unlock(&adev->pm.mutex);
  1135. } else {
  1136. if (enable) {
  1137. mutex_lock(&adev->pm.mutex);
  1138. adev->pm.dpm.vce_active = true;
  1139. /* XXX select vce level based on ring/task */
  1140. adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
  1141. mutex_unlock(&adev->pm.mutex);
  1142. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1143. AMD_CG_STATE_UNGATE);
  1144. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1145. AMD_PG_STATE_UNGATE);
  1146. amdgpu_pm_compute_clocks(adev);
  1147. } else {
  1148. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1149. AMD_PG_STATE_GATE);
  1150. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
  1151. AMD_CG_STATE_GATE);
  1152. mutex_lock(&adev->pm.mutex);
  1153. adev->pm.dpm.vce_active = false;
  1154. mutex_unlock(&adev->pm.mutex);
  1155. amdgpu_pm_compute_clocks(adev);
  1156. }
  1157. }
  1158. }
  1159. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  1160. {
  1161. int i;
  1162. if (adev->pp_enabled)
  1163. /* TO DO */
  1164. return;
  1165. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  1166. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  1167. }
  1168. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  1169. {
  1170. int ret;
  1171. if (adev->pm.sysfs_initialized)
  1172. return 0;
  1173. if (!adev->pp_enabled) {
  1174. if (adev->pm.funcs->get_temperature == NULL)
  1175. return 0;
  1176. }
  1177. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  1178. DRIVER_NAME, adev,
  1179. hwmon_groups);
  1180. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  1181. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  1182. dev_err(adev->dev,
  1183. "Unable to register hwmon device: %d\n", ret);
  1184. return ret;
  1185. }
  1186. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  1187. if (ret) {
  1188. DRM_ERROR("failed to create device file for dpm state\n");
  1189. return ret;
  1190. }
  1191. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1192. if (ret) {
  1193. DRM_ERROR("failed to create device file for dpm state\n");
  1194. return ret;
  1195. }
  1196. if (adev->pp_enabled) {
  1197. ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
  1198. if (ret) {
  1199. DRM_ERROR("failed to create device file pp_num_states\n");
  1200. return ret;
  1201. }
  1202. ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
  1203. if (ret) {
  1204. DRM_ERROR("failed to create device file pp_cur_state\n");
  1205. return ret;
  1206. }
  1207. ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
  1208. if (ret) {
  1209. DRM_ERROR("failed to create device file pp_force_state\n");
  1210. return ret;
  1211. }
  1212. ret = device_create_file(adev->dev, &dev_attr_pp_table);
  1213. if (ret) {
  1214. DRM_ERROR("failed to create device file pp_table\n");
  1215. return ret;
  1216. }
  1217. }
  1218. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1219. if (ret) {
  1220. DRM_ERROR("failed to create device file pp_dpm_sclk\n");
  1221. return ret;
  1222. }
  1223. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1224. if (ret) {
  1225. DRM_ERROR("failed to create device file pp_dpm_mclk\n");
  1226. return ret;
  1227. }
  1228. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1229. if (ret) {
  1230. DRM_ERROR("failed to create device file pp_dpm_pcie\n");
  1231. return ret;
  1232. }
  1233. ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
  1234. if (ret) {
  1235. DRM_ERROR("failed to create device file pp_sclk_od\n");
  1236. return ret;
  1237. }
  1238. ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
  1239. if (ret) {
  1240. DRM_ERROR("failed to create device file pp_mclk_od\n");
  1241. return ret;
  1242. }
  1243. ret = device_create_file(adev->dev,
  1244. &dev_attr_pp_gfx_power_profile);
  1245. if (ret) {
  1246. DRM_ERROR("failed to create device file "
  1247. "pp_gfx_power_profile\n");
  1248. return ret;
  1249. }
  1250. ret = device_create_file(adev->dev,
  1251. &dev_attr_pp_compute_power_profile);
  1252. if (ret) {
  1253. DRM_ERROR("failed to create device file "
  1254. "pp_compute_power_profile\n");
  1255. return ret;
  1256. }
  1257. ret = amdgpu_debugfs_pm_init(adev);
  1258. if (ret) {
  1259. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1260. return ret;
  1261. }
  1262. adev->pm.sysfs_initialized = true;
  1263. return 0;
  1264. }
  1265. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  1266. {
  1267. if (adev->pm.int_hwmon_dev)
  1268. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  1269. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  1270. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1271. if (adev->pp_enabled) {
  1272. device_remove_file(adev->dev, &dev_attr_pp_num_states);
  1273. device_remove_file(adev->dev, &dev_attr_pp_cur_state);
  1274. device_remove_file(adev->dev, &dev_attr_pp_force_state);
  1275. device_remove_file(adev->dev, &dev_attr_pp_table);
  1276. }
  1277. device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1278. device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1279. device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1280. device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
  1281. device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
  1282. device_remove_file(adev->dev,
  1283. &dev_attr_pp_gfx_power_profile);
  1284. device_remove_file(adev->dev,
  1285. &dev_attr_pp_compute_power_profile);
  1286. }
  1287. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  1288. {
  1289. struct drm_device *ddev = adev->ddev;
  1290. struct drm_crtc *crtc;
  1291. struct amdgpu_crtc *amdgpu_crtc;
  1292. int i = 0;
  1293. if (!adev->pm.dpm_enabled)
  1294. return;
  1295. if (adev->mode_info.num_crtc)
  1296. amdgpu_display_bandwidth_update(adev);
  1297. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1298. struct amdgpu_ring *ring = adev->rings[i];
  1299. if (ring && ring->ready)
  1300. amdgpu_fence_wait_empty(ring);
  1301. }
  1302. if (adev->pp_enabled) {
  1303. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE, NULL, NULL);
  1304. } else {
  1305. mutex_lock(&adev->pm.mutex);
  1306. adev->pm.dpm.new_active_crtcs = 0;
  1307. adev->pm.dpm.new_active_crtc_count = 0;
  1308. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  1309. list_for_each_entry(crtc,
  1310. &ddev->mode_config.crtc_list, head) {
  1311. amdgpu_crtc = to_amdgpu_crtc(crtc);
  1312. if (crtc->enabled) {
  1313. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  1314. adev->pm.dpm.new_active_crtc_count++;
  1315. }
  1316. }
  1317. }
  1318. /* update battery/ac status */
  1319. if (power_supply_is_system_supplied() > 0)
  1320. adev->pm.dpm.ac_power = true;
  1321. else
  1322. adev->pm.dpm.ac_power = false;
  1323. amdgpu_dpm_change_power_state_locked(adev);
  1324. mutex_unlock(&adev->pm.mutex);
  1325. }
  1326. }
  1327. /*
  1328. * Debugfs info
  1329. */
  1330. #if defined(CONFIG_DEBUG_FS)
  1331. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  1332. {
  1333. uint32_t value;
  1334. struct pp_gpu_power query = {0};
  1335. int size;
  1336. /* sanity check PP is enabled */
  1337. if (!(adev->powerplay.pp_funcs &&
  1338. adev->powerplay.pp_funcs->read_sensor))
  1339. return -EINVAL;
  1340. /* GPU Clocks */
  1341. size = sizeof(value);
  1342. seq_printf(m, "GFX Clocks and Power:\n");
  1343. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, (void *)&value, &size))
  1344. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  1345. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, (void *)&value, &size))
  1346. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  1347. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, (void *)&value, &size))
  1348. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  1349. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, (void *)&value, &size))
  1350. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  1351. size = sizeof(query);
  1352. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_POWER, (void *)&query, &size)) {
  1353. seq_printf(m, "\t%u.%u W (VDDC)\n", query.vddc_power >> 8,
  1354. query.vddc_power & 0xff);
  1355. seq_printf(m, "\t%u.%u W (VDDCI)\n", query.vddci_power >> 8,
  1356. query.vddci_power & 0xff);
  1357. seq_printf(m, "\t%u.%u W (max GPU)\n", query.max_gpu_power >> 8,
  1358. query.max_gpu_power & 0xff);
  1359. seq_printf(m, "\t%u.%u W (average GPU)\n", query.average_gpu_power >> 8,
  1360. query.average_gpu_power & 0xff);
  1361. }
  1362. size = sizeof(value);
  1363. seq_printf(m, "\n");
  1364. /* GPU Temp */
  1365. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, (void *)&value, &size))
  1366. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  1367. /* GPU Load */
  1368. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, (void *)&value, &size))
  1369. seq_printf(m, "GPU Load: %u %%\n", value);
  1370. seq_printf(m, "\n");
  1371. /* UVD clocks */
  1372. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, (void *)&value, &size)) {
  1373. if (!value) {
  1374. seq_printf(m, "UVD: Disabled\n");
  1375. } else {
  1376. seq_printf(m, "UVD: Enabled\n");
  1377. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, (void *)&value, &size))
  1378. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  1379. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, (void *)&value, &size))
  1380. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  1381. }
  1382. }
  1383. seq_printf(m, "\n");
  1384. /* VCE clocks */
  1385. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, (void *)&value, &size)) {
  1386. if (!value) {
  1387. seq_printf(m, "VCE: Disabled\n");
  1388. } else {
  1389. seq_printf(m, "VCE: Enabled\n");
  1390. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, (void *)&value, &size))
  1391. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  1392. }
  1393. }
  1394. return 0;
  1395. }
  1396. static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
  1397. {
  1398. int i;
  1399. for (i = 0; clocks[i].flag; i++)
  1400. seq_printf(m, "\t%s: %s\n", clocks[i].name,
  1401. (flags & clocks[i].flag) ? "On" : "Off");
  1402. }
  1403. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  1404. {
  1405. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1406. struct drm_device *dev = node->minor->dev;
  1407. struct amdgpu_device *adev = dev->dev_private;
  1408. struct drm_device *ddev = adev->ddev;
  1409. u32 flags = 0;
  1410. amdgpu_get_clockgating_state(adev, &flags);
  1411. seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
  1412. amdgpu_parse_cg_state(m, flags);
  1413. seq_printf(m, "\n");
  1414. if (!adev->pm.dpm_enabled) {
  1415. seq_printf(m, "dpm not enabled\n");
  1416. return 0;
  1417. }
  1418. if ((adev->flags & AMD_IS_PX) &&
  1419. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1420. seq_printf(m, "PX asic powered off\n");
  1421. } else if (adev->pp_enabled) {
  1422. return amdgpu_debugfs_pm_info_pp(m, adev);
  1423. } else {
  1424. mutex_lock(&adev->pm.mutex);
  1425. if (adev->pm.funcs->debugfs_print_current_performance_level)
  1426. adev->pm.funcs->debugfs_print_current_performance_level(adev, m);
  1427. else
  1428. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1429. mutex_unlock(&adev->pm.mutex);
  1430. }
  1431. return 0;
  1432. }
  1433. static const struct drm_info_list amdgpu_pm_info_list[] = {
  1434. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  1435. };
  1436. #endif
  1437. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  1438. {
  1439. #if defined(CONFIG_DEBUG_FS)
  1440. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  1441. #else
  1442. return 0;
  1443. #endif
  1444. }