xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 4f2f203976964e267dc477de6648bdb3acd2b74b)
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 		}
26337615527SChristian König 		r = radeon_fence_wait_empty(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 
364*4f2f2039SAlex Deucher 	/* Can't set profile when the card is off */
365*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
366*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
367*4f2f2039SAlex Deucher 		return -EINVAL;
368*4f2f2039SAlex Deucher 
369a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
370ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
371ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
372ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
373ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
374ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
375ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
376ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
377c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
378c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
379ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
380ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
381ce8f5370SAlex Deucher 		else {
3821783e4bfSThomas Renninger 			count = -EINVAL;
383ce8f5370SAlex Deucher 			goto fail;
384ce8f5370SAlex Deucher 		}
385ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
386ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3871783e4bfSThomas Renninger 	} else
3881783e4bfSThomas Renninger 		count = -EINVAL;
3891783e4bfSThomas Renninger 
390ce8f5370SAlex Deucher fail:
391a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
392a424816fSAlex Deucher 
393a424816fSAlex Deucher 	return count;
394a424816fSAlex Deucher }
395a424816fSAlex Deucher 
396ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
397ce8f5370SAlex Deucher 				    struct device_attribute *attr,
398ce8f5370SAlex Deucher 				    char *buf)
39956278a8eSAlex Deucher {
4003e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
401ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
402ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
40356278a8eSAlex Deucher 
404ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
405da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
406da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
40756278a8eSAlex Deucher }
40856278a8eSAlex Deucher 
409ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
410ce8f5370SAlex Deucher 				    struct device_attribute *attr,
411ce8f5370SAlex Deucher 				    const char *buf,
412ce8f5370SAlex Deucher 				    size_t count)
413d0d6cb81SRafał Miłecki {
4143e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
415ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
416ce8f5370SAlex Deucher 
417*4f2f2039SAlex Deucher 	/* Can't set method when the card is off */
418*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
419*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
420*4f2f2039SAlex Deucher 		count = -EINVAL;
421*4f2f2039SAlex Deucher 		goto fail;
422*4f2f2039SAlex Deucher 	}
423*4f2f2039SAlex Deucher 
424da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
425da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
426da321c8aSAlex Deucher 		count = -EINVAL;
427da321c8aSAlex Deucher 		goto fail;
428da321c8aSAlex Deucher 	}
429ce8f5370SAlex Deucher 
430ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
431ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
432ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
433ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
434ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
435ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
436ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
437ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
438ce8f5370SAlex Deucher 		/* disable dynpm */
439ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
440ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4413f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
442ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
44332c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
444ce8f5370SAlex Deucher 	} else {
4451783e4bfSThomas Renninger 		count = -EINVAL;
446ce8f5370SAlex Deucher 		goto fail;
447d0d6cb81SRafał Miłecki 	}
448ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
449ce8f5370SAlex Deucher fail:
450ce8f5370SAlex Deucher 	return count;
451ce8f5370SAlex Deucher }
452ce8f5370SAlex Deucher 
453da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
454da321c8aSAlex Deucher 				    struct device_attribute *attr,
455da321c8aSAlex Deucher 				    char *buf)
456da321c8aSAlex Deucher {
4573e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
458da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
459da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
460da321c8aSAlex Deucher 
461*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
462*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
463*4f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
464*4f2f2039SAlex Deucher 
465da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
466da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
467da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
468da321c8aSAlex Deucher }
469da321c8aSAlex Deucher 
470da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
471da321c8aSAlex Deucher 				    struct device_attribute *attr,
472da321c8aSAlex Deucher 				    const char *buf,
473da321c8aSAlex Deucher 				    size_t count)
474da321c8aSAlex Deucher {
4753e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
476da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
477da321c8aSAlex Deucher 
478*4f2f2039SAlex Deucher 	/* Can't set dpm state when the card is off */
479*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
480*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
481*4f2f2039SAlex Deucher 		return -EINVAL;
482*4f2f2039SAlex Deucher 
483da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
484da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
485da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
486da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
487da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
488da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
489da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
490da321c8aSAlex Deucher 	else {
491da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
492da321c8aSAlex Deucher 		count = -EINVAL;
493da321c8aSAlex Deucher 		goto fail;
494da321c8aSAlex Deucher 	}
495da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
496da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
497da321c8aSAlex Deucher fail:
498da321c8aSAlex Deucher 	return count;
499da321c8aSAlex Deucher }
500da321c8aSAlex Deucher 
50170d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
50270d01a5eSAlex Deucher 						       struct device_attribute *attr,
50370d01a5eSAlex Deucher 						       char *buf)
50470d01a5eSAlex Deucher {
5053e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
50670d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
50770d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
50870d01a5eSAlex Deucher 
509*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
510*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
511*4f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
512*4f2f2039SAlex Deucher 
51370d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
51470d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
51570d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
51670d01a5eSAlex Deucher }
51770d01a5eSAlex Deucher 
51870d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
51970d01a5eSAlex Deucher 						       struct device_attribute *attr,
52070d01a5eSAlex Deucher 						       const char *buf,
52170d01a5eSAlex Deucher 						       size_t count)
52270d01a5eSAlex Deucher {
5233e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
52470d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
52570d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
52670d01a5eSAlex Deucher 	int ret = 0;
52770d01a5eSAlex Deucher 
528*4f2f2039SAlex Deucher 	/* Can't force performance level when the card is off */
529*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
530*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
531*4f2f2039SAlex Deucher 		return -EINVAL;
532*4f2f2039SAlex Deucher 
53370d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
53470d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
53570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
53670d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
53770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
53870d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
53970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
54070d01a5eSAlex Deucher 	} else {
54170d01a5eSAlex Deucher 		count = -EINVAL;
54270d01a5eSAlex Deucher 		goto fail;
54370d01a5eSAlex Deucher 	}
54470d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5450a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5460a17af37SAlex Deucher 			count = -EINVAL;
5470a17af37SAlex Deucher 			goto fail;
5480a17af37SAlex Deucher 		}
54970d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
55070d01a5eSAlex Deucher 		if (ret)
55170d01a5eSAlex Deucher 			count = -EINVAL;
55270d01a5eSAlex Deucher 	}
55370d01a5eSAlex Deucher fail:
5540a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5550a17af37SAlex Deucher 
55670d01a5eSAlex Deucher 	return count;
55770d01a5eSAlex Deucher }
55870d01a5eSAlex Deucher 
559ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
560ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
561da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
56270d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
56370d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
56470d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
565ce8f5370SAlex Deucher 
56621a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
56721a8122aSAlex Deucher 				      struct device_attribute *attr,
56821a8122aSAlex Deucher 				      char *buf)
56921a8122aSAlex Deucher {
570ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
571*4f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
57220d391d7SAlex Deucher 	int temp;
57321a8122aSAlex Deucher 
574*4f2f2039SAlex Deucher 	/* Can't get temperature when the card is off */
575*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
576*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
577*4f2f2039SAlex Deucher 		return -EINVAL;
578*4f2f2039SAlex Deucher 
5796bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
5806bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
5816bd1c385SAlex Deucher 	else
58221a8122aSAlex Deucher 		temp = 0;
58321a8122aSAlex Deucher 
58421a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
58521a8122aSAlex Deucher }
58621a8122aSAlex Deucher 
5876ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
5886ea4e84dSJean Delvare 					     struct device_attribute *attr,
5896ea4e84dSJean Delvare 					     char *buf)
5906ea4e84dSJean Delvare {
591e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
5926ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
5936ea4e84dSJean Delvare 	int temp;
5946ea4e84dSJean Delvare 
5956ea4e84dSJean Delvare 	if (hyst)
5966ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
5976ea4e84dSJean Delvare 	else
5986ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
5996ea4e84dSJean Delvare 
6006ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
6016ea4e84dSJean Delvare }
6026ea4e84dSJean Delvare 
60321a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
6046ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
6056ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
60621a8122aSAlex Deucher 
60721a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
60821a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
6096ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
6106ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
61121a8122aSAlex Deucher 	NULL
61221a8122aSAlex Deucher };
61321a8122aSAlex Deucher 
6146ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
6156ea4e84dSJean Delvare 					struct attribute *attr, int index)
6166ea4e84dSJean Delvare {
6176ea4e84dSJean Delvare 	struct device *dev = container_of(kobj, struct device, kobj);
618e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
6196ea4e84dSJean Delvare 
6206ea4e84dSJean Delvare 	/* Skip limit attributes if DPM is not enabled */
6216ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
6226ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
6236ea4e84dSJean Delvare 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
6246ea4e84dSJean Delvare 		return 0;
6256ea4e84dSJean Delvare 
6266ea4e84dSJean Delvare 	return attr->mode;
6276ea4e84dSJean Delvare }
6286ea4e84dSJean Delvare 
62921a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
63021a8122aSAlex Deucher 	.attrs = hwmon_attributes,
6316ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
63221a8122aSAlex Deucher };
63321a8122aSAlex Deucher 
634ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
635ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
636ec39f64bSGuenter Roeck 	NULL
637ec39f64bSGuenter Roeck };
638ec39f64bSGuenter Roeck 
6390d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
64021a8122aSAlex Deucher {
6410d18abedSDan Carpenter 	int err = 0;
64221a8122aSAlex Deucher 
64321a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
64421a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
64521a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
64621a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
647457558edSAlex Deucher 	case THERMAL_TYPE_NI:
648e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
6491bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
650286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
651286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
6526bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
6535d7486c7SAlex Deucher 			return err;
654cb3e4e7cSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
655ec39f64bSGuenter Roeck 									   "radeon", rdev,
656ec39f64bSGuenter Roeck 									   hwmon_groups);
657cb3e4e7cSAlex Deucher 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
658cb3e4e7cSAlex Deucher 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
6590d18abedSDan Carpenter 			dev_err(rdev->dev,
6600d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
6610d18abedSDan Carpenter 		}
66221a8122aSAlex Deucher 		break;
66321a8122aSAlex Deucher 	default:
66421a8122aSAlex Deucher 		break;
66521a8122aSAlex Deucher 	}
6660d18abedSDan Carpenter 
6670d18abedSDan Carpenter 	return err;
66821a8122aSAlex Deucher }
66921a8122aSAlex Deucher 
670cb3e4e7cSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
671cb3e4e7cSAlex Deucher {
672cb3e4e7cSAlex Deucher 	if (rdev->pm.int_hwmon_dev)
673cb3e4e7cSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
674cb3e4e7cSAlex Deucher }
675cb3e4e7cSAlex Deucher 
676da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
677da321c8aSAlex Deucher {
678da321c8aSAlex Deucher 	struct radeon_device *rdev =
679da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
680da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
681da321c8aSAlex Deucher 	/* switch to the thermal state */
682da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
683da321c8aSAlex Deucher 
684da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
685da321c8aSAlex Deucher 		return;
686da321c8aSAlex Deucher 
687da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
688da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
689da321c8aSAlex Deucher 
690da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
691da321c8aSAlex Deucher 			/* switch back the user state */
692da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
693da321c8aSAlex Deucher 	} else {
694da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
695da321c8aSAlex Deucher 			/* switch back the user state */
696da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
697da321c8aSAlex Deucher 	}
69860320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
69960320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
70060320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
70160320347SAlex Deucher 	else
70260320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
70360320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
70460320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
70560320347SAlex Deucher 
70660320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
707da321c8aSAlex Deucher }
708da321c8aSAlex Deucher 
709da321c8aSAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
710da321c8aSAlex Deucher 						     enum radeon_pm_state_type dpm_state)
711da321c8aSAlex Deucher {
712da321c8aSAlex Deucher 	int i;
713da321c8aSAlex Deucher 	struct radeon_ps *ps;
714da321c8aSAlex Deucher 	u32 ui_class;
71548783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
71648783069SAlex Deucher 		true : false;
71748783069SAlex Deucher 
71848783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
71948783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
72048783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
72148783069SAlex Deucher 			single_display = false;
72248783069SAlex Deucher 	}
723da321c8aSAlex Deucher 
724edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
725edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
726edcaa5b1SAlex Deucher 	 */
727edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
728edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
729da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
730da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
731da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
732da321c8aSAlex Deucher 
733edcaa5b1SAlex Deucher restart_search:
734da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
735da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
736da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
737da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
738da321c8aSAlex Deucher 		switch (dpm_state) {
739da321c8aSAlex Deucher 		/* user states */
740da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
741da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
742da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
74348783069SAlex Deucher 					if (single_display)
744da321c8aSAlex Deucher 						return ps;
745da321c8aSAlex Deucher 				} else
746da321c8aSAlex Deucher 					return ps;
747da321c8aSAlex Deucher 			}
748da321c8aSAlex Deucher 			break;
749da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
750da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
751da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
75248783069SAlex Deucher 					if (single_display)
753da321c8aSAlex Deucher 						return ps;
754da321c8aSAlex Deucher 				} else
755da321c8aSAlex Deucher 					return ps;
756da321c8aSAlex Deucher 			}
757da321c8aSAlex Deucher 			break;
758da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
759da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
760da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
76148783069SAlex Deucher 					if (single_display)
762da321c8aSAlex Deucher 						return ps;
763da321c8aSAlex Deucher 				} else
764da321c8aSAlex Deucher 					return ps;
765da321c8aSAlex Deucher 			}
766da321c8aSAlex Deucher 			break;
767da321c8aSAlex Deucher 		/* internal states */
768da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
769d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
770da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
771d4d3278cSAlex Deucher 			else
772d4d3278cSAlex Deucher 				break;
773da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
774da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
775da321c8aSAlex Deucher 				return ps;
776da321c8aSAlex Deucher 			break;
777da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
778da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
779da321c8aSAlex Deucher 				return ps;
780da321c8aSAlex Deucher 			break;
781da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
782da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
783da321c8aSAlex Deucher 				return ps;
784da321c8aSAlex Deucher 			break;
785da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
786da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
787da321c8aSAlex Deucher 				return ps;
788da321c8aSAlex Deucher 			break;
789da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
790da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
791da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
792da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
793da321c8aSAlex Deucher 				return ps;
794da321c8aSAlex Deucher 			break;
795da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
796da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
797da321c8aSAlex Deucher 				return ps;
798da321c8aSAlex Deucher 			break;
799da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
800da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
801da321c8aSAlex Deucher 				return ps;
802da321c8aSAlex Deucher 			break;
803edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
804edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
805edcaa5b1SAlex Deucher 				return ps;
806edcaa5b1SAlex Deucher 			break;
807da321c8aSAlex Deucher 		default:
808da321c8aSAlex Deucher 			break;
809da321c8aSAlex Deucher 		}
810da321c8aSAlex Deucher 	}
811da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
812da321c8aSAlex Deucher 	switch (dpm_state) {
813da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
814ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
815ce3537d5SAlex Deucher 		goto restart_search;
816da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
817da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
818da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
819d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
820da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
821d4d3278cSAlex Deucher 		} else {
822d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
823d4d3278cSAlex Deucher 			goto restart_search;
824d4d3278cSAlex Deucher 		}
825da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
826da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
827da321c8aSAlex Deucher 		goto restart_search;
828da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
829da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
830da321c8aSAlex Deucher 		goto restart_search;
831da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
832edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
833edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
834da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
835da321c8aSAlex Deucher 		goto restart_search;
836da321c8aSAlex Deucher 	default:
837da321c8aSAlex Deucher 		break;
838da321c8aSAlex Deucher 	}
839da321c8aSAlex Deucher 
840da321c8aSAlex Deucher 	return NULL;
841da321c8aSAlex Deucher }
842da321c8aSAlex Deucher 
843da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
844da321c8aSAlex Deucher {
845da321c8aSAlex Deucher 	int i;
846da321c8aSAlex Deucher 	struct radeon_ps *ps;
847da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
84884dd1928SAlex Deucher 	int ret;
849da321c8aSAlex Deucher 
850da321c8aSAlex Deucher 	/* if dpm init failed */
851da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
852da321c8aSAlex Deucher 		return;
853da321c8aSAlex Deucher 
854da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
855da321c8aSAlex Deucher 		/* add other state override checks here */
8568a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
8578a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
858da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
859da321c8aSAlex Deucher 	}
860da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
861da321c8aSAlex Deucher 
862da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
863da321c8aSAlex Deucher 	if (ps)
86489c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
865da321c8aSAlex Deucher 	else
866da321c8aSAlex Deucher 		return;
867da321c8aSAlex Deucher 
868d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
869da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
870b62d628bSAlex Deucher 		/* vce just modifies an existing state so force a change */
871b62d628bSAlex Deucher 		if (ps->vce_active != rdev->pm.dpm.vce_active)
872b62d628bSAlex Deucher 			goto force;
873d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
874d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
875d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
876d22b7e40SAlex Deucher 			 */
877da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
878d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
879da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
880da321c8aSAlex Deucher 				/* update displays */
881da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
882da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
883da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
884da321c8aSAlex Deucher 			}
885da321c8aSAlex Deucher 			return;
886d22b7e40SAlex Deucher 		} else {
887d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
888d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
889d22b7e40SAlex Deucher 			 * update display configuration.
890d22b7e40SAlex Deucher 			 */
891d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
892d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
893d22b7e40SAlex Deucher 				return;
894d22b7e40SAlex Deucher 			} else {
895d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
896d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
897d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
898d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
899d22b7e40SAlex Deucher 					/* update displays */
900d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
901d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
902d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
903d22b7e40SAlex Deucher 					return;
904d22b7e40SAlex Deucher 				}
905d22b7e40SAlex Deucher 			}
906d22b7e40SAlex Deucher 		}
907da321c8aSAlex Deucher 	}
908da321c8aSAlex Deucher 
909b62d628bSAlex Deucher force:
910033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
911da321c8aSAlex Deucher 		printk("switching from power state:\n");
912da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
913da321c8aSAlex Deucher 		printk("switching to power state:\n");
914da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
915033a37dfSAlex Deucher 	}
916b62d628bSAlex Deucher 
917da321c8aSAlex Deucher 	mutex_lock(&rdev->ddev->struct_mutex);
918da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
919da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
920da321c8aSAlex Deucher 
921b62d628bSAlex Deucher 	/* update whether vce is active */
922b62d628bSAlex Deucher 	ps->vce_active = rdev->pm.dpm.vce_active;
923b62d628bSAlex Deucher 
92484dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
92584dd1928SAlex Deucher 	if (ret)
92684dd1928SAlex Deucher 		goto done;
92784dd1928SAlex Deucher 
928da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
929da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
930da321c8aSAlex Deucher 	/* update displays */
931da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
932da321c8aSAlex Deucher 
933da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
934da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
935da321c8aSAlex Deucher 
936da321c8aSAlex Deucher 	/* wait for the rings to drain */
937da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
938da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
939da321c8aSAlex Deucher 		if (ring->ready)
94037615527SChristian König 			radeon_fence_wait_empty(rdev, i);
941da321c8aSAlex Deucher 	}
942da321c8aSAlex Deucher 
943da321c8aSAlex Deucher 	/* program the new power state */
944da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
945da321c8aSAlex Deucher 
946da321c8aSAlex Deucher 	/* update current power state */
947da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
948da321c8aSAlex Deucher 
94984dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
95084dd1928SAlex Deucher 
9511cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
95214ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
95314ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
95460320347SAlex Deucher 			/* force low perf level for thermal */
95560320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
95614ac88afSAlex Deucher 			/* save the user's level */
95714ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
95814ac88afSAlex Deucher 		} else {
95914ac88afSAlex Deucher 			/* otherwise, user selected level */
96014ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
96114ac88afSAlex Deucher 		}
96260320347SAlex Deucher 	}
96360320347SAlex Deucher 
96484dd1928SAlex Deucher done:
965da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
966da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
967da321c8aSAlex Deucher 	mutex_unlock(&rdev->ddev->struct_mutex);
968da321c8aSAlex Deucher }
969da321c8aSAlex Deucher 
970ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
971ce3537d5SAlex Deucher {
972ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
973ce3537d5SAlex Deucher 
9749e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
9759e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
9768158eb9eSChristian König 		/* don't powergate anything if we
9778158eb9eSChristian König 		   have active but pause streams */
9788158eb9eSChristian König 		enable |= rdev->pm.dpm.sd > 0;
9798158eb9eSChristian König 		enable |= rdev->pm.dpm.hd > 0;
9809e9d9762SAlex Deucher 		/* enable/disable UVD */
9819e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
9829e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
9839e9d9762SAlex Deucher 	} else {
984ce3537d5SAlex Deucher 		if (enable) {
985ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
986ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
987ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
988ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
989ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
990ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
991ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
992ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
993ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
994ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
995ce3537d5SAlex Deucher 			else
996ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
997ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
998ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
999ce3537d5SAlex Deucher 		} else {
1000ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1001ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
1002ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1003ce3537d5SAlex Deucher 		}
1004ce3537d5SAlex Deucher 
1005ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1006ce3537d5SAlex Deucher 	}
10079e9d9762SAlex Deucher }
1008ce3537d5SAlex Deucher 
100903afe6f6SAlex Deucher void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
101003afe6f6SAlex Deucher {
101103afe6f6SAlex Deucher 	if (enable) {
101203afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
101303afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = true;
101403afe6f6SAlex Deucher 		/* XXX select vce level based on ring/task */
101503afe6f6SAlex Deucher 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
101603afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
101703afe6f6SAlex Deucher 	} else {
101803afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
101903afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = false;
102003afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
102103afe6f6SAlex Deucher 	}
102203afe6f6SAlex Deucher 
102303afe6f6SAlex Deucher 	radeon_pm_compute_clocks(rdev);
102403afe6f6SAlex Deucher }
102503afe6f6SAlex Deucher 
1026da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
1027ce8f5370SAlex Deucher {
1028ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
10293f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
10303f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
10313f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
10323f53eb6fSRafael J. Wysocki 	}
1033ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
103432c87fcaSTejun Heo 
103532c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1036ce8f5370SAlex Deucher }
1037ce8f5370SAlex Deucher 
1038da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1039da321c8aSAlex Deucher {
1040da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1041da321c8aSAlex Deucher 	/* disable dpm */
1042da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
1043da321c8aSAlex Deucher 	/* reset the power state */
1044da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1045da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
1046da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1047da321c8aSAlex Deucher }
1048da321c8aSAlex Deucher 
1049da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1050da321c8aSAlex Deucher {
1051da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1052da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1053da321c8aSAlex Deucher 	else
1054da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1055da321c8aSAlex Deucher }
1056da321c8aSAlex Deucher 
1057da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1058ce8f5370SAlex Deucher {
1059ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
10602e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
106136099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
10622e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1063ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
10648a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
10658a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
10662feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
10672feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
10682feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1069ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1070ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1071ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1072ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1073ed18a360SAlex Deucher 	}
1074f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1075f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1076f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1077f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
10789ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
10799ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
108037016951SMichel Dänzer 	if (rdev->pm.power_state) {
10814d60173fSAlex Deucher 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
10822feea49aSAlex Deucher 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
108337016951SMichel Dänzer 	}
10843f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
10853f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
10863f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
108732c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
10883f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
10893f53eb6fSRafael J. Wysocki 	}
1090f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1091ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1092d0d6cb81SRafał Miłecki }
1093d0d6cb81SRafał Miłecki 
1094da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
10957433874eSRafał Miłecki {
109626481fb1SDave Airlie 	int ret;
10970d18abedSDan Carpenter 
1098da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1099da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1100da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1101da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1102da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1103da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1104e14cd2bbSAlex Deucher 	if (ret)
1105e14cd2bbSAlex Deucher 		goto dpm_resume_fail;
1106e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = true;
1107e14cd2bbSAlex Deucher 	radeon_pm_compute_clocks(rdev);
1108e14cd2bbSAlex Deucher 	return;
1109e14cd2bbSAlex Deucher 
1110e14cd2bbSAlex Deucher dpm_resume_fail:
1111da321c8aSAlex Deucher 	DRM_ERROR("radeon: dpm resume failed\n");
1112da321c8aSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
111336099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1114da321c8aSAlex Deucher 	    rdev->mc_fw) {
1115da321c8aSAlex Deucher 		if (rdev->pm.default_vddc)
1116da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1117da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1118da321c8aSAlex Deucher 		if (rdev->pm.default_vddci)
1119da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1120da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1121da321c8aSAlex Deucher 		if (rdev->pm.default_sclk)
1122da321c8aSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1123da321c8aSAlex Deucher 		if (rdev->pm.default_mclk)
1124da321c8aSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1125da321c8aSAlex Deucher 	}
1126da321c8aSAlex Deucher }
1127da321c8aSAlex Deucher 
1128da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1129da321c8aSAlex Deucher {
1130da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1131da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1132da321c8aSAlex Deucher 	else
1133da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1134da321c8aSAlex Deucher }
1135da321c8aSAlex Deucher 
1136da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1137da321c8aSAlex Deucher {
1138da321c8aSAlex Deucher 	int ret;
1139da321c8aSAlex Deucher 
1140f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1141ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1142ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1143ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1144ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
11459ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
11469ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1147f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1148f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
114921a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1150c913e23aSRafał Miłecki 
115156278a8eSAlex Deucher 	if (rdev->bios) {
115256278a8eSAlex Deucher 		if (rdev->is_atom_bios)
115356278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
115456278a8eSAlex Deucher 		else
115556278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1156f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1157ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1158ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
11592e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
116036099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
11612e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1162ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
11638a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
11648a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
11654639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
11664639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
11674639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1168ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1169ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1170ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1171ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1172ed18a360SAlex Deucher 		}
117356278a8eSAlex Deucher 	}
117456278a8eSAlex Deucher 
117521a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
11760d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
11770d18abedSDan Carpenter 	if (ret)
11780d18abedSDan Carpenter 		return ret;
117932c87fcaSTejun Heo 
118032c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
118132c87fcaSTejun Heo 
1182ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1183ce8f5370SAlex Deucher 		/* where's the best place to put these? */
118426481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
118526481fb1SDave Airlie 		if (ret)
118626481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
118726481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
118826481fb1SDave Airlie 		if (ret)
118926481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
1190ce8f5370SAlex Deucher 
11917433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1192c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
11937433874eSRafał Miłecki 		}
11947433874eSRafał Miłecki 
1195c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1196ce8f5370SAlex Deucher 	}
1197c913e23aSRafał Miłecki 
11987433874eSRafał Miłecki 	return 0;
11997433874eSRafał Miłecki }
12007433874eSRafał Miłecki 
1201da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1202da321c8aSAlex Deucher {
1203da321c8aSAlex Deucher 	int i;
1204da321c8aSAlex Deucher 
1205da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1206da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1207da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1208da321c8aSAlex Deucher 	}
1209da321c8aSAlex Deucher }
1210da321c8aSAlex Deucher 
1211da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1212da321c8aSAlex Deucher {
1213da321c8aSAlex Deucher 	int ret;
1214da321c8aSAlex Deucher 
12151cd8b21aSAlex Deucher 	/* default to balanced state */
1216edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1217edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
12181cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1219da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1220da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1221da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1222da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1223da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1224da321c8aSAlex Deucher 
1225da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1226da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1227da321c8aSAlex Deucher 	else
1228da321c8aSAlex Deucher 		return -EINVAL;
1229da321c8aSAlex Deucher 
1230da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1231da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1232da321c8aSAlex Deucher 	if (ret)
1233da321c8aSAlex Deucher 		return ret;
1234da321c8aSAlex Deucher 
1235da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1236da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1237da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1238da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1239033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1240da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1241da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1242da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1243da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1244e14cd2bbSAlex Deucher 	if (ret)
1245e14cd2bbSAlex Deucher 		goto dpm_failed;
1246da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1247da321c8aSAlex Deucher 
1248da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1249da321c8aSAlex Deucher 	if (ret)
1250da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
125170d01a5eSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
125270d01a5eSAlex Deucher 	if (ret)
125370d01a5eSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
1254da321c8aSAlex Deucher 	/* XXX: these are noops for dpm but are here for backwards compat */
1255da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1256da321c8aSAlex Deucher 	if (ret)
1257da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power profile\n");
1258da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_method);
1259da321c8aSAlex Deucher 	if (ret)
1260da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power method\n");
12611316b792SAlex Deucher 
12621316b792SAlex Deucher 	if (radeon_debugfs_pm_init(rdev)) {
12631316b792SAlex Deucher 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
12641316b792SAlex Deucher 	}
12651316b792SAlex Deucher 
1266da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1267da321c8aSAlex Deucher 
1268da321c8aSAlex Deucher 	return 0;
1269e14cd2bbSAlex Deucher 
1270e14cd2bbSAlex Deucher dpm_failed:
1271e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = false;
1272e14cd2bbSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
1273e14cd2bbSAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1274e14cd2bbSAlex Deucher 	    rdev->mc_fw) {
1275e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddc)
1276e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1277e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1278e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddci)
1279e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1280e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1281e14cd2bbSAlex Deucher 		if (rdev->pm.default_sclk)
1282e14cd2bbSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1283e14cd2bbSAlex Deucher 		if (rdev->pm.default_mclk)
1284e14cd2bbSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1285e14cd2bbSAlex Deucher 	}
1286e14cd2bbSAlex Deucher 	DRM_ERROR("radeon: dpm initialization failed\n");
1287e14cd2bbSAlex Deucher 	return ret;
1288da321c8aSAlex Deucher }
1289da321c8aSAlex Deucher 
1290da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1291da321c8aSAlex Deucher {
1292da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1293da321c8aSAlex Deucher 	switch (rdev->family) {
12944a6369e9SAlex Deucher 	case CHIP_RV610:
12954a6369e9SAlex Deucher 	case CHIP_RV630:
12964a6369e9SAlex Deucher 	case CHIP_RV620:
12974a6369e9SAlex Deucher 	case CHIP_RV635:
12984a6369e9SAlex Deucher 	case CHIP_RV670:
12999d67006eSAlex Deucher 	case CHIP_RS780:
13009d67006eSAlex Deucher 	case CHIP_RS880:
130176e6dcecSAlex Deucher 	case CHIP_RV770:
1302919cf555SAlex Deucher 	case CHIP_BARTS:
1303919cf555SAlex Deucher 	case CHIP_TURKS:
1304919cf555SAlex Deucher 	case CHIP_CAICOS:
130569e0b57aSAlex Deucher 	case CHIP_CAYMAN:
13068a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1307761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1308761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13098a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
13108a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
13118a53fa23SAlex Deucher 			 (!rdev->smc_fw))
13128a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1313761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
13149d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
13159d67006eSAlex Deucher 		else
13169d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13179d67006eSAlex Deucher 		break;
1318ab70b1ddSAlex Deucher 	case CHIP_RV730:
1319ab70b1ddSAlex Deucher 	case CHIP_RV710:
1320ab70b1ddSAlex Deucher 	case CHIP_RV740:
132159f7a2f2SAlex Deucher 	case CHIP_CEDAR:
132259f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
132359f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
132459f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
132559f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
13265a16f761SAlex Deucher 	case CHIP_PALM:
13275a16f761SAlex Deucher 	case CHIP_SUMO:
13285a16f761SAlex Deucher 	case CHIP_SUMO2:
13293a118989SAlex Deucher 	case CHIP_ARUBA:
133068bc7785SAlex Deucher 	case CHIP_TAHITI:
133168bc7785SAlex Deucher 	case CHIP_PITCAIRN:
133268bc7785SAlex Deucher 	case CHIP_VERDE:
133368bc7785SAlex Deucher 	case CHIP_OLAND:
133468bc7785SAlex Deucher 	case CHIP_HAINAN:
13354f22dde3SAlex Deucher 	case CHIP_BONAIRE:
1336e308b1d3SAlex Deucher 	case CHIP_KABINI:
1337e308b1d3SAlex Deucher 	case CHIP_KAVERI:
13384f22dde3SAlex Deucher 	case CHIP_HAWAII:
13397d032a4bSSamuel Li 	case CHIP_MULLINS:
13405a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
13415a16f761SAlex Deucher 		if (!rdev->rlc_fw)
13425a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13435a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
13445a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
13455a16f761SAlex Deucher 			 (!rdev->smc_fw))
13465a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13475a16f761SAlex Deucher 		else if (radeon_dpm == 0)
13485a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13495a16f761SAlex Deucher 		else
13505a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
13515a16f761SAlex Deucher 		break;
1352da321c8aSAlex Deucher 	default:
1353da321c8aSAlex Deucher 		/* default to profile method */
1354da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1355da321c8aSAlex Deucher 		break;
1356da321c8aSAlex Deucher 	}
1357da321c8aSAlex Deucher 
1358da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1359da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1360da321c8aSAlex Deucher 	else
1361da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1362da321c8aSAlex Deucher }
1363da321c8aSAlex Deucher 
1364914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1365914a8987SAlex Deucher {
1366914a8987SAlex Deucher 	int ret = 0;
1367914a8987SAlex Deucher 
1368914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
1369914a8987SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1370914a8987SAlex Deucher 		ret = radeon_dpm_late_enable(rdev);
1371914a8987SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1372914a8987SAlex Deucher 	}
1373914a8987SAlex Deucher 	return ret;
1374914a8987SAlex Deucher }
1375914a8987SAlex Deucher 
1376da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
137729fb52caSAlex Deucher {
1378ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1379a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1380ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1381ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1382ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1383ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1384ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1385ce8f5370SAlex Deucher 			/* reset default clocks */
1386ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1387ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1388ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
138958e21dffSAlex Deucher 		}
1390ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
139132c87fcaSTejun Heo 
139232c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
139358e21dffSAlex Deucher 
1394ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1395ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1396ce8f5370SAlex Deucher 	}
1397a424816fSAlex Deucher 
1398cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
1399cb3e4e7cSAlex Deucher 
14000975b162SAlex Deucher 	if (rdev->pm.power_state)
14010975b162SAlex Deucher 		kfree(rdev->pm.power_state);
140229fb52caSAlex Deucher }
140329fb52caSAlex Deucher 
1404da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1405da321c8aSAlex Deucher {
1406da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1407da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1408da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1409da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1410da321c8aSAlex Deucher 
1411da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
141270d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1413da321c8aSAlex Deucher 		/* XXX backwards compat */
1414da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1415da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1416da321c8aSAlex Deucher 	}
1417da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1418da321c8aSAlex Deucher 
1419cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
1420cb3e4e7cSAlex Deucher 
1421da321c8aSAlex Deucher 	if (rdev->pm.power_state)
1422da321c8aSAlex Deucher 		kfree(rdev->pm.power_state);
1423da321c8aSAlex Deucher }
1424da321c8aSAlex Deucher 
1425da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1426da321c8aSAlex Deucher {
1427da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1428da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1429da321c8aSAlex Deucher 	else
1430da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1431da321c8aSAlex Deucher }
1432da321c8aSAlex Deucher 
1433da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1434c913e23aSRafał Miłecki {
1435c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1436a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1437c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1438c913e23aSRafał Miłecki 
1439ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1440ce8f5370SAlex Deucher 		return;
1441ce8f5370SAlex Deucher 
1442c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1443c913e23aSRafał Miłecki 
1444c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1445a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
14463ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1447a48b9b4eSAlex Deucher 		list_for_each_entry(crtc,
1448a48b9b4eSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1449a48b9b4eSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1450a48b9b4eSAlex Deucher 			if (radeon_crtc->enabled) {
1451c913e23aSRafał Miłecki 				rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1452a48b9b4eSAlex Deucher 				rdev->pm.active_crtc_count++;
1453c913e23aSRafał Miłecki 			}
1454c913e23aSRafał Miłecki 		}
14553ed9a335SAlex Deucher 	}
1456c913e23aSRafał Miłecki 
1457ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1458ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1459ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1460ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1461ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1462a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1463ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1464ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1465c913e23aSRafał Miłecki 
1466ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1467ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1468ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1469ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1470c913e23aSRafał Miłecki 
1471d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1472c913e23aSRafał Miłecki 				}
1473a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1474c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1475c913e23aSRafał Miłecki 
1476ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1477ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1478ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1479ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1480ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1481c913e23aSRafał Miłecki 
148232c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1483c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1484ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1485ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
148632c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1487c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1488d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1489c913e23aSRafał Miłecki 				}
1490a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1491ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1492ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1493c913e23aSRafał Miłecki 
1494ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1495ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1496ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1497ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1498ce8f5370SAlex Deucher 				}
1499ce8f5370SAlex Deucher 			}
150073a6d3fcSRafał Miłecki 		}
1501c913e23aSRafał Miłecki 	}
1502c913e23aSRafał Miłecki 
1503c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1504c913e23aSRafał Miłecki }
1505c913e23aSRafał Miłecki 
1506da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1507da321c8aSAlex Deucher {
1508da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1509da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1510da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1511da321c8aSAlex Deucher 
15126c7bcceaSAlex Deucher 	if (!rdev->pm.dpm_enabled)
15136c7bcceaSAlex Deucher 		return;
15146c7bcceaSAlex Deucher 
1515da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1516da321c8aSAlex Deucher 
15175ca302f7SAlex Deucher 	/* update active crtc counts */
1518da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1519da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
15203ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1521da321c8aSAlex Deucher 		list_for_each_entry(crtc,
1522da321c8aSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1523da321c8aSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1524da321c8aSAlex Deucher 			if (crtc->enabled) {
1525da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1526da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtc_count++;
1527da321c8aSAlex Deucher 			}
1528da321c8aSAlex Deucher 		}
15293ed9a335SAlex Deucher 	}
1530da321c8aSAlex Deucher 
15315ca302f7SAlex Deucher 	/* update battery/ac status */
15325ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
15335ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
15345ca302f7SAlex Deucher 	else
15355ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
15365ca302f7SAlex Deucher 
1537da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1538da321c8aSAlex Deucher 
1539da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
15408a227555SAlex Deucher 
1541da321c8aSAlex Deucher }
1542da321c8aSAlex Deucher 
1543da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1544da321c8aSAlex Deucher {
1545da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1546da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1547da321c8aSAlex Deucher 	else
1548da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1549da321c8aSAlex Deucher }
1550da321c8aSAlex Deucher 
1551ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1552f735261bSDave Airlie {
155375fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1554f735261bSDave Airlie 	bool in_vbl = true;
1555f735261bSDave Airlie 
155675fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
155775fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
155875fa0b08SMario Kleiner 	 */
155975fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
156075fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
1561abca9e45SVille Syrjälä 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
1562f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1563f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
1564f735261bSDave Airlie 				in_vbl = false;
1565f735261bSDave Airlie 		}
1566f735261bSDave Airlie 	}
1567f81f2024SMatthew Garrett 
1568f81f2024SMatthew Garrett 	return in_vbl;
1569f81f2024SMatthew Garrett }
1570f81f2024SMatthew Garrett 
1571ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1572f81f2024SMatthew Garrett {
1573f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1574f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1575f81f2024SMatthew Garrett 
1576f735261bSDave Airlie 	if (in_vbl == false)
1577d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1578bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1579f735261bSDave Airlie 	return in_vbl;
1580f735261bSDave Airlie }
1581c913e23aSRafał Miłecki 
1582ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1583c913e23aSRafał Miłecki {
1584c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1585d9932a32SMatthew Garrett 	int resched;
1586c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1587ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1588c913e23aSRafał Miłecki 
1589d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1590c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1591ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1592c913e23aSRafał Miłecki 		int not_processed = 0;
15937465280cSAlex Deucher 		int i;
1594c913e23aSRafał Miłecki 
15957465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
15960ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
15970ec0612aSAlex Deucher 
15980ec0612aSAlex Deucher 			if (ring->ready) {
159947492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
16007465280cSAlex Deucher 				if (not_processed >= 3)
16017465280cSAlex Deucher 					break;
16027465280cSAlex Deucher 			}
16030ec0612aSAlex Deucher 		}
1604c913e23aSRafał Miłecki 
1605c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1606ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1607ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1608ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1609ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1610ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1611ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1612ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1613c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1614c913e23aSRafał Miłecki 			}
1615c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1616ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1617ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1618ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1619ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1620ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1621ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1622ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1623c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1624c913e23aSRafał Miłecki 			}
1625c913e23aSRafał Miłecki 		}
1626c913e23aSRafał Miłecki 
1627d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1628d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1629d7311171SAlex Deucher 		 */
1630ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1631ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1632ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1633ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1634c913e23aSRafał Miłecki 		}
1635c913e23aSRafał Miłecki 
163632c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1637c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1638c913e23aSRafał Miłecki 	}
16393f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
16403f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
16413f53eb6fSRafael J. Wysocki }
1642c913e23aSRafał Miłecki 
16437433874eSRafał Miłecki /*
16447433874eSRafał Miłecki  * Debugfs info
16457433874eSRafał Miłecki  */
16467433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
16477433874eSRafał Miłecki 
16487433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
16497433874eSRafał Miłecki {
16507433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
16517433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
16527433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
1653*4f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
16547433874eSRafał Miłecki 
1655*4f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
1656*4f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1657*4f2f2039SAlex Deucher 		seq_printf(m, "PX asic powered off\n");
1658*4f2f2039SAlex Deucher 	} else if (rdev->pm.dpm_enabled) {
16591316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
16601316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
16611316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
16621316b792SAlex Deucher 		else
166371375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
16641316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
16651316b792SAlex Deucher 	} else {
16669ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1667bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1668bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1669bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1670bf05d998SAlex Deucher 		else
16716234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
16729ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1673798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
16746234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
16750fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
16760fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1677798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1678aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
16791316b792SAlex Deucher 	}
16807433874eSRafał Miłecki 
16817433874eSRafał Miłecki 	return 0;
16827433874eSRafał Miłecki }
16837433874eSRafał Miłecki 
16847433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
16857433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
16867433874eSRafał Miłecki };
16877433874eSRafał Miłecki #endif
16887433874eSRafał Miłecki 
1689c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
16907433874eSRafał Miłecki {
16917433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
16927433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
16937433874eSRafał Miłecki #else
16947433874eSRafał Miłecki 	return 0;
16957433874eSRafał Miłecki #endif
16967433874eSRafał Miłecki }
1697