xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision e0b34e3857004ac2257458d6354c31349f51e207)
17433874eSRafał Miłecki /*
27433874eSRafał Miłecki  * Permission is hereby granted, free of charge, to any person obtaining a
37433874eSRafał Miłecki  * copy of this software and associated documentation files (the "Software"),
47433874eSRafał Miłecki  * to deal in the Software without restriction, including without limitation
57433874eSRafał Miłecki  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
67433874eSRafał Miłecki  * and/or sell copies of the Software, and to permit persons to whom the
77433874eSRafał Miłecki  * Software is furnished to do so, subject to the following conditions:
87433874eSRafał Miłecki  *
97433874eSRafał Miłecki  * The above copyright notice and this permission notice shall be included in
107433874eSRafał Miłecki  * all copies or substantial portions of the Software.
117433874eSRafał Miłecki  *
127433874eSRafał Miłecki  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
137433874eSRafał Miłecki  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147433874eSRafał Miłecki  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
157433874eSRafał Miłecki  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
167433874eSRafał Miłecki  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
177433874eSRafał Miłecki  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
187433874eSRafał Miłecki  * OTHER DEALINGS IN THE SOFTWARE.
197433874eSRafał Miłecki  *
207433874eSRafał Miłecki  * Authors: Rafał Miłecki <zajec5@gmail.com>
2156278a8eSAlex Deucher  *          Alex Deucher <alexdeucher@gmail.com>
227433874eSRafał Miłecki  */
23760285e7SDavid Howells #include <drm/drmP.h>
247433874eSRafał Miłecki #include "radeon.h"
25f735261bSDave Airlie #include "avivod.h"
268a83ec5eSAlex Deucher #include "atom.h"
2799736703SOleg Chernovskiy #include "r600_dpm.h"
28ce8f5370SAlex Deucher #include <linux/power_supply.h>
2921a8122aSAlex Deucher #include <linux/hwmon.h>
3021a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
317433874eSRafał Miłecki 
32c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
33c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3473a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
35c913e23aSRafał Miłecki 
36f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
37eb2c27a0SAlex Deucher 	"",
38f712d0c7SRafał Miłecki 	"Powersave",
39f712d0c7SRafał Miłecki 	"Battery",
40f712d0c7SRafał Miłecki 	"Balanced",
41f712d0c7SRafał Miłecki 	"Performance",
42f712d0c7SRafał Miłecki };
43f712d0c7SRafał Miłecki 
44ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
45c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
46ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
47ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
48ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
49ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
50ce8f5370SAlex Deucher 
51a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
52a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
53a4c9e2eeSAlex Deucher 			     int instance)
54a4c9e2eeSAlex Deucher {
55a4c9e2eeSAlex Deucher 	int i;
56a4c9e2eeSAlex Deucher 	int found_instance = -1;
57a4c9e2eeSAlex Deucher 
58a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
59a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
60a4c9e2eeSAlex Deucher 			found_instance++;
61a4c9e2eeSAlex Deucher 			if (found_instance == instance)
62a4c9e2eeSAlex Deucher 				return i;
63a4c9e2eeSAlex Deucher 		}
64a4c9e2eeSAlex Deucher 	}
65a4c9e2eeSAlex Deucher 	/* return default if no match */
66a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
67a4c9e2eeSAlex Deucher }
68a4c9e2eeSAlex Deucher 
69c4917074SAlex Deucher void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
70ce8f5370SAlex Deucher {
711c71bda0SAlex Deucher 	if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
721c71bda0SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
731c71bda0SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
741c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = true;
751c71bda0SAlex Deucher 		else
761c71bda0SAlex Deucher 			rdev->pm.dpm.ac_power = false;
7796682956SAlex Deucher 		if (rdev->family == CHIP_ARUBA) {
781c71bda0SAlex Deucher 			if (rdev->asic->dpm.enable_bapm)
791c71bda0SAlex Deucher 				radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
8096682956SAlex Deucher 		}
811c71bda0SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
821c71bda0SAlex Deucher         } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
83ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
84ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
85ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
86ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
87ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
88ce8f5370SAlex Deucher 		}
89ce8f5370SAlex Deucher 	}
90ce8f5370SAlex Deucher }
91ce8f5370SAlex Deucher 
92ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
93ce8f5370SAlex Deucher {
94ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
95ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
96ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
97ce8f5370SAlex Deucher 		break;
98ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
99ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
100ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
101ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
102ce8f5370SAlex Deucher 			else
103ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
104ce8f5370SAlex Deucher 		} else {
105ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
106c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
107ce8f5370SAlex Deucher 			else
108c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
109ce8f5370SAlex Deucher 		}
110ce8f5370SAlex Deucher 		break;
111ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
112ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
113ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
114ce8f5370SAlex Deucher 		else
115ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
116ce8f5370SAlex Deucher 		break;
117c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
118c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
119c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
120c9e75b21SAlex Deucher 		else
121c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
122c9e75b21SAlex Deucher 		break;
123ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
124ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
125ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
126ce8f5370SAlex Deucher 		else
127ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
128ce8f5370SAlex Deucher 		break;
129ce8f5370SAlex Deucher 	}
130ce8f5370SAlex Deucher 
131ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
132ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
133ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
134ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
135ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
136ce8f5370SAlex Deucher 	} else {
137ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
138ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
139ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
140ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
141ce8f5370SAlex Deucher 	}
142ce8f5370SAlex Deucher }
143c913e23aSRafał Miłecki 
1445876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1455876dd24SMatthew Garrett {
1465876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1475876dd24SMatthew Garrett 
1485876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1495876dd24SMatthew Garrett 		return;
1505876dd24SMatthew Garrett 
1515876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1525876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1535876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1545876dd24SMatthew Garrett 	}
1555876dd24SMatthew Garrett }
1565876dd24SMatthew Garrett 
157ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
158ce8f5370SAlex Deucher {
159ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
160ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
161ce8f5370SAlex Deucher 		wait_event_timeout(
162ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
163ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
164ce8f5370SAlex Deucher 	}
165ce8f5370SAlex Deucher }
166ce8f5370SAlex Deucher 
167ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
168ce8f5370SAlex Deucher {
169ce8f5370SAlex Deucher 	u32 sclk, mclk;
17092645879SAlex Deucher 	bool misc_after = false;
171ce8f5370SAlex Deucher 
172ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
173ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
174ce8f5370SAlex Deucher 		return;
175ce8f5370SAlex Deucher 
176ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
177ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
178ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1799ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1809ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
181ce8f5370SAlex Deucher 
18227810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
18327810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1847ae764b1SAlex Deucher 		 * mclk and vddci.
18527810fb2SAlex Deucher 		 */
18627810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
18727810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
18827810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
18927810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
19027810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
19127810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
19227810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
19327810fb2SAlex Deucher 		else
194ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
195ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
19627810fb2SAlex Deucher 
1979ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1989ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
199ce8f5370SAlex Deucher 
20092645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
20192645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
20292645879SAlex Deucher 			misc_after = true;
20392645879SAlex Deucher 
20492645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
20592645879SAlex Deucher 
20692645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
20792645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
20892645879SAlex Deucher 				return;
20992645879SAlex Deucher 		}
21092645879SAlex Deucher 
21192645879SAlex Deucher 		radeon_pm_prepare(rdev);
21292645879SAlex Deucher 
21392645879SAlex Deucher 		if (!misc_after)
214ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
215ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
216ce8f5370SAlex Deucher 
217ce8f5370SAlex Deucher 		/* set engine clock */
218ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
219ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
220ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
221ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
222ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
223d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
224ce8f5370SAlex Deucher 		}
225ce8f5370SAlex Deucher 
226ce8f5370SAlex Deucher 		/* set memory clock */
227798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
228ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
229ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
230ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
231ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
232d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
233ce8f5370SAlex Deucher 		}
23492645879SAlex Deucher 
23592645879SAlex Deucher 		if (misc_after)
23692645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
23792645879SAlex Deucher 			radeon_pm_misc(rdev);
23892645879SAlex Deucher 
239ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
240ce8f5370SAlex Deucher 
241ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
242ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
243ce8f5370SAlex Deucher 	} else
244d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
245ce8f5370SAlex Deucher }
246ce8f5370SAlex Deucher 
247ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
248a424816fSAlex Deucher {
2495f8f635eSJerome Glisse 	int i, r;
2502aba631cSMatthew Garrett 
2514e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2524e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2534e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2544e186b2dSAlex Deucher 		return;
2554e186b2dSAlex Deucher 
256db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
257d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2584f3218cbSAlex Deucher 
25995f5a3acSAlex Deucher 	/* wait for the rings to drain */
26095f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
26195f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2625f8f635eSJerome Glisse 		if (!ring->ready) {
2635f8f635eSJerome Glisse 			continue;
2645f8f635eSJerome Glisse 		}
26537615527SChristian König 		r = radeon_fence_wait_empty(rdev, i);
2665f8f635eSJerome Glisse 		if (r) {
2675f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2685f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2695f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2705f8f635eSJerome Glisse 			return;
2715f8f635eSJerome Glisse 		}
272ce8f5370SAlex Deucher 	}
27395f5a3acSAlex Deucher 
2745876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2755876dd24SMatthew Garrett 
276ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2772aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2782aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
279*e0b34e38SMario Kleiner 				/* This can fail if a modeset is in progress */
280*e0b34e38SMario Kleiner 				if (drm_vblank_get(rdev->ddev, i) == 0)
2812aba631cSMatthew Garrett 					rdev->pm.req_vblank |= (1 << i);
282*e0b34e38SMario Kleiner 				else
283*e0b34e38SMario Kleiner 					DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
284*e0b34e38SMario Kleiner 							 i);
2852aba631cSMatthew Garrett 			}
2862aba631cSMatthew Garrett 		}
2872aba631cSMatthew Garrett 	}
2882aba631cSMatthew Garrett 
289ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2902aba631cSMatthew Garrett 
291ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2922aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2932aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2942aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2952aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2962aba631cSMatthew Garrett 			}
2972aba631cSMatthew Garrett 		}
2982aba631cSMatthew Garrett 	}
299a424816fSAlex Deucher 
300a424816fSAlex Deucher 	/* update display watermarks based on new power state */
301a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
302a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
303a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
304a424816fSAlex Deucher 
305ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3062aba631cSMatthew Garrett 
307d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
308db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
309a424816fSAlex Deucher }
310a424816fSAlex Deucher 
311f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
312f712d0c7SRafał Miłecki {
313f712d0c7SRafał Miłecki 	int i, j;
314f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
315f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
316f712d0c7SRafał Miłecki 
317d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
318f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
319f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
320d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
321f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
322f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
323d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
324f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
325d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
326f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
327d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
328d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
329f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
330f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
331f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
332eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
333f712d0c7SRafał Miłecki 						 j,
334eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
335f712d0c7SRafał Miłecki 			else
336eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
337f712d0c7SRafał Miłecki 						 j,
338f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
339f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
340eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
341f712d0c7SRafał Miłecki 		}
342f712d0c7SRafał Miłecki 	}
343f712d0c7SRafał Miłecki }
344f712d0c7SRafał Miłecki 
345ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
346a424816fSAlex Deucher 				     struct device_attribute *attr,
347a424816fSAlex Deucher 				     char *buf)
348a424816fSAlex Deucher {
3493e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
350a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
351ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
352a424816fSAlex Deucher 
353a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
354ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
355ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
35612e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
357ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
358a424816fSAlex Deucher }
359a424816fSAlex Deucher 
360ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
361a424816fSAlex Deucher 				     struct device_attribute *attr,
362a424816fSAlex Deucher 				     const char *buf,
363a424816fSAlex Deucher 				     size_t count)
364a424816fSAlex Deucher {
3653e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
366a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
367a424816fSAlex Deucher 
3684f2f2039SAlex Deucher 	/* Can't set profile when the card is off */
3694f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
3704f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
3714f2f2039SAlex Deucher 		return -EINVAL;
3724f2f2039SAlex Deucher 
373a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
374ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
375ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
376ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
377ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
378ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
379ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
380ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
381c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
382c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
383ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
384ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
385ce8f5370SAlex Deucher 		else {
3861783e4bfSThomas Renninger 			count = -EINVAL;
387ce8f5370SAlex Deucher 			goto fail;
388ce8f5370SAlex Deucher 		}
389ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
390ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3911783e4bfSThomas Renninger 	} else
3921783e4bfSThomas Renninger 		count = -EINVAL;
3931783e4bfSThomas Renninger 
394ce8f5370SAlex Deucher fail:
395a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
396a424816fSAlex Deucher 
397a424816fSAlex Deucher 	return count;
398a424816fSAlex Deucher }
399a424816fSAlex Deucher 
400ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
401ce8f5370SAlex Deucher 				    struct device_attribute *attr,
402ce8f5370SAlex Deucher 				    char *buf)
40356278a8eSAlex Deucher {
4043e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
405ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
406ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
40756278a8eSAlex Deucher 
408ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
409da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
410da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
41156278a8eSAlex Deucher }
41256278a8eSAlex Deucher 
413ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
414ce8f5370SAlex Deucher 				    struct device_attribute *attr,
415ce8f5370SAlex Deucher 				    const char *buf,
416ce8f5370SAlex Deucher 				    size_t count)
417d0d6cb81SRafał Miłecki {
4183e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
419ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
420ce8f5370SAlex Deucher 
4214f2f2039SAlex Deucher 	/* Can't set method when the card is off */
4224f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
4234f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
4244f2f2039SAlex Deucher 		count = -EINVAL;
4254f2f2039SAlex Deucher 		goto fail;
4264f2f2039SAlex Deucher 	}
4274f2f2039SAlex Deucher 
428da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
429da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
430da321c8aSAlex Deucher 		count = -EINVAL;
431da321c8aSAlex Deucher 		goto fail;
432da321c8aSAlex Deucher 	}
433ce8f5370SAlex Deucher 
434ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
435ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
436ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
437ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
438ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
439ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
440ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
441ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
442ce8f5370SAlex Deucher 		/* disable dynpm */
443ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
444ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4453f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
446ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
44732c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
448ce8f5370SAlex Deucher 	} else {
4491783e4bfSThomas Renninger 		count = -EINVAL;
450ce8f5370SAlex Deucher 		goto fail;
451d0d6cb81SRafał Miłecki 	}
452ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
453ce8f5370SAlex Deucher fail:
454ce8f5370SAlex Deucher 	return count;
455ce8f5370SAlex Deucher }
456ce8f5370SAlex Deucher 
457da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
458da321c8aSAlex Deucher 				    struct device_attribute *attr,
459da321c8aSAlex Deucher 				    char *buf)
460da321c8aSAlex Deucher {
4613e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
462da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
463da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
464da321c8aSAlex Deucher 
465da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
466da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
467da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
468da321c8aSAlex Deucher }
469da321c8aSAlex Deucher 
470da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
471da321c8aSAlex Deucher 				    struct device_attribute *attr,
472da321c8aSAlex Deucher 				    const char *buf,
473da321c8aSAlex Deucher 				    size_t count)
474da321c8aSAlex Deucher {
4753e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
476da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
477da321c8aSAlex Deucher 
478da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
479da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
480da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
481da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
482da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
483da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
484da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
485da321c8aSAlex Deucher 	else {
486da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
487da321c8aSAlex Deucher 		count = -EINVAL;
488da321c8aSAlex Deucher 		goto fail;
489da321c8aSAlex Deucher 	}
490da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
491b07a657eSPali Rohár 
492b07a657eSPali Rohár 	/* Can't set dpm state when the card is off */
493b07a657eSPali Rohár 	if (!(rdev->flags & RADEON_IS_PX) ||
494b07a657eSPali Rohár 	    (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
495da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
496b07a657eSPali Rohár 
497da321c8aSAlex Deucher fail:
498da321c8aSAlex Deucher 	return count;
499da321c8aSAlex Deucher }
500da321c8aSAlex Deucher 
50170d01a5eSAlex Deucher static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
50270d01a5eSAlex Deucher 						       struct device_attribute *attr,
50370d01a5eSAlex Deucher 						       char *buf)
50470d01a5eSAlex Deucher {
5053e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
50670d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
50770d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
50870d01a5eSAlex Deucher 
5094f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5104f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5114f2f2039SAlex Deucher 		return snprintf(buf, PAGE_SIZE, "off\n");
5124f2f2039SAlex Deucher 
51370d01a5eSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
51470d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
51570d01a5eSAlex Deucher 			(level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
51670d01a5eSAlex Deucher }
51770d01a5eSAlex Deucher 
51870d01a5eSAlex Deucher static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
51970d01a5eSAlex Deucher 						       struct device_attribute *attr,
52070d01a5eSAlex Deucher 						       const char *buf,
52170d01a5eSAlex Deucher 						       size_t count)
52270d01a5eSAlex Deucher {
5233e4e2129SJean Delvare 	struct drm_device *ddev = dev_get_drvdata(dev);
52470d01a5eSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
52570d01a5eSAlex Deucher 	enum radeon_dpm_forced_level level;
52670d01a5eSAlex Deucher 	int ret = 0;
52770d01a5eSAlex Deucher 
5284f2f2039SAlex Deucher 	/* Can't force performance level when the card is off */
5294f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
5304f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
5314f2f2039SAlex Deucher 		return -EINVAL;
5324f2f2039SAlex Deucher 
53370d01a5eSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
53470d01a5eSAlex Deucher 	if (strncmp("low", buf, strlen("low")) == 0) {
53570d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_LOW;
53670d01a5eSAlex Deucher 	} else if (strncmp("high", buf, strlen("high")) == 0) {
53770d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_HIGH;
53870d01a5eSAlex Deucher 	} else if (strncmp("auto", buf, strlen("auto")) == 0) {
53970d01a5eSAlex Deucher 		level = RADEON_DPM_FORCED_LEVEL_AUTO;
54070d01a5eSAlex Deucher 	} else {
54170d01a5eSAlex Deucher 		count = -EINVAL;
54270d01a5eSAlex Deucher 		goto fail;
54370d01a5eSAlex Deucher 	}
54470d01a5eSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
5450a17af37SAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
5460a17af37SAlex Deucher 			count = -EINVAL;
5470a17af37SAlex Deucher 			goto fail;
5480a17af37SAlex Deucher 		}
54970d01a5eSAlex Deucher 		ret = radeon_dpm_force_performance_level(rdev, level);
55070d01a5eSAlex Deucher 		if (ret)
55170d01a5eSAlex Deucher 			count = -EINVAL;
55270d01a5eSAlex Deucher 	}
55370d01a5eSAlex Deucher fail:
5540a17af37SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
5550a17af37SAlex Deucher 
55670d01a5eSAlex Deucher 	return count;
55770d01a5eSAlex Deucher }
55870d01a5eSAlex Deucher 
55999736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
56099736703SOleg Chernovskiy 					    struct device_attribute *attr,
56199736703SOleg Chernovskiy 					    char *buf)
56299736703SOleg Chernovskiy {
56399736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
56499736703SOleg Chernovskiy 	u32 pwm_mode = 0;
56599736703SOleg Chernovskiy 
56699736703SOleg Chernovskiy 	if (rdev->asic->dpm.fan_ctrl_get_mode)
56799736703SOleg Chernovskiy 		pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
56899736703SOleg Chernovskiy 
56999736703SOleg Chernovskiy 	/* never 0 (full-speed), fuse or smc-controlled always */
57099736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
57199736703SOleg Chernovskiy }
57299736703SOleg Chernovskiy 
57399736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
57499736703SOleg Chernovskiy 					    struct device_attribute *attr,
57599736703SOleg Chernovskiy 					    const char *buf,
57699736703SOleg Chernovskiy 					    size_t count)
57799736703SOleg Chernovskiy {
57899736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
57999736703SOleg Chernovskiy 	int err;
58099736703SOleg Chernovskiy 	int value;
58199736703SOleg Chernovskiy 
58299736703SOleg Chernovskiy 	if(!rdev->asic->dpm.fan_ctrl_set_mode)
58399736703SOleg Chernovskiy 		return -EINVAL;
58499736703SOleg Chernovskiy 
58599736703SOleg Chernovskiy 	err = kstrtoint(buf, 10, &value);
58699736703SOleg Chernovskiy 	if (err)
58799736703SOleg Chernovskiy 		return err;
58899736703SOleg Chernovskiy 
58999736703SOleg Chernovskiy 	switch (value) {
59099736703SOleg Chernovskiy 	case 1: /* manual, percent-based */
59199736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
59299736703SOleg Chernovskiy 		break;
59399736703SOleg Chernovskiy 	default: /* disable */
59499736703SOleg Chernovskiy 		rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
59599736703SOleg Chernovskiy 		break;
59699736703SOleg Chernovskiy 	}
59799736703SOleg Chernovskiy 
59899736703SOleg Chernovskiy 	return count;
59999736703SOleg Chernovskiy }
60099736703SOleg Chernovskiy 
60199736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
60299736703SOleg Chernovskiy 					 struct device_attribute *attr,
60399736703SOleg Chernovskiy 					 char *buf)
60499736703SOleg Chernovskiy {
60599736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", 0);
60699736703SOleg Chernovskiy }
60799736703SOleg Chernovskiy 
60899736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
60999736703SOleg Chernovskiy 					 struct device_attribute *attr,
61099736703SOleg Chernovskiy 					 char *buf)
61199736703SOleg Chernovskiy {
612082452e1SAlex Deucher 	return sprintf(buf, "%i\n", 255);
61399736703SOleg Chernovskiy }
61499736703SOleg Chernovskiy 
61599736703SOleg Chernovskiy static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
61699736703SOleg Chernovskiy 				     struct device_attribute *attr,
61799736703SOleg Chernovskiy 				     const char *buf, size_t count)
61899736703SOleg Chernovskiy {
61999736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
62099736703SOleg Chernovskiy 	int err;
62199736703SOleg Chernovskiy 	u32 value;
62299736703SOleg Chernovskiy 
62399736703SOleg Chernovskiy 	err = kstrtou32(buf, 10, &value);
62499736703SOleg Chernovskiy 	if (err)
62599736703SOleg Chernovskiy 		return err;
62699736703SOleg Chernovskiy 
627082452e1SAlex Deucher 	value = (value * 100) / 255;
628082452e1SAlex Deucher 
62999736703SOleg Chernovskiy 	err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
63099736703SOleg Chernovskiy 	if (err)
63199736703SOleg Chernovskiy 		return err;
63299736703SOleg Chernovskiy 
63399736703SOleg Chernovskiy 	return count;
63499736703SOleg Chernovskiy }
63599736703SOleg Chernovskiy 
63699736703SOleg Chernovskiy static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
63799736703SOleg Chernovskiy 				     struct device_attribute *attr,
63899736703SOleg Chernovskiy 				     char *buf)
63999736703SOleg Chernovskiy {
64099736703SOleg Chernovskiy 	struct radeon_device *rdev = dev_get_drvdata(dev);
64199736703SOleg Chernovskiy 	int err;
64299736703SOleg Chernovskiy 	u32 speed;
64399736703SOleg Chernovskiy 
64499736703SOleg Chernovskiy 	err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
64599736703SOleg Chernovskiy 	if (err)
64699736703SOleg Chernovskiy 		return err;
64799736703SOleg Chernovskiy 
648082452e1SAlex Deucher 	speed = (speed * 255) / 100;
649082452e1SAlex Deucher 
65099736703SOleg Chernovskiy 	return sprintf(buf, "%i\n", speed);
65199736703SOleg Chernovskiy }
65299736703SOleg Chernovskiy 
653ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
654ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
655da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
65670d01a5eSAlex Deucher static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
65770d01a5eSAlex Deucher 		   radeon_get_dpm_forced_performance_level,
65870d01a5eSAlex Deucher 		   radeon_set_dpm_forced_performance_level);
659ce8f5370SAlex Deucher 
66021a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
66121a8122aSAlex Deucher 				      struct device_attribute *attr,
66221a8122aSAlex Deucher 				      char *buf)
66321a8122aSAlex Deucher {
664ec39f64bSGuenter Roeck 	struct radeon_device *rdev = dev_get_drvdata(dev);
6654f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
66620d391d7SAlex Deucher 	int temp;
66721a8122aSAlex Deucher 
6684f2f2039SAlex Deucher 	/* Can't get temperature when the card is off */
6694f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
6704f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
6714f2f2039SAlex Deucher 		return -EINVAL;
6724f2f2039SAlex Deucher 
6736bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
6746bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
6756bd1c385SAlex Deucher 	else
67621a8122aSAlex Deucher 		temp = 0;
67721a8122aSAlex Deucher 
67821a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
67921a8122aSAlex Deucher }
68021a8122aSAlex Deucher 
6816ea4e84dSJean Delvare static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
6826ea4e84dSJean Delvare 					     struct device_attribute *attr,
6836ea4e84dSJean Delvare 					     char *buf)
6846ea4e84dSJean Delvare {
685e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
6866ea4e84dSJean Delvare 	int hyst = to_sensor_dev_attr(attr)->index;
6876ea4e84dSJean Delvare 	int temp;
6886ea4e84dSJean Delvare 
6896ea4e84dSJean Delvare 	if (hyst)
6906ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.min_temp;
6916ea4e84dSJean Delvare 	else
6926ea4e84dSJean Delvare 		temp = rdev->pm.dpm.thermal.max_temp;
6936ea4e84dSJean Delvare 
6946ea4e84dSJean Delvare 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
6956ea4e84dSJean Delvare }
6966ea4e84dSJean Delvare 
69721a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
6986ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
6996ea4e84dSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
70099736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
70199736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
70299736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
70399736703SOleg Chernovskiy static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
70499736703SOleg Chernovskiy 
70521a8122aSAlex Deucher 
70621a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
70721a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
7086ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
7096ea4e84dSJean Delvare 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
71099736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1.dev_attr.attr,
71199736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_enable.dev_attr.attr,
71299736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_min.dev_attr.attr,
71399736703SOleg Chernovskiy 	&sensor_dev_attr_pwm1_max.dev_attr.attr,
71421a8122aSAlex Deucher 	NULL
71521a8122aSAlex Deucher };
71621a8122aSAlex Deucher 
7176ea4e84dSJean Delvare static umode_t hwmon_attributes_visible(struct kobject *kobj,
7186ea4e84dSJean Delvare 					struct attribute *attr, int index)
7196ea4e84dSJean Delvare {
720e3837b00SGeliang Tang 	struct device *dev = kobj_to_dev(kobj);
721e4158f1bSSergey Senozhatsky 	struct radeon_device *rdev = dev_get_drvdata(dev);
72299736703SOleg Chernovskiy 	umode_t effective_mode = attr->mode;
7236ea4e84dSJean Delvare 
7242a7d44f4SAlex Deucher 	/* Skip attributes if DPM is not enabled */
7256ea4e84dSJean Delvare 	if (rdev->pm.pm_method != PM_METHOD_DPM &&
7266ea4e84dSJean Delvare 	    (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
7272a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
7282a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
7292a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
7302a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
7312a7d44f4SAlex Deucher 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
7326ea4e84dSJean Delvare 		return 0;
7336ea4e84dSJean Delvare 
73499736703SOleg Chernovskiy 	/* Skip fan attributes if fan is not present */
73599736703SOleg Chernovskiy 	if (rdev->pm.no_fan &&
73699736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
73799736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
73899736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
73999736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
74099736703SOleg Chernovskiy 		return 0;
74199736703SOleg Chernovskiy 
74299736703SOleg Chernovskiy 	/* mask fan attributes if we have no bindings for this asic to expose */
74399736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.get_fan_speed_percent &&
74499736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
74599736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_get_mode &&
74699736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
74799736703SOleg Chernovskiy 		effective_mode &= ~S_IRUGO;
74899736703SOleg Chernovskiy 
74999736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
75099736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
75199736703SOleg Chernovskiy 	    (!rdev->asic->dpm.fan_ctrl_set_mode &&
75299736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
75399736703SOleg Chernovskiy 		effective_mode &= ~S_IWUSR;
75499736703SOleg Chernovskiy 
75599736703SOleg Chernovskiy 	/* hide max/min values if we can't both query and manage the fan */
75699736703SOleg Chernovskiy 	if ((!rdev->asic->dpm.set_fan_speed_percent &&
75799736703SOleg Chernovskiy 	     !rdev->asic->dpm.get_fan_speed_percent) &&
75899736703SOleg Chernovskiy 	    (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
75999736703SOleg Chernovskiy 	     attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
76099736703SOleg Chernovskiy 		return 0;
76199736703SOleg Chernovskiy 
76299736703SOleg Chernovskiy 	return effective_mode;
7636ea4e84dSJean Delvare }
7646ea4e84dSJean Delvare 
76521a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
76621a8122aSAlex Deucher 	.attrs = hwmon_attributes,
7676ea4e84dSJean Delvare 	.is_visible = hwmon_attributes_visible,
76821a8122aSAlex Deucher };
76921a8122aSAlex Deucher 
770ec39f64bSGuenter Roeck static const struct attribute_group *hwmon_groups[] = {
771ec39f64bSGuenter Roeck 	&hwmon_attrgroup,
772ec39f64bSGuenter Roeck 	NULL
773ec39f64bSGuenter Roeck };
774ec39f64bSGuenter Roeck 
7750d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
77621a8122aSAlex Deucher {
7770d18abedSDan Carpenter 	int err = 0;
77821a8122aSAlex Deucher 
77921a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
78021a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
78121a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
78221a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
783457558edSAlex Deucher 	case THERMAL_TYPE_NI:
784e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
7851bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
786286d9cc6SAlex Deucher 	case THERMAL_TYPE_CI:
787286d9cc6SAlex Deucher 	case THERMAL_TYPE_KV:
7886bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
7895d7486c7SAlex Deucher 			return err;
790cb3e4e7cSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
791ec39f64bSGuenter Roeck 									   "radeon", rdev,
792ec39f64bSGuenter Roeck 									   hwmon_groups);
793cb3e4e7cSAlex Deucher 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
794cb3e4e7cSAlex Deucher 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
7950d18abedSDan Carpenter 			dev_err(rdev->dev,
7960d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
7970d18abedSDan Carpenter 		}
79821a8122aSAlex Deucher 		break;
79921a8122aSAlex Deucher 	default:
80021a8122aSAlex Deucher 		break;
80121a8122aSAlex Deucher 	}
8020d18abedSDan Carpenter 
8030d18abedSDan Carpenter 	return err;
80421a8122aSAlex Deucher }
80521a8122aSAlex Deucher 
806cb3e4e7cSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
807cb3e4e7cSAlex Deucher {
808cb3e4e7cSAlex Deucher 	if (rdev->pm.int_hwmon_dev)
809cb3e4e7cSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
810cb3e4e7cSAlex Deucher }
811cb3e4e7cSAlex Deucher 
812da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
813da321c8aSAlex Deucher {
814da321c8aSAlex Deucher 	struct radeon_device *rdev =
815da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
816da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
817da321c8aSAlex Deucher 	/* switch to the thermal state */
818da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
819da321c8aSAlex Deucher 
820da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
821da321c8aSAlex Deucher 		return;
822da321c8aSAlex Deucher 
823da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
824da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
825da321c8aSAlex Deucher 
826da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
827da321c8aSAlex Deucher 			/* switch back the user state */
828da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
829da321c8aSAlex Deucher 	} else {
830da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
831da321c8aSAlex Deucher 			/* switch back the user state */
832da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
833da321c8aSAlex Deucher 	}
83460320347SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
83560320347SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
83660320347SAlex Deucher 		rdev->pm.dpm.thermal_active = true;
83760320347SAlex Deucher 	else
83860320347SAlex Deucher 		rdev->pm.dpm.thermal_active = false;
83960320347SAlex Deucher 	rdev->pm.dpm.state = dpm_state;
84060320347SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
84160320347SAlex Deucher 
84260320347SAlex Deucher 	radeon_pm_compute_clocks(rdev);
843da321c8aSAlex Deucher }
844da321c8aSAlex Deucher 
8453899ca84SAlex Deucher static bool radeon_dpm_single_display(struct radeon_device *rdev)
846da321c8aSAlex Deucher {
84748783069SAlex Deucher 	bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
84848783069SAlex Deucher 		true : false;
84948783069SAlex Deucher 
85048783069SAlex Deucher 	/* check if the vblank period is too short to adjust the mclk */
85148783069SAlex Deucher 	if (single_display && rdev->asic->dpm.vblank_too_short) {
85248783069SAlex Deucher 		if (radeon_dpm_vblank_too_short(rdev))
85348783069SAlex Deucher 			single_display = false;
85448783069SAlex Deucher 	}
855da321c8aSAlex Deucher 
856951caa6aSAlex Deucher 	/* 120hz tends to be problematic even if they are under the
857951caa6aSAlex Deucher 	 * vblank limit.
858951caa6aSAlex Deucher 	 */
859951caa6aSAlex Deucher 	if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
860951caa6aSAlex Deucher 		single_display = false;
861951caa6aSAlex Deucher 
8623899ca84SAlex Deucher 	return single_display;
8633899ca84SAlex Deucher }
8643899ca84SAlex Deucher 
8653899ca84SAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
8663899ca84SAlex Deucher 						     enum radeon_pm_state_type dpm_state)
8673899ca84SAlex Deucher {
8683899ca84SAlex Deucher 	int i;
8693899ca84SAlex Deucher 	struct radeon_ps *ps;
8703899ca84SAlex Deucher 	u32 ui_class;
8713899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
8723899ca84SAlex Deucher 
873edcaa5b1SAlex Deucher 	/* certain older asics have a separare 3D performance state,
874edcaa5b1SAlex Deucher 	 * so try that first if the user selected performance
875edcaa5b1SAlex Deucher 	 */
876edcaa5b1SAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
877edcaa5b1SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
878da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
879da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
880da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
881da321c8aSAlex Deucher 
882edcaa5b1SAlex Deucher restart_search:
883da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
884da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
885da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
886da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
887da321c8aSAlex Deucher 		switch (dpm_state) {
888da321c8aSAlex Deucher 		/* user states */
889da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
890da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
891da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
89248783069SAlex Deucher 					if (single_display)
893da321c8aSAlex Deucher 						return ps;
894da321c8aSAlex Deucher 				} else
895da321c8aSAlex Deucher 					return ps;
896da321c8aSAlex Deucher 			}
897da321c8aSAlex Deucher 			break;
898da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
899da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
900da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
90148783069SAlex Deucher 					if (single_display)
902da321c8aSAlex Deucher 						return ps;
903da321c8aSAlex Deucher 				} else
904da321c8aSAlex Deucher 					return ps;
905da321c8aSAlex Deucher 			}
906da321c8aSAlex Deucher 			break;
907da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
908da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
909da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
91048783069SAlex Deucher 					if (single_display)
911da321c8aSAlex Deucher 						return ps;
912da321c8aSAlex Deucher 				} else
913da321c8aSAlex Deucher 					return ps;
914da321c8aSAlex Deucher 			}
915da321c8aSAlex Deucher 			break;
916da321c8aSAlex Deucher 		/* internal states */
917da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
918d4d3278cSAlex Deucher 			if (rdev->pm.dpm.uvd_ps)
919da321c8aSAlex Deucher 				return rdev->pm.dpm.uvd_ps;
920d4d3278cSAlex Deucher 			else
921d4d3278cSAlex Deucher 				break;
922da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
923da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
924da321c8aSAlex Deucher 				return ps;
925da321c8aSAlex Deucher 			break;
926da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
927da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
928da321c8aSAlex Deucher 				return ps;
929da321c8aSAlex Deucher 			break;
930da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
931da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
932da321c8aSAlex Deucher 				return ps;
933da321c8aSAlex Deucher 			break;
934da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
935da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
936da321c8aSAlex Deucher 				return ps;
937da321c8aSAlex Deucher 			break;
938da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
939da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
940da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
941da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
942da321c8aSAlex Deucher 				return ps;
943da321c8aSAlex Deucher 			break;
944da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
945da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
946da321c8aSAlex Deucher 				return ps;
947da321c8aSAlex Deucher 			break;
948da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
949da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
950da321c8aSAlex Deucher 				return ps;
951da321c8aSAlex Deucher 			break;
952edcaa5b1SAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_3DPERF:
953edcaa5b1SAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
954edcaa5b1SAlex Deucher 				return ps;
955edcaa5b1SAlex Deucher 			break;
956da321c8aSAlex Deucher 		default:
957da321c8aSAlex Deucher 			break;
958da321c8aSAlex Deucher 		}
959da321c8aSAlex Deucher 	}
960da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
961da321c8aSAlex Deucher 	switch (dpm_state) {
962da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
963ce3537d5SAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
964ce3537d5SAlex Deucher 		goto restart_search;
965da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
966da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
967da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
968d4d3278cSAlex Deucher 		if (rdev->pm.dpm.uvd_ps) {
969da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
970d4d3278cSAlex Deucher 		} else {
971d4d3278cSAlex Deucher 			dpm_state = POWER_STATE_TYPE_PERFORMANCE;
972d4d3278cSAlex Deucher 			goto restart_search;
973d4d3278cSAlex Deucher 		}
974da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
975da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
976da321c8aSAlex Deucher 		goto restart_search;
977da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
978da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
979da321c8aSAlex Deucher 		goto restart_search;
980da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
981edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_BALANCED:
982edcaa5b1SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_3DPERF:
983da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
984da321c8aSAlex Deucher 		goto restart_search;
985da321c8aSAlex Deucher 	default:
986da321c8aSAlex Deucher 		break;
987da321c8aSAlex Deucher 	}
988da321c8aSAlex Deucher 
989da321c8aSAlex Deucher 	return NULL;
990da321c8aSAlex Deucher }
991da321c8aSAlex Deucher 
992da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
993da321c8aSAlex Deucher {
994da321c8aSAlex Deucher 	int i;
995da321c8aSAlex Deucher 	struct radeon_ps *ps;
996da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
99784dd1928SAlex Deucher 	int ret;
9983899ca84SAlex Deucher 	bool single_display = radeon_dpm_single_display(rdev);
999da321c8aSAlex Deucher 
1000da321c8aSAlex Deucher 	/* if dpm init failed */
1001da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
1002da321c8aSAlex Deucher 		return;
1003da321c8aSAlex Deucher 
1004da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1005da321c8aSAlex Deucher 		/* add other state override checks here */
10068a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
10078a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
1008da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1009da321c8aSAlex Deucher 	}
1010da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
1011da321c8aSAlex Deucher 
1012da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1013da321c8aSAlex Deucher 	if (ps)
101489c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
1015da321c8aSAlex Deucher 	else
1016da321c8aSAlex Deucher 		return;
1017da321c8aSAlex Deucher 
1018d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
1019da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
1020b62d628bSAlex Deucher 		/* vce just modifies an existing state so force a change */
1021b62d628bSAlex Deucher 		if (ps->vce_active != rdev->pm.dpm.vce_active)
1022b62d628bSAlex Deucher 			goto force;
10233899ca84SAlex Deucher 		/* user has made a display change (such as timing) */
10243899ca84SAlex Deucher 		if (rdev->pm.dpm.single_display != single_display)
10253899ca84SAlex Deucher 			goto force;
1026d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1027d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
1028d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
1029d22b7e40SAlex Deucher 			 */
1030da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1031d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
1032da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
1033da321c8aSAlex Deucher 				/* update displays */
1034da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
1035da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1036da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1037da321c8aSAlex Deucher 			}
1038da321c8aSAlex Deucher 			return;
1039d22b7e40SAlex Deucher 		} else {
1040d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
1041d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
1042d22b7e40SAlex Deucher 			 * update display configuration.
1043d22b7e40SAlex Deucher 			 */
1044d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
1045d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
1046d22b7e40SAlex Deucher 				return;
1047d22b7e40SAlex Deucher 			} else {
1048d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1049d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
1050d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
1051d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
1052d22b7e40SAlex Deucher 					/* update displays */
1053d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
1054d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1055d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1056d22b7e40SAlex Deucher 					return;
1057d22b7e40SAlex Deucher 				}
1058d22b7e40SAlex Deucher 			}
1059d22b7e40SAlex Deucher 		}
1060da321c8aSAlex Deucher 	}
1061da321c8aSAlex Deucher 
1062b62d628bSAlex Deucher force:
1063033a37dfSAlex Deucher 	if (radeon_dpm == 1) {
1064da321c8aSAlex Deucher 		printk("switching from power state:\n");
1065da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1066da321c8aSAlex Deucher 		printk("switching to power state:\n");
1067da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1068033a37dfSAlex Deucher 	}
1069b62d628bSAlex Deucher 
1070da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
1071da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
1072da321c8aSAlex Deucher 
1073b62d628bSAlex Deucher 	/* update whether vce is active */
1074b62d628bSAlex Deucher 	ps->vce_active = rdev->pm.dpm.vce_active;
1075b62d628bSAlex Deucher 
107684dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
107784dd1928SAlex Deucher 	if (ret)
107884dd1928SAlex Deucher 		goto done;
107984dd1928SAlex Deucher 
1080da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
1081da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
1082da321c8aSAlex Deucher 	/* update displays */
1083da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
1084da321c8aSAlex Deucher 
1085da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1086da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
10873899ca84SAlex Deucher 	rdev->pm.dpm.single_display = single_display;
1088da321c8aSAlex Deucher 
1089da321c8aSAlex Deucher 	/* wait for the rings to drain */
1090da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
1091da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
1092da321c8aSAlex Deucher 		if (ring->ready)
109337615527SChristian König 			radeon_fence_wait_empty(rdev, i);
1094da321c8aSAlex Deucher 	}
1095da321c8aSAlex Deucher 
1096da321c8aSAlex Deucher 	/* program the new power state */
1097da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
1098da321c8aSAlex Deucher 
1099da321c8aSAlex Deucher 	/* update current power state */
1100da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1101da321c8aSAlex Deucher 
110284dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
110384dd1928SAlex Deucher 
11041cd8b21aSAlex Deucher 	if (rdev->asic->dpm.force_performance_level) {
110514ac88afSAlex Deucher 		if (rdev->pm.dpm.thermal_active) {
110614ac88afSAlex Deucher 			enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
110760320347SAlex Deucher 			/* force low perf level for thermal */
110860320347SAlex Deucher 			radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
110914ac88afSAlex Deucher 			/* save the user's level */
111014ac88afSAlex Deucher 			rdev->pm.dpm.forced_level = level;
111114ac88afSAlex Deucher 		} else {
111214ac88afSAlex Deucher 			/* otherwise, user selected level */
111314ac88afSAlex Deucher 			radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
111414ac88afSAlex Deucher 		}
111560320347SAlex Deucher 	}
111660320347SAlex Deucher 
111784dd1928SAlex Deucher done:
1118da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
1119da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
1120da321c8aSAlex Deucher }
1121da321c8aSAlex Deucher 
1122ce3537d5SAlex Deucher void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1123ce3537d5SAlex Deucher {
1124ce3537d5SAlex Deucher 	enum radeon_pm_state_type dpm_state;
1125ce3537d5SAlex Deucher 
11269e9d9762SAlex Deucher 	if (rdev->asic->dpm.powergate_uvd) {
11279e9d9762SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
11288158eb9eSChristian König 		/* don't powergate anything if we
11298158eb9eSChristian König 		   have active but pause streams */
11308158eb9eSChristian König 		enable |= rdev->pm.dpm.sd > 0;
11318158eb9eSChristian König 		enable |= rdev->pm.dpm.hd > 0;
11329e9d9762SAlex Deucher 		/* enable/disable UVD */
11339e9d9762SAlex Deucher 		radeon_dpm_powergate_uvd(rdev, !enable);
11349e9d9762SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
11359e9d9762SAlex Deucher 	} else {
1136ce3537d5SAlex Deucher 		if (enable) {
1137ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1138ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = true;
11390690a229SAlex Deucher 			/* disable this for now */
11400690a229SAlex Deucher #if 0
1141ce3537d5SAlex Deucher 			if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1142ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1143ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1144ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1145ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1146ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1147ce3537d5SAlex Deucher 			else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1148ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1149ce3537d5SAlex Deucher 			else
11500690a229SAlex Deucher #endif
1151ce3537d5SAlex Deucher 				dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1152ce3537d5SAlex Deucher 			rdev->pm.dpm.state = dpm_state;
1153ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1154ce3537d5SAlex Deucher 		} else {
1155ce3537d5SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1156ce3537d5SAlex Deucher 			rdev->pm.dpm.uvd_active = false;
1157ce3537d5SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
1158ce3537d5SAlex Deucher 		}
1159ce3537d5SAlex Deucher 
1160ce3537d5SAlex Deucher 		radeon_pm_compute_clocks(rdev);
1161ce3537d5SAlex Deucher 	}
11629e9d9762SAlex Deucher }
1163ce3537d5SAlex Deucher 
116403afe6f6SAlex Deucher void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
116503afe6f6SAlex Deucher {
116603afe6f6SAlex Deucher 	if (enable) {
116703afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
116803afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = true;
116903afe6f6SAlex Deucher 		/* XXX select vce level based on ring/task */
117003afe6f6SAlex Deucher 		rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
117103afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
117203afe6f6SAlex Deucher 	} else {
117303afe6f6SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
117403afe6f6SAlex Deucher 		rdev->pm.dpm.vce_active = false;
117503afe6f6SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
117603afe6f6SAlex Deucher 	}
117703afe6f6SAlex Deucher 
117803afe6f6SAlex Deucher 	radeon_pm_compute_clocks(rdev);
117903afe6f6SAlex Deucher }
118003afe6f6SAlex Deucher 
1181da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
1182ce8f5370SAlex Deucher {
1183ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
11843f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
11853f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
11863f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
11873f53eb6fSRafael J. Wysocki 	}
1188ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
118932c87fcaSTejun Heo 
119032c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
1191ce8f5370SAlex Deucher }
1192ce8f5370SAlex Deucher 
1193da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1194da321c8aSAlex Deucher {
1195da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1196da321c8aSAlex Deucher 	/* disable dpm */
1197da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
1198da321c8aSAlex Deucher 	/* reset the power state */
1199da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1200da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
1201da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1202da321c8aSAlex Deucher }
1203da321c8aSAlex Deucher 
1204da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
1205da321c8aSAlex Deucher {
1206da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1207da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
1208da321c8aSAlex Deucher 	else
1209da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
1210da321c8aSAlex Deucher }
1211da321c8aSAlex Deucher 
1212da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
1213ce8f5370SAlex Deucher {
1214ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
12152e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
121636099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
12172e3b3b10SAlex Deucher 	    rdev->mc_fw) {
1218ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
12198a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
12208a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
12212feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
12222feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
12232feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1224ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
1225ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1226ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
1227ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1228ed18a360SAlex Deucher 	}
1229f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
1230f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1231f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1232f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
12339ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
12349ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
123537016951SMichel Dänzer 	if (rdev->pm.power_state) {
12364d60173fSAlex Deucher 		rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
12372feea49aSAlex Deucher 		rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
123837016951SMichel Dänzer 	}
12393f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
12403f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
12413f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
124232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
12433f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
12443f53eb6fSRafael J. Wysocki 	}
1245f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1246ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
1247d0d6cb81SRafał Miłecki }
1248d0d6cb81SRafał Miłecki 
1249da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
12507433874eSRafał Miłecki {
125126481fb1SDave Airlie 	int ret;
12520d18abedSDan Carpenter 
1253da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
1254da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1255da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1256da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1257da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1258da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1259e14cd2bbSAlex Deucher 	if (ret)
1260e14cd2bbSAlex Deucher 		goto dpm_resume_fail;
1261e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = true;
1262e14cd2bbSAlex Deucher 	return;
1263e14cd2bbSAlex Deucher 
1264e14cd2bbSAlex Deucher dpm_resume_fail:
1265da321c8aSAlex Deucher 	DRM_ERROR("radeon: dpm resume failed\n");
1266da321c8aSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
126736099186SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1268da321c8aSAlex Deucher 	    rdev->mc_fw) {
1269da321c8aSAlex Deucher 		if (rdev->pm.default_vddc)
1270da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1271da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1272da321c8aSAlex Deucher 		if (rdev->pm.default_vddci)
1273da321c8aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1274da321c8aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1275da321c8aSAlex Deucher 		if (rdev->pm.default_sclk)
1276da321c8aSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1277da321c8aSAlex Deucher 		if (rdev->pm.default_mclk)
1278da321c8aSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1279da321c8aSAlex Deucher 	}
1280da321c8aSAlex Deucher }
1281da321c8aSAlex Deucher 
1282da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
1283da321c8aSAlex Deucher {
1284da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1285da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
1286da321c8aSAlex Deucher 	else
1287da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
1288da321c8aSAlex Deucher }
1289da321c8aSAlex Deucher 
1290da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
1291da321c8aSAlex Deucher {
1292da321c8aSAlex Deucher 	int ret;
1293da321c8aSAlex Deucher 
1294f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
1295ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1296ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1297ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
1298ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
12999ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
13009ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1301f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1302f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
130321a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1304c913e23aSRafał Miłecki 
130556278a8eSAlex Deucher 	if (rdev->bios) {
130656278a8eSAlex Deucher 		if (rdev->is_atom_bios)
130756278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
130856278a8eSAlex Deucher 		else
130956278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
1310f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
1311ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
1312ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
13132e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
131436099186SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
13152e3b3b10SAlex Deucher 		    rdev->mc_fw) {
1316ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
13178a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
13188a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
13194639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
13204639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
13214639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1322ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
1323ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1324ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
1325ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1326ed18a360SAlex Deucher 		}
132756278a8eSAlex Deucher 	}
132856278a8eSAlex Deucher 
132921a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
13300d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
13310d18abedSDan Carpenter 	if (ret)
13320d18abedSDan Carpenter 		return ret;
133332c87fcaSTejun Heo 
133432c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
133532c87fcaSTejun Heo 
1336ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
13377433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
1338c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
13397433874eSRafał Miłecki 		}
13407433874eSRafał Miłecki 
1341c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
1342ce8f5370SAlex Deucher 	}
1343c913e23aSRafał Miłecki 
13447433874eSRafał Miłecki 	return 0;
13457433874eSRafał Miłecki }
13467433874eSRafał Miłecki 
1347da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1348da321c8aSAlex Deucher {
1349da321c8aSAlex Deucher 	int i;
1350da321c8aSAlex Deucher 
1351da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1352da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
1353da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1354da321c8aSAlex Deucher 	}
1355da321c8aSAlex Deucher }
1356da321c8aSAlex Deucher 
1357da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1358da321c8aSAlex Deucher {
1359da321c8aSAlex Deucher 	int ret;
1360da321c8aSAlex Deucher 
13611cd8b21aSAlex Deucher 	/* default to balanced state */
1362edcaa5b1SAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1363edcaa5b1SAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
13641cd8b21aSAlex Deucher 	rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
1365da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1366da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1367da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1368da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1369da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1370da321c8aSAlex Deucher 
1371da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1372da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1373da321c8aSAlex Deucher 	else
1374da321c8aSAlex Deucher 		return -EINVAL;
1375da321c8aSAlex Deucher 
1376da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1377da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1378da321c8aSAlex Deucher 	if (ret)
1379da321c8aSAlex Deucher 		return ret;
1380da321c8aSAlex Deucher 
1381da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1382da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1383da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1384da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1385033a37dfSAlex Deucher 	if (radeon_dpm == 1)
1386da321c8aSAlex Deucher 		radeon_dpm_print_power_states(rdev);
1387da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1388da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1389da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1390e14cd2bbSAlex Deucher 	if (ret)
1391e14cd2bbSAlex Deucher 		goto dpm_failed;
1392da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1393da321c8aSAlex Deucher 
13941316b792SAlex Deucher 	if (radeon_debugfs_pm_init(rdev)) {
13951316b792SAlex Deucher 		DRM_ERROR("Failed to register debugfs file for dpm!\n");
13961316b792SAlex Deucher 	}
13971316b792SAlex Deucher 
1398da321c8aSAlex Deucher 	DRM_INFO("radeon: dpm initialized\n");
1399da321c8aSAlex Deucher 
1400da321c8aSAlex Deucher 	return 0;
1401e14cd2bbSAlex Deucher 
1402e14cd2bbSAlex Deucher dpm_failed:
1403e14cd2bbSAlex Deucher 	rdev->pm.dpm_enabled = false;
1404e14cd2bbSAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
1405e14cd2bbSAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
1406e14cd2bbSAlex Deucher 	    rdev->mc_fw) {
1407e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddc)
1408e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1409e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
1410e14cd2bbSAlex Deucher 		if (rdev->pm.default_vddci)
1411e14cd2bbSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1412e14cd2bbSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
1413e14cd2bbSAlex Deucher 		if (rdev->pm.default_sclk)
1414e14cd2bbSAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1415e14cd2bbSAlex Deucher 		if (rdev->pm.default_mclk)
1416e14cd2bbSAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1417e14cd2bbSAlex Deucher 	}
1418e14cd2bbSAlex Deucher 	DRM_ERROR("radeon: dpm initialization failed\n");
1419e14cd2bbSAlex Deucher 	return ret;
1420da321c8aSAlex Deucher }
1421da321c8aSAlex Deucher 
14224369a69eSAlex Deucher struct radeon_dpm_quirk {
14234369a69eSAlex Deucher 	u32 chip_vendor;
14244369a69eSAlex Deucher 	u32 chip_device;
14254369a69eSAlex Deucher 	u32 subsys_vendor;
14264369a69eSAlex Deucher 	u32 subsys_device;
14274369a69eSAlex Deucher };
14284369a69eSAlex Deucher 
14294369a69eSAlex Deucher /* cards with dpm stability problems */
14304369a69eSAlex Deucher static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
14314369a69eSAlex Deucher 	/* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
14324369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
14334369a69eSAlex Deucher 	/* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
14344369a69eSAlex Deucher 	{ PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
14354369a69eSAlex Deucher 	{ 0, 0, 0, 0 },
14364369a69eSAlex Deucher };
14374369a69eSAlex Deucher 
1438da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1439da321c8aSAlex Deucher {
14404369a69eSAlex Deucher 	struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
14414369a69eSAlex Deucher 	bool disable_dpm = false;
14424369a69eSAlex Deucher 
14434369a69eSAlex Deucher 	/* Apply dpm quirks */
14444369a69eSAlex Deucher 	while (p && p->chip_device != 0) {
14454369a69eSAlex Deucher 		if (rdev->pdev->vendor == p->chip_vendor &&
14464369a69eSAlex Deucher 		    rdev->pdev->device == p->chip_device &&
14474369a69eSAlex Deucher 		    rdev->pdev->subsystem_vendor == p->subsys_vendor &&
14484369a69eSAlex Deucher 		    rdev->pdev->subsystem_device == p->subsys_device) {
14494369a69eSAlex Deucher 			disable_dpm = true;
14504369a69eSAlex Deucher 			break;
14514369a69eSAlex Deucher 		}
14524369a69eSAlex Deucher 		++p;
14534369a69eSAlex Deucher 	}
14544369a69eSAlex Deucher 
1455da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1456da321c8aSAlex Deucher 	switch (rdev->family) {
14574a6369e9SAlex Deucher 	case CHIP_RV610:
14584a6369e9SAlex Deucher 	case CHIP_RV630:
14594a6369e9SAlex Deucher 	case CHIP_RV620:
14604a6369e9SAlex Deucher 	case CHIP_RV635:
14614a6369e9SAlex Deucher 	case CHIP_RV670:
14629d67006eSAlex Deucher 	case CHIP_RS780:
14639d67006eSAlex Deucher 	case CHIP_RS880:
146476e6dcecSAlex Deucher 	case CHIP_RV770:
14658a53fa23SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
1466761bfb99SAlex Deucher 		if (!rdev->rlc_fw)
1467761bfb99SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
14688a53fa23SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
14698a53fa23SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
14708a53fa23SAlex Deucher 			 (!rdev->smc_fw))
14718a53fa23SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
1472761bfb99SAlex Deucher 		else if (radeon_dpm == 1)
14739d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
14749d67006eSAlex Deucher 		else
14759d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
14769d67006eSAlex Deucher 		break;
1477ab70b1ddSAlex Deucher 	case CHIP_RV730:
1478ab70b1ddSAlex Deucher 	case CHIP_RV710:
1479ab70b1ddSAlex Deucher 	case CHIP_RV740:
148059f7a2f2SAlex Deucher 	case CHIP_CEDAR:
148159f7a2f2SAlex Deucher 	case CHIP_REDWOOD:
148259f7a2f2SAlex Deucher 	case CHIP_JUNIPER:
148359f7a2f2SAlex Deucher 	case CHIP_CYPRESS:
148459f7a2f2SAlex Deucher 	case CHIP_HEMLOCK:
14855a16f761SAlex Deucher 	case CHIP_PALM:
14865a16f761SAlex Deucher 	case CHIP_SUMO:
14875a16f761SAlex Deucher 	case CHIP_SUMO2:
1488c08abf11SAlex Deucher 	case CHIP_BARTS:
1489c08abf11SAlex Deucher 	case CHIP_TURKS:
1490c08abf11SAlex Deucher 	case CHIP_CAICOS:
14918f500af4SAlex Deucher 	case CHIP_CAYMAN:
14923a118989SAlex Deucher 	case CHIP_ARUBA:
149368bc7785SAlex Deucher 	case CHIP_TAHITI:
149468bc7785SAlex Deucher 	case CHIP_PITCAIRN:
149568bc7785SAlex Deucher 	case CHIP_VERDE:
149668bc7785SAlex Deucher 	case CHIP_OLAND:
149768bc7785SAlex Deucher 	case CHIP_HAINAN:
14984f22dde3SAlex Deucher 	case CHIP_BONAIRE:
1499e308b1d3SAlex Deucher 	case CHIP_KABINI:
1500e308b1d3SAlex Deucher 	case CHIP_KAVERI:
15014f22dde3SAlex Deucher 	case CHIP_HAWAII:
15027d032a4bSSamuel Li 	case CHIP_MULLINS:
15035a16f761SAlex Deucher 		/* DPM requires the RLC, RV770+ dGPU requires SMC */
15045a16f761SAlex Deucher 		if (!rdev->rlc_fw)
15055a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15065a16f761SAlex Deucher 		else if ((rdev->family >= CHIP_RV770) &&
15075a16f761SAlex Deucher 			 (!(rdev->flags & RADEON_IS_IGP)) &&
15085a16f761SAlex Deucher 			 (!rdev->smc_fw))
15095a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15104369a69eSAlex Deucher 		else if (disable_dpm && (radeon_dpm == -1))
15114369a69eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15125a16f761SAlex Deucher 		else if (radeon_dpm == 0)
15135a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
15145a16f761SAlex Deucher 		else
15155a16f761SAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
15165a16f761SAlex Deucher 		break;
1517da321c8aSAlex Deucher 	default:
1518da321c8aSAlex Deucher 		/* default to profile method */
1519da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1520da321c8aSAlex Deucher 		break;
1521da321c8aSAlex Deucher 	}
1522da321c8aSAlex Deucher 
1523da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1524da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1525da321c8aSAlex Deucher 	else
1526da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1527da321c8aSAlex Deucher }
1528da321c8aSAlex Deucher 
1529914a8987SAlex Deucher int radeon_pm_late_init(struct radeon_device *rdev)
1530914a8987SAlex Deucher {
1531914a8987SAlex Deucher 	int ret = 0;
1532914a8987SAlex Deucher 
1533914a8987SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
153451a4726bSAlex Deucher 		if (rdev->pm.dpm_enabled) {
153549abb266SAlex Deucher 			if (!rdev->pm.sysfs_initialized) {
153651a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
153751a4726bSAlex Deucher 				if (ret)
153851a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
153951a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
154051a4726bSAlex Deucher 				if (ret)
154151a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for dpm state\n");
154251a4726bSAlex Deucher 				/* XXX: these are noops for dpm but are here for backwards compat */
154351a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_profile);
154451a4726bSAlex Deucher 				if (ret)
154551a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power profile\n");
154651a4726bSAlex Deucher 				ret = device_create_file(rdev->dev, &dev_attr_power_method);
154751a4726bSAlex Deucher 				if (ret)
154851a4726bSAlex Deucher 					DRM_ERROR("failed to create device file for power method\n");
154949abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
155049abb266SAlex Deucher 			}
155151a4726bSAlex Deucher 
1552914a8987SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
1553914a8987SAlex Deucher 			ret = radeon_dpm_late_enable(rdev);
1554914a8987SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
155551a4726bSAlex Deucher 			if (ret) {
155651a4726bSAlex Deucher 				rdev->pm.dpm_enabled = false;
155751a4726bSAlex Deucher 				DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
155851a4726bSAlex Deucher 			} else {
155951a4726bSAlex Deucher 				/* set the dpm state for PX since there won't be
156051a4726bSAlex Deucher 				 * a modeset to call this.
156151a4726bSAlex Deucher 				 */
156251a4726bSAlex Deucher 				radeon_pm_compute_clocks(rdev);
156351a4726bSAlex Deucher 			}
156451a4726bSAlex Deucher 		}
156551a4726bSAlex Deucher 	} else {
156649abb266SAlex Deucher 		if ((rdev->pm.num_power_states > 1) &&
156749abb266SAlex Deucher 		    (!rdev->pm.sysfs_initialized)) {
156851a4726bSAlex Deucher 			/* where's the best place to put these? */
156951a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_profile);
157051a4726bSAlex Deucher 			if (ret)
157151a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power profile\n");
157251a4726bSAlex Deucher 			ret = device_create_file(rdev->dev, &dev_attr_power_method);
157351a4726bSAlex Deucher 			if (ret)
157451a4726bSAlex Deucher 				DRM_ERROR("failed to create device file for power method\n");
157549abb266SAlex Deucher 			if (!ret)
157649abb266SAlex Deucher 				rdev->pm.sysfs_initialized = true;
157751a4726bSAlex Deucher 		}
1578914a8987SAlex Deucher 	}
1579914a8987SAlex Deucher 	return ret;
1580914a8987SAlex Deucher }
1581914a8987SAlex Deucher 
1582da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
158329fb52caSAlex Deucher {
1584ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1585a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1586ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1587ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1588ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1589ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1590ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1591ce8f5370SAlex Deucher 			/* reset default clocks */
1592ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1593ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1594ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
159558e21dffSAlex Deucher 		}
1596ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
159732c87fcaSTejun Heo 
159832c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
159958e21dffSAlex Deucher 
1600ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1601ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1602ce8f5370SAlex Deucher 	}
1603a424816fSAlex Deucher 
1604cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
16050975b162SAlex Deucher 	kfree(rdev->pm.power_state);
160629fb52caSAlex Deucher }
160729fb52caSAlex Deucher 
1608da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1609da321c8aSAlex Deucher {
1610da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1611da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1612da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1613da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1614da321c8aSAlex Deucher 
1615da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
161670d01a5eSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1617da321c8aSAlex Deucher 		/* XXX backwards compat */
1618da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1619da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1620da321c8aSAlex Deucher 	}
1621da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1622da321c8aSAlex Deucher 
1623cb3e4e7cSAlex Deucher 	radeon_hwmon_fini(rdev);
1624da321c8aSAlex Deucher 	kfree(rdev->pm.power_state);
1625da321c8aSAlex Deucher }
1626da321c8aSAlex Deucher 
1627da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1628da321c8aSAlex Deucher {
1629da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1630da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1631da321c8aSAlex Deucher 	else
1632da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1633da321c8aSAlex Deucher }
1634da321c8aSAlex Deucher 
1635da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1636c913e23aSRafał Miłecki {
1637c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1638a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1639c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1640c913e23aSRafał Miłecki 
1641ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1642ce8f5370SAlex Deucher 		return;
1643ce8f5370SAlex Deucher 
1644c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1645c913e23aSRafał Miłecki 
1646c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1647a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
16483ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1649a48b9b4eSAlex Deucher 		list_for_each_entry(crtc,
1650a48b9b4eSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1651a48b9b4eSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1652a48b9b4eSAlex Deucher 			if (radeon_crtc->enabled) {
1653c913e23aSRafał Miłecki 				rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1654a48b9b4eSAlex Deucher 				rdev->pm.active_crtc_count++;
1655c913e23aSRafał Miłecki 			}
1656c913e23aSRafał Miłecki 		}
16573ed9a335SAlex Deucher 	}
1658c913e23aSRafał Miłecki 
1659ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1660ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1661ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1662ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1663ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1664a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1665ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1666ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1667c913e23aSRafał Miłecki 
1668ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1669ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1670ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1671ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1672c913e23aSRafał Miłecki 
1673d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1674c913e23aSRafał Miłecki 				}
1675a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1676c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1677c913e23aSRafał Miłecki 
1678ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1679ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1680ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1681ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1682ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1683c913e23aSRafał Miłecki 
168432c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1685c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1686ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1687ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
168832c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1689c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1690d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1691c913e23aSRafał Miłecki 				}
1692a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1693ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1694ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1695c913e23aSRafał Miłecki 
1696ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1697ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1698ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1699ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1700ce8f5370SAlex Deucher 				}
1701ce8f5370SAlex Deucher 			}
170273a6d3fcSRafał Miłecki 		}
1703c913e23aSRafał Miłecki 	}
1704c913e23aSRafał Miłecki 
1705c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1706c913e23aSRafał Miłecki }
1707c913e23aSRafał Miłecki 
1708da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1709da321c8aSAlex Deucher {
1710da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1711da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1712da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1713da321c8aSAlex Deucher 
17146c7bcceaSAlex Deucher 	if (!rdev->pm.dpm_enabled)
17156c7bcceaSAlex Deucher 		return;
17166c7bcceaSAlex Deucher 
1717da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1718da321c8aSAlex Deucher 
17195ca302f7SAlex Deucher 	/* update active crtc counts */
1720da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1721da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
17223ed9a335SAlex Deucher 	if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1723da321c8aSAlex Deucher 		list_for_each_entry(crtc,
1724da321c8aSAlex Deucher 				    &ddev->mode_config.crtc_list, head) {
1725da321c8aSAlex Deucher 			radeon_crtc = to_radeon_crtc(crtc);
1726da321c8aSAlex Deucher 			if (crtc->enabled) {
1727da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1728da321c8aSAlex Deucher 				rdev->pm.dpm.new_active_crtc_count++;
1729da321c8aSAlex Deucher 			}
1730da321c8aSAlex Deucher 		}
17313ed9a335SAlex Deucher 	}
1732da321c8aSAlex Deucher 
17335ca302f7SAlex Deucher 	/* update battery/ac status */
17345ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
17355ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
17365ca302f7SAlex Deucher 	else
17375ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
17385ca302f7SAlex Deucher 
1739da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1740da321c8aSAlex Deucher 
1741da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
17428a227555SAlex Deucher 
1743da321c8aSAlex Deucher }
1744da321c8aSAlex Deucher 
1745da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1746da321c8aSAlex Deucher {
1747da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1748da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1749da321c8aSAlex Deucher 	else
1750da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1751da321c8aSAlex Deucher }
1752da321c8aSAlex Deucher 
1753ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1754f735261bSDave Airlie {
175575fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1756f735261bSDave Airlie 	bool in_vbl = true;
1757f735261bSDave Airlie 
175875fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
175975fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
176075fa0b08SMario Kleiner 	 */
176175fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
176275fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
17635b5561b3SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
17645b5561b3SMario Kleiner 								crtc,
17655b5561b3SMario Kleiner 								USE_REAL_VBLANKSTART,
17663bb403bfSVille Syrjälä 								&vpos, &hpos, NULL, NULL,
17673bb403bfSVille Syrjälä 								&rdev->mode_info.crtcs[crtc]->base.hwmode);
1768f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
17693d3cbd84SDaniel Vetter 			    !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
1770f735261bSDave Airlie 				in_vbl = false;
1771f735261bSDave Airlie 		}
1772f735261bSDave Airlie 	}
1773f81f2024SMatthew Garrett 
1774f81f2024SMatthew Garrett 	return in_vbl;
1775f81f2024SMatthew Garrett }
1776f81f2024SMatthew Garrett 
1777ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1778f81f2024SMatthew Garrett {
1779f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1780f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1781f81f2024SMatthew Garrett 
1782f735261bSDave Airlie 	if (in_vbl == false)
1783d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1784bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1785f735261bSDave Airlie 	return in_vbl;
1786f735261bSDave Airlie }
1787c913e23aSRafał Miłecki 
1788ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1789c913e23aSRafał Miłecki {
1790c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1791d9932a32SMatthew Garrett 	int resched;
1792c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1793ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1794c913e23aSRafał Miłecki 
1795d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1796c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1797ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1798c913e23aSRafał Miłecki 		int not_processed = 0;
17997465280cSAlex Deucher 		int i;
1800c913e23aSRafał Miłecki 
18017465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
18020ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
18030ec0612aSAlex Deucher 
18040ec0612aSAlex Deucher 			if (ring->ready) {
180547492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
18067465280cSAlex Deucher 				if (not_processed >= 3)
18077465280cSAlex Deucher 					break;
18087465280cSAlex Deucher 			}
18090ec0612aSAlex Deucher 		}
1810c913e23aSRafał Miłecki 
1811c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1812ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1813ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1814ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1815ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1816ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1817ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1818ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1819c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1820c913e23aSRafał Miłecki 			}
1821c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1822ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1823ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1824ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1825ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1826ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1827ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1828ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1829c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1830c913e23aSRafał Miłecki 			}
1831c913e23aSRafał Miłecki 		}
1832c913e23aSRafał Miłecki 
1833d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1834d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1835d7311171SAlex Deucher 		 */
1836ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1837ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1838ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1839ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1840c913e23aSRafał Miłecki 		}
1841c913e23aSRafał Miłecki 
184232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1843c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1844c913e23aSRafał Miłecki 	}
18453f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
18463f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
18473f53eb6fSRafael J. Wysocki }
1848c913e23aSRafał Miłecki 
18497433874eSRafał Miłecki /*
18507433874eSRafał Miłecki  * Debugfs info
18517433874eSRafał Miłecki  */
18527433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
18537433874eSRafał Miłecki 
18547433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
18557433874eSRafał Miłecki {
18567433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
18577433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
18587433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
18594f2f2039SAlex Deucher 	struct drm_device *ddev = rdev->ddev;
18607433874eSRafał Miłecki 
18614f2f2039SAlex Deucher 	if  ((rdev->flags & RADEON_IS_PX) &&
18624f2f2039SAlex Deucher 	     (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
18634f2f2039SAlex Deucher 		seq_printf(m, "PX asic powered off\n");
18644f2f2039SAlex Deucher 	} else if (rdev->pm.dpm_enabled) {
18651316b792SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
18661316b792SAlex Deucher 		if (rdev->asic->dpm.debugfs_print_current_performance_level)
18671316b792SAlex Deucher 			radeon_dpm_debugfs_print_current_performance_level(rdev, m);
18681316b792SAlex Deucher 		else
186971375929SAlex Deucher 			seq_printf(m, "Debugfs support not implemented for this asic\n");
18701316b792SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
18711316b792SAlex Deucher 	} else {
18729ace9f7bSAlex Deucher 		seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1873bf05d998SAlex Deucher 		/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1874bf05d998SAlex Deucher 		if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1875bf05d998SAlex Deucher 			seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1876bf05d998SAlex Deucher 		else
18776234077dSRafał Miłecki 			seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
18789ace9f7bSAlex Deucher 		seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1879798bcf73SAlex Deucher 		if (rdev->asic->pm.get_memory_clock)
18806234077dSRafał Miłecki 			seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
18810fcbe947SRafał Miłecki 		if (rdev->pm.current_vddc)
18820fcbe947SRafał Miłecki 			seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1883798bcf73SAlex Deucher 		if (rdev->asic->pm.get_pcie_lanes)
1884aa5120d2SRafał Miłecki 			seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
18851316b792SAlex Deucher 	}
18867433874eSRafał Miłecki 
18877433874eSRafał Miłecki 	return 0;
18887433874eSRafał Miłecki }
18897433874eSRafał Miłecki 
18907433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
18917433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
18927433874eSRafał Miłecki };
18937433874eSRafał Miłecki #endif
18947433874eSRafał Miłecki 
1895c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
18967433874eSRafał Miłecki {
18977433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
18987433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
18997433874eSRafał Miłecki #else
19007433874eSRafał Miłecki 	return 0;
19017433874eSRafał Miłecki #endif
19027433874eSRafał Miłecki }
1903