xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
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_vblank.h>
30f9183127SSam Ravnborg 
31f9183127SSam Ravnborg #include "atom.h"
32f9183127SSam Ravnborg #include "avivod.h"
33f9183127SSam Ravnborg #include "r600_dpm.h"
34f9183127SSam Ravnborg #include "radeon.h"
35bb29f896SLee Jones #include "radeon_pm.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);
505b54d679SNirmoy Das static void 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 
radeon_pm_get_type_index(struct radeon_device * rdev,enum radeon_pm_state_type ps_type,int instance)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 
radeon_pm_acpi_event_handler(struct radeon_device * rdev)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 
radeon_pm_update_profile(struct radeon_device * rdev)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 
radeon_unmap_vram_bos(struct radeon_device * rdev)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) {
157d3116756SChristian König 		if (bo->tbo.resource->mem_type == TTM_PL_VRAM)
1585876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1595876dd24SMatthew Garrett 	}
1605876dd24SMatthew Garrett }
1615876dd24SMatthew Garrett 
radeon_sync_with_vblank(struct radeon_device * rdev)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 
radeon_set_power_state(struct radeon_device * rdev)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 
radeon_pm_set_clocks(struct radeon_device * rdev)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;
284*5e3a0f77SWu Hoi Pok 		drm_for_each_crtc(crtc, rdev_to_drm(rdev)) {
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;
301*5e3a0f77SWu Hoi Pok 		drm_for_each_crtc(crtc, rdev_to_drm(rdev)) {
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 
radeon_pm_print_states(struct radeon_device * rdev)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 
radeon_get_pm_profile(struct device * dev,struct device_attribute * attr,char * buf)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 
3632b0e617aSTian Tao 	return sysfs_emit(buf, "%s\n", (cp == PM_PROFILE_AUTO) ? "auto" :
364ce8f5370SAlex Deucher 			  (cp == PM_PROFILE_LOW) ? "low" :
36512e27be8SDaniel J Blueman 			  (cp == PM_PROFILE_MID) ? "mid" :
366ce8f5370SAlex Deucher 			  (cp == PM_PROFILE_HIGH) ? "high" : "default");
367a424816fSAlex Deucher }
368a424816fSAlex Deucher 
radeon_set_pm_profile(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)369ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
370a424816fSAlex Deucher 				     struct device_attribute *attr,
371a424816fSAlex Deucher 				     const char *buf,
372a424816fSAlex Deucher 				     size_t count)
373a424816fSAlex Deucher {
3743e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
375a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
376a424816fSAlex Deucher 
3774f2f2039SAlex Deucher 	/* Can't set profile when the card is off */
3784f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
3794f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
3804f2f2039SAlex Deucher 		return -EINVAL;
3814f2f2039SAlex Deucher 
382a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
383ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
384ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
385ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
386ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
387ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
388ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
389ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
390c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
391c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
392ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
393ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
394ce8f5370SAlex Deucher 		else {
3951783e4bfSThomas Renninger 			count = -EINVAL;
396ce8f5370SAlex Deucher 			goto fail;
397ce8f5370SAlex Deucher 		}
398ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
399ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
4001783e4bfSThomas Renninger 	} else
4011783e4bfSThomas Renninger 		count = -EINVAL;
4021783e4bfSThomas Renninger 
403ce8f5370SAlex Deucher fail:
404a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
405a424816fSAlex Deucher 
406a424816fSAlex Deucher 	return count;
407a424816fSAlex Deucher }
408a424816fSAlex Deucher 
radeon_get_pm_method(struct device * dev,struct device_attribute * attr,char * buf)409ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
410ce8f5370SAlex Deucher 				    struct device_attribute *attr,
411ce8f5370SAlex Deucher 				    char *buf)
41256278a8eSAlex Deucher {
4133e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
414ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
415ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
41656278a8eSAlex Deucher 
4172b0e617aSTian Tao 	return sysfs_emit(buf, "%s\n", (pm == PM_METHOD_DYNPM) ? "dynpm" :
418da321c8aSAlex Deucher 			  (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
41956278a8eSAlex Deucher }
42056278a8eSAlex Deucher 
radeon_set_pm_method(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)421ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
422ce8f5370SAlex Deucher 				    struct device_attribute *attr,
423ce8f5370SAlex Deucher 				    const char *buf,
424ce8f5370SAlex Deucher 				    size_t count)
425d0d6cb81SRafał Miłecki {
4263e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
427ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
428ce8f5370SAlex Deucher 
4294f2f2039SAlex Deucher 	/* Can't set method when the card is off */
4304f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4314f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
4324f2f2039SAlex Deucher 		count = -EINVAL;
4334f2f2039SAlex Deucher 		goto fail;
4344f2f2039SAlex Deucher 	}
4354f2f2039SAlex Deucher 
436da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
437da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
438da321c8aSAlex Deucher 		count = -EINVAL;
439da321c8aSAlex Deucher 		goto fail;
440da321c8aSAlex Deucher 	}
441ce8f5370SAlex Deucher 
442ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
443ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
444ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
445ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
446ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
447ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
448ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
449ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
450ce8f5370SAlex Deucher 		/* disable dynpm */
451ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
452ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4533f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
454ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
45532c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
456ce8f5370SAlex Deucher 	} else {
4571783e4bfSThomas Renninger 		count = -EINVAL;
458ce8f5370SAlex Deucher 		goto fail;
459d0d6cb81SRafał Miłecki 	}
460ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
461ce8f5370SAlex Deucher fail:
462ce8f5370SAlex Deucher 	return count;
463ce8f5370SAlex Deucher }
464ce8f5370SAlex Deucher 
radeon_get_dpm_state(struct device * dev,struct device_attribute * attr,char * buf)465da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
466da321c8aSAlex Deucher 				    struct device_attribute *attr,
467da321c8aSAlex Deucher 				    char *buf)
468da321c8aSAlex Deucher {
4693e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
470da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
471da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
472da321c8aSAlex Deucher 
4732b0e617aSTian Tao 	return sysfs_emit(buf, "%s\n",
474da321c8aSAlex Deucher 			  (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
475da321c8aSAlex Deucher 			  (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
476da321c8aSAlex Deucher }
477da321c8aSAlex Deucher 
radeon_set_dpm_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)478da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
479da321c8aSAlex Deucher 				    struct device_attribute *attr,
480da321c8aSAlex Deucher 				    const char *buf,
481da321c8aSAlex Deucher 				    size_t count)
482da321c8aSAlex Deucher {
4833e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
484da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
485da321c8aSAlex Deucher 
486da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
487da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
488da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
489da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
490da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
491da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
492da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
493da321c8aSAlex Deucher 	else {
494da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
495da321c8aSAlex Deucher 		count = -EINVAL;
496da321c8aSAlex Deucher 		goto fail;
497da321c8aSAlex Deucher 	}
498da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
499b07a657eSPali Rohár 
500b07a657eSPali Rohár 	/* Can't set dpm state when the card is off */
501b07a657eSPali Rohár 	if (!(rdev->flags & RADEON_IS_PX) ||
502b07a657eSPali Rohár 	    (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
503da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
504b07a657eSPali Rohár 
505da321c8aSAlex Deucher fail:
506da321c8aSAlex Deucher 	return count;
507da321c8aSAlex Deucher }
508da321c8aSAlex Deucher 
radeon_get_dpm_forced_performance_level(struct device * dev,struct device_attribute * attr,char * buf)50970d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
51070d01a5eSAlex Deucher 						       struct device_attribute *attr,
51170d01a5eSAlex Deucher 						       char *buf)
51270d01a5eSAlex Deucher {
5133e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
51470d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
51570d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
51670d01a5eSAlex Deucher 
5174f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5184f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5192b0e617aSTian Tao 		return sysfs_emit(buf, "off\n");
5204f2f2039SAlex Deucher 
5212b0e617aSTian Tao 	return sysfs_emit(buf, "%s\n",
52270d01a5eSAlex Deucher 			  (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
52370d01a5eSAlex Deucher 			  (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
52470d01a5eSAlex Deucher }
52570d01a5eSAlex Deucher 
radeon_set_dpm_forced_performance_level(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)52670d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
52770d01a5eSAlex Deucher 						       struct device_attribute *attr,
52870d01a5eSAlex Deucher 						       const char *buf,
52970d01a5eSAlex Deucher 						       size_t count)
53070d01a5eSAlex Deucher {
5313e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
53270d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
53370d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
53470d01a5eSAlex Deucher 	int ret = 0;
53570d01a5eSAlex Deucher 
5364f2f2039SAlex Deucher 	/* Can't force performance level when the card is off */
5374f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5384f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5394f2f2039SAlex Deucher 		return -EINVAL;
5404f2f2039SAlex Deucher 
54170d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
54270d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
54370d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
54470d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
54570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
54670d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
54770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
54870d01a5eSAlex Deucher 	} else {
54970d01a5eSAlex Deucher 		count = -EINVAL;
55070d01a5eSAlex Deucher 		goto fail;
55170d01a5eSAlex Deucher 	}
55270d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5530a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5540a17af37SAlex Deucher 			count = -EINVAL;
5550a17af37SAlex Deucher 			goto fail;
5560a17af37SAlex Deucher 		}
55770d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
55870d01a5eSAlex Deucher 		if (ret)
55970d01a5eSAlex Deucher 			count = -EINVAL;
56070d01a5eSAlex Deucher 	}
56170d01a5eSAlex Deucher fail:
5620a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5630a17af37SAlex Deucher 
56470d01a5eSAlex Deucher 	return count;
56570d01a5eSAlex Deucher }
56670d01a5eSAlex Deucher 
radeon_hwmon_get_pwm1_enable(struct device * dev,struct device_attribute * attr,char * buf)56799736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
56899736703SOleg Chernovskiy 					    struct device_attribute *attr,
56999736703SOleg Chernovskiy 					    char *buf)
57099736703SOleg Chernovskiy {
57199736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
57299736703SOleg Chernovskiy 	u32 pwm_mode = 0;
57399736703SOleg Chernovskiy 
57499736703SOleg Chernovskiy 	if (rdev->asic->dpm.fan_ctrl_get_mode)
57599736703SOleg Chernovskiy 		pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
57699736703SOleg Chernovskiy 
57799736703SOleg Chernovskiy 	/* never 0 (full-speed), fuse or smc-controlled always */
57899736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
57999736703SOleg Chernovskiy }
58099736703SOleg Chernovskiy 
radeon_hwmon_set_pwm1_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)58199736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
58299736703SOleg Chernovskiy 					    struct device_attribute *attr,
58399736703SOleg Chernovskiy 					    const char *buf,
58499736703SOleg Chernovskiy 					    size_t count)
58599736703SOleg Chernovskiy {
58699736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
58799736703SOleg Chernovskiy 	int err;
58899736703SOleg Chernovskiy 	int value;
58999736703SOleg Chernovskiy 
59099736703SOleg Chernovskiy 	if(!rdev->asic->dpm.fan_ctrl_set_mode)
59199736703SOleg Chernovskiy 		return -EINVAL;
59299736703SOleg Chernovskiy 
59399736703SOleg Chernovskiy 	err = kstrtoint(buf, 10, &value);
59499736703SOleg Chernovskiy 	if (err)
59599736703SOleg Chernovskiy 		return err;
59699736703SOleg Chernovskiy 
59799736703SOleg Chernovskiy 	switch (value) {
59899736703SOleg Chernovskiy 	case 1: /* manual, percent-based */
59999736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
60099736703SOleg Chernovskiy 		break;
60199736703SOleg Chernovskiy 	default: /* disable */
60299736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
60399736703SOleg Chernovskiy 		break;
60499736703SOleg Chernovskiy 	}
60599736703SOleg Chernovskiy 
60699736703SOleg Chernovskiy 	return count;
60799736703SOleg Chernovskiy }
60899736703SOleg Chernovskiy 
radeon_hwmon_get_pwm1_min(struct device * dev,struct device_attribute * attr,char * buf)60999736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
61099736703SOleg Chernovskiy 					 struct device_attribute *attr,
61199736703SOleg Chernovskiy 					 char *buf)
61299736703SOleg Chernovskiy {
61399736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", 0);
61499736703SOleg Chernovskiy }
61599736703SOleg Chernovskiy 
radeon_hwmon_get_pwm1_max(struct device * dev,struct device_attribute * attr,char * buf)61699736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
61799736703SOleg Chernovskiy 					 struct device_attribute *attr,
61899736703SOleg Chernovskiy 					 char *buf)
61999736703SOleg Chernovskiy {
620082452e1SAlex Deucher 	return sprintf(buf, "%i\n", 255);
62199736703SOleg Chernovskiy }
62299736703SOleg Chernovskiy 
radeon_hwmon_set_pwm1(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)62399736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
62499736703SOleg Chernovskiy 				     struct device_attribute *attr,
62599736703SOleg Chernovskiy 				     const char *buf, size_t count)
62699736703SOleg Chernovskiy {
62799736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
62899736703SOleg Chernovskiy 	int err;
62999736703SOleg Chernovskiy 	u32 value;
63099736703SOleg Chernovskiy 
63199736703SOleg Chernovskiy 	err = kstrtou32(buf, 10, &value);
63299736703SOleg Chernovskiy 	if (err)
63399736703SOleg Chernovskiy 		return err;
63499736703SOleg Chernovskiy 
635082452e1SAlex Deucher 	value = (value * 100) / 255;
636082452e1SAlex Deucher 
63799736703SOleg Chernovskiy 	err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
63899736703SOleg Chernovskiy 	if (err)
63999736703SOleg Chernovskiy 		return err;
64099736703SOleg Chernovskiy 
64199736703SOleg Chernovskiy 	return count;
64299736703SOleg Chernovskiy }
64399736703SOleg Chernovskiy 
radeon_hwmon_get_pwm1(struct device * dev,struct device_attribute * attr,char * buf)64499736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
64599736703SOleg Chernovskiy 				     struct device_attribute *attr,
64699736703SOleg Chernovskiy 				     char *buf)
64799736703SOleg Chernovskiy {
64899736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
64999736703SOleg Chernovskiy 	int err;
65099736703SOleg Chernovskiy 	u32 speed;
65199736703SOleg Chernovskiy 
65299736703SOleg Chernovskiy 	err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
65399736703SOleg Chernovskiy 	if (err)
65499736703SOleg Chernovskiy 		return err;
65599736703SOleg Chernovskiy 
656082452e1SAlex Deucher 	speed = (speed * 255) / 100;
657082452e1SAlex Deucher 
65899736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", speed);
65999736703SOleg Chernovskiy }
66099736703SOleg Chernovskiy 
661ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
662ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
663da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
66470d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
66570d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
66670d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
667ce8f5370SAlex Deucher 
radeon_hwmon_show_temp(struct device * dev,struct device_attribute * attr,char * buf)66821a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
66921a8122aSAlex Deucher 				      struct device_attribute *attr,
67021a8122aSAlex Deucher 				      char *buf)
67121a8122aSAlex Deucher {
672ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
673*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
67420d391d7SAlex Deucher 	int temp;
67521a8122aSAlex Deucher 
6764f2f2039SAlex Deucher 	/* Can't get temperature when the card is off */
6774f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
6784f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
6794f2f2039SAlex Deucher 		return -EINVAL;
6804f2f2039SAlex Deucher 
6816bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
6826bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
6836bd1c385SAlex Deucher 	else
68421a8122aSAlex Deucher 		temp = 0;
68521a8122aSAlex Deucher 
6862b0e617aSTian Tao 	return sysfs_emit(buf, "%d\n", temp);
68721a8122aSAlex Deucher }
68821a8122aSAlex Deucher 
radeon_hwmon_show_temp_thresh(struct device * dev,struct device_attribute * attr,char * buf)6896ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
6906ea4e84dSJean Delvare 					     struct device_attribute *attr,
6916ea4e84dSJean Delvare 					     char *buf)
6926ea4e84dSJean Delvare {
693e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
6946ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
6956ea4e84dSJean Delvare 	int temp;
6966ea4e84dSJean Delvare 
6976ea4e84dSJean Delvare 	if (hyst)
6986ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
6996ea4e84dSJean Delvare 	else
7006ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
7016ea4e84dSJean Delvare 
7022b0e617aSTian Tao 	return sysfs_emit(buf, "%d\n", temp);
7036ea4e84dSJean Delvare }
7046ea4e84dSJean Delvare 
70521a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
7066ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
7076ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
70899736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
70999736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
71099736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
71199736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
71299736703SOleg Chernovskiy 
radeon_hwmon_show_sclk(struct device * dev,struct device_attribute * attr,char * buf)713052813d9SSandeep Raghuraman static ssize_t radeon_hwmon_show_sclk(struct device *dev,
714052813d9SSandeep Raghuraman 				      struct device_attribute *attr, char *buf)
715052813d9SSandeep Raghuraman {
716052813d9SSandeep Raghuraman 	struct radeon_device *rdev = dev_get_drvdata(dev);
717*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
718052813d9SSandeep Raghuraman 	u32 sclk = 0;
719052813d9SSandeep Raghuraman 
720052813d9SSandeep Raghuraman 	/* Can't get clock frequency when the card is off */
721052813d9SSandeep Raghuraman 	if ((rdev->flags & RADEON_IS_PX) &&
722052813d9SSandeep Raghuraman 	    (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
723052813d9SSandeep Raghuraman 		return -EINVAL;
724052813d9SSandeep Raghuraman 
725052813d9SSandeep Raghuraman 	if (rdev->asic->dpm.get_current_sclk)
726052813d9SSandeep Raghuraman 		sclk = radeon_dpm_get_current_sclk(rdev);
727052813d9SSandeep Raghuraman 
728052813d9SSandeep Raghuraman 	/* Value returned by dpm is in 10 KHz units, need to convert it into Hz
729052813d9SSandeep Raghuraman 	   for hwmon */
730052813d9SSandeep Raghuraman 	sclk *= 10000;
731052813d9SSandeep Raghuraman 
7322b0e617aSTian Tao 	return sysfs_emit(buf, "%u\n", sclk);
733052813d9SSandeep Raghuraman }
734052813d9SSandeep Raghuraman 
735052813d9SSandeep Raghuraman static SENSOR_DEVICE_ATTR(freq1_input, S_IRUGO, radeon_hwmon_show_sclk, NULL,
736052813d9SSandeep Raghuraman 			  0);
737052813d9SSandeep Raghuraman 
radeon_hwmon_show_vddc(struct device * dev,struct device_attribute * attr,char * buf)738fddc611cSSandeep Raghuraman static ssize_t radeon_hwmon_show_vddc(struct device *dev,
739fddc611cSSandeep Raghuraman 				      struct device_attribute *attr, char *buf)
740fddc611cSSandeep Raghuraman {
741fddc611cSSandeep Raghuraman 	struct radeon_device *rdev = dev_get_drvdata(dev);
742*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
743fddc611cSSandeep Raghuraman 	u16 vddc = 0;
744fddc611cSSandeep Raghuraman 
745fddc611cSSandeep Raghuraman 	/* Can't get vddc when the card is off */
746fddc611cSSandeep Raghuraman 	if ((rdev->flags & RADEON_IS_PX) &&
747fddc611cSSandeep Raghuraman 		(ddev->switch_power_state != DRM_SWITCH_POWER_ON))
748fddc611cSSandeep Raghuraman 		return -EINVAL;
749fddc611cSSandeep Raghuraman 
750fddc611cSSandeep Raghuraman 	if (rdev->asic->dpm.get_current_vddc)
751fddc611cSSandeep Raghuraman 		vddc = rdev->asic->dpm.get_current_vddc(rdev);
752fddc611cSSandeep Raghuraman 
7532b0e617aSTian Tao 	return sysfs_emit(buf, "%u\n", vddc);
754fddc611cSSandeep Raghuraman }
755fddc611cSSandeep Raghuraman 
756fddc611cSSandeep Raghuraman static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, radeon_hwmon_show_vddc, NULL,
757fddc611cSSandeep Raghuraman 			  0);
75821a8122aSAlex Deucher 
75921a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
76021a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
7616ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
7626ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
76399736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1.dev_attr.attr,
76499736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
76599736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
76699736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
767052813d9SSandeep Raghuraman 	&sensor_dev_attr_freq1_input.dev_attr.attr,
768fddc611cSSandeep Raghuraman 	&sensor_dev_attr_in0_input.dev_attr.attr,
76921a8122aSAlex Deucher 	NULL
77021a8122aSAlex Deucher };
77121a8122aSAlex Deucher 
hwmon_attributes_visible(struct kobject * kobj,struct attribute * attr,int index)7726ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
7736ea4e84dSJean Delvare 					struct attribute *attr, int index)
7746ea4e84dSJean Delvare {
775e3837b00SGeliang Tang 	struct device *dev = kobj_to_dev(kobj);
776e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
77799736703SOleg Chernovskiy 	umode_t effective_mode = attr->mode;
7786ea4e84dSJean Delvare 
7792a7d44f4SAlex Deucher 	/* Skip attributes if DPM is not enabled */
7806ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
7816ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
7822a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
7832a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
7842a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
7852a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
786052813d9SSandeep Raghuraman 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr ||
787fddc611cSSandeep Raghuraman 	     attr == &sensor_dev_attr_freq1_input.dev_attr.attr ||
788fddc611cSSandeep Raghuraman 	     attr == &sensor_dev_attr_in0_input.dev_attr.attr))
789fddc611cSSandeep Raghuraman 		return 0;
790fddc611cSSandeep Raghuraman 
791fddc611cSSandeep Raghuraman 	/* Skip vddc attribute if get_current_vddc is not implemented */
792fddc611cSSandeep Raghuraman 	if(attr == &sensor_dev_attr_in0_input.dev_attr.attr &&
793fddc611cSSandeep Raghuraman 		!rdev->asic->dpm.get_current_vddc)
7946ea4e84dSJean Delvare 		return 0;
7956ea4e84dSJean Delvare 
79699736703SOleg Chernovskiy 	/* Skip fan attributes if fan is not present */
79799736703SOleg Chernovskiy 	if (rdev->pm.no_fan &&
79899736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
79999736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
80099736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
80199736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
80299736703SOleg Chernovskiy 		return 0;
80399736703SOleg Chernovskiy 
80499736703SOleg Chernovskiy 	/* mask fan attributes if we have no bindings for this asic to expose */
80599736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.get_fan_speed_percent &&
80699736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
80799736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_get_mode &&
80899736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
80999736703SOleg Chernovskiy 		effective_mode &= ~S_IRUGO;
81099736703SOleg Chernovskiy 
81199736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
81299736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
81399736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_set_mode &&
81499736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
81599736703SOleg Chernovskiy 		effective_mode &= ~S_IWUSR;
81699736703SOleg Chernovskiy 
81799736703SOleg Chernovskiy 	/* hide max/min values if we can't both query and manage the fan */
81899736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
81999736703SOleg Chernovskiy 	     !rdev->asic->dpm.get_fan_speed_percent) &&
82099736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
82199736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
82299736703SOleg Chernovskiy 		return 0;
82399736703SOleg Chernovskiy 
82499736703SOleg Chernovskiy 	return effective_mode;
8256ea4e84dSJean Delvare }
8266ea4e84dSJean Delvare 
82721a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
82821a8122aSAlex Deucher 	.attrs = hwmon_attributes,
8296ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
83021a8122aSAlex Deucher };
83121a8122aSAlex Deucher 
832ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
833ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
834ec39f64bSGuenter Roeck 	NULL
835ec39f64bSGuenter Roeck };
836ec39f64bSGuenter Roeck 
radeon_hwmon_init(struct radeon_device * rdev)8370d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
83821a8122aSAlex Deucher {
8390d18abedSDan Carpenter 	int err = 0;
84021a8122aSAlex Deucher 
84121a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
84221a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
84321a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
84421a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
845457558edSAlex Deucher 	case THERMAL_TYPE_NI:
846e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
8471bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
848286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
849286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
8506bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
8515d7486c7SAlex Deucher 			return err;
852cb3e4e7cSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
853ec39f64bSGuenter Roeck 									   "radeon", rdev,
854ec39f64bSGuenter Roeck 									   hwmon_groups);
855cb3e4e7cSAlex Deucher 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
856cb3e4e7cSAlex Deucher 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
8570d18abedSDan Carpenter 			dev_err(rdev->dev,
8580d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
8590d18abedSDan Carpenter 		}
86021a8122aSAlex Deucher 		break;
86121a8122aSAlex Deucher 	default:
86221a8122aSAlex Deucher 		break;
86321a8122aSAlex Deucher 	}
8640d18abedSDan Carpenter 
8650d18abedSDan Carpenter 	return err;
86621a8122aSAlex Deucher }
86721a8122aSAlex Deucher 
radeon_hwmon_fini(struct radeon_device * rdev)868cb3e4e7cSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
869cb3e4e7cSAlex Deucher {
870cb3e4e7cSAlex Deucher 	if (rdev->pm.int_hwmon_dev)
871cb3e4e7cSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
872cb3e4e7cSAlex Deucher }
873cb3e4e7cSAlex Deucher 
radeon_dpm_thermal_work_handler(struct work_struct * work)874da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
875da321c8aSAlex Deucher {
876da321c8aSAlex Deucher 	struct radeon_device *rdev =
877da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
878da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
879da321c8aSAlex Deucher 	/* switch to the thermal state */
880da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
881da321c8aSAlex Deucher 
882da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
883da321c8aSAlex Deucher 		return;
884da321c8aSAlex Deucher 
885da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
886da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
887da321c8aSAlex Deucher 
888da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
889da321c8aSAlex Deucher 			/* switch back the user state */
890da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
891da321c8aSAlex Deucher 	} else {
892da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
893da321c8aSAlex Deucher 			/* switch back the user state */
894da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
895da321c8aSAlex Deucher 	}
89660320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
89760320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
89860320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
89960320347SAlex Deucher 	else
90060320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
90160320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
90260320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
90360320347SAlex Deucher 
90460320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
905da321c8aSAlex Deucher }
906da321c8aSAlex Deucher 
radeon_dpm_single_display(struct radeon_device * rdev)9073899ca84SAlex Deucher static bool radeon_dpm_single_display(struct radeon_device *rdev)
908da321c8aSAlex Deucher {
90948783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
91048783069SAlex Deucher 		true : false;
91148783069SAlex Deucher 
91248783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
91348783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
91448783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
91548783069SAlex Deucher 			single_display = false;
91648783069SAlex Deucher 	}
917da321c8aSAlex Deucher 
918951caa6aSAlex Deucher 	/* 120hz tends to be problematic even if they are under the
919951caa6aSAlex Deucher 	 * vblank limit.
920951caa6aSAlex Deucher 	 */
921951caa6aSAlex Deucher 	if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
922951caa6aSAlex Deucher 		single_display = false;
923951caa6aSAlex Deucher 
9243899ca84SAlex Deucher 	return single_display;
9253899ca84SAlex Deucher }
9263899ca84SAlex Deucher 
radeon_dpm_pick_power_state(struct radeon_device * rdev,enum radeon_pm_state_type dpm_state)9273899ca84SAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
9283899ca84SAlex Deucher 						     enum radeon_pm_state_type dpm_state)
9293899ca84SAlex Deucher {
9303899ca84SAlex Deucher 	int i;
9313899ca84SAlex Deucher 	struct radeon_ps *ps;
9323899ca84SAlex Deucher 	u32 ui_class;
9333899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
9343899ca84SAlex Deucher 
935edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
936edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
937edcaa5b1SAlex Deucher 	 */
938edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
939edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
940da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
941da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
94253bf277bSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
943da321c8aSAlex Deucher 
944edcaa5b1SAlex Deucher restart_search:
945da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
946da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
947da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
948da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
949da321c8aSAlex Deucher 		switch (dpm_state) {
950da321c8aSAlex Deucher 		/* user states */
951da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
952da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
953da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
95448783069SAlex Deucher 					if (single_display)
955da321c8aSAlex Deucher 						return ps;
956da321c8aSAlex Deucher 				} else
957da321c8aSAlex Deucher 					return ps;
958da321c8aSAlex Deucher 			}
959da321c8aSAlex Deucher 			break;
960da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
961da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
962da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
96348783069SAlex Deucher 					if (single_display)
964da321c8aSAlex Deucher 						return ps;
965da321c8aSAlex Deucher 				} else
966da321c8aSAlex Deucher 					return ps;
967da321c8aSAlex Deucher 			}
968da321c8aSAlex Deucher 			break;
969da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
970da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
971da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
97248783069SAlex Deucher 					if (single_display)
973da321c8aSAlex Deucher 						return ps;
974da321c8aSAlex Deucher 				} else
975da321c8aSAlex Deucher 					return ps;
976da321c8aSAlex Deucher 			}
977da321c8aSAlex Deucher 			break;
978da321c8aSAlex Deucher 		/* internal states */
979da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
980d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
981da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
982d4d3278cSAlex Deucher 			else
983d4d3278cSAlex Deucher 				break;
984da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
985da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
986da321c8aSAlex Deucher 				return ps;
987da321c8aSAlex Deucher 			break;
988da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
989da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
990da321c8aSAlex Deucher 				return ps;
991da321c8aSAlex Deucher 			break;
992da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
993da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
994da321c8aSAlex Deucher 				return ps;
995da321c8aSAlex Deucher 			break;
996da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
997da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
998da321c8aSAlex Deucher 				return ps;
999da321c8aSAlex Deucher 			break;
1000da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
1001da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
1002da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
1003da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
1004da321c8aSAlex Deucher 				return ps;
1005da321c8aSAlex Deucher 			break;
1006da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
1007da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
1008da321c8aSAlex Deucher 				return ps;
1009da321c8aSAlex Deucher 			break;
1010da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
1011da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
1012da321c8aSAlex Deucher 				return ps;
1013da321c8aSAlex Deucher 			break;
1014edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
1015edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
1016edcaa5b1SAlex Deucher 				return ps;
1017edcaa5b1SAlex Deucher 			break;
1018da321c8aSAlex Deucher 		default:
1019da321c8aSAlex Deucher 			break;
1020da321c8aSAlex Deucher 		}
1021da321c8aSAlex Deucher 	}
1022da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
1023da321c8aSAlex Deucher 	switch (dpm_state) {
1024da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
1025ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1026ce3537d5SAlex Deucher 		goto restart_search;
1027da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
1028da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
1029da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
1030d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
1031da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
1032d4d3278cSAlex Deucher 		} else {
1033d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1034d4d3278cSAlex Deucher 			goto restart_search;
1035d4d3278cSAlex Deucher 		}
1036da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
1037da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
1038da321c8aSAlex Deucher 		goto restart_search;
1039da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
1040da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
1041da321c8aSAlex Deucher 		goto restart_search;
1042da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
1043edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
1044edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
1045da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
1046da321c8aSAlex Deucher 		goto restart_search;
1047da321c8aSAlex Deucher 	default:
1048da321c8aSAlex Deucher 		break;
1049da321c8aSAlex Deucher 	}
1050da321c8aSAlex Deucher 
1051da321c8aSAlex Deucher 	return NULL;
1052da321c8aSAlex Deucher }
1053da321c8aSAlex Deucher 
radeon_dpm_change_power_state_locked(struct radeon_device * rdev)1054da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
1055da321c8aSAlex Deucher {
1056da321c8aSAlex Deucher 	int i;
1057da321c8aSAlex Deucher 	struct radeon_ps *ps;
1058da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
105984dd1928SAlex Deucher 	int ret;
10603899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
1061da321c8aSAlex Deucher 
1062da321c8aSAlex Deucher 	/* if dpm init failed */
1063da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
1064da321c8aSAlex Deucher 		return;
1065da321c8aSAlex Deucher 
1066da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1067da321c8aSAlex Deucher 		/* add other state override checks here */
10688a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
10698a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
1070da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1071da321c8aSAlex Deucher 	}
1072da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
1073da321c8aSAlex Deucher 
1074da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1075da321c8aSAlex Deucher 	if (ps)
107689c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
1077da321c8aSAlex Deucher 	else
1078da321c8aSAlex Deucher 		return;
1079da321c8aSAlex Deucher 
1080d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
1081da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
1082b62d628bSAlex Deucher 		/* vce just modifies an existing state so force a change */
1083b62d628bSAlex Deucher 		if (ps->vce_active != rdev->pm.dpm.vce_active)
1084b62d628bSAlex Deucher 			goto force;
10853899ca84SAlex Deucher 		/* user has made a display change (such as timing) */
10863899ca84SAlex Deucher 		if (rdev->pm.dpm.single_display != single_display)
10873899ca84SAlex Deucher 			goto force;
1088d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1089d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
1090d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
1091d22b7e40SAlex Deucher 			 */
1092da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1093d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
1094da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
1095da321c8aSAlex Deucher 				/* update displays */
1096da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
1097da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1098da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1099da321c8aSAlex Deucher 			}
1100da321c8aSAlex Deucher 			return;
1101d22b7e40SAlex Deucher 		} else {
1102d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
1103d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
1104d22b7e40SAlex Deucher 			 * update display configuration.
1105d22b7e40SAlex Deucher 			 */
1106d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
1107d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
1108d22b7e40SAlex Deucher 				return;
1109d22b7e40SAlex Deucher 			} else {
1110d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1111d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
1112d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
1113d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
1114d22b7e40SAlex Deucher 					/* update displays */
1115d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
1116d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1117d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1118d22b7e40SAlex Deucher 					return;
1119d22b7e40SAlex Deucher 				}
1120d22b7e40SAlex Deucher 			}
1121d22b7e40SAlex Deucher 		}
1122da321c8aSAlex Deucher 	}
1123da321c8aSAlex Deucher 
1124b62d628bSAlex Deucher force:
1125033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
1126da321c8aSAlex Deucher 		printk("switching from power state:\n");
1127da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1128da321c8aSAlex Deucher 		printk("switching to power state:\n");
1129da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1130033a37dfSAlex Deucher 	}
1131b62d628bSAlex Deucher 
1132da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
1133da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
1134da321c8aSAlex Deucher 
1135b62d628bSAlex Deucher 	/* update whether vce is active */
1136b62d628bSAlex Deucher 	ps->vce_active = rdev->pm.dpm.vce_active;
1137b62d628bSAlex Deucher 
113884dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
113984dd1928SAlex Deucher 	if (ret)
114084dd1928SAlex Deucher 		goto done;
114184dd1928SAlex Deucher 
1142da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
1143da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
1144d74e766eSAlex Deucher 	/* update displays */
1145d74e766eSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
1146da321c8aSAlex Deucher 
1147da321c8aSAlex Deucher 	/* wait for the rings to drain */
1148da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
1149da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
1150da321c8aSAlex Deucher 		if (ring->ready)
115137615527SChristian König 			radeon_fence_wait_empty(rdev, i);
1152da321c8aSAlex Deucher 	}
1153da321c8aSAlex Deucher 
1154da321c8aSAlex Deucher 	/* program the new power state */
1155da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
1156da321c8aSAlex Deucher 
1157da321c8aSAlex Deucher 	/* update current power state */
1158da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1159da321c8aSAlex Deucher 
116084dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
116184dd1928SAlex Deucher 
11625e031d9fSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
11635e031d9fSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
11645e031d9fSAlex Deucher 	rdev->pm.dpm.single_display = single_display;
11655e031d9fSAlex Deucher 
11661cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
116714ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
116814ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
116960320347SAlex Deucher 			/* force low perf level for thermal */
117060320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
117114ac88afSAlex Deucher 			/* save the user's level */
117214ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
117314ac88afSAlex Deucher 		} else {
117414ac88afSAlex Deucher 			/* otherwise, user selected level */
117514ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
117614ac88afSAlex Deucher 		}
117760320347SAlex Deucher 	}
117860320347SAlex Deucher 
117984dd1928SAlex Deucher done:
1180da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
1181da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
1182da321c8aSAlex Deucher }
1183da321c8aSAlex Deucher 
radeon_dpm_enable_uvd(struct radeon_device * rdev,bool enable)1184ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1185ce3537d5SAlex Deucher {
1186ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
1187ce3537d5SAlex Deucher 
11889e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
11899e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
11908158eb9eSChristian König 		/* don't powergate anything if we
11918158eb9eSChristian König 		   have active but pause streams */
11928158eb9eSChristian König 		enable |= rdev->pm.dpm.sd > 0;
11938158eb9eSChristian König 		enable |= rdev->pm.dpm.hd > 0;
11949e9d9762SAlex Deucher 		/* enable/disable UVD */
11959e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
11969e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
11979e9d9762SAlex Deucher 	} else {
1198ce3537d5SAlex Deucher 		if (enable) {
1199ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1200ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
12010690a229SAlex Deucher 			/* disable this for now */
12020690a229SAlex Deucher #if 0
1203ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1204ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1205ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1206ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1207ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1208ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1209ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1210ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1211ce3537d5SAlex Deucher 			else
12120690a229SAlex Deucher #endif
1213ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1214ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
1215ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1216ce3537d5SAlex Deucher 		} else {
1217ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1218ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
1219ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1220ce3537d5SAlex Deucher 		}
1221ce3537d5SAlex Deucher 
1222ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1223ce3537d5SAlex Deucher 	}
12249e9d9762SAlex Deucher }
1225ce3537d5SAlex Deucher 
radeon_dpm_enable_vce(struct radeon_device * rdev,bool enable)122603afe6f6SAlex Deucher void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
122703afe6f6SAlex Deucher {
122803afe6f6SAlex Deucher 	if (enable) {
122903afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
123003afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = true;
123103afe6f6SAlex Deucher 		/* XXX select vce level based on ring/task */
123203afe6f6SAlex Deucher 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
123303afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
123403afe6f6SAlex Deucher 	} else {
123503afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
123603afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = false;
123703afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
123803afe6f6SAlex Deucher 	}
123903afe6f6SAlex Deucher 
124003afe6f6SAlex Deucher 	radeon_pm_compute_clocks(rdev);
124103afe6f6SAlex Deucher }
124203afe6f6SAlex Deucher 
radeon_pm_suspend_old(struct radeon_device * rdev)1243da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
1244ce8f5370SAlex Deucher {
1245ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
12463f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
12473f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
12483f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
12493f53eb6fSRafael J. Wysocki 	}
1250ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
125132c87fcaSTejun Heo 
125232c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1253ce8f5370SAlex Deucher }
1254ce8f5370SAlex Deucher 
radeon_pm_suspend_dpm(struct radeon_device * rdev)1255da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1256da321c8aSAlex Deucher {
1257da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1258da321c8aSAlex Deucher 	/* disable dpm */
1259da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
1260da321c8aSAlex Deucher 	/* reset the power state */
1261da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1262da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
1263da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1264da321c8aSAlex Deucher }
1265da321c8aSAlex Deucher 
radeon_pm_suspend(struct radeon_device * rdev)1266da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1267da321c8aSAlex Deucher {
1268da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1269da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1270da321c8aSAlex Deucher 	else
1271da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1272da321c8aSAlex Deucher }
1273da321c8aSAlex Deucher 
radeon_pm_resume_old(struct radeon_device * rdev)1274da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1275ce8f5370SAlex Deucher {
1276ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
12772e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
127836099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
12792e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1280ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
12818a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
12828a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
12832feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
12842feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
12852feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1286ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1287ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1288ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1289ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1290ed18a360SAlex Deucher 	}
1291f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1292f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1293f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1294f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
12959ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
12969ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
129737016951SMichel Dänzer 	if (rdev->pm.power_state) {
12984d60173fSAlex Deucher 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
12992feea49aSAlex Deucher 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
130037016951SMichel Dänzer 	}
13013f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
13023f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
13033f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
130432c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
13053f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
13063f53eb6fSRafael J. Wysocki 	}
1307f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1308ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1309d0d6cb81SRafał Miłecki }
1310d0d6cb81SRafał Miłecki 
radeon_pm_resume_dpm(struct radeon_device * rdev)1311da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
13127433874eSRafał Miłecki {
131326481fb1SDave Airlie 	int ret;
13140d18abedSDan Carpenter 
1315da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1316da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1317da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1318da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1319da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1320da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1321e14cd2bbSAlex Deucher 	if (ret)
1322e14cd2bbSAlex Deucher 		goto dpm_resume_fail;
1323e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = true;
1324e14cd2bbSAlex Deucher 	return;
1325e14cd2bbSAlex Deucher 
1326e14cd2bbSAlex Deucher dpm_resume_fail:
1327da321c8aSAlex Deucher 	DRM_ERROR("radeon: dpm resume failed\n");
1328da321c8aSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
132936099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1330da321c8aSAlex Deucher 	    rdev->mc_fw) {
1331da321c8aSAlex Deucher 		if (rdev->pm.default_vddc)
1332da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1333da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1334da321c8aSAlex Deucher 		if (rdev->pm.default_vddci)
1335da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1336da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1337da321c8aSAlex Deucher 		if (rdev->pm.default_sclk)
1338da321c8aSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1339da321c8aSAlex Deucher 		if (rdev->pm.default_mclk)
1340da321c8aSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1341da321c8aSAlex Deucher 	}
1342da321c8aSAlex Deucher }
1343da321c8aSAlex Deucher 
radeon_pm_resume(struct radeon_device * rdev)1344da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1345da321c8aSAlex Deucher {
1346da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1347da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1348da321c8aSAlex Deucher 	else
1349da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1350da321c8aSAlex Deucher }
1351da321c8aSAlex Deucher 
radeon_pm_init_old(struct radeon_device * rdev)1352da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1353da321c8aSAlex Deucher {
1354da321c8aSAlex Deucher 	int ret;
1355da321c8aSAlex Deucher 
1356f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1357ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1358ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1359ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1360ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
13619ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
13629ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1363f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1364f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
136521a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1366c913e23aSRafał Miłecki 
136756278a8eSAlex Deucher 	if (rdev->bios) {
136856278a8eSAlex Deucher 		if (rdev->is_atom_bios)
136956278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
137056278a8eSAlex Deucher 		else
137156278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1372f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1373ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1374ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
13752e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
137636099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
13772e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1378ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
13798a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
13808a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
13814639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
13824639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
13834639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1384ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1385ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1386ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1387ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1388ed18a360SAlex Deucher 		}
138956278a8eSAlex Deucher 	}
139056278a8eSAlex Deucher 
139121a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
13920d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
13930d18abedSDan Carpenter 	if (ret)
13940d18abedSDan Carpenter 		return ret;
139532c87fcaSTejun Heo 
139632c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
139732c87fcaSTejun Heo 
1398ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
13995b54d679SNirmoy Das 		radeon_debugfs_pm_init(rdev);
1400c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1401ce8f5370SAlex Deucher 	}
1402c913e23aSRafał Miłecki 
14037433874eSRafał Miłecki 	return 0;
14047433874eSRafał Miłecki }
14057433874eSRafał Miłecki 
radeon_dpm_print_power_states(struct radeon_device * rdev)1406da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1407da321c8aSAlex Deucher {
1408da321c8aSAlex Deucher 	int i;
1409da321c8aSAlex Deucher 
1410da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1411da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1412da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1413da321c8aSAlex Deucher 	}
1414da321c8aSAlex Deucher }
1415da321c8aSAlex Deucher 
radeon_pm_init_dpm(struct radeon_device * rdev)1416da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1417da321c8aSAlex Deucher {
1418da321c8aSAlex Deucher 	int ret;
1419da321c8aSAlex Deucher 
14201cd8b21aSAlex Deucher 	/* default to balanced state */
1421edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1422edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
14231cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1424da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1425da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1426da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1427da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1428da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1429da321c8aSAlex Deucher 
1430da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1431da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1432da321c8aSAlex Deucher 	else
1433da321c8aSAlex Deucher 		return -EINVAL;
1434da321c8aSAlex Deucher 
1435da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1436da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1437da321c8aSAlex Deucher 	if (ret)
1438da321c8aSAlex Deucher 		return ret;
1439da321c8aSAlex Deucher 
1440da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1441da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1442da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1443da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1444033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1445da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1446da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1447da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1448da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1449e14cd2bbSAlex Deucher 	if (ret)
1450e14cd2bbSAlex Deucher 		goto dpm_failed;
1451da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1452da321c8aSAlex Deucher 
14535b54d679SNirmoy Das 	radeon_debugfs_pm_init(rdev);
14541316b792SAlex Deucher 
1455da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1456da321c8aSAlex Deucher 
1457da321c8aSAlex Deucher 	return 0;
1458e14cd2bbSAlex Deucher 
1459e14cd2bbSAlex Deucher dpm_failed:
1460e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = false;
1461e14cd2bbSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
1462e14cd2bbSAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1463e14cd2bbSAlex Deucher 	    rdev->mc_fw) {
1464e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddc)
1465e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1466e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1467e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddci)
1468e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1469e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1470e14cd2bbSAlex Deucher 		if (rdev->pm.default_sclk)
1471e14cd2bbSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1472e14cd2bbSAlex Deucher 		if (rdev->pm.default_mclk)
1473e14cd2bbSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1474e14cd2bbSAlex Deucher 	}
1475e14cd2bbSAlex Deucher 	DRM_ERROR("radeon: dpm initialization failed\n");
1476e14cd2bbSAlex Deucher 	return ret;
1477da321c8aSAlex Deucher }
1478da321c8aSAlex Deucher 
14794369a69eSAlex Deucher struct radeon_dpm_quirk {
14804369a69eSAlex Deucher 	u32 chip_vendor;
14814369a69eSAlex Deucher 	u32 chip_device;
14824369a69eSAlex Deucher 	u32 subsys_vendor;
14834369a69eSAlex Deucher 	u32 subsys_device;
14844369a69eSAlex Deucher };
14854369a69eSAlex Deucher 
14864369a69eSAlex Deucher /* cards with dpm stability problems */
14874369a69eSAlex Deucher static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
14884369a69eSAlex Deucher 	/* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
14894369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
14904369a69eSAlex Deucher 	/* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
14914369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
14924369a69eSAlex Deucher 	{ 0, 0, 0, 0 },
14934369a69eSAlex Deucher };
14944369a69eSAlex Deucher 
radeon_pm_init(struct radeon_device * rdev)1495da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1496da321c8aSAlex Deucher {
14974369a69eSAlex Deucher 	struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
14984369a69eSAlex Deucher 	bool disable_dpm = false;
14994369a69eSAlex Deucher 
15004369a69eSAlex Deucher 	/* Apply dpm quirks */
15014369a69eSAlex Deucher 	while (p && p->chip_device != 0) {
15024369a69eSAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
15034369a69eSAlex Deucher 		    rdev->pdev->device == p->chip_device &&
15044369a69eSAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
15054369a69eSAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
15064369a69eSAlex Deucher 			disable_dpm = true;
15074369a69eSAlex Deucher 			break;
15084369a69eSAlex Deucher 		}
15094369a69eSAlex Deucher 		++p;
15104369a69eSAlex Deucher 	}
15114369a69eSAlex Deucher 
1512da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1513da321c8aSAlex Deucher 	switch (rdev->family) {
15144a6369e9SAlex Deucher 	case CHIP_RV610:
15154a6369e9SAlex Deucher 	case CHIP_RV630:
15164a6369e9SAlex Deucher 	case CHIP_RV620:
15174a6369e9SAlex Deucher 	case CHIP_RV635:
15184a6369e9SAlex Deucher 	case CHIP_RV670:
15199d67006eSAlex Deucher 	case CHIP_RS780:
15209d67006eSAlex Deucher 	case CHIP_RS880:
152176e6dcecSAlex Deucher 	case CHIP_RV770:
15228a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1523761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1524761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15258a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
15268a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
15278a53fa23SAlex Deucher 			 (!rdev->smc_fw))
15288a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1529761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
15309d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
15319d67006eSAlex Deucher 		else
15329d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15339d67006eSAlex Deucher 		break;
1534ab70b1ddSAlex Deucher 	case CHIP_RV730:
1535ab70b1ddSAlex Deucher 	case CHIP_RV710:
1536ab70b1ddSAlex Deucher 	case CHIP_RV740:
153759f7a2f2SAlex Deucher 	case CHIP_CEDAR:
153859f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
153959f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
154059f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
154159f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
15425a16f761SAlex Deucher 	case CHIP_PALM:
15435a16f761SAlex Deucher 	case CHIP_SUMO:
15445a16f761SAlex Deucher 	case CHIP_SUMO2:
1545c08abf11SAlex Deucher 	case CHIP_BARTS:
1546c08abf11SAlex Deucher 	case CHIP_TURKS:
1547c08abf11SAlex Deucher 	case CHIP_CAICOS:
15488f500af4SAlex Deucher 	case CHIP_CAYMAN:
15493a118989SAlex Deucher 	case CHIP_ARUBA:
155068bc7785SAlex Deucher 	case CHIP_TAHITI:
155168bc7785SAlex Deucher 	case CHIP_PITCAIRN:
155268bc7785SAlex Deucher 	case CHIP_VERDE:
155368bc7785SAlex Deucher 	case CHIP_OLAND:
155468bc7785SAlex Deucher 	case CHIP_HAINAN:
15554f22dde3SAlex Deucher 	case CHIP_BONAIRE:
1556e308b1d3SAlex Deucher 	case CHIP_KABINI:
1557e308b1d3SAlex Deucher 	case CHIP_KAVERI:
15584f22dde3SAlex Deucher 	case CHIP_HAWAII:
15597d032a4bSSamuel Li 	case CHIP_MULLINS:
15605a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
15615a16f761SAlex Deucher 		if (!rdev->rlc_fw)
15625a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15635a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
15645a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
15655a16f761SAlex Deucher 			 (!rdev->smc_fw))
15665a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15674369a69eSAlex Deucher 		else if (disable_dpm && (radeon_dpm == -1))
15684369a69eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15695a16f761SAlex Deucher 		else if (radeon_dpm == 0)
15705a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15715a16f761SAlex Deucher 		else
15725a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
15735a16f761SAlex Deucher 		break;
1574da321c8aSAlex Deucher 	default:
1575da321c8aSAlex Deucher 		/* default to profile method */
1576da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1577da321c8aSAlex Deucher 		break;
1578da321c8aSAlex Deucher 	}
1579da321c8aSAlex Deucher 
1580da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1581da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1582da321c8aSAlex Deucher 	else
1583da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1584da321c8aSAlex Deucher }
1585da321c8aSAlex Deucher 
radeon_pm_late_init(struct radeon_device * rdev)1586914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1587914a8987SAlex Deucher {
1588914a8987SAlex Deucher 	int ret = 0;
1589914a8987SAlex Deucher 
1590914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
159151a4726bSAlex Deucher 		if (rdev->pm.dpm_enabled) {
159249abb266SAlex Deucher 			if (!rdev->pm.sysfs_initialized) {
159351a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
159451a4726bSAlex Deucher 				if (ret)
159551a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
159651a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
159751a4726bSAlex Deucher 				if (ret)
159851a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
159951a4726bSAlex Deucher 				/* XXX: these are noops for dpm but are here for backwards compat */
160051a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_profile);
160151a4726bSAlex Deucher 				if (ret)
160251a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power profile\n");
160351a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_method);
160451a4726bSAlex Deucher 				if (ret)
160551a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power method\n");
160649abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
160749abb266SAlex Deucher 			}
160851a4726bSAlex Deucher 
1609914a8987SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1610914a8987SAlex Deucher 			ret = radeon_dpm_late_enable(rdev);
1611914a8987SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
161251a4726bSAlex Deucher 			if (ret) {
161351a4726bSAlex Deucher 				rdev->pm.dpm_enabled = false;
161451a4726bSAlex Deucher 				DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
161551a4726bSAlex Deucher 			} else {
161651a4726bSAlex Deucher 				/* set the dpm state for PX since there won't be
161751a4726bSAlex Deucher 				 * a modeset to call this.
161851a4726bSAlex Deucher 				 */
161951a4726bSAlex Deucher 				radeon_pm_compute_clocks(rdev);
162051a4726bSAlex Deucher 			}
162151a4726bSAlex Deucher 		}
162251a4726bSAlex Deucher 	} else {
162349abb266SAlex Deucher 		if ((rdev->pm.num_power_states > 1) &&
162449abb266SAlex Deucher 		    (!rdev->pm.sysfs_initialized)) {
162551a4726bSAlex Deucher 			/* where's the best place to put these? */
162651a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_profile);
162751a4726bSAlex Deucher 			if (ret)
162851a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power profile\n");
162951a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_method);
163051a4726bSAlex Deucher 			if (ret)
163151a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power method\n");
163269f0b547SGuo Zhengkui 			else
163349abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
163451a4726bSAlex Deucher 		}
1635914a8987SAlex Deucher 	}
1636914a8987SAlex Deucher 	return ret;
1637914a8987SAlex Deucher }
1638914a8987SAlex Deucher 
radeon_pm_fini_old(struct radeon_device * rdev)1639da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
164029fb52caSAlex Deucher {
1641ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1642a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1643ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1644ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1645ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1646ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1647ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1648ce8f5370SAlex Deucher 			/* reset default clocks */
1649ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1650ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1651ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
165258e21dffSAlex Deucher 		}
1653ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
165432c87fcaSTejun Heo 
165532c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
165658e21dffSAlex Deucher 
1657ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1658ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1659ce8f5370SAlex Deucher 	}
1660a424816fSAlex Deucher 
1661cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
16620975b162SAlex Deucher 	kfree(rdev->pm.power_state);
166329fb52caSAlex Deucher }
166429fb52caSAlex Deucher 
radeon_pm_fini_dpm(struct radeon_device * rdev)1665da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1666da321c8aSAlex Deucher {
1667da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1668da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1669da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1670da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1671da321c8aSAlex Deucher 
1672da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
167370d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1674da321c8aSAlex Deucher 		/* XXX backwards compat */
1675da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1676da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1677da321c8aSAlex Deucher 	}
1678da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1679da321c8aSAlex Deucher 
1680cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
1681da321c8aSAlex Deucher 	kfree(rdev->pm.power_state);
1682da321c8aSAlex Deucher }
1683da321c8aSAlex Deucher 
radeon_pm_fini(struct radeon_device * rdev)1684da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1685da321c8aSAlex Deucher {
1686da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1687da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1688da321c8aSAlex Deucher 	else
1689da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1690da321c8aSAlex Deucher }
1691da321c8aSAlex Deucher 
radeon_pm_compute_clocks_old(struct radeon_device * rdev)1692da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1693c913e23aSRafał Miłecki {
1694*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
1695a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1696c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1697c913e23aSRafał Miłecki 
1698ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1699ce8f5370SAlex Deucher 		return;
1700ce8f5370SAlex Deucher 
1701c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1702c913e23aSRafał Miłecki 
1703c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1704a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
17053ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1706a48b9b4eSAlex Deucher 		list_for_each_entry(crtc,
1707a48b9b4eSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1708a48b9b4eSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1709a48b9b4eSAlex Deucher 			if (radeon_crtc->enabled) {
1710c913e23aSRafał Miłecki 				rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1711a48b9b4eSAlex Deucher 				rdev->pm.active_crtc_count++;
1712c913e23aSRafał Miłecki 			}
1713c913e23aSRafał Miłecki 		}
17143ed9a335SAlex Deucher 	}
1715c913e23aSRafał Miłecki 
1716ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1717ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1718ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1719ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1720ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1721a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1722ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1723ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1724c913e23aSRafał Miłecki 
1725ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1726ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1727ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1728ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1729c913e23aSRafał Miłecki 
1730d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1731c913e23aSRafał Miłecki 				}
1732a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1733c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1734c913e23aSRafał Miłecki 
1735ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1736ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1737ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1738ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1739ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1740c913e23aSRafał Miłecki 
174132c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1742c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1743ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1744ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
174532c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1746c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1747d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1748c913e23aSRafał Miłecki 				}
1749a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1750ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1751ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1752c913e23aSRafał Miłecki 
1753ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1754ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1755ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1756ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1757ce8f5370SAlex Deucher 				}
1758ce8f5370SAlex Deucher 			}
175973a6d3fcSRafał Miłecki 		}
1760c913e23aSRafał Miłecki 	}
1761c913e23aSRafał Miłecki 
1762c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1763c913e23aSRafał Miłecki }
1764c913e23aSRafał Miłecki 
radeon_pm_compute_clocks_dpm(struct radeon_device * rdev)1765da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1766da321c8aSAlex Deucher {
1767*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
1768da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1769da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1770227545b9SKai-Heng Feng 	struct radeon_connector *radeon_connector;
1771da321c8aSAlex Deucher 
17726c7bcceaSAlex Deucher 	if (!rdev->pm.dpm_enabled)
17736c7bcceaSAlex Deucher 		return;
17746c7bcceaSAlex Deucher 
1775da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1776da321c8aSAlex Deucher 
17775ca302f7SAlex Deucher 	/* update active crtc counts */
1778da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1779da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
1780227545b9SKai-Heng Feng 	rdev->pm.dpm.high_pixelclock_count = 0;
17813ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1782da321c8aSAlex Deucher 		list_for_each_entry(crtc,
1783da321c8aSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1784da321c8aSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1785da321c8aSAlex Deucher 			if (crtc->enabled) {
1786da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1787da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtc_count++;
1788227545b9SKai-Heng Feng 				if (!radeon_crtc->connector)
1789227545b9SKai-Heng Feng 					continue;
1790227545b9SKai-Heng Feng 
1791227545b9SKai-Heng Feng 				radeon_connector = to_radeon_connector(radeon_crtc->connector);
1792227545b9SKai-Heng Feng 				if (radeon_connector->pixelclock_for_modeset > 297000)
1793227545b9SKai-Heng Feng 					rdev->pm.dpm.high_pixelclock_count++;
1794da321c8aSAlex Deucher 			}
1795da321c8aSAlex Deucher 		}
17963ed9a335SAlex Deucher 	}
1797da321c8aSAlex Deucher 
17985ca302f7SAlex Deucher 	/* update battery/ac status */
17995ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
18005ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
18015ca302f7SAlex Deucher 	else
18025ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
18035ca302f7SAlex Deucher 
1804da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1805da321c8aSAlex Deucher 
1806da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
18078a227555SAlex Deucher 
1808da321c8aSAlex Deucher }
1809da321c8aSAlex Deucher 
radeon_pm_compute_clocks(struct radeon_device * rdev)1810da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1811da321c8aSAlex Deucher {
1812da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1813da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1814da321c8aSAlex Deucher 	else
1815da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1816da321c8aSAlex Deucher }
1817da321c8aSAlex Deucher 
radeon_pm_in_vbl(struct radeon_device * rdev)1818ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1819f735261bSDave Airlie {
182075fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1821f735261bSDave Airlie 	bool in_vbl = true;
1822f735261bSDave Airlie 
182375fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
182475fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
182575fa0b08SMario Kleiner 	 */
182675fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
182775fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
1828*5e3a0f77SWu Hoi Pok 			vbl_status = radeon_get_crtc_scanoutpos(rdev_to_drm(rdev),
18295b5561b3SMario Kleiner 								crtc,
18305b5561b3SMario Kleiner 								USE_REAL_VBLANKSTART,
18313bb403bfSVille Syrjälä 								&vpos, &hpos, NULL, NULL,
18323bb403bfSVille Syrjälä 								&rdev->mode_info.crtcs[crtc]->base.hwmode);
1833f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
18343d3cbd84SDaniel Vetter 			    !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
1835f735261bSDave Airlie 				in_vbl = false;
1836f735261bSDave Airlie 		}
1837f735261bSDave Airlie 	}
1838f81f2024SMatthew Garrett 
1839f81f2024SMatthew Garrett 	return in_vbl;
1840f81f2024SMatthew Garrett }
1841f81f2024SMatthew Garrett 
radeon_pm_debug_check_in_vbl(struct radeon_device * rdev,bool finish)1842ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1843f81f2024SMatthew Garrett {
1844f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1845f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1846f81f2024SMatthew Garrett 
1847fbd62354SWambui Karuga 	if (!in_vbl)
1848d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1849bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1850f735261bSDave Airlie 	return in_vbl;
1851f735261bSDave Airlie }
1852c913e23aSRafał Miłecki 
radeon_dynpm_idle_work_handler(struct work_struct * work)1853ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1854c913e23aSRafał Miłecki {
1855c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1856cd3a8a59SChristian König 
1857c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1858ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1859c913e23aSRafał Miłecki 
1860c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1861ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1862c913e23aSRafał Miłecki 		int not_processed = 0;
18637465280cSAlex Deucher 		int i;
1864c913e23aSRafał Miłecki 
18657465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18660ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
18670ec0612aSAlex Deucher 
18680ec0612aSAlex Deucher 			if (ring->ready) {
186947492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
18707465280cSAlex Deucher 				if (not_processed >= 3)
18717465280cSAlex Deucher 					break;
18727465280cSAlex Deucher 			}
18730ec0612aSAlex Deucher 		}
1874c913e23aSRafał Miłecki 
1875c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1876ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1877ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1878ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1879ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1880ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1881ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1882ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1883c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1884c913e23aSRafał Miłecki 			}
1885c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1886ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1887ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1888ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1889ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1890ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1891ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1892ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1893c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1894c913e23aSRafał Miłecki 			}
1895c913e23aSRafał Miłecki 		}
1896c913e23aSRafał Miłecki 
1897d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1898d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1899d7311171SAlex Deucher 		 */
1900ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
19012387e212SYu Zhe 		    time_after(jiffies, rdev->pm.dynpm_action_timeout)) {
1902ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1903ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1904c913e23aSRafał Miłecki 		}
1905c913e23aSRafał Miłecki 
190632c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1907c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1908c913e23aSRafał Miłecki 	}
19093f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
19103f53eb6fSRafael J. Wysocki }
1911c913e23aSRafał Miłecki 
19127433874eSRafał Miłecki /*
19137433874eSRafał Miłecki  * Debugfs info
19147433874eSRafał Miłecki  */
19157433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
19167433874eSRafał Miłecki 
radeon_debugfs_pm_info_show(struct seq_file * m,void * unused)19175b54d679SNirmoy Das static int radeon_debugfs_pm_info_show(struct seq_file *m, void *unused)
19187433874eSRafał Miłecki {
19196091ede9SSu Hui 	struct radeon_device *rdev = m->private;
1920*5e3a0f77SWu Hoi Pok 	struct drm_device *ddev = rdev_to_drm(rdev);
19217433874eSRafał Miłecki 
19224f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
19234f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
19244f2f2039SAlex Deucher 		seq_printf(m, "PX asic powered off\n");
19254f2f2039SAlex Deucher 	} else if (rdev->pm.dpm_enabled) {
19261316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
19271316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
19281316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
19291316b792SAlex Deucher 		else
193071375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
19311316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
19321316b792SAlex Deucher 	} else {
19339ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1934bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1935bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1936bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1937bf05d998SAlex Deucher 		else
19386234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
19399ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1940798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
19416234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
19420fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
19430fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1944798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1945aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
19461316b792SAlex Deucher 	}
19477433874eSRafał Miłecki 
19487433874eSRafał Miłecki 	return 0;
19497433874eSRafał Miłecki }
19507433874eSRafał Miłecki 
19515b54d679SNirmoy Das DEFINE_SHOW_ATTRIBUTE(radeon_debugfs_pm_info);
19527433874eSRafał Miłecki #endif
19537433874eSRafał Miłecki 
radeon_debugfs_pm_init(struct radeon_device * rdev)19545b54d679SNirmoy Das static void radeon_debugfs_pm_init(struct radeon_device *rdev)
19557433874eSRafał Miłecki {
19567433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
1957*5e3a0f77SWu Hoi Pok 	struct dentry *root = rdev_to_drm(rdev)->primary->debugfs_root;
19585b54d679SNirmoy Das 
19595b54d679SNirmoy Das 	debugfs_create_file("radeon_pm_info", 0444, root, rdev,
19605b54d679SNirmoy Das 			    &radeon_debugfs_pm_info_fops);
19615b54d679SNirmoy Das 
19627433874eSRafał Miłecki #endif
19637433874eSRafał Miłecki }
1964