amdgpu_pm.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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 Light Sleep"},
  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_MC_LS, "Memory Controller Light Sleep"},
  44. {AMD_CG_SUPPORT_MC_MGCG, "Memory Controller Medium Grain Clock Gating"},
  45. {AMD_CG_SUPPORT_SDMA_LS, "System Direct Memory Access Light Sleep"},
  46. {AMD_CG_SUPPORT_SDMA_MGCG, "System Direct Memory Access Medium Grain Clock Gating"},
  47. {AMD_CG_SUPPORT_BIF_LS, "Bus Interface Light Sleep"},
  48. {AMD_CG_SUPPORT_UVD_MGCG, "Unified Video Decoder Medium Grain Clock Gating"},
  49. {AMD_CG_SUPPORT_VCE_MGCG, "Video Compression Engine Medium Grain Clock Gating"},
  50. {AMD_CG_SUPPORT_HDP_LS, "Host Data Path Light Sleep"},
  51. {AMD_CG_SUPPORT_HDP_MGCG, "Host Data Path Medium Grain Clock Gating"},
  52. {AMD_CG_SUPPORT_ROM_MGCG, "Rom Medium Grain Clock Gating"},
  53. {0, NULL},
  54. };
  55. void amdgpu_pm_acpi_event_handler(struct amdgpu_device *adev)
  56. {
  57. if (adev->pp_enabled)
  58. /* TODO */
  59. return;
  60. if (adev->pm.dpm_enabled) {
  61. mutex_lock(&adev->pm.mutex);
  62. if (power_supply_is_system_supplied() > 0)
  63. adev->pm.dpm.ac_power = true;
  64. else
  65. adev->pm.dpm.ac_power = false;
  66. if (adev->pm.funcs->enable_bapm)
  67. amdgpu_dpm_enable_bapm(adev, adev->pm.dpm.ac_power);
  68. mutex_unlock(&adev->pm.mutex);
  69. }
  70. }
  71. static ssize_t amdgpu_get_dpm_state(struct device *dev,
  72. struct device_attribute *attr,
  73. char *buf)
  74. {
  75. struct drm_device *ddev = dev_get_drvdata(dev);
  76. struct amdgpu_device *adev = ddev->dev_private;
  77. enum amd_pm_state_type pm;
  78. if (adev->pp_enabled) {
  79. pm = amdgpu_dpm_get_current_power_state(adev);
  80. } else
  81. pm = adev->pm.dpm.user_state;
  82. return snprintf(buf, PAGE_SIZE, "%s\n",
  83. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  84. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  85. }
  86. static ssize_t amdgpu_set_dpm_state(struct device *dev,
  87. struct device_attribute *attr,
  88. const char *buf,
  89. size_t count)
  90. {
  91. struct drm_device *ddev = dev_get_drvdata(dev);
  92. struct amdgpu_device *adev = ddev->dev_private;
  93. enum amd_pm_state_type state;
  94. if (strncmp("battery", buf, strlen("battery")) == 0)
  95. state = POWER_STATE_TYPE_BATTERY;
  96. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  97. state = POWER_STATE_TYPE_BALANCED;
  98. else if (strncmp("performance", buf, strlen("performance")) == 0)
  99. state = POWER_STATE_TYPE_PERFORMANCE;
  100. else {
  101. count = -EINVAL;
  102. goto fail;
  103. }
  104. if (adev->pp_enabled) {
  105. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  106. } else {
  107. mutex_lock(&adev->pm.mutex);
  108. adev->pm.dpm.user_state = state;
  109. mutex_unlock(&adev->pm.mutex);
  110. /* Can't set dpm state when the card is off */
  111. if (!(adev->flags & AMD_IS_PX) ||
  112. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  113. amdgpu_pm_compute_clocks(adev);
  114. }
  115. fail:
  116. return count;
  117. }
  118. static ssize_t amdgpu_get_dpm_forced_performance_level(struct device *dev,
  119. struct device_attribute *attr,
  120. char *buf)
  121. {
  122. struct drm_device *ddev = dev_get_drvdata(dev);
  123. struct amdgpu_device *adev = ddev->dev_private;
  124. enum amd_dpm_forced_level level;
  125. if ((adev->flags & AMD_IS_PX) &&
  126. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  127. return snprintf(buf, PAGE_SIZE, "off\n");
  128. level = amdgpu_dpm_get_performance_level(adev);
  129. return snprintf(buf, PAGE_SIZE, "%s\n",
  130. (level & (AMD_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  131. (level & AMD_DPM_FORCED_LEVEL_LOW) ? "low" :
  132. (level & AMD_DPM_FORCED_LEVEL_HIGH) ? "high" :
  133. (level & AMD_DPM_FORCED_LEVEL_MANUAL) ? "manual" :
  134. (level & AMD_DPM_FORCED_LEVEL_PROFILING) ? "profiling" :
  135. "unknown"));
  136. }
  137. static ssize_t amdgpu_set_dpm_forced_performance_level(struct device *dev,
  138. struct device_attribute *attr,
  139. const char *buf,
  140. size_t count)
  141. {
  142. struct drm_device *ddev = dev_get_drvdata(dev);
  143. struct amdgpu_device *adev = ddev->dev_private;
  144. enum amd_dpm_forced_level level;
  145. enum amd_dpm_forced_level current_level;
  146. int ret = 0;
  147. /* Can't force performance level when the card is off */
  148. if ((adev->flags & AMD_IS_PX) &&
  149. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  150. return -EINVAL;
  151. current_level = amdgpu_dpm_get_performance_level(adev);
  152. if (strncmp("low", buf, strlen("low")) == 0) {
  153. level = AMD_DPM_FORCED_LEVEL_LOW;
  154. } else if (strncmp("high", buf, strlen("high")) == 0) {
  155. level = AMD_DPM_FORCED_LEVEL_HIGH;
  156. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  157. level = AMD_DPM_FORCED_LEVEL_AUTO;
  158. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  159. level = AMD_DPM_FORCED_LEVEL_MANUAL;
  160. } else if (strncmp("profile", buf, strlen("profile")) == 0) {
  161. level = AMD_DPM_FORCED_LEVEL_PROFILING;
  162. } else {
  163. count = -EINVAL;
  164. goto fail;
  165. }
  166. if (current_level == level)
  167. return count;
  168. if (level == AMD_DPM_FORCED_LEVEL_PROFILING)
  169. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_GFX,
  170. AMD_CG_STATE_UNGATE);
  171. else if (level != AMD_DPM_FORCED_LEVEL_PROFILING &&
  172. current_level == AMD_DPM_FORCED_LEVEL_PROFILING)
  173. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_GFX,
  174. AMD_CG_STATE_GATE);
  175. if (adev->pp_enabled)
  176. amdgpu_dpm_force_performance_level(adev, level);
  177. else {
  178. mutex_lock(&adev->pm.mutex);
  179. if (adev->pm.dpm.thermal_active) {
  180. count = -EINVAL;
  181. mutex_unlock(&adev->pm.mutex);
  182. goto fail;
  183. }
  184. ret = amdgpu_dpm_force_performance_level(adev, level);
  185. if (ret)
  186. count = -EINVAL;
  187. else
  188. adev->pm.dpm.forced_level = level;
  189. mutex_unlock(&adev->pm.mutex);
  190. }
  191. fail:
  192. return count;
  193. }
  194. static ssize_t amdgpu_get_pp_num_states(struct device *dev,
  195. struct device_attribute *attr,
  196. char *buf)
  197. {
  198. struct drm_device *ddev = dev_get_drvdata(dev);
  199. struct amdgpu_device *adev = ddev->dev_private;
  200. struct pp_states_info data;
  201. int i, buf_len;
  202. if (adev->pp_enabled)
  203. amdgpu_dpm_get_pp_num_states(adev, &data);
  204. buf_len = snprintf(buf, PAGE_SIZE, "states: %d\n", data.nums);
  205. for (i = 0; i < data.nums; i++)
  206. buf_len += snprintf(buf + buf_len, PAGE_SIZE, "%d %s\n", i,
  207. (data.states[i] == POWER_STATE_TYPE_INTERNAL_BOOT) ? "boot" :
  208. (data.states[i] == POWER_STATE_TYPE_BATTERY) ? "battery" :
  209. (data.states[i] == POWER_STATE_TYPE_BALANCED) ? "balanced" :
  210. (data.states[i] == POWER_STATE_TYPE_PERFORMANCE) ? "performance" : "default");
  211. return buf_len;
  212. }
  213. static ssize_t amdgpu_get_pp_cur_state(struct device *dev,
  214. struct device_attribute *attr,
  215. char *buf)
  216. {
  217. struct drm_device *ddev = dev_get_drvdata(dev);
  218. struct amdgpu_device *adev = ddev->dev_private;
  219. struct pp_states_info data;
  220. enum amd_pm_state_type pm = 0;
  221. int i = 0;
  222. if (adev->pp_enabled) {
  223. pm = amdgpu_dpm_get_current_power_state(adev);
  224. amdgpu_dpm_get_pp_num_states(adev, &data);
  225. for (i = 0; i < data.nums; i++) {
  226. if (pm == data.states[i])
  227. break;
  228. }
  229. if (i == data.nums)
  230. i = -EINVAL;
  231. }
  232. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  233. }
  234. static ssize_t amdgpu_get_pp_force_state(struct device *dev,
  235. struct device_attribute *attr,
  236. char *buf)
  237. {
  238. struct drm_device *ddev = dev_get_drvdata(dev);
  239. struct amdgpu_device *adev = ddev->dev_private;
  240. struct pp_states_info data;
  241. enum amd_pm_state_type pm = 0;
  242. int i;
  243. if (adev->pp_force_state_enabled && adev->pp_enabled) {
  244. pm = amdgpu_dpm_get_current_power_state(adev);
  245. amdgpu_dpm_get_pp_num_states(adev, &data);
  246. for (i = 0; i < data.nums; i++) {
  247. if (pm == data.states[i])
  248. break;
  249. }
  250. if (i == data.nums)
  251. i = -EINVAL;
  252. return snprintf(buf, PAGE_SIZE, "%d\n", i);
  253. } else
  254. return snprintf(buf, PAGE_SIZE, "\n");
  255. }
  256. static ssize_t amdgpu_set_pp_force_state(struct device *dev,
  257. struct device_attribute *attr,
  258. const char *buf,
  259. size_t count)
  260. {
  261. struct drm_device *ddev = dev_get_drvdata(dev);
  262. struct amdgpu_device *adev = ddev->dev_private;
  263. enum amd_pm_state_type state = 0;
  264. unsigned long idx;
  265. int ret;
  266. if (strlen(buf) == 1)
  267. adev->pp_force_state_enabled = false;
  268. else if (adev->pp_enabled) {
  269. struct pp_states_info data;
  270. ret = kstrtoul(buf, 0, &idx);
  271. if (ret || idx >= ARRAY_SIZE(data.states)) {
  272. count = -EINVAL;
  273. goto fail;
  274. }
  275. amdgpu_dpm_get_pp_num_states(adev, &data);
  276. state = data.states[idx];
  277. /* only set user selected power states */
  278. if (state != POWER_STATE_TYPE_INTERNAL_BOOT &&
  279. state != POWER_STATE_TYPE_DEFAULT) {
  280. amdgpu_dpm_dispatch_task(adev,
  281. AMD_PP_EVENT_ENABLE_USER_STATE, &state, NULL);
  282. adev->pp_force_state_enabled = true;
  283. }
  284. }
  285. fail:
  286. return count;
  287. }
  288. static ssize_t amdgpu_get_pp_table(struct device *dev,
  289. struct device_attribute *attr,
  290. char *buf)
  291. {
  292. struct drm_device *ddev = dev_get_drvdata(dev);
  293. struct amdgpu_device *adev = ddev->dev_private;
  294. char *table = NULL;
  295. int size;
  296. if (adev->pp_enabled)
  297. size = amdgpu_dpm_get_pp_table(adev, &table);
  298. else
  299. return 0;
  300. if (size >= PAGE_SIZE)
  301. size = PAGE_SIZE - 1;
  302. memcpy(buf, table, size);
  303. return size;
  304. }
  305. static ssize_t amdgpu_set_pp_table(struct device *dev,
  306. struct device_attribute *attr,
  307. const char *buf,
  308. size_t count)
  309. {
  310. struct drm_device *ddev = dev_get_drvdata(dev);
  311. struct amdgpu_device *adev = ddev->dev_private;
  312. if (adev->pp_enabled)
  313. amdgpu_dpm_set_pp_table(adev, buf, count);
  314. return count;
  315. }
  316. static ssize_t amdgpu_get_pp_dpm_sclk(struct device *dev,
  317. struct device_attribute *attr,
  318. char *buf)
  319. {
  320. struct drm_device *ddev = dev_get_drvdata(dev);
  321. struct amdgpu_device *adev = ddev->dev_private;
  322. ssize_t size = 0;
  323. if (adev->pp_enabled)
  324. size = amdgpu_dpm_print_clock_levels(adev, PP_SCLK, buf);
  325. else if (adev->pm.funcs->print_clock_levels)
  326. size = adev->pm.funcs->print_clock_levels(adev, PP_SCLK, buf);
  327. return size;
  328. }
  329. static ssize_t amdgpu_set_pp_dpm_sclk(struct device *dev,
  330. struct device_attribute *attr,
  331. const char *buf,
  332. size_t count)
  333. {
  334. struct drm_device *ddev = dev_get_drvdata(dev);
  335. struct amdgpu_device *adev = ddev->dev_private;
  336. int ret;
  337. long level;
  338. uint32_t i, mask = 0;
  339. char sub_str[2];
  340. for (i = 0; i < strlen(buf); i++) {
  341. if (*(buf + i) == '\n')
  342. continue;
  343. sub_str[0] = *(buf + i);
  344. sub_str[1] = '\0';
  345. ret = kstrtol(sub_str, 0, &level);
  346. if (ret) {
  347. count = -EINVAL;
  348. goto fail;
  349. }
  350. mask |= 1 << level;
  351. }
  352. if (adev->pp_enabled)
  353. amdgpu_dpm_force_clock_level(adev, PP_SCLK, mask);
  354. else if (adev->pm.funcs->force_clock_level)
  355. adev->pm.funcs->force_clock_level(adev, PP_SCLK, mask);
  356. fail:
  357. return count;
  358. }
  359. static ssize_t amdgpu_get_pp_dpm_mclk(struct device *dev,
  360. struct device_attribute *attr,
  361. char *buf)
  362. {
  363. struct drm_device *ddev = dev_get_drvdata(dev);
  364. struct amdgpu_device *adev = ddev->dev_private;
  365. ssize_t size = 0;
  366. if (adev->pp_enabled)
  367. size = amdgpu_dpm_print_clock_levels(adev, PP_MCLK, buf);
  368. else if (adev->pm.funcs->print_clock_levels)
  369. size = adev->pm.funcs->print_clock_levels(adev, PP_MCLK, buf);
  370. return size;
  371. }
  372. static ssize_t amdgpu_set_pp_dpm_mclk(struct device *dev,
  373. struct device_attribute *attr,
  374. const char *buf,
  375. size_t count)
  376. {
  377. struct drm_device *ddev = dev_get_drvdata(dev);
  378. struct amdgpu_device *adev = ddev->dev_private;
  379. int ret;
  380. long level;
  381. uint32_t i, mask = 0;
  382. char sub_str[2];
  383. for (i = 0; i < strlen(buf); i++) {
  384. if (*(buf + i) == '\n')
  385. continue;
  386. sub_str[0] = *(buf + i);
  387. sub_str[1] = '\0';
  388. ret = kstrtol(sub_str, 0, &level);
  389. if (ret) {
  390. count = -EINVAL;
  391. goto fail;
  392. }
  393. mask |= 1 << level;
  394. }
  395. if (adev->pp_enabled)
  396. amdgpu_dpm_force_clock_level(adev, PP_MCLK, mask);
  397. else if (adev->pm.funcs->force_clock_level)
  398. adev->pm.funcs->force_clock_level(adev, PP_MCLK, mask);
  399. fail:
  400. return count;
  401. }
  402. static ssize_t amdgpu_get_pp_dpm_pcie(struct device *dev,
  403. struct device_attribute *attr,
  404. char *buf)
  405. {
  406. struct drm_device *ddev = dev_get_drvdata(dev);
  407. struct amdgpu_device *adev = ddev->dev_private;
  408. ssize_t size = 0;
  409. if (adev->pp_enabled)
  410. size = amdgpu_dpm_print_clock_levels(adev, PP_PCIE, buf);
  411. else if (adev->pm.funcs->print_clock_levels)
  412. size = adev->pm.funcs->print_clock_levels(adev, PP_PCIE, buf);
  413. return size;
  414. }
  415. static ssize_t amdgpu_set_pp_dpm_pcie(struct device *dev,
  416. struct device_attribute *attr,
  417. const char *buf,
  418. size_t count)
  419. {
  420. struct drm_device *ddev = dev_get_drvdata(dev);
  421. struct amdgpu_device *adev = ddev->dev_private;
  422. int ret;
  423. long level;
  424. uint32_t i, mask = 0;
  425. char sub_str[2];
  426. for (i = 0; i < strlen(buf); i++) {
  427. if (*(buf + i) == '\n')
  428. continue;
  429. sub_str[0] = *(buf + i);
  430. sub_str[1] = '\0';
  431. ret = kstrtol(sub_str, 0, &level);
  432. if (ret) {
  433. count = -EINVAL;
  434. goto fail;
  435. }
  436. mask |= 1 << level;
  437. }
  438. if (adev->pp_enabled)
  439. amdgpu_dpm_force_clock_level(adev, PP_PCIE, mask);
  440. else if (adev->pm.funcs->force_clock_level)
  441. adev->pm.funcs->force_clock_level(adev, PP_PCIE, mask);
  442. fail:
  443. return count;
  444. }
  445. static ssize_t amdgpu_get_pp_sclk_od(struct device *dev,
  446. struct device_attribute *attr,
  447. char *buf)
  448. {
  449. struct drm_device *ddev = dev_get_drvdata(dev);
  450. struct amdgpu_device *adev = ddev->dev_private;
  451. uint32_t value = 0;
  452. if (adev->pp_enabled)
  453. value = amdgpu_dpm_get_sclk_od(adev);
  454. else if (adev->pm.funcs->get_sclk_od)
  455. value = adev->pm.funcs->get_sclk_od(adev);
  456. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  457. }
  458. static ssize_t amdgpu_set_pp_sclk_od(struct device *dev,
  459. struct device_attribute *attr,
  460. const char *buf,
  461. size_t count)
  462. {
  463. struct drm_device *ddev = dev_get_drvdata(dev);
  464. struct amdgpu_device *adev = ddev->dev_private;
  465. int ret;
  466. long int value;
  467. ret = kstrtol(buf, 0, &value);
  468. if (ret) {
  469. count = -EINVAL;
  470. goto fail;
  471. }
  472. if (adev->pp_enabled) {
  473. amdgpu_dpm_set_sclk_od(adev, (uint32_t)value);
  474. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  475. } else if (adev->pm.funcs->set_sclk_od) {
  476. adev->pm.funcs->set_sclk_od(adev, (uint32_t)value);
  477. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  478. amdgpu_pm_compute_clocks(adev);
  479. }
  480. fail:
  481. return count;
  482. }
  483. static ssize_t amdgpu_get_pp_mclk_od(struct device *dev,
  484. struct device_attribute *attr,
  485. char *buf)
  486. {
  487. struct drm_device *ddev = dev_get_drvdata(dev);
  488. struct amdgpu_device *adev = ddev->dev_private;
  489. uint32_t value = 0;
  490. if (adev->pp_enabled)
  491. value = amdgpu_dpm_get_mclk_od(adev);
  492. else if (adev->pm.funcs->get_mclk_od)
  493. value = adev->pm.funcs->get_mclk_od(adev);
  494. return snprintf(buf, PAGE_SIZE, "%d\n", value);
  495. }
  496. static ssize_t amdgpu_set_pp_mclk_od(struct device *dev,
  497. struct device_attribute *attr,
  498. const char *buf,
  499. size_t count)
  500. {
  501. struct drm_device *ddev = dev_get_drvdata(dev);
  502. struct amdgpu_device *adev = ddev->dev_private;
  503. int ret;
  504. long int value;
  505. ret = kstrtol(buf, 0, &value);
  506. if (ret) {
  507. count = -EINVAL;
  508. goto fail;
  509. }
  510. if (adev->pp_enabled) {
  511. amdgpu_dpm_set_mclk_od(adev, (uint32_t)value);
  512. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_READJUST_POWER_STATE, NULL, NULL);
  513. } else if (adev->pm.funcs->set_mclk_od) {
  514. adev->pm.funcs->set_mclk_od(adev, (uint32_t)value);
  515. adev->pm.dpm.current_ps = adev->pm.dpm.boot_ps;
  516. amdgpu_pm_compute_clocks(adev);
  517. }
  518. fail:
  519. return count;
  520. }
  521. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, amdgpu_get_dpm_state, amdgpu_set_dpm_state);
  522. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  523. amdgpu_get_dpm_forced_performance_level,
  524. amdgpu_set_dpm_forced_performance_level);
  525. static DEVICE_ATTR(pp_num_states, S_IRUGO, amdgpu_get_pp_num_states, NULL);
  526. static DEVICE_ATTR(pp_cur_state, S_IRUGO, amdgpu_get_pp_cur_state, NULL);
  527. static DEVICE_ATTR(pp_force_state, S_IRUGO | S_IWUSR,
  528. amdgpu_get_pp_force_state,
  529. amdgpu_set_pp_force_state);
  530. static DEVICE_ATTR(pp_table, S_IRUGO | S_IWUSR,
  531. amdgpu_get_pp_table,
  532. amdgpu_set_pp_table);
  533. static DEVICE_ATTR(pp_dpm_sclk, S_IRUGO | S_IWUSR,
  534. amdgpu_get_pp_dpm_sclk,
  535. amdgpu_set_pp_dpm_sclk);
  536. static DEVICE_ATTR(pp_dpm_mclk, S_IRUGO | S_IWUSR,
  537. amdgpu_get_pp_dpm_mclk,
  538. amdgpu_set_pp_dpm_mclk);
  539. static DEVICE_ATTR(pp_dpm_pcie, S_IRUGO | S_IWUSR,
  540. amdgpu_get_pp_dpm_pcie,
  541. amdgpu_set_pp_dpm_pcie);
  542. static DEVICE_ATTR(pp_sclk_od, S_IRUGO | S_IWUSR,
  543. amdgpu_get_pp_sclk_od,
  544. amdgpu_set_pp_sclk_od);
  545. static DEVICE_ATTR(pp_mclk_od, S_IRUGO | S_IWUSR,
  546. amdgpu_get_pp_mclk_od,
  547. amdgpu_set_pp_mclk_od);
  548. static ssize_t amdgpu_hwmon_show_temp(struct device *dev,
  549. struct device_attribute *attr,
  550. char *buf)
  551. {
  552. struct amdgpu_device *adev = dev_get_drvdata(dev);
  553. struct drm_device *ddev = adev->ddev;
  554. int temp;
  555. /* Can't get temperature when the card is off */
  556. if ((adev->flags & AMD_IS_PX) &&
  557. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  558. return -EINVAL;
  559. if (!adev->pp_enabled && !adev->pm.funcs->get_temperature)
  560. temp = 0;
  561. else
  562. temp = amdgpu_dpm_get_temperature(adev);
  563. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  564. }
  565. static ssize_t amdgpu_hwmon_show_temp_thresh(struct device *dev,
  566. struct device_attribute *attr,
  567. char *buf)
  568. {
  569. struct amdgpu_device *adev = dev_get_drvdata(dev);
  570. int hyst = to_sensor_dev_attr(attr)->index;
  571. int temp;
  572. if (hyst)
  573. temp = adev->pm.dpm.thermal.min_temp;
  574. else
  575. temp = adev->pm.dpm.thermal.max_temp;
  576. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  577. }
  578. static ssize_t amdgpu_hwmon_get_pwm1_enable(struct device *dev,
  579. struct device_attribute *attr,
  580. char *buf)
  581. {
  582. struct amdgpu_device *adev = dev_get_drvdata(dev);
  583. u32 pwm_mode = 0;
  584. if (!adev->pp_enabled && !adev->pm.funcs->get_fan_control_mode)
  585. return -EINVAL;
  586. pwm_mode = amdgpu_dpm_get_fan_control_mode(adev);
  587. /* never 0 (full-speed), fuse or smc-controlled always */
  588. return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
  589. }
  590. static ssize_t amdgpu_hwmon_set_pwm1_enable(struct device *dev,
  591. struct device_attribute *attr,
  592. const char *buf,
  593. size_t count)
  594. {
  595. struct amdgpu_device *adev = dev_get_drvdata(dev);
  596. int err;
  597. int value;
  598. if (!adev->pp_enabled && !adev->pm.funcs->set_fan_control_mode)
  599. return -EINVAL;
  600. err = kstrtoint(buf, 10, &value);
  601. if (err)
  602. return err;
  603. switch (value) {
  604. case 1: /* manual, percent-based */
  605. amdgpu_dpm_set_fan_control_mode(adev, FDO_PWM_MODE_STATIC);
  606. break;
  607. default: /* disable */
  608. amdgpu_dpm_set_fan_control_mode(adev, 0);
  609. break;
  610. }
  611. return count;
  612. }
  613. static ssize_t amdgpu_hwmon_get_pwm1_min(struct device *dev,
  614. struct device_attribute *attr,
  615. char *buf)
  616. {
  617. return sprintf(buf, "%i\n", 0);
  618. }
  619. static ssize_t amdgpu_hwmon_get_pwm1_max(struct device *dev,
  620. struct device_attribute *attr,
  621. char *buf)
  622. {
  623. return sprintf(buf, "%i\n", 255);
  624. }
  625. static ssize_t amdgpu_hwmon_set_pwm1(struct device *dev,
  626. struct device_attribute *attr,
  627. const char *buf, size_t count)
  628. {
  629. struct amdgpu_device *adev = dev_get_drvdata(dev);
  630. int err;
  631. u32 value;
  632. err = kstrtou32(buf, 10, &value);
  633. if (err)
  634. return err;
  635. value = (value * 100) / 255;
  636. err = amdgpu_dpm_set_fan_speed_percent(adev, value);
  637. if (err)
  638. return err;
  639. return count;
  640. }
  641. static ssize_t amdgpu_hwmon_get_pwm1(struct device *dev,
  642. struct device_attribute *attr,
  643. char *buf)
  644. {
  645. struct amdgpu_device *adev = dev_get_drvdata(dev);
  646. int err;
  647. u32 speed;
  648. err = amdgpu_dpm_get_fan_speed_percent(adev, &speed);
  649. if (err)
  650. return err;
  651. speed = (speed * 255) / 100;
  652. return sprintf(buf, "%i\n", speed);
  653. }
  654. static ssize_t amdgpu_hwmon_get_fan1_input(struct device *dev,
  655. struct device_attribute *attr,
  656. char *buf)
  657. {
  658. struct amdgpu_device *adev = dev_get_drvdata(dev);
  659. int err;
  660. u32 speed;
  661. err = amdgpu_dpm_get_fan_speed_rpm(adev, &speed);
  662. if (err)
  663. return err;
  664. return sprintf(buf, "%i\n", speed);
  665. }
  666. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, amdgpu_hwmon_show_temp, NULL, 0);
  667. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 0);
  668. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, amdgpu_hwmon_show_temp_thresh, NULL, 1);
  669. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1, amdgpu_hwmon_set_pwm1, 0);
  670. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, amdgpu_hwmon_get_pwm1_enable, amdgpu_hwmon_set_pwm1_enable, 0);
  671. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, amdgpu_hwmon_get_pwm1_min, NULL, 0);
  672. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, amdgpu_hwmon_get_pwm1_max, NULL, 0);
  673. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, amdgpu_hwmon_get_fan1_input, NULL, 0);
  674. static struct attribute *hwmon_attributes[] = {
  675. &sensor_dev_attr_temp1_input.dev_attr.attr,
  676. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  677. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  678. &sensor_dev_attr_pwm1.dev_attr.attr,
  679. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  680. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  681. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  682. &sensor_dev_attr_fan1_input.dev_attr.attr,
  683. NULL
  684. };
  685. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  686. struct attribute *attr, int index)
  687. {
  688. struct device *dev = kobj_to_dev(kobj);
  689. struct amdgpu_device *adev = dev_get_drvdata(dev);
  690. umode_t effective_mode = attr->mode;
  691. /* Skip limit attributes if DPM is not enabled */
  692. if (!adev->pm.dpm_enabled &&
  693. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  694. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
  695. attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  696. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  697. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  698. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  699. return 0;
  700. if (adev->pp_enabled)
  701. return effective_mode;
  702. /* Skip fan attributes if fan is not present */
  703. if (adev->pm.no_fan &&
  704. (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
  705. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
  706. attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  707. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  708. return 0;
  709. /* mask fan attributes if we have no bindings for this asic to expose */
  710. if ((!adev->pm.funcs->get_fan_speed_percent &&
  711. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
  712. (!adev->pm.funcs->get_fan_control_mode &&
  713. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
  714. effective_mode &= ~S_IRUGO;
  715. if ((!adev->pm.funcs->set_fan_speed_percent &&
  716. attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
  717. (!adev->pm.funcs->set_fan_control_mode &&
  718. attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
  719. effective_mode &= ~S_IWUSR;
  720. /* hide max/min values if we can't both query and manage the fan */
  721. if ((!adev->pm.funcs->set_fan_speed_percent &&
  722. !adev->pm.funcs->get_fan_speed_percent) &&
  723. (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
  724. attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
  725. return 0;
  726. /* requires powerplay */
  727. if (attr == &sensor_dev_attr_fan1_input.dev_attr.attr)
  728. return 0;
  729. return effective_mode;
  730. }
  731. static const struct attribute_group hwmon_attrgroup = {
  732. .attrs = hwmon_attributes,
  733. .is_visible = hwmon_attributes_visible,
  734. };
  735. static const struct attribute_group *hwmon_groups[] = {
  736. &hwmon_attrgroup,
  737. NULL
  738. };
  739. void amdgpu_dpm_thermal_work_handler(struct work_struct *work)
  740. {
  741. struct amdgpu_device *adev =
  742. container_of(work, struct amdgpu_device,
  743. pm.dpm.thermal.work);
  744. /* switch to the thermal state */
  745. enum amd_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  746. if (!adev->pm.dpm_enabled)
  747. return;
  748. if (adev->pm.funcs->get_temperature) {
  749. int temp = amdgpu_dpm_get_temperature(adev);
  750. if (temp < adev->pm.dpm.thermal.min_temp)
  751. /* switch back the user state */
  752. dpm_state = adev->pm.dpm.user_state;
  753. } else {
  754. if (adev->pm.dpm.thermal.high_to_low)
  755. /* switch back the user state */
  756. dpm_state = adev->pm.dpm.user_state;
  757. }
  758. mutex_lock(&adev->pm.mutex);
  759. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  760. adev->pm.dpm.thermal_active = true;
  761. else
  762. adev->pm.dpm.thermal_active = false;
  763. adev->pm.dpm.state = dpm_state;
  764. mutex_unlock(&adev->pm.mutex);
  765. amdgpu_pm_compute_clocks(adev);
  766. }
  767. static struct amdgpu_ps *amdgpu_dpm_pick_power_state(struct amdgpu_device *adev,
  768. enum amd_pm_state_type dpm_state)
  769. {
  770. int i;
  771. struct amdgpu_ps *ps;
  772. u32 ui_class;
  773. bool single_display = (adev->pm.dpm.new_active_crtc_count < 2) ?
  774. true : false;
  775. /* check if the vblank period is too short to adjust the mclk */
  776. if (single_display && adev->pm.funcs->vblank_too_short) {
  777. if (amdgpu_dpm_vblank_too_short(adev))
  778. single_display = false;
  779. }
  780. /* certain older asics have a separare 3D performance state,
  781. * so try that first if the user selected performance
  782. */
  783. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  784. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  785. /* balanced states don't exist at the moment */
  786. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  787. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  788. restart_search:
  789. /* Pick the best power state based on current conditions */
  790. for (i = 0; i < adev->pm.dpm.num_ps; i++) {
  791. ps = &adev->pm.dpm.ps[i];
  792. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  793. switch (dpm_state) {
  794. /* user states */
  795. case POWER_STATE_TYPE_BATTERY:
  796. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  797. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  798. if (single_display)
  799. return ps;
  800. } else
  801. return ps;
  802. }
  803. break;
  804. case POWER_STATE_TYPE_BALANCED:
  805. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  806. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  807. if (single_display)
  808. return ps;
  809. } else
  810. return ps;
  811. }
  812. break;
  813. case POWER_STATE_TYPE_PERFORMANCE:
  814. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  815. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  816. if (single_display)
  817. return ps;
  818. } else
  819. return ps;
  820. }
  821. break;
  822. /* internal states */
  823. case POWER_STATE_TYPE_INTERNAL_UVD:
  824. if (adev->pm.dpm.uvd_ps)
  825. return adev->pm.dpm.uvd_ps;
  826. else
  827. break;
  828. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  829. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  830. return ps;
  831. break;
  832. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  833. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  834. return ps;
  835. break;
  836. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  837. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  838. return ps;
  839. break;
  840. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  841. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  842. return ps;
  843. break;
  844. case POWER_STATE_TYPE_INTERNAL_BOOT:
  845. return adev->pm.dpm.boot_ps;
  846. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  847. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  848. return ps;
  849. break;
  850. case POWER_STATE_TYPE_INTERNAL_ACPI:
  851. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  852. return ps;
  853. break;
  854. case POWER_STATE_TYPE_INTERNAL_ULV:
  855. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  856. return ps;
  857. break;
  858. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  859. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  860. return ps;
  861. break;
  862. default:
  863. break;
  864. }
  865. }
  866. /* use a fallback state if we didn't match */
  867. switch (dpm_state) {
  868. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  869. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  870. goto restart_search;
  871. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  872. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  873. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  874. if (adev->pm.dpm.uvd_ps) {
  875. return adev->pm.dpm.uvd_ps;
  876. } else {
  877. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  878. goto restart_search;
  879. }
  880. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  881. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  882. goto restart_search;
  883. case POWER_STATE_TYPE_INTERNAL_ACPI:
  884. dpm_state = POWER_STATE_TYPE_BATTERY;
  885. goto restart_search;
  886. case POWER_STATE_TYPE_BATTERY:
  887. case POWER_STATE_TYPE_BALANCED:
  888. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  889. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  890. goto restart_search;
  891. default:
  892. break;
  893. }
  894. return NULL;
  895. }
  896. static void amdgpu_dpm_change_power_state_locked(struct amdgpu_device *adev)
  897. {
  898. struct amdgpu_ps *ps;
  899. enum amd_pm_state_type dpm_state;
  900. int ret;
  901. bool equal;
  902. /* if dpm init failed */
  903. if (!adev->pm.dpm_enabled)
  904. return;
  905. if (adev->pm.dpm.user_state != adev->pm.dpm.state) {
  906. /* add other state override checks here */
  907. if ((!adev->pm.dpm.thermal_active) &&
  908. (!adev->pm.dpm.uvd_active))
  909. adev->pm.dpm.state = adev->pm.dpm.user_state;
  910. }
  911. dpm_state = adev->pm.dpm.state;
  912. ps = amdgpu_dpm_pick_power_state(adev, dpm_state);
  913. if (ps)
  914. adev->pm.dpm.requested_ps = ps;
  915. else
  916. return;
  917. if (amdgpu_dpm == 1) {
  918. printk("switching from power state:\n");
  919. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.current_ps);
  920. printk("switching to power state:\n");
  921. amdgpu_dpm_print_power_state(adev, adev->pm.dpm.requested_ps);
  922. }
  923. /* update whether vce is active */
  924. ps->vce_active = adev->pm.dpm.vce_active;
  925. amdgpu_dpm_display_configuration_changed(adev);
  926. ret = amdgpu_dpm_pre_set_power_state(adev);
  927. if (ret)
  928. return;
  929. if ((0 != amgdpu_dpm_check_state_equal(adev, adev->pm.dpm.current_ps, adev->pm.dpm.requested_ps, &equal)))
  930. equal = false;
  931. if (equal)
  932. return;
  933. amdgpu_dpm_set_power_state(adev);
  934. amdgpu_dpm_post_set_power_state(adev);
  935. adev->pm.dpm.current_active_crtcs = adev->pm.dpm.new_active_crtcs;
  936. adev->pm.dpm.current_active_crtc_count = adev->pm.dpm.new_active_crtc_count;
  937. if (adev->pm.funcs->force_performance_level) {
  938. if (adev->pm.dpm.thermal_active) {
  939. enum amd_dpm_forced_level level = adev->pm.dpm.forced_level;
  940. /* force low perf level for thermal */
  941. amdgpu_dpm_force_performance_level(adev, AMD_DPM_FORCED_LEVEL_LOW);
  942. /* save the user's level */
  943. adev->pm.dpm.forced_level = level;
  944. } else {
  945. /* otherwise, user selected level */
  946. amdgpu_dpm_force_performance_level(adev, adev->pm.dpm.forced_level);
  947. }
  948. }
  949. }
  950. void amdgpu_dpm_enable_uvd(struct amdgpu_device *adev, bool enable)
  951. {
  952. if (adev->pp_enabled || adev->pm.funcs->powergate_uvd) {
  953. /* enable/disable UVD */
  954. mutex_lock(&adev->pm.mutex);
  955. amdgpu_dpm_powergate_uvd(adev, !enable);
  956. mutex_unlock(&adev->pm.mutex);
  957. } else {
  958. if (enable) {
  959. mutex_lock(&adev->pm.mutex);
  960. adev->pm.dpm.uvd_active = true;
  961. adev->pm.dpm.state = POWER_STATE_TYPE_INTERNAL_UVD;
  962. mutex_unlock(&adev->pm.mutex);
  963. } else {
  964. mutex_lock(&adev->pm.mutex);
  965. adev->pm.dpm.uvd_active = false;
  966. mutex_unlock(&adev->pm.mutex);
  967. }
  968. amdgpu_pm_compute_clocks(adev);
  969. }
  970. }
  971. void amdgpu_dpm_enable_vce(struct amdgpu_device *adev, bool enable)
  972. {
  973. if (adev->pp_enabled || adev->pm.funcs->powergate_vce) {
  974. /* enable/disable VCE */
  975. mutex_lock(&adev->pm.mutex);
  976. amdgpu_dpm_powergate_vce(adev, !enable);
  977. mutex_unlock(&adev->pm.mutex);
  978. } else {
  979. if (enable) {
  980. mutex_lock(&adev->pm.mutex);
  981. adev->pm.dpm.vce_active = true;
  982. /* XXX select vce level based on ring/task */
  983. adev->pm.dpm.vce_level = AMD_VCE_LEVEL_AC_ALL;
  984. mutex_unlock(&adev->pm.mutex);
  985. } else {
  986. mutex_lock(&adev->pm.mutex);
  987. adev->pm.dpm.vce_active = false;
  988. mutex_unlock(&adev->pm.mutex);
  989. }
  990. amdgpu_pm_compute_clocks(adev);
  991. }
  992. }
  993. void amdgpu_pm_print_power_states(struct amdgpu_device *adev)
  994. {
  995. int i;
  996. if (adev->pp_enabled)
  997. /* TO DO */
  998. return;
  999. for (i = 0; i < adev->pm.dpm.num_ps; i++)
  1000. amdgpu_dpm_print_power_state(adev, &adev->pm.dpm.ps[i]);
  1001. }
  1002. int amdgpu_pm_sysfs_init(struct amdgpu_device *adev)
  1003. {
  1004. int ret;
  1005. if (adev->pm.sysfs_initialized)
  1006. return 0;
  1007. if (!adev->pp_enabled) {
  1008. if (adev->pm.funcs->get_temperature == NULL)
  1009. return 0;
  1010. }
  1011. adev->pm.int_hwmon_dev = hwmon_device_register_with_groups(adev->dev,
  1012. DRIVER_NAME, adev,
  1013. hwmon_groups);
  1014. if (IS_ERR(adev->pm.int_hwmon_dev)) {
  1015. ret = PTR_ERR(adev->pm.int_hwmon_dev);
  1016. dev_err(adev->dev,
  1017. "Unable to register hwmon device: %d\n", ret);
  1018. return ret;
  1019. }
  1020. ret = device_create_file(adev->dev, &dev_attr_power_dpm_state);
  1021. if (ret) {
  1022. DRM_ERROR("failed to create device file for dpm state\n");
  1023. return ret;
  1024. }
  1025. ret = device_create_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1026. if (ret) {
  1027. DRM_ERROR("failed to create device file for dpm state\n");
  1028. return ret;
  1029. }
  1030. if (adev->pp_enabled) {
  1031. ret = device_create_file(adev->dev, &dev_attr_pp_num_states);
  1032. if (ret) {
  1033. DRM_ERROR("failed to create device file pp_num_states\n");
  1034. return ret;
  1035. }
  1036. ret = device_create_file(adev->dev, &dev_attr_pp_cur_state);
  1037. if (ret) {
  1038. DRM_ERROR("failed to create device file pp_cur_state\n");
  1039. return ret;
  1040. }
  1041. ret = device_create_file(adev->dev, &dev_attr_pp_force_state);
  1042. if (ret) {
  1043. DRM_ERROR("failed to create device file pp_force_state\n");
  1044. return ret;
  1045. }
  1046. ret = device_create_file(adev->dev, &dev_attr_pp_table);
  1047. if (ret) {
  1048. DRM_ERROR("failed to create device file pp_table\n");
  1049. return ret;
  1050. }
  1051. }
  1052. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1053. if (ret) {
  1054. DRM_ERROR("failed to create device file pp_dpm_sclk\n");
  1055. return ret;
  1056. }
  1057. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1058. if (ret) {
  1059. DRM_ERROR("failed to create device file pp_dpm_mclk\n");
  1060. return ret;
  1061. }
  1062. ret = device_create_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1063. if (ret) {
  1064. DRM_ERROR("failed to create device file pp_dpm_pcie\n");
  1065. return ret;
  1066. }
  1067. ret = device_create_file(adev->dev, &dev_attr_pp_sclk_od);
  1068. if (ret) {
  1069. DRM_ERROR("failed to create device file pp_sclk_od\n");
  1070. return ret;
  1071. }
  1072. ret = device_create_file(adev->dev, &dev_attr_pp_mclk_od);
  1073. if (ret) {
  1074. DRM_ERROR("failed to create device file pp_mclk_od\n");
  1075. return ret;
  1076. }
  1077. ret = amdgpu_debugfs_pm_init(adev);
  1078. if (ret) {
  1079. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1080. return ret;
  1081. }
  1082. adev->pm.sysfs_initialized = true;
  1083. return 0;
  1084. }
  1085. void amdgpu_pm_sysfs_fini(struct amdgpu_device *adev)
  1086. {
  1087. if (adev->pm.int_hwmon_dev)
  1088. hwmon_device_unregister(adev->pm.int_hwmon_dev);
  1089. device_remove_file(adev->dev, &dev_attr_power_dpm_state);
  1090. device_remove_file(adev->dev, &dev_attr_power_dpm_force_performance_level);
  1091. if (adev->pp_enabled) {
  1092. device_remove_file(adev->dev, &dev_attr_pp_num_states);
  1093. device_remove_file(adev->dev, &dev_attr_pp_cur_state);
  1094. device_remove_file(adev->dev, &dev_attr_pp_force_state);
  1095. device_remove_file(adev->dev, &dev_attr_pp_table);
  1096. }
  1097. device_remove_file(adev->dev, &dev_attr_pp_dpm_sclk);
  1098. device_remove_file(adev->dev, &dev_attr_pp_dpm_mclk);
  1099. device_remove_file(adev->dev, &dev_attr_pp_dpm_pcie);
  1100. device_remove_file(adev->dev, &dev_attr_pp_sclk_od);
  1101. device_remove_file(adev->dev, &dev_attr_pp_mclk_od);
  1102. }
  1103. void amdgpu_pm_compute_clocks(struct amdgpu_device *adev)
  1104. {
  1105. struct drm_device *ddev = adev->ddev;
  1106. struct drm_crtc *crtc;
  1107. struct amdgpu_crtc *amdgpu_crtc;
  1108. int i = 0;
  1109. if (!adev->pm.dpm_enabled)
  1110. return;
  1111. amdgpu_display_bandwidth_update(adev);
  1112. for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
  1113. struct amdgpu_ring *ring = adev->rings[i];
  1114. if (ring && ring->ready)
  1115. amdgpu_fence_wait_empty(ring);
  1116. }
  1117. if (adev->pp_enabled) {
  1118. amdgpu_dpm_dispatch_task(adev, AMD_PP_EVENT_DISPLAY_CONFIG_CHANGE, NULL, NULL);
  1119. } else {
  1120. mutex_lock(&adev->pm.mutex);
  1121. adev->pm.dpm.new_active_crtcs = 0;
  1122. adev->pm.dpm.new_active_crtc_count = 0;
  1123. if (adev->mode_info.num_crtc && adev->mode_info.mode_config_initialized) {
  1124. list_for_each_entry(crtc,
  1125. &ddev->mode_config.crtc_list, head) {
  1126. amdgpu_crtc = to_amdgpu_crtc(crtc);
  1127. if (crtc->enabled) {
  1128. adev->pm.dpm.new_active_crtcs |= (1 << amdgpu_crtc->crtc_id);
  1129. adev->pm.dpm.new_active_crtc_count++;
  1130. }
  1131. }
  1132. }
  1133. /* update battery/ac status */
  1134. if (power_supply_is_system_supplied() > 0)
  1135. adev->pm.dpm.ac_power = true;
  1136. else
  1137. adev->pm.dpm.ac_power = false;
  1138. amdgpu_dpm_change_power_state_locked(adev);
  1139. mutex_unlock(&adev->pm.mutex);
  1140. }
  1141. }
  1142. /*
  1143. * Debugfs info
  1144. */
  1145. #if defined(CONFIG_DEBUG_FS)
  1146. static int amdgpu_debugfs_pm_info_pp(struct seq_file *m, struct amdgpu_device *adev)
  1147. {
  1148. int32_t value;
  1149. /* sanity check PP is enabled */
  1150. if (!(adev->powerplay.pp_funcs &&
  1151. adev->powerplay.pp_funcs->read_sensor))
  1152. return -EINVAL;
  1153. /* GPU Clocks */
  1154. seq_printf(m, "GFX Clocks and Power:\n");
  1155. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_MCLK, &value))
  1156. seq_printf(m, "\t%u MHz (MCLK)\n", value/100);
  1157. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GFX_SCLK, &value))
  1158. seq_printf(m, "\t%u MHz (SCLK)\n", value/100);
  1159. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDGFX, &value))
  1160. seq_printf(m, "\t%u mV (VDDGFX)\n", value);
  1161. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VDDNB, &value))
  1162. seq_printf(m, "\t%u mV (VDDNB)\n", value);
  1163. seq_printf(m, "\n");
  1164. /* GPU Temp */
  1165. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_TEMP, &value))
  1166. seq_printf(m, "GPU Temperature: %u C\n", value/1000);
  1167. /* GPU Load */
  1168. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_GPU_LOAD, &value))
  1169. seq_printf(m, "GPU Load: %u %%\n", value);
  1170. seq_printf(m, "\n");
  1171. /* UVD clocks */
  1172. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_POWER, &value)) {
  1173. if (!value) {
  1174. seq_printf(m, "UVD: Disabled\n");
  1175. } else {
  1176. seq_printf(m, "UVD: Enabled\n");
  1177. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_DCLK, &value))
  1178. seq_printf(m, "\t%u MHz (DCLK)\n", value/100);
  1179. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_UVD_VCLK, &value))
  1180. seq_printf(m, "\t%u MHz (VCLK)\n", value/100);
  1181. }
  1182. }
  1183. seq_printf(m, "\n");
  1184. /* VCE clocks */
  1185. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_POWER, &value)) {
  1186. if (!value) {
  1187. seq_printf(m, "VCE: Disabled\n");
  1188. } else {
  1189. seq_printf(m, "VCE: Enabled\n");
  1190. if (!amdgpu_dpm_read_sensor(adev, AMDGPU_PP_SENSOR_VCE_ECCLK, &value))
  1191. seq_printf(m, "\t%u MHz (ECCLK)\n", value/100);
  1192. }
  1193. }
  1194. return 0;
  1195. }
  1196. static void amdgpu_parse_cg_state(struct seq_file *m, u32 flags)
  1197. {
  1198. int i;
  1199. for (i = 0; clocks[i].flag; i++)
  1200. seq_printf(m, "\t%s: %s\n", clocks[i].name,
  1201. (flags & clocks[i].flag) ? "On" : "Off");
  1202. }
  1203. static int amdgpu_debugfs_pm_info(struct seq_file *m, void *data)
  1204. {
  1205. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1206. struct drm_device *dev = node->minor->dev;
  1207. struct amdgpu_device *adev = dev->dev_private;
  1208. struct drm_device *ddev = adev->ddev;
  1209. u32 flags = 0;
  1210. amdgpu_get_clockgating_state(adev, &flags);
  1211. seq_printf(m, "Clock Gating Flags Mask: 0x%x\n", flags);
  1212. amdgpu_parse_cg_state(m, flags);
  1213. seq_printf(m, "\n");
  1214. if (!adev->pm.dpm_enabled) {
  1215. seq_printf(m, "dpm not enabled\n");
  1216. return 0;
  1217. }
  1218. if ((adev->flags & AMD_IS_PX) &&
  1219. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1220. seq_printf(m, "PX asic powered off\n");
  1221. } else if (adev->pp_enabled) {
  1222. return amdgpu_debugfs_pm_info_pp(m, adev);
  1223. } else {
  1224. mutex_lock(&adev->pm.mutex);
  1225. if (adev->pm.funcs->debugfs_print_current_performance_level)
  1226. adev->pm.funcs->debugfs_print_current_performance_level(adev, m);
  1227. else
  1228. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1229. mutex_unlock(&adev->pm.mutex);
  1230. }
  1231. return 0;
  1232. }
  1233. static const struct drm_info_list amdgpu_pm_info_list[] = {
  1234. {"amdgpu_pm_info", amdgpu_debugfs_pm_info, 0, NULL},
  1235. };
  1236. #endif
  1237. static int amdgpu_debugfs_pm_init(struct amdgpu_device *adev)
  1238. {
  1239. #if defined(CONFIG_DEBUG_FS)
  1240. return amdgpu_debugfs_add_files(adev, amdgpu_pm_info_list, ARRAY_SIZE(amdgpu_pm_info_list));
  1241. #else
  1242. return 0;
  1243. #endif
  1244. }