amdgpu_pm.c 38 KB

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