xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 89c9bc565138ba7801e4ac1925ec9f013a8b4a57)
17433874eSRafał Miłecki /*
27433874eSRafał Miłecki  * Permission is hereby granted, free of charge, to any person obtaining a
37433874eSRafał Miłecki  * copy of this software and associated documentation files (the "Software"),
47433874eSRafał Miłecki  * to deal in the Software without restriction, including without limitation
57433874eSRafał Miłecki  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
67433874eSRafał Miłecki  * and/or sell copies of the Software, and to permit persons to whom the
77433874eSRafał Miłecki  * Software is furnished to do so, subject to the following conditions:
87433874eSRafał Miłecki  *
97433874eSRafał Miłecki  * The above copyright notice and this permission notice shall be included in
107433874eSRafał Miłecki  * all copies or substantial portions of the Software.
117433874eSRafał Miłecki  *
127433874eSRafał Miłecki  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
137433874eSRafał Miłecki  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
147433874eSRafał Miłecki  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
157433874eSRafał Miłecki  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
167433874eSRafał Miłecki  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
177433874eSRafał Miłecki  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
187433874eSRafał Miłecki  * OTHER DEALINGS IN THE SOFTWARE.
197433874eSRafał Miłecki  *
207433874eSRafał Miłecki  * Authors: Rafał Miłecki <zajec5@gmail.com>
2156278a8eSAlex Deucher  *          Alex Deucher <alexdeucher@gmail.com>
227433874eSRafał Miłecki  */
23760285e7SDavid Howells #include <drm/drmP.h>
247433874eSRafał Miłecki #include "radeon.h"
25f735261bSDave Airlie #include "avivod.h"
268a83ec5eSAlex Deucher #include "atom.h"
27ce8f5370SAlex Deucher #include <linux/power_supply.h>
2821a8122aSAlex Deucher #include <linux/hwmon.h>
2921a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
307433874eSRafał Miłecki 
31c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
32c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3373a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
34c913e23aSRafał Miłecki 
35f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
36eb2c27a0SAlex Deucher 	"",
37f712d0c7SRafał Miłecki 	"Powersave",
38f712d0c7SRafał Miłecki 	"Battery",
39f712d0c7SRafał Miłecki 	"Balanced",
40f712d0c7SRafał Miłecki 	"Performance",
41f712d0c7SRafał Miłecki };
42f712d0c7SRafał Miłecki 
43ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
44c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
45ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
46ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
47ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
48ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
49ce8f5370SAlex Deucher 
50a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
51a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
52a4c9e2eeSAlex Deucher 			     int instance)
53a4c9e2eeSAlex Deucher {
54a4c9e2eeSAlex Deucher 	int i;
55a4c9e2eeSAlex Deucher 	int found_instance = -1;
56a4c9e2eeSAlex Deucher 
57a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
58a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
59a4c9e2eeSAlex Deucher 			found_instance++;
60a4c9e2eeSAlex Deucher 			if (found_instance == instance)
61a4c9e2eeSAlex Deucher 				return i;
62a4c9e2eeSAlex Deucher 		}
63a4c9e2eeSAlex Deucher 	}
64a4c9e2eeSAlex Deucher 	/* return default if no match */
65a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
66a4c9e2eeSAlex Deucher }
67a4c9e2eeSAlex Deucher 
68c4917074SAlex Deucher void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
69ce8f5370SAlex Deucher {
70ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
71ce8f5370SAlex Deucher 		if (rdev->pm.profile == PM_PROFILE_AUTO) {
72ce8f5370SAlex Deucher 			mutex_lock(&rdev->pm.mutex);
73ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
74ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
75ce8f5370SAlex Deucher 			mutex_unlock(&rdev->pm.mutex);
76ce8f5370SAlex Deucher 		}
77ce8f5370SAlex Deucher 	}
78ce8f5370SAlex Deucher }
79ce8f5370SAlex Deucher 
80ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
81ce8f5370SAlex Deucher {
82ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
83ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
84ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
85ce8f5370SAlex Deucher 		break;
86ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
87ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
88ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
89ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
90ce8f5370SAlex Deucher 			else
91ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
92ce8f5370SAlex Deucher 		} else {
93ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
94c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
95ce8f5370SAlex Deucher 			else
96c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
97ce8f5370SAlex Deucher 		}
98ce8f5370SAlex Deucher 		break;
99ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
100ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
101ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
102ce8f5370SAlex Deucher 		else
103ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
104ce8f5370SAlex Deucher 		break;
105c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
106c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
107c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
108c9e75b21SAlex Deucher 		else
109c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
110c9e75b21SAlex Deucher 		break;
111ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
112ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
113ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
114ce8f5370SAlex Deucher 		else
115ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
116ce8f5370SAlex Deucher 		break;
117ce8f5370SAlex Deucher 	}
118ce8f5370SAlex Deucher 
119ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
120ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
121ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
122ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
123ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
124ce8f5370SAlex Deucher 	} else {
125ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
126ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
127ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
128ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
129ce8f5370SAlex Deucher 	}
130ce8f5370SAlex Deucher }
131c913e23aSRafał Miłecki 
1325876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1335876dd24SMatthew Garrett {
1345876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1355876dd24SMatthew Garrett 
1365876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1375876dd24SMatthew Garrett 		return;
1385876dd24SMatthew Garrett 
1395876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1405876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1415876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1425876dd24SMatthew Garrett 	}
1435876dd24SMatthew Garrett }
1445876dd24SMatthew Garrett 
145ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
146ce8f5370SAlex Deucher {
147ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
148ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
149ce8f5370SAlex Deucher 		wait_event_timeout(
150ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
151ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
152ce8f5370SAlex Deucher 	}
153ce8f5370SAlex Deucher }
154ce8f5370SAlex Deucher 
155ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
156ce8f5370SAlex Deucher {
157ce8f5370SAlex Deucher 	u32 sclk, mclk;
15892645879SAlex Deucher 	bool misc_after = false;
159ce8f5370SAlex Deucher 
160ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
161ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
162ce8f5370SAlex Deucher 		return;
163ce8f5370SAlex Deucher 
164ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
165ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
166ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1679ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1689ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
169ce8f5370SAlex Deucher 
17027810fb2SAlex Deucher 		/* starting with BTC, there is one state that is used for both
17127810fb2SAlex Deucher 		 * MH and SH.  Difference is that we always use the high clock index for
1727ae764b1SAlex Deucher 		 * mclk and vddci.
17327810fb2SAlex Deucher 		 */
17427810fb2SAlex Deucher 		if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
17527810fb2SAlex Deucher 		    (rdev->family >= CHIP_BARTS) &&
17627810fb2SAlex Deucher 		    rdev->pm.active_crtc_count &&
17727810fb2SAlex Deucher 		    ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
17827810fb2SAlex Deucher 		     (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
17927810fb2SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
18027810fb2SAlex Deucher 				clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
18127810fb2SAlex Deucher 		else
182ce8f5370SAlex Deucher 			mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
183ce8f5370SAlex Deucher 				clock_info[rdev->pm.requested_clock_mode_index].mclk;
18427810fb2SAlex Deucher 
1859ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1869ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
187ce8f5370SAlex Deucher 
18892645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
18992645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
19092645879SAlex Deucher 			misc_after = true;
19192645879SAlex Deucher 
19292645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
19392645879SAlex Deucher 
19492645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
19592645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
19692645879SAlex Deucher 				return;
19792645879SAlex Deucher 		}
19892645879SAlex Deucher 
19992645879SAlex Deucher 		radeon_pm_prepare(rdev);
20092645879SAlex Deucher 
20192645879SAlex Deucher 		if (!misc_after)
202ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
203ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
204ce8f5370SAlex Deucher 
205ce8f5370SAlex Deucher 		/* set engine clock */
206ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
207ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
208ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
209ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
210ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
211d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
212ce8f5370SAlex Deucher 		}
213ce8f5370SAlex Deucher 
214ce8f5370SAlex Deucher 		/* set memory clock */
215798bcf73SAlex Deucher 		if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
216ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
217ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
218ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
219ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
220d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
221ce8f5370SAlex Deucher 		}
22292645879SAlex Deucher 
22392645879SAlex Deucher 		if (misc_after)
22492645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
22592645879SAlex Deucher 			radeon_pm_misc(rdev);
22692645879SAlex Deucher 
227ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
228ce8f5370SAlex Deucher 
229ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
230ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
231ce8f5370SAlex Deucher 	} else
232d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
233ce8f5370SAlex Deucher }
234ce8f5370SAlex Deucher 
235ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
236a424816fSAlex Deucher {
2375f8f635eSJerome Glisse 	int i, r;
2382aba631cSMatthew Garrett 
2394e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2404e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2414e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2424e186b2dSAlex Deucher 		return;
2434e186b2dSAlex Deucher 
244612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
245db7fce39SChristian König 	down_write(&rdev->pm.mclk_lock);
246d6999bc7SChristian König 	mutex_lock(&rdev->ring_lock);
2474f3218cbSAlex Deucher 
24895f5a3acSAlex Deucher 	/* wait for the rings to drain */
24995f5a3acSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
25095f5a3acSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
2515f8f635eSJerome Glisse 		if (!ring->ready) {
2525f8f635eSJerome Glisse 			continue;
2535f8f635eSJerome Glisse 		}
2545f8f635eSJerome Glisse 		r = radeon_fence_wait_empty_locked(rdev, i);
2555f8f635eSJerome Glisse 		if (r) {
2565f8f635eSJerome Glisse 			/* needs a GPU reset dont reset here */
2575f8f635eSJerome Glisse 			mutex_unlock(&rdev->ring_lock);
2585f8f635eSJerome Glisse 			up_write(&rdev->pm.mclk_lock);
2595f8f635eSJerome Glisse 			mutex_unlock(&rdev->ddev->struct_mutex);
2605f8f635eSJerome Glisse 			return;
2615f8f635eSJerome Glisse 		}
262ce8f5370SAlex Deucher 	}
26395f5a3acSAlex Deucher 
2645876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2655876dd24SMatthew Garrett 
266ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2672aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2682aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2692aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2702aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2712aba631cSMatthew Garrett 			}
2722aba631cSMatthew Garrett 		}
2732aba631cSMatthew Garrett 	}
2742aba631cSMatthew Garrett 
275ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2762aba631cSMatthew Garrett 
277ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2782aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2792aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2802aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2812aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2822aba631cSMatthew Garrett 			}
2832aba631cSMatthew Garrett 		}
2842aba631cSMatthew Garrett 	}
285a424816fSAlex Deucher 
286a424816fSAlex Deucher 	/* update display watermarks based on new power state */
287a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
288a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
289a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
290a424816fSAlex Deucher 
291ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
2922aba631cSMatthew Garrett 
293d6999bc7SChristian König 	mutex_unlock(&rdev->ring_lock);
294db7fce39SChristian König 	up_write(&rdev->pm.mclk_lock);
295612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
296a424816fSAlex Deucher }
297a424816fSAlex Deucher 
298f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
299f712d0c7SRafał Miłecki {
300f712d0c7SRafał Miłecki 	int i, j;
301f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
302f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
303f712d0c7SRafał Miłecki 
304d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
305f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
306f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
307d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
308f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
309f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
310d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
311f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
312d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
313f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
314d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
315d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
316f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
317f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
318f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
319eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
320f712d0c7SRafał Miłecki 						 j,
321eb2c27a0SAlex Deucher 						 clock_info->sclk * 10);
322f712d0c7SRafał Miłecki 			else
323eb2c27a0SAlex Deucher 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
324f712d0c7SRafał Miłecki 						 j,
325f712d0c7SRafał Miłecki 						 clock_info->sclk * 10,
326f712d0c7SRafał Miłecki 						 clock_info->mclk * 10,
327eb2c27a0SAlex Deucher 						 clock_info->voltage.voltage);
328f712d0c7SRafał Miłecki 		}
329f712d0c7SRafał Miłecki 	}
330f712d0c7SRafał Miłecki }
331f712d0c7SRafał Miłecki 
332ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
333a424816fSAlex Deucher 				     struct device_attribute *attr,
334a424816fSAlex Deucher 				     char *buf)
335a424816fSAlex Deucher {
336a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
337a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
338ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
339a424816fSAlex Deucher 
340a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
341ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
342ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
34312e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
344ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
345a424816fSAlex Deucher }
346a424816fSAlex Deucher 
347ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
348a424816fSAlex Deucher 				     struct device_attribute *attr,
349a424816fSAlex Deucher 				     const char *buf,
350a424816fSAlex Deucher 				     size_t count)
351a424816fSAlex Deucher {
352a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
353a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
354a424816fSAlex Deucher 
355a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
356ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
357ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
358ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
359ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
360ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
361ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
362ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
363c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
364c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
365ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
366ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
367ce8f5370SAlex Deucher 		else {
3681783e4bfSThomas Renninger 			count = -EINVAL;
369ce8f5370SAlex Deucher 			goto fail;
370ce8f5370SAlex Deucher 		}
371ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
372ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3731783e4bfSThomas Renninger 	} else
3741783e4bfSThomas Renninger 		count = -EINVAL;
3751783e4bfSThomas Renninger 
376ce8f5370SAlex Deucher fail:
377a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
378a424816fSAlex Deucher 
379a424816fSAlex Deucher 	return count;
380a424816fSAlex Deucher }
381a424816fSAlex Deucher 
382ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
383ce8f5370SAlex Deucher 				    struct device_attribute *attr,
384ce8f5370SAlex Deucher 				    char *buf)
38556278a8eSAlex Deucher {
386ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
387ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
388ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
38956278a8eSAlex Deucher 
390ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
391da321c8aSAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" :
392da321c8aSAlex Deucher 			(pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
39356278a8eSAlex Deucher }
39456278a8eSAlex Deucher 
395ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
396ce8f5370SAlex Deucher 				    struct device_attribute *attr,
397ce8f5370SAlex Deucher 				    const char *buf,
398ce8f5370SAlex Deucher 				    size_t count)
399d0d6cb81SRafał Miłecki {
400ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
401ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
402ce8f5370SAlex Deucher 
403da321c8aSAlex Deucher 	/* we don't support the legacy modes with dpm */
404da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM) {
405da321c8aSAlex Deucher 		count = -EINVAL;
406da321c8aSAlex Deucher 		goto fail;
407da321c8aSAlex Deucher 	}
408ce8f5370SAlex Deucher 
409ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
410ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
411ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
412ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
413ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
414ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
415ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
416ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
417ce8f5370SAlex Deucher 		/* disable dynpm */
418ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
419ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4203f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
421ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
42232c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
423ce8f5370SAlex Deucher 	} else {
4241783e4bfSThomas Renninger 		count = -EINVAL;
425ce8f5370SAlex Deucher 		goto fail;
426d0d6cb81SRafał Miłecki 	}
427ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
428ce8f5370SAlex Deucher fail:
429ce8f5370SAlex Deucher 	return count;
430ce8f5370SAlex Deucher }
431ce8f5370SAlex Deucher 
432da321c8aSAlex Deucher static ssize_t radeon_get_dpm_state(struct device *dev,
433da321c8aSAlex Deucher 				    struct device_attribute *attr,
434da321c8aSAlex Deucher 				    char *buf)
435da321c8aSAlex Deucher {
436da321c8aSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
437da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
438da321c8aSAlex Deucher 	enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
439da321c8aSAlex Deucher 
440da321c8aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
441da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
442da321c8aSAlex Deucher 			(pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
443da321c8aSAlex Deucher }
444da321c8aSAlex Deucher 
445da321c8aSAlex Deucher static ssize_t radeon_set_dpm_state(struct device *dev,
446da321c8aSAlex Deucher 				    struct device_attribute *attr,
447da321c8aSAlex Deucher 				    const char *buf,
448da321c8aSAlex Deucher 				    size_t count)
449da321c8aSAlex Deucher {
450da321c8aSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
451da321c8aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
452da321c8aSAlex Deucher 
453da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
454da321c8aSAlex Deucher 	if (strncmp("battery", buf, strlen("battery")) == 0)
455da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
456da321c8aSAlex Deucher 	else if (strncmp("balanced", buf, strlen("balanced")) == 0)
457da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
458da321c8aSAlex Deucher 	else if (strncmp("performance", buf, strlen("performance")) == 0)
459da321c8aSAlex Deucher 		rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
460da321c8aSAlex Deucher 	else {
461da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
462da321c8aSAlex Deucher 		count = -EINVAL;
463da321c8aSAlex Deucher 		goto fail;
464da321c8aSAlex Deucher 	}
465da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
466da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
467da321c8aSAlex Deucher fail:
468da321c8aSAlex Deucher 	return count;
469da321c8aSAlex Deucher }
470da321c8aSAlex Deucher 
471ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
472ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
473da321c8aSAlex Deucher static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
474ce8f5370SAlex Deucher 
47521a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
47621a8122aSAlex Deucher 				      struct device_attribute *attr,
47721a8122aSAlex Deucher 				      char *buf)
47821a8122aSAlex Deucher {
47921a8122aSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
48021a8122aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
48120d391d7SAlex Deucher 	int temp;
48221a8122aSAlex Deucher 
4836bd1c385SAlex Deucher 	if (rdev->asic->pm.get_temperature)
4846bd1c385SAlex Deucher 		temp = radeon_get_temperature(rdev);
4856bd1c385SAlex Deucher 	else
48621a8122aSAlex Deucher 		temp = 0;
48721a8122aSAlex Deucher 
48821a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
48921a8122aSAlex Deucher }
49021a8122aSAlex Deucher 
49121a8122aSAlex Deucher static ssize_t radeon_hwmon_show_name(struct device *dev,
49221a8122aSAlex Deucher 				      struct device_attribute *attr,
49321a8122aSAlex Deucher 				      char *buf)
49421a8122aSAlex Deucher {
49521a8122aSAlex Deucher 	return sprintf(buf, "radeon\n");
49621a8122aSAlex Deucher }
49721a8122aSAlex Deucher 
49821a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
49921a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
50021a8122aSAlex Deucher 
50121a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
50221a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
50321a8122aSAlex Deucher 	&sensor_dev_attr_name.dev_attr.attr,
50421a8122aSAlex Deucher 	NULL
50521a8122aSAlex Deucher };
50621a8122aSAlex Deucher 
50721a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
50821a8122aSAlex Deucher 	.attrs = hwmon_attributes,
50921a8122aSAlex Deucher };
51021a8122aSAlex Deucher 
5110d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
51221a8122aSAlex Deucher {
5130d18abedSDan Carpenter 	int err = 0;
51421a8122aSAlex Deucher 
51521a8122aSAlex Deucher 	rdev->pm.int_hwmon_dev = NULL;
51621a8122aSAlex Deucher 
51721a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
51821a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
51921a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
52021a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
521457558edSAlex Deucher 	case THERMAL_TYPE_NI:
522e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
5231bd47d2eSAlex Deucher 	case THERMAL_TYPE_SI:
5246bd1c385SAlex Deucher 		if (rdev->asic->pm.get_temperature == NULL)
5255d7486c7SAlex Deucher 			return err;
52621a8122aSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
5270d18abedSDan Carpenter 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
5280d18abedSDan Carpenter 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
5290d18abedSDan Carpenter 			dev_err(rdev->dev,
5300d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
5310d18abedSDan Carpenter 			break;
5320d18abedSDan Carpenter 		}
53321a8122aSAlex Deucher 		dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
53421a8122aSAlex Deucher 		err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
53521a8122aSAlex Deucher 					 &hwmon_attrgroup);
5360d18abedSDan Carpenter 		if (err) {
5370d18abedSDan Carpenter 			dev_err(rdev->dev,
5380d18abedSDan Carpenter 				"Unable to create hwmon sysfs file: %d\n", err);
5390d18abedSDan Carpenter 			hwmon_device_unregister(rdev->dev);
5400d18abedSDan Carpenter 		}
54121a8122aSAlex Deucher 		break;
54221a8122aSAlex Deucher 	default:
54321a8122aSAlex Deucher 		break;
54421a8122aSAlex Deucher 	}
5450d18abedSDan Carpenter 
5460d18abedSDan Carpenter 	return err;
54721a8122aSAlex Deucher }
54821a8122aSAlex Deucher 
54921a8122aSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
55021a8122aSAlex Deucher {
55121a8122aSAlex Deucher 	if (rdev->pm.int_hwmon_dev) {
55221a8122aSAlex Deucher 		sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
55321a8122aSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
55421a8122aSAlex Deucher 	}
55521a8122aSAlex Deucher }
55621a8122aSAlex Deucher 
557da321c8aSAlex Deucher static void radeon_dpm_thermal_work_handler(struct work_struct *work)
558da321c8aSAlex Deucher {
559da321c8aSAlex Deucher 	struct radeon_device *rdev =
560da321c8aSAlex Deucher 		container_of(work, struct radeon_device,
561da321c8aSAlex Deucher 			     pm.dpm.thermal.work);
562da321c8aSAlex Deucher 	/* switch to the thermal state */
563da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
564da321c8aSAlex Deucher 
565da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
566da321c8aSAlex Deucher 		return;
567da321c8aSAlex Deucher 
568da321c8aSAlex Deucher 	if (rdev->asic->pm.get_temperature) {
569da321c8aSAlex Deucher 		int temp = radeon_get_temperature(rdev);
570da321c8aSAlex Deucher 
571da321c8aSAlex Deucher 		if (temp < rdev->pm.dpm.thermal.min_temp)
572da321c8aSAlex Deucher 			/* switch back the user state */
573da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
574da321c8aSAlex Deucher 	} else {
575da321c8aSAlex Deucher 		if (rdev->pm.dpm.thermal.high_to_low)
576da321c8aSAlex Deucher 			/* switch back the user state */
577da321c8aSAlex Deucher 			dpm_state = rdev->pm.dpm.user_state;
578da321c8aSAlex Deucher 	}
579da321c8aSAlex Deucher 	radeon_dpm_enable_power_state(rdev, dpm_state);
580da321c8aSAlex Deucher }
581da321c8aSAlex Deucher 
582da321c8aSAlex Deucher static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
583da321c8aSAlex Deucher 						     enum radeon_pm_state_type dpm_state)
584da321c8aSAlex Deucher {
585da321c8aSAlex Deucher 	int i;
586da321c8aSAlex Deucher 	struct radeon_ps *ps;
587da321c8aSAlex Deucher 	u32 ui_class;
588da321c8aSAlex Deucher 
589da321c8aSAlex Deucher restart_search:
590da321c8aSAlex Deucher 	/* balanced states don't exist at the moment */
591da321c8aSAlex Deucher 	if (dpm_state == POWER_STATE_TYPE_BALANCED)
592da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
593da321c8aSAlex Deucher 
594da321c8aSAlex Deucher 	/* Pick the best power state based on current conditions */
595da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
596da321c8aSAlex Deucher 		ps = &rdev->pm.dpm.ps[i];
597da321c8aSAlex Deucher 		ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
598da321c8aSAlex Deucher 		switch (dpm_state) {
599da321c8aSAlex Deucher 		/* user states */
600da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BATTERY:
601da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
602da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
603da321c8aSAlex Deucher 					if (rdev->pm.dpm.new_active_crtc_count < 2)
604da321c8aSAlex Deucher 						return ps;
605da321c8aSAlex Deucher 				} else
606da321c8aSAlex Deucher 					return ps;
607da321c8aSAlex Deucher 			}
608da321c8aSAlex Deucher 			break;
609da321c8aSAlex Deucher 		case POWER_STATE_TYPE_BALANCED:
610da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
611da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
612da321c8aSAlex Deucher 					if (rdev->pm.dpm.new_active_crtc_count < 2)
613da321c8aSAlex Deucher 						return ps;
614da321c8aSAlex Deucher 				} else
615da321c8aSAlex Deucher 					return ps;
616da321c8aSAlex Deucher 			}
617da321c8aSAlex Deucher 			break;
618da321c8aSAlex Deucher 		case POWER_STATE_TYPE_PERFORMANCE:
619da321c8aSAlex Deucher 			if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
620da321c8aSAlex Deucher 				if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
621da321c8aSAlex Deucher 					if (rdev->pm.dpm.new_active_crtc_count < 2)
622da321c8aSAlex Deucher 						return ps;
623da321c8aSAlex Deucher 				} else
624da321c8aSAlex Deucher 					return ps;
625da321c8aSAlex Deucher 			}
626da321c8aSAlex Deucher 			break;
627da321c8aSAlex Deucher 		/* internal states */
628da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD:
629da321c8aSAlex Deucher 			return rdev->pm.dpm.uvd_ps;
630da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_SD:
631da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
632da321c8aSAlex Deucher 				return ps;
633da321c8aSAlex Deucher 			break;
634da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD:
635da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
636da321c8aSAlex Deucher 				return ps;
637da321c8aSAlex Deucher 			break;
638da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
639da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
640da321c8aSAlex Deucher 				return ps;
641da321c8aSAlex Deucher 			break;
642da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
643da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
644da321c8aSAlex Deucher 				return ps;
645da321c8aSAlex Deucher 			break;
646da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_BOOT:
647da321c8aSAlex Deucher 			return rdev->pm.dpm.boot_ps;
648da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_THERMAL:
649da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
650da321c8aSAlex Deucher 				return ps;
651da321c8aSAlex Deucher 			break;
652da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ACPI:
653da321c8aSAlex Deucher 			if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
654da321c8aSAlex Deucher 				return ps;
655da321c8aSAlex Deucher 			break;
656da321c8aSAlex Deucher 		case POWER_STATE_TYPE_INTERNAL_ULV:
657da321c8aSAlex Deucher 			if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
658da321c8aSAlex Deucher 				return ps;
659da321c8aSAlex Deucher 			break;
660da321c8aSAlex Deucher 		default:
661da321c8aSAlex Deucher 			break;
662da321c8aSAlex Deucher 		}
663da321c8aSAlex Deucher 	}
664da321c8aSAlex Deucher 	/* use a fallback state if we didn't match */
665da321c8aSAlex Deucher 	switch (dpm_state) {
666da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
667da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
668da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
669da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
670da321c8aSAlex Deucher 		return rdev->pm.dpm.uvd_ps;
671da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
672da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
673da321c8aSAlex Deucher 		goto restart_search;
674da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_ACPI:
675da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_BATTERY;
676da321c8aSAlex Deucher 		goto restart_search;
677da321c8aSAlex Deucher 	case POWER_STATE_TYPE_BATTERY:
678da321c8aSAlex Deucher 		dpm_state = POWER_STATE_TYPE_PERFORMANCE;
679da321c8aSAlex Deucher 		goto restart_search;
680da321c8aSAlex Deucher 	default:
681da321c8aSAlex Deucher 		break;
682da321c8aSAlex Deucher 	}
683da321c8aSAlex Deucher 
684da321c8aSAlex Deucher 	return NULL;
685da321c8aSAlex Deucher }
686da321c8aSAlex Deucher 
687da321c8aSAlex Deucher static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
688da321c8aSAlex Deucher {
689da321c8aSAlex Deucher 	int i;
690da321c8aSAlex Deucher 	struct radeon_ps *ps;
691da321c8aSAlex Deucher 	enum radeon_pm_state_type dpm_state;
69284dd1928SAlex Deucher 	int ret;
693da321c8aSAlex Deucher 
694da321c8aSAlex Deucher 	/* if dpm init failed */
695da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
696da321c8aSAlex Deucher 		return;
697da321c8aSAlex Deucher 
698da321c8aSAlex Deucher 	if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
699da321c8aSAlex Deucher 		/* add other state override checks here */
7008a227555SAlex Deucher 		if ((!rdev->pm.dpm.thermal_active) &&
7018a227555SAlex Deucher 		    (!rdev->pm.dpm.uvd_active))
702da321c8aSAlex Deucher 			rdev->pm.dpm.state = rdev->pm.dpm.user_state;
703da321c8aSAlex Deucher 	}
704da321c8aSAlex Deucher 	dpm_state = rdev->pm.dpm.state;
705da321c8aSAlex Deucher 
706da321c8aSAlex Deucher 	ps = radeon_dpm_pick_power_state(rdev, dpm_state);
707da321c8aSAlex Deucher 	if (ps)
708*89c9bc56SAlex Deucher 		rdev->pm.dpm.requested_ps = ps;
709da321c8aSAlex Deucher 	else
710da321c8aSAlex Deucher 		return;
711da321c8aSAlex Deucher 
712d22b7e40SAlex Deucher 	/* no need to reprogram if nothing changed unless we are on BTC+ */
713da321c8aSAlex Deucher 	if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
714d22b7e40SAlex Deucher 		if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
715d22b7e40SAlex Deucher 			/* for pre-BTC and APUs if the num crtcs changed but state is the same,
716d22b7e40SAlex Deucher 			 * all we need to do is update the display configuration.
717d22b7e40SAlex Deucher 			 */
718da321c8aSAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
719d22b7e40SAlex Deucher 				/* update display watermarks based on new power state */
720da321c8aSAlex Deucher 				radeon_bandwidth_update(rdev);
721da321c8aSAlex Deucher 				/* update displays */
722da321c8aSAlex Deucher 				radeon_dpm_display_configuration_changed(rdev);
723da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
724da321c8aSAlex Deucher 				rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
725da321c8aSAlex Deucher 			}
726da321c8aSAlex Deucher 			return;
727d22b7e40SAlex Deucher 		} else {
728d22b7e40SAlex Deucher 			/* for BTC+ if the num crtcs hasn't changed and state is the same,
729d22b7e40SAlex Deucher 			 * nothing to do, if the num crtcs is > 1 and state is the same,
730d22b7e40SAlex Deucher 			 * update display configuration.
731d22b7e40SAlex Deucher 			 */
732d22b7e40SAlex Deucher 			if (rdev->pm.dpm.new_active_crtcs ==
733d22b7e40SAlex Deucher 			    rdev->pm.dpm.current_active_crtcs) {
734d22b7e40SAlex Deucher 				return;
735d22b7e40SAlex Deucher 			} else {
736d22b7e40SAlex Deucher 				if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
737d22b7e40SAlex Deucher 				    (rdev->pm.dpm.new_active_crtc_count > 1)) {
738d22b7e40SAlex Deucher 					/* update display watermarks based on new power state */
739d22b7e40SAlex Deucher 					radeon_bandwidth_update(rdev);
740d22b7e40SAlex Deucher 					/* update displays */
741d22b7e40SAlex Deucher 					radeon_dpm_display_configuration_changed(rdev);
742d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
743d22b7e40SAlex Deucher 					rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
744d22b7e40SAlex Deucher 					return;
745d22b7e40SAlex Deucher 				}
746d22b7e40SAlex Deucher 			}
747d22b7e40SAlex Deucher 		}
748da321c8aSAlex Deucher 	}
749da321c8aSAlex Deucher 
750da321c8aSAlex Deucher 	printk("switching from power state:\n");
751da321c8aSAlex Deucher 	radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
752da321c8aSAlex Deucher 	printk("switching to power state:\n");
753da321c8aSAlex Deucher 	radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
754da321c8aSAlex Deucher 
755da321c8aSAlex Deucher 	mutex_lock(&rdev->ddev->struct_mutex);
756da321c8aSAlex Deucher 	down_write(&rdev->pm.mclk_lock);
757da321c8aSAlex Deucher 	mutex_lock(&rdev->ring_lock);
758da321c8aSAlex Deucher 
75984dd1928SAlex Deucher 	ret = radeon_dpm_pre_set_power_state(rdev);
76084dd1928SAlex Deucher 	if (ret)
76184dd1928SAlex Deucher 		goto done;
76284dd1928SAlex Deucher 
763da321c8aSAlex Deucher 	/* update display watermarks based on new power state */
764da321c8aSAlex Deucher 	radeon_bandwidth_update(rdev);
765da321c8aSAlex Deucher 	/* update displays */
766da321c8aSAlex Deucher 	radeon_dpm_display_configuration_changed(rdev);
767da321c8aSAlex Deucher 
768da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
769da321c8aSAlex Deucher 	rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
770da321c8aSAlex Deucher 
771da321c8aSAlex Deucher 	/* wait for the rings to drain */
772da321c8aSAlex Deucher 	for (i = 0; i < RADEON_NUM_RINGS; i++) {
773da321c8aSAlex Deucher 		struct radeon_ring *ring = &rdev->ring[i];
774da321c8aSAlex Deucher 		if (ring->ready)
775da321c8aSAlex Deucher 			radeon_fence_wait_empty_locked(rdev, i);
776da321c8aSAlex Deucher 	}
777da321c8aSAlex Deucher 
778da321c8aSAlex Deucher 	/* program the new power state */
779da321c8aSAlex Deucher 	radeon_dpm_set_power_state(rdev);
780da321c8aSAlex Deucher 
781da321c8aSAlex Deucher 	/* update current power state */
782da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
783da321c8aSAlex Deucher 
78484dd1928SAlex Deucher 	radeon_dpm_post_set_power_state(rdev);
78584dd1928SAlex Deucher 
78684dd1928SAlex Deucher done:
787da321c8aSAlex Deucher 	mutex_unlock(&rdev->ring_lock);
788da321c8aSAlex Deucher 	up_write(&rdev->pm.mclk_lock);
789da321c8aSAlex Deucher 	mutex_unlock(&rdev->ddev->struct_mutex);
790da321c8aSAlex Deucher }
791da321c8aSAlex Deucher 
792da321c8aSAlex Deucher void radeon_dpm_enable_power_state(struct radeon_device *rdev,
793da321c8aSAlex Deucher 				   enum radeon_pm_state_type dpm_state)
794da321c8aSAlex Deucher {
795da321c8aSAlex Deucher 	if (!rdev->pm.dpm_enabled)
796da321c8aSAlex Deucher 		return;
797da321c8aSAlex Deucher 
798da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
799da321c8aSAlex Deucher 	switch (dpm_state) {
800da321c8aSAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_THERMAL:
801da321c8aSAlex Deucher 		rdev->pm.dpm.thermal_active = true;
802da321c8aSAlex Deucher 		break;
8038a227555SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD:
8048a227555SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_SD:
8058a227555SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD:
8068a227555SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
8078a227555SAlex Deucher 	case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
8088a227555SAlex Deucher 		rdev->pm.dpm.uvd_active = true;
8098a227555SAlex Deucher 		break;
810da321c8aSAlex Deucher 	default:
811da321c8aSAlex Deucher 		rdev->pm.dpm.thermal_active = false;
8128a227555SAlex Deucher 		rdev->pm.dpm.uvd_active = false;
813da321c8aSAlex Deucher 		break;
814da321c8aSAlex Deucher 	}
815da321c8aSAlex Deucher 	rdev->pm.dpm.state = dpm_state;
816da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
817da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
818da321c8aSAlex Deucher }
819da321c8aSAlex Deucher 
820da321c8aSAlex Deucher static void radeon_pm_suspend_old(struct radeon_device *rdev)
821ce8f5370SAlex Deucher {
822ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
8233f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
8243f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
8253f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
8263f53eb6fSRafael J. Wysocki 	}
827ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
82832c87fcaSTejun Heo 
82932c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
830ce8f5370SAlex Deucher }
831ce8f5370SAlex Deucher 
832da321c8aSAlex Deucher static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
833da321c8aSAlex Deucher {
834da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
835da321c8aSAlex Deucher 	/* disable dpm */
836da321c8aSAlex Deucher 	radeon_dpm_disable(rdev);
837da321c8aSAlex Deucher 	/* reset the power state */
838da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
839da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = false;
840da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
841da321c8aSAlex Deucher }
842da321c8aSAlex Deucher 
843da321c8aSAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
844da321c8aSAlex Deucher {
845da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
846da321c8aSAlex Deucher 		radeon_pm_suspend_dpm(rdev);
847da321c8aSAlex Deucher 	else
848da321c8aSAlex Deucher 		radeon_pm_suspend_old(rdev);
849da321c8aSAlex Deucher }
850da321c8aSAlex Deucher 
851da321c8aSAlex Deucher static void radeon_pm_resume_old(struct radeon_device *rdev)
852ce8f5370SAlex Deucher {
853ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
8542e3b3b10SAlex Deucher 	if ((rdev->family >= CHIP_BARTS) &&
8552e3b3b10SAlex Deucher 	    (rdev->family <= CHIP_CAYMAN) &&
8562e3b3b10SAlex Deucher 	    rdev->mc_fw) {
857ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
8588a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
8598a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
8602feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
8612feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
8622feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
863ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
864ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
865ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
866ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
867ed18a360SAlex Deucher 	}
868f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
869f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
870f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
871f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
8729ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
8739ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
8744d60173fSAlex Deucher 	rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
8752feea49aSAlex Deucher 	rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
8763f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
8773f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
8783f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
87932c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
8803f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
8813f53eb6fSRafael J. Wysocki 	}
882f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
883ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
884d0d6cb81SRafał Miłecki }
885d0d6cb81SRafał Miłecki 
886da321c8aSAlex Deucher static void radeon_pm_resume_dpm(struct radeon_device *rdev)
8877433874eSRafał Miłecki {
88826481fb1SDave Airlie 	int ret;
8890d18abedSDan Carpenter 
890da321c8aSAlex Deucher 	/* asic init will reset to the boot state */
891da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
892da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
893da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
894da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
895da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
896da321c8aSAlex Deucher 	if (ret) {
897da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm resume failed\n");
898da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
899da321c8aSAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
900da321c8aSAlex Deucher 		    rdev->mc_fw) {
901da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
902da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
903da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
904da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
905da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
906da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
907da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
908da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
909da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
910da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
911da321c8aSAlex Deucher 		}
912da321c8aSAlex Deucher 	} else {
913da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = true;
914da321c8aSAlex Deucher 		radeon_pm_compute_clocks(rdev);
915da321c8aSAlex Deucher 	}
916da321c8aSAlex Deucher }
917da321c8aSAlex Deucher 
918da321c8aSAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
919da321c8aSAlex Deucher {
920da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
921da321c8aSAlex Deucher 		radeon_pm_resume_dpm(rdev);
922da321c8aSAlex Deucher 	else
923da321c8aSAlex Deucher 		radeon_pm_resume_old(rdev);
924da321c8aSAlex Deucher }
925da321c8aSAlex Deucher 
926da321c8aSAlex Deucher static int radeon_pm_init_old(struct radeon_device *rdev)
927da321c8aSAlex Deucher {
928da321c8aSAlex Deucher 	int ret;
929da321c8aSAlex Deucher 
930f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
931ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
932ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
933ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
934ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
9359ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
9369ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
937f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
938f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
93921a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
940c913e23aSRafał Miłecki 
94156278a8eSAlex Deucher 	if (rdev->bios) {
94256278a8eSAlex Deucher 		if (rdev->is_atom_bios)
94356278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
94456278a8eSAlex Deucher 		else
94556278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
946f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
947ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
948ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
9492e3b3b10SAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
9502e3b3b10SAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
9512e3b3b10SAlex Deucher 		    rdev->mc_fw) {
952ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
9538a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
9548a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
9554639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
9564639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
9574639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
958ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
959ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
960ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
961ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
962ed18a360SAlex Deucher 		}
96356278a8eSAlex Deucher 	}
96456278a8eSAlex Deucher 
96521a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
9660d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
9670d18abedSDan Carpenter 	if (ret)
9680d18abedSDan Carpenter 		return ret;
96932c87fcaSTejun Heo 
97032c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
97132c87fcaSTejun Heo 
972ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
973ce8f5370SAlex Deucher 		/* where's the best place to put these? */
97426481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
97526481fb1SDave Airlie 		if (ret)
97626481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
97726481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
97826481fb1SDave Airlie 		if (ret)
97926481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
980ce8f5370SAlex Deucher 
9817433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
982c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
9837433874eSRafał Miłecki 		}
9847433874eSRafał Miłecki 
985c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
986ce8f5370SAlex Deucher 	}
987c913e23aSRafał Miłecki 
9887433874eSRafał Miłecki 	return 0;
9897433874eSRafał Miłecki }
9907433874eSRafał Miłecki 
991da321c8aSAlex Deucher static void radeon_dpm_print_power_states(struct radeon_device *rdev)
992da321c8aSAlex Deucher {
993da321c8aSAlex Deucher 	int i;
994da321c8aSAlex Deucher 
995da321c8aSAlex Deucher 	for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
996da321c8aSAlex Deucher 		printk("== power state %d ==\n", i);
997da321c8aSAlex Deucher 		radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
998da321c8aSAlex Deucher 	}
999da321c8aSAlex Deucher }
1000da321c8aSAlex Deucher 
1001da321c8aSAlex Deucher static int radeon_pm_init_dpm(struct radeon_device *rdev)
1002da321c8aSAlex Deucher {
1003da321c8aSAlex Deucher 	int ret;
1004da321c8aSAlex Deucher 
1005da321c8aSAlex Deucher 	/* default to performance state */
1006da321c8aSAlex Deucher 	rdev->pm.dpm.state = POWER_STATE_TYPE_PERFORMANCE;
1007da321c8aSAlex Deucher 	rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
1008da321c8aSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
1009da321c8aSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
1010da321c8aSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
1011da321c8aSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
1012da321c8aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1013da321c8aSAlex Deucher 
1014da321c8aSAlex Deucher 	if (rdev->bios && rdev->is_atom_bios)
1015da321c8aSAlex Deucher 		radeon_atombios_get_power_modes(rdev);
1016da321c8aSAlex Deucher 	else
1017da321c8aSAlex Deucher 		return -EINVAL;
1018da321c8aSAlex Deucher 
1019da321c8aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
1020da321c8aSAlex Deucher 	ret = radeon_hwmon_init(rdev);
1021da321c8aSAlex Deucher 	if (ret)
1022da321c8aSAlex Deucher 		return ret;
1023da321c8aSAlex Deucher 
1024da321c8aSAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1025da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1026da321c8aSAlex Deucher 	radeon_dpm_init(rdev);
1027da321c8aSAlex Deucher 	rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1028da321c8aSAlex Deucher 	radeon_dpm_print_power_states(rdev);
1029da321c8aSAlex Deucher 	radeon_dpm_setup_asic(rdev);
1030da321c8aSAlex Deucher 	ret = radeon_dpm_enable(rdev);
1031da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
1032da321c8aSAlex Deucher 	if (ret) {
1033da321c8aSAlex Deucher 		rdev->pm.dpm_enabled = false;
1034da321c8aSAlex Deucher 		if ((rdev->family >= CHIP_BARTS) &&
1035da321c8aSAlex Deucher 		    (rdev->family <= CHIP_CAYMAN) &&
1036da321c8aSAlex Deucher 		    rdev->mc_fw) {
1037da321c8aSAlex Deucher 			if (rdev->pm.default_vddc)
1038da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1039da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
1040da321c8aSAlex Deucher 			if (rdev->pm.default_vddci)
1041da321c8aSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1042da321c8aSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
1043da321c8aSAlex Deucher 			if (rdev->pm.default_sclk)
1044da321c8aSAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1045da321c8aSAlex Deucher 			if (rdev->pm.default_mclk)
1046da321c8aSAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1047da321c8aSAlex Deucher 		}
1048da321c8aSAlex Deucher 		DRM_ERROR("radeon: dpm initialization failed\n");
1049da321c8aSAlex Deucher 		return ret;
1050da321c8aSAlex Deucher 	}
1051da321c8aSAlex Deucher 	rdev->pm.dpm_enabled = true;
1052da321c8aSAlex Deucher 	radeon_pm_compute_clocks(rdev);
1053da321c8aSAlex Deucher 
1054da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1055da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1056da321c8aSAlex Deucher 		if (ret)
1057da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for dpm state\n");
1058da321c8aSAlex Deucher 		/* XXX: these are noops for dpm but are here for backwards compat */
1059da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1060da321c8aSAlex Deucher 		if (ret)
1061da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for power profile\n");
1062da321c8aSAlex Deucher 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
1063da321c8aSAlex Deucher 		if (ret)
1064da321c8aSAlex Deucher 			DRM_ERROR("failed to create device file for power method\n");
1065da321c8aSAlex Deucher 		DRM_INFO("radeon: dpm initialized\n");
1066da321c8aSAlex Deucher 	}
1067da321c8aSAlex Deucher 
1068da321c8aSAlex Deucher 	return 0;
1069da321c8aSAlex Deucher }
1070da321c8aSAlex Deucher 
1071da321c8aSAlex Deucher int radeon_pm_init(struct radeon_device *rdev)
1072da321c8aSAlex Deucher {
1073da321c8aSAlex Deucher 	/* enable dpm on rv6xx+ */
1074da321c8aSAlex Deucher 	switch (rdev->family) {
10754a6369e9SAlex Deucher 	case CHIP_RV610:
10764a6369e9SAlex Deucher 	case CHIP_RV630:
10774a6369e9SAlex Deucher 	case CHIP_RV620:
10784a6369e9SAlex Deucher 	case CHIP_RV635:
10794a6369e9SAlex Deucher 	case CHIP_RV670:
10809d67006eSAlex Deucher 	case CHIP_RS780:
10819d67006eSAlex Deucher 	case CHIP_RS880:
108266229b20SAlex Deucher 	case CHIP_RV770:
108366229b20SAlex Deucher 	case CHIP_RV730:
108466229b20SAlex Deucher 	case CHIP_RV710:
108566229b20SAlex Deucher 	case CHIP_RV740:
1086dc50ba7fSAlex Deucher 	case CHIP_CEDAR:
1087dc50ba7fSAlex Deucher 	case CHIP_REDWOOD:
1088dc50ba7fSAlex Deucher 	case CHIP_JUNIPER:
1089dc50ba7fSAlex Deucher 	case CHIP_CYPRESS:
1090dc50ba7fSAlex Deucher 	case CHIP_HEMLOCK:
109180ea2c12SAlex Deucher 	case CHIP_PALM:
109280ea2c12SAlex Deucher 	case CHIP_SUMO:
109380ea2c12SAlex Deucher 	case CHIP_SUMO2:
10946596afd4SAlex Deucher 	case CHIP_BARTS:
10956596afd4SAlex Deucher 	case CHIP_TURKS:
10966596afd4SAlex Deucher 	case CHIP_CAICOS:
109769e0b57aSAlex Deucher 	case CHIP_CAYMAN:
1098d70229f7SAlex Deucher 	case CHIP_ARUBA:
10999d67006eSAlex Deucher 		if (radeon_dpm == 1)
11009d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_DPM;
11019d67006eSAlex Deucher 		else
11029d67006eSAlex Deucher 			rdev->pm.pm_method = PM_METHOD_PROFILE;
11039d67006eSAlex Deucher 		break;
1104da321c8aSAlex Deucher 	default:
1105da321c8aSAlex Deucher 		/* default to profile method */
1106da321c8aSAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
1107da321c8aSAlex Deucher 		break;
1108da321c8aSAlex Deucher 	}
1109da321c8aSAlex Deucher 
1110da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1111da321c8aSAlex Deucher 		return radeon_pm_init_dpm(rdev);
1112da321c8aSAlex Deucher 	else
1113da321c8aSAlex Deucher 		return radeon_pm_init_old(rdev);
1114da321c8aSAlex Deucher }
1115da321c8aSAlex Deucher 
1116da321c8aSAlex Deucher static void radeon_pm_fini_old(struct radeon_device *rdev)
111729fb52caSAlex Deucher {
1118ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1119a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1120ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1121ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
1122ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
1123ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1124ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1125ce8f5370SAlex Deucher 			/* reset default clocks */
1126ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1127ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1128ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
112958e21dffSAlex Deucher 		}
1130ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
113132c87fcaSTejun Heo 
113232c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
113358e21dffSAlex Deucher 
1134ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1135ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1136ce8f5370SAlex Deucher 	}
1137a424816fSAlex Deucher 
11380975b162SAlex Deucher 	if (rdev->pm.power_state)
11390975b162SAlex Deucher 		kfree(rdev->pm.power_state);
11400975b162SAlex Deucher 
114121a8122aSAlex Deucher 	radeon_hwmon_fini(rdev);
114229fb52caSAlex Deucher }
114329fb52caSAlex Deucher 
1144da321c8aSAlex Deucher static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1145da321c8aSAlex Deucher {
1146da321c8aSAlex Deucher 	if (rdev->pm.num_power_states > 1) {
1147da321c8aSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
1148da321c8aSAlex Deucher 		radeon_dpm_disable(rdev);
1149da321c8aSAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
1150da321c8aSAlex Deucher 
1151da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
1152da321c8aSAlex Deucher 		/* XXX backwards compat */
1153da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
1154da321c8aSAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
1155da321c8aSAlex Deucher 	}
1156da321c8aSAlex Deucher 	radeon_dpm_fini(rdev);
1157da321c8aSAlex Deucher 
1158da321c8aSAlex Deucher 	if (rdev->pm.power_state)
1159da321c8aSAlex Deucher 		kfree(rdev->pm.power_state);
1160da321c8aSAlex Deucher 
1161da321c8aSAlex Deucher 	radeon_hwmon_fini(rdev);
1162da321c8aSAlex Deucher }
1163da321c8aSAlex Deucher 
1164da321c8aSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
1165da321c8aSAlex Deucher {
1166da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1167da321c8aSAlex Deucher 		radeon_pm_fini_dpm(rdev);
1168da321c8aSAlex Deucher 	else
1169da321c8aSAlex Deucher 		radeon_pm_fini_old(rdev);
1170da321c8aSAlex Deucher }
1171da321c8aSAlex Deucher 
1172da321c8aSAlex Deucher static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
1173c913e23aSRafał Miłecki {
1174c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
1175a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
1176c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
1177c913e23aSRafał Miłecki 
1178ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
1179ce8f5370SAlex Deucher 		return;
1180ce8f5370SAlex Deucher 
11814a6369e9SAlex Deucher 	INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1182c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1183c913e23aSRafał Miłecki 
1184c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
1185a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
1186a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
1187a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1188a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1189a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
1190c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1191a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
1192c913e23aSRafał Miłecki 		}
1193c913e23aSRafał Miłecki 	}
1194c913e23aSRafał Miłecki 
1195ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1196ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
1197ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
1198ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1199ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1200a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
1201ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1202ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1203c913e23aSRafał Miłecki 
1204ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1205ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1206ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1207ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1208c913e23aSRafał Miłecki 
1209d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
1210c913e23aSRafał Miłecki 				}
1211a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
1212c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
1213c913e23aSRafał Miłecki 
1214ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1215ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1216ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1217ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1218ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1219c913e23aSRafał Miłecki 
122032c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1221c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1222ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1223ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
122432c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1225c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1226d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
1227c913e23aSRafał Miłecki 				}
1228a48b9b4eSAlex Deucher 			} else { /* count == 0 */
1229ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1230ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
1231c913e23aSRafał Miłecki 
1232ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1233ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1234ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
1235ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
1236ce8f5370SAlex Deucher 				}
1237ce8f5370SAlex Deucher 			}
123873a6d3fcSRafał Miłecki 		}
1239c913e23aSRafał Miłecki 	}
1240c913e23aSRafał Miłecki 
1241c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
1242c913e23aSRafał Miłecki }
1243c913e23aSRafał Miłecki 
1244da321c8aSAlex Deucher static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1245da321c8aSAlex Deucher {
1246da321c8aSAlex Deucher 	struct drm_device *ddev = rdev->ddev;
1247da321c8aSAlex Deucher 	struct drm_crtc *crtc;
1248da321c8aSAlex Deucher 	struct radeon_crtc *radeon_crtc;
1249da321c8aSAlex Deucher 
1250da321c8aSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
1251da321c8aSAlex Deucher 
12525ca302f7SAlex Deucher 	/* update active crtc counts */
1253da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtcs = 0;
1254da321c8aSAlex Deucher 	rdev->pm.dpm.new_active_crtc_count = 0;
1255da321c8aSAlex Deucher 	list_for_each_entry(crtc,
1256da321c8aSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
1257da321c8aSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
1258da321c8aSAlex Deucher 		if (crtc->enabled) {
1259da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1260da321c8aSAlex Deucher 			rdev->pm.dpm.new_active_crtc_count++;
1261da321c8aSAlex Deucher 		}
1262da321c8aSAlex Deucher 	}
1263da321c8aSAlex Deucher 
12645ca302f7SAlex Deucher 	/* update battery/ac status */
12655ca302f7SAlex Deucher 	if (power_supply_is_system_supplied() > 0)
12665ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = true;
12675ca302f7SAlex Deucher 	else
12685ca302f7SAlex Deucher 		rdev->pm.dpm.ac_power = false;
12695ca302f7SAlex Deucher 
1270da321c8aSAlex Deucher 	radeon_dpm_change_power_state_locked(rdev);
1271da321c8aSAlex Deucher 
1272da321c8aSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
12738a227555SAlex Deucher 
1274da321c8aSAlex Deucher }
1275da321c8aSAlex Deucher 
1276da321c8aSAlex Deucher void radeon_pm_compute_clocks(struct radeon_device *rdev)
1277da321c8aSAlex Deucher {
1278da321c8aSAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_DPM)
1279da321c8aSAlex Deucher 		radeon_pm_compute_clocks_dpm(rdev);
1280da321c8aSAlex Deucher 	else
1281da321c8aSAlex Deucher 		radeon_pm_compute_clocks_old(rdev);
1282da321c8aSAlex Deucher }
1283da321c8aSAlex Deucher 
1284ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
1285f735261bSDave Airlie {
128675fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
1287f735261bSDave Airlie 	bool in_vbl = true;
1288f735261bSDave Airlie 
128975fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
129075fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
129175fa0b08SMario Kleiner 	 */
129275fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
129375fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
1294f5a80209SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
1295f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1296f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
1297f735261bSDave Airlie 				in_vbl = false;
1298f735261bSDave Airlie 		}
1299f735261bSDave Airlie 	}
1300f81f2024SMatthew Garrett 
1301f81f2024SMatthew Garrett 	return in_vbl;
1302f81f2024SMatthew Garrett }
1303f81f2024SMatthew Garrett 
1304ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
1305f81f2024SMatthew Garrett {
1306f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
1307f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
1308f81f2024SMatthew Garrett 
1309f735261bSDave Airlie 	if (in_vbl == false)
1310d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
1311bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
1312f735261bSDave Airlie 	return in_vbl;
1313f735261bSDave Airlie }
1314c913e23aSRafał Miłecki 
1315ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
1316c913e23aSRafał Miłecki {
1317c913e23aSRafał Miłecki 	struct radeon_device *rdev;
1318d9932a32SMatthew Garrett 	int resched;
1319c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
1320ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
1321c913e23aSRafał Miłecki 
1322d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
1323c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
1324ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1325c913e23aSRafał Miłecki 		int not_processed = 0;
13267465280cSAlex Deucher 		int i;
1327c913e23aSRafał Miłecki 
13287465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
13290ec0612aSAlex Deucher 			struct radeon_ring *ring = &rdev->ring[i];
13300ec0612aSAlex Deucher 
13310ec0612aSAlex Deucher 			if (ring->ready) {
133247492a23SChristian König 				not_processed += radeon_fence_count_emitted(rdev, i);
13337465280cSAlex Deucher 				if (not_processed >= 3)
13347465280cSAlex Deucher 					break;
13357465280cSAlex Deucher 			}
13360ec0612aSAlex Deucher 		}
1337c913e23aSRafał Miłecki 
1338c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
1339ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1340ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1341ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1342ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
1343ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1344ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
1345ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1346c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1347c913e23aSRafał Miłecki 			}
1348c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
1349ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1350ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1351ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1352ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
1353ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
1354ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
1355ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
1356c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1357c913e23aSRafał Miłecki 			}
1358c913e23aSRafał Miłecki 		}
1359c913e23aSRafał Miłecki 
1360d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
1361d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
1362d7311171SAlex Deucher 		 */
1363ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1364ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
1365ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
1366ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
1367c913e23aSRafał Miłecki 		}
1368c913e23aSRafał Miłecki 
136932c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1370c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
1371c913e23aSRafał Miłecki 	}
13723f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
13733f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
13743f53eb6fSRafael J. Wysocki }
1375c913e23aSRafał Miłecki 
13767433874eSRafał Miłecki /*
13777433874eSRafał Miłecki  * Debugfs info
13787433874eSRafał Miłecki  */
13797433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
13807433874eSRafał Miłecki 
13817433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
13827433874eSRafał Miłecki {
13837433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
13847433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
13857433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
13867433874eSRafał Miłecki 
13879ace9f7bSAlex Deucher 	seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1388bf05d998SAlex Deucher 	/* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1389bf05d998SAlex Deucher 	if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1390bf05d998SAlex Deucher 		seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1391bf05d998SAlex Deucher 	else
13926234077dSRafał Miłecki 		seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
13939ace9f7bSAlex Deucher 	seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1394798bcf73SAlex Deucher 	if (rdev->asic->pm.get_memory_clock)
13956234077dSRafał Miłecki 		seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
13960fcbe947SRafał Miłecki 	if (rdev->pm.current_vddc)
13970fcbe947SRafał Miłecki 		seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1398798bcf73SAlex Deucher 	if (rdev->asic->pm.get_pcie_lanes)
1399aa5120d2SRafał Miłecki 		seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
14007433874eSRafał Miłecki 
14017433874eSRafał Miłecki 	return 0;
14027433874eSRafał Miłecki }
14037433874eSRafał Miłecki 
14047433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
14057433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
14067433874eSRafał Miłecki };
14077433874eSRafał Miłecki #endif
14087433874eSRafał Miłecki 
1409c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
14107433874eSRafał Miłecki {
14117433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
14127433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
14137433874eSRafał Miłecki #else
14147433874eSRafał Miłecki 	return 0;
14157433874eSRafał Miłecki #endif
14167433874eSRafał Miłecki }
1417