amdgpu_pm.c 37 KB

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