amdgpu_pm.c 37 KB

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