xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 052813d9fa22d52857b10356c2a7d9c53c285c72)
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  */
23f9183127SSam Ravnborg 
2421a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
25f9183127SSam Ravnborg #include <linux/hwmon.h>
262ef79416SThomas Zimmermann #include <linux/pci.h>
27f9183127SSam Ravnborg #include <linux/power_supply.h>
28f9183127SSam Ravnborg 
29f9183127SSam Ravnborg #include <drm/drm_debugfs.h>
30f9183127SSam Ravnborg #include <drm/drm_vblank.h>
31f9183127SSam Ravnborg 
32f9183127SSam Ravnborg #include "atom.h"
33f9183127SSam Ravnborg #include "avivod.h"
34f9183127SSam Ravnborg #include "r600_dpm.h"
35f9183127SSam Ravnborg #include "radeon.h"
367433874eSRafał Miłecki 
37c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
38c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3973a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
40c913e23aSRafał Miłecki 
41f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
42eb2c27a0SAlex Deucher 	"",
43f712d0c7SRafał Miłecki 	"Powersave",
44f712d0c7SRafał Miłecki 	"Battery",
45f712d0c7SRafał Miłecki 	"Balanced",
46f712d0c7SRafał Miłecki 	"Performance",
47f712d0c7SRafał Miłecki };
48f712d0c7SRafał Miłecki 
49ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
50c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
51ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
52ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
53ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
54ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
55ce8f5370SAlex Deucher 
56a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
57a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
58a4c9e2eeSAlex Deucher 			     int instance)
59a4c9e2eeSAlex Deucher {
60a4c9e2eeSAlex Deucher 	int i;
61a4c9e2eeSAlex Deucher 	int found_instance = -1;
62a4c9e2eeSAlex Deucher 
63a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
64a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
65a4c9e2eeSAlex Deucher 			found_instance++;
66a4c9e2eeSAlex Deucher 			if (found_instance == instance)
67a4c9e2eeSAlex Deucher 				return i;
68a4c9e2eeSAlex Deucher 		}
69a4c9e2eeSAlex Deucher 	}
70a4c9e2eeSAlex Deucher 	/* return default if no match */
71a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
72a4c9e2eeSAlex Deucher }
73a4c9e2eeSAlex Deucher 
74c4917074SAlex Deucher void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
75ce8f5370SAlex Deucher {
761c71bda0SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
771c71bda0SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
781c71bda0SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
791c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = true;
801c71bda0SAlex Deucher 		else
811c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = false;
8296682956SAlex Deucher 		if (rdev->family == CHIP_ARUBA) {
831c71bda0SAlex Deucher 			if (rdev->asic->dpm.enable_bapm)
841c71bda0SAlex Deucher 				radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
8596682956SAlex Deucher 		}
861c71bda0SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
871c71bda0SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
88ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
89ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
90ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
91ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
92ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
93ce8f5370SAlex Deucher 		}
94ce8f5370SAlex Deucher 	}
95ce8f5370SAlex Deucher }
96ce8f5370SAlex Deucher 
97ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
98ce8f5370SAlex Deucher {
99ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
100ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
101ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
102ce8f5370SAlex Deucher 		break;
103ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
104ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
105ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
106ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
107ce8f5370SAlex Deucher 			else
108ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
109ce8f5370SAlex Deucher 		} else {
110ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
111c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
112ce8f5370SAlex Deucher 			else
113c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
114ce8f5370SAlex Deucher 		}
115ce8f5370SAlex Deucher 		break;
116ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
117ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
118ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
119ce8f5370SAlex Deucher 		else
120ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
121ce8f5370SAlex Deucher 		break;
122c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
123c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
124c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
125c9e75b21SAlex Deucher 		else
126c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
127c9e75b21SAlex Deucher 		break;
128ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
129ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
130ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
131ce8f5370SAlex Deucher 		else
132ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
133ce8f5370SAlex Deucher 		break;
134ce8f5370SAlex Deucher 	}
135ce8f5370SAlex Deucher 
136ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
137ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
138ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
139ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
140ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
141ce8f5370SAlex Deucher 	} else {
142ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
143ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
144ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
145ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
146ce8f5370SAlex Deucher 	}
147ce8f5370SAlex Deucher }
148c913e23aSRafał Miłecki 
1495876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1505876dd24SMatthew Garrett {
1515876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1525876dd24SMatthew Garrett 
1535876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1545876dd24SMatthew Garrett 		return;
1555876dd24SMatthew Garrett 
1565876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1575876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1585876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1595876dd24SMatthew Garrett 	}
1605876dd24SMatthew Garrett }
1615876dd24SMatthew Garrett 
162ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
163ce8f5370SAlex Deucher {
164ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
165ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
166ce8f5370SAlex Deucher 		wait_event_timeout(
167ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
168ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
169ce8f5370SAlex Deucher 	}
170ce8f5370SAlex Deucher }
171ce8f5370SAlex Deucher 
172ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
173ce8f5370SAlex Deucher {
174ce8f5370SAlex Deucher 	u32 sclk, mclk;
17592645879SAlex Deucher 	bool misc_after = false;
176ce8f5370SAlex Deucher 
177ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
178ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
179ce8f5370SAlex Deucher 		return;
180ce8f5370SAlex Deucher 
181ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
182ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
183ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1849ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1859ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
186ce8f5370SAlex Deucher 
18727810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
18827810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1897ae764b1SAlex Deucher 		 * mclk and vddci.
19027810fb2SAlex Deucher 		 */
19127810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
19227810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
19327810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
19427810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
19527810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
19627810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
19727810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
19827810fb2SAlex Deucher 		else
199ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
200ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
20127810fb2SAlex Deucher 
2029ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
2039ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
204ce8f5370SAlex Deucher 
20592645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
20692645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
20792645879SAlex Deucher 			misc_after = true;
20892645879SAlex Deucher 
20992645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
21092645879SAlex Deucher 
21192645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
21292645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
21392645879SAlex Deucher 				return;
21492645879SAlex Deucher 		}
21592645879SAlex Deucher 
21692645879SAlex Deucher 		radeon_pm_prepare(rdev);
21792645879SAlex Deucher 
21892645879SAlex Deucher 		if (!misc_after)
219ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
220ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
221ce8f5370SAlex Deucher 
222ce8f5370SAlex Deucher 		/* set engine clock */
223ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
224ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
225ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
226ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
227ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
228d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
229ce8f5370SAlex Deucher 		}
230ce8f5370SAlex Deucher 
231ce8f5370SAlex Deucher 		/* set memory clock */
232798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
233ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
234ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
235ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
236ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
237d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
238ce8f5370SAlex Deucher 		}
23992645879SAlex Deucher 
24092645879SAlex Deucher 		if (misc_after)
24192645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
24292645879SAlex Deucher 			radeon_pm_misc(rdev);
24392645879SAlex Deucher 
244ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
245ce8f5370SAlex Deucher 
246ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
247ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
248ce8f5370SAlex Deucher 	} else
249d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
250ce8f5370SAlex Deucher }
251ce8f5370SAlex Deucher 
252ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
253a424816fSAlex Deucher {
254a782bca5SGustavo Padovan 	struct drm_crtc *crtc;
2555f8f635eSJerome Glisse 	int i, r;
2562aba631cSMatthew Garrett 
2574e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2584e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2594e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2604e186b2dSAlex Deucher 		return;
2614e186b2dSAlex Deucher 
262db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
263d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2644f3218cbSAlex Deucher 
26595f5a3acSAlex Deucher 	/* wait for the rings to drain */
26695f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
26795f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2685f8f635eSJerome Glisse 		if (!ring->ready) {
2695f8f635eSJerome Glisse 			continue;
2705f8f635eSJerome Glisse 		}
27137615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
2725f8f635eSJerome Glisse 		if (r) {
2735f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2745f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2755f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2765f8f635eSJerome Glisse 			return;
2775f8f635eSJerome Glisse 		}
278ce8f5370SAlex Deucher 	}
27995f5a3acSAlex Deucher 
2805876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2815876dd24SMatthew Garrett 
282ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
283a782bca5SGustavo Padovan 		i = 0;
284a782bca5SGustavo Padovan 		drm_for_each_crtc(crtc, rdev->ddev) {
2852aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
286e0b34e38SMario Kleiner 				/* This can fail if a modeset is in progress */
287a782bca5SGustavo Padovan 				if (drm_crtc_vblank_get(crtc) == 0)
2882aba631cSMatthew Garrett 					rdev->pm.req_vblank |= (1 << i);
289e0b34e38SMario Kleiner 				else
290e0b34e38SMario Kleiner 					DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
291e0b34e38SMario Kleiner 							 i);
2922aba631cSMatthew Garrett 			}
293a782bca5SGustavo Padovan 			i++;
2942aba631cSMatthew Garrett 		}
2952aba631cSMatthew Garrett 	}
2962aba631cSMatthew Garrett 
297ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2982aba631cSMatthew Garrett 
299ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
300a782bca5SGustavo Padovan 		i = 0;
301a782bca5SGustavo Padovan 		drm_for_each_crtc(crtc, rdev->ddev) {
3022aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
3032aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
304a782bca5SGustavo Padovan 				drm_crtc_vblank_put(crtc);
3052aba631cSMatthew Garrett 			}
306a782bca5SGustavo Padovan 			i++;
3072aba631cSMatthew Garrett 		}
3082aba631cSMatthew Garrett 	}
309a424816fSAlex Deucher 
310a424816fSAlex Deucher 	/* update display watermarks based on new power state */
311a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
312a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
313a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
314a424816fSAlex Deucher 
315ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3162aba631cSMatthew Garrett 
317d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
318db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
319a424816fSAlex Deucher }
320a424816fSAlex Deucher 
321f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
322f712d0c7SRafał Miłecki {
323f712d0c7SRafał Miłecki 	int i, j;
324f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
325f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
326f712d0c7SRafał Miłecki 
327d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
328f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
329f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
330d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
331f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
332f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
333d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
334f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
335d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
336f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
337d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
338d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
339f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
340f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
341f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
342eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
343f712d0c7SRafał Miłecki 						 j,
344eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
345f712d0c7SRafał Miłecki 			else
346eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
347f712d0c7SRafał Miłecki 						 j,
348f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
349f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
350eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
351f712d0c7SRafał Miłecki 		}
352f712d0c7SRafał Miłecki 	}
353f712d0c7SRafał Miłecki }
354f712d0c7SRafał Miłecki 
355ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
356a424816fSAlex Deucher 				     struct device_attribute *attr,
357a424816fSAlex Deucher 				     char *buf)
358a424816fSAlex Deucher {
3593e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
360a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
361ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
362a424816fSAlex Deucher 
363a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
364ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
365ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
36612e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
367ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
368a424816fSAlex Deucher }
369a424816fSAlex Deucher 
370ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
371a424816fSAlex Deucher 				     struct device_attribute *attr,
372a424816fSAlex Deucher 				     const char *buf,
373a424816fSAlex Deucher 				     size_t count)
374a424816fSAlex Deucher {
3753e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
376a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
377a424816fSAlex Deucher 
3784f2f2039SAlex Deucher 	/* Can't set profile when the card is off */
3794f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
3804f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
3814f2f2039SAlex Deucher 		return -EINVAL;
3824f2f2039SAlex Deucher 
383a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
384ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
385ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
386ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
387ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
388ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
389ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
390ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
391c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
392c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
393ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
394ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
395ce8f5370SAlex Deucher 		else {
3961783e4bfSThomas Renninger 			count = -EINVAL;
397ce8f5370SAlex Deucher 			goto fail;
398ce8f5370SAlex Deucher 		}
399ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
400ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
4011783e4bfSThomas Renninger 	} else
4021783e4bfSThomas Renninger 		count = -EINVAL;
4031783e4bfSThomas Renninger 
404ce8f5370SAlex Deucher fail:
405a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
406a424816fSAlex Deucher 
407a424816fSAlex Deucher 	return count;
408a424816fSAlex Deucher }
409a424816fSAlex Deucher 
410ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
411ce8f5370SAlex Deucher 				    struct device_attribute *attr,
412ce8f5370SAlex Deucher 				    char *buf)
41356278a8eSAlex Deucher {
4143e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
415ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
416ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
41756278a8eSAlex Deucher 
418ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
419da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
420da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
42156278a8eSAlex Deucher }
42256278a8eSAlex Deucher 
423ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
424ce8f5370SAlex Deucher 				    struct device_attribute *attr,
425ce8f5370SAlex Deucher 				    const char *buf,
426ce8f5370SAlex Deucher 				    size_t count)
427d0d6cb81SRafał Miłecki {
4283e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
429ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
430ce8f5370SAlex Deucher 
4314f2f2039SAlex Deucher 	/* Can't set method when the card is off */
4324f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4334f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
4344f2f2039SAlex Deucher 		count = -EINVAL;
4354f2f2039SAlex Deucher 		goto fail;
4364f2f2039SAlex Deucher 	}
4374f2f2039SAlex Deucher 
438da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
439da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
440da321c8aSAlex Deucher 		count = -EINVAL;
441da321c8aSAlex Deucher 		goto fail;
442da321c8aSAlex Deucher 	}
443ce8f5370SAlex Deucher 
444ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
445ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
446ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
447ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
448ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
449ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
450ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
451ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
452ce8f5370SAlex Deucher 		/* disable dynpm */
453ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
454ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4553f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
456ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
45732c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
458ce8f5370SAlex Deucher 	} else {
4591783e4bfSThomas Renninger 		count = -EINVAL;
460ce8f5370SAlex Deucher 		goto fail;
461d0d6cb81SRafał Miłecki 	}
462ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
463ce8f5370SAlex Deucher fail:
464ce8f5370SAlex Deucher 	return count;
465ce8f5370SAlex Deucher }
466ce8f5370SAlex Deucher 
467da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
468da321c8aSAlex Deucher 				    struct device_attribute *attr,
469da321c8aSAlex Deucher 				    char *buf)
470da321c8aSAlex Deucher {
4713e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
472da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
473da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
474da321c8aSAlex Deucher 
475da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
476da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
477da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
478da321c8aSAlex Deucher }
479da321c8aSAlex Deucher 
480da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
481da321c8aSAlex Deucher 				    struct device_attribute *attr,
482da321c8aSAlex Deucher 				    const char *buf,
483da321c8aSAlex Deucher 				    size_t count)
484da321c8aSAlex Deucher {
4853e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
486da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
487da321c8aSAlex Deucher 
488da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
489da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
490da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
491da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
492da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
493da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
494da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
495da321c8aSAlex Deucher 	else {
496da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
497da321c8aSAlex Deucher 		count = -EINVAL;
498da321c8aSAlex Deucher 		goto fail;
499da321c8aSAlex Deucher 	}
500da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
501b07a657eSPali Rohár 
502b07a657eSPali Rohár 	/* Can't set dpm state when the card is off */
503b07a657eSPali Rohár 	if (!(rdev->flags & RADEON_IS_PX) ||
504b07a657eSPali Rohár 	    (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
505da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
506b07a657eSPali Rohár 
507da321c8aSAlex Deucher fail:
508da321c8aSAlex Deucher 	return count;
509da321c8aSAlex Deucher }
510da321c8aSAlex Deucher 
51170d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
51270d01a5eSAlex Deucher 						       struct device_attribute *attr,
51370d01a5eSAlex Deucher 						       char *buf)
51470d01a5eSAlex Deucher {
5153e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
51670d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
51770d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
51870d01a5eSAlex Deucher 
5194f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5204f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5214f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
5224f2f2039SAlex Deucher 
52370d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
52470d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
52570d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
52670d01a5eSAlex Deucher }
52770d01a5eSAlex Deucher 
52870d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
52970d01a5eSAlex Deucher 						       struct device_attribute *attr,
53070d01a5eSAlex Deucher 						       const char *buf,
53170d01a5eSAlex Deucher 						       size_t count)
53270d01a5eSAlex Deucher {
5333e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
53470d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
53570d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
53670d01a5eSAlex Deucher 	int ret = 0;
53770d01a5eSAlex Deucher 
5384f2f2039SAlex Deucher 	/* Can't force performance level when the card is off */
5394f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5404f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5414f2f2039SAlex Deucher 		return -EINVAL;
5424f2f2039SAlex Deucher 
54370d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
54470d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
54570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
54670d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
54770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
54870d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
54970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
55070d01a5eSAlex Deucher 	} else {
55170d01a5eSAlex Deucher 		count = -EINVAL;
55270d01a5eSAlex Deucher 		goto fail;
55370d01a5eSAlex Deucher 	}
55470d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5550a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5560a17af37SAlex Deucher 			count = -EINVAL;
5570a17af37SAlex Deucher 			goto fail;
5580a17af37SAlex Deucher 		}
55970d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
56070d01a5eSAlex Deucher 		if (ret)
56170d01a5eSAlex Deucher 			count = -EINVAL;
56270d01a5eSAlex Deucher 	}
56370d01a5eSAlex Deucher fail:
5640a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5650a17af37SAlex Deucher 
56670d01a5eSAlex Deucher 	return count;
56770d01a5eSAlex Deucher }
56870d01a5eSAlex Deucher 
56999736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
57099736703SOleg Chernovskiy 					    struct device_attribute *attr,
57199736703SOleg Chernovskiy 					    char *buf)
57299736703SOleg Chernovskiy {
57399736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
57499736703SOleg Chernovskiy 	u32 pwm_mode = 0;
57599736703SOleg Chernovskiy 
57699736703SOleg Chernovskiy 	if (rdev->asic->dpm.fan_ctrl_get_mode)
57799736703SOleg Chernovskiy 		pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
57899736703SOleg Chernovskiy 
57999736703SOleg Chernovskiy 	/* never 0 (full-speed), fuse or smc-controlled always */
58099736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
58199736703SOleg Chernovskiy }
58299736703SOleg Chernovskiy 
58399736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
58499736703SOleg Chernovskiy 					    struct device_attribute *attr,
58599736703SOleg Chernovskiy 					    const char *buf,
58699736703SOleg Chernovskiy 					    size_t count)
58799736703SOleg Chernovskiy {
58899736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
58999736703SOleg Chernovskiy 	int err;
59099736703SOleg Chernovskiy 	int value;
59199736703SOleg Chernovskiy 
59299736703SOleg Chernovskiy 	if(!rdev->asic->dpm.fan_ctrl_set_mode)
59399736703SOleg Chernovskiy 		return -EINVAL;
59499736703SOleg Chernovskiy 
59599736703SOleg Chernovskiy 	err = kstrtoint(buf, 10, &value);
59699736703SOleg Chernovskiy 	if (err)
59799736703SOleg Chernovskiy 		return err;
59899736703SOleg Chernovskiy 
59999736703SOleg Chernovskiy 	switch (value) {
60099736703SOleg Chernovskiy 	case 1: /* manual, percent-based */
60199736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
60299736703SOleg Chernovskiy 		break;
60399736703SOleg Chernovskiy 	default: /* disable */
60499736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
60599736703SOleg Chernovskiy 		break;
60699736703SOleg Chernovskiy 	}
60799736703SOleg Chernovskiy 
60899736703SOleg Chernovskiy 	return count;
60999736703SOleg Chernovskiy }
61099736703SOleg Chernovskiy 
61199736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
61299736703SOleg Chernovskiy 					 struct device_attribute *attr,
61399736703SOleg Chernovskiy 					 char *buf)
61499736703SOleg Chernovskiy {
61599736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", 0);
61699736703SOleg Chernovskiy }
61799736703SOleg Chernovskiy 
61899736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
61999736703SOleg Chernovskiy 					 struct device_attribute *attr,
62099736703SOleg Chernovskiy 					 char *buf)
62199736703SOleg Chernovskiy {
622082452e1SAlex Deucher 	return sprintf(buf, "%i\n", 255);
62399736703SOleg Chernovskiy }
62499736703SOleg Chernovskiy 
62599736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
62699736703SOleg Chernovskiy 				     struct device_attribute *attr,
62799736703SOleg Chernovskiy 				     const char *buf, size_t count)
62899736703SOleg Chernovskiy {
62999736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
63099736703SOleg Chernovskiy 	int err;
63199736703SOleg Chernovskiy 	u32 value;
63299736703SOleg Chernovskiy 
63399736703SOleg Chernovskiy 	err = kstrtou32(buf, 10, &value);
63499736703SOleg Chernovskiy 	if (err)
63599736703SOleg Chernovskiy 		return err;
63699736703SOleg Chernovskiy 
637082452e1SAlex Deucher 	value = (value * 100) / 255;
638082452e1SAlex Deucher 
63999736703SOleg Chernovskiy 	err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
64099736703SOleg Chernovskiy 	if (err)
64199736703SOleg Chernovskiy 		return err;
64299736703SOleg Chernovskiy 
64399736703SOleg Chernovskiy 	return count;
64499736703SOleg Chernovskiy }
64599736703SOleg Chernovskiy 
64699736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
64799736703SOleg Chernovskiy 				     struct device_attribute *attr,
64899736703SOleg Chernovskiy 				     char *buf)
64999736703SOleg Chernovskiy {
65099736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
65199736703SOleg Chernovskiy 	int err;
65299736703SOleg Chernovskiy 	u32 speed;
65399736703SOleg Chernovskiy 
65499736703SOleg Chernovskiy 	err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
65599736703SOleg Chernovskiy 	if (err)
65699736703SOleg Chernovskiy 		return err;
65799736703SOleg Chernovskiy 
658082452e1SAlex Deucher 	speed = (speed * 255) / 100;
659082452e1SAlex Deucher 
66099736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", speed);
66199736703SOleg Chernovskiy }
66299736703SOleg Chernovskiy 
663ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
664ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
665da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
66670d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
66770d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
66870d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
669ce8f5370SAlex Deucher 
67021a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
67121a8122aSAlex Deucher 				      struct device_attribute *attr,
67221a8122aSAlex Deucher 				      char *buf)
67321a8122aSAlex Deucher {
674ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
6754f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
67620d391d7SAlex Deucher 	int temp;
67721a8122aSAlex Deucher 
6784f2f2039SAlex Deucher 	/* Can't get temperature when the card is off */
6794f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
6804f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
6814f2f2039SAlex Deucher 		return -EINVAL;
6824f2f2039SAlex Deucher 
6836bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
6846bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
6856bd1c385SAlex Deucher 	else
68621a8122aSAlex Deucher 		temp = 0;
68721a8122aSAlex Deucher 
68821a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
68921a8122aSAlex Deucher }
69021a8122aSAlex Deucher 
6916ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
6926ea4e84dSJean Delvare 					     struct device_attribute *attr,
6936ea4e84dSJean Delvare 					     char *buf)
6946ea4e84dSJean Delvare {
695e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
6966ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
6976ea4e84dSJean Delvare 	int temp;
6986ea4e84dSJean Delvare 
6996ea4e84dSJean Delvare 	if (hyst)
7006ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
7016ea4e84dSJean Delvare 	else
7026ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
7036ea4e84dSJean Delvare 
7046ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
7056ea4e84dSJean Delvare }
7066ea4e84dSJean Delvare 
70721a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
7086ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
7096ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
71099736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
71199736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
71299736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
71399736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
71499736703SOleg Chernovskiy 
715*052813d9SSandeep Raghuraman static ssize_t radeon_hwmon_show_sclk(struct device *dev,
716*052813d9SSandeep Raghuraman 				      struct device_attribute *attr, char *buf)
717*052813d9SSandeep Raghuraman {
718*052813d9SSandeep Raghuraman 	struct radeon_device *rdev = dev_get_drvdata(dev);
719*052813d9SSandeep Raghuraman 	struct drm_device *ddev = rdev->ddev;
720*052813d9SSandeep Raghuraman 	u32 sclk = 0;
721*052813d9SSandeep Raghuraman 
722*052813d9SSandeep Raghuraman 	/* Can't get clock frequency when the card is off */
723*052813d9SSandeep Raghuraman 	if ((rdev->flags & RADEON_IS_PX) &&
724*052813d9SSandeep Raghuraman 	    (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
725*052813d9SSandeep Raghuraman 		return -EINVAL;
726*052813d9SSandeep Raghuraman 
727*052813d9SSandeep Raghuraman 	if (rdev->asic->dpm.get_current_sclk)
728*052813d9SSandeep Raghuraman 		sclk = radeon_dpm_get_current_sclk(rdev);
729*052813d9SSandeep Raghuraman 
730*052813d9SSandeep Raghuraman 	/* Value returned by dpm is in 10 KHz units, need to convert it into Hz
731*052813d9SSandeep Raghuraman 	   for hwmon */
732*052813d9SSandeep Raghuraman 	sclk *= 10000;
733*052813d9SSandeep Raghuraman 
734*052813d9SSandeep Raghuraman 	return snprintf(buf, PAGE_SIZE, "%u\n", sclk);
735*052813d9SSandeep Raghuraman }
736*052813d9SSandeep Raghuraman 
737*052813d9SSandeep Raghuraman static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, radeon_hwmon_show_sclk, NULL,
738*052813d9SSandeep Raghuraman 			  0);
739*052813d9SSandeep Raghuraman 
74021a8122aSAlex Deucher 
74121a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
74221a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
7436ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
7446ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
74599736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1.dev_attr.attr,
74699736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
74799736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
74899736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
749*052813d9SSandeep Raghuraman 	&sensor_dev_attr_freq1_input.dev_attr.attr,
75021a8122aSAlex Deucher 	NULL
75121a8122aSAlex Deucher };
75221a8122aSAlex Deucher 
7536ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
7546ea4e84dSJean Delvare 					struct attribute *attr, int index)
7556ea4e84dSJean Delvare {
756e3837b00SGeliang Tang 	struct device *dev = kobj_to_dev(kobj);
757e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
75899736703SOleg Chernovskiy 	umode_t effective_mode = attr->mode;
7596ea4e84dSJean Delvare 
7602a7d44f4SAlex Deucher 	/* Skip attributes if DPM is not enabled */
7616ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
7626ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
7632a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
7642a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
7652a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
7662a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
767*052813d9SSandeep Raghuraman 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
768*052813d9SSandeep Raghuraman 	     attr == &sensor_dev_attr_freq1_input.dev_attr.attr))
7696ea4e84dSJean Delvare 		return 0;
7706ea4e84dSJean Delvare 
77199736703SOleg Chernovskiy 	/* Skip fan attributes if fan is not present */
77299736703SOleg Chernovskiy 	if (rdev->pm.no_fan &&
77399736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
77499736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
77599736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
77699736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
77799736703SOleg Chernovskiy 		return 0;
77899736703SOleg Chernovskiy 
77999736703SOleg Chernovskiy 	/* mask fan attributes if we have no bindings for this asic to expose */
78099736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.get_fan_speed_percent &&
78199736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
78299736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_get_mode &&
78399736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
78499736703SOleg Chernovskiy 		effective_mode &= ~S_IRUGO;
78599736703SOleg Chernovskiy 
78699736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
78799736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
78899736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_set_mode &&
78999736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
79099736703SOleg Chernovskiy 		effective_mode &= ~S_IWUSR;
79199736703SOleg Chernovskiy 
79299736703SOleg Chernovskiy 	/* hide max/min values if we can't both query and manage the fan */
79399736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
79499736703SOleg Chernovskiy 	     !rdev->asic->dpm.get_fan_speed_percent) &&
79599736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
79699736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
79799736703SOleg Chernovskiy 		return 0;
79899736703SOleg Chernovskiy 
79999736703SOleg Chernovskiy 	return effective_mode;
8006ea4e84dSJean Delvare }
8016ea4e84dSJean Delvare 
80221a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
80321a8122aSAlex Deucher 	.attrs = hwmon_attributes,
8046ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
80521a8122aSAlex Deucher };
80621a8122aSAlex Deucher 
807ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
808ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
809ec39f64bSGuenter Roeck 	NULL
810ec39f64bSGuenter Roeck };
811ec39f64bSGuenter Roeck 
8120d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
81321a8122aSAlex Deucher {
8140d18abedSDan Carpenter 	int err = 0;
81521a8122aSAlex Deucher 
81621a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
81721a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
81821a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
81921a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
820457558edSAlex Deucher 	case THERMAL_TYPE_NI:
821e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
8221bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
823286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
824286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
8256bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
8265d7486c7SAlex Deucher 			return err;
827cb3e4e7cSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
828ec39f64bSGuenter Roeck 									   "radeon", rdev,
829ec39f64bSGuenter Roeck 									   hwmon_groups);
830cb3e4e7cSAlex Deucher 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
831cb3e4e7cSAlex Deucher 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
8320d18abedSDan Carpenter 			dev_err(rdev->dev,
8330d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
8340d18abedSDan Carpenter 		}
83521a8122aSAlex Deucher 		break;
83621a8122aSAlex Deucher 	default:
83721a8122aSAlex Deucher 		break;
83821a8122aSAlex Deucher 	}
8390d18abedSDan Carpenter 
8400d18abedSDan Carpenter 	return err;
84121a8122aSAlex Deucher }
84221a8122aSAlex Deucher 
843cb3e4e7cSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
844cb3e4e7cSAlex Deucher {
845cb3e4e7cSAlex Deucher 	if (rdev->pm.int_hwmon_dev)
846cb3e4e7cSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
847cb3e4e7cSAlex Deucher }
848cb3e4e7cSAlex Deucher 
849da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
850da321c8aSAlex Deucher {
851da321c8aSAlex Deucher 	struct radeon_device *rdev =
852da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
853da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
854da321c8aSAlex Deucher 	/* switch to the thermal state */
855da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
856da321c8aSAlex Deucher 
857da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
858da321c8aSAlex Deucher 		return;
859da321c8aSAlex Deucher 
860da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
861da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
862da321c8aSAlex Deucher 
863da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
864da321c8aSAlex Deucher 			/* switch back the user state */
865da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
866da321c8aSAlex Deucher 	} else {
867da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
868da321c8aSAlex Deucher 			/* switch back the user state */
869da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
870da321c8aSAlex Deucher 	}
87160320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
87260320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
87360320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
87460320347SAlex Deucher 	else
87560320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
87660320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
87760320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
87860320347SAlex Deucher 
87960320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
880da321c8aSAlex Deucher }
881da321c8aSAlex Deucher 
8823899ca84SAlex Deucher static bool radeon_dpm_single_display(struct radeon_device *rdev)
883da321c8aSAlex Deucher {
88448783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
88548783069SAlex Deucher 		true : false;
88648783069SAlex Deucher 
88748783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
88848783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
88948783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
89048783069SAlex Deucher 			single_display = false;
89148783069SAlex Deucher 	}
892da321c8aSAlex Deucher 
893951caa6aSAlex Deucher 	/* 120hz tends to be problematic even if they are under the
894951caa6aSAlex Deucher 	 * vblank limit.
895951caa6aSAlex Deucher 	 */
896951caa6aSAlex Deucher 	if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
897951caa6aSAlex Deucher 		single_display = false;
898951caa6aSAlex Deucher 
8993899ca84SAlex Deucher 	return single_display;
9003899ca84SAlex Deucher }
9013899ca84SAlex Deucher 
9023899ca84SAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
9033899ca84SAlex Deucher 						     enum radeon_pm_state_type dpm_state)
9043899ca84SAlex Deucher {
9053899ca84SAlex Deucher 	int i;
9063899ca84SAlex Deucher 	struct radeon_ps *ps;
9073899ca84SAlex Deucher 	u32 ui_class;
9083899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
9093899ca84SAlex Deucher 
910edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
911edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
912edcaa5b1SAlex Deucher 	 */
913edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
914edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
915da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
916da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
91753bf277bSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
918da321c8aSAlex Deucher 
919edcaa5b1SAlex Deucher restart_search:
920da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
921da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
922da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
923da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
924da321c8aSAlex Deucher 		switch (dpm_state) {
925da321c8aSAlex Deucher 		/* user states */
926da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
927da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
928da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
92948783069SAlex Deucher 					if (single_display)
930da321c8aSAlex Deucher 						return ps;
931da321c8aSAlex Deucher 				} else
932da321c8aSAlex Deucher 					return ps;
933da321c8aSAlex Deucher 			}
934da321c8aSAlex Deucher 			break;
935da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
936da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
937da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
93848783069SAlex Deucher 					if (single_display)
939da321c8aSAlex Deucher 						return ps;
940da321c8aSAlex Deucher 				} else
941da321c8aSAlex Deucher 					return ps;
942da321c8aSAlex Deucher 			}
943da321c8aSAlex Deucher 			break;
944da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
945da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
946da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
94748783069SAlex Deucher 					if (single_display)
948da321c8aSAlex Deucher 						return ps;
949da321c8aSAlex Deucher 				} else
950da321c8aSAlex Deucher 					return ps;
951da321c8aSAlex Deucher 			}
952da321c8aSAlex Deucher 			break;
953da321c8aSAlex Deucher 		/* internal states */
954da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
955d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
956da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
957d4d3278cSAlex Deucher 			else
958d4d3278cSAlex Deucher 				break;
959da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
960da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
961da321c8aSAlex Deucher 				return ps;
962da321c8aSAlex Deucher 			break;
963da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
964da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
965da321c8aSAlex Deucher 				return ps;
966da321c8aSAlex Deucher 			break;
967da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
968da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
969da321c8aSAlex Deucher 				return ps;
970da321c8aSAlex Deucher 			break;
971da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
972da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
973da321c8aSAlex Deucher 				return ps;
974da321c8aSAlex Deucher 			break;
975da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
976da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
977da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
978da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
979da321c8aSAlex Deucher 				return ps;
980da321c8aSAlex Deucher 			break;
981da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
982da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
983da321c8aSAlex Deucher 				return ps;
984da321c8aSAlex Deucher 			break;
985da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
986da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
987da321c8aSAlex Deucher 				return ps;
988da321c8aSAlex Deucher 			break;
989edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
990edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
991edcaa5b1SAlex Deucher 				return ps;
992edcaa5b1SAlex Deucher 			break;
993da321c8aSAlex Deucher 		default:
994da321c8aSAlex Deucher 			break;
995da321c8aSAlex Deucher 		}
996da321c8aSAlex Deucher 	}
997da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
998da321c8aSAlex Deucher 	switch (dpm_state) {
999da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1000ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1001ce3537d5SAlex Deucher 		goto restart_search;
1002da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1003da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1004da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1005d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
1006da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
1007d4d3278cSAlex Deucher 		} else {
1008d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1009d4d3278cSAlex Deucher 			goto restart_search;
1010d4d3278cSAlex Deucher 		}
1011da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
1012da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1013da321c8aSAlex Deucher 		goto restart_search;
1014da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
1015da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
1016da321c8aSAlex Deucher 		goto restart_search;
1017da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
1018edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
1019edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
1020da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1021da321c8aSAlex Deucher 		goto restart_search;
1022da321c8aSAlex Deucher 	default:
1023da321c8aSAlex Deucher 		break;
1024da321c8aSAlex Deucher 	}
1025da321c8aSAlex Deucher 
1026da321c8aSAlex Deucher 	return NULL;
1027da321c8aSAlex Deucher }
1028da321c8aSAlex Deucher 
1029da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
1030da321c8aSAlex Deucher {
1031da321c8aSAlex Deucher 	int i;
1032da321c8aSAlex Deucher 	struct radeon_ps *ps;
1033da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
103484dd1928SAlex Deucher 	int ret;
10353899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
1036da321c8aSAlex Deucher 
1037da321c8aSAlex Deucher 	/* if dpm init failed */
1038da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
1039da321c8aSAlex Deucher 		return;
1040da321c8aSAlex Deucher 
1041da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1042da321c8aSAlex Deucher 		/* add other state override checks here */
10438a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
10448a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
1045da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1046da321c8aSAlex Deucher 	}
1047da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
1048da321c8aSAlex Deucher 
1049da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1050da321c8aSAlex Deucher 	if (ps)
105189c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
1052da321c8aSAlex Deucher 	else
1053da321c8aSAlex Deucher 		return;
1054da321c8aSAlex Deucher 
1055d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
1056da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
1057b62d628bSAlex Deucher 		/* vce just modifies an existing state so force a change */
1058b62d628bSAlex Deucher 		if (ps->vce_active != rdev->pm.dpm.vce_active)
1059b62d628bSAlex Deucher 			goto force;
10603899ca84SAlex Deucher 		/* user has made a display change (such as timing) */
10613899ca84SAlex Deucher 		if (rdev->pm.dpm.single_display != single_display)
10623899ca84SAlex Deucher 			goto force;
1063d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1064d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
1065d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
1066d22b7e40SAlex Deucher 			 */
1067da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1068d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
1069da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
1070da321c8aSAlex Deucher 				/* update displays */
1071da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
1072da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1073da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1074da321c8aSAlex Deucher 			}
1075da321c8aSAlex Deucher 			return;
1076d22b7e40SAlex Deucher 		} else {
1077d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
1078d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
1079d22b7e40SAlex Deucher 			 * update display configuration.
1080d22b7e40SAlex Deucher 			 */
1081d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
1082d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
1083d22b7e40SAlex Deucher 				return;
1084d22b7e40SAlex Deucher 			} else {
1085d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1086d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
1087d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
1088d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
1089d22b7e40SAlex Deucher 					/* update displays */
1090d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
1091d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1092d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1093d22b7e40SAlex Deucher 					return;
1094d22b7e40SAlex Deucher 				}
1095d22b7e40SAlex Deucher 			}
1096d22b7e40SAlex Deucher 		}
1097da321c8aSAlex Deucher 	}
1098da321c8aSAlex Deucher 
1099b62d628bSAlex Deucher force:
1100033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
1101da321c8aSAlex Deucher 		printk("switching from power state:\n");
1102da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1103da321c8aSAlex Deucher 		printk("switching to power state:\n");
1104da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1105033a37dfSAlex Deucher 	}
1106b62d628bSAlex Deucher 
1107da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
1108da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
1109da321c8aSAlex Deucher 
1110b62d628bSAlex Deucher 	/* update whether vce is active */
1111b62d628bSAlex Deucher 	ps->vce_active = rdev->pm.dpm.vce_active;
1112b62d628bSAlex Deucher 
111384dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
111484dd1928SAlex Deucher 	if (ret)
111584dd1928SAlex Deucher 		goto done;
111684dd1928SAlex Deucher 
1117da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
1118da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
1119d74e766eSAlex Deucher 	/* update displays */
1120d74e766eSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
1121da321c8aSAlex Deucher 
1122da321c8aSAlex Deucher 	/* wait for the rings to drain */
1123da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
1124da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
1125da321c8aSAlex Deucher 		if (ring->ready)
112637615527SChristian König 			radeon_fence_wait_empty(rdev, i);
1127da321c8aSAlex Deucher 	}
1128da321c8aSAlex Deucher 
1129da321c8aSAlex Deucher 	/* program the new power state */
1130da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
1131da321c8aSAlex Deucher 
1132da321c8aSAlex Deucher 	/* update current power state */
1133da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1134da321c8aSAlex Deucher 
113584dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
113684dd1928SAlex Deucher 
11375e031d9fSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
11385e031d9fSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
11395e031d9fSAlex Deucher 	rdev->pm.dpm.single_display = single_display;
11405e031d9fSAlex Deucher 
11411cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
114214ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
114314ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
114460320347SAlex Deucher 			/* force low perf level for thermal */
114560320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
114614ac88afSAlex Deucher 			/* save the user's level */
114714ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
114814ac88afSAlex Deucher 		} else {
114914ac88afSAlex Deucher 			/* otherwise, user selected level */
115014ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
115114ac88afSAlex Deucher 		}
115260320347SAlex Deucher 	}
115360320347SAlex Deucher 
115484dd1928SAlex Deucher done:
1155da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
1156da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
1157da321c8aSAlex Deucher }
1158da321c8aSAlex Deucher 
1159ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1160ce3537d5SAlex Deucher {
1161ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
1162ce3537d5SAlex Deucher 
11639e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
11649e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
11658158eb9eSChristian König 		/* don't powergate anything if we
11668158eb9eSChristian König 		   have active but pause streams */
11678158eb9eSChristian König 		enable |= rdev->pm.dpm.sd > 0;
11688158eb9eSChristian König 		enable |= rdev->pm.dpm.hd > 0;
11699e9d9762SAlex Deucher 		/* enable/disable UVD */
11709e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
11719e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
11729e9d9762SAlex Deucher 	} else {
1173ce3537d5SAlex Deucher 		if (enable) {
1174ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1175ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
11760690a229SAlex Deucher 			/* disable this for now */
11770690a229SAlex Deucher #if 0
1178ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1179ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1180ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1181ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1182ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1183ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1184ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1185ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1186ce3537d5SAlex Deucher 			else
11870690a229SAlex Deucher #endif
1188ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1189ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
1190ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1191ce3537d5SAlex Deucher 		} else {
1192ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1193ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
1194ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1195ce3537d5SAlex Deucher 		}
1196ce3537d5SAlex Deucher 
1197ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1198ce3537d5SAlex Deucher 	}
11999e9d9762SAlex Deucher }
1200ce3537d5SAlex Deucher 
120103afe6f6SAlex Deucher void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
120203afe6f6SAlex Deucher {
120303afe6f6SAlex Deucher 	if (enable) {
120403afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
120503afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = true;
120603afe6f6SAlex Deucher 		/* XXX select vce level based on ring/task */
120703afe6f6SAlex Deucher 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
120803afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
120903afe6f6SAlex Deucher 	} else {
121003afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
121103afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = false;
121203afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
121303afe6f6SAlex Deucher 	}
121403afe6f6SAlex Deucher 
121503afe6f6SAlex Deucher 	radeon_pm_compute_clocks(rdev);
121603afe6f6SAlex Deucher }
121703afe6f6SAlex Deucher 
1218da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
1219ce8f5370SAlex Deucher {
1220ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
12213f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
12223f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
12233f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
12243f53eb6fSRafael J. Wysocki 	}
1225ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
122632c87fcaSTejun Heo 
122732c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1228ce8f5370SAlex Deucher }
1229ce8f5370SAlex Deucher 
1230da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1231da321c8aSAlex Deucher {
1232da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1233da321c8aSAlex Deucher 	/* disable dpm */
1234da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
1235da321c8aSAlex Deucher 	/* reset the power state */
1236da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1237da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
1238da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1239da321c8aSAlex Deucher }
1240da321c8aSAlex Deucher 
1241da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1242da321c8aSAlex Deucher {
1243da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1244da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1245da321c8aSAlex Deucher 	else
1246da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1247da321c8aSAlex Deucher }
1248da321c8aSAlex Deucher 
1249da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1250ce8f5370SAlex Deucher {
1251ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
12522e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
125336099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
12542e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1255ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
12568a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
12578a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
12582feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
12592feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
12602feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1261ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1262ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1263ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1264ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1265ed18a360SAlex Deucher 	}
1266f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1267f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1268f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1269f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
12709ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
12719ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
127237016951SMichel Dänzer 	if (rdev->pm.power_state) {
12734d60173fSAlex Deucher 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
12742feea49aSAlex Deucher 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
127537016951SMichel Dänzer 	}
12763f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
12773f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
12783f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
127932c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
12803f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
12813f53eb6fSRafael J. Wysocki 	}
1282f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1283ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1284d0d6cb81SRafał Miłecki }
1285d0d6cb81SRafał Miłecki 
1286da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
12877433874eSRafał Miłecki {
128826481fb1SDave Airlie 	int ret;
12890d18abedSDan Carpenter 
1290da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1291da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1292da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1293da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1294da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1295da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1296e14cd2bbSAlex Deucher 	if (ret)
1297e14cd2bbSAlex Deucher 		goto dpm_resume_fail;
1298e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = true;
1299e14cd2bbSAlex Deucher 	return;
1300e14cd2bbSAlex Deucher 
1301e14cd2bbSAlex Deucher dpm_resume_fail:
1302da321c8aSAlex Deucher 	DRM_ERROR("radeon: dpm resume failed\n");
1303da321c8aSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
130436099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1305da321c8aSAlex Deucher 	    rdev->mc_fw) {
1306da321c8aSAlex Deucher 		if (rdev->pm.default_vddc)
1307da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1308da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1309da321c8aSAlex Deucher 		if (rdev->pm.default_vddci)
1310da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1311da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1312da321c8aSAlex Deucher 		if (rdev->pm.default_sclk)
1313da321c8aSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1314da321c8aSAlex Deucher 		if (rdev->pm.default_mclk)
1315da321c8aSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1316da321c8aSAlex Deucher 	}
1317da321c8aSAlex Deucher }
1318da321c8aSAlex Deucher 
1319da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1320da321c8aSAlex Deucher {
1321da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1322da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1323da321c8aSAlex Deucher 	else
1324da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1325da321c8aSAlex Deucher }
1326da321c8aSAlex Deucher 
1327da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1328da321c8aSAlex Deucher {
1329da321c8aSAlex Deucher 	int ret;
1330da321c8aSAlex Deucher 
1331f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1332ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1333ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1334ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1335ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
13369ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
13379ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1338f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1339f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
134021a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1341c913e23aSRafał Miłecki 
134256278a8eSAlex Deucher 	if (rdev->bios) {
134356278a8eSAlex Deucher 		if (rdev->is_atom_bios)
134456278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
134556278a8eSAlex Deucher 		else
134656278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1347f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1348ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1349ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
13502e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
135136099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
13522e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1353ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
13548a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
13558a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
13564639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
13574639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
13584639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1359ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1360ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1361ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1362ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1363ed18a360SAlex Deucher 		}
136456278a8eSAlex Deucher 	}
136556278a8eSAlex Deucher 
136621a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
13670d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
13680d18abedSDan Carpenter 	if (ret)
13690d18abedSDan Carpenter 		return ret;
137032c87fcaSTejun Heo 
137132c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
137232c87fcaSTejun Heo 
1373ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
13747433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1375c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
13767433874eSRafał Miłecki 		}
13777433874eSRafał Miłecki 
1378c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1379ce8f5370SAlex Deucher 	}
1380c913e23aSRafał Miłecki 
13817433874eSRafał Miłecki 	return 0;
13827433874eSRafał Miłecki }
13837433874eSRafał Miłecki 
1384da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1385da321c8aSAlex Deucher {
1386da321c8aSAlex Deucher 	int i;
1387da321c8aSAlex Deucher 
1388da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1389da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1390da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1391da321c8aSAlex Deucher 	}
1392da321c8aSAlex Deucher }
1393da321c8aSAlex Deucher 
1394da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1395da321c8aSAlex Deucher {
1396da321c8aSAlex Deucher 	int ret;
1397da321c8aSAlex Deucher 
13981cd8b21aSAlex Deucher 	/* default to balanced state */
1399edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1400edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
14011cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1402da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1403da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1404da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1405da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1406da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1407da321c8aSAlex Deucher 
1408da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1409da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1410da321c8aSAlex Deucher 	else
1411da321c8aSAlex Deucher 		return -EINVAL;
1412da321c8aSAlex Deucher 
1413da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1414da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1415da321c8aSAlex Deucher 	if (ret)
1416da321c8aSAlex Deucher 		return ret;
1417da321c8aSAlex Deucher 
1418da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1419da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1420da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1421da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1422033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1423da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1424da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1425da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1426da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1427e14cd2bbSAlex Deucher 	if (ret)
1428e14cd2bbSAlex Deucher 		goto dpm_failed;
1429da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1430da321c8aSAlex Deucher 
14311316b792SAlex Deucher 	if (radeon_debugfs_pm_init(rdev)) {
14321316b792SAlex Deucher 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
14331316b792SAlex Deucher 	}
14341316b792SAlex Deucher 
1435da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1436da321c8aSAlex Deucher 
1437da321c8aSAlex Deucher 	return 0;
1438e14cd2bbSAlex Deucher 
1439e14cd2bbSAlex Deucher dpm_failed:
1440e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = false;
1441e14cd2bbSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
1442e14cd2bbSAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1443e14cd2bbSAlex Deucher 	    rdev->mc_fw) {
1444e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddc)
1445e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1446e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1447e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddci)
1448e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1449e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1450e14cd2bbSAlex Deucher 		if (rdev->pm.default_sclk)
1451e14cd2bbSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1452e14cd2bbSAlex Deucher 		if (rdev->pm.default_mclk)
1453e14cd2bbSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1454e14cd2bbSAlex Deucher 	}
1455e14cd2bbSAlex Deucher 	DRM_ERROR("radeon: dpm initialization failed\n");
1456e14cd2bbSAlex Deucher 	return ret;
1457da321c8aSAlex Deucher }
1458da321c8aSAlex Deucher 
14594369a69eSAlex Deucher struct radeon_dpm_quirk {
14604369a69eSAlex Deucher 	u32 chip_vendor;
14614369a69eSAlex Deucher 	u32 chip_device;
14624369a69eSAlex Deucher 	u32 subsys_vendor;
14634369a69eSAlex Deucher 	u32 subsys_device;
14644369a69eSAlex Deucher };
14654369a69eSAlex Deucher 
14664369a69eSAlex Deucher /* cards with dpm stability problems */
14674369a69eSAlex Deucher static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
14684369a69eSAlex Deucher 	/* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
14694369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
14704369a69eSAlex Deucher 	/* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
14714369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
14724369a69eSAlex Deucher 	{ 0, 0, 0, 0 },
14734369a69eSAlex Deucher };
14744369a69eSAlex Deucher 
1475da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1476da321c8aSAlex Deucher {
14774369a69eSAlex Deucher 	struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
14784369a69eSAlex Deucher 	bool disable_dpm = false;
14794369a69eSAlex Deucher 
14804369a69eSAlex Deucher 	/* Apply dpm quirks */
14814369a69eSAlex Deucher 	while (p && p->chip_device != 0) {
14824369a69eSAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
14834369a69eSAlex Deucher 		    rdev->pdev->device == p->chip_device &&
14844369a69eSAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
14854369a69eSAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
14864369a69eSAlex Deucher 			disable_dpm = true;
14874369a69eSAlex Deucher 			break;
14884369a69eSAlex Deucher 		}
14894369a69eSAlex Deucher 		++p;
14904369a69eSAlex Deucher 	}
14914369a69eSAlex Deucher 
1492da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1493da321c8aSAlex Deucher 	switch (rdev->family) {
14944a6369e9SAlex Deucher 	case CHIP_RV610:
14954a6369e9SAlex Deucher 	case CHIP_RV630:
14964a6369e9SAlex Deucher 	case CHIP_RV620:
14974a6369e9SAlex Deucher 	case CHIP_RV635:
14984a6369e9SAlex Deucher 	case CHIP_RV670:
14999d67006eSAlex Deucher 	case CHIP_RS780:
15009d67006eSAlex Deucher 	case CHIP_RS880:
150176e6dcecSAlex Deucher 	case CHIP_RV770:
15028a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1503761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1504761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15058a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
15068a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
15078a53fa23SAlex Deucher 			 (!rdev->smc_fw))
15088a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1509761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
15109d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
15119d67006eSAlex Deucher 		else
15129d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15139d67006eSAlex Deucher 		break;
1514ab70b1ddSAlex Deucher 	case CHIP_RV730:
1515ab70b1ddSAlex Deucher 	case CHIP_RV710:
1516ab70b1ddSAlex Deucher 	case CHIP_RV740:
151759f7a2f2SAlex Deucher 	case CHIP_CEDAR:
151859f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
151959f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
152059f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
152159f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
15225a16f761SAlex Deucher 	case CHIP_PALM:
15235a16f761SAlex Deucher 	case CHIP_SUMO:
15245a16f761SAlex Deucher 	case CHIP_SUMO2:
1525c08abf11SAlex Deucher 	case CHIP_BARTS:
1526c08abf11SAlex Deucher 	case CHIP_TURKS:
1527c08abf11SAlex Deucher 	case CHIP_CAICOS:
15288f500af4SAlex Deucher 	case CHIP_CAYMAN:
15293a118989SAlex Deucher 	case CHIP_ARUBA:
153068bc7785SAlex Deucher 	case CHIP_TAHITI:
153168bc7785SAlex Deucher 	case CHIP_PITCAIRN:
153268bc7785SAlex Deucher 	case CHIP_VERDE:
153368bc7785SAlex Deucher 	case CHIP_OLAND:
153468bc7785SAlex Deucher 	case CHIP_HAINAN:
15354f22dde3SAlex Deucher 	case CHIP_BONAIRE:
1536e308b1d3SAlex Deucher 	case CHIP_KABINI:
1537e308b1d3SAlex Deucher 	case CHIP_KAVERI:
15384f22dde3SAlex Deucher 	case CHIP_HAWAII:
15397d032a4bSSamuel Li 	case CHIP_MULLINS:
15405a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
15415a16f761SAlex Deucher 		if (!rdev->rlc_fw)
15425a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15435a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
15445a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
15455a16f761SAlex Deucher 			 (!rdev->smc_fw))
15465a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15474369a69eSAlex Deucher 		else if (disable_dpm && (radeon_dpm == -1))
15484369a69eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15495a16f761SAlex Deucher 		else if (radeon_dpm == 0)
15505a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15515a16f761SAlex Deucher 		else
15525a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
15535a16f761SAlex Deucher 		break;
1554da321c8aSAlex Deucher 	default:
1555da321c8aSAlex Deucher 		/* default to profile method */
1556da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1557da321c8aSAlex Deucher 		break;
1558da321c8aSAlex Deucher 	}
1559da321c8aSAlex Deucher 
1560da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1561da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1562da321c8aSAlex Deucher 	else
1563da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1564da321c8aSAlex Deucher }
1565da321c8aSAlex Deucher 
1566914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1567914a8987SAlex Deucher {
1568914a8987SAlex Deucher 	int ret = 0;
1569914a8987SAlex Deucher 
1570914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
157151a4726bSAlex Deucher 		if (rdev->pm.dpm_enabled) {
157249abb266SAlex Deucher 			if (!rdev->pm.sysfs_initialized) {
157351a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
157451a4726bSAlex Deucher 				if (ret)
157551a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
157651a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
157751a4726bSAlex Deucher 				if (ret)
157851a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
157951a4726bSAlex Deucher 				/* XXX: these are noops for dpm but are here for backwards compat */
158051a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_profile);
158151a4726bSAlex Deucher 				if (ret)
158251a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power profile\n");
158351a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_method);
158451a4726bSAlex Deucher 				if (ret)
158551a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power method\n");
158649abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
158749abb266SAlex Deucher 			}
158851a4726bSAlex Deucher 
1589914a8987SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1590914a8987SAlex Deucher 			ret = radeon_dpm_late_enable(rdev);
1591914a8987SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
159251a4726bSAlex Deucher 			if (ret) {
159351a4726bSAlex Deucher 				rdev->pm.dpm_enabled = false;
159451a4726bSAlex Deucher 				DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
159551a4726bSAlex Deucher 			} else {
159651a4726bSAlex Deucher 				/* set the dpm state for PX since there won't be
159751a4726bSAlex Deucher 				 * a modeset to call this.
159851a4726bSAlex Deucher 				 */
159951a4726bSAlex Deucher 				radeon_pm_compute_clocks(rdev);
160051a4726bSAlex Deucher 			}
160151a4726bSAlex Deucher 		}
160251a4726bSAlex Deucher 	} else {
160349abb266SAlex Deucher 		if ((rdev->pm.num_power_states > 1) &&
160449abb266SAlex Deucher 		    (!rdev->pm.sysfs_initialized)) {
160551a4726bSAlex Deucher 			/* where's the best place to put these? */
160651a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_profile);
160751a4726bSAlex Deucher 			if (ret)
160851a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power profile\n");
160951a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_method);
161051a4726bSAlex Deucher 			if (ret)
161151a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power method\n");
161249abb266SAlex Deucher 			if (!ret)
161349abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
161451a4726bSAlex Deucher 		}
1615914a8987SAlex Deucher 	}
1616914a8987SAlex Deucher 	return ret;
1617914a8987SAlex Deucher }
1618914a8987SAlex Deucher 
1619da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
162029fb52caSAlex Deucher {
1621ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1622a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1623ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1624ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1625ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1626ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1627ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1628ce8f5370SAlex Deucher 			/* reset default clocks */
1629ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1630ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1631ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
163258e21dffSAlex Deucher 		}
1633ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
163432c87fcaSTejun Heo 
163532c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
163658e21dffSAlex Deucher 
1637ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1638ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1639ce8f5370SAlex Deucher 	}
1640a424816fSAlex Deucher 
1641cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
16420975b162SAlex Deucher 	kfree(rdev->pm.power_state);
164329fb52caSAlex Deucher }
164429fb52caSAlex Deucher 
1645da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1646da321c8aSAlex Deucher {
1647da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1648da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1649da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1650da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1651da321c8aSAlex Deucher 
1652da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
165370d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1654da321c8aSAlex Deucher 		/* XXX backwards compat */
1655da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1656da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1657da321c8aSAlex Deucher 	}
1658da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1659da321c8aSAlex Deucher 
1660cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
1661da321c8aSAlex Deucher 	kfree(rdev->pm.power_state);
1662da321c8aSAlex Deucher }
1663da321c8aSAlex Deucher 
1664da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1665da321c8aSAlex Deucher {
1666da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1667da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1668da321c8aSAlex Deucher 	else
1669da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1670da321c8aSAlex Deucher }
1671da321c8aSAlex Deucher 
1672da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1673c913e23aSRafał Miłecki {
1674c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1675a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1676c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1677c913e23aSRafał Miłecki 
1678ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1679ce8f5370SAlex Deucher 		return;
1680ce8f5370SAlex Deucher 
1681c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1682c913e23aSRafał Miłecki 
1683c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1684a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
16853ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1686a48b9b4eSAlex Deucher 		list_for_each_entry(crtc,
1687a48b9b4eSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1688a48b9b4eSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1689a48b9b4eSAlex Deucher 			if (radeon_crtc->enabled) {
1690c913e23aSRafał Miłecki 				rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1691a48b9b4eSAlex Deucher 				rdev->pm.active_crtc_count++;
1692c913e23aSRafał Miłecki 			}
1693c913e23aSRafał Miłecki 		}
16943ed9a335SAlex Deucher 	}
1695c913e23aSRafał Miłecki 
1696ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1697ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1698ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1699ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1700ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1701a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1702ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1703ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1704c913e23aSRafał Miłecki 
1705ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1706ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1707ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1708ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1709c913e23aSRafał Miłecki 
1710d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1711c913e23aSRafał Miłecki 				}
1712a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1713c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1714c913e23aSRafał Miłecki 
1715ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1716ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1717ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1718ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1719ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1720c913e23aSRafał Miłecki 
172132c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1722c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1723ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1724ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
172532c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1726c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1727d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1728c913e23aSRafał Miłecki 				}
1729a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1730ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1731ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1732c913e23aSRafał Miłecki 
1733ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1734ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1735ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1736ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1737ce8f5370SAlex Deucher 				}
1738ce8f5370SAlex Deucher 			}
173973a6d3fcSRafał Miłecki 		}
1740c913e23aSRafał Miłecki 	}
1741c913e23aSRafał Miłecki 
1742c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1743c913e23aSRafał Miłecki }
1744c913e23aSRafał Miłecki 
1745da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1746da321c8aSAlex Deucher {
1747da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1748da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1749da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1750da321c8aSAlex Deucher 
17516c7bcceaSAlex Deucher 	if (!rdev->pm.dpm_enabled)
17526c7bcceaSAlex Deucher 		return;
17536c7bcceaSAlex Deucher 
1754da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1755da321c8aSAlex Deucher 
17565ca302f7SAlex Deucher 	/* update active crtc counts */
1757da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1758da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
17593ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1760da321c8aSAlex Deucher 		list_for_each_entry(crtc,
1761da321c8aSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1762da321c8aSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1763da321c8aSAlex Deucher 			if (crtc->enabled) {
1764da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1765da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtc_count++;
1766da321c8aSAlex Deucher 			}
1767da321c8aSAlex Deucher 		}
17683ed9a335SAlex Deucher 	}
1769da321c8aSAlex Deucher 
17705ca302f7SAlex Deucher 	/* update battery/ac status */
17715ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
17725ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
17735ca302f7SAlex Deucher 	else
17745ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
17755ca302f7SAlex Deucher 
1776da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1777da321c8aSAlex Deucher 
1778da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
17798a227555SAlex Deucher 
1780da321c8aSAlex Deucher }
1781da321c8aSAlex Deucher 
1782da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1783da321c8aSAlex Deucher {
1784da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1785da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1786da321c8aSAlex Deucher 	else
1787da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1788da321c8aSAlex Deucher }
1789da321c8aSAlex Deucher 
1790ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1791f735261bSDave Airlie {
179275fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1793f735261bSDave Airlie 	bool in_vbl = true;
1794f735261bSDave Airlie 
179575fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
179675fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
179775fa0b08SMario Kleiner 	 */
179875fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
179975fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
18005b5561b3SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
18015b5561b3SMario Kleiner 								crtc,
18025b5561b3SMario Kleiner 								USE_REAL_VBLANKSTART,
18033bb403bfSVille Syrjälä 								&vpos, &hpos, NULL, NULL,
18043bb403bfSVille Syrjälä 								&rdev->mode_info.crtcs[crtc]->base.hwmode);
1805f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
18063d3cbd84SDaniel Vetter 			    !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
1807f735261bSDave Airlie 				in_vbl = false;
1808f735261bSDave Airlie 		}
1809f735261bSDave Airlie 	}
1810f81f2024SMatthew Garrett 
1811f81f2024SMatthew Garrett 	return in_vbl;
1812f81f2024SMatthew Garrett }
1813f81f2024SMatthew Garrett 
1814ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1815f81f2024SMatthew Garrett {
1816f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1817f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1818f81f2024SMatthew Garrett 
1819fbd62354SWambui Karuga 	if (!in_vbl)
1820d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1821bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1822f735261bSDave Airlie 	return in_vbl;
1823f735261bSDave Airlie }
1824c913e23aSRafał Miłecki 
1825ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1826c913e23aSRafał Miłecki {
1827c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1828d9932a32SMatthew Garrett 	int resched;
1829c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1830ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1831c913e23aSRafał Miłecki 
1832d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1833c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1834ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1835c913e23aSRafał Miłecki 		int not_processed = 0;
18367465280cSAlex Deucher 		int i;
1837c913e23aSRafał Miłecki 
18387465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18390ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
18400ec0612aSAlex Deucher 
18410ec0612aSAlex Deucher 			if (ring->ready) {
184247492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
18437465280cSAlex Deucher 				if (not_processed >= 3)
18447465280cSAlex Deucher 					break;
18457465280cSAlex Deucher 			}
18460ec0612aSAlex Deucher 		}
1847c913e23aSRafał Miłecki 
1848c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1849ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1850ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1851ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1852ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1853ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1854ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1855ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1856c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1857c913e23aSRafał Miłecki 			}
1858c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1859ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1860ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1861ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1862ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1863ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1864ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1865ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1866c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1867c913e23aSRafał Miłecki 			}
1868c913e23aSRafał Miłecki 		}
1869c913e23aSRafał Miłecki 
1870d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1871d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1872d7311171SAlex Deucher 		 */
1873ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1874ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1875ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1876ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1877c913e23aSRafał Miłecki 		}
1878c913e23aSRafał Miłecki 
187932c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1880c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1881c913e23aSRafał Miłecki 	}
18823f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
18833f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18843f53eb6fSRafael J. Wysocki }
1885c913e23aSRafał Miłecki 
18867433874eSRafał Miłecki /*
18877433874eSRafał Miłecki  * Debugfs info
18887433874eSRafał Miłecki  */
18897433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
18907433874eSRafał Miłecki 
18917433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
18927433874eSRafał Miłecki {
18937433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
18947433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
18957433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
18964f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
18977433874eSRafał Miłecki 
18984f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
18994f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
19004f2f2039SAlex Deucher 		seq_printf(m, "PX asic powered off\n");
19014f2f2039SAlex Deucher 	} else if (rdev->pm.dpm_enabled) {
19021316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
19031316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
19041316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
19051316b792SAlex Deucher 		else
190671375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
19071316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
19081316b792SAlex Deucher 	} else {
19099ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1910bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1911bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1912bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1913bf05d998SAlex Deucher 		else
19146234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
19159ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1916798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
19176234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
19180fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
19190fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1920798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1921aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
19221316b792SAlex Deucher 	}
19237433874eSRafał Miłecki 
19247433874eSRafał Miłecki 	return 0;
19257433874eSRafał Miłecki }
19267433874eSRafał Miłecki 
19277433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
19287433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
19297433874eSRafał Miłecki };
19307433874eSRafał Miłecki #endif
19317433874eSRafał Miłecki 
1932c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
19337433874eSRafał Miłecki {
19347433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
19357433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
19367433874eSRafał Miłecki #else
19377433874eSRafał Miłecki 	return 0;
19387433874eSRafał Miłecki #endif
19397433874eSRafał Miłecki }
1940