amdgpu_pm.c 37 KB

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