WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / radeon / radeon_pm.c
blob1995dad59dd098893f7007dd6fee3c559a6d05c9
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>
24 #include <linux/hwmon-sysfs.h>
25 #include <linux/hwmon.h>
26 #include <linux/pci.h>
27 #include <linux/power_supply.h>
29 #include <drm/drm_debugfs.h>
30 #include <drm/drm_vblank.h>
32 #include "atom.h"
33 #include "avivod.h"
34 #include "r600_dpm.h"
35 #include "radeon.h"
36 #include "radeon_pm.h"
38 #define RADEON_IDLE_LOOP_MS 100
39 #define RADEON_RECLOCK_DELAY_MS 200
40 #define RADEON_WAIT_VBLANK_TIMEOUT 200
42 static const char *radeon_pm_state_type_name[5] = {
43 "",
44 "Powersave",
45 "Battery",
46 "Balanced",
47 "Performance",
50 static void radeon_dynpm_idle_work_handler(struct work_struct *work);
51 static int radeon_debugfs_pm_init(struct radeon_device *rdev);
52 static bool radeon_pm_in_vbl(struct radeon_device *rdev);
53 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
54 static void radeon_pm_update_profile(struct radeon_device *rdev);
55 static void radeon_pm_set_clocks(struct radeon_device *rdev);
57 int radeon_pm_get_type_index(struct radeon_device *rdev,
58 enum radeon_pm_state_type ps_type,
59 int instance)
61 int i;
62 int found_instance = -1;
64 for (i = 0; i < rdev->pm.num_power_states; i++) {
65 if (rdev->pm.power_state[i].type == ps_type) {
66 found_instance++;
67 if (found_instance == instance)
68 return i;
71 /* return default if no match */
72 return rdev->pm.default_power_state_index;
75 void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
77 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
78 mutex_lock(&rdev->pm.mutex);
79 if (power_supply_is_system_supplied() > 0)
80 rdev->pm.dpm.ac_power = true;
81 else
82 rdev->pm.dpm.ac_power = false;
83 if (rdev->family == CHIP_ARUBA) {
84 if (rdev->asic->dpm.enable_bapm)
85 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
87 mutex_unlock(&rdev->pm.mutex);
88 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
89 if (rdev->pm.profile == PM_PROFILE_AUTO) {
90 mutex_lock(&rdev->pm.mutex);
91 radeon_pm_update_profile(rdev);
92 radeon_pm_set_clocks(rdev);
93 mutex_unlock(&rdev->pm.mutex);
98 static void radeon_pm_update_profile(struct radeon_device *rdev)
100 switch (rdev->pm.profile) {
101 case PM_PROFILE_DEFAULT:
102 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
103 break;
104 case PM_PROFILE_AUTO:
105 if (power_supply_is_system_supplied() > 0) {
106 if (rdev->pm.active_crtc_count > 1)
107 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
108 else
109 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
110 } else {
111 if (rdev->pm.active_crtc_count > 1)
112 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
113 else
114 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
116 break;
117 case PM_PROFILE_LOW:
118 if (rdev->pm.active_crtc_count > 1)
119 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
120 else
121 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
122 break;
123 case PM_PROFILE_MID:
124 if (rdev->pm.active_crtc_count > 1)
125 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
126 else
127 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
128 break;
129 case PM_PROFILE_HIGH:
130 if (rdev->pm.active_crtc_count > 1)
131 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
132 else
133 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
134 break;
137 if (rdev->pm.active_crtc_count == 0) {
138 rdev->pm.requested_power_state_index =
139 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
140 rdev->pm.requested_clock_mode_index =
141 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
142 } else {
143 rdev->pm.requested_power_state_index =
144 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
145 rdev->pm.requested_clock_mode_index =
146 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
150 static void radeon_unmap_vram_bos(struct radeon_device *rdev)
152 struct radeon_bo *bo, *n;
154 if (list_empty(&rdev->gem.objects))
155 return;
157 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
158 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
159 ttm_bo_unmap_virtual(&bo->tbo);
163 static void radeon_sync_with_vblank(struct radeon_device *rdev)
165 if (rdev->pm.active_crtcs) {
166 rdev->pm.vblank_sync = false;
167 wait_event_timeout(
168 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
169 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
173 static void radeon_set_power_state(struct radeon_device *rdev)
175 u32 sclk, mclk;
176 bool misc_after = false;
178 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
179 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
180 return;
182 if (radeon_gui_idle(rdev)) {
183 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
184 clock_info[rdev->pm.requested_clock_mode_index].sclk;
185 if (sclk > rdev->pm.default_sclk)
186 sclk = rdev->pm.default_sclk;
188 /* starting with BTC, there is one state that is used for both
189 * MH and SH. Difference is that we always use the high clock index for
190 * mclk and vddci.
192 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
193 (rdev->family >= CHIP_BARTS) &&
194 rdev->pm.active_crtc_count &&
195 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
196 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
197 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
198 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
199 else
200 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
201 clock_info[rdev->pm.requested_clock_mode_index].mclk;
203 if (mclk > rdev->pm.default_mclk)
204 mclk = rdev->pm.default_mclk;
206 /* upvolt before raising clocks, downvolt after lowering clocks */
207 if (sclk < rdev->pm.current_sclk)
208 misc_after = true;
210 radeon_sync_with_vblank(rdev);
212 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
213 if (!radeon_pm_in_vbl(rdev))
214 return;
217 radeon_pm_prepare(rdev);
219 if (!misc_after)
220 /* voltage, pcie lanes, etc.*/
221 radeon_pm_misc(rdev);
223 /* set engine clock */
224 if (sclk != rdev->pm.current_sclk) {
225 radeon_pm_debug_check_in_vbl(rdev, false);
226 radeon_set_engine_clock(rdev, sclk);
227 radeon_pm_debug_check_in_vbl(rdev, true);
228 rdev->pm.current_sclk = sclk;
229 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
232 /* set memory clock */
233 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
234 radeon_pm_debug_check_in_vbl(rdev, false);
235 radeon_set_memory_clock(rdev, mclk);
236 radeon_pm_debug_check_in_vbl(rdev, true);
237 rdev->pm.current_mclk = mclk;
238 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
241 if (misc_after)
242 /* voltage, pcie lanes, etc.*/
243 radeon_pm_misc(rdev);
245 radeon_pm_finish(rdev);
247 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
248 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
249 } else
250 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
253 static void radeon_pm_set_clocks(struct radeon_device *rdev)
255 struct drm_crtc *crtc;
256 int i, r;
258 /* no need to take locks, etc. if nothing's going to change */
259 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
260 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
261 return;
263 down_write(&rdev->pm.mclk_lock);
264 mutex_lock(&rdev->ring_lock);
266 /* wait for the rings to drain */
267 for (i = 0; i < RADEON_NUM_RINGS; i++) {
268 struct radeon_ring *ring = &rdev->ring[i];
269 if (!ring->ready) {
270 continue;
272 r = radeon_fence_wait_empty(rdev, i);
273 if (r) {
274 /* needs a GPU reset dont reset here */
275 mutex_unlock(&rdev->ring_lock);
276 up_write(&rdev->pm.mclk_lock);
277 return;
281 radeon_unmap_vram_bos(rdev);
283 if (rdev->irq.installed) {
284 i = 0;
285 drm_for_each_crtc(crtc, rdev->ddev) {
286 if (rdev->pm.active_crtcs & (1 << i)) {
287 /* This can fail if a modeset is in progress */
288 if (drm_crtc_vblank_get(crtc) == 0)
289 rdev->pm.req_vblank |= (1 << i);
290 else
291 DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
294 i++;
298 radeon_set_power_state(rdev);
300 if (rdev->irq.installed) {
301 i = 0;
302 drm_for_each_crtc(crtc, rdev->ddev) {
303 if (rdev->pm.req_vblank & (1 << i)) {
304 rdev->pm.req_vblank &= ~(1 << i);
305 drm_crtc_vblank_put(crtc);
307 i++;
311 /* update display watermarks based on new power state */
312 radeon_update_bandwidth_info(rdev);
313 if (rdev->pm.active_crtc_count)
314 radeon_bandwidth_update(rdev);
316 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
318 mutex_unlock(&rdev->ring_lock);
319 up_write(&rdev->pm.mclk_lock);
322 static void radeon_pm_print_states(struct radeon_device *rdev)
324 int i, j;
325 struct radeon_power_state *power_state;
326 struct radeon_pm_clock_info *clock_info;
328 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
329 for (i = 0; i < rdev->pm.num_power_states; i++) {
330 power_state = &rdev->pm.power_state[i];
331 DRM_DEBUG_DRIVER("State %d: %s\n", i,
332 radeon_pm_state_type_name[power_state->type]);
333 if (i == rdev->pm.default_power_state_index)
334 DRM_DEBUG_DRIVER("\tDefault");
335 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
336 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
337 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
338 DRM_DEBUG_DRIVER("\tSingle display only\n");
339 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
340 for (j = 0; j < power_state->num_clock_modes; j++) {
341 clock_info = &(power_state->clock_info[j]);
342 if (rdev->flags & RADEON_IS_IGP)
343 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
345 clock_info->sclk * 10);
346 else
347 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
349 clock_info->sclk * 10,
350 clock_info->mclk * 10,
351 clock_info->voltage.voltage);
356 static ssize_t radeon_get_pm_profile(struct device *dev,
357 struct device_attribute *attr,
358 char *buf)
360 struct drm_device *ddev = dev_get_drvdata(dev);
361 struct radeon_device *rdev = ddev->dev_private;
362 int cp = rdev->pm.profile;
364 return snprintf(buf, PAGE_SIZE, "%s\n",
365 (cp == PM_PROFILE_AUTO) ? "auto" :
366 (cp == PM_PROFILE_LOW) ? "low" :
367 (cp == PM_PROFILE_MID) ? "mid" :
368 (cp == PM_PROFILE_HIGH) ? "high" : "default");
371 static ssize_t radeon_set_pm_profile(struct device *dev,
372 struct device_attribute *attr,
373 const char *buf,
374 size_t count)
376 struct drm_device *ddev = dev_get_drvdata(dev);
377 struct radeon_device *rdev = ddev->dev_private;
379 /* Can't set profile when the card is off */
380 if ((rdev->flags & RADEON_IS_PX) &&
381 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
382 return -EINVAL;
384 mutex_lock(&rdev->pm.mutex);
385 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
386 if (strncmp("default", buf, strlen("default")) == 0)
387 rdev->pm.profile = PM_PROFILE_DEFAULT;
388 else if (strncmp("auto", buf, strlen("auto")) == 0)
389 rdev->pm.profile = PM_PROFILE_AUTO;
390 else if (strncmp("low", buf, strlen("low")) == 0)
391 rdev->pm.profile = PM_PROFILE_LOW;
392 else if (strncmp("mid", buf, strlen("mid")) == 0)
393 rdev->pm.profile = PM_PROFILE_MID;
394 else if (strncmp("high", buf, strlen("high")) == 0)
395 rdev->pm.profile = PM_PROFILE_HIGH;
396 else {
397 count = -EINVAL;
398 goto fail;
400 radeon_pm_update_profile(rdev);
401 radeon_pm_set_clocks(rdev);
402 } else
403 count = -EINVAL;
405 fail:
406 mutex_unlock(&rdev->pm.mutex);
408 return count;
411 static ssize_t radeon_get_pm_method(struct device *dev,
412 struct device_attribute *attr,
413 char *buf)
415 struct drm_device *ddev = dev_get_drvdata(dev);
416 struct radeon_device *rdev = ddev->dev_private;
417 int pm = rdev->pm.pm_method;
419 return snprintf(buf, PAGE_SIZE, "%s\n",
420 (pm == PM_METHOD_DYNPM) ? "dynpm" :
421 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
424 static ssize_t radeon_set_pm_method(struct device *dev,
425 struct device_attribute *attr,
426 const char *buf,
427 size_t count)
429 struct drm_device *ddev = dev_get_drvdata(dev);
430 struct radeon_device *rdev = ddev->dev_private;
432 /* Can't set method when the card is off */
433 if ((rdev->flags & RADEON_IS_PX) &&
434 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
435 count = -EINVAL;
436 goto fail;
439 /* we don't support the legacy modes with dpm */
440 if (rdev->pm.pm_method == PM_METHOD_DPM) {
441 count = -EINVAL;
442 goto fail;
445 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
446 mutex_lock(&rdev->pm.mutex);
447 rdev->pm.pm_method = PM_METHOD_DYNPM;
448 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
449 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
450 mutex_unlock(&rdev->pm.mutex);
451 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
452 mutex_lock(&rdev->pm.mutex);
453 /* disable dynpm */
454 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
455 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
456 rdev->pm.pm_method = PM_METHOD_PROFILE;
457 mutex_unlock(&rdev->pm.mutex);
458 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
459 } else {
460 count = -EINVAL;
461 goto fail;
463 radeon_pm_compute_clocks(rdev);
464 fail:
465 return count;
468 static ssize_t radeon_get_dpm_state(struct device *dev,
469 struct device_attribute *attr,
470 char *buf)
472 struct drm_device *ddev = dev_get_drvdata(dev);
473 struct radeon_device *rdev = ddev->dev_private;
474 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
476 return snprintf(buf, PAGE_SIZE, "%s\n",
477 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
478 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
481 static ssize_t radeon_set_dpm_state(struct device *dev,
482 struct device_attribute *attr,
483 const char *buf,
484 size_t count)
486 struct drm_device *ddev = dev_get_drvdata(dev);
487 struct radeon_device *rdev = ddev->dev_private;
489 mutex_lock(&rdev->pm.mutex);
490 if (strncmp("battery", buf, strlen("battery")) == 0)
491 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
492 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
493 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
494 else if (strncmp("performance", buf, strlen("performance")) == 0)
495 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
496 else {
497 mutex_unlock(&rdev->pm.mutex);
498 count = -EINVAL;
499 goto fail;
501 mutex_unlock(&rdev->pm.mutex);
503 /* Can't set dpm state when the card is off */
504 if (!(rdev->flags & RADEON_IS_PX) ||
505 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
506 radeon_pm_compute_clocks(rdev);
508 fail:
509 return count;
512 static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
513 struct device_attribute *attr,
514 char *buf)
516 struct drm_device *ddev = dev_get_drvdata(dev);
517 struct radeon_device *rdev = ddev->dev_private;
518 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
520 if ((rdev->flags & RADEON_IS_PX) &&
521 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
522 return snprintf(buf, PAGE_SIZE, "off\n");
524 return snprintf(buf, PAGE_SIZE, "%s\n",
525 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
526 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
529 static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
530 struct device_attribute *attr,
531 const char *buf,
532 size_t count)
534 struct drm_device *ddev = dev_get_drvdata(dev);
535 struct radeon_device *rdev = ddev->dev_private;
536 enum radeon_dpm_forced_level level;
537 int ret = 0;
539 /* Can't force performance level when the card is off */
540 if ((rdev->flags & RADEON_IS_PX) &&
541 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
542 return -EINVAL;
544 mutex_lock(&rdev->pm.mutex);
545 if (strncmp("low", buf, strlen("low")) == 0) {
546 level = RADEON_DPM_FORCED_LEVEL_LOW;
547 } else if (strncmp("high", buf, strlen("high")) == 0) {
548 level = RADEON_DPM_FORCED_LEVEL_HIGH;
549 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
550 level = RADEON_DPM_FORCED_LEVEL_AUTO;
551 } else {
552 count = -EINVAL;
553 goto fail;
555 if (rdev->asic->dpm.force_performance_level) {
556 if (rdev->pm.dpm.thermal_active) {
557 count = -EINVAL;
558 goto fail;
560 ret = radeon_dpm_force_performance_level(rdev, level);
561 if (ret)
562 count = -EINVAL;
564 fail:
565 mutex_unlock(&rdev->pm.mutex);
567 return count;
570 static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
571 struct device_attribute *attr,
572 char *buf)
574 struct radeon_device *rdev = dev_get_drvdata(dev);
575 u32 pwm_mode = 0;
577 if (rdev->asic->dpm.fan_ctrl_get_mode)
578 pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
580 /* never 0 (full-speed), fuse or smc-controlled always */
581 return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
584 static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
585 struct device_attribute *attr,
586 const char *buf,
587 size_t count)
589 struct radeon_device *rdev = dev_get_drvdata(dev);
590 int err;
591 int value;
593 if(!rdev->asic->dpm.fan_ctrl_set_mode)
594 return -EINVAL;
596 err = kstrtoint(buf, 10, &value);
597 if (err)
598 return err;
600 switch (value) {
601 case 1: /* manual, percent-based */
602 rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
603 break;
604 default: /* disable */
605 rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
606 break;
609 return count;
612 static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
613 struct device_attribute *attr,
614 char *buf)
616 return sprintf(buf, "%i\n", 0);
619 static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
620 struct device_attribute *attr,
621 char *buf)
623 return sprintf(buf, "%i\n", 255);
626 static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
627 struct device_attribute *attr,
628 const char *buf, size_t count)
630 struct radeon_device *rdev = dev_get_drvdata(dev);
631 int err;
632 u32 value;
634 err = kstrtou32(buf, 10, &value);
635 if (err)
636 return err;
638 value = (value * 100) / 255;
640 err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
641 if (err)
642 return err;
644 return count;
647 static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
648 struct device_attribute *attr,
649 char *buf)
651 struct radeon_device *rdev = dev_get_drvdata(dev);
652 int err;
653 u32 speed;
655 err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
656 if (err)
657 return err;
659 speed = (speed * 255) / 100;
661 return sprintf(buf, "%i\n", speed);
664 static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
665 static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
666 static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
667 static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
668 radeon_get_dpm_forced_performance_level,
669 radeon_set_dpm_forced_performance_level);
671 static ssize_t radeon_hwmon_show_temp(struct device *dev,
672 struct device_attribute *attr,
673 char *buf)
675 struct radeon_device *rdev = dev_get_drvdata(dev);
676 struct drm_device *ddev = rdev->ddev;
677 int temp;
679 /* Can't get temperature when the card is off */
680 if ((rdev->flags & RADEON_IS_PX) &&
681 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
682 return -EINVAL;
684 if (rdev->asic->pm.get_temperature)
685 temp = radeon_get_temperature(rdev);
686 else
687 temp = 0;
689 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
692 static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
693 struct device_attribute *attr,
694 char *buf)
696 struct radeon_device *rdev = dev_get_drvdata(dev);
697 int hyst = to_sensor_dev_attr(attr)->index;
698 int temp;
700 if (hyst)
701 temp = rdev->pm.dpm.thermal.min_temp;
702 else
703 temp = rdev->pm.dpm.thermal.max_temp;
705 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
708 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
709 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
710 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
711 static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
712 static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
713 static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
714 static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
716 static ssize_t radeon_hwmon_show_sclk(struct device *dev,
717 struct device_attribute *attr, char *buf)
719 struct radeon_device *rdev = dev_get_drvdata(dev);
720 struct drm_device *ddev = rdev->ddev;
721 u32 sclk = 0;
723 /* Can't get clock frequency when the card is off */
724 if ((rdev->flags & RADEON_IS_PX) &&
725 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
726 return -EINVAL;
728 if (rdev->asic->dpm.get_current_sclk)
729 sclk = radeon_dpm_get_current_sclk(rdev);
731 /* Value returned by dpm is in 10 KHz units, need to convert it into Hz
732 for hwmon */
733 sclk *= 10000;
735 return snprintf(buf, PAGE_SIZE, "%u\n", sclk);
738 static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, radeon_hwmon_show_sclk, NULL,
741 static ssize_t radeon_hwmon_show_vddc(struct device *dev,
742 struct device_attribute *attr, char *buf)
744 struct radeon_device *rdev = dev_get_drvdata(dev);
745 struct drm_device *ddev = rdev->ddev;
746 u16 vddc = 0;
748 /* Can't get vddc when the card is off */
749 if ((rdev->flags & RADEON_IS_PX) &&
750 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
751 return -EINVAL;
753 if (rdev->asic->dpm.get_current_vddc)
754 vddc = rdev->asic->dpm.get_current_vddc(rdev);
756 return snprintf(buf, PAGE_SIZE, "%u\n", vddc);
759 static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, radeon_hwmon_show_vddc, NULL,
762 static struct attribute *hwmon_attributes[] = {
763 &sensor_dev_attr_temp1_input.dev_attr.attr,
764 &sensor_dev_attr_temp1_crit.dev_attr.attr,
765 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
766 &sensor_dev_attr_pwm1.dev_attr.attr,
767 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
768 &sensor_dev_attr_pwm1_min.dev_attr.attr,
769 &sensor_dev_attr_pwm1_max.dev_attr.attr,
770 &sensor_dev_attr_freq1_input.dev_attr.attr,
771 &sensor_dev_attr_in0_input.dev_attr.attr,
772 NULL
775 static umode_t hwmon_attributes_visible(struct kobject *kobj,
776 struct attribute *attr, int index)
778 struct device *dev = kobj_to_dev(kobj);
779 struct radeon_device *rdev = dev_get_drvdata(dev);
780 umode_t effective_mode = attr->mode;
782 /* Skip attributes if DPM is not enabled */
783 if (rdev->pm.pm_method != PM_METHOD_DPM &&
784 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
785 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
786 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
787 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
788 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
789 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
790 attr == &sensor_dev_attr_freq1_input.dev_attr.attr ||
791 attr == &sensor_dev_attr_in0_input.dev_attr.attr))
792 return 0;
794 /* Skip vddc attribute if get_current_vddc is not implemented */
795 if(attr == &sensor_dev_attr_in0_input.dev_attr.attr &&
796 !rdev->asic->dpm.get_current_vddc)
797 return 0;
799 /* Skip fan attributes if fan is not present */
800 if (rdev->pm.no_fan &&
801 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
802 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
803 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
804 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
805 return 0;
807 /* mask fan attributes if we have no bindings for this asic to expose */
808 if ((!rdev->asic->dpm.get_fan_speed_percent &&
809 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
810 (!rdev->asic->dpm.fan_ctrl_get_mode &&
811 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
812 effective_mode &= ~S_IRUGO;
814 if ((!rdev->asic->dpm.set_fan_speed_percent &&
815 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
816 (!rdev->asic->dpm.fan_ctrl_set_mode &&
817 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
818 effective_mode &= ~S_IWUSR;
820 /* hide max/min values if we can't both query and manage the fan */
821 if ((!rdev->asic->dpm.set_fan_speed_percent &&
822 !rdev->asic->dpm.get_fan_speed_percent) &&
823 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
824 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
825 return 0;
827 return effective_mode;
830 static const struct attribute_group hwmon_attrgroup = {
831 .attrs = hwmon_attributes,
832 .is_visible = hwmon_attributes_visible,
835 static const struct attribute_group *hwmon_groups[] = {
836 &hwmon_attrgroup,
837 NULL
840 static int radeon_hwmon_init(struct radeon_device *rdev)
842 int err = 0;
844 switch (rdev->pm.int_thermal_type) {
845 case THERMAL_TYPE_RV6XX:
846 case THERMAL_TYPE_RV770:
847 case THERMAL_TYPE_EVERGREEN:
848 case THERMAL_TYPE_NI:
849 case THERMAL_TYPE_SUMO:
850 case THERMAL_TYPE_SI:
851 case THERMAL_TYPE_CI:
852 case THERMAL_TYPE_KV:
853 if (rdev->asic->pm.get_temperature == NULL)
854 return err;
855 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
856 "radeon", rdev,
857 hwmon_groups);
858 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
859 err = PTR_ERR(rdev->pm.int_hwmon_dev);
860 dev_err(rdev->dev,
861 "Unable to register hwmon device: %d\n", err);
863 break;
864 default:
865 break;
868 return err;
871 static void radeon_hwmon_fini(struct radeon_device *rdev)
873 if (rdev->pm.int_hwmon_dev)
874 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
877 static void radeon_dpm_thermal_work_handler(struct work_struct *work)
879 struct radeon_device *rdev =
880 container_of(work, struct radeon_device,
881 pm.dpm.thermal.work);
882 /* switch to the thermal state */
883 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
885 if (!rdev->pm.dpm_enabled)
886 return;
888 if (rdev->asic->pm.get_temperature) {
889 int temp = radeon_get_temperature(rdev);
891 if (temp < rdev->pm.dpm.thermal.min_temp)
892 /* switch back the user state */
893 dpm_state = rdev->pm.dpm.user_state;
894 } else {
895 if (rdev->pm.dpm.thermal.high_to_low)
896 /* switch back the user state */
897 dpm_state = rdev->pm.dpm.user_state;
899 mutex_lock(&rdev->pm.mutex);
900 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
901 rdev->pm.dpm.thermal_active = true;
902 else
903 rdev->pm.dpm.thermal_active = false;
904 rdev->pm.dpm.state = dpm_state;
905 mutex_unlock(&rdev->pm.mutex);
907 radeon_pm_compute_clocks(rdev);
910 static bool radeon_dpm_single_display(struct radeon_device *rdev)
912 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
913 true : false;
915 /* check if the vblank period is too short to adjust the mclk */
916 if (single_display && rdev->asic->dpm.vblank_too_short) {
917 if (radeon_dpm_vblank_too_short(rdev))
918 single_display = false;
921 /* 120hz tends to be problematic even if they are under the
922 * vblank limit.
924 if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
925 single_display = false;
927 return single_display;
930 static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
931 enum radeon_pm_state_type dpm_state)
933 int i;
934 struct radeon_ps *ps;
935 u32 ui_class;
936 bool single_display = radeon_dpm_single_display(rdev);
938 /* certain older asics have a separare 3D performance state,
939 * so try that first if the user selected performance
941 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
942 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
943 /* balanced states don't exist at the moment */
944 if (dpm_state == POWER_STATE_TYPE_BALANCED)
945 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
947 restart_search:
948 /* Pick the best power state based on current conditions */
949 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
950 ps = &rdev->pm.dpm.ps[i];
951 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
952 switch (dpm_state) {
953 /* user states */
954 case POWER_STATE_TYPE_BATTERY:
955 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
956 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
957 if (single_display)
958 return ps;
959 } else
960 return ps;
962 break;
963 case POWER_STATE_TYPE_BALANCED:
964 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
965 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
966 if (single_display)
967 return ps;
968 } else
969 return ps;
971 break;
972 case POWER_STATE_TYPE_PERFORMANCE:
973 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
974 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
975 if (single_display)
976 return ps;
977 } else
978 return ps;
980 break;
981 /* internal states */
982 case POWER_STATE_TYPE_INTERNAL_UVD:
983 if (rdev->pm.dpm.uvd_ps)
984 return rdev->pm.dpm.uvd_ps;
985 else
986 break;
987 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
988 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
989 return ps;
990 break;
991 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
992 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
993 return ps;
994 break;
995 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
996 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
997 return ps;
998 break;
999 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1000 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
1001 return ps;
1002 break;
1003 case POWER_STATE_TYPE_INTERNAL_BOOT:
1004 return rdev->pm.dpm.boot_ps;
1005 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1006 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1007 return ps;
1008 break;
1009 case POWER_STATE_TYPE_INTERNAL_ACPI:
1010 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1011 return ps;
1012 break;
1013 case POWER_STATE_TYPE_INTERNAL_ULV:
1014 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1015 return ps;
1016 break;
1017 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1018 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1019 return ps;
1020 break;
1021 default:
1022 break;
1025 /* use a fallback state if we didn't match */
1026 switch (dpm_state) {
1027 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1028 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1029 goto restart_search;
1030 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1031 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1032 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1033 if (rdev->pm.dpm.uvd_ps) {
1034 return rdev->pm.dpm.uvd_ps;
1035 } else {
1036 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1037 goto restart_search;
1039 case POWER_STATE_TYPE_INTERNAL_THERMAL:
1040 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1041 goto restart_search;
1042 case POWER_STATE_TYPE_INTERNAL_ACPI:
1043 dpm_state = POWER_STATE_TYPE_BATTERY;
1044 goto restart_search;
1045 case POWER_STATE_TYPE_BATTERY:
1046 case POWER_STATE_TYPE_BALANCED:
1047 case POWER_STATE_TYPE_INTERNAL_3DPERF:
1048 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1049 goto restart_search;
1050 default:
1051 break;
1054 return NULL;
1057 static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
1059 int i;
1060 struct radeon_ps *ps;
1061 enum radeon_pm_state_type dpm_state;
1062 int ret;
1063 bool single_display = radeon_dpm_single_display(rdev);
1065 /* if dpm init failed */
1066 if (!rdev->pm.dpm_enabled)
1067 return;
1069 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1070 /* add other state override checks here */
1071 if ((!rdev->pm.dpm.thermal_active) &&
1072 (!rdev->pm.dpm.uvd_active))
1073 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1075 dpm_state = rdev->pm.dpm.state;
1077 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1078 if (ps)
1079 rdev->pm.dpm.requested_ps = ps;
1080 else
1081 return;
1083 /* no need to reprogram if nothing changed unless we are on BTC+ */
1084 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
1085 /* vce just modifies an existing state so force a change */
1086 if (ps->vce_active != rdev->pm.dpm.vce_active)
1087 goto force;
1088 /* user has made a display change (such as timing) */
1089 if (rdev->pm.dpm.single_display != single_display)
1090 goto force;
1091 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1092 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
1093 * all we need to do is update the display configuration.
1095 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1096 /* update display watermarks based on new power state */
1097 radeon_bandwidth_update(rdev);
1098 /* update displays */
1099 radeon_dpm_display_configuration_changed(rdev);
1100 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1101 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1103 return;
1104 } else {
1105 /* for BTC+ if the num crtcs hasn't changed and state is the same,
1106 * nothing to do, if the num crtcs is > 1 and state is the same,
1107 * update display configuration.
1109 if (rdev->pm.dpm.new_active_crtcs ==
1110 rdev->pm.dpm.current_active_crtcs) {
1111 return;
1112 } else {
1113 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1114 (rdev->pm.dpm.new_active_crtc_count > 1)) {
1115 /* update display watermarks based on new power state */
1116 radeon_bandwidth_update(rdev);
1117 /* update displays */
1118 radeon_dpm_display_configuration_changed(rdev);
1119 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1120 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1121 return;
1127 force:
1128 if (radeon_dpm == 1) {
1129 printk("switching from power state:\n");
1130 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1131 printk("switching to power state:\n");
1132 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1135 down_write(&rdev->pm.mclk_lock);
1136 mutex_lock(&rdev->ring_lock);
1138 /* update whether vce is active */
1139 ps->vce_active = rdev->pm.dpm.vce_active;
1141 ret = radeon_dpm_pre_set_power_state(rdev);
1142 if (ret)
1143 goto done;
1145 /* update display watermarks based on new power state */
1146 radeon_bandwidth_update(rdev);
1147 /* update displays */
1148 radeon_dpm_display_configuration_changed(rdev);
1150 /* wait for the rings to drain */
1151 for (i = 0; i < RADEON_NUM_RINGS; i++) {
1152 struct radeon_ring *ring = &rdev->ring[i];
1153 if (ring->ready)
1154 radeon_fence_wait_empty(rdev, i);
1157 /* program the new power state */
1158 radeon_dpm_set_power_state(rdev);
1160 /* update current power state */
1161 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1163 radeon_dpm_post_set_power_state(rdev);
1165 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1166 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1167 rdev->pm.dpm.single_display = single_display;
1169 if (rdev->asic->dpm.force_performance_level) {
1170 if (rdev->pm.dpm.thermal_active) {
1171 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
1172 /* force low perf level for thermal */
1173 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
1174 /* save the user's level */
1175 rdev->pm.dpm.forced_level = level;
1176 } else {
1177 /* otherwise, user selected level */
1178 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
1182 done:
1183 mutex_unlock(&rdev->ring_lock);
1184 up_write(&rdev->pm.mclk_lock);
1187 void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1189 enum radeon_pm_state_type dpm_state;
1191 if (rdev->asic->dpm.powergate_uvd) {
1192 mutex_lock(&rdev->pm.mutex);
1193 /* don't powergate anything if we
1194 have active but pause streams */
1195 enable |= rdev->pm.dpm.sd > 0;
1196 enable |= rdev->pm.dpm.hd > 0;
1197 /* enable/disable UVD */
1198 radeon_dpm_powergate_uvd(rdev, !enable);
1199 mutex_unlock(&rdev->pm.mutex);
1200 } else {
1201 if (enable) {
1202 mutex_lock(&rdev->pm.mutex);
1203 rdev->pm.dpm.uvd_active = true;
1204 /* disable this for now */
1205 #if 0
1206 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1207 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1208 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1209 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1210 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1211 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1212 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1213 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1214 else
1215 #endif
1216 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1217 rdev->pm.dpm.state = dpm_state;
1218 mutex_unlock(&rdev->pm.mutex);
1219 } else {
1220 mutex_lock(&rdev->pm.mutex);
1221 rdev->pm.dpm.uvd_active = false;
1222 mutex_unlock(&rdev->pm.mutex);
1225 radeon_pm_compute_clocks(rdev);
1229 void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1231 if (enable) {
1232 mutex_lock(&rdev->pm.mutex);
1233 rdev->pm.dpm.vce_active = true;
1234 /* XXX select vce level based on ring/task */
1235 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1236 mutex_unlock(&rdev->pm.mutex);
1237 } else {
1238 mutex_lock(&rdev->pm.mutex);
1239 rdev->pm.dpm.vce_active = false;
1240 mutex_unlock(&rdev->pm.mutex);
1243 radeon_pm_compute_clocks(rdev);
1246 static void radeon_pm_suspend_old(struct radeon_device *rdev)
1248 mutex_lock(&rdev->pm.mutex);
1249 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1250 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1251 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
1253 mutex_unlock(&rdev->pm.mutex);
1255 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1258 static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1260 mutex_lock(&rdev->pm.mutex);
1261 /* disable dpm */
1262 radeon_dpm_disable(rdev);
1263 /* reset the power state */
1264 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1265 rdev->pm.dpm_enabled = false;
1266 mutex_unlock(&rdev->pm.mutex);
1269 void radeon_pm_suspend(struct radeon_device *rdev)
1271 if (rdev->pm.pm_method == PM_METHOD_DPM)
1272 radeon_pm_suspend_dpm(rdev);
1273 else
1274 radeon_pm_suspend_old(rdev);
1277 static void radeon_pm_resume_old(struct radeon_device *rdev)
1279 /* set up the default clocks if the MC ucode is loaded */
1280 if ((rdev->family >= CHIP_BARTS) &&
1281 (rdev->family <= CHIP_CAYMAN) &&
1282 rdev->mc_fw) {
1283 if (rdev->pm.default_vddc)
1284 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1285 SET_VOLTAGE_TYPE_ASIC_VDDC);
1286 if (rdev->pm.default_vddci)
1287 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1288 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1289 if (rdev->pm.default_sclk)
1290 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1291 if (rdev->pm.default_mclk)
1292 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1294 /* asic init will reset the default power state */
1295 mutex_lock(&rdev->pm.mutex);
1296 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1297 rdev->pm.current_clock_mode_index = 0;
1298 rdev->pm.current_sclk = rdev->pm.default_sclk;
1299 rdev->pm.current_mclk = rdev->pm.default_mclk;
1300 if (rdev->pm.power_state) {
1301 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1302 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1304 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1305 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1306 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1307 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1308 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1310 mutex_unlock(&rdev->pm.mutex);
1311 radeon_pm_compute_clocks(rdev);
1314 static void radeon_pm_resume_dpm(struct radeon_device *rdev)
1316 int ret;
1318 /* asic init will reset to the boot state */
1319 mutex_lock(&rdev->pm.mutex);
1320 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1321 radeon_dpm_setup_asic(rdev);
1322 ret = radeon_dpm_enable(rdev);
1323 mutex_unlock(&rdev->pm.mutex);
1324 if (ret)
1325 goto dpm_resume_fail;
1326 rdev->pm.dpm_enabled = true;
1327 return;
1329 dpm_resume_fail:
1330 DRM_ERROR("radeon: dpm resume failed\n");
1331 if ((rdev->family >= CHIP_BARTS) &&
1332 (rdev->family <= CHIP_CAYMAN) &&
1333 rdev->mc_fw) {
1334 if (rdev->pm.default_vddc)
1335 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1336 SET_VOLTAGE_TYPE_ASIC_VDDC);
1337 if (rdev->pm.default_vddci)
1338 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1339 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1340 if (rdev->pm.default_sclk)
1341 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1342 if (rdev->pm.default_mclk)
1343 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1347 void radeon_pm_resume(struct radeon_device *rdev)
1349 if (rdev->pm.pm_method == PM_METHOD_DPM)
1350 radeon_pm_resume_dpm(rdev);
1351 else
1352 radeon_pm_resume_old(rdev);
1355 static int radeon_pm_init_old(struct radeon_device *rdev)
1357 int ret;
1359 rdev->pm.profile = PM_PROFILE_DEFAULT;
1360 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1361 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1362 rdev->pm.dynpm_can_upclock = true;
1363 rdev->pm.dynpm_can_downclock = true;
1364 rdev->pm.default_sclk = rdev->clock.default_sclk;
1365 rdev->pm.default_mclk = rdev->clock.default_mclk;
1366 rdev->pm.current_sclk = rdev->clock.default_sclk;
1367 rdev->pm.current_mclk = rdev->clock.default_mclk;
1368 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1370 if (rdev->bios) {
1371 if (rdev->is_atom_bios)
1372 radeon_atombios_get_power_modes(rdev);
1373 else
1374 radeon_combios_get_power_modes(rdev);
1375 radeon_pm_print_states(rdev);
1376 radeon_pm_init_profile(rdev);
1377 /* set up the default clocks if the MC ucode is loaded */
1378 if ((rdev->family >= CHIP_BARTS) &&
1379 (rdev->family <= CHIP_CAYMAN) &&
1380 rdev->mc_fw) {
1381 if (rdev->pm.default_vddc)
1382 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1383 SET_VOLTAGE_TYPE_ASIC_VDDC);
1384 if (rdev->pm.default_vddci)
1385 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1386 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1387 if (rdev->pm.default_sclk)
1388 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1389 if (rdev->pm.default_mclk)
1390 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1394 /* set up the internal thermal sensor if applicable */
1395 ret = radeon_hwmon_init(rdev);
1396 if (ret)
1397 return ret;
1399 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1401 if (rdev->pm.num_power_states > 1) {
1402 if (radeon_debugfs_pm_init(rdev)) {
1403 DRM_ERROR("Failed to register debugfs file for PM!\n");
1406 DRM_INFO("radeon: power management initialized\n");
1409 return 0;
1412 static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1414 int i;
1416 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1417 printk("== power state %d ==\n", i);
1418 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1422 static int radeon_pm_init_dpm(struct radeon_device *rdev)
1424 int ret;
1426 /* default to balanced state */
1427 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1428 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
1429 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1430 rdev->pm.default_sclk = rdev->clock.default_sclk;
1431 rdev->pm.default_mclk = rdev->clock.default_mclk;
1432 rdev->pm.current_sclk = rdev->clock.default_sclk;
1433 rdev->pm.current_mclk = rdev->clock.default_mclk;
1434 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1436 if (rdev->bios && rdev->is_atom_bios)
1437 radeon_atombios_get_power_modes(rdev);
1438 else
1439 return -EINVAL;
1441 /* set up the internal thermal sensor if applicable */
1442 ret = radeon_hwmon_init(rdev);
1443 if (ret)
1444 return ret;
1446 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1447 mutex_lock(&rdev->pm.mutex);
1448 radeon_dpm_init(rdev);
1449 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1450 if (radeon_dpm == 1)
1451 radeon_dpm_print_power_states(rdev);
1452 radeon_dpm_setup_asic(rdev);
1453 ret = radeon_dpm_enable(rdev);
1454 mutex_unlock(&rdev->pm.mutex);
1455 if (ret)
1456 goto dpm_failed;
1457 rdev->pm.dpm_enabled = true;
1459 if (radeon_debugfs_pm_init(rdev)) {
1460 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1463 DRM_INFO("radeon: dpm initialized\n");
1465 return 0;
1467 dpm_failed:
1468 rdev->pm.dpm_enabled = false;
1469 if ((rdev->family >= CHIP_BARTS) &&
1470 (rdev->family <= CHIP_CAYMAN) &&
1471 rdev->mc_fw) {
1472 if (rdev->pm.default_vddc)
1473 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1474 SET_VOLTAGE_TYPE_ASIC_VDDC);
1475 if (rdev->pm.default_vddci)
1476 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1477 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1478 if (rdev->pm.default_sclk)
1479 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1480 if (rdev->pm.default_mclk)
1481 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1483 DRM_ERROR("radeon: dpm initialization failed\n");
1484 return ret;
1487 struct radeon_dpm_quirk {
1488 u32 chip_vendor;
1489 u32 chip_device;
1490 u32 subsys_vendor;
1491 u32 subsys_device;
1494 /* cards with dpm stability problems */
1495 static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
1496 /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
1497 { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
1498 /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
1499 { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
1500 { 0, 0, 0, 0 },
1503 int radeon_pm_init(struct radeon_device *rdev)
1505 struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
1506 bool disable_dpm = false;
1508 /* Apply dpm quirks */
1509 while (p && p->chip_device != 0) {
1510 if (rdev->pdev->vendor == p->chip_vendor &&
1511 rdev->pdev->device == p->chip_device &&
1512 rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1513 rdev->pdev->subsystem_device == p->subsys_device) {
1514 disable_dpm = true;
1515 break;
1517 ++p;
1520 /* enable dpm on rv6xx+ */
1521 switch (rdev->family) {
1522 case CHIP_RV610:
1523 case CHIP_RV630:
1524 case CHIP_RV620:
1525 case CHIP_RV635:
1526 case CHIP_RV670:
1527 case CHIP_RS780:
1528 case CHIP_RS880:
1529 case CHIP_RV770:
1530 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1531 if (!rdev->rlc_fw)
1532 rdev->pm.pm_method = PM_METHOD_PROFILE;
1533 else if ((rdev->family >= CHIP_RV770) &&
1534 (!(rdev->flags & RADEON_IS_IGP)) &&
1535 (!rdev->smc_fw))
1536 rdev->pm.pm_method = PM_METHOD_PROFILE;
1537 else if (radeon_dpm == 1)
1538 rdev->pm.pm_method = PM_METHOD_DPM;
1539 else
1540 rdev->pm.pm_method = PM_METHOD_PROFILE;
1541 break;
1542 case CHIP_RV730:
1543 case CHIP_RV710:
1544 case CHIP_RV740:
1545 case CHIP_CEDAR:
1546 case CHIP_REDWOOD:
1547 case CHIP_JUNIPER:
1548 case CHIP_CYPRESS:
1549 case CHIP_HEMLOCK:
1550 case CHIP_PALM:
1551 case CHIP_SUMO:
1552 case CHIP_SUMO2:
1553 case CHIP_BARTS:
1554 case CHIP_TURKS:
1555 case CHIP_CAICOS:
1556 case CHIP_CAYMAN:
1557 case CHIP_ARUBA:
1558 case CHIP_TAHITI:
1559 case CHIP_PITCAIRN:
1560 case CHIP_VERDE:
1561 case CHIP_OLAND:
1562 case CHIP_HAINAN:
1563 case CHIP_BONAIRE:
1564 case CHIP_KABINI:
1565 case CHIP_KAVERI:
1566 case CHIP_HAWAII:
1567 case CHIP_MULLINS:
1568 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1569 if (!rdev->rlc_fw)
1570 rdev->pm.pm_method = PM_METHOD_PROFILE;
1571 else if ((rdev->family >= CHIP_RV770) &&
1572 (!(rdev->flags & RADEON_IS_IGP)) &&
1573 (!rdev->smc_fw))
1574 rdev->pm.pm_method = PM_METHOD_PROFILE;
1575 else if (disable_dpm && (radeon_dpm == -1))
1576 rdev->pm.pm_method = PM_METHOD_PROFILE;
1577 else if (radeon_dpm == 0)
1578 rdev->pm.pm_method = PM_METHOD_PROFILE;
1579 else
1580 rdev->pm.pm_method = PM_METHOD_DPM;
1581 break;
1582 default:
1583 /* default to profile method */
1584 rdev->pm.pm_method = PM_METHOD_PROFILE;
1585 break;
1588 if (rdev->pm.pm_method == PM_METHOD_DPM)
1589 return radeon_pm_init_dpm(rdev);
1590 else
1591 return radeon_pm_init_old(rdev);
1594 int radeon_pm_late_init(struct radeon_device *rdev)
1596 int ret = 0;
1598 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1599 if (rdev->pm.dpm_enabled) {
1600 if (!rdev->pm.sysfs_initialized) {
1601 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1602 if (ret)
1603 DRM_ERROR("failed to create device file for dpm state\n");
1604 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1605 if (ret)
1606 DRM_ERROR("failed to create device file for dpm state\n");
1607 /* XXX: these are noops for dpm but are here for backwards compat */
1608 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1609 if (ret)
1610 DRM_ERROR("failed to create device file for power profile\n");
1611 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1612 if (ret)
1613 DRM_ERROR("failed to create device file for power method\n");
1614 rdev->pm.sysfs_initialized = true;
1617 mutex_lock(&rdev->pm.mutex);
1618 ret = radeon_dpm_late_enable(rdev);
1619 mutex_unlock(&rdev->pm.mutex);
1620 if (ret) {
1621 rdev->pm.dpm_enabled = false;
1622 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1623 } else {
1624 /* set the dpm state for PX since there won't be
1625 * a modeset to call this.
1627 radeon_pm_compute_clocks(rdev);
1630 } else {
1631 if ((rdev->pm.num_power_states > 1) &&
1632 (!rdev->pm.sysfs_initialized)) {
1633 /* where's the best place to put these? */
1634 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1635 if (ret)
1636 DRM_ERROR("failed to create device file for power profile\n");
1637 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1638 if (ret)
1639 DRM_ERROR("failed to create device file for power method\n");
1640 if (!ret)
1641 rdev->pm.sysfs_initialized = true;
1644 return ret;
1647 static void radeon_pm_fini_old(struct radeon_device *rdev)
1649 if (rdev->pm.num_power_states > 1) {
1650 mutex_lock(&rdev->pm.mutex);
1651 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1652 rdev->pm.profile = PM_PROFILE_DEFAULT;
1653 radeon_pm_update_profile(rdev);
1654 radeon_pm_set_clocks(rdev);
1655 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1656 /* reset default clocks */
1657 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1658 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1659 radeon_pm_set_clocks(rdev);
1661 mutex_unlock(&rdev->pm.mutex);
1663 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1665 device_remove_file(rdev->dev, &dev_attr_power_profile);
1666 device_remove_file(rdev->dev, &dev_attr_power_method);
1669 radeon_hwmon_fini(rdev);
1670 kfree(rdev->pm.power_state);
1673 static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1675 if (rdev->pm.num_power_states > 1) {
1676 mutex_lock(&rdev->pm.mutex);
1677 radeon_dpm_disable(rdev);
1678 mutex_unlock(&rdev->pm.mutex);
1680 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
1681 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1682 /* XXX backwards compat */
1683 device_remove_file(rdev->dev, &dev_attr_power_profile);
1684 device_remove_file(rdev->dev, &dev_attr_power_method);
1686 radeon_dpm_fini(rdev);
1688 radeon_hwmon_fini(rdev);
1689 kfree(rdev->pm.power_state);
1692 void radeon_pm_fini(struct radeon_device *rdev)
1694 if (rdev->pm.pm_method == PM_METHOD_DPM)
1695 radeon_pm_fini_dpm(rdev);
1696 else
1697 radeon_pm_fini_old(rdev);
1700 static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1702 struct drm_device *ddev = rdev->ddev;
1703 struct drm_crtc *crtc;
1704 struct radeon_crtc *radeon_crtc;
1706 if (rdev->pm.num_power_states < 2)
1707 return;
1709 mutex_lock(&rdev->pm.mutex);
1711 rdev->pm.active_crtcs = 0;
1712 rdev->pm.active_crtc_count = 0;
1713 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1714 list_for_each_entry(crtc,
1715 &ddev->mode_config.crtc_list, head) {
1716 radeon_crtc = to_radeon_crtc(crtc);
1717 if (radeon_crtc->enabled) {
1718 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1719 rdev->pm.active_crtc_count++;
1724 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1725 radeon_pm_update_profile(rdev);
1726 radeon_pm_set_clocks(rdev);
1727 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1728 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1729 if (rdev->pm.active_crtc_count > 1) {
1730 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1731 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1733 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1734 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1735 radeon_pm_get_dynpm_state(rdev);
1736 radeon_pm_set_clocks(rdev);
1738 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1740 } else if (rdev->pm.active_crtc_count == 1) {
1741 /* TODO: Increase clocks if needed for current mode */
1743 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1744 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1745 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1746 radeon_pm_get_dynpm_state(rdev);
1747 radeon_pm_set_clocks(rdev);
1749 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1750 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1751 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1752 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1753 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1754 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1755 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1757 } else { /* count == 0 */
1758 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1759 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1761 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1762 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1763 radeon_pm_get_dynpm_state(rdev);
1764 radeon_pm_set_clocks(rdev);
1770 mutex_unlock(&rdev->pm.mutex);
1773 static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1775 struct drm_device *ddev = rdev->ddev;
1776 struct drm_crtc *crtc;
1777 struct radeon_crtc *radeon_crtc;
1779 if (!rdev->pm.dpm_enabled)
1780 return;
1782 mutex_lock(&rdev->pm.mutex);
1784 /* update active crtc counts */
1785 rdev->pm.dpm.new_active_crtcs = 0;
1786 rdev->pm.dpm.new_active_crtc_count = 0;
1787 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1788 list_for_each_entry(crtc,
1789 &ddev->mode_config.crtc_list, head) {
1790 radeon_crtc = to_radeon_crtc(crtc);
1791 if (crtc->enabled) {
1792 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1793 rdev->pm.dpm.new_active_crtc_count++;
1798 /* update battery/ac status */
1799 if (power_supply_is_system_supplied() > 0)
1800 rdev->pm.dpm.ac_power = true;
1801 else
1802 rdev->pm.dpm.ac_power = false;
1804 radeon_dpm_change_power_state_locked(rdev);
1806 mutex_unlock(&rdev->pm.mutex);
1810 void radeon_pm_compute_clocks(struct radeon_device *rdev)
1812 if (rdev->pm.pm_method == PM_METHOD_DPM)
1813 radeon_pm_compute_clocks_dpm(rdev);
1814 else
1815 radeon_pm_compute_clocks_old(rdev);
1818 static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1820 int crtc, vpos, hpos, vbl_status;
1821 bool in_vbl = true;
1823 /* Iterate over all active crtc's. All crtc's must be in vblank,
1824 * otherwise return in_vbl == false.
1826 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1827 if (rdev->pm.active_crtcs & (1 << crtc)) {
1828 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
1829 crtc,
1830 USE_REAL_VBLANKSTART,
1831 &vpos, &hpos, NULL, NULL,
1832 &rdev->mode_info.crtcs[crtc]->base.hwmode);
1833 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1834 !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
1835 in_vbl = false;
1839 return in_vbl;
1842 static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1844 u32 stat_crtc = 0;
1845 bool in_vbl = radeon_pm_in_vbl(rdev);
1847 if (!in_vbl)
1848 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1849 finish ? "exit" : "entry");
1850 return in_vbl;
1853 static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1855 struct radeon_device *rdev;
1856 int resched;
1857 rdev = container_of(work, struct radeon_device,
1858 pm.dynpm_idle_work.work);
1860 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1861 mutex_lock(&rdev->pm.mutex);
1862 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1863 int not_processed = 0;
1864 int i;
1866 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
1867 struct radeon_ring *ring = &rdev->ring[i];
1869 if (ring->ready) {
1870 not_processed += radeon_fence_count_emitted(rdev, i);
1871 if (not_processed >= 3)
1872 break;
1876 if (not_processed >= 3) { /* should upclock */
1877 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1878 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1879 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1880 rdev->pm.dynpm_can_upclock) {
1881 rdev->pm.dynpm_planned_action =
1882 DYNPM_ACTION_UPCLOCK;
1883 rdev->pm.dynpm_action_timeout = jiffies +
1884 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1886 } else if (not_processed == 0) { /* should downclock */
1887 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1888 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1889 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1890 rdev->pm.dynpm_can_downclock) {
1891 rdev->pm.dynpm_planned_action =
1892 DYNPM_ACTION_DOWNCLOCK;
1893 rdev->pm.dynpm_action_timeout = jiffies +
1894 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1898 /* Note, radeon_pm_set_clocks is called with static_switch set
1899 * to false since we want to wait for vbl to avoid flicker.
1901 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1902 jiffies > rdev->pm.dynpm_action_timeout) {
1903 radeon_pm_get_dynpm_state(rdev);
1904 radeon_pm_set_clocks(rdev);
1907 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1908 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1910 mutex_unlock(&rdev->pm.mutex);
1911 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
1915 * Debugfs info
1917 #if defined(CONFIG_DEBUG_FS)
1919 static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1921 struct drm_info_node *node = (struct drm_info_node *) m->private;
1922 struct drm_device *dev = node->minor->dev;
1923 struct radeon_device *rdev = dev->dev_private;
1924 struct drm_device *ddev = rdev->ddev;
1926 if ((rdev->flags & RADEON_IS_PX) &&
1927 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1928 seq_printf(m, "PX asic powered off\n");
1929 } else if (rdev->pm.dpm_enabled) {
1930 mutex_lock(&rdev->pm.mutex);
1931 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1932 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1933 else
1934 seq_printf(m, "Debugfs support not implemented for this asic\n");
1935 mutex_unlock(&rdev->pm.mutex);
1936 } else {
1937 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1938 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1939 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1940 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1941 else
1942 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1943 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1944 if (rdev->asic->pm.get_memory_clock)
1945 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1946 if (rdev->pm.current_vddc)
1947 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1948 if (rdev->asic->pm.get_pcie_lanes)
1949 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1952 return 0;
1955 static struct drm_info_list radeon_pm_info_list[] = {
1956 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1958 #endif
1960 static int radeon_debugfs_pm_init(struct radeon_device *rdev)
1962 #if defined(CONFIG_DEBUG_FS)
1963 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1964 #else
1965 return 0;
1966 #endif