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