xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 14ac88af156efcefac9ba3cf249ae84f9ff71d37)
17433874eSRafał Miłecki /*
27433874eSRafał Miłecki  * Permission is hereby granted, free of charge, to any person obtaining a
37433874eSRafał Miłecki  * copy of this software and associated documentation files (the "Software"),
47433874eSRafał Miłecki  * to deal in the Software without restriction, including without limitation
57433874eSRafał Miłecki  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
67433874eSRafał Miłecki  * and/or sell copies of the Software, and to permit persons to whom the
77433874eSRafał Miłecki  * Software is furnished to do so, subject to the following conditions:
87433874eSRafał Miłecki  *
97433874eSRafał Miłecki  * The above copyright notice and this permission notice shall be included in
107433874eSRafał Miłecki  * all copies or substantial portions of the Software.
117433874eSRafał Miłecki  *
127433874eSRafał Miłecki  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
137433874eSRafał Miłecki  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147433874eSRafał Miłecki  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
157433874eSRafał Miłecki  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
167433874eSRafał Miłecki  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
177433874eSRafał Miłecki  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
187433874eSRafał Miłecki  * OTHER DEALINGS IN THE SOFTWARE.
197433874eSRafał Miłecki  *
207433874eSRafał Miłecki  * Authors: Rafał Miłecki <zajec5@gmail.com>
2156278a8eSAlex Deucher  *          Alex Deucher <alexdeucher@gmail.com>
227433874eSRafał Miłecki  */
23760285e7SDavid Howells #include <drm/drmP.h>
247433874eSRafał Miłecki #include "radeon.h"
25f735261bSDave Airlie #include "avivod.h"
268a83ec5eSAlex Deucher #include "atom.h"
27ce8f5370SAlex Deucher #include <linux/power_supply.h>
2821a8122aSAlex Deucher #include <linux/hwmon.h>
2921a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
307433874eSRafał Miłecki 
31c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
32c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3373a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
34c913e23aSRafał Miłecki 
35f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
36eb2c27a0SAlex Deucher 	"",
37f712d0c7SRafał Miłecki 	"Powersave",
38f712d0c7SRafał Miłecki 	"Battery",
39f712d0c7SRafał Miłecki 	"Balanced",
40f712d0c7SRafał Miłecki 	"Performance",
41f712d0c7SRafał Miłecki };
42f712d0c7SRafał Miłecki 
43ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
44c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
45ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
46ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
47ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
48ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
49ce8f5370SAlex Deucher 
50a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
51a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
52a4c9e2eeSAlex Deucher 			     int instance)
53a4c9e2eeSAlex Deucher {
54a4c9e2eeSAlex Deucher 	int i;
55a4c9e2eeSAlex Deucher 	int found_instance = -1;
56a4c9e2eeSAlex Deucher 
57a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
58a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
59a4c9e2eeSAlex Deucher 			found_instance++;
60a4c9e2eeSAlex Deucher 			if (found_instance == instance)
61a4c9e2eeSAlex Deucher 				return i;
62a4c9e2eeSAlex Deucher 		}
63a4c9e2eeSAlex Deucher 	}
64a4c9e2eeSAlex Deucher 	/* return default if no match */
65a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
66a4c9e2eeSAlex Deucher }
67a4c9e2eeSAlex Deucher 
68c4917074SAlex Deucher void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
69ce8f5370SAlex Deucher {
701c71bda0SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
711c71bda0SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
721c71bda0SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
731c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = true;
741c71bda0SAlex Deucher 		else
751c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = false;
761c71bda0SAlex Deucher 		if (rdev->asic->dpm.enable_bapm)
771c71bda0SAlex Deucher 			radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
781c71bda0SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
791c71bda0SAlex Deucher         } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
80ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
81ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
82ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
83ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
84ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
85ce8f5370SAlex Deucher 		}
86ce8f5370SAlex Deucher 	}
87ce8f5370SAlex Deucher }
88ce8f5370SAlex Deucher 
89ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
90ce8f5370SAlex Deucher {
91ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
92ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
93ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
94ce8f5370SAlex Deucher 		break;
95ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
96ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
97ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
98ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
99ce8f5370SAlex Deucher 			else
100ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
101ce8f5370SAlex Deucher 		} else {
102ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
103c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
104ce8f5370SAlex Deucher 			else
105c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
106ce8f5370SAlex Deucher 		}
107ce8f5370SAlex Deucher 		break;
108ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
109ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
110ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
111ce8f5370SAlex Deucher 		else
112ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
113ce8f5370SAlex Deucher 		break;
114c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
115c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
116c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
117c9e75b21SAlex Deucher 		else
118c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
119c9e75b21SAlex Deucher 		break;
120ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
121ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
122ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
123ce8f5370SAlex Deucher 		else
124ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
125ce8f5370SAlex Deucher 		break;
126ce8f5370SAlex Deucher 	}
127ce8f5370SAlex Deucher 
128ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
129ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
130ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
131ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
132ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
133ce8f5370SAlex Deucher 	} else {
134ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
135ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
136ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
137ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
138ce8f5370SAlex Deucher 	}
139ce8f5370SAlex Deucher }
140c913e23aSRafał Miłecki 
1415876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1425876dd24SMatthew Garrett {
1435876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1445876dd24SMatthew Garrett 
1455876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1465876dd24SMatthew Garrett 		return;
1475876dd24SMatthew Garrett 
1485876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1495876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1505876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1515876dd24SMatthew Garrett 	}
1525876dd24SMatthew Garrett }
1535876dd24SMatthew Garrett 
154ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
155ce8f5370SAlex Deucher {
156ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
157ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
158ce8f5370SAlex Deucher 		wait_event_timeout(
159ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
160ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
161ce8f5370SAlex Deucher 	}
162ce8f5370SAlex Deucher }
163ce8f5370SAlex Deucher 
164ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
165ce8f5370SAlex Deucher {
166ce8f5370SAlex Deucher 	u32 sclk, mclk;
16792645879SAlex Deucher 	bool misc_after = false;
168ce8f5370SAlex Deucher 
169ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
170ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
171ce8f5370SAlex Deucher 		return;
172ce8f5370SAlex Deucher 
173ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
174ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
175ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1769ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1779ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
178ce8f5370SAlex Deucher 
17927810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
18027810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1817ae764b1SAlex Deucher 		 * mclk and vddci.
18227810fb2SAlex Deucher 		 */
18327810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
18427810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
18527810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
18627810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
18727810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
18827810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
18927810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
19027810fb2SAlex Deucher 		else
191ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
192ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
19327810fb2SAlex Deucher 
1949ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1959ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
196ce8f5370SAlex Deucher 
19792645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
19892645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
19992645879SAlex Deucher 			misc_after = true;
20092645879SAlex Deucher 
20192645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
20292645879SAlex Deucher 
20392645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
20492645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
20592645879SAlex Deucher 				return;
20692645879SAlex Deucher 		}
20792645879SAlex Deucher 
20892645879SAlex Deucher 		radeon_pm_prepare(rdev);
20992645879SAlex Deucher 
21092645879SAlex Deucher 		if (!misc_after)
211ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
212ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
213ce8f5370SAlex Deucher 
214ce8f5370SAlex Deucher 		/* set engine clock */
215ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
216ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
217ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
218ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
219ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
220d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
221ce8f5370SAlex Deucher 		}
222ce8f5370SAlex Deucher 
223ce8f5370SAlex Deucher 		/* set memory clock */
224798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
225ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
226ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
227ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
228ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
229d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
230ce8f5370SAlex Deucher 		}
23192645879SAlex Deucher 
23292645879SAlex Deucher 		if (misc_after)
23392645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
23492645879SAlex Deucher 			radeon_pm_misc(rdev);
23592645879SAlex Deucher 
236ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
237ce8f5370SAlex Deucher 
238ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
239ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
240ce8f5370SAlex Deucher 	} else
241d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
242ce8f5370SAlex Deucher }
243ce8f5370SAlex Deucher 
244ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
245a424816fSAlex Deucher {
2465f8f635eSJerome Glisse 	int i, r;
2472aba631cSMatthew Garrett 
2484e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2494e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2504e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2514e186b2dSAlex Deucher 		return;
2524e186b2dSAlex Deucher 
253612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
254db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
255d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2564f3218cbSAlex Deucher 
25795f5a3acSAlex Deucher 	/* wait for the rings to drain */
25895f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
25995f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2605f8f635eSJerome Glisse 		if (!ring->ready) {
2615f8f635eSJerome Glisse 			continue;
2625f8f635eSJerome Glisse 		}
2635f8f635eSJerome Glisse 		r = radeon_fence_wait_empty_locked(rdev, i);
2645f8f635eSJerome Glisse 		if (r) {
2655f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2665f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2675f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2685f8f635eSJerome Glisse 			mutex_unlock(&rdev->ddev->struct_mutex);
2695f8f635eSJerome Glisse 			return;
2705f8f635eSJerome Glisse 		}
271ce8f5370SAlex Deucher 	}
27295f5a3acSAlex Deucher 
2735876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2745876dd24SMatthew Garrett 
275ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2762aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2772aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2782aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2792aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2802aba631cSMatthew Garrett 			}
2812aba631cSMatthew Garrett 		}
2822aba631cSMatthew Garrett 	}
2832aba631cSMatthew Garrett 
284ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2852aba631cSMatthew Garrett 
286ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2872aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2882aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2892aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2902aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2912aba631cSMatthew Garrett 			}
2922aba631cSMatthew Garrett 		}
2932aba631cSMatthew Garrett 	}
294a424816fSAlex Deucher 
295a424816fSAlex Deucher 	/* update display watermarks based on new power state */
296a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
297a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
298a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
299a424816fSAlex Deucher 
300ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3012aba631cSMatthew Garrett 
302d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
303db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
304612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
305a424816fSAlex Deucher }
306a424816fSAlex Deucher 
307f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
308f712d0c7SRafał Miłecki {
309f712d0c7SRafał Miłecki 	int i, j;
310f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
311f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
312f712d0c7SRafał Miłecki 
313d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
314f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
315f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
316d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
317f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
318f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
319d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
320f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
321d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
322f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
323d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
324d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
325f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
326f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
327f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
328eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
329f712d0c7SRafał Miłecki 						 j,
330eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
331f712d0c7SRafał Miłecki 			else
332eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
333f712d0c7SRafał Miłecki 						 j,
334f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
335f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
336eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
337f712d0c7SRafał Miłecki 		}
338f712d0c7SRafał Miłecki 	}
339f712d0c7SRafał Miłecki }
340f712d0c7SRafał Miłecki 
341ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
342a424816fSAlex Deucher 				     struct device_attribute *attr,
343a424816fSAlex Deucher 				     char *buf)
344a424816fSAlex Deucher {
3453e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
346a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
347ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
348a424816fSAlex Deucher 
349a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
350ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
351ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
35212e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
353ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
354a424816fSAlex Deucher }
355a424816fSAlex Deucher 
356ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
357a424816fSAlex Deucher 				     struct device_attribute *attr,
358a424816fSAlex Deucher 				     const char *buf,
359a424816fSAlex Deucher 				     size_t count)
360a424816fSAlex Deucher {
3613e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
362a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
363a424816fSAlex Deucher 
364a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
365ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
366ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
367ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
368ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
369ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
370ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
371ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
372c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
373c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
374ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
375ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
376ce8f5370SAlex Deucher 		else {
3771783e4bfSThomas Renninger 			count = -EINVAL;
378ce8f5370SAlex Deucher 			goto fail;
379ce8f5370SAlex Deucher 		}
380ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
381ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3821783e4bfSThomas Renninger 	} else
3831783e4bfSThomas Renninger 		count = -EINVAL;
3841783e4bfSThomas Renninger 
385ce8f5370SAlex Deucher fail:
386a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
387a424816fSAlex Deucher 
388a424816fSAlex Deucher 	return count;
389a424816fSAlex Deucher }
390a424816fSAlex Deucher 
391ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
392ce8f5370SAlex Deucher 				    struct device_attribute *attr,
393ce8f5370SAlex Deucher 				    char *buf)
39456278a8eSAlex Deucher {
3953e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
396ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
397ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
39856278a8eSAlex Deucher 
399ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
400da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
401da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
40256278a8eSAlex Deucher }
40356278a8eSAlex Deucher 
404ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
405ce8f5370SAlex Deucher 				    struct device_attribute *attr,
406ce8f5370SAlex Deucher 				    const char *buf,
407ce8f5370SAlex Deucher 				    size_t count)
408d0d6cb81SRafał Miłecki {
4093e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
410ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
411ce8f5370SAlex Deucher 
412da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
413da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
414da321c8aSAlex Deucher 		count = -EINVAL;
415da321c8aSAlex Deucher 		goto fail;
416da321c8aSAlex Deucher 	}
417ce8f5370SAlex Deucher 
418ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
419ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
420ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
421ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
422ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
423ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
424ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
425ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
426ce8f5370SAlex Deucher 		/* disable dynpm */
427ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
428ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4293f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
430ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
43132c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
432ce8f5370SAlex Deucher 	} else {
4331783e4bfSThomas Renninger 		count = -EINVAL;
434ce8f5370SAlex Deucher 		goto fail;
435d0d6cb81SRafał Miłecki 	}
436ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
437ce8f5370SAlex Deucher fail:
438ce8f5370SAlex Deucher 	return count;
439ce8f5370SAlex Deucher }
440ce8f5370SAlex Deucher 
441da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
442da321c8aSAlex Deucher 				    struct device_attribute *attr,
443da321c8aSAlex Deucher 				    char *buf)
444da321c8aSAlex Deucher {
4453e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
446da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
447da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
448da321c8aSAlex Deucher 
449da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
450da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
451da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
452da321c8aSAlex Deucher }
453da321c8aSAlex Deucher 
454da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
455da321c8aSAlex Deucher 				    struct device_attribute *attr,
456da321c8aSAlex Deucher 				    const char *buf,
457da321c8aSAlex Deucher 				    size_t count)
458da321c8aSAlex Deucher {
4593e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
460da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
461da321c8aSAlex Deucher 
462da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
463da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
464da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
465da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
466da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
467da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
468da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
469da321c8aSAlex Deucher 	else {
470da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
471da321c8aSAlex Deucher 		count = -EINVAL;
472da321c8aSAlex Deucher 		goto fail;
473da321c8aSAlex Deucher 	}
474da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
475da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
476da321c8aSAlex Deucher fail:
477da321c8aSAlex Deucher 	return count;
478da321c8aSAlex Deucher }
479da321c8aSAlex Deucher 
48070d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
48170d01a5eSAlex Deucher 						       struct device_attribute *attr,
48270d01a5eSAlex Deucher 						       char *buf)
48370d01a5eSAlex Deucher {
4843e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
48570d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
48670d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
48770d01a5eSAlex Deucher 
48870d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
48970d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
49070d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
49170d01a5eSAlex Deucher }
49270d01a5eSAlex Deucher 
49370d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
49470d01a5eSAlex Deucher 						       struct device_attribute *attr,
49570d01a5eSAlex Deucher 						       const char *buf,
49670d01a5eSAlex Deucher 						       size_t count)
49770d01a5eSAlex Deucher {
4983e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
49970d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
50070d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
50170d01a5eSAlex Deucher 	int ret = 0;
50270d01a5eSAlex Deucher 
50370d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
50470d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
50570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
50670d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
50770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
50870d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
50970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
51070d01a5eSAlex Deucher 	} else {
51170d01a5eSAlex Deucher 		count = -EINVAL;
51270d01a5eSAlex Deucher 		goto fail;
51370d01a5eSAlex Deucher 	}
51470d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5150a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5160a17af37SAlex Deucher 			count = -EINVAL;
5170a17af37SAlex Deucher 			goto fail;
5180a17af37SAlex Deucher 		}
51970d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
52070d01a5eSAlex Deucher 		if (ret)
52170d01a5eSAlex Deucher 			count = -EINVAL;
52270d01a5eSAlex Deucher 	}
52370d01a5eSAlex Deucher fail:
5240a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5250a17af37SAlex Deucher 
52670d01a5eSAlex Deucher 	return count;
52770d01a5eSAlex Deucher }
52870d01a5eSAlex Deucher 
529ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
530ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
531da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
53270d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
53370d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
53470d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
535ce8f5370SAlex Deucher 
53621a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
53721a8122aSAlex Deucher 				      struct device_attribute *attr,
53821a8122aSAlex Deucher 				      char *buf)
53921a8122aSAlex Deucher {
5403e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
54121a8122aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
54220d391d7SAlex Deucher 	int temp;
54321a8122aSAlex Deucher 
5446bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
5456bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
5466bd1c385SAlex Deucher 	else
54721a8122aSAlex Deucher 		temp = 0;
54821a8122aSAlex Deucher 
54921a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
55021a8122aSAlex Deucher }
55121a8122aSAlex Deucher 
5526ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
5536ea4e84dSJean Delvare 					     struct device_attribute *attr,
5546ea4e84dSJean Delvare 					     char *buf)
5556ea4e84dSJean Delvare {
5566ea4e84dSJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
5576ea4e84dSJean Delvare 	struct radeon_device *rdev = ddev->dev_private;
5586ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
5596ea4e84dSJean Delvare 	int temp;
5606ea4e84dSJean Delvare 
5616ea4e84dSJean Delvare 	if (hyst)
5626ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
5636ea4e84dSJean Delvare 	else
5646ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
5656ea4e84dSJean Delvare 
5666ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
5676ea4e84dSJean Delvare }
5686ea4e84dSJean Delvare 
56921a8122aSAlex Deucher static ssize_t radeon_hwmon_show_name(struct device *dev,
57021a8122aSAlex Deucher 				      struct device_attribute *attr,
57121a8122aSAlex Deucher 				      char *buf)
57221a8122aSAlex Deucher {
57321a8122aSAlex Deucher 	return sprintf(buf, "radeon\n");
57421a8122aSAlex Deucher }
57521a8122aSAlex Deucher 
57621a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
5776ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
5786ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
57921a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
58021a8122aSAlex Deucher 
58121a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
58221a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
5836ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
5846ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
58521a8122aSAlex Deucher 	&sensor_dev_attr_name.dev_attr.attr,
58621a8122aSAlex Deucher 	NULL
58721a8122aSAlex Deucher };
58821a8122aSAlex Deucher 
5896ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
5906ea4e84dSJean Delvare 					struct attribute *attr, int index)
5916ea4e84dSJean Delvare {
5926ea4e84dSJean Delvare 	struct device *dev = container_of(kobj, struct device, kobj);
5936ea4e84dSJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
5946ea4e84dSJean Delvare 	struct radeon_device *rdev = ddev->dev_private;
5956ea4e84dSJean Delvare 
5966ea4e84dSJean Delvare 	/* Skip limit attributes if DPM is not enabled */
5976ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
5986ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
5996ea4e84dSJean Delvare 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
6006ea4e84dSJean Delvare 		return 0;
6016ea4e84dSJean Delvare 
6026ea4e84dSJean Delvare 	return attr->mode;
6036ea4e84dSJean Delvare }
6046ea4e84dSJean Delvare 
60521a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
60621a8122aSAlex Deucher 	.attrs = hwmon_attributes,
6076ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
60821a8122aSAlex Deucher };
60921a8122aSAlex Deucher 
6100d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
61121a8122aSAlex Deucher {
6120d18abedSDan Carpenter 	int err = 0;
61321a8122aSAlex Deucher 
61421a8122aSAlex Deucher 	rdev->pm.int_hwmon_dev = NULL;
61521a8122aSAlex Deucher 
61621a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
61721a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
61821a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
61921a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
620457558edSAlex Deucher 	case THERMAL_TYPE_NI:
621e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
6221bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
623286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
624286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
6256bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
6265d7486c7SAlex Deucher 			return err;
62721a8122aSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
6280d18abedSDan Carpenter 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
6290d18abedSDan Carpenter 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
6300d18abedSDan Carpenter 			dev_err(rdev->dev,
6310d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
6320d18abedSDan Carpenter 			break;
6330d18abedSDan Carpenter 		}
63421a8122aSAlex Deucher 		dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
63521a8122aSAlex Deucher 		err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
63621a8122aSAlex Deucher 					 &hwmon_attrgroup);
6370d18abedSDan Carpenter 		if (err) {
6380d18abedSDan Carpenter 			dev_err(rdev->dev,
6390d18abedSDan Carpenter 				"Unable to create hwmon sysfs file: %d\n", err);
6400d18abedSDan Carpenter 			hwmon_device_unregister(rdev->dev);
6410d18abedSDan Carpenter 		}
64221a8122aSAlex Deucher 		break;
64321a8122aSAlex Deucher 	default:
64421a8122aSAlex Deucher 		break;
64521a8122aSAlex Deucher 	}
6460d18abedSDan Carpenter 
6470d18abedSDan Carpenter 	return err;
64821a8122aSAlex Deucher }
64921a8122aSAlex Deucher 
65021a8122aSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
65121a8122aSAlex Deucher {
65221a8122aSAlex Deucher 	if (rdev->pm.int_hwmon_dev) {
65321a8122aSAlex Deucher 		sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
65421a8122aSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
65521a8122aSAlex Deucher 	}
65621a8122aSAlex Deucher }
65721a8122aSAlex Deucher 
658da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
659da321c8aSAlex Deucher {
660da321c8aSAlex Deucher 	struct radeon_device *rdev =
661da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
662da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
663da321c8aSAlex Deucher 	/* switch to the thermal state */
664da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
665da321c8aSAlex Deucher 
666da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
667da321c8aSAlex Deucher 		return;
668da321c8aSAlex Deucher 
669da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
670da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
671da321c8aSAlex Deucher 
672da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
673da321c8aSAlex Deucher 			/* switch back the user state */
674da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
675da321c8aSAlex Deucher 	} else {
676da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
677da321c8aSAlex Deucher 			/* switch back the user state */
678da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
679da321c8aSAlex Deucher 	}
68060320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
68160320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
68260320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
68360320347SAlex Deucher 	else
68460320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
68560320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
68660320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
68760320347SAlex Deucher 
68860320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
689da321c8aSAlex Deucher }
690da321c8aSAlex Deucher 
691da321c8aSAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
692da321c8aSAlex Deucher 						     enum radeon_pm_state_type dpm_state)
693da321c8aSAlex Deucher {
694da321c8aSAlex Deucher 	int i;
695da321c8aSAlex Deucher 	struct radeon_ps *ps;
696da321c8aSAlex Deucher 	u32 ui_class;
69748783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
69848783069SAlex Deucher 		true : false;
69948783069SAlex Deucher 
70048783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
70148783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
70248783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
70348783069SAlex Deucher 			single_display = false;
70448783069SAlex Deucher 	}
705da321c8aSAlex Deucher 
706edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
707edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
708edcaa5b1SAlex Deucher 	 */
709edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
710edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
711da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
712da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
713da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
714da321c8aSAlex Deucher 
715edcaa5b1SAlex Deucher restart_search:
716da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
717da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
718da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
719da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
720da321c8aSAlex Deucher 		switch (dpm_state) {
721da321c8aSAlex Deucher 		/* user states */
722da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
723da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
724da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
72548783069SAlex Deucher 					if (single_display)
726da321c8aSAlex Deucher 						return ps;
727da321c8aSAlex Deucher 				} else
728da321c8aSAlex Deucher 					return ps;
729da321c8aSAlex Deucher 			}
730da321c8aSAlex Deucher 			break;
731da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
732da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
733da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
73448783069SAlex Deucher 					if (single_display)
735da321c8aSAlex Deucher 						return ps;
736da321c8aSAlex Deucher 				} else
737da321c8aSAlex Deucher 					return ps;
738da321c8aSAlex Deucher 			}
739da321c8aSAlex Deucher 			break;
740da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
741da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
742da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
74348783069SAlex Deucher 					if (single_display)
744da321c8aSAlex Deucher 						return ps;
745da321c8aSAlex Deucher 				} else
746da321c8aSAlex Deucher 					return ps;
747da321c8aSAlex Deucher 			}
748da321c8aSAlex Deucher 			break;
749da321c8aSAlex Deucher 		/* internal states */
750da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
751d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
752da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
753d4d3278cSAlex Deucher 			else
754d4d3278cSAlex Deucher 				break;
755da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
756da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
757da321c8aSAlex Deucher 				return ps;
758da321c8aSAlex Deucher 			break;
759da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
760da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
761da321c8aSAlex Deucher 				return ps;
762da321c8aSAlex Deucher 			break;
763da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
764da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
765da321c8aSAlex Deucher 				return ps;
766da321c8aSAlex Deucher 			break;
767da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
768da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
769da321c8aSAlex Deucher 				return ps;
770da321c8aSAlex Deucher 			break;
771da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
772da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
773da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
774da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
775da321c8aSAlex Deucher 				return ps;
776da321c8aSAlex Deucher 			break;
777da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
778da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
779da321c8aSAlex Deucher 				return ps;
780da321c8aSAlex Deucher 			break;
781da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
782da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
783da321c8aSAlex Deucher 				return ps;
784da321c8aSAlex Deucher 			break;
785edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
786edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
787edcaa5b1SAlex Deucher 				return ps;
788edcaa5b1SAlex Deucher 			break;
789da321c8aSAlex Deucher 		default:
790da321c8aSAlex Deucher 			break;
791da321c8aSAlex Deucher 		}
792da321c8aSAlex Deucher 	}
793da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
794da321c8aSAlex Deucher 	switch (dpm_state) {
795da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
796ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
797ce3537d5SAlex Deucher 		goto restart_search;
798da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
799da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
800da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
801d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
802da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
803d4d3278cSAlex Deucher 		} else {
804d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
805d4d3278cSAlex Deucher 			goto restart_search;
806d4d3278cSAlex Deucher 		}
807da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
808da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
809da321c8aSAlex Deucher 		goto restart_search;
810da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
811da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
812da321c8aSAlex Deucher 		goto restart_search;
813da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
814edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
815edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
816da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
817da321c8aSAlex Deucher 		goto restart_search;
818da321c8aSAlex Deucher 	default:
819da321c8aSAlex Deucher 		break;
820da321c8aSAlex Deucher 	}
821da321c8aSAlex Deucher 
822da321c8aSAlex Deucher 	return NULL;
823da321c8aSAlex Deucher }
824da321c8aSAlex Deucher 
825da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
826da321c8aSAlex Deucher {
827da321c8aSAlex Deucher 	int i;
828da321c8aSAlex Deucher 	struct radeon_ps *ps;
829da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
83084dd1928SAlex Deucher 	int ret;
831da321c8aSAlex Deucher 
832da321c8aSAlex Deucher 	/* if dpm init failed */
833da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
834da321c8aSAlex Deucher 		return;
835da321c8aSAlex Deucher 
836da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
837da321c8aSAlex Deucher 		/* add other state override checks here */
8388a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
8398a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
840da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
841da321c8aSAlex Deucher 	}
842da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
843da321c8aSAlex Deucher 
844da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
845da321c8aSAlex Deucher 	if (ps)
84689c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
847da321c8aSAlex Deucher 	else
848da321c8aSAlex Deucher 		return;
849da321c8aSAlex Deucher 
850d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
851da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
852d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
853d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
854d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
855d22b7e40SAlex Deucher 			 */
856da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
857d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
858da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
859da321c8aSAlex Deucher 				/* update displays */
860da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
861da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
862da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
863da321c8aSAlex Deucher 			}
864da321c8aSAlex Deucher 			return;
865d22b7e40SAlex Deucher 		} else {
866d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
867d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
868d22b7e40SAlex Deucher 			 * update display configuration.
869d22b7e40SAlex Deucher 			 */
870d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
871d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
872d22b7e40SAlex Deucher 				return;
873d22b7e40SAlex Deucher 			} else {
874d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
875d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
876d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
877d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
878d22b7e40SAlex Deucher 					/* update displays */
879d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
880d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
881d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
882d22b7e40SAlex Deucher 					return;
883d22b7e40SAlex Deucher 				}
884d22b7e40SAlex Deucher 			}
885d22b7e40SAlex Deucher 		}
886da321c8aSAlex Deucher 	}
887da321c8aSAlex Deucher 
888033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
889da321c8aSAlex Deucher 		printk("switching from power state:\n");
890da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
891da321c8aSAlex Deucher 		printk("switching to power state:\n");
892da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
893033a37dfSAlex Deucher 	}
894da321c8aSAlex Deucher 	mutex_lock(&rdev->ddev->struct_mutex);
895da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
896da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
897da321c8aSAlex Deucher 
89884dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
89984dd1928SAlex Deucher 	if (ret)
90084dd1928SAlex Deucher 		goto done;
90184dd1928SAlex Deucher 
902da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
903da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
904da321c8aSAlex Deucher 	/* update displays */
905da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
906da321c8aSAlex Deucher 
907da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
908da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
909da321c8aSAlex Deucher 
910da321c8aSAlex Deucher 	/* wait for the rings to drain */
911da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
912da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
913da321c8aSAlex Deucher 		if (ring->ready)
914da321c8aSAlex Deucher 			radeon_fence_wait_empty_locked(rdev, i);
915da321c8aSAlex Deucher 	}
916da321c8aSAlex Deucher 
917da321c8aSAlex Deucher 	/* program the new power state */
918da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
919da321c8aSAlex Deucher 
920da321c8aSAlex Deucher 	/* update current power state */
921da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
922da321c8aSAlex Deucher 
92384dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
92484dd1928SAlex Deucher 
9251cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
926*14ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
927*14ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
92860320347SAlex Deucher 			/* force low perf level for thermal */
92960320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
930*14ac88afSAlex Deucher 			/* save the user's level */
931*14ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
932*14ac88afSAlex Deucher 		} else {
933*14ac88afSAlex Deucher 			/* otherwise, user selected level */
934*14ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
935*14ac88afSAlex Deucher 		}
93660320347SAlex Deucher 	}
93760320347SAlex Deucher 
93884dd1928SAlex Deucher done:
939da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
940da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
941da321c8aSAlex Deucher 	mutex_unlock(&rdev->ddev->struct_mutex);
942da321c8aSAlex Deucher }
943da321c8aSAlex Deucher 
944ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
945ce3537d5SAlex Deucher {
946ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
947ce3537d5SAlex Deucher 
9489e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
9499e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
9509e9d9762SAlex Deucher 		/* enable/disable UVD */
9519e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
9529e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
9539e9d9762SAlex Deucher 	} else {
954ce3537d5SAlex Deucher 		if (enable) {
955ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
956ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
957ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
958ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
959ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
960ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
961ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
962ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
963ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
964ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
965ce3537d5SAlex Deucher 			else
966ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
967ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
968ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
969ce3537d5SAlex Deucher 		} else {
970ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
971ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
972ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
973ce3537d5SAlex Deucher 		}
974ce3537d5SAlex Deucher 
975ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
976ce3537d5SAlex Deucher 	}
9779e9d9762SAlex Deucher }
978ce3537d5SAlex Deucher 
979da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
980ce8f5370SAlex Deucher {
981ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
9823f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
9833f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
9843f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
9853f53eb6fSRafael J. Wysocki 	}
986ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
98732c87fcaSTejun Heo 
98832c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
989ce8f5370SAlex Deucher }
990ce8f5370SAlex Deucher 
991da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
992da321c8aSAlex Deucher {
993da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
994da321c8aSAlex Deucher 	/* disable dpm */
995da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
996da321c8aSAlex Deucher 	/* reset the power state */
997da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
998da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
999da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1000da321c8aSAlex Deucher }
1001da321c8aSAlex Deucher 
1002da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1003da321c8aSAlex Deucher {
1004da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1005da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1006da321c8aSAlex Deucher 	else
1007da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1008da321c8aSAlex Deucher }
1009da321c8aSAlex Deucher 
1010da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1011ce8f5370SAlex Deucher {
1012ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
10132e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
101436099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
10152e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1016ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
10178a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
10188a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
10192feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
10202feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
10212feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1022ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1023ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1024ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1025ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1026ed18a360SAlex Deucher 	}
1027f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1028f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1029f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1030f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
10319ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
10329ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
10334d60173fSAlex Deucher 	rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
10342feea49aSAlex Deucher 	rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
10353f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
10363f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
10373f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
103832c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
10393f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
10403f53eb6fSRafael J. Wysocki 	}
1041f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1042ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1043d0d6cb81SRafał Miłecki }
1044d0d6cb81SRafał Miłecki 
1045da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
10467433874eSRafał Miłecki {
104726481fb1SDave Airlie 	int ret;
10480d18abedSDan Carpenter 
1049da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1050da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1051da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1052da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1053da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1054da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1055da321c8aSAlex Deucher 	if (ret) {
1056da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm resume failed\n");
1057da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
105836099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
1059da321c8aSAlex Deucher 		    rdev->mc_fw) {
1060da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
1061da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1062da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
1063da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
1064da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1065da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1066da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
1067da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1068da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
1069da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1070da321c8aSAlex Deucher 		}
1071da321c8aSAlex Deucher 	} else {
1072da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = true;
1073da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
1074da321c8aSAlex Deucher 	}
1075da321c8aSAlex Deucher }
1076da321c8aSAlex Deucher 
1077da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1078da321c8aSAlex Deucher {
1079da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1080da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1081da321c8aSAlex Deucher 	else
1082da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1083da321c8aSAlex Deucher }
1084da321c8aSAlex Deucher 
1085da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1086da321c8aSAlex Deucher {
1087da321c8aSAlex Deucher 	int ret;
1088da321c8aSAlex Deucher 
1089f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1090ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1091ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1092ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1093ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
10949ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
10959ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1096f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1097f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
109821a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1099c913e23aSRafał Miłecki 
110056278a8eSAlex Deucher 	if (rdev->bios) {
110156278a8eSAlex Deucher 		if (rdev->is_atom_bios)
110256278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
110356278a8eSAlex Deucher 		else
110456278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1105f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1106ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1107ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
11082e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
110936099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
11102e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1111ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
11128a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
11138a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
11144639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
11154639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
11164639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1117ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1118ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1119ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1120ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1121ed18a360SAlex Deucher 		}
112256278a8eSAlex Deucher 	}
112356278a8eSAlex Deucher 
112421a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
11250d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
11260d18abedSDan Carpenter 	if (ret)
11270d18abedSDan Carpenter 		return ret;
112832c87fcaSTejun Heo 
112932c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
113032c87fcaSTejun Heo 
1131ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1132ce8f5370SAlex Deucher 		/* where's the best place to put these? */
113326481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
113426481fb1SDave Airlie 		if (ret)
113526481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
113626481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
113726481fb1SDave Airlie 		if (ret)
113826481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
1139ce8f5370SAlex Deucher 
11407433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1141c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
11427433874eSRafał Miłecki 		}
11437433874eSRafał Miłecki 
1144c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1145ce8f5370SAlex Deucher 	}
1146c913e23aSRafał Miłecki 
11477433874eSRafał Miłecki 	return 0;
11487433874eSRafał Miłecki }
11497433874eSRafał Miłecki 
1150da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1151da321c8aSAlex Deucher {
1152da321c8aSAlex Deucher 	int i;
1153da321c8aSAlex Deucher 
1154da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1155da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1156da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1157da321c8aSAlex Deucher 	}
1158da321c8aSAlex Deucher }
1159da321c8aSAlex Deucher 
1160da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1161da321c8aSAlex Deucher {
1162da321c8aSAlex Deucher 	int ret;
1163da321c8aSAlex Deucher 
11641cd8b21aSAlex Deucher 	/* default to balanced state */
1165edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1166edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
11671cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1168da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1169da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1170da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1171da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1172da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1173da321c8aSAlex Deucher 
1174da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1175da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1176da321c8aSAlex Deucher 	else
1177da321c8aSAlex Deucher 		return -EINVAL;
1178da321c8aSAlex Deucher 
1179da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1180da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1181da321c8aSAlex Deucher 	if (ret)
1182da321c8aSAlex Deucher 		return ret;
1183da321c8aSAlex Deucher 
1184da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1185da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1186da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1187da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1188033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1189da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1190da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1191da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1192da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1193da321c8aSAlex Deucher 	if (ret) {
1194da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = false;
1195da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
119636099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
1197da321c8aSAlex Deucher 		    rdev->mc_fw) {
1198da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
1199da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1200da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
1201da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
1202da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1203da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1204da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
1205da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1206da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
1207da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1208da321c8aSAlex Deucher 		}
1209da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm initialization failed\n");
1210da321c8aSAlex Deucher 		return ret;
1211da321c8aSAlex Deucher 	}
1212da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1213da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
1214da321c8aSAlex Deucher 
1215da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1216da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1217da321c8aSAlex Deucher 		if (ret)
1218da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for dpm state\n");
121970d01a5eSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
122070d01a5eSAlex Deucher 		if (ret)
122170d01a5eSAlex Deucher 			DRM_ERROR("failed to create device file for dpm state\n");
1222da321c8aSAlex Deucher 		/* XXX: these are noops for dpm but are here for backwards compat */
1223da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1224da321c8aSAlex Deucher 		if (ret)
1225da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for power profile\n");
1226da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
1227da321c8aSAlex Deucher 		if (ret)
1228da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for power method\n");
12291316b792SAlex Deucher 
12301316b792SAlex Deucher 		if (radeon_debugfs_pm_init(rdev)) {
12311316b792SAlex Deucher 			DRM_ERROR("Failed to register debugfs file for dpm!\n");
12321316b792SAlex Deucher 		}
12331316b792SAlex Deucher 
1234da321c8aSAlex Deucher 		DRM_INFO("radeon: dpm initialized\n");
1235da321c8aSAlex Deucher 	}
1236da321c8aSAlex Deucher 
1237da321c8aSAlex Deucher 	return 0;
1238da321c8aSAlex Deucher }
1239da321c8aSAlex Deucher 
1240da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1241da321c8aSAlex Deucher {
1242da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1243da321c8aSAlex Deucher 	switch (rdev->family) {
12444a6369e9SAlex Deucher 	case CHIP_RV610:
12454a6369e9SAlex Deucher 	case CHIP_RV630:
12464a6369e9SAlex Deucher 	case CHIP_RV620:
12474a6369e9SAlex Deucher 	case CHIP_RV635:
12484a6369e9SAlex Deucher 	case CHIP_RV670:
12499d67006eSAlex Deucher 	case CHIP_RS780:
12509d67006eSAlex Deucher 	case CHIP_RS880:
125169e0b57aSAlex Deucher 	case CHIP_CAYMAN:
1252d70229f7SAlex Deucher 	case CHIP_ARUBA:
1253cc8dbbb4SAlex Deucher 	case CHIP_BONAIRE:
125441a524abSAlex Deucher 	case CHIP_KABINI:
125541a524abSAlex Deucher 	case CHIP_KAVERI:
12568a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1257761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1258761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12598a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
12608a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
12618a53fa23SAlex Deucher 			 (!rdev->smc_fw))
12628a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1263761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
12649d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
12659d67006eSAlex Deucher 		else
12669d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12679d67006eSAlex Deucher 		break;
1268ab70b1ddSAlex Deucher 	case CHIP_RV770:
1269ab70b1ddSAlex Deucher 	case CHIP_RV730:
1270ab70b1ddSAlex Deucher 	case CHIP_RV710:
1271ab70b1ddSAlex Deucher 	case CHIP_RV740:
127259f7a2f2SAlex Deucher 	case CHIP_CEDAR:
127359f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
127459f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
127559f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
127659f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
12775a16f761SAlex Deucher 	case CHIP_PALM:
12785a16f761SAlex Deucher 	case CHIP_SUMO:
12795a16f761SAlex Deucher 	case CHIP_SUMO2:
128056684ec5SAlex Deucher 	case CHIP_BARTS:
128156684ec5SAlex Deucher 	case CHIP_TURKS:
128256684ec5SAlex Deucher 	case CHIP_CAICOS:
128368bc7785SAlex Deucher 	case CHIP_TAHITI:
128468bc7785SAlex Deucher 	case CHIP_PITCAIRN:
128568bc7785SAlex Deucher 	case CHIP_VERDE:
128668bc7785SAlex Deucher 	case CHIP_OLAND:
128768bc7785SAlex Deucher 	case CHIP_HAINAN:
12885a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
12895a16f761SAlex Deucher 		if (!rdev->rlc_fw)
12905a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12915a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
12925a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
12935a16f761SAlex Deucher 			 (!rdev->smc_fw))
12945a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12955a16f761SAlex Deucher 		else if (radeon_dpm == 0)
12965a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
12975a16f761SAlex Deucher 		else
12985a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
12995a16f761SAlex Deucher 		break;
1300da321c8aSAlex Deucher 	default:
1301da321c8aSAlex Deucher 		/* default to profile method */
1302da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1303da321c8aSAlex Deucher 		break;
1304da321c8aSAlex Deucher 	}
1305da321c8aSAlex Deucher 
1306da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1307da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1308da321c8aSAlex Deucher 	else
1309da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1310da321c8aSAlex Deucher }
1311da321c8aSAlex Deucher 
1312da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
131329fb52caSAlex Deucher {
1314ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1315a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1316ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1317ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1318ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1319ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1320ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1321ce8f5370SAlex Deucher 			/* reset default clocks */
1322ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1323ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1324ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
132558e21dffSAlex Deucher 		}
1326ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
132732c87fcaSTejun Heo 
132832c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
132958e21dffSAlex Deucher 
1330ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1331ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1332ce8f5370SAlex Deucher 	}
1333a424816fSAlex Deucher 
13340975b162SAlex Deucher 	if (rdev->pm.power_state)
13350975b162SAlex Deucher 		kfree(rdev->pm.power_state);
13360975b162SAlex Deucher 
133721a8122aSAlex Deucher 	radeon_hwmon_fini(rdev);
133829fb52caSAlex Deucher }
133929fb52caSAlex Deucher 
1340da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1341da321c8aSAlex Deucher {
1342da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1343da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1344da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1345da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1346da321c8aSAlex Deucher 
1347da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
134870d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1349da321c8aSAlex Deucher 		/* XXX backwards compat */
1350da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1351da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1352da321c8aSAlex Deucher 	}
1353da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1354da321c8aSAlex Deucher 
1355da321c8aSAlex Deucher 	if (rdev->pm.power_state)
1356da321c8aSAlex Deucher 		kfree(rdev->pm.power_state);
1357da321c8aSAlex Deucher 
1358da321c8aSAlex Deucher 	radeon_hwmon_fini(rdev);
1359da321c8aSAlex Deucher }
1360da321c8aSAlex Deucher 
1361da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1362da321c8aSAlex Deucher {
1363da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1364da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1365da321c8aSAlex Deucher 	else
1366da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1367da321c8aSAlex Deucher }
1368da321c8aSAlex Deucher 
1369da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1370c913e23aSRafał Miłecki {
1371c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1372a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1373c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1374c913e23aSRafał Miłecki 
1375ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1376ce8f5370SAlex Deucher 		return;
1377ce8f5370SAlex Deucher 
1378c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1379c913e23aSRafał Miłecki 
1380c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1381a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
1382a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
1383a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1384a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1385a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
1386c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1387a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
1388c913e23aSRafał Miłecki 		}
1389c913e23aSRafał Miłecki 	}
1390c913e23aSRafał Miłecki 
1391ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1392ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1393ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1394ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1395ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1396a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1397ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1398ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1399c913e23aSRafał Miłecki 
1400ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1401ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1402ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1403ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1404c913e23aSRafał Miłecki 
1405d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1406c913e23aSRafał Miłecki 				}
1407a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1408c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1409c913e23aSRafał Miłecki 
1410ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1411ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1412ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1413ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1414ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1415c913e23aSRafał Miłecki 
141632c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1417c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1418ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1419ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
142032c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1421c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1422d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1423c913e23aSRafał Miłecki 				}
1424a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1425ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1426ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1427c913e23aSRafał Miłecki 
1428ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1429ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1430ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1431ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1432ce8f5370SAlex Deucher 				}
1433ce8f5370SAlex Deucher 			}
143473a6d3fcSRafał Miłecki 		}
1435c913e23aSRafał Miłecki 	}
1436c913e23aSRafał Miłecki 
1437c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1438c913e23aSRafał Miłecki }
1439c913e23aSRafał Miłecki 
1440da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1441da321c8aSAlex Deucher {
1442da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1443da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1444da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1445da321c8aSAlex Deucher 
1446da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1447da321c8aSAlex Deucher 
14485ca302f7SAlex Deucher 	/* update active crtc counts */
1449da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1450da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
1451da321c8aSAlex Deucher 	list_for_each_entry(crtc,
1452da321c8aSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1453da321c8aSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1454da321c8aSAlex Deucher 		if (crtc->enabled) {
1455da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1456da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtc_count++;
1457da321c8aSAlex Deucher 		}
1458da321c8aSAlex Deucher 	}
1459da321c8aSAlex Deucher 
14605ca302f7SAlex Deucher 	/* update battery/ac status */
14615ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
14625ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
14635ca302f7SAlex Deucher 	else
14645ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
14655ca302f7SAlex Deucher 
1466da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1467da321c8aSAlex Deucher 
1468da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
14698a227555SAlex Deucher 
1470da321c8aSAlex Deucher }
1471da321c8aSAlex Deucher 
1472da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1473da321c8aSAlex Deucher {
1474da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1475da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1476da321c8aSAlex Deucher 	else
1477da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1478da321c8aSAlex Deucher }
1479da321c8aSAlex Deucher 
1480ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1481f735261bSDave Airlie {
148275fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1483f735261bSDave Airlie 	bool in_vbl = true;
1484f735261bSDave Airlie 
148575fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
148675fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
148775fa0b08SMario Kleiner 	 */
148875fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
148975fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
1490f5a80209SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
1491f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1492f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
1493f735261bSDave Airlie 				in_vbl = false;
1494f735261bSDave Airlie 		}
1495f735261bSDave Airlie 	}
1496f81f2024SMatthew Garrett 
1497f81f2024SMatthew Garrett 	return in_vbl;
1498f81f2024SMatthew Garrett }
1499f81f2024SMatthew Garrett 
1500ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1501f81f2024SMatthew Garrett {
1502f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1503f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1504f81f2024SMatthew Garrett 
1505f735261bSDave Airlie 	if (in_vbl == false)
1506d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1507bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1508f735261bSDave Airlie 	return in_vbl;
1509f735261bSDave Airlie }
1510c913e23aSRafał Miłecki 
1511ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1512c913e23aSRafał Miłecki {
1513c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1514d9932a32SMatthew Garrett 	int resched;
1515c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1516ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1517c913e23aSRafał Miłecki 
1518d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1519c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1520ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1521c913e23aSRafał Miłecki 		int not_processed = 0;
15227465280cSAlex Deucher 		int i;
1523c913e23aSRafał Miłecki 
15247465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
15250ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
15260ec0612aSAlex Deucher 
15270ec0612aSAlex Deucher 			if (ring->ready) {
152847492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
15297465280cSAlex Deucher 				if (not_processed >= 3)
15307465280cSAlex Deucher 					break;
15317465280cSAlex Deucher 			}
15320ec0612aSAlex Deucher 		}
1533c913e23aSRafał Miłecki 
1534c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1535ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1536ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1537ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1538ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1539ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1540ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1541ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1542c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1543c913e23aSRafał Miłecki 			}
1544c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1545ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1546ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1547ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1548ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1549ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1550ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1551ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1552c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1553c913e23aSRafał Miłecki 			}
1554c913e23aSRafał Miłecki 		}
1555c913e23aSRafał Miłecki 
1556d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1557d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1558d7311171SAlex Deucher 		 */
1559ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1560ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1561ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1562ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1563c913e23aSRafał Miłecki 		}
1564c913e23aSRafał Miłecki 
156532c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1566c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1567c913e23aSRafał Miłecki 	}
15683f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
15693f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
15703f53eb6fSRafael J. Wysocki }
1571c913e23aSRafał Miłecki 
15727433874eSRafał Miłecki /*
15737433874eSRafał Miłecki  * Debugfs info
15747433874eSRafał Miłecki  */
15757433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
15767433874eSRafał Miłecki 
15777433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
15787433874eSRafał Miłecki {
15797433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
15807433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
15817433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
15827433874eSRafał Miłecki 
15831316b792SAlex Deucher 	if (rdev->pm.dpm_enabled) {
15841316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
15851316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
15861316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
15871316b792SAlex Deucher 		else
158871375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
15891316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
15901316b792SAlex Deucher 	} else {
15919ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1592bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1593bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1594bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1595bf05d998SAlex Deucher 		else
15966234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
15979ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1598798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
15996234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
16000fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
16010fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1602798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1603aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
16041316b792SAlex Deucher 	}
16057433874eSRafał Miłecki 
16067433874eSRafał Miłecki 	return 0;
16077433874eSRafał Miłecki }
16087433874eSRafał Miłecki 
16097433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
16107433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
16117433874eSRafał Miłecki };
16127433874eSRafał Miłecki #endif
16137433874eSRafał Miłecki 
1614c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
16157433874eSRafał Miłecki {
16167433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
16177433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
16187433874eSRafał Miłecki #else
16197433874eSRafał Miłecki 	return 0;
16207433874eSRafał Miłecki #endif
16217433874eSRafał Miłecki }
1622