drm/exynos: Stop using drm_framebuffer_unregister_private
[linux/fpc-iii.git] / drivers / gpu / drm / radeon / radeon_pm.c
blob326ad068c15aa63ef38a6c85fbcc368ad52bd801
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:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
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.
20 * Authors: Rafał Miłecki <zajec5@gmail.com>
21 * Alex Deucher <alexdeucher@gmail.com>
23 #include <drm/drmP.h>
24 #include "radeon.h"
25 #include "avivod.h"
26 #include "atom.h"
27 #include "r600_dpm.h"
28 #include <linux/power_supply.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
32 #define RADEON_IDLE_LOOP_MS 100
33 #define RADEON_RECLOCK_DELAY_MS 200
34 #define RADEON_WAIT_VBLANK_TIMEOUT 200
36 static const char *radeon_pm_state_type_name[5] = {
37 "",
38 "Powersave",
39 "Battery",
40 "Balanced",
41 "Performance",
44 static void radeon_dynpm_idle_work_handler(struct work_struct *work);
45 static int radeon_debugfs_pm_init(struct radeon_device *rdev);
46 static bool radeon_pm_in_vbl(struct radeon_device *rdev);
47 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
48 static void radeon_pm_update_profile(struct radeon_device *rdev);
49 static void radeon_pm_set_clocks(struct radeon_device *rdev);
50 static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev);
52 int radeon_pm_get_type_index(struct radeon_device *rdev,
53 enum radeon_pm_state_type ps_type,
54 int instance)
56 int i;
57 int found_instance = -1;
59 for (i = 0; i < rdev->pm.num_power_states; i++) {
60 if (rdev->pm.power_state[i].type == ps_type) {
61 found_instance++;
62 if (found_instance == instance)
63 return i;
66 /* return default if no match */
67 return rdev->pm.default_power_state_index;
70 void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
72 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
73 mutex_lock(&rdev->pm.mutex);
74 if (power_supply_is_system_supplied() > 0)
75 rdev->pm.dpm.ac_power = true;
76 else
77 rdev->pm.dpm.ac_power = false;
78 if (rdev->family == CHIP_ARUBA) {
79 if (rdev->asic->dpm.enable_bapm)
80 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
82 mutex_unlock(&rdev->pm.mutex);
83 /* allow new DPM state to be picked */
84 radeon_pm_compute_clocks_dpm(rdev);
85 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
86 if (rdev->pm.profile == PM_PROFILE_AUTO) {
87 mutex_lock(&rdev->pm.mutex);
88 radeon_pm_update_profile(rdev);
89 radeon_pm_set_clocks(rdev);
90 mutex_unlock(&rdev->pm.mutex);
95 static void radeon_pm_update_profile(struct radeon_device *rdev)
97 switch (rdev->pm.profile) {
98 case PM_PROFILE_DEFAULT:
99 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
100 break;
101 case PM_PROFILE_AUTO:
102 if (power_supply_is_system_supplied() > 0) {
103 if (rdev->pm.active_crtc_count > 1)
104 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
105 else
106 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
107 } else {
108 if (rdev->pm.active_crtc_count > 1)
109 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
110 else
111 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
113 break;
114 case PM_PROFILE_LOW:
115 if (rdev->pm.active_crtc_count > 1)
116 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
117 else
118 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
119 break;
120 case PM_PROFILE_MID:
121 if (rdev->pm.active_crtc_count > 1)
122 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
123 else
124 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
125 break;
126 case PM_PROFILE_HIGH:
127 if (rdev->pm.active_crtc_count > 1)
128 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
129 else
130 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
131 break;
134 if (rdev->pm.active_crtc_count == 0) {
135 rdev->pm.requested_power_state_index =
136 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
137 rdev->pm.requested_clock_mode_index =
138 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
139 } else {
140 rdev->pm.requested_power_state_index =
141 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
142 rdev->pm.requested_clock_mode_index =
143 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
147 static void radeon_unmap_vram_bos(struct radeon_device *rdev)
149 struct radeon_bo *bo, *n;
151 if (list_empty(&rdev->gem.objects))
152 return;
154 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
155 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
156 ttm_bo_unmap_virtual(&bo->tbo);
160 static void radeon_sync_with_vblank(struct radeon_device *rdev)
162 if (rdev->pm.active_crtcs) {
163 rdev->pm.vblank_sync = false;
164 wait_event_timeout(
165 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
166 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
170 static void radeon_set_power_state(struct radeon_device *rdev)
172 u32 sclk, mclk;
173 bool misc_after = false;
175 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
176 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
177 return;
179 if (radeon_gui_idle(rdev)) {
180 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
181 clock_info[rdev->pm.requested_clock_mode_index].sclk;
182 if (sclk > rdev->pm.default_sclk)
183 sclk = rdev->pm.default_sclk;
185 /* starting with BTC, there is one state that is used for both
186 * MH and SH. Difference is that we always use the high clock index for
187 * mclk and vddci.
189 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
190 (rdev->family >= CHIP_BARTS) &&
191 rdev->pm.active_crtc_count &&
192 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
193 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
194 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
195 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
196 else
197 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
198 clock_info[rdev->pm.requested_clock_mode_index].mclk;
200 if (mclk > rdev->pm.default_mclk)
201 mclk = rdev->pm.default_mclk;
203 /* upvolt before raising clocks, downvolt after lowering clocks */
204 if (sclk < rdev->pm.current_sclk)
205 misc_after = true;
207 radeon_sync_with_vblank(rdev);
209 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
210 if (!radeon_pm_in_vbl(rdev))
211 return;
214 radeon_pm_prepare(rdev);
216 if (!misc_after)
217 /* voltage, pcie lanes, etc.*/
218 radeon_pm_misc(rdev);
220 /* set engine clock */
221 if (sclk != rdev->pm.current_sclk) {
222 radeon_pm_debug_check_in_vbl(rdev, false);
223 radeon_set_engine_clock(rdev, sclk);
224 radeon_pm_debug_check_in_vbl(rdev, true);
225 rdev->pm.current_sclk = sclk;
226 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
229 /* set memory clock */
230 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
231 radeon_pm_debug_check_in_vbl(rdev, false);
232 radeon_set_memory_clock(rdev, mclk);
233 radeon_pm_debug_check_in_vbl(rdev, true);
234 rdev->pm.current_mclk = mclk;
235 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
238 if (misc_after)
239 /* voltage, pcie lanes, etc.*/
240 radeon_pm_misc(rdev);
242 radeon_pm_finish(rdev);
244 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
245 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
246 } else
247 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
250 static void radeon_pm_set_clocks(struct radeon_device *rdev)
252 struct drm_crtc *crtc;
253 int i, r;
255 /* no need to take locks, etc. if nothing's going to change */
256 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
257 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
258 return;
260 down_write(&rdev->pm.mclk_lock);
261 mutex_lock(&rdev->ring_lock);
263 /* wait for the rings to drain */
264 for (i = 0; i < RADEON_NUM_RINGS; i++) {
265 struct radeon_ring *ring = &rdev->ring[i];
266 if (!ring->ready) {
267 continue;
269 r = radeon_fence_wait_empty(rdev, i);
270 if (r) {
271 /* needs a GPU reset dont reset here */
272 mutex_unlock(&rdev->ring_lock);
273 up_write(&rdev->pm.mclk_lock);
274 return;
278 radeon_unmap_vram_bos(rdev);
280 if (rdev->irq.installed) {
281 i = 0;
282 drm_for_each_crtc(crtc, rdev->ddev) {
283 if (rdev->pm.active_crtcs & (1 << i)) {
284 /* This can fail if a modeset is in progress */
285 if (drm_crtc_vblank_get(crtc) == 0)
286 rdev->pm.req_vblank |= (1 << i);
287 else
288 DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
291 i++;
295 radeon_set_power_state(rdev);
297 if (rdev->irq.installed) {
298 i = 0;
299 drm_for_each_crtc(crtc, rdev->ddev) {
300 if (rdev->pm.req_vblank & (1 << i)) {
301 rdev->pm.req_vblank &= ~(1 << i);
302 drm_crtc_vblank_put(crtc);
304 i++;
308 /* update display watermarks based on new power state */
309 radeon_update_bandwidth_info(rdev);
310 if (rdev->pm.active_crtc_count)
311 radeon_bandwidth_update(rdev);
313 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
315 mutex_unlock(&rdev->ring_lock);
316 up_write(&rdev->pm.mclk_lock);
319 static void radeon_pm_print_states(struct radeon_device *rdev)
321 int i, j;
322 struct radeon_power_state *power_state;
323 struct radeon_pm_clock_info *clock_info;
325 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
326 for (i = 0; i < rdev->pm.num_power_states; i++) {
327 power_state = &rdev->pm.power_state[i];
328 DRM_DEBUG_DRIVER("State %d: %s\n", i,
329 radeon_pm_state_type_name[power_state->type]);
330 if (i == rdev->pm.default_power_state_index)
331 DRM_DEBUG_DRIVER("\tDefault");
332 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
333 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
334 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
335 DRM_DEBUG_DRIVER("\tSingle display only\n");
336 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
337 for (j = 0; j < power_state->num_clock_modes; j++) {
338 clock_info = &(power_state->clock_info[j]);
339 if (rdev->flags & RADEON_IS_IGP)
340 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
342 clock_info->sclk * 10);
343 else
344 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
346 clock_info->sclk * 10,
347 clock_info->mclk * 10,
348 clock_info->voltage.voltage);
353 static ssize_t radeon_get_pm_profile(struct device *dev,
354 struct device_attribute *attr,
355 char *buf)
357 struct drm_device *ddev = dev_get_drvdata(dev);
358 struct radeon_device *rdev = ddev->dev_private;
359 int cp = rdev->pm.profile;
361 return snprintf(buf, PAGE_SIZE, "%s\n",
362 (cp == PM_PROFILE_AUTO) ? "auto" :
363 (cp == PM_PROFILE_LOW) ? "low" :
364 (cp == PM_PROFILE_MID) ? "mid" :
365 (cp == PM_PROFILE_HIGH) ? "high" : "default");
368 static ssize_t radeon_set_pm_profile(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf,
371 size_t count)
373 struct drm_device *ddev = dev_get_drvdata(dev);
374 struct radeon_device *rdev = ddev->dev_private;
376 /* Can't set profile when the card is off */
377 if ((rdev->flags & RADEON_IS_PX) &&
378 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
379 return -EINVAL;
381 mutex_lock(&rdev->pm.mutex);
382 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
383 if (strncmp("default", buf, strlen("default")) == 0)
384 rdev->pm.profile = PM_PROFILE_DEFAULT;
385 else if (strncmp("auto", buf, strlen("auto")) == 0)
386 rdev->pm.profile = PM_PROFILE_AUTO;
387 else if (strncmp("low", buf, strlen("low")) == 0)
388 rdev->pm.profile = PM_PROFILE_LOW;
389 else if (strncmp("mid", buf, strlen("mid")) == 0)
390 rdev->pm.profile = PM_PROFILE_MID;
391 else if (strncmp("high", buf, strlen("high")) == 0)
392 rdev->pm.profile = PM_PROFILE_HIGH;
393 else {
394 count = -EINVAL;
395 goto fail;
397 radeon_pm_update_profile(rdev);
398 radeon_pm_set_clocks(rdev);
399 } else
400 count = -EINVAL;
402 fail:
403 mutex_unlock(&rdev->pm.mutex);
405 return count;
408 static ssize_t radeon_get_pm_method(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
412 struct drm_device *ddev = dev_get_drvdata(dev);
413 struct radeon_device *rdev = ddev->dev_private;
414 int pm = rdev->pm.pm_method;
416 return snprintf(buf, PAGE_SIZE, "%s\n",
417 (pm == PM_METHOD_DYNPM) ? "dynpm" :
418 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
421 static ssize_t radeon_set_pm_method(struct device *dev,
422 struct device_attribute *attr,
423 const char *buf,
424 size_t count)
426 struct drm_device *ddev = dev_get_drvdata(dev);
427 struct radeon_device *rdev = ddev->dev_private;
429 /* Can't set method when the card is off */
430 if ((rdev->flags & RADEON_IS_PX) &&
431 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
432 count = -EINVAL;
433 goto fail;
436 /* we don't support the legacy modes with dpm */
437 if (rdev->pm.pm_method == PM_METHOD_DPM) {
438 count = -EINVAL;
439 goto fail;
442 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
443 mutex_lock(&rdev->pm.mutex);
444 rdev->pm.pm_method = PM_METHOD_DYNPM;
445 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
446 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
447 mutex_unlock(&rdev->pm.mutex);
448 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
449 mutex_lock(&rdev->pm.mutex);
450 /* disable dynpm */
451 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
452 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
453 rdev->pm.pm_method = PM_METHOD_PROFILE;
454 mutex_unlock(&rdev->pm.mutex);
455 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
456 } else {
457 count = -EINVAL;
458 goto fail;
460 radeon_pm_compute_clocks(rdev);
461 fail:
462 return count;
465 static ssize_t radeon_get_dpm_state(struct device *dev,
466 struct device_attribute *attr,
467 char *buf)
469 struct drm_device *ddev = dev_get_drvdata(dev);
470 struct radeon_device *rdev = ddev->dev_private;
471 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
473 return snprintf(buf, PAGE_SIZE, "%s\n",
474 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
475 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
478 static ssize_t radeon_set_dpm_state(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf,
481 size_t count)
483 struct drm_device *ddev = dev_get_drvdata(dev);
484 struct radeon_device *rdev = ddev->dev_private;
486 mutex_lock(&rdev->pm.mutex);
487 if (strncmp("battery", buf, strlen("battery")) == 0)
488 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
489 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
490 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
491 else if (strncmp("performance", buf, strlen("performance")) == 0)
492 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
493 else {
494 mutex_unlock(&rdev->pm.mutex);
495 count = -EINVAL;
496 goto fail;
498 mutex_unlock(&rdev->pm.mutex);
500 /* Can't set dpm state when the card is off */
501 if (!(rdev->flags & RADEON_IS_PX) ||
502 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
503 radeon_pm_compute_clocks(rdev);
505 fail:
506 return count;
509 static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
513 struct drm_device *ddev = dev_get_drvdata(dev);
514 struct radeon_device *rdev = ddev->dev_private;
515 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
517 if ((rdev->flags & RADEON_IS_PX) &&
518 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
519 return snprintf(buf, PAGE_SIZE, "off\n");
521 return snprintf(buf, PAGE_SIZE, "%s\n",
522 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
523 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
526 static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf,
529 size_t count)
531 struct drm_device *ddev = dev_get_drvdata(dev);
532 struct radeon_device *rdev = ddev->dev_private;
533 enum radeon_dpm_forced_level level;
534 int ret = 0;
536 /* Can't force performance level when the card is off */
537 if ((rdev->flags & RADEON_IS_PX) &&
538 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
539 return -EINVAL;
541 mutex_lock(&rdev->pm.mutex);
542 if (strncmp("low", buf, strlen("low")) == 0) {
543 level = RADEON_DPM_FORCED_LEVEL_LOW;
544 } else if (strncmp("high", buf, strlen("high")) == 0) {
545 level = RADEON_DPM_FORCED_LEVEL_HIGH;
546 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
547 level = RADEON_DPM_FORCED_LEVEL_AUTO;
548 } else {
549 count = -EINVAL;
550 goto fail;
552 if (rdev->asic->dpm.force_performance_level) {
553 if (rdev->pm.dpm.thermal_active) {
554 count = -EINVAL;
555 goto fail;
557 ret = radeon_dpm_force_performance_level(rdev, level);
558 if (ret)
559 count = -EINVAL;
561 fail:
562 mutex_unlock(&rdev->pm.mutex);
564 return count;
567 static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
568 struct device_attribute *attr,
569 char *buf)
571 struct radeon_device *rdev = dev_get_drvdata(dev);
572 u32 pwm_mode = 0;
574 if (rdev->asic->dpm.fan_ctrl_get_mode)
575 pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
577 /* never 0 (full-speed), fuse or smc-controlled always */
578 return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
581 static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
582 struct device_attribute *attr,
583 const char *buf,
584 size_t count)
586 struct radeon_device *rdev = dev_get_drvdata(dev);
587 int err;
588 int value;
590 if(!rdev->asic->dpm.fan_ctrl_set_mode)
591 return -EINVAL;
593 err = kstrtoint(buf, 10, &value);
594 if (err)
595 return err;
597 switch (value) {
598 case 1: /* manual, percent-based */
599 rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
600 break;
601 default: /* disable */
602 rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
603 break;
606 return count;
609 static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
610 struct device_attribute *attr,
611 char *buf)
613 return sprintf(buf, "%i\n", 0);
616 static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
617 struct device_attribute *attr,
618 char *buf)
620 return sprintf(buf, "%i\n", 255);
623 static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
624 struct device_attribute *attr,
625 const char *buf, size_t count)
627 struct radeon_device *rdev = dev_get_drvdata(dev);
628 int err;
629 u32 value;
631 err = kstrtou32(buf, 10, &value);
632 if (err)
633 return err;
635 value = (value * 100) / 255;
637 err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
638 if (err)
639 return err;
641 return count;
644 static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
645 struct device_attribute *attr,
646 char *buf)
648 struct radeon_device *rdev = dev_get_drvdata(dev);
649 int err;
650 u32 speed;
652 err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
653 if (err)
654 return err;
656 speed = (speed * 255) / 100;
658 return sprintf(buf, "%i\n", speed);
661 static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
662 static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
663 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
664 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
665 radeon_get_dpm_forced_performance_level,
666 radeon_set_dpm_forced_performance_level);
668 static ssize_t radeon_hwmon_show_temp(struct device *dev,
669 struct device_attribute *attr,
670 char *buf)
672 struct radeon_device *rdev = dev_get_drvdata(dev);
673 struct drm_device *ddev = rdev->ddev;
674 int temp;
676 /* Can't get temperature when the card is off */
677 if ((rdev->flags & RADEON_IS_PX) &&
678 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
679 return -EINVAL;
681 if (rdev->asic->pm.get_temperature)
682 temp = radeon_get_temperature(rdev);
683 else
684 temp = 0;
686 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
689 static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
690 struct device_attribute *attr,
691 char *buf)
693 struct radeon_device *rdev = dev_get_drvdata(dev);
694 int hyst = to_sensor_dev_attr(attr)->index;
695 int temp;
697 if (hyst)
698 temp = rdev->pm.dpm.thermal.min_temp;
699 else
700 temp = rdev->pm.dpm.thermal.max_temp;
702 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
705 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
706 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
707 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
708 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
709 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
710 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
711 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
714 static struct attribute *hwmon_attributes[] = {
715 &sensor_dev_attr_temp1_input.dev_attr.attr,
716 &sensor_dev_attr_temp1_crit.dev_attr.attr,
717 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
718 &sensor_dev_attr_pwm1.dev_attr.attr,
719 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
720 &sensor_dev_attr_pwm1_min.dev_attr.attr,
721 &sensor_dev_attr_pwm1_max.dev_attr.attr,
722 NULL
725 static umode_t hwmon_attributes_visible(struct kobject *kobj,
726 struct attribute *attr, int index)
728 struct device *dev = kobj_to_dev(kobj);
729 struct radeon_device *rdev = dev_get_drvdata(dev);
730 umode_t effective_mode = attr->mode;
732 /* Skip attributes if DPM is not enabled */
733 if (rdev->pm.pm_method != PM_METHOD_DPM &&
734 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
735 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
736 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
737 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
738 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
739 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
740 return 0;
742 /* Skip fan attributes if fan is not present */
743 if (rdev->pm.no_fan &&
744 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
745 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
746 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
747 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
748 return 0;
750 /* mask fan attributes if we have no bindings for this asic to expose */
751 if ((!rdev->asic->dpm.get_fan_speed_percent &&
752 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
753 (!rdev->asic->dpm.fan_ctrl_get_mode &&
754 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
755 effective_mode &= ~S_IRUGO;
757 if ((!rdev->asic->dpm.set_fan_speed_percent &&
758 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
759 (!rdev->asic->dpm.fan_ctrl_set_mode &&
760 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
761 effective_mode &= ~S_IWUSR;
763 /* hide max/min values if we can't both query and manage the fan */
764 if ((!rdev->asic->dpm.set_fan_speed_percent &&
765 !rdev->asic->dpm.get_fan_speed_percent) &&
766 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
767 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
768 return 0;
770 return effective_mode;
773 static const struct attribute_group hwmon_attrgroup = {
774 .attrs = hwmon_attributes,
775 .is_visible = hwmon_attributes_visible,
778 static const struct attribute_group *hwmon_groups[] = {
779 &hwmon_attrgroup,
780 NULL
783 static int radeon_hwmon_init(struct radeon_device *rdev)
785 int err = 0;
787 switch (rdev->pm.int_thermal_type) {
788 case THERMAL_TYPE_RV6XX:
789 case THERMAL_TYPE_RV770:
790 case THERMAL_TYPE_EVERGREEN:
791 case THERMAL_TYPE_NI:
792 case THERMAL_TYPE_SUMO:
793 case THERMAL_TYPE_SI:
794 case THERMAL_TYPE_CI:
795 case THERMAL_TYPE_KV:
796 if (rdev->asic->pm.get_temperature == NULL)
797 return err;
798 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
799 "radeon", rdev,
800 hwmon_groups);
801 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
802 err = PTR_ERR(rdev->pm.int_hwmon_dev);
803 dev_err(rdev->dev,
804 "Unable to register hwmon device: %d\n", err);
806 break;
807 default:
808 break;
811 return err;
814 static void radeon_hwmon_fini(struct radeon_device *rdev)
816 if (rdev->pm.int_hwmon_dev)
817 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
820 static void radeon_dpm_thermal_work_handler(struct work_struct *work)
822 struct radeon_device *rdev =
823 container_of(work, struct radeon_device,
824 pm.dpm.thermal.work);
825 /* switch to the thermal state */
826 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
828 if (!rdev->pm.dpm_enabled)
829 return;
831 if (rdev->asic->pm.get_temperature) {
832 int temp = radeon_get_temperature(rdev);
834 if (temp < rdev->pm.dpm.thermal.min_temp)
835 /* switch back the user state */
836 dpm_state = rdev->pm.dpm.user_state;
837 } else {
838 if (rdev->pm.dpm.thermal.high_to_low)
839 /* switch back the user state */
840 dpm_state = rdev->pm.dpm.user_state;
842 mutex_lock(&rdev->pm.mutex);
843 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
844 rdev->pm.dpm.thermal_active = true;
845 else
846 rdev->pm.dpm.thermal_active = false;
847 rdev->pm.dpm.state = dpm_state;
848 mutex_unlock(&rdev->pm.mutex);
850 radeon_pm_compute_clocks(rdev);
853 static bool radeon_dpm_single_display(struct radeon_device *rdev)
855 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
856 true : false;
858 /* check if the vblank period is too short to adjust the mclk */
859 if (single_display && rdev->asic->dpm.vblank_too_short) {
860 if (radeon_dpm_vblank_too_short(rdev))
861 single_display = false;
864 /* 120hz tends to be problematic even if they are under the
865 * vblank limit.
867 if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
868 single_display = false;
870 return single_display;
873 static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
874 enum radeon_pm_state_type dpm_state)
876 int i;
877 struct radeon_ps *ps;
878 u32 ui_class;
879 bool single_display = radeon_dpm_single_display(rdev);
881 /* certain older asics have a separare 3D performance state,
882 * so try that first if the user selected performance
884 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
885 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
886 /* balanced states don't exist at the moment */
887 if (dpm_state == POWER_STATE_TYPE_BALANCED)
888 dpm_state = rdev->pm.dpm.ac_power ?
889 POWER_STATE_TYPE_PERFORMANCE : POWER_STATE_TYPE_BATTERY;
891 restart_search:
892 /* Pick the best power state based on current conditions */
893 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
894 ps = &rdev->pm.dpm.ps[i];
895 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
896 switch (dpm_state) {
897 /* user states */
898 case POWER_STATE_TYPE_BATTERY:
899 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
900 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
901 if (single_display)
902 return ps;
903 } else
904 return ps;
906 break;
907 case POWER_STATE_TYPE_BALANCED:
908 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
909 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
910 if (single_display)
911 return ps;
912 } else
913 return ps;
915 break;
916 case POWER_STATE_TYPE_PERFORMANCE:
917 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
918 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
919 if (single_display)
920 return ps;
921 } else
922 return ps;
924 break;
925 /* internal states */
926 case POWER_STATE_TYPE_INTERNAL_UVD:
927 if (rdev->pm.dpm.uvd_ps)
928 return rdev->pm.dpm.uvd_ps;
929 else
930 break;
931 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
932 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
933 return ps;
934 break;
935 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
936 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
937 return ps;
938 break;
939 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
940 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
941 return ps;
942 break;
943 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
944 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
945 return ps;
946 break;
947 case POWER_STATE_TYPE_INTERNAL_BOOT:
948 return rdev->pm.dpm.boot_ps;
949 case POWER_STATE_TYPE_INTERNAL_THERMAL:
950 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
951 return ps;
952 break;
953 case POWER_STATE_TYPE_INTERNAL_ACPI:
954 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
955 return ps;
956 break;
957 case POWER_STATE_TYPE_INTERNAL_ULV:
958 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
959 return ps;
960 break;
961 case POWER_STATE_TYPE_INTERNAL_3DPERF:
962 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
963 return ps;
964 break;
965 default:
966 break;
969 /* use a fallback state if we didn't match */
970 switch (dpm_state) {
971 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
972 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
973 goto restart_search;
974 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
975 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
976 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
977 if (rdev->pm.dpm.uvd_ps) {
978 return rdev->pm.dpm.uvd_ps;
979 } else {
980 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
981 goto restart_search;
983 case POWER_STATE_TYPE_INTERNAL_THERMAL:
984 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
985 goto restart_search;
986 case POWER_STATE_TYPE_INTERNAL_ACPI:
987 dpm_state = POWER_STATE_TYPE_BATTERY;
988 goto restart_search;
989 case POWER_STATE_TYPE_BATTERY:
990 case POWER_STATE_TYPE_BALANCED:
991 case POWER_STATE_TYPE_INTERNAL_3DPERF:
992 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
993 goto restart_search;
994 default:
995 break;
998 return NULL;
1001 static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
1003 int i;
1004 struct radeon_ps *ps;
1005 enum radeon_pm_state_type dpm_state;
1006 int ret;
1007 bool single_display = radeon_dpm_single_display(rdev);
1009 /* if dpm init failed */
1010 if (!rdev->pm.dpm_enabled)
1011 return;
1013 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1014 /* add other state override checks here */
1015 if ((!rdev->pm.dpm.thermal_active) &&
1016 (!rdev->pm.dpm.uvd_active))
1017 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1019 dpm_state = rdev->pm.dpm.state;
1021 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1022 if (ps)
1023 rdev->pm.dpm.requested_ps = ps;
1024 else
1025 return;
1027 /* no need to reprogram if nothing changed unless we are on BTC+ */
1028 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
1029 /* vce just modifies an existing state so force a change */
1030 if (ps->vce_active != rdev->pm.dpm.vce_active)
1031 goto force;
1032 /* user has made a display change (such as timing) */
1033 if (rdev->pm.dpm.single_display != single_display)
1034 goto force;
1035 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1036 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
1037 * all we need to do is update the display configuration.
1039 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1040 /* update display watermarks based on new power state */
1041 radeon_bandwidth_update(rdev);
1042 /* update displays */
1043 radeon_dpm_display_configuration_changed(rdev);
1044 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1045 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1047 return;
1048 } else {
1049 /* for BTC+ if the num crtcs hasn't changed and state is the same,
1050 * nothing to do, if the num crtcs is > 1 and state is the same,
1051 * update display configuration.
1053 if (rdev->pm.dpm.new_active_crtcs ==
1054 rdev->pm.dpm.current_active_crtcs) {
1055 return;
1056 } else {
1057 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1058 (rdev->pm.dpm.new_active_crtc_count > 1)) {
1059 /* update display watermarks based on new power state */
1060 radeon_bandwidth_update(rdev);
1061 /* update displays */
1062 radeon_dpm_display_configuration_changed(rdev);
1063 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1064 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1065 return;
1071 force:
1072 if (radeon_dpm == 1) {
1073 printk("switching from power state:\n");
1074 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1075 printk("switching to power state:\n");
1076 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1079 down_write(&rdev->pm.mclk_lock);
1080 mutex_lock(&rdev->ring_lock);
1082 /* update whether vce is active */
1083 ps->vce_active = rdev->pm.dpm.vce_active;
1085 ret = radeon_dpm_pre_set_power_state(rdev);
1086 if (ret)
1087 goto done;
1089 /* update display watermarks based on new power state */
1090 radeon_bandwidth_update(rdev);
1091 /* update displays */
1092 radeon_dpm_display_configuration_changed(rdev);
1094 /* wait for the rings to drain */
1095 for (i = 0; i < RADEON_NUM_RINGS; i++) {
1096 struct radeon_ring *ring = &rdev->ring[i];
1097 if (ring->ready)
1098 radeon_fence_wait_empty(rdev, i);
1101 /* program the new power state */
1102 radeon_dpm_set_power_state(rdev);
1104 /* update current power state */
1105 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1107 radeon_dpm_post_set_power_state(rdev);
1109 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1110 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1111 rdev->pm.dpm.single_display = single_display;
1113 if (rdev->asic->dpm.force_performance_level) {
1114 if (rdev->pm.dpm.thermal_active) {
1115 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
1116 /* force low perf level for thermal */
1117 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
1118 /* save the user's level */
1119 rdev->pm.dpm.forced_level = level;
1120 } else {
1121 /* otherwise, user selected level */
1122 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
1126 done:
1127 mutex_unlock(&rdev->ring_lock);
1128 up_write(&rdev->pm.mclk_lock);
1131 void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1133 enum radeon_pm_state_type dpm_state;
1135 if (rdev->asic->dpm.powergate_uvd) {
1136 mutex_lock(&rdev->pm.mutex);
1137 /* don't powergate anything if we
1138 have active but pause streams */
1139 enable |= rdev->pm.dpm.sd > 0;
1140 enable |= rdev->pm.dpm.hd > 0;
1141 /* enable/disable UVD */
1142 radeon_dpm_powergate_uvd(rdev, !enable);
1143 mutex_unlock(&rdev->pm.mutex);
1144 } else {
1145 if (enable) {
1146 mutex_lock(&rdev->pm.mutex);
1147 rdev->pm.dpm.uvd_active = true;
1148 /* disable this for now */
1149 #if 0
1150 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1151 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1152 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1153 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1154 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1155 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1156 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1157 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1158 else
1159 #endif
1160 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1161 rdev->pm.dpm.state = dpm_state;
1162 mutex_unlock(&rdev->pm.mutex);
1163 } else {
1164 mutex_lock(&rdev->pm.mutex);
1165 rdev->pm.dpm.uvd_active = false;
1166 mutex_unlock(&rdev->pm.mutex);
1169 radeon_pm_compute_clocks(rdev);
1173 void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1175 if (enable) {
1176 mutex_lock(&rdev->pm.mutex);
1177 rdev->pm.dpm.vce_active = true;
1178 /* XXX select vce level based on ring/task */
1179 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1180 mutex_unlock(&rdev->pm.mutex);
1181 } else {
1182 mutex_lock(&rdev->pm.mutex);
1183 rdev->pm.dpm.vce_active = false;
1184 mutex_unlock(&rdev->pm.mutex);
1187 radeon_pm_compute_clocks(rdev);
1190 static void radeon_pm_suspend_old(struct radeon_device *rdev)
1192 mutex_lock(&rdev->pm.mutex);
1193 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1194 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1195 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
1197 mutex_unlock(&rdev->pm.mutex);
1199 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1202 static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1204 mutex_lock(&rdev->pm.mutex);
1205 /* disable dpm */
1206 radeon_dpm_disable(rdev);
1207 /* reset the power state */
1208 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1209 rdev->pm.dpm_enabled = false;
1210 mutex_unlock(&rdev->pm.mutex);
1213 void radeon_pm_suspend(struct radeon_device *rdev)
1215 if (rdev->pm.pm_method == PM_METHOD_DPM)
1216 radeon_pm_suspend_dpm(rdev);
1217 else
1218 radeon_pm_suspend_old(rdev);
1221 static void radeon_pm_resume_old(struct radeon_device *rdev)
1223 /* set up the default clocks if the MC ucode is loaded */
1224 if ((rdev->family >= CHIP_BARTS) &&
1225 (rdev->family <= CHIP_CAYMAN) &&
1226 rdev->mc_fw) {
1227 if (rdev->pm.default_vddc)
1228 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1229 SET_VOLTAGE_TYPE_ASIC_VDDC);
1230 if (rdev->pm.default_vddci)
1231 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1232 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1233 if (rdev->pm.default_sclk)
1234 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1235 if (rdev->pm.default_mclk)
1236 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1238 /* asic init will reset the default power state */
1239 mutex_lock(&rdev->pm.mutex);
1240 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1241 rdev->pm.current_clock_mode_index = 0;
1242 rdev->pm.current_sclk = rdev->pm.default_sclk;
1243 rdev->pm.current_mclk = rdev->pm.default_mclk;
1244 if (rdev->pm.power_state) {
1245 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1246 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1248 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1249 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1250 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1251 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1252 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1254 mutex_unlock(&rdev->pm.mutex);
1255 radeon_pm_compute_clocks(rdev);
1258 static void radeon_pm_resume_dpm(struct radeon_device *rdev)
1260 int ret;
1262 /* asic init will reset to the boot state */
1263 mutex_lock(&rdev->pm.mutex);
1264 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1265 radeon_dpm_setup_asic(rdev);
1266 ret = radeon_dpm_enable(rdev);
1267 mutex_unlock(&rdev->pm.mutex);
1268 if (ret)
1269 goto dpm_resume_fail;
1270 rdev->pm.dpm_enabled = true;
1271 return;
1273 dpm_resume_fail:
1274 DRM_ERROR("radeon: dpm resume failed\n");
1275 if ((rdev->family >= CHIP_BARTS) &&
1276 (rdev->family <= CHIP_CAYMAN) &&
1277 rdev->mc_fw) {
1278 if (rdev->pm.default_vddc)
1279 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1280 SET_VOLTAGE_TYPE_ASIC_VDDC);
1281 if (rdev->pm.default_vddci)
1282 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1283 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1284 if (rdev->pm.default_sclk)
1285 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1286 if (rdev->pm.default_mclk)
1287 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1291 void radeon_pm_resume(struct radeon_device *rdev)
1293 if (rdev->pm.pm_method == PM_METHOD_DPM)
1294 radeon_pm_resume_dpm(rdev);
1295 else
1296 radeon_pm_resume_old(rdev);
1299 static int radeon_pm_init_old(struct radeon_device *rdev)
1301 int ret;
1303 rdev->pm.profile = PM_PROFILE_DEFAULT;
1304 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1305 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1306 rdev->pm.dynpm_can_upclock = true;
1307 rdev->pm.dynpm_can_downclock = true;
1308 rdev->pm.default_sclk = rdev->clock.default_sclk;
1309 rdev->pm.default_mclk = rdev->clock.default_mclk;
1310 rdev->pm.current_sclk = rdev->clock.default_sclk;
1311 rdev->pm.current_mclk = rdev->clock.default_mclk;
1312 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1314 if (rdev->bios) {
1315 if (rdev->is_atom_bios)
1316 radeon_atombios_get_power_modes(rdev);
1317 else
1318 radeon_combios_get_power_modes(rdev);
1319 radeon_pm_print_states(rdev);
1320 radeon_pm_init_profile(rdev);
1321 /* set up the default clocks if the MC ucode is loaded */
1322 if ((rdev->family >= CHIP_BARTS) &&
1323 (rdev->family <= CHIP_CAYMAN) &&
1324 rdev->mc_fw) {
1325 if (rdev->pm.default_vddc)
1326 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1327 SET_VOLTAGE_TYPE_ASIC_VDDC);
1328 if (rdev->pm.default_vddci)
1329 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1330 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1331 if (rdev->pm.default_sclk)
1332 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1333 if (rdev->pm.default_mclk)
1334 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1338 /* set up the internal thermal sensor if applicable */
1339 ret = radeon_hwmon_init(rdev);
1340 if (ret)
1341 return ret;
1343 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1345 if (rdev->pm.num_power_states > 1) {
1346 if (radeon_debugfs_pm_init(rdev)) {
1347 DRM_ERROR("Failed to register debugfs file for PM!\n");
1350 DRM_INFO("radeon: power management initialized\n");
1353 return 0;
1356 static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1358 int i;
1360 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1361 printk("== power state %d ==\n", i);
1362 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1366 static int radeon_pm_init_dpm(struct radeon_device *rdev)
1368 int ret;
1370 /* default to balanced state */
1371 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1372 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
1373 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1374 rdev->pm.default_sclk = rdev->clock.default_sclk;
1375 rdev->pm.default_mclk = rdev->clock.default_mclk;
1376 rdev->pm.current_sclk = rdev->clock.default_sclk;
1377 rdev->pm.current_mclk = rdev->clock.default_mclk;
1378 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1380 if (rdev->bios && rdev->is_atom_bios)
1381 radeon_atombios_get_power_modes(rdev);
1382 else
1383 return -EINVAL;
1385 /* set up the internal thermal sensor if applicable */
1386 ret = radeon_hwmon_init(rdev);
1387 if (ret)
1388 return ret;
1390 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1391 mutex_lock(&rdev->pm.mutex);
1392 radeon_dpm_init(rdev);
1393 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1394 if (radeon_dpm == 1)
1395 radeon_dpm_print_power_states(rdev);
1396 radeon_dpm_setup_asic(rdev);
1397 ret = radeon_dpm_enable(rdev);
1398 mutex_unlock(&rdev->pm.mutex);
1399 if (ret)
1400 goto dpm_failed;
1401 rdev->pm.dpm_enabled = true;
1403 if (radeon_debugfs_pm_init(rdev)) {
1404 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1407 DRM_INFO("radeon: dpm initialized\n");
1409 return 0;
1411 dpm_failed:
1412 rdev->pm.dpm_enabled = false;
1413 if ((rdev->family >= CHIP_BARTS) &&
1414 (rdev->family <= CHIP_CAYMAN) &&
1415 rdev->mc_fw) {
1416 if (rdev->pm.default_vddc)
1417 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1418 SET_VOLTAGE_TYPE_ASIC_VDDC);
1419 if (rdev->pm.default_vddci)
1420 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1421 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1422 if (rdev->pm.default_sclk)
1423 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1424 if (rdev->pm.default_mclk)
1425 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1427 DRM_ERROR("radeon: dpm initialization failed\n");
1428 return ret;
1431 struct radeon_dpm_quirk {
1432 u32 chip_vendor;
1433 u32 chip_device;
1434 u32 subsys_vendor;
1435 u32 subsys_device;
1438 /* cards with dpm stability problems */
1439 static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
1440 /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
1441 { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
1442 /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
1443 { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
1444 { 0, 0, 0, 0 },
1447 int radeon_pm_init(struct radeon_device *rdev)
1449 struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
1450 bool disable_dpm = false;
1452 /* Apply dpm quirks */
1453 while (p && p->chip_device != 0) {
1454 if (rdev->pdev->vendor == p->chip_vendor &&
1455 rdev->pdev->device == p->chip_device &&
1456 rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1457 rdev->pdev->subsystem_device == p->subsys_device) {
1458 disable_dpm = true;
1459 break;
1461 ++p;
1464 /* enable dpm on rv6xx+ */
1465 switch (rdev->family) {
1466 case CHIP_RV610:
1467 case CHIP_RV630:
1468 case CHIP_RV620:
1469 case CHIP_RV635:
1470 case CHIP_RV670:
1471 case CHIP_RS780:
1472 case CHIP_RS880:
1473 case CHIP_RV770:
1474 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1475 if (!rdev->rlc_fw)
1476 rdev->pm.pm_method = PM_METHOD_PROFILE;
1477 else if ((rdev->family >= CHIP_RV770) &&
1478 (!(rdev->flags & RADEON_IS_IGP)) &&
1479 (!rdev->smc_fw))
1480 rdev->pm.pm_method = PM_METHOD_PROFILE;
1481 else if (radeon_dpm == 1)
1482 rdev->pm.pm_method = PM_METHOD_DPM;
1483 else
1484 rdev->pm.pm_method = PM_METHOD_PROFILE;
1485 break;
1486 case CHIP_RV730:
1487 case CHIP_RV710:
1488 case CHIP_RV740:
1489 case CHIP_CEDAR:
1490 case CHIP_REDWOOD:
1491 case CHIP_JUNIPER:
1492 case CHIP_CYPRESS:
1493 case CHIP_HEMLOCK:
1494 case CHIP_PALM:
1495 case CHIP_SUMO:
1496 case CHIP_SUMO2:
1497 case CHIP_BARTS:
1498 case CHIP_TURKS:
1499 case CHIP_CAICOS:
1500 case CHIP_CAYMAN:
1501 case CHIP_ARUBA:
1502 case CHIP_TAHITI:
1503 case CHIP_PITCAIRN:
1504 case CHIP_VERDE:
1505 case CHIP_OLAND:
1506 case CHIP_HAINAN:
1507 case CHIP_BONAIRE:
1508 case CHIP_KABINI:
1509 case CHIP_KAVERI:
1510 case CHIP_HAWAII:
1511 case CHIP_MULLINS:
1512 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1513 if (!rdev->rlc_fw)
1514 rdev->pm.pm_method = PM_METHOD_PROFILE;
1515 else if ((rdev->family >= CHIP_RV770) &&
1516 (!(rdev->flags & RADEON_IS_IGP)) &&
1517 (!rdev->smc_fw))
1518 rdev->pm.pm_method = PM_METHOD_PROFILE;
1519 else if (disable_dpm && (radeon_dpm == -1))
1520 rdev->pm.pm_method = PM_METHOD_PROFILE;
1521 else if (radeon_dpm == 0)
1522 rdev->pm.pm_method = PM_METHOD_PROFILE;
1523 else
1524 rdev->pm.pm_method = PM_METHOD_DPM;
1525 break;
1526 default:
1527 /* default to profile method */
1528 rdev->pm.pm_method = PM_METHOD_PROFILE;
1529 break;
1532 if (rdev->pm.pm_method == PM_METHOD_DPM)
1533 return radeon_pm_init_dpm(rdev);
1534 else
1535 return radeon_pm_init_old(rdev);
1538 int radeon_pm_late_init(struct radeon_device *rdev)
1540 int ret = 0;
1542 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1543 if (rdev->pm.dpm_enabled) {
1544 if (!rdev->pm.sysfs_initialized) {
1545 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1546 if (ret)
1547 DRM_ERROR("failed to create device file for dpm state\n");
1548 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1549 if (ret)
1550 DRM_ERROR("failed to create device file for dpm state\n");
1551 /* XXX: these are noops for dpm but are here for backwards compat */
1552 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1553 if (ret)
1554 DRM_ERROR("failed to create device file for power profile\n");
1555 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1556 if (ret)
1557 DRM_ERROR("failed to create device file for power method\n");
1558 rdev->pm.sysfs_initialized = true;
1561 mutex_lock(&rdev->pm.mutex);
1562 ret = radeon_dpm_late_enable(rdev);
1563 mutex_unlock(&rdev->pm.mutex);
1564 if (ret) {
1565 rdev->pm.dpm_enabled = false;
1566 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1567 } else {
1568 /* set the dpm state for PX since there won't be
1569 * a modeset to call this.
1571 radeon_pm_compute_clocks(rdev);
1574 } else {
1575 if ((rdev->pm.num_power_states > 1) &&
1576 (!rdev->pm.sysfs_initialized)) {
1577 /* where's the best place to put these? */
1578 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1579 if (ret)
1580 DRM_ERROR("failed to create device file for power profile\n");
1581 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1582 if (ret)
1583 DRM_ERROR("failed to create device file for power method\n");
1584 if (!ret)
1585 rdev->pm.sysfs_initialized = true;
1588 return ret;
1591 static void radeon_pm_fini_old(struct radeon_device *rdev)
1593 if (rdev->pm.num_power_states > 1) {
1594 mutex_lock(&rdev->pm.mutex);
1595 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1596 rdev->pm.profile = PM_PROFILE_DEFAULT;
1597 radeon_pm_update_profile(rdev);
1598 radeon_pm_set_clocks(rdev);
1599 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1600 /* reset default clocks */
1601 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1602 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1603 radeon_pm_set_clocks(rdev);
1605 mutex_unlock(&rdev->pm.mutex);
1607 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1609 device_remove_file(rdev->dev, &dev_attr_power_profile);
1610 device_remove_file(rdev->dev, &dev_attr_power_method);
1613 radeon_hwmon_fini(rdev);
1614 kfree(rdev->pm.power_state);
1617 static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1619 if (rdev->pm.num_power_states > 1) {
1620 mutex_lock(&rdev->pm.mutex);
1621 radeon_dpm_disable(rdev);
1622 mutex_unlock(&rdev->pm.mutex);
1624 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
1625 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1626 /* XXX backwards compat */
1627 device_remove_file(rdev->dev, &dev_attr_power_profile);
1628 device_remove_file(rdev->dev, &dev_attr_power_method);
1630 radeon_dpm_fini(rdev);
1632 radeon_hwmon_fini(rdev);
1633 kfree(rdev->pm.power_state);
1636 void radeon_pm_fini(struct radeon_device *rdev)
1638 if (rdev->pm.pm_method == PM_METHOD_DPM)
1639 radeon_pm_fini_dpm(rdev);
1640 else
1641 radeon_pm_fini_old(rdev);
1644 static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1646 struct drm_device *ddev = rdev->ddev;
1647 struct drm_crtc *crtc;
1648 struct radeon_crtc *radeon_crtc;
1650 if (rdev->pm.num_power_states < 2)
1651 return;
1653 mutex_lock(&rdev->pm.mutex);
1655 rdev->pm.active_crtcs = 0;
1656 rdev->pm.active_crtc_count = 0;
1657 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1658 list_for_each_entry(crtc,
1659 &ddev->mode_config.crtc_list, head) {
1660 radeon_crtc = to_radeon_crtc(crtc);
1661 if (radeon_crtc->enabled) {
1662 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1663 rdev->pm.active_crtc_count++;
1668 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1669 radeon_pm_update_profile(rdev);
1670 radeon_pm_set_clocks(rdev);
1671 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1672 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1673 if (rdev->pm.active_crtc_count > 1) {
1674 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1675 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1677 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1678 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1679 radeon_pm_get_dynpm_state(rdev);
1680 radeon_pm_set_clocks(rdev);
1682 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1684 } else if (rdev->pm.active_crtc_count == 1) {
1685 /* TODO: Increase clocks if needed for current mode */
1687 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1688 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1689 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1690 radeon_pm_get_dynpm_state(rdev);
1691 radeon_pm_set_clocks(rdev);
1693 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1694 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1695 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1696 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1697 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1698 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1699 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1701 } else { /* count == 0 */
1702 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1703 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1705 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1706 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1707 radeon_pm_get_dynpm_state(rdev);
1708 radeon_pm_set_clocks(rdev);
1714 mutex_unlock(&rdev->pm.mutex);
1717 static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1719 struct drm_device *ddev = rdev->ddev;
1720 struct drm_crtc *crtc;
1721 struct radeon_crtc *radeon_crtc;
1723 if (!rdev->pm.dpm_enabled)
1724 return;
1726 mutex_lock(&rdev->pm.mutex);
1728 /* update active crtc counts */
1729 rdev->pm.dpm.new_active_crtcs = 0;
1730 rdev->pm.dpm.new_active_crtc_count = 0;
1731 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1732 list_for_each_entry(crtc,
1733 &ddev->mode_config.crtc_list, head) {
1734 radeon_crtc = to_radeon_crtc(crtc);
1735 if (crtc->enabled) {
1736 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1737 rdev->pm.dpm.new_active_crtc_count++;
1742 /* update battery/ac status */
1743 if (power_supply_is_system_supplied() > 0)
1744 rdev->pm.dpm.ac_power = true;
1745 else
1746 rdev->pm.dpm.ac_power = false;
1748 radeon_dpm_change_power_state_locked(rdev);
1750 mutex_unlock(&rdev->pm.mutex);
1754 void radeon_pm_compute_clocks(struct radeon_device *rdev)
1756 if (rdev->pm.pm_method == PM_METHOD_DPM)
1757 radeon_pm_compute_clocks_dpm(rdev);
1758 else
1759 radeon_pm_compute_clocks_old(rdev);
1762 static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1764 int crtc, vpos, hpos, vbl_status;
1765 bool in_vbl = true;
1767 /* Iterate over all active crtc's. All crtc's must be in vblank,
1768 * otherwise return in_vbl == false.
1770 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1771 if (rdev->pm.active_crtcs & (1 << crtc)) {
1772 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
1773 crtc,
1774 USE_REAL_VBLANKSTART,
1775 &vpos, &hpos, NULL, NULL,
1776 &rdev->mode_info.crtcs[crtc]->base.hwmode);
1777 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1778 !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
1779 in_vbl = false;
1783 return in_vbl;
1786 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1788 u32 stat_crtc = 0;
1789 bool in_vbl = radeon_pm_in_vbl(rdev);
1791 if (in_vbl == false)
1792 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1793 finish ? "exit" : "entry");
1794 return in_vbl;
1797 static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1799 struct radeon_device *rdev;
1800 int resched;
1801 rdev = container_of(work, struct radeon_device,
1802 pm.dynpm_idle_work.work);
1804 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1805 mutex_lock(&rdev->pm.mutex);
1806 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1807 int not_processed = 0;
1808 int i;
1810 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1811 struct radeon_ring *ring = &rdev->ring[i];
1813 if (ring->ready) {
1814 not_processed += radeon_fence_count_emitted(rdev, i);
1815 if (not_processed >= 3)
1816 break;
1820 if (not_processed >= 3) { /* should upclock */
1821 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1822 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1823 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1824 rdev->pm.dynpm_can_upclock) {
1825 rdev->pm.dynpm_planned_action =
1826 DYNPM_ACTION_UPCLOCK;
1827 rdev->pm.dynpm_action_timeout = jiffies +
1828 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1830 } else if (not_processed == 0) { /* should downclock */
1831 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1832 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1833 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1834 rdev->pm.dynpm_can_downclock) {
1835 rdev->pm.dynpm_planned_action =
1836 DYNPM_ACTION_DOWNCLOCK;
1837 rdev->pm.dynpm_action_timeout = jiffies +
1838 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1842 /* Note, radeon_pm_set_clocks is called with static_switch set
1843 * to false since we want to wait for vbl to avoid flicker.
1845 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1846 jiffies > rdev->pm.dynpm_action_timeout) {
1847 radeon_pm_get_dynpm_state(rdev);
1848 radeon_pm_set_clocks(rdev);
1851 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1852 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1854 mutex_unlock(&rdev->pm.mutex);
1855 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
1859 * Debugfs info
1861 #if defined(CONFIG_DEBUG_FS)
1863 static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1865 struct drm_info_node *node = (struct drm_info_node *) m->private;
1866 struct drm_device *dev = node->minor->dev;
1867 struct radeon_device *rdev = dev->dev_private;
1868 struct drm_device *ddev = rdev->ddev;
1870 if ((rdev->flags & RADEON_IS_PX) &&
1871 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1872 seq_printf(m, "PX asic powered off\n");
1873 } else if (rdev->pm.dpm_enabled) {
1874 mutex_lock(&rdev->pm.mutex);
1875 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1876 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1877 else
1878 seq_printf(m, "Debugfs support not implemented for this asic\n");
1879 mutex_unlock(&rdev->pm.mutex);
1880 } else {
1881 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1882 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1883 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1884 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1885 else
1886 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1887 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1888 if (rdev->asic->pm.get_memory_clock)
1889 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1890 if (rdev->pm.current_vddc)
1891 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1892 if (rdev->asic->pm.get_pcie_lanes)
1893 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1896 return 0;
1899 static struct drm_info_list radeon_pm_info_list[] = {
1900 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1902 #endif
1904 static int radeon_debugfs_pm_init(struct radeon_device *rdev)
1906 #if defined(CONFIG_DEBUG_FS)
1907 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1908 #else
1909 return 0;
1910 #endif