xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 914a89872be27db59546525ea96be817c7b2509c)
17433874eSRafał Miłecki /*
27433874eSRafał Miłecki  * Permission is hereby granted, free of charge, to any person obtaining a
37433874eSRafał Miłecki  * copy of this software and associated documentation files (the "Software"),
47433874eSRafał Miłecki  * to deal in the Software without restriction, including without limitation
57433874eSRafał Miłecki  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
67433874eSRafał Miłecki  * and/or sell copies of the Software, and to permit persons to whom the
77433874eSRafał Miłecki  * Software is furnished to do so, subject to the following conditions:
87433874eSRafał Miłecki  *
97433874eSRafał Miłecki  * The above copyright notice and this permission notice shall be included in
107433874eSRafał Miłecki  * all copies or substantial portions of the Software.
117433874eSRafał Miłecki  *
127433874eSRafał Miłecki  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
137433874eSRafał Miłecki  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147433874eSRafał Miłecki  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
157433874eSRafał Miłecki  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
167433874eSRafał Miłecki  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
177433874eSRafał Miłecki  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
187433874eSRafał Miłecki  * OTHER DEALINGS IN THE SOFTWARE.
197433874eSRafał Miłecki  *
207433874eSRafał Miłecki  * Authors: Rafał Miłecki <zajec5@gmail.com>
2156278a8eSAlex Deucher  *          Alex Deucher <alexdeucher@gmail.com>
227433874eSRafał Miłecki  */
23760285e7SDavid Howells #include <drm/drmP.h>
247433874eSRafał Miłecki #include "radeon.h"
25f735261bSDave Airlie #include "avivod.h"
268a83ec5eSAlex Deucher #include "atom.h"
27ce8f5370SAlex Deucher #include <linux/power_supply.h>
2821a8122aSAlex Deucher #include <linux/hwmon.h>
2921a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
307433874eSRafał Miłecki 
31c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
32c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3373a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
34c913e23aSRafał Miłecki 
35f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
36eb2c27a0SAlex Deucher 	"",
37f712d0c7SRafał Miłecki 	"Powersave",
38f712d0c7SRafał Miłecki 	"Battery",
39f712d0c7SRafał Miłecki 	"Balanced",
40f712d0c7SRafał Miłecki 	"Performance",
41f712d0c7SRafał Miłecki };
42f712d0c7SRafał Miłecki 
43ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
44c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
45ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
46ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
47ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
48ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
49ce8f5370SAlex Deucher 
50a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
51a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
52a4c9e2eeSAlex Deucher 			     int instance)
53a4c9e2eeSAlex Deucher {
54a4c9e2eeSAlex Deucher 	int i;
55a4c9e2eeSAlex Deucher 	int found_instance = -1;
56a4c9e2eeSAlex Deucher 
57a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
58a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
59a4c9e2eeSAlex Deucher 			found_instance++;
60a4c9e2eeSAlex Deucher 			if (found_instance == instance)
61a4c9e2eeSAlex Deucher 				return i;
62a4c9e2eeSAlex Deucher 		}
63a4c9e2eeSAlex Deucher 	}
64a4c9e2eeSAlex Deucher 	/* return default if no match */
65a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
66a4c9e2eeSAlex Deucher }
67a4c9e2eeSAlex Deucher 
68c4917074SAlex Deucher void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
69ce8f5370SAlex Deucher {
701c71bda0SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
711c71bda0SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
721c71bda0SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
731c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = true;
741c71bda0SAlex Deucher 		else
751c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = false;
761c71bda0SAlex Deucher 		if (rdev->asic->dpm.enable_bapm)
771c71bda0SAlex Deucher 			radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
781c71bda0SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
791c71bda0SAlex Deucher         } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
80ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
81ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
82ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
83ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
84ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
85ce8f5370SAlex Deucher 		}
86ce8f5370SAlex Deucher 	}
87ce8f5370SAlex Deucher }
88ce8f5370SAlex Deucher 
89ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
90ce8f5370SAlex Deucher {
91ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
92ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
93ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
94ce8f5370SAlex Deucher 		break;
95ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
96ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
97ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
98ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
99ce8f5370SAlex Deucher 			else
100ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
101ce8f5370SAlex Deucher 		} else {
102ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
103c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
104ce8f5370SAlex Deucher 			else
105c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
106ce8f5370SAlex Deucher 		}
107ce8f5370SAlex Deucher 		break;
108ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
109ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
110ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
111ce8f5370SAlex Deucher 		else
112ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
113ce8f5370SAlex Deucher 		break;
114c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
115c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
116c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
117c9e75b21SAlex Deucher 		else
118c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
119c9e75b21SAlex Deucher 		break;
120ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
121ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
122ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
123ce8f5370SAlex Deucher 		else
124ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
125ce8f5370SAlex Deucher 		break;
126ce8f5370SAlex Deucher 	}
127ce8f5370SAlex Deucher 
128ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
129ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
130ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
131ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
132ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
133ce8f5370SAlex Deucher 	} else {
134ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
135ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
136ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
137ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
138ce8f5370SAlex Deucher 	}
139ce8f5370SAlex Deucher }
140c913e23aSRafał Miłecki 
1415876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1425876dd24SMatthew Garrett {
1435876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1445876dd24SMatthew Garrett 
1455876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1465876dd24SMatthew Garrett 		return;
1475876dd24SMatthew Garrett 
1485876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1495876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1505876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1515876dd24SMatthew Garrett 	}
1525876dd24SMatthew Garrett }
1535876dd24SMatthew Garrett 
154ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
155ce8f5370SAlex Deucher {
156ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
157ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
158ce8f5370SAlex Deucher 		wait_event_timeout(
159ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
160ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
161ce8f5370SAlex Deucher 	}
162ce8f5370SAlex Deucher }
163ce8f5370SAlex Deucher 
164ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
165ce8f5370SAlex Deucher {
166ce8f5370SAlex Deucher 	u32 sclk, mclk;
16792645879SAlex Deucher 	bool misc_after = false;
168ce8f5370SAlex Deucher 
169ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
170ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
171ce8f5370SAlex Deucher 		return;
172ce8f5370SAlex Deucher 
173ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
174ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
175ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1769ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1779ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
178ce8f5370SAlex Deucher 
17927810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
18027810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1817ae764b1SAlex Deucher 		 * mclk and vddci.
18227810fb2SAlex Deucher 		 */
18327810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
18427810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
18527810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
18627810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
18727810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
18827810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
18927810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
19027810fb2SAlex Deucher 		else
191ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
192ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
19327810fb2SAlex Deucher 
1949ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1959ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
196ce8f5370SAlex Deucher 
19792645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
19892645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
19992645879SAlex Deucher 			misc_after = true;
20092645879SAlex Deucher 
20192645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
20292645879SAlex Deucher 
20392645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
20492645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
20592645879SAlex Deucher 				return;
20692645879SAlex Deucher 		}
20792645879SAlex Deucher 
20892645879SAlex Deucher 		radeon_pm_prepare(rdev);
20992645879SAlex Deucher 
21092645879SAlex Deucher 		if (!misc_after)
211ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
212ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
213ce8f5370SAlex Deucher 
214ce8f5370SAlex Deucher 		/* set engine clock */
215ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
216ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
217ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
218ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
219ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
220d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
221ce8f5370SAlex Deucher 		}
222ce8f5370SAlex Deucher 
223ce8f5370SAlex Deucher 		/* set memory clock */
224798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
225ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
226ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
227ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
228ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
229d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
230ce8f5370SAlex Deucher 		}
23192645879SAlex Deucher 
23292645879SAlex Deucher 		if (misc_after)
23392645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
23492645879SAlex Deucher 			radeon_pm_misc(rdev);
23592645879SAlex Deucher 
236ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
237ce8f5370SAlex Deucher 
238ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
239ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
240ce8f5370SAlex Deucher 	} else
241d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
242ce8f5370SAlex Deucher }
243ce8f5370SAlex Deucher 
244ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
245a424816fSAlex Deucher {
2465f8f635eSJerome Glisse 	int i, r;
2472aba631cSMatthew Garrett 
2484e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2494e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2504e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2514e186b2dSAlex Deucher 		return;
2524e186b2dSAlex Deucher 
253612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
254db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
255d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2564f3218cbSAlex Deucher 
25795f5a3acSAlex Deucher 	/* wait for the rings to drain */
25895f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
25995f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2605f8f635eSJerome Glisse 		if (!ring->ready) {
2615f8f635eSJerome Glisse 			continue;
2625f8f635eSJerome Glisse 		}
2635f8f635eSJerome Glisse 		r = radeon_fence_wait_empty_locked(rdev, i);
2645f8f635eSJerome Glisse 		if (r) {
2655f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2665f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2675f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2685f8f635eSJerome Glisse 			mutex_unlock(&rdev->ddev->struct_mutex);
2695f8f635eSJerome Glisse 			return;
2705f8f635eSJerome Glisse 		}
271ce8f5370SAlex Deucher 	}
27295f5a3acSAlex Deucher 
2735876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2745876dd24SMatthew Garrett 
275ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2762aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2772aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2782aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2792aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2802aba631cSMatthew Garrett 			}
2812aba631cSMatthew Garrett 		}
2822aba631cSMatthew Garrett 	}
2832aba631cSMatthew Garrett 
284ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2852aba631cSMatthew Garrett 
286ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2872aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2882aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2892aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2902aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2912aba631cSMatthew Garrett 			}
2922aba631cSMatthew Garrett 		}
2932aba631cSMatthew Garrett 	}
294a424816fSAlex Deucher 
295a424816fSAlex Deucher 	/* update display watermarks based on new power state */
296a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
297a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
298a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
299a424816fSAlex Deucher 
300ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3012aba631cSMatthew Garrett 
302d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
303db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
304612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
305a424816fSAlex Deucher }
306a424816fSAlex Deucher 
307f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
308f712d0c7SRafał Miłecki {
309f712d0c7SRafał Miłecki 	int i, j;
310f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
311f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
312f712d0c7SRafał Miłecki 
313d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
314f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
315f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
316d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
317f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
318f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
319d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
320f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
321d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
322f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
323d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
324d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
325f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
326f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
327f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
328eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
329f712d0c7SRafał Miłecki 						 j,
330eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
331f712d0c7SRafał Miłecki 			else
332eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
333f712d0c7SRafał Miłecki 						 j,
334f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
335f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
336eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
337f712d0c7SRafał Miłecki 		}
338f712d0c7SRafał Miłecki 	}
339f712d0c7SRafał Miłecki }
340f712d0c7SRafał Miłecki 
341ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
342a424816fSAlex Deucher 				     struct device_attribute *attr,
343a424816fSAlex Deucher 				     char *buf)
344a424816fSAlex Deucher {
3453e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
346a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
347ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
348a424816fSAlex Deucher 
349a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
350ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
351ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
35212e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
353ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
354a424816fSAlex Deucher }
355a424816fSAlex Deucher 
356ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
357a424816fSAlex Deucher 				     struct device_attribute *attr,
358a424816fSAlex Deucher 				     const char *buf,
359a424816fSAlex Deucher 				     size_t count)
360a424816fSAlex Deucher {
3613e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
362a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
363a424816fSAlex Deucher 
364a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
365ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
366ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
367ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
368ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
369ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
370ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
371ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
372c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
373c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
374ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
375ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
376ce8f5370SAlex Deucher 		else {
3771783e4bfSThomas Renninger 			count = -EINVAL;
378ce8f5370SAlex Deucher 			goto fail;
379ce8f5370SAlex Deucher 		}
380ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
381ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3821783e4bfSThomas Renninger 	} else
3831783e4bfSThomas Renninger 		count = -EINVAL;
3841783e4bfSThomas Renninger 
385ce8f5370SAlex Deucher fail:
386a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
387a424816fSAlex Deucher 
388a424816fSAlex Deucher 	return count;
389a424816fSAlex Deucher }
390a424816fSAlex Deucher 
391ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
392ce8f5370SAlex Deucher 				    struct device_attribute *attr,
393ce8f5370SAlex Deucher 				    char *buf)
39456278a8eSAlex Deucher {
3953e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
396ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
397ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
39856278a8eSAlex Deucher 
399ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
400da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
401da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
40256278a8eSAlex Deucher }
40356278a8eSAlex Deucher 
404ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
405ce8f5370SAlex Deucher 				    struct device_attribute *attr,
406ce8f5370SAlex Deucher 				    const char *buf,
407ce8f5370SAlex Deucher 				    size_t count)
408d0d6cb81SRafał Miłecki {
4093e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
410ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
411ce8f5370SAlex Deucher 
412da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
413da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
414da321c8aSAlex Deucher 		count = -EINVAL;
415da321c8aSAlex Deucher 		goto fail;
416da321c8aSAlex Deucher 	}
417ce8f5370SAlex Deucher 
418ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
419ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
420ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
421ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
422ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
423ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
424ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
425ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
426ce8f5370SAlex Deucher 		/* disable dynpm */
427ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
428ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4293f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
430ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
43132c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
432ce8f5370SAlex Deucher 	} else {
4331783e4bfSThomas Renninger 		count = -EINVAL;
434ce8f5370SAlex Deucher 		goto fail;
435d0d6cb81SRafał Miłecki 	}
436ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
437ce8f5370SAlex Deucher fail:
438ce8f5370SAlex Deucher 	return count;
439ce8f5370SAlex Deucher }
440ce8f5370SAlex Deucher 
441da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
442da321c8aSAlex Deucher 				    struct device_attribute *attr,
443da321c8aSAlex Deucher 				    char *buf)
444da321c8aSAlex Deucher {
4453e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
446da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
447da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
448da321c8aSAlex Deucher 
449da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
450da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
451da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
452da321c8aSAlex Deucher }
453da321c8aSAlex Deucher 
454da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
455da321c8aSAlex Deucher 				    struct device_attribute *attr,
456da321c8aSAlex Deucher 				    const char *buf,
457da321c8aSAlex Deucher 				    size_t count)
458da321c8aSAlex Deucher {
4593e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
460da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
461da321c8aSAlex Deucher 
462da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
463da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
464da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
465da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
466da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
467da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
468da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
469da321c8aSAlex Deucher 	else {
470da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
471da321c8aSAlex Deucher 		count = -EINVAL;
472da321c8aSAlex Deucher 		goto fail;
473da321c8aSAlex Deucher 	}
474da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
475da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
476da321c8aSAlex Deucher fail:
477da321c8aSAlex Deucher 	return count;
478da321c8aSAlex Deucher }
479da321c8aSAlex Deucher 
48070d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
48170d01a5eSAlex Deucher 						       struct device_attribute *attr,
48270d01a5eSAlex Deucher 						       char *buf)
48370d01a5eSAlex Deucher {
4843e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
48570d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
48670d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
48770d01a5eSAlex Deucher 
48870d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
48970d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
49070d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
49170d01a5eSAlex Deucher }
49270d01a5eSAlex Deucher 
49370d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
49470d01a5eSAlex Deucher 						       struct device_attribute *attr,
49570d01a5eSAlex Deucher 						       const char *buf,
49670d01a5eSAlex Deucher 						       size_t count)
49770d01a5eSAlex Deucher {
4983e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
49970d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
50070d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
50170d01a5eSAlex Deucher 	int ret = 0;
50270d01a5eSAlex Deucher 
50370d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
50470d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
50570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
50670d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
50770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
50870d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
50970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
51070d01a5eSAlex Deucher 	} else {
51170d01a5eSAlex Deucher 		count = -EINVAL;
51270d01a5eSAlex Deucher 		goto fail;
51370d01a5eSAlex Deucher 	}
51470d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5150a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5160a17af37SAlex Deucher 			count = -EINVAL;
5170a17af37SAlex Deucher 			goto fail;
5180a17af37SAlex Deucher 		}
51970d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
52070d01a5eSAlex Deucher 		if (ret)
52170d01a5eSAlex Deucher 			count = -EINVAL;
52270d01a5eSAlex Deucher 	}
52370d01a5eSAlex Deucher fail:
5240a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5250a17af37SAlex Deucher 
52670d01a5eSAlex Deucher 	return count;
52770d01a5eSAlex Deucher }
52870d01a5eSAlex Deucher 
529ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
530ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
531da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
53270d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
53370d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
53470d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
535ce8f5370SAlex Deucher 
53621a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
53721a8122aSAlex Deucher 				      struct device_attribute *attr,
53821a8122aSAlex Deucher 				      char *buf)
53921a8122aSAlex Deucher {
540ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
54120d391d7SAlex Deucher 	int temp;
54221a8122aSAlex Deucher 
5436bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
5446bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
5456bd1c385SAlex Deucher 	else
54621a8122aSAlex Deucher 		temp = 0;
54721a8122aSAlex Deucher 
54821a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
54921a8122aSAlex Deucher }
55021a8122aSAlex Deucher 
5516ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
5526ea4e84dSJean Delvare 					     struct device_attribute *attr,
5536ea4e84dSJean Delvare 					     char *buf)
5546ea4e84dSJean Delvare {
555e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
5566ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
5576ea4e84dSJean Delvare 	int temp;
5586ea4e84dSJean Delvare 
5596ea4e84dSJean Delvare 	if (hyst)
5606ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
5616ea4e84dSJean Delvare 	else
5626ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
5636ea4e84dSJean Delvare 
5646ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
5656ea4e84dSJean Delvare }
5666ea4e84dSJean Delvare 
56721a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
5686ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
5696ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
57021a8122aSAlex Deucher 
57121a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
57221a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
5736ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
5746ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
57521a8122aSAlex Deucher 	NULL
57621a8122aSAlex Deucher };
57721a8122aSAlex Deucher 
5786ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
5796ea4e84dSJean Delvare 					struct attribute *attr, int index)
5806ea4e84dSJean Delvare {
5816ea4e84dSJean Delvare 	struct device *dev = container_of(kobj, struct device, kobj);
582e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
5836ea4e84dSJean Delvare 
5846ea4e84dSJean Delvare 	/* Skip limit attributes if DPM is not enabled */
5856ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
5866ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
5876ea4e84dSJean Delvare 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
5886ea4e84dSJean Delvare 		return 0;
5896ea4e84dSJean Delvare 
5906ea4e84dSJean Delvare 	return attr->mode;
5916ea4e84dSJean Delvare }
5926ea4e84dSJean Delvare 
59321a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
59421a8122aSAlex Deucher 	.attrs = hwmon_attributes,
5956ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
59621a8122aSAlex Deucher };
59721a8122aSAlex Deucher 
598ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
599ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
600ec39f64bSGuenter Roeck 	NULL
601ec39f64bSGuenter Roeck };
602ec39f64bSGuenter Roeck 
6030d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
60421a8122aSAlex Deucher {
6050d18abedSDan Carpenter 	int err = 0;
606ec39f64bSGuenter Roeck 	struct device *hwmon_dev;
60721a8122aSAlex Deucher 
60821a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
60921a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
61021a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
61121a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
612457558edSAlex Deucher 	case THERMAL_TYPE_NI:
613e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
6141bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
615286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
616286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
6176bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
6185d7486c7SAlex Deucher 			return err;
619ec39f64bSGuenter Roeck 		hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
620ec39f64bSGuenter Roeck 							      "radeon", rdev,
621ec39f64bSGuenter Roeck 							      hwmon_groups);
622ec39f64bSGuenter Roeck 		if (IS_ERR(hwmon_dev)) {
623ec39f64bSGuenter Roeck 			err = PTR_ERR(hwmon_dev);
6240d18abedSDan Carpenter 			dev_err(rdev->dev,
6250d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
6260d18abedSDan Carpenter 		}
62721a8122aSAlex Deucher 		break;
62821a8122aSAlex Deucher 	default:
62921a8122aSAlex Deucher 		break;
63021a8122aSAlex Deucher 	}
6310d18abedSDan Carpenter 
6320d18abedSDan Carpenter 	return err;
63321a8122aSAlex Deucher }
63421a8122aSAlex Deucher 
635da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
636da321c8aSAlex Deucher {
637da321c8aSAlex Deucher 	struct radeon_device *rdev =
638da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
639da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
640da321c8aSAlex Deucher 	/* switch to the thermal state */
641da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
642da321c8aSAlex Deucher 
643da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
644da321c8aSAlex Deucher 		return;
645da321c8aSAlex Deucher 
646da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
647da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
648da321c8aSAlex Deucher 
649da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
650da321c8aSAlex Deucher 			/* switch back the user state */
651da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
652da321c8aSAlex Deucher 	} else {
653da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
654da321c8aSAlex Deucher 			/* switch back the user state */
655da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
656da321c8aSAlex Deucher 	}
65760320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
65860320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
65960320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
66060320347SAlex Deucher 	else
66160320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
66260320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
66360320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
66460320347SAlex Deucher 
66560320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
666da321c8aSAlex Deucher }
667da321c8aSAlex Deucher 
668da321c8aSAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
669da321c8aSAlex Deucher 						     enum radeon_pm_state_type dpm_state)
670da321c8aSAlex Deucher {
671da321c8aSAlex Deucher 	int i;
672da321c8aSAlex Deucher 	struct radeon_ps *ps;
673da321c8aSAlex Deucher 	u32 ui_class;
67448783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
67548783069SAlex Deucher 		true : false;
67648783069SAlex Deucher 
67748783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
67848783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
67948783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
68048783069SAlex Deucher 			single_display = false;
68148783069SAlex Deucher 	}
682da321c8aSAlex Deucher 
683edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
684edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
685edcaa5b1SAlex Deucher 	 */
686edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
687edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
688da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
689da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
690da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
691da321c8aSAlex Deucher 
692edcaa5b1SAlex Deucher restart_search:
693da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
694da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
695da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
696da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
697da321c8aSAlex Deucher 		switch (dpm_state) {
698da321c8aSAlex Deucher 		/* user states */
699da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
700da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
701da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
70248783069SAlex Deucher 					if (single_display)
703da321c8aSAlex Deucher 						return ps;
704da321c8aSAlex Deucher 				} else
705da321c8aSAlex Deucher 					return ps;
706da321c8aSAlex Deucher 			}
707da321c8aSAlex Deucher 			break;
708da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
709da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
710da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
71148783069SAlex Deucher 					if (single_display)
712da321c8aSAlex Deucher 						return ps;
713da321c8aSAlex Deucher 				} else
714da321c8aSAlex Deucher 					return ps;
715da321c8aSAlex Deucher 			}
716da321c8aSAlex Deucher 			break;
717da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
718da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
719da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
72048783069SAlex Deucher 					if (single_display)
721da321c8aSAlex Deucher 						return ps;
722da321c8aSAlex Deucher 				} else
723da321c8aSAlex Deucher 					return ps;
724da321c8aSAlex Deucher 			}
725da321c8aSAlex Deucher 			break;
726da321c8aSAlex Deucher 		/* internal states */
727da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
728d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
729da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
730d4d3278cSAlex Deucher 			else
731d4d3278cSAlex Deucher 				break;
732da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
733da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
734da321c8aSAlex Deucher 				return ps;
735da321c8aSAlex Deucher 			break;
736da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
737da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
738da321c8aSAlex Deucher 				return ps;
739da321c8aSAlex Deucher 			break;
740da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
741da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
742da321c8aSAlex Deucher 				return ps;
743da321c8aSAlex Deucher 			break;
744da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
745da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
746da321c8aSAlex Deucher 				return ps;
747da321c8aSAlex Deucher 			break;
748da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
749da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
750da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
751da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
752da321c8aSAlex Deucher 				return ps;
753da321c8aSAlex Deucher 			break;
754da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
755da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
756da321c8aSAlex Deucher 				return ps;
757da321c8aSAlex Deucher 			break;
758da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
759da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
760da321c8aSAlex Deucher 				return ps;
761da321c8aSAlex Deucher 			break;
762edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
763edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
764edcaa5b1SAlex Deucher 				return ps;
765edcaa5b1SAlex Deucher 			break;
766da321c8aSAlex Deucher 		default:
767da321c8aSAlex Deucher 			break;
768da321c8aSAlex Deucher 		}
769da321c8aSAlex Deucher 	}
770da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
771da321c8aSAlex Deucher 	switch (dpm_state) {
772da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
773ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
774ce3537d5SAlex Deucher 		goto restart_search;
775da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
776da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
777da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
778d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
779da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
780d4d3278cSAlex Deucher 		} else {
781d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
782d4d3278cSAlex Deucher 			goto restart_search;
783d4d3278cSAlex Deucher 		}
784da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
785da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
786da321c8aSAlex Deucher 		goto restart_search;
787da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
788da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
789da321c8aSAlex Deucher 		goto restart_search;
790da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
791edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
792edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
793da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
794da321c8aSAlex Deucher 		goto restart_search;
795da321c8aSAlex Deucher 	default:
796da321c8aSAlex Deucher 		break;
797da321c8aSAlex Deucher 	}
798da321c8aSAlex Deucher 
799da321c8aSAlex Deucher 	return NULL;
800da321c8aSAlex Deucher }
801da321c8aSAlex Deucher 
802da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
803da321c8aSAlex Deucher {
804da321c8aSAlex Deucher 	int i;
805da321c8aSAlex Deucher 	struct radeon_ps *ps;
806da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
80784dd1928SAlex Deucher 	int ret;
808da321c8aSAlex Deucher 
809da321c8aSAlex Deucher 	/* if dpm init failed */
810da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
811da321c8aSAlex Deucher 		return;
812da321c8aSAlex Deucher 
813da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
814da321c8aSAlex Deucher 		/* add other state override checks here */
8158a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
8168a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
817da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
818da321c8aSAlex Deucher 	}
819da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
820da321c8aSAlex Deucher 
821da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
822da321c8aSAlex Deucher 	if (ps)
82389c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
824da321c8aSAlex Deucher 	else
825da321c8aSAlex Deucher 		return;
826da321c8aSAlex Deucher 
827d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
828da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
829d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
830d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
831d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
832d22b7e40SAlex Deucher 			 */
833da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
834d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
835da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
836da321c8aSAlex Deucher 				/* update displays */
837da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
838da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
839da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
840da321c8aSAlex Deucher 			}
841da321c8aSAlex Deucher 			return;
842d22b7e40SAlex Deucher 		} else {
843d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
844d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
845d22b7e40SAlex Deucher 			 * update display configuration.
846d22b7e40SAlex Deucher 			 */
847d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
848d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
849d22b7e40SAlex Deucher 				return;
850d22b7e40SAlex Deucher 			} else {
851d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
852d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
853d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
854d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
855d22b7e40SAlex Deucher 					/* update displays */
856d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
857d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
858d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
859d22b7e40SAlex Deucher 					return;
860d22b7e40SAlex Deucher 				}
861d22b7e40SAlex Deucher 			}
862d22b7e40SAlex Deucher 		}
863da321c8aSAlex Deucher 	}
864da321c8aSAlex Deucher 
865033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
866da321c8aSAlex Deucher 		printk("switching from power state:\n");
867da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
868da321c8aSAlex Deucher 		printk("switching to power state:\n");
869da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
870033a37dfSAlex Deucher 	}
871da321c8aSAlex Deucher 	mutex_lock(&rdev->ddev->struct_mutex);
872da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
873da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
874da321c8aSAlex Deucher 
87584dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
87684dd1928SAlex Deucher 	if (ret)
87784dd1928SAlex Deucher 		goto done;
87884dd1928SAlex Deucher 
879da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
880da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
881da321c8aSAlex Deucher 	/* update displays */
882da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
883da321c8aSAlex Deucher 
884da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
885da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
886da321c8aSAlex Deucher 
887da321c8aSAlex Deucher 	/* wait for the rings to drain */
888da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
889da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
890da321c8aSAlex Deucher 		if (ring->ready)
891da321c8aSAlex Deucher 			radeon_fence_wait_empty_locked(rdev, i);
892da321c8aSAlex Deucher 	}
893da321c8aSAlex Deucher 
894da321c8aSAlex Deucher 	/* program the new power state */
895da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
896da321c8aSAlex Deucher 
897da321c8aSAlex Deucher 	/* update current power state */
898da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
899da321c8aSAlex Deucher 
90084dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
90184dd1928SAlex Deucher 
9021cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
90314ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
90414ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
90560320347SAlex Deucher 			/* force low perf level for thermal */
90660320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
90714ac88afSAlex Deucher 			/* save the user's level */
90814ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
90914ac88afSAlex Deucher 		} else {
91014ac88afSAlex Deucher 			/* otherwise, user selected level */
91114ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
91214ac88afSAlex Deucher 		}
91360320347SAlex Deucher 	}
91460320347SAlex Deucher 
91584dd1928SAlex Deucher done:
916da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
917da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
918da321c8aSAlex Deucher 	mutex_unlock(&rdev->ddev->struct_mutex);
919da321c8aSAlex Deucher }
920da321c8aSAlex Deucher 
921ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
922ce3537d5SAlex Deucher {
923ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
924ce3537d5SAlex Deucher 
9259e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
9269e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
9279e9d9762SAlex Deucher 		/* enable/disable UVD */
9289e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
9299e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
9309e9d9762SAlex Deucher 	} else {
931ce3537d5SAlex Deucher 		if (enable) {
932ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
933ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
934dca5086aSAlex Deucher 			/* disable this for now */
935dca5086aSAlex Deucher #if 0
936ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
937ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
938ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
939ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
940ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
941ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
942ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
943ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
944ce3537d5SAlex Deucher 			else
945dca5086aSAlex Deucher #endif
946ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
947ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
948ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
949ce3537d5SAlex Deucher 		} else {
950ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
951ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
952ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
953ce3537d5SAlex Deucher 		}
954ce3537d5SAlex Deucher 
955ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
956ce3537d5SAlex Deucher 	}
9579e9d9762SAlex Deucher }
958ce3537d5SAlex Deucher 
959da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
960ce8f5370SAlex Deucher {
961ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
9623f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
9633f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
9643f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
9653f53eb6fSRafael J. Wysocki 	}
966ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
96732c87fcaSTejun Heo 
96832c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
969ce8f5370SAlex Deucher }
970ce8f5370SAlex Deucher 
971da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
972da321c8aSAlex Deucher {
973da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
974da321c8aSAlex Deucher 	/* disable dpm */
975da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
976da321c8aSAlex Deucher 	/* reset the power state */
977da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
978da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
979da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
980da321c8aSAlex Deucher }
981da321c8aSAlex Deucher 
982da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
983da321c8aSAlex Deucher {
984da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
985da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
986da321c8aSAlex Deucher 	else
987da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
988da321c8aSAlex Deucher }
989da321c8aSAlex Deucher 
990da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
991ce8f5370SAlex Deucher {
992ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
9932e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
99436099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
9952e3b3b10SAlex Deucher 	    rdev->mc_fw) {
996ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
9978a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
9988a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
9992feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
10002feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
10012feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1002ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1003ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1004ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1005ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1006ed18a360SAlex Deucher 	}
1007f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1008f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1009f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1010f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
10119ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
10129ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
10134d60173fSAlex Deucher 	rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
10142feea49aSAlex Deucher 	rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
10153f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
10163f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
10173f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
101832c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
10193f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
10203f53eb6fSRafael J. Wysocki 	}
1021f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1022ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1023d0d6cb81SRafał Miłecki }
1024d0d6cb81SRafał Miłecki 
1025da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
10267433874eSRafał Miłecki {
102726481fb1SDave Airlie 	int ret;
10280d18abedSDan Carpenter 
1029da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1030da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1031da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1032da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1033da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1034da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1035da321c8aSAlex Deucher 	if (ret) {
1036da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm resume failed\n");
1037da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
103836099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
1039da321c8aSAlex Deucher 		    rdev->mc_fw) {
1040da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
1041da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1042da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
1043da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
1044da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1045da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1046da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
1047da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1048da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
1049da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1050da321c8aSAlex Deucher 		}
1051da321c8aSAlex Deucher 	} else {
1052da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = true;
1053da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
1054da321c8aSAlex Deucher 	}
1055da321c8aSAlex Deucher }
1056da321c8aSAlex Deucher 
1057da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1058da321c8aSAlex Deucher {
1059da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1060da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1061da321c8aSAlex Deucher 	else
1062da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1063da321c8aSAlex Deucher }
1064da321c8aSAlex Deucher 
1065da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1066da321c8aSAlex Deucher {
1067da321c8aSAlex Deucher 	int ret;
1068da321c8aSAlex Deucher 
1069f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1070ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1071ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1072ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1073ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
10749ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
10759ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1076f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1077f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
107821a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1079c913e23aSRafał Miłecki 
108056278a8eSAlex Deucher 	if (rdev->bios) {
108156278a8eSAlex Deucher 		if (rdev->is_atom_bios)
108256278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
108356278a8eSAlex Deucher 		else
108456278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1085f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1086ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1087ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
10882e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
108936099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
10902e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1091ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
10928a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
10938a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
10944639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
10954639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
10964639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1097ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1098ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1099ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1100ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1101ed18a360SAlex Deucher 		}
110256278a8eSAlex Deucher 	}
110356278a8eSAlex Deucher 
110421a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
11050d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
11060d18abedSDan Carpenter 	if (ret)
11070d18abedSDan Carpenter 		return ret;
110832c87fcaSTejun Heo 
110932c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
111032c87fcaSTejun Heo 
1111ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1112ce8f5370SAlex Deucher 		/* where's the best place to put these? */
111326481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
111426481fb1SDave Airlie 		if (ret)
111526481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
111626481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
111726481fb1SDave Airlie 		if (ret)
111826481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
1119ce8f5370SAlex Deucher 
11207433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1121c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
11227433874eSRafał Miłecki 		}
11237433874eSRafał Miłecki 
1124c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1125ce8f5370SAlex Deucher 	}
1126c913e23aSRafał Miłecki 
11277433874eSRafał Miłecki 	return 0;
11287433874eSRafał Miłecki }
11297433874eSRafał Miłecki 
1130da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1131da321c8aSAlex Deucher {
1132da321c8aSAlex Deucher 	int i;
1133da321c8aSAlex Deucher 
1134da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1135da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1136da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1137da321c8aSAlex Deucher 	}
1138da321c8aSAlex Deucher }
1139da321c8aSAlex Deucher 
1140da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1141da321c8aSAlex Deucher {
1142da321c8aSAlex Deucher 	int ret;
1143da321c8aSAlex Deucher 
11441cd8b21aSAlex Deucher 	/* default to balanced state */
1145edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1146edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
11471cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1148da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1149da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1150da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1151da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1152da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1153da321c8aSAlex Deucher 
1154da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1155da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1156da321c8aSAlex Deucher 	else
1157da321c8aSAlex Deucher 		return -EINVAL;
1158da321c8aSAlex Deucher 
1159da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1160da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1161da321c8aSAlex Deucher 	if (ret)
1162da321c8aSAlex Deucher 		return ret;
1163da321c8aSAlex Deucher 
1164da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1165da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1166da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1167da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1168033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1169da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1170da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1171da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1172da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1173da321c8aSAlex Deucher 	if (ret) {
1174da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = false;
1175da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
117636099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
1177da321c8aSAlex Deucher 		    rdev->mc_fw) {
1178da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
1179da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1180da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
1181da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
1182da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1183da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1184da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
1185da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1186da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
1187da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1188da321c8aSAlex Deucher 		}
1189da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm initialization failed\n");
1190da321c8aSAlex Deucher 		return ret;
1191da321c8aSAlex Deucher 	}
1192da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1193da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
1194da321c8aSAlex Deucher 
1195da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1196da321c8aSAlex Deucher 	if (ret)
1197da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
119870d01a5eSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
119970d01a5eSAlex Deucher 	if (ret)
120070d01a5eSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
1201da321c8aSAlex Deucher 	/* XXX: these are noops for dpm but are here for backwards compat */
1202da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1203da321c8aSAlex Deucher 	if (ret)
1204da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power profile\n");
1205da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_method);
1206da321c8aSAlex Deucher 	if (ret)
1207da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power method\n");
12081316b792SAlex Deucher 
12091316b792SAlex Deucher 	if (radeon_debugfs_pm_init(rdev)) {
12101316b792SAlex Deucher 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
12111316b792SAlex Deucher 	}
12121316b792SAlex Deucher 
1213da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1214da321c8aSAlex Deucher 
1215da321c8aSAlex Deucher 	return 0;
1216da321c8aSAlex Deucher }
1217da321c8aSAlex Deucher 
1218da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1219da321c8aSAlex Deucher {
1220da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1221da321c8aSAlex Deucher 	switch (rdev->family) {
12224a6369e9SAlex Deucher 	case CHIP_RV610:
12234a6369e9SAlex Deucher 	case CHIP_RV630:
12244a6369e9SAlex Deucher 	case CHIP_RV620:
12254a6369e9SAlex Deucher 	case CHIP_RV635:
12264a6369e9SAlex Deucher 	case CHIP_RV670:
12279d67006eSAlex Deucher 	case CHIP_RS780:
12289d67006eSAlex Deucher 	case CHIP_RS880:
122969e0b57aSAlex Deucher 	case CHIP_CAYMAN:
1230cc8dbbb4SAlex Deucher 	case CHIP_BONAIRE:
123141a524abSAlex Deucher 	case CHIP_KABINI:
123241a524abSAlex Deucher 	case CHIP_KAVERI:
12332d40038dSAlex Deucher 	case CHIP_HAWAII:
12348a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1235761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1236761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12378a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
12388a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
12398a53fa23SAlex Deucher 			 (!rdev->smc_fw))
12408a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1241761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
12429d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
12439d67006eSAlex Deucher 		else
12449d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12459d67006eSAlex Deucher 		break;
1246ab70b1ddSAlex Deucher 	case CHIP_RV770:
1247ab70b1ddSAlex Deucher 	case CHIP_RV730:
1248ab70b1ddSAlex Deucher 	case CHIP_RV710:
1249ab70b1ddSAlex Deucher 	case CHIP_RV740:
125059f7a2f2SAlex Deucher 	case CHIP_CEDAR:
125159f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
125259f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
125359f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
125459f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
12555a16f761SAlex Deucher 	case CHIP_PALM:
12565a16f761SAlex Deucher 	case CHIP_SUMO:
12575a16f761SAlex Deucher 	case CHIP_SUMO2:
125856684ec5SAlex Deucher 	case CHIP_BARTS:
125956684ec5SAlex Deucher 	case CHIP_TURKS:
126056684ec5SAlex Deucher 	case CHIP_CAICOS:
12613a118989SAlex Deucher 	case CHIP_ARUBA:
126268bc7785SAlex Deucher 	case CHIP_TAHITI:
126368bc7785SAlex Deucher 	case CHIP_PITCAIRN:
126468bc7785SAlex Deucher 	case CHIP_VERDE:
126568bc7785SAlex Deucher 	case CHIP_OLAND:
126668bc7785SAlex Deucher 	case CHIP_HAINAN:
12675a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
12685a16f761SAlex Deucher 		if (!rdev->rlc_fw)
12695a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12705a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
12715a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
12725a16f761SAlex Deucher 			 (!rdev->smc_fw))
12735a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12745a16f761SAlex Deucher 		else if (radeon_dpm == 0)
12755a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12765a16f761SAlex Deucher 		else
12775a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
12785a16f761SAlex Deucher 		break;
1279da321c8aSAlex Deucher 	default:
1280da321c8aSAlex Deucher 		/* default to profile method */
1281da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1282da321c8aSAlex Deucher 		break;
1283da321c8aSAlex Deucher 	}
1284da321c8aSAlex Deucher 
1285da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1286da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1287da321c8aSAlex Deucher 	else
1288da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1289da321c8aSAlex Deucher }
1290da321c8aSAlex Deucher 
1291*914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1292*914a8987SAlex Deucher {
1293*914a8987SAlex Deucher 	int ret = 0;
1294*914a8987SAlex Deucher 
1295*914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
1296*914a8987SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1297*914a8987SAlex Deucher 		ret = radeon_dpm_late_enable(rdev);
1298*914a8987SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1299*914a8987SAlex Deucher 	}
1300*914a8987SAlex Deucher 	return ret;
1301*914a8987SAlex Deucher }
1302*914a8987SAlex Deucher 
1303da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
130429fb52caSAlex Deucher {
1305ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1306a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1307ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1308ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1309ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1310ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1311ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1312ce8f5370SAlex Deucher 			/* reset default clocks */
1313ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1314ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1315ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
131658e21dffSAlex Deucher 		}
1317ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
131832c87fcaSTejun Heo 
131932c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
132058e21dffSAlex Deucher 
1321ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1322ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1323ce8f5370SAlex Deucher 	}
1324a424816fSAlex Deucher 
13250975b162SAlex Deucher 	if (rdev->pm.power_state)
13260975b162SAlex Deucher 		kfree(rdev->pm.power_state);
132729fb52caSAlex Deucher }
132829fb52caSAlex Deucher 
1329da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1330da321c8aSAlex Deucher {
1331da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1332da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1333da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1334da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1335da321c8aSAlex Deucher 
1336da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
133770d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1338da321c8aSAlex Deucher 		/* XXX backwards compat */
1339da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1340da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1341da321c8aSAlex Deucher 	}
1342da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1343da321c8aSAlex Deucher 
1344da321c8aSAlex Deucher 	if (rdev->pm.power_state)
1345da321c8aSAlex Deucher 		kfree(rdev->pm.power_state);
1346da321c8aSAlex Deucher }
1347da321c8aSAlex Deucher 
1348da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1349da321c8aSAlex Deucher {
1350da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1351da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1352da321c8aSAlex Deucher 	else
1353da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1354da321c8aSAlex Deucher }
1355da321c8aSAlex Deucher 
1356da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1357c913e23aSRafał Miłecki {
1358c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1359a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1360c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1361c913e23aSRafał Miłecki 
1362ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1363ce8f5370SAlex Deucher 		return;
1364ce8f5370SAlex Deucher 
1365c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1366c913e23aSRafał Miłecki 
1367c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1368a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
1369a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
1370a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1371a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1372a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
1373c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1374a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
1375c913e23aSRafał Miłecki 		}
1376c913e23aSRafał Miłecki 	}
1377c913e23aSRafał Miłecki 
1378ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1379ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1380ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1381ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1382ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1383a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1384ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1385ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1386c913e23aSRafał Miłecki 
1387ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1388ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1389ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1390ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1391c913e23aSRafał Miłecki 
1392d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1393c913e23aSRafał Miłecki 				}
1394a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1395c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1396c913e23aSRafał Miłecki 
1397ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1398ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1399ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1400ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1401ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1402c913e23aSRafał Miłecki 
140332c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1404c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1405ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1406ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
140732c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1408c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1409d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1410c913e23aSRafał Miłecki 				}
1411a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1412ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1413ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1414c913e23aSRafał Miłecki 
1415ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1416ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1417ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1418ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1419ce8f5370SAlex Deucher 				}
1420ce8f5370SAlex Deucher 			}
142173a6d3fcSRafał Miłecki 		}
1422c913e23aSRafał Miłecki 	}
1423c913e23aSRafał Miłecki 
1424c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1425c913e23aSRafał Miłecki }
1426c913e23aSRafał Miłecki 
1427da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1428da321c8aSAlex Deucher {
1429da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1430da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1431da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1432da321c8aSAlex Deucher 
1433da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1434da321c8aSAlex Deucher 
14355ca302f7SAlex Deucher 	/* update active crtc counts */
1436da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1437da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
1438da321c8aSAlex Deucher 	list_for_each_entry(crtc,
1439da321c8aSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1440da321c8aSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1441da321c8aSAlex Deucher 		if (crtc->enabled) {
1442da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1443da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtc_count++;
1444da321c8aSAlex Deucher 		}
1445da321c8aSAlex Deucher 	}
1446da321c8aSAlex Deucher 
14475ca302f7SAlex Deucher 	/* update battery/ac status */
14485ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
14495ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
14505ca302f7SAlex Deucher 	else
14515ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
14525ca302f7SAlex Deucher 
1453da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1454da321c8aSAlex Deucher 
1455da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
14568a227555SAlex Deucher 
1457da321c8aSAlex Deucher }
1458da321c8aSAlex Deucher 
1459da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1460da321c8aSAlex Deucher {
1461da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1462da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1463da321c8aSAlex Deucher 	else
1464da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1465da321c8aSAlex Deucher }
1466da321c8aSAlex Deucher 
1467ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1468f735261bSDave Airlie {
146975fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1470f735261bSDave Airlie 	bool in_vbl = true;
1471f735261bSDave Airlie 
147275fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
147375fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
147475fa0b08SMario Kleiner 	 */
147575fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
147675fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
1477d47abc58SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos, NULL, NULL);
1478f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1479f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
1480f735261bSDave Airlie 				in_vbl = false;
1481f735261bSDave Airlie 		}
1482f735261bSDave Airlie 	}
1483f81f2024SMatthew Garrett 
1484f81f2024SMatthew Garrett 	return in_vbl;
1485f81f2024SMatthew Garrett }
1486f81f2024SMatthew Garrett 
1487ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1488f81f2024SMatthew Garrett {
1489f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1490f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1491f81f2024SMatthew Garrett 
1492f735261bSDave Airlie 	if (in_vbl == false)
1493d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1494bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1495f735261bSDave Airlie 	return in_vbl;
1496f735261bSDave Airlie }
1497c913e23aSRafał Miłecki 
1498ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1499c913e23aSRafał Miłecki {
1500c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1501d9932a32SMatthew Garrett 	int resched;
1502c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1503ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1504c913e23aSRafał Miłecki 
1505d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1506c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1507ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1508c913e23aSRafał Miłecki 		int not_processed = 0;
15097465280cSAlex Deucher 		int i;
1510c913e23aSRafał Miłecki 
15117465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
15120ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
15130ec0612aSAlex Deucher 
15140ec0612aSAlex Deucher 			if (ring->ready) {
151547492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
15167465280cSAlex Deucher 				if (not_processed >= 3)
15177465280cSAlex Deucher 					break;
15187465280cSAlex Deucher 			}
15190ec0612aSAlex Deucher 		}
1520c913e23aSRafał Miłecki 
1521c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1522ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1523ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1524ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1525ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1526ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1527ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1528ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1529c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1530c913e23aSRafał Miłecki 			}
1531c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1532ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1533ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1534ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1535ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1536ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1537ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1538ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1539c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1540c913e23aSRafał Miłecki 			}
1541c913e23aSRafał Miłecki 		}
1542c913e23aSRafał Miłecki 
1543d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1544d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1545d7311171SAlex Deucher 		 */
1546ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1547ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1548ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1549ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1550c913e23aSRafał Miłecki 		}
1551c913e23aSRafał Miłecki 
155232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1553c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1554c913e23aSRafał Miłecki 	}
15553f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
15563f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
15573f53eb6fSRafael J. Wysocki }
1558c913e23aSRafał Miłecki 
15597433874eSRafał Miłecki /*
15607433874eSRafał Miłecki  * Debugfs info
15617433874eSRafał Miłecki  */
15627433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
15637433874eSRafał Miłecki 
15647433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
15657433874eSRafał Miłecki {
15667433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
15677433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
15687433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
15697433874eSRafał Miłecki 
15701316b792SAlex Deucher 	if (rdev->pm.dpm_enabled) {
15711316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
15721316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
15731316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
15741316b792SAlex Deucher 		else
157571375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
15761316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
15771316b792SAlex Deucher 	} else {
15789ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1579bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1580bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1581bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1582bf05d998SAlex Deucher 		else
15836234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
15849ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1585798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
15866234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
15870fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
15880fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1589798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1590aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
15911316b792SAlex Deucher 	}
15927433874eSRafał Miłecki 
15937433874eSRafał Miłecki 	return 0;
15947433874eSRafał Miłecki }
15957433874eSRafał Miłecki 
15967433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
15977433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
15987433874eSRafał Miłecki };
15997433874eSRafał Miłecki #endif
16007433874eSRafał Miłecki 
1601c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
16027433874eSRafał Miłecki {
16037433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
16047433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
16057433874eSRafał Miłecki #else
16067433874eSRafał Miłecki 	return 0;
16077433874eSRafał Miłecki #endif
16087433874eSRafał Miłecki }
1609