xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 8f500af4efe347d1a8ac674d115220e8caa84559)
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;
7696682956SAlex Deucher 		if (rdev->family == CHIP_ARUBA) {
771c71bda0SAlex Deucher 			if (rdev->asic->dpm.enable_bapm)
781c71bda0SAlex Deucher 				radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
7996682956SAlex Deucher 		}
801c71bda0SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
811c71bda0SAlex Deucher         } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
82ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
83ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
84ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
85ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
86ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
87ce8f5370SAlex Deucher 		}
88ce8f5370SAlex Deucher 	}
89ce8f5370SAlex Deucher }
90ce8f5370SAlex Deucher 
91ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
92ce8f5370SAlex Deucher {
93ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
94ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
95ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
96ce8f5370SAlex Deucher 		break;
97ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
98ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
99ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
100ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
101ce8f5370SAlex Deucher 			else
102ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
103ce8f5370SAlex Deucher 		} else {
104ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
105c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
106ce8f5370SAlex Deucher 			else
107c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
108ce8f5370SAlex Deucher 		}
109ce8f5370SAlex Deucher 		break;
110ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
111ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
112ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
113ce8f5370SAlex Deucher 		else
114ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
115ce8f5370SAlex Deucher 		break;
116c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
117c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
118c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
119c9e75b21SAlex Deucher 		else
120c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
121c9e75b21SAlex Deucher 		break;
122ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
123ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
124ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
125ce8f5370SAlex Deucher 		else
126ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
127ce8f5370SAlex Deucher 		break;
128ce8f5370SAlex Deucher 	}
129ce8f5370SAlex Deucher 
130ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
131ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
132ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
133ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
134ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
135ce8f5370SAlex Deucher 	} else {
136ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
137ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
138ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
139ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
140ce8f5370SAlex Deucher 	}
141ce8f5370SAlex Deucher }
142c913e23aSRafał Miłecki 
1435876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1445876dd24SMatthew Garrett {
1455876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1465876dd24SMatthew Garrett 
1475876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1485876dd24SMatthew Garrett 		return;
1495876dd24SMatthew Garrett 
1505876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1515876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1525876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1535876dd24SMatthew Garrett 	}
1545876dd24SMatthew Garrett }
1555876dd24SMatthew Garrett 
156ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
157ce8f5370SAlex Deucher {
158ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
159ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
160ce8f5370SAlex Deucher 		wait_event_timeout(
161ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
162ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
163ce8f5370SAlex Deucher 	}
164ce8f5370SAlex Deucher }
165ce8f5370SAlex Deucher 
166ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
167ce8f5370SAlex Deucher {
168ce8f5370SAlex Deucher 	u32 sclk, mclk;
16992645879SAlex Deucher 	bool misc_after = false;
170ce8f5370SAlex Deucher 
171ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
172ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
173ce8f5370SAlex Deucher 		return;
174ce8f5370SAlex Deucher 
175ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
176ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
177ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1789ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1799ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
180ce8f5370SAlex Deucher 
18127810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
18227810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1837ae764b1SAlex Deucher 		 * mclk and vddci.
18427810fb2SAlex Deucher 		 */
18527810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
18627810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
18727810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
18827810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
18927810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
19027810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
19127810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
19227810fb2SAlex Deucher 		else
193ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
194ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
19527810fb2SAlex Deucher 
1969ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1979ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
198ce8f5370SAlex Deucher 
19992645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
20092645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
20192645879SAlex Deucher 			misc_after = true;
20292645879SAlex Deucher 
20392645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
20492645879SAlex Deucher 
20592645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
20692645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
20792645879SAlex Deucher 				return;
20892645879SAlex Deucher 		}
20992645879SAlex Deucher 
21092645879SAlex Deucher 		radeon_pm_prepare(rdev);
21192645879SAlex Deucher 
21292645879SAlex Deucher 		if (!misc_after)
213ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
214ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
215ce8f5370SAlex Deucher 
216ce8f5370SAlex Deucher 		/* set engine clock */
217ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
218ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
219ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
220ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
221ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
222d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
223ce8f5370SAlex Deucher 		}
224ce8f5370SAlex Deucher 
225ce8f5370SAlex Deucher 		/* set memory clock */
226798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
227ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
228ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
229ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
230ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
231d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
232ce8f5370SAlex Deucher 		}
23392645879SAlex Deucher 
23492645879SAlex Deucher 		if (misc_after)
23592645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
23692645879SAlex Deucher 			radeon_pm_misc(rdev);
23792645879SAlex Deucher 
238ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
239ce8f5370SAlex Deucher 
240ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
241ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
242ce8f5370SAlex Deucher 	} else
243d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
244ce8f5370SAlex Deucher }
245ce8f5370SAlex Deucher 
246ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
247a424816fSAlex Deucher {
2485f8f635eSJerome Glisse 	int i, r;
2492aba631cSMatthew Garrett 
2504e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2514e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2524e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2534e186b2dSAlex Deucher 		return;
2544e186b2dSAlex Deucher 
255612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
256db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
257d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2584f3218cbSAlex Deucher 
25995f5a3acSAlex Deucher 	/* wait for the rings to drain */
26095f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
26195f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2625f8f635eSJerome Glisse 		if (!ring->ready) {
2635f8f635eSJerome Glisse 			continue;
2645f8f635eSJerome Glisse 		}
26537615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
2665f8f635eSJerome Glisse 		if (r) {
2675f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2685f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2695f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2705f8f635eSJerome Glisse 			mutex_unlock(&rdev->ddev->struct_mutex);
2715f8f635eSJerome Glisse 			return;
2725f8f635eSJerome Glisse 		}
273ce8f5370SAlex Deucher 	}
27495f5a3acSAlex Deucher 
2755876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2765876dd24SMatthew Garrett 
277ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2782aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2792aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2802aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2812aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2822aba631cSMatthew Garrett 			}
2832aba631cSMatthew Garrett 		}
2842aba631cSMatthew Garrett 	}
2852aba631cSMatthew Garrett 
286ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2872aba631cSMatthew Garrett 
288ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2892aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2902aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2912aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2922aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2932aba631cSMatthew Garrett 			}
2942aba631cSMatthew Garrett 		}
2952aba631cSMatthew Garrett 	}
296a424816fSAlex Deucher 
297a424816fSAlex Deucher 	/* update display watermarks based on new power state */
298a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
299a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
300a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
301a424816fSAlex Deucher 
302ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3032aba631cSMatthew Garrett 
304d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
305db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
306612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
307a424816fSAlex Deucher }
308a424816fSAlex Deucher 
309f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
310f712d0c7SRafał Miłecki {
311f712d0c7SRafał Miłecki 	int i, j;
312f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
313f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
314f712d0c7SRafał Miłecki 
315d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
316f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
317f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
318d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
319f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
320f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
321d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
322f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
323d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
324f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
325d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
326d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
327f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
328f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
329f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
330eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
331f712d0c7SRafał Miłecki 						 j,
332eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
333f712d0c7SRafał Miłecki 			else
334eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
335f712d0c7SRafał Miłecki 						 j,
336f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
337f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
338eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
339f712d0c7SRafał Miłecki 		}
340f712d0c7SRafał Miłecki 	}
341f712d0c7SRafał Miłecki }
342f712d0c7SRafał Miłecki 
343ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
344a424816fSAlex Deucher 				     struct device_attribute *attr,
345a424816fSAlex Deucher 				     char *buf)
346a424816fSAlex Deucher {
3473e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
348a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
349ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
350a424816fSAlex Deucher 
351a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
352ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
353ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
35412e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
355ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
356a424816fSAlex Deucher }
357a424816fSAlex Deucher 
358ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
359a424816fSAlex Deucher 				     struct device_attribute *attr,
360a424816fSAlex Deucher 				     const char *buf,
361a424816fSAlex Deucher 				     size_t count)
362a424816fSAlex Deucher {
3633e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
364a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
365a424816fSAlex Deucher 
3664f2f2039SAlex Deucher 	/* Can't set profile when the card is off */
3674f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
3684f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
3694f2f2039SAlex Deucher 		return -EINVAL;
3704f2f2039SAlex Deucher 
371a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
372ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
373ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
374ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
375ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
376ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
377ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
378ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
379c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
380c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
381ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
382ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
383ce8f5370SAlex Deucher 		else {
3841783e4bfSThomas Renninger 			count = -EINVAL;
385ce8f5370SAlex Deucher 			goto fail;
386ce8f5370SAlex Deucher 		}
387ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
388ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3891783e4bfSThomas Renninger 	} else
3901783e4bfSThomas Renninger 		count = -EINVAL;
3911783e4bfSThomas Renninger 
392ce8f5370SAlex Deucher fail:
393a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
394a424816fSAlex Deucher 
395a424816fSAlex Deucher 	return count;
396a424816fSAlex Deucher }
397a424816fSAlex Deucher 
398ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
399ce8f5370SAlex Deucher 				    struct device_attribute *attr,
400ce8f5370SAlex Deucher 				    char *buf)
40156278a8eSAlex Deucher {
4023e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
403ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
404ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
40556278a8eSAlex Deucher 
406ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
407da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
408da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
40956278a8eSAlex Deucher }
41056278a8eSAlex Deucher 
411ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
412ce8f5370SAlex Deucher 				    struct device_attribute *attr,
413ce8f5370SAlex Deucher 				    const char *buf,
414ce8f5370SAlex Deucher 				    size_t count)
415d0d6cb81SRafał Miłecki {
4163e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
417ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
418ce8f5370SAlex Deucher 
4194f2f2039SAlex Deucher 	/* Can't set method when the card is off */
4204f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4214f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
4224f2f2039SAlex Deucher 		count = -EINVAL;
4234f2f2039SAlex Deucher 		goto fail;
4244f2f2039SAlex Deucher 	}
4254f2f2039SAlex Deucher 
426da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
427da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
428da321c8aSAlex Deucher 		count = -EINVAL;
429da321c8aSAlex Deucher 		goto fail;
430da321c8aSAlex Deucher 	}
431ce8f5370SAlex Deucher 
432ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
433ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
434ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
435ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
436ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
437ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
438ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
439ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
440ce8f5370SAlex Deucher 		/* disable dynpm */
441ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
442ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4433f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
444ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
44532c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
446ce8f5370SAlex Deucher 	} else {
4471783e4bfSThomas Renninger 		count = -EINVAL;
448ce8f5370SAlex Deucher 		goto fail;
449d0d6cb81SRafał Miłecki 	}
450ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
451ce8f5370SAlex Deucher fail:
452ce8f5370SAlex Deucher 	return count;
453ce8f5370SAlex Deucher }
454ce8f5370SAlex Deucher 
455da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
456da321c8aSAlex Deucher 				    struct device_attribute *attr,
457da321c8aSAlex Deucher 				    char *buf)
458da321c8aSAlex Deucher {
4593e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
460da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
461da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
462da321c8aSAlex Deucher 
4634f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4644f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
4654f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
4664f2f2039SAlex Deucher 
467da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
468da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
469da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
470da321c8aSAlex Deucher }
471da321c8aSAlex Deucher 
472da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
473da321c8aSAlex Deucher 				    struct device_attribute *attr,
474da321c8aSAlex Deucher 				    const char *buf,
475da321c8aSAlex Deucher 				    size_t count)
476da321c8aSAlex Deucher {
4773e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
478da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
479da321c8aSAlex Deucher 
4804f2f2039SAlex Deucher 	/* Can't set dpm state when the card is off */
4814f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4824f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
4834f2f2039SAlex Deucher 		return -EINVAL;
4844f2f2039SAlex Deucher 
485da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
486da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
487da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
488da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
489da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
490da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
491da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
492da321c8aSAlex Deucher 	else {
493da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
494da321c8aSAlex Deucher 		count = -EINVAL;
495da321c8aSAlex Deucher 		goto fail;
496da321c8aSAlex Deucher 	}
497da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
498da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
499da321c8aSAlex Deucher fail:
500da321c8aSAlex Deucher 	return count;
501da321c8aSAlex Deucher }
502da321c8aSAlex Deucher 
50370d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
50470d01a5eSAlex Deucher 						       struct device_attribute *attr,
50570d01a5eSAlex Deucher 						       char *buf)
50670d01a5eSAlex Deucher {
5073e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
50870d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
50970d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
51070d01a5eSAlex Deucher 
5114f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5124f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5134f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
5144f2f2039SAlex Deucher 
51570d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
51670d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
51770d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
51870d01a5eSAlex Deucher }
51970d01a5eSAlex Deucher 
52070d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
52170d01a5eSAlex Deucher 						       struct device_attribute *attr,
52270d01a5eSAlex Deucher 						       const char *buf,
52370d01a5eSAlex Deucher 						       size_t count)
52470d01a5eSAlex Deucher {
5253e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
52670d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
52770d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
52870d01a5eSAlex Deucher 	int ret = 0;
52970d01a5eSAlex Deucher 
5304f2f2039SAlex Deucher 	/* Can't force performance level when the card is off */
5314f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5324f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5334f2f2039SAlex Deucher 		return -EINVAL;
5344f2f2039SAlex Deucher 
53570d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
53670d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
53770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
53870d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
53970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
54070d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
54170d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
54270d01a5eSAlex Deucher 	} else {
54370d01a5eSAlex Deucher 		count = -EINVAL;
54470d01a5eSAlex Deucher 		goto fail;
54570d01a5eSAlex Deucher 	}
54670d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5470a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5480a17af37SAlex Deucher 			count = -EINVAL;
5490a17af37SAlex Deucher 			goto fail;
5500a17af37SAlex Deucher 		}
55170d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
55270d01a5eSAlex Deucher 		if (ret)
55370d01a5eSAlex Deucher 			count = -EINVAL;
55470d01a5eSAlex Deucher 	}
55570d01a5eSAlex Deucher fail:
5560a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5570a17af37SAlex Deucher 
55870d01a5eSAlex Deucher 	return count;
55970d01a5eSAlex Deucher }
56070d01a5eSAlex Deucher 
561ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
562ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
563da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
56470d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
56570d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
56670d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
567ce8f5370SAlex Deucher 
56821a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
56921a8122aSAlex Deucher 				      struct device_attribute *attr,
57021a8122aSAlex Deucher 				      char *buf)
57121a8122aSAlex Deucher {
572ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
5734f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
57420d391d7SAlex Deucher 	int temp;
57521a8122aSAlex Deucher 
5764f2f2039SAlex Deucher 	/* Can't get temperature when the card is off */
5774f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5784f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5794f2f2039SAlex Deucher 		return -EINVAL;
5804f2f2039SAlex Deucher 
5816bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
5826bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
5836bd1c385SAlex Deucher 	else
58421a8122aSAlex Deucher 		temp = 0;
58521a8122aSAlex Deucher 
58621a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
58721a8122aSAlex Deucher }
58821a8122aSAlex Deucher 
5896ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
5906ea4e84dSJean Delvare 					     struct device_attribute *attr,
5916ea4e84dSJean Delvare 					     char *buf)
5926ea4e84dSJean Delvare {
593e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
5946ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
5956ea4e84dSJean Delvare 	int temp;
5966ea4e84dSJean Delvare 
5976ea4e84dSJean Delvare 	if (hyst)
5986ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
5996ea4e84dSJean Delvare 	else
6006ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
6016ea4e84dSJean Delvare 
6026ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
6036ea4e84dSJean Delvare }
6046ea4e84dSJean Delvare 
60521a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
6066ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
6076ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
60821a8122aSAlex Deucher 
60921a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
61021a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
6116ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
6126ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
61321a8122aSAlex Deucher 	NULL
61421a8122aSAlex Deucher };
61521a8122aSAlex Deucher 
6166ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
6176ea4e84dSJean Delvare 					struct attribute *attr, int index)
6186ea4e84dSJean Delvare {
6196ea4e84dSJean Delvare 	struct device *dev = container_of(kobj, struct device, kobj);
620e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
6216ea4e84dSJean Delvare 
6226ea4e84dSJean Delvare 	/* Skip limit attributes if DPM is not enabled */
6236ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
6246ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
6256ea4e84dSJean Delvare 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
6266ea4e84dSJean Delvare 		return 0;
6276ea4e84dSJean Delvare 
6286ea4e84dSJean Delvare 	return attr->mode;
6296ea4e84dSJean Delvare }
6306ea4e84dSJean Delvare 
63121a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
63221a8122aSAlex Deucher 	.attrs = hwmon_attributes,
6336ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
63421a8122aSAlex Deucher };
63521a8122aSAlex Deucher 
636ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
637ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
638ec39f64bSGuenter Roeck 	NULL
639ec39f64bSGuenter Roeck };
640ec39f64bSGuenter Roeck 
6410d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
64221a8122aSAlex Deucher {
6430d18abedSDan Carpenter 	int err = 0;
64421a8122aSAlex Deucher 
64521a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
64621a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
64721a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
64821a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
649457558edSAlex Deucher 	case THERMAL_TYPE_NI:
650e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
6511bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
652286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
653286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
6546bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
6555d7486c7SAlex Deucher 			return err;
656cb3e4e7cSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
657ec39f64bSGuenter Roeck 									   "radeon", rdev,
658ec39f64bSGuenter Roeck 									   hwmon_groups);
659cb3e4e7cSAlex Deucher 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
660cb3e4e7cSAlex Deucher 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
6610d18abedSDan Carpenter 			dev_err(rdev->dev,
6620d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
6630d18abedSDan Carpenter 		}
66421a8122aSAlex Deucher 		break;
66521a8122aSAlex Deucher 	default:
66621a8122aSAlex Deucher 		break;
66721a8122aSAlex Deucher 	}
6680d18abedSDan Carpenter 
6690d18abedSDan Carpenter 	return err;
67021a8122aSAlex Deucher }
67121a8122aSAlex Deucher 
672cb3e4e7cSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
673cb3e4e7cSAlex Deucher {
674cb3e4e7cSAlex Deucher 	if (rdev->pm.int_hwmon_dev)
675cb3e4e7cSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
676cb3e4e7cSAlex Deucher }
677cb3e4e7cSAlex Deucher 
678da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
679da321c8aSAlex Deucher {
680da321c8aSAlex Deucher 	struct radeon_device *rdev =
681da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
682da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
683da321c8aSAlex Deucher 	/* switch to the thermal state */
684da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
685da321c8aSAlex Deucher 
686da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
687da321c8aSAlex Deucher 		return;
688da321c8aSAlex Deucher 
689da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
690da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
691da321c8aSAlex Deucher 
692da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
693da321c8aSAlex Deucher 			/* switch back the user state */
694da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
695da321c8aSAlex Deucher 	} else {
696da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
697da321c8aSAlex Deucher 			/* switch back the user state */
698da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
699da321c8aSAlex Deucher 	}
70060320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
70160320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
70260320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
70360320347SAlex Deucher 	else
70460320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
70560320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
70660320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
70760320347SAlex Deucher 
70860320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
709da321c8aSAlex Deucher }
710da321c8aSAlex Deucher 
711da321c8aSAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
712da321c8aSAlex Deucher 						     enum radeon_pm_state_type dpm_state)
713da321c8aSAlex Deucher {
714da321c8aSAlex Deucher 	int i;
715da321c8aSAlex Deucher 	struct radeon_ps *ps;
716da321c8aSAlex Deucher 	u32 ui_class;
71748783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
71848783069SAlex Deucher 		true : false;
71948783069SAlex Deucher 
72048783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
72148783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
72248783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
72348783069SAlex Deucher 			single_display = false;
72448783069SAlex Deucher 	}
725da321c8aSAlex Deucher 
726edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
727edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
728edcaa5b1SAlex Deucher 	 */
729edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
730edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
731da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
732da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
733da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
734da321c8aSAlex Deucher 
735edcaa5b1SAlex Deucher restart_search:
736da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
737da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
738da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
739da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
740da321c8aSAlex Deucher 		switch (dpm_state) {
741da321c8aSAlex Deucher 		/* user states */
742da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
743da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
744da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
74548783069SAlex Deucher 					if (single_display)
746da321c8aSAlex Deucher 						return ps;
747da321c8aSAlex Deucher 				} else
748da321c8aSAlex Deucher 					return ps;
749da321c8aSAlex Deucher 			}
750da321c8aSAlex Deucher 			break;
751da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
752da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
753da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
75448783069SAlex Deucher 					if (single_display)
755da321c8aSAlex Deucher 						return ps;
756da321c8aSAlex Deucher 				} else
757da321c8aSAlex Deucher 					return ps;
758da321c8aSAlex Deucher 			}
759da321c8aSAlex Deucher 			break;
760da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
761da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
762da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
76348783069SAlex Deucher 					if (single_display)
764da321c8aSAlex Deucher 						return ps;
765da321c8aSAlex Deucher 				} else
766da321c8aSAlex Deucher 					return ps;
767da321c8aSAlex Deucher 			}
768da321c8aSAlex Deucher 			break;
769da321c8aSAlex Deucher 		/* internal states */
770da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
771d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
772da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
773d4d3278cSAlex Deucher 			else
774d4d3278cSAlex Deucher 				break;
775da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
776da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
777da321c8aSAlex Deucher 				return ps;
778da321c8aSAlex Deucher 			break;
779da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
780da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
781da321c8aSAlex Deucher 				return ps;
782da321c8aSAlex Deucher 			break;
783da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
784da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
785da321c8aSAlex Deucher 				return ps;
786da321c8aSAlex Deucher 			break;
787da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
788da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
789da321c8aSAlex Deucher 				return ps;
790da321c8aSAlex Deucher 			break;
791da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
792da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
793da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
794da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
795da321c8aSAlex Deucher 				return ps;
796da321c8aSAlex Deucher 			break;
797da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
798da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
799da321c8aSAlex Deucher 				return ps;
800da321c8aSAlex Deucher 			break;
801da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
802da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
803da321c8aSAlex Deucher 				return ps;
804da321c8aSAlex Deucher 			break;
805edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
806edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
807edcaa5b1SAlex Deucher 				return ps;
808edcaa5b1SAlex Deucher 			break;
809da321c8aSAlex Deucher 		default:
810da321c8aSAlex Deucher 			break;
811da321c8aSAlex Deucher 		}
812da321c8aSAlex Deucher 	}
813da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
814da321c8aSAlex Deucher 	switch (dpm_state) {
815da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
816ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
817ce3537d5SAlex Deucher 		goto restart_search;
818da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
819da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
820da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
821d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
822da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
823d4d3278cSAlex Deucher 		} else {
824d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
825d4d3278cSAlex Deucher 			goto restart_search;
826d4d3278cSAlex Deucher 		}
827da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
828da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
829da321c8aSAlex Deucher 		goto restart_search;
830da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
831da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
832da321c8aSAlex Deucher 		goto restart_search;
833da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
834edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
835edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
836da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
837da321c8aSAlex Deucher 		goto restart_search;
838da321c8aSAlex Deucher 	default:
839da321c8aSAlex Deucher 		break;
840da321c8aSAlex Deucher 	}
841da321c8aSAlex Deucher 
842da321c8aSAlex Deucher 	return NULL;
843da321c8aSAlex Deucher }
844da321c8aSAlex Deucher 
845da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
846da321c8aSAlex Deucher {
847da321c8aSAlex Deucher 	int i;
848da321c8aSAlex Deucher 	struct radeon_ps *ps;
849da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
85084dd1928SAlex Deucher 	int ret;
851da321c8aSAlex Deucher 
852da321c8aSAlex Deucher 	/* if dpm init failed */
853da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
854da321c8aSAlex Deucher 		return;
855da321c8aSAlex Deucher 
856da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
857da321c8aSAlex Deucher 		/* add other state override checks here */
8588a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
8598a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
860da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
861da321c8aSAlex Deucher 	}
862da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
863da321c8aSAlex Deucher 
864da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
865da321c8aSAlex Deucher 	if (ps)
86689c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
867da321c8aSAlex Deucher 	else
868da321c8aSAlex Deucher 		return;
869da321c8aSAlex Deucher 
870d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
871da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
872b62d628bSAlex Deucher 		/* vce just modifies an existing state so force a change */
873b62d628bSAlex Deucher 		if (ps->vce_active != rdev->pm.dpm.vce_active)
874b62d628bSAlex Deucher 			goto force;
875d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
876d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
877d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
878d22b7e40SAlex Deucher 			 */
879da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
880d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
881da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
882da321c8aSAlex Deucher 				/* update displays */
883da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
884da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
885da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
886da321c8aSAlex Deucher 			}
887da321c8aSAlex Deucher 			return;
888d22b7e40SAlex Deucher 		} else {
889d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
890d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
891d22b7e40SAlex Deucher 			 * update display configuration.
892d22b7e40SAlex Deucher 			 */
893d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
894d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
895d22b7e40SAlex Deucher 				return;
896d22b7e40SAlex Deucher 			} else {
897d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
898d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
899d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
900d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
901d22b7e40SAlex Deucher 					/* update displays */
902d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
903d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
904d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
905d22b7e40SAlex Deucher 					return;
906d22b7e40SAlex Deucher 				}
907d22b7e40SAlex Deucher 			}
908d22b7e40SAlex Deucher 		}
909da321c8aSAlex Deucher 	}
910da321c8aSAlex Deucher 
911b62d628bSAlex Deucher force:
912033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
913da321c8aSAlex Deucher 		printk("switching from power state:\n");
914da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
915da321c8aSAlex Deucher 		printk("switching to power state:\n");
916da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
917033a37dfSAlex Deucher 	}
918b62d628bSAlex Deucher 
919da321c8aSAlex Deucher 	mutex_lock(&rdev->ddev->struct_mutex);
920da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
921da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
922da321c8aSAlex Deucher 
923b62d628bSAlex Deucher 	/* update whether vce is active */
924b62d628bSAlex Deucher 	ps->vce_active = rdev->pm.dpm.vce_active;
925b62d628bSAlex Deucher 
92684dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
92784dd1928SAlex Deucher 	if (ret)
92884dd1928SAlex Deucher 		goto done;
92984dd1928SAlex Deucher 
930da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
931da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
932da321c8aSAlex Deucher 	/* update displays */
933da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
934da321c8aSAlex Deucher 
935da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
936da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
937da321c8aSAlex Deucher 
938da321c8aSAlex Deucher 	/* wait for the rings to drain */
939da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
940da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
941da321c8aSAlex Deucher 		if (ring->ready)
94237615527SChristian König 			radeon_fence_wait_empty(rdev, i);
943da321c8aSAlex Deucher 	}
944da321c8aSAlex Deucher 
945da321c8aSAlex Deucher 	/* program the new power state */
946da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
947da321c8aSAlex Deucher 
948da321c8aSAlex Deucher 	/* update current power state */
949da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
950da321c8aSAlex Deucher 
95184dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
95284dd1928SAlex Deucher 
9531cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
95414ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
95514ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
95660320347SAlex Deucher 			/* force low perf level for thermal */
95760320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
95814ac88afSAlex Deucher 			/* save the user's level */
95914ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
96014ac88afSAlex Deucher 		} else {
96114ac88afSAlex Deucher 			/* otherwise, user selected level */
96214ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
96314ac88afSAlex Deucher 		}
96460320347SAlex Deucher 	}
96560320347SAlex Deucher 
96684dd1928SAlex Deucher done:
967da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
968da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
969da321c8aSAlex Deucher 	mutex_unlock(&rdev->ddev->struct_mutex);
970da321c8aSAlex Deucher }
971da321c8aSAlex Deucher 
972ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
973ce3537d5SAlex Deucher {
974ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
975ce3537d5SAlex Deucher 
9769e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
9779e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
9788158eb9eSChristian König 		/* don't powergate anything if we
9798158eb9eSChristian König 		   have active but pause streams */
9808158eb9eSChristian König 		enable |= rdev->pm.dpm.sd > 0;
9818158eb9eSChristian König 		enable |= rdev->pm.dpm.hd > 0;
9829e9d9762SAlex Deucher 		/* enable/disable UVD */
9839e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
9849e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
9859e9d9762SAlex Deucher 	} else {
986ce3537d5SAlex Deucher 		if (enable) {
987ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
988ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
9890690a229SAlex Deucher 			/* disable this for now */
9900690a229SAlex Deucher #if 0
991ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
992ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
993ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
994ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
995ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
996ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
997ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
998ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
999ce3537d5SAlex Deucher 			else
10000690a229SAlex Deucher #endif
1001ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1002ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
1003ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1004ce3537d5SAlex Deucher 		} else {
1005ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1006ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
1007ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1008ce3537d5SAlex Deucher 		}
1009ce3537d5SAlex Deucher 
1010ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1011ce3537d5SAlex Deucher 	}
10129e9d9762SAlex Deucher }
1013ce3537d5SAlex Deucher 
101403afe6f6SAlex Deucher void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
101503afe6f6SAlex Deucher {
101603afe6f6SAlex Deucher 	if (enable) {
101703afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
101803afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = true;
101903afe6f6SAlex Deucher 		/* XXX select vce level based on ring/task */
102003afe6f6SAlex Deucher 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
102103afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
102203afe6f6SAlex Deucher 	} else {
102303afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
102403afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = false;
102503afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
102603afe6f6SAlex Deucher 	}
102703afe6f6SAlex Deucher 
102803afe6f6SAlex Deucher 	radeon_pm_compute_clocks(rdev);
102903afe6f6SAlex Deucher }
103003afe6f6SAlex Deucher 
1031da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
1032ce8f5370SAlex Deucher {
1033ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
10343f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
10353f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
10363f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
10373f53eb6fSRafael J. Wysocki 	}
1038ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
103932c87fcaSTejun Heo 
104032c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1041ce8f5370SAlex Deucher }
1042ce8f5370SAlex Deucher 
1043da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1044da321c8aSAlex Deucher {
1045da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1046da321c8aSAlex Deucher 	/* disable dpm */
1047da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
1048da321c8aSAlex Deucher 	/* reset the power state */
1049da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1050da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
1051da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1052da321c8aSAlex Deucher }
1053da321c8aSAlex Deucher 
1054da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1055da321c8aSAlex Deucher {
1056da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1057da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1058da321c8aSAlex Deucher 	else
1059da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1060da321c8aSAlex Deucher }
1061da321c8aSAlex Deucher 
1062da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1063ce8f5370SAlex Deucher {
1064ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
10652e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
106636099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
10672e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1068ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
10698a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
10708a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
10712feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
10722feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
10732feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1074ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1075ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1076ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1077ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1078ed18a360SAlex Deucher 	}
1079f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1080f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1081f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1082f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
10839ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
10849ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
108537016951SMichel Dänzer 	if (rdev->pm.power_state) {
10864d60173fSAlex Deucher 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
10872feea49aSAlex Deucher 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
108837016951SMichel Dänzer 	}
10893f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
10903f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
10913f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
109232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
10933f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
10943f53eb6fSRafael J. Wysocki 	}
1095f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1096ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1097d0d6cb81SRafał Miłecki }
1098d0d6cb81SRafał Miłecki 
1099da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
11007433874eSRafał Miłecki {
110126481fb1SDave Airlie 	int ret;
11020d18abedSDan Carpenter 
1103da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1104da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1105da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1106da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1107da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1108da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1109e14cd2bbSAlex Deucher 	if (ret)
1110e14cd2bbSAlex Deucher 		goto dpm_resume_fail;
1111e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = true;
1112e14cd2bbSAlex Deucher 	return;
1113e14cd2bbSAlex Deucher 
1114e14cd2bbSAlex Deucher dpm_resume_fail:
1115da321c8aSAlex Deucher 	DRM_ERROR("radeon: dpm resume failed\n");
1116da321c8aSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
111736099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1118da321c8aSAlex Deucher 	    rdev->mc_fw) {
1119da321c8aSAlex Deucher 		if (rdev->pm.default_vddc)
1120da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1121da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1122da321c8aSAlex Deucher 		if (rdev->pm.default_vddci)
1123da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1124da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1125da321c8aSAlex Deucher 		if (rdev->pm.default_sclk)
1126da321c8aSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1127da321c8aSAlex Deucher 		if (rdev->pm.default_mclk)
1128da321c8aSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1129da321c8aSAlex Deucher 	}
1130da321c8aSAlex Deucher }
1131da321c8aSAlex Deucher 
1132da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1133da321c8aSAlex Deucher {
1134da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1135da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1136da321c8aSAlex Deucher 	else
1137da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1138da321c8aSAlex Deucher }
1139da321c8aSAlex Deucher 
1140da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1141da321c8aSAlex Deucher {
1142da321c8aSAlex Deucher 	int ret;
1143da321c8aSAlex Deucher 
1144f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1145ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1146ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1147ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1148ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
11499ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
11509ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1151f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1152f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
115321a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1154c913e23aSRafał Miłecki 
115556278a8eSAlex Deucher 	if (rdev->bios) {
115656278a8eSAlex Deucher 		if (rdev->is_atom_bios)
115756278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
115856278a8eSAlex Deucher 		else
115956278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1160f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1161ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1162ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
11632e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
116436099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
11652e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1166ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
11678a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
11688a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
11694639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
11704639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
11714639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1172ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1173ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1174ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1175ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1176ed18a360SAlex Deucher 		}
117756278a8eSAlex Deucher 	}
117856278a8eSAlex Deucher 
117921a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
11800d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
11810d18abedSDan Carpenter 	if (ret)
11820d18abedSDan Carpenter 		return ret;
118332c87fcaSTejun Heo 
118432c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
118532c87fcaSTejun Heo 
1186ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1187ce8f5370SAlex Deucher 		/* where's the best place to put these? */
118826481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
118926481fb1SDave Airlie 		if (ret)
119026481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
119126481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
119226481fb1SDave Airlie 		if (ret)
119326481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
1194ce8f5370SAlex Deucher 
11957433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1196c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
11977433874eSRafał Miłecki 		}
11987433874eSRafał Miłecki 
1199c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1200ce8f5370SAlex Deucher 	}
1201c913e23aSRafał Miłecki 
12027433874eSRafał Miłecki 	return 0;
12037433874eSRafał Miłecki }
12047433874eSRafał Miłecki 
1205da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1206da321c8aSAlex Deucher {
1207da321c8aSAlex Deucher 	int i;
1208da321c8aSAlex Deucher 
1209da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1210da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1211da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1212da321c8aSAlex Deucher 	}
1213da321c8aSAlex Deucher }
1214da321c8aSAlex Deucher 
1215da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1216da321c8aSAlex Deucher {
1217da321c8aSAlex Deucher 	int ret;
1218da321c8aSAlex Deucher 
12191cd8b21aSAlex Deucher 	/* default to balanced state */
1220edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1221edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
12221cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1223da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1224da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1225da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1226da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1227da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1228da321c8aSAlex Deucher 
1229da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1230da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1231da321c8aSAlex Deucher 	else
1232da321c8aSAlex Deucher 		return -EINVAL;
1233da321c8aSAlex Deucher 
1234da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1235da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1236da321c8aSAlex Deucher 	if (ret)
1237da321c8aSAlex Deucher 		return ret;
1238da321c8aSAlex Deucher 
1239da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1240da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1241da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1242da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1243033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1244da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1245da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1246da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1247da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1248e14cd2bbSAlex Deucher 	if (ret)
1249e14cd2bbSAlex Deucher 		goto dpm_failed;
1250da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1251da321c8aSAlex Deucher 
1252da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1253da321c8aSAlex Deucher 	if (ret)
1254da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
125570d01a5eSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
125670d01a5eSAlex Deucher 	if (ret)
125770d01a5eSAlex Deucher 		DRM_ERROR("failed to create device file for dpm state\n");
1258da321c8aSAlex Deucher 	/* XXX: these are noops for dpm but are here for backwards compat */
1259da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1260da321c8aSAlex Deucher 	if (ret)
1261da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power profile\n");
1262da321c8aSAlex Deucher 	ret = device_create_file(rdev->dev, &dev_attr_power_method);
1263da321c8aSAlex Deucher 	if (ret)
1264da321c8aSAlex Deucher 		DRM_ERROR("failed to create device file for power method\n");
12651316b792SAlex Deucher 
12661316b792SAlex Deucher 	if (radeon_debugfs_pm_init(rdev)) {
12671316b792SAlex Deucher 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
12681316b792SAlex Deucher 	}
12691316b792SAlex Deucher 
1270da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1271da321c8aSAlex Deucher 
1272da321c8aSAlex Deucher 	return 0;
1273e14cd2bbSAlex Deucher 
1274e14cd2bbSAlex Deucher dpm_failed:
1275e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = false;
1276e14cd2bbSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
1277e14cd2bbSAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1278e14cd2bbSAlex Deucher 	    rdev->mc_fw) {
1279e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddc)
1280e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1281e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1282e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddci)
1283e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1284e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1285e14cd2bbSAlex Deucher 		if (rdev->pm.default_sclk)
1286e14cd2bbSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1287e14cd2bbSAlex Deucher 		if (rdev->pm.default_mclk)
1288e14cd2bbSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1289e14cd2bbSAlex Deucher 	}
1290e14cd2bbSAlex Deucher 	DRM_ERROR("radeon: dpm initialization failed\n");
1291e14cd2bbSAlex Deucher 	return ret;
1292da321c8aSAlex Deucher }
1293da321c8aSAlex Deucher 
1294da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1295da321c8aSAlex Deucher {
1296da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1297da321c8aSAlex Deucher 	switch (rdev->family) {
12984a6369e9SAlex Deucher 	case CHIP_RV610:
12994a6369e9SAlex Deucher 	case CHIP_RV630:
13004a6369e9SAlex Deucher 	case CHIP_RV620:
13014a6369e9SAlex Deucher 	case CHIP_RV635:
13024a6369e9SAlex Deucher 	case CHIP_RV670:
13039d67006eSAlex Deucher 	case CHIP_RS780:
13049d67006eSAlex Deucher 	case CHIP_RS880:
130576e6dcecSAlex Deucher 	case CHIP_RV770:
1306919cf555SAlex Deucher 	case CHIP_BARTS:
1307919cf555SAlex Deucher 	case CHIP_TURKS:
1308919cf555SAlex Deucher 	case CHIP_CAICOS:
13098a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1310761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1311761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13128a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
13138a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
13148a53fa23SAlex Deucher 			 (!rdev->smc_fw))
13158a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1316761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
13179d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
13189d67006eSAlex Deucher 		else
13199d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13209d67006eSAlex Deucher 		break;
1321ab70b1ddSAlex Deucher 	case CHIP_RV730:
1322ab70b1ddSAlex Deucher 	case CHIP_RV710:
1323ab70b1ddSAlex Deucher 	case CHIP_RV740:
132459f7a2f2SAlex Deucher 	case CHIP_CEDAR:
132559f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
132659f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
132759f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
132859f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
13295a16f761SAlex Deucher 	case CHIP_PALM:
13305a16f761SAlex Deucher 	case CHIP_SUMO:
13315a16f761SAlex Deucher 	case CHIP_SUMO2:
1332*8f500af4SAlex Deucher 	case CHIP_CAYMAN:
13333a118989SAlex Deucher 	case CHIP_ARUBA:
133468bc7785SAlex Deucher 	case CHIP_TAHITI:
133568bc7785SAlex Deucher 	case CHIP_PITCAIRN:
133668bc7785SAlex Deucher 	case CHIP_VERDE:
133768bc7785SAlex Deucher 	case CHIP_OLAND:
133868bc7785SAlex Deucher 	case CHIP_HAINAN:
13394f22dde3SAlex Deucher 	case CHIP_BONAIRE:
1340e308b1d3SAlex Deucher 	case CHIP_KABINI:
1341e308b1d3SAlex Deucher 	case CHIP_KAVERI:
13424f22dde3SAlex Deucher 	case CHIP_HAWAII:
13437d032a4bSSamuel Li 	case CHIP_MULLINS:
13445a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
13455a16f761SAlex Deucher 		if (!rdev->rlc_fw)
13465a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13475a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
13485a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
13495a16f761SAlex Deucher 			 (!rdev->smc_fw))
13505a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13515a16f761SAlex Deucher 		else if (radeon_dpm == 0)
13525a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
13535a16f761SAlex Deucher 		else
13545a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
13555a16f761SAlex Deucher 		break;
1356da321c8aSAlex Deucher 	default:
1357da321c8aSAlex Deucher 		/* default to profile method */
1358da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1359da321c8aSAlex Deucher 		break;
1360da321c8aSAlex Deucher 	}
1361da321c8aSAlex Deucher 
1362da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1363da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1364da321c8aSAlex Deucher 	else
1365da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1366da321c8aSAlex Deucher }
1367da321c8aSAlex Deucher 
1368914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1369914a8987SAlex Deucher {
1370914a8987SAlex Deucher 	int ret = 0;
1371914a8987SAlex Deucher 
1372914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
1373914a8987SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1374914a8987SAlex Deucher 		ret = radeon_dpm_late_enable(rdev);
1375914a8987SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1376914a8987SAlex Deucher 	}
1377914a8987SAlex Deucher 	return ret;
1378914a8987SAlex Deucher }
1379914a8987SAlex Deucher 
1380da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
138129fb52caSAlex Deucher {
1382ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1383a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1384ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1385ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1386ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1387ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1388ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1389ce8f5370SAlex Deucher 			/* reset default clocks */
1390ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1391ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1392ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
139358e21dffSAlex Deucher 		}
1394ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
139532c87fcaSTejun Heo 
139632c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
139758e21dffSAlex Deucher 
1398ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1399ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1400ce8f5370SAlex Deucher 	}
1401a424816fSAlex Deucher 
1402cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
14030975b162SAlex Deucher 	kfree(rdev->pm.power_state);
140429fb52caSAlex Deucher }
140529fb52caSAlex Deucher 
1406da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1407da321c8aSAlex Deucher {
1408da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1409da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1410da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1411da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1412da321c8aSAlex Deucher 
1413da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
141470d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1415da321c8aSAlex Deucher 		/* XXX backwards compat */
1416da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1417da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1418da321c8aSAlex Deucher 	}
1419da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1420da321c8aSAlex Deucher 
1421cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
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;
16534f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
16547433874eSRafał Miłecki 
16554f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
16564f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
16574f2f2039SAlex Deucher 		seq_printf(m, "PX asic powered off\n");
16584f2f2039SAlex 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