xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 7465280c076d6440e5908c158c83b542dc063a30)
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  */
237433874eSRafał Miłecki #include "drmP.h"
247433874eSRafał Miłecki #include "radeon.h"
25f735261bSDave Airlie #include "avivod.h"
268a83ec5eSAlex Deucher #include "atom.h"
27ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
28ce8f5370SAlex Deucher #include <linux/acpi.h>
29ce8f5370SAlex Deucher #endif
30ce8f5370SAlex Deucher #include <linux/power_supply.h>
3121a8122aSAlex Deucher #include <linux/hwmon.h>
3221a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
337433874eSRafał Miłecki 
34c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
35c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3673a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
372031f77cSAlex Deucher #define RADEON_WAIT_IDLE_TIMEOUT 200
38c913e23aSRafał Miłecki 
39f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
40f712d0c7SRafał Miłecki 	"Default",
41f712d0c7SRafał Miłecki 	"Powersave",
42f712d0c7SRafał Miłecki 	"Battery",
43f712d0c7SRafał Miłecki 	"Balanced",
44f712d0c7SRafał Miłecki 	"Performance",
45f712d0c7SRafał Miłecki };
46f712d0c7SRafał Miłecki 
47ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
48c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
49ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
50ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
51ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
52ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
53ce8f5370SAlex Deucher 
54ce8f5370SAlex Deucher #define ACPI_AC_CLASS           "ac_adapter"
55ce8f5370SAlex Deucher 
56a4c9e2eeSAlex Deucher int radeon_pm_get_type_index(struct radeon_device *rdev,
57a4c9e2eeSAlex Deucher 			     enum radeon_pm_state_type ps_type,
58a4c9e2eeSAlex Deucher 			     int instance)
59a4c9e2eeSAlex Deucher {
60a4c9e2eeSAlex Deucher 	int i;
61a4c9e2eeSAlex Deucher 	int found_instance = -1;
62a4c9e2eeSAlex Deucher 
63a4c9e2eeSAlex Deucher 	for (i = 0; i < rdev->pm.num_power_states; i++) {
64a4c9e2eeSAlex Deucher 		if (rdev->pm.power_state[i].type == ps_type) {
65a4c9e2eeSAlex Deucher 			found_instance++;
66a4c9e2eeSAlex Deucher 			if (found_instance == instance)
67a4c9e2eeSAlex Deucher 				return i;
68a4c9e2eeSAlex Deucher 		}
69a4c9e2eeSAlex Deucher 	}
70a4c9e2eeSAlex Deucher 	/* return default if no match */
71a4c9e2eeSAlex Deucher 	return rdev->pm.default_power_state_index;
72a4c9e2eeSAlex Deucher }
73a4c9e2eeSAlex Deucher 
74ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
75ce8f5370SAlex Deucher static int radeon_acpi_event(struct notifier_block *nb,
76ce8f5370SAlex Deucher 			     unsigned long val,
77ce8f5370SAlex Deucher 			     void *data)
78ce8f5370SAlex Deucher {
79ce8f5370SAlex Deucher 	struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
80ce8f5370SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
81ce8f5370SAlex Deucher 
82ce8f5370SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
83ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
84d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("pm: AC\n");
85ce8f5370SAlex Deucher 		else
86d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("pm: DC\n");
87ce8f5370SAlex Deucher 
88ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
89ce8f5370SAlex Deucher 			if (rdev->pm.profile == PM_PROFILE_AUTO) {
90ce8f5370SAlex Deucher 				mutex_lock(&rdev->pm.mutex);
91ce8f5370SAlex Deucher 				radeon_pm_update_profile(rdev);
92ce8f5370SAlex Deucher 				radeon_pm_set_clocks(rdev);
93ce8f5370SAlex Deucher 				mutex_unlock(&rdev->pm.mutex);
94ce8f5370SAlex Deucher 			}
95ce8f5370SAlex Deucher 		}
96ce8f5370SAlex Deucher 	}
97ce8f5370SAlex Deucher 
98ce8f5370SAlex Deucher 	return NOTIFY_OK;
99ce8f5370SAlex Deucher }
100ce8f5370SAlex Deucher #endif
101ce8f5370SAlex Deucher 
102ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
103ce8f5370SAlex Deucher {
104ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
105ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
106ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
107ce8f5370SAlex Deucher 		break;
108ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
109ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
110ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
111ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
112ce8f5370SAlex Deucher 			else
113ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
114ce8f5370SAlex Deucher 		} else {
115ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
116c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
117ce8f5370SAlex Deucher 			else
118c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
119ce8f5370SAlex Deucher 		}
120ce8f5370SAlex Deucher 		break;
121ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
122ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
123ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
124ce8f5370SAlex Deucher 		else
125ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
126ce8f5370SAlex Deucher 		break;
127c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
128c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
129c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
130c9e75b21SAlex Deucher 		else
131c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
132c9e75b21SAlex Deucher 		break;
133ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
134ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
135ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
136ce8f5370SAlex Deucher 		else
137ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
138ce8f5370SAlex Deucher 		break;
139ce8f5370SAlex Deucher 	}
140ce8f5370SAlex Deucher 
141ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
142ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
143ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
144ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
145ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
146ce8f5370SAlex Deucher 	} else {
147ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
148ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
149ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
150ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
151ce8f5370SAlex Deucher 	}
152ce8f5370SAlex Deucher }
153c913e23aSRafał Miłecki 
1545876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1555876dd24SMatthew Garrett {
1565876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1575876dd24SMatthew Garrett 
1585876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1595876dd24SMatthew Garrett 		return;
1605876dd24SMatthew Garrett 
1615876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1625876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1635876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1645876dd24SMatthew Garrett 	}
1655876dd24SMatthew Garrett }
1665876dd24SMatthew Garrett 
167ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
168ce8f5370SAlex Deucher {
169ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
170ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
171ce8f5370SAlex Deucher 		wait_event_timeout(
172ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
173ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
174ce8f5370SAlex Deucher 	}
175ce8f5370SAlex Deucher }
176ce8f5370SAlex Deucher 
177ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
178ce8f5370SAlex Deucher {
179ce8f5370SAlex Deucher 	u32 sclk, mclk;
18092645879SAlex Deucher 	bool misc_after = false;
181ce8f5370SAlex Deucher 
182ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
183ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
184ce8f5370SAlex Deucher 		return;
185ce8f5370SAlex Deucher 
186ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
187ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
188ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1899ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1909ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
191ce8f5370SAlex Deucher 
192ce8f5370SAlex Deucher 		mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
193ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].mclk;
1949ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1959ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
196ce8f5370SAlex Deucher 
19792645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
19892645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
19992645879SAlex Deucher 			misc_after = true;
20092645879SAlex Deucher 
20192645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
20292645879SAlex Deucher 
20392645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
20492645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
20592645879SAlex Deucher 				return;
20692645879SAlex Deucher 		}
20792645879SAlex Deucher 
20892645879SAlex Deucher 		radeon_pm_prepare(rdev);
20992645879SAlex Deucher 
21092645879SAlex Deucher 		if (!misc_after)
211ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
212ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
213ce8f5370SAlex Deucher 
214ce8f5370SAlex Deucher 		/* set engine clock */
215ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
216ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
217ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
218ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
219ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
220d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
221ce8f5370SAlex Deucher 		}
222ce8f5370SAlex Deucher 
223ce8f5370SAlex Deucher 		/* set memory clock */
224ce8f5370SAlex Deucher 		if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
225ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
226ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
227ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
228ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
229d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
230ce8f5370SAlex Deucher 		}
23192645879SAlex Deucher 
23292645879SAlex Deucher 		if (misc_after)
23392645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
23492645879SAlex Deucher 			radeon_pm_misc(rdev);
23592645879SAlex Deucher 
236ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
237ce8f5370SAlex Deucher 
238ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
239ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
240ce8f5370SAlex Deucher 	} else
241d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
242ce8f5370SAlex Deucher }
243ce8f5370SAlex Deucher 
244ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
245a424816fSAlex Deucher {
2462aba631cSMatthew Garrett 	int i;
2472aba631cSMatthew Garrett 
2484e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2494e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2504e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2514e186b2dSAlex Deucher 		return;
2524e186b2dSAlex Deucher 
253612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
254612e06ceSMatthew Garrett 	mutex_lock(&rdev->vram_mutex);
255a424816fSAlex Deucher 	mutex_lock(&rdev->cp.mutex);
2564f3218cbSAlex Deucher 
2574f3218cbSAlex Deucher 	/* gui idle int has issues on older chips it seems */
2584f3218cbSAlex Deucher 	if (rdev->family >= CHIP_R600) {
259ce8f5370SAlex Deucher 		if (rdev->irq.installed) {
260a424816fSAlex Deucher 			/* wait for GPU idle */
261a424816fSAlex Deucher 			rdev->pm.gui_idle = false;
262a424816fSAlex Deucher 			rdev->irq.gui_idle = true;
263a424816fSAlex Deucher 			radeon_irq_set(rdev);
264a424816fSAlex Deucher 			wait_event_interruptible_timeout(
265a424816fSAlex Deucher 				rdev->irq.idle_queue, rdev->pm.gui_idle,
266a424816fSAlex Deucher 				msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
267a424816fSAlex Deucher 			rdev->irq.gui_idle = false;
268a424816fSAlex Deucher 			radeon_irq_set(rdev);
269ce8f5370SAlex Deucher 		}
27001434b4bSMatthew Garrett 	} else {
271ce8f5370SAlex Deucher 		if (rdev->cp.ready) {
27201434b4bSMatthew Garrett 			struct radeon_fence *fence;
27301434b4bSMatthew Garrett 			radeon_ring_alloc(rdev, 64);
274*7465280cSAlex Deucher 			radeon_fence_create(rdev, &fence, RADEON_RING_TYPE_GFX_INDEX);
27501434b4bSMatthew Garrett 			radeon_fence_emit(rdev, fence);
27601434b4bSMatthew Garrett 			radeon_ring_commit(rdev);
27701434b4bSMatthew Garrett 			radeon_fence_wait(fence, false);
27801434b4bSMatthew Garrett 			radeon_fence_unref(&fence);
2794f3218cbSAlex Deucher 		}
280ce8f5370SAlex Deucher 	}
2815876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2825876dd24SMatthew Garrett 
283ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2842aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2852aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2862aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2872aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2882aba631cSMatthew Garrett 			}
2892aba631cSMatthew Garrett 		}
2902aba631cSMatthew Garrett 	}
2912aba631cSMatthew Garrett 
292ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2932aba631cSMatthew Garrett 
294ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2952aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2962aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2972aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2982aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2992aba631cSMatthew Garrett 			}
3002aba631cSMatthew Garrett 		}
3012aba631cSMatthew Garrett 	}
302a424816fSAlex Deucher 
303a424816fSAlex Deucher 	/* update display watermarks based on new power state */
304a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
305a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
306a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
307a424816fSAlex Deucher 
308ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
3092aba631cSMatthew Garrett 
310a424816fSAlex Deucher 	mutex_unlock(&rdev->cp.mutex);
311612e06ceSMatthew Garrett 	mutex_unlock(&rdev->vram_mutex);
312612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
313a424816fSAlex Deucher }
314a424816fSAlex Deucher 
315f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
316f712d0c7SRafał Miłecki {
317f712d0c7SRafał Miłecki 	int i, j;
318f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
319f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
320f712d0c7SRafał Miłecki 
321d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
322f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
323f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
324d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
325f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
326f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
327d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
328f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
329d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
330f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
331d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
332d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
333f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
334f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
335f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
336d9fdaafbSDave Airlie 				DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
337f712d0c7SRafał Miłecki 					j,
338f712d0c7SRafał Miłecki 					clock_info->sclk * 10,
339f712d0c7SRafał Miłecki 					clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
340f712d0c7SRafał Miłecki 			else
341d9fdaafbSDave Airlie 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
342f712d0c7SRafał Miłecki 					j,
343f712d0c7SRafał Miłecki 					clock_info->sclk * 10,
344f712d0c7SRafał Miłecki 					clock_info->mclk * 10,
345f712d0c7SRafał Miłecki 					clock_info->voltage.voltage,
346f712d0c7SRafał Miłecki 					clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
347f712d0c7SRafał Miłecki 		}
348f712d0c7SRafał Miłecki 	}
349f712d0c7SRafał Miłecki }
350f712d0c7SRafał Miłecki 
351ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
352a424816fSAlex Deucher 				     struct device_attribute *attr,
353a424816fSAlex Deucher 				     char *buf)
354a424816fSAlex Deucher {
355a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
356a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
357ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
358a424816fSAlex Deucher 
359a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
360ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
361ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
36212e27be8SDaniel J Blueman 			(cp == PM_PROFILE_MID) ? "mid" :
363ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
364a424816fSAlex Deucher }
365a424816fSAlex Deucher 
366ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
367a424816fSAlex Deucher 				     struct device_attribute *attr,
368a424816fSAlex Deucher 				     const char *buf,
369a424816fSAlex Deucher 				     size_t count)
370a424816fSAlex Deucher {
371a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
372a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
373a424816fSAlex Deucher 
374a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
375ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
376ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
377ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
378ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
379ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
380ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
381ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
382c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
383c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
384ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
385ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
386ce8f5370SAlex Deucher 		else {
3871783e4bfSThomas Renninger 			count = -EINVAL;
388ce8f5370SAlex Deucher 			goto fail;
389ce8f5370SAlex Deucher 		}
390ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
391ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
3921783e4bfSThomas Renninger 	} else
3931783e4bfSThomas Renninger 		count = -EINVAL;
3941783e4bfSThomas Renninger 
395ce8f5370SAlex Deucher fail:
396a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
397a424816fSAlex Deucher 
398a424816fSAlex Deucher 	return count;
399a424816fSAlex Deucher }
400a424816fSAlex Deucher 
401ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
402ce8f5370SAlex Deucher 				    struct device_attribute *attr,
403ce8f5370SAlex Deucher 				    char *buf)
40456278a8eSAlex Deucher {
405ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
406ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
407ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
40856278a8eSAlex Deucher 
409ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
410ce8f5370SAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
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 {
418ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
419ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
420ce8f5370SAlex Deucher 
421ce8f5370SAlex Deucher 
422ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
423ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
424ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
425ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
426ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
427ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
428ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
429ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
430ce8f5370SAlex Deucher 		/* disable dynpm */
431ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
432ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4333f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
434ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
43532c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
436ce8f5370SAlex Deucher 	} else {
4371783e4bfSThomas Renninger 		count = -EINVAL;
438ce8f5370SAlex Deucher 		goto fail;
439d0d6cb81SRafał Miłecki 	}
440ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
441ce8f5370SAlex Deucher fail:
442ce8f5370SAlex Deucher 	return count;
443ce8f5370SAlex Deucher }
444ce8f5370SAlex Deucher 
445ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
446ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
447ce8f5370SAlex Deucher 
44821a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
44921a8122aSAlex Deucher 				      struct device_attribute *attr,
45021a8122aSAlex Deucher 				      char *buf)
45121a8122aSAlex Deucher {
45221a8122aSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
45321a8122aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
45420d391d7SAlex Deucher 	int temp;
45521a8122aSAlex Deucher 
45621a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
45721a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
45821a8122aSAlex Deucher 		temp = rv6xx_get_temp(rdev);
45921a8122aSAlex Deucher 		break;
46021a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
46121a8122aSAlex Deucher 		temp = rv770_get_temp(rdev);
46221a8122aSAlex Deucher 		break;
46321a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
4644fddba1fSAlex Deucher 	case THERMAL_TYPE_NI:
46521a8122aSAlex Deucher 		temp = evergreen_get_temp(rdev);
46621a8122aSAlex Deucher 		break;
467e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
468e33df25fSAlex Deucher 		temp = sumo_get_temp(rdev);
469e33df25fSAlex Deucher 		break;
47021a8122aSAlex Deucher 	default:
47121a8122aSAlex Deucher 		temp = 0;
47221a8122aSAlex Deucher 		break;
47321a8122aSAlex Deucher 	}
47421a8122aSAlex Deucher 
47521a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
47621a8122aSAlex Deucher }
47721a8122aSAlex Deucher 
47821a8122aSAlex Deucher static ssize_t radeon_hwmon_show_name(struct device *dev,
47921a8122aSAlex Deucher 				      struct device_attribute *attr,
48021a8122aSAlex Deucher 				      char *buf)
48121a8122aSAlex Deucher {
48221a8122aSAlex Deucher 	return sprintf(buf, "radeon\n");
48321a8122aSAlex Deucher }
48421a8122aSAlex Deucher 
48521a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
48621a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
48721a8122aSAlex Deucher 
48821a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
48921a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
49021a8122aSAlex Deucher 	&sensor_dev_attr_name.dev_attr.attr,
49121a8122aSAlex Deucher 	NULL
49221a8122aSAlex Deucher };
49321a8122aSAlex Deucher 
49421a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
49521a8122aSAlex Deucher 	.attrs = hwmon_attributes,
49621a8122aSAlex Deucher };
49721a8122aSAlex Deucher 
4980d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
49921a8122aSAlex Deucher {
5000d18abedSDan Carpenter 	int err = 0;
50121a8122aSAlex Deucher 
50221a8122aSAlex Deucher 	rdev->pm.int_hwmon_dev = NULL;
50321a8122aSAlex Deucher 
50421a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
50521a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
50621a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
50721a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
508457558edSAlex Deucher 	case THERMAL_TYPE_NI:
509e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
51021a8122aSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
5110d18abedSDan Carpenter 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
5120d18abedSDan Carpenter 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
5130d18abedSDan Carpenter 			dev_err(rdev->dev,
5140d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
5150d18abedSDan Carpenter 			break;
5160d18abedSDan Carpenter 		}
51721a8122aSAlex Deucher 		dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
51821a8122aSAlex Deucher 		err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
51921a8122aSAlex Deucher 					 &hwmon_attrgroup);
5200d18abedSDan Carpenter 		if (err) {
5210d18abedSDan Carpenter 			dev_err(rdev->dev,
5220d18abedSDan Carpenter 				"Unable to create hwmon sysfs file: %d\n", err);
5230d18abedSDan Carpenter 			hwmon_device_unregister(rdev->dev);
5240d18abedSDan Carpenter 		}
52521a8122aSAlex Deucher 		break;
52621a8122aSAlex Deucher 	default:
52721a8122aSAlex Deucher 		break;
52821a8122aSAlex Deucher 	}
5290d18abedSDan Carpenter 
5300d18abedSDan Carpenter 	return err;
53121a8122aSAlex Deucher }
53221a8122aSAlex Deucher 
53321a8122aSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
53421a8122aSAlex Deucher {
53521a8122aSAlex Deucher 	if (rdev->pm.int_hwmon_dev) {
53621a8122aSAlex Deucher 		sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
53721a8122aSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
53821a8122aSAlex Deucher 	}
53921a8122aSAlex Deucher }
54021a8122aSAlex Deucher 
541ce8f5370SAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
542ce8f5370SAlex Deucher {
543ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
5443f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
5453f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
5463f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
5473f53eb6fSRafael J. Wysocki 	}
548ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
54932c87fcaSTejun Heo 
55032c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
551ce8f5370SAlex Deucher }
552ce8f5370SAlex Deucher 
553ce8f5370SAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
554ce8f5370SAlex Deucher {
555ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
556ed18a360SAlex Deucher 	if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
557ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
5588a83ec5eSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
5598a83ec5eSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDC);
5602feea49aSAlex Deucher 		if (rdev->pm.default_vddci)
5612feea49aSAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
5622feea49aSAlex Deucher 						SET_VOLTAGE_TYPE_ASIC_VDDCI);
563ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
564ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
565ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
566ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
567ed18a360SAlex Deucher 	}
568f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
569f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
570f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
571f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
5729ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
5739ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
5744d60173fSAlex Deucher 	rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
5752feea49aSAlex Deucher 	rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
5763f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
5773f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
5783f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
57932c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
5803f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
5813f53eb6fSRafael J. Wysocki 	}
582f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
583ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
584d0d6cb81SRafał Miłecki }
585d0d6cb81SRafał Miłecki 
5867433874eSRafał Miłecki int radeon_pm_init(struct radeon_device *rdev)
5877433874eSRafał Miłecki {
58826481fb1SDave Airlie 	int ret;
5890d18abedSDan Carpenter 
590ce8f5370SAlex Deucher 	/* default to profile method */
591ce8f5370SAlex Deucher 	rdev->pm.pm_method = PM_METHOD_PROFILE;
592f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
593ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
594ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
595ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
596ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
5979ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
5989ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
599f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
600f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
60121a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
602c913e23aSRafał Miłecki 
60356278a8eSAlex Deucher 	if (rdev->bios) {
60456278a8eSAlex Deucher 		if (rdev->is_atom_bios)
60556278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
60656278a8eSAlex Deucher 		else
60756278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
608f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
609ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
610ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
611ed18a360SAlex Deucher 		if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
612ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
6138a83ec5eSAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
6148a83ec5eSAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDC);
6154639dd21SAlex Deucher 			if (rdev->pm.default_vddci)
6164639dd21SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
6174639dd21SAlex Deucher 							SET_VOLTAGE_TYPE_ASIC_VDDCI);
618ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
619ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
620ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
621ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
622ed18a360SAlex Deucher 		}
62356278a8eSAlex Deucher 	}
62456278a8eSAlex Deucher 
62521a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
6260d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
6270d18abedSDan Carpenter 	if (ret)
6280d18abedSDan Carpenter 		return ret;
62932c87fcaSTejun Heo 
63032c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
63132c87fcaSTejun Heo 
632ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
633ce8f5370SAlex Deucher 		/* where's the best place to put these? */
63426481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
63526481fb1SDave Airlie 		if (ret)
63626481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
63726481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
63826481fb1SDave Airlie 		if (ret)
63926481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
640ce8f5370SAlex Deucher 
641ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
642ce8f5370SAlex Deucher 		rdev->acpi_nb.notifier_call = radeon_acpi_event;
643ce8f5370SAlex Deucher 		register_acpi_notifier(&rdev->acpi_nb);
644ce8f5370SAlex Deucher #endif
6457433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
646c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
6477433874eSRafał Miłecki 		}
6487433874eSRafał Miłecki 
649c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
650ce8f5370SAlex Deucher 	}
651c913e23aSRafał Miłecki 
6527433874eSRafał Miłecki 	return 0;
6537433874eSRafał Miłecki }
6547433874eSRafał Miłecki 
65529fb52caSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
65629fb52caSAlex Deucher {
657ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
658a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
659ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
660ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
661ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
662ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
663ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
664ce8f5370SAlex Deucher 			/* reset default clocks */
665ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
666ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
667ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
66858e21dffSAlex Deucher 		}
669ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
67032c87fcaSTejun Heo 
67132c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
67258e21dffSAlex Deucher 
673ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
674ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
675ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
676ce8f5370SAlex Deucher 		unregister_acpi_notifier(&rdev->acpi_nb);
677ce8f5370SAlex Deucher #endif
678ce8f5370SAlex Deucher 	}
679a424816fSAlex Deucher 
6800975b162SAlex Deucher 	if (rdev->pm.power_state)
6810975b162SAlex Deucher 		kfree(rdev->pm.power_state);
6820975b162SAlex Deucher 
68321a8122aSAlex Deucher 	radeon_hwmon_fini(rdev);
68429fb52caSAlex Deucher }
68529fb52caSAlex Deucher 
686c913e23aSRafał Miłecki void radeon_pm_compute_clocks(struct radeon_device *rdev)
687c913e23aSRafał Miłecki {
688c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
689a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
690c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
691c913e23aSRafał Miłecki 
692ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
693ce8f5370SAlex Deucher 		return;
694ce8f5370SAlex Deucher 
695c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
696c913e23aSRafał Miłecki 
697c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
698a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
699a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
700a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
701a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
702a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
703c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
704a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
705c913e23aSRafał Miłecki 		}
706c913e23aSRafał Miłecki 	}
707c913e23aSRafał Miłecki 
708ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
709ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
710ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
711ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
712ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
713a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
714ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
715ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
716c913e23aSRafał Miłecki 
717ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
718ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
719ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
720ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
721c913e23aSRafał Miłecki 
722d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
723c913e23aSRafał Miłecki 				}
724a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
725c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
726c913e23aSRafał Miłecki 
727ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
728ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
729ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
730ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
731ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
732c913e23aSRafał Miłecki 
73332c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
734c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
735ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
736ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
73732c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
738c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
739d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
740c913e23aSRafał Miłecki 				}
741a48b9b4eSAlex Deucher 			} else { /* count == 0 */
742ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
743ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
744c913e23aSRafał Miłecki 
745ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
746ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
747ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
748ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
749ce8f5370SAlex Deucher 				}
750ce8f5370SAlex Deucher 			}
75173a6d3fcSRafał Miłecki 		}
752c913e23aSRafał Miłecki 	}
753c913e23aSRafał Miłecki 
754c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
755c913e23aSRafał Miłecki }
756c913e23aSRafał Miłecki 
757ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
758f735261bSDave Airlie {
75975fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
760f735261bSDave Airlie 	bool in_vbl = true;
761f735261bSDave Airlie 
76275fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
76375fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
76475fa0b08SMario Kleiner 	 */
76575fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
76675fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
767f5a80209SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
768f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
769f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
770f735261bSDave Airlie 				in_vbl = false;
771f735261bSDave Airlie 		}
772f735261bSDave Airlie 	}
773f81f2024SMatthew Garrett 
774f81f2024SMatthew Garrett 	return in_vbl;
775f81f2024SMatthew Garrett }
776f81f2024SMatthew Garrett 
777ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
778f81f2024SMatthew Garrett {
779f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
780f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
781f81f2024SMatthew Garrett 
782f735261bSDave Airlie 	if (in_vbl == false)
783d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
784bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
785f735261bSDave Airlie 	return in_vbl;
786f735261bSDave Airlie }
787c913e23aSRafał Miłecki 
788ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
789c913e23aSRafał Miłecki {
790c913e23aSRafał Miłecki 	struct radeon_device *rdev;
791d9932a32SMatthew Garrett 	int resched;
792c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
793ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
794c913e23aSRafał Miłecki 
795d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
796c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
797ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
798c913e23aSRafał Miłecki 		unsigned long irq_flags;
799c913e23aSRafał Miłecki 		int not_processed = 0;
800*7465280cSAlex Deucher 		int i;
801c913e23aSRafał Miłecki 
802*7465280cSAlex Deucher 		read_lock_irqsave(&rdev->fence_lock, irq_flags);
803*7465280cSAlex Deucher 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
804*7465280cSAlex Deucher 			if (!rdev->fence_drv[i].initialized)
805*7465280cSAlex Deucher 				continue;
806*7465280cSAlex Deucher 
807*7465280cSAlex Deucher 			if (!list_empty(&rdev->fence_drv[i].emitted)) {
808c913e23aSRafał Miłecki 				struct list_head *ptr;
809*7465280cSAlex Deucher 				list_for_each(ptr, &rdev->fence_drv[i].emitted) {
810c913e23aSRafał Miłecki 					/* count up to 3, that's enought info */
811c913e23aSRafał Miłecki 					if (++not_processed >= 3)
812c913e23aSRafał Miłecki 						break;
813c913e23aSRafał Miłecki 				}
814c913e23aSRafał Miłecki 			}
815*7465280cSAlex Deucher 			if (not_processed >= 3)
816*7465280cSAlex Deucher 				break;
817*7465280cSAlex Deucher 		}
818*7465280cSAlex Deucher 		read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
819c913e23aSRafał Miłecki 
820c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
821ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
822ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
823ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
824ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
825ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
826ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
827ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
828c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
829c913e23aSRafał Miłecki 			}
830c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
831ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
832ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
833ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
834ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
835ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
836ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
837ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
838c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
839c913e23aSRafał Miłecki 			}
840c913e23aSRafał Miłecki 		}
841c913e23aSRafał Miłecki 
842d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
843d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
844d7311171SAlex Deucher 		 */
845ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
846ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
847ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
848ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
849c913e23aSRafał Miłecki 		}
850c913e23aSRafał Miłecki 
85132c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
852c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
853c913e23aSRafał Miłecki 	}
8543f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
8553f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
8563f53eb6fSRafael J. Wysocki }
857c913e23aSRafał Miłecki 
8587433874eSRafał Miłecki /*
8597433874eSRafał Miłecki  * Debugfs info
8607433874eSRafał Miłecki  */
8617433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
8627433874eSRafał Miłecki 
8637433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
8647433874eSRafał Miłecki {
8657433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
8667433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
8677433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
8687433874eSRafał Miłecki 
8699ace9f7bSAlex Deucher 	seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
8706234077dSRafał Miłecki 	seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
8719ace9f7bSAlex Deucher 	seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
8726234077dSRafał Miłecki 	if (rdev->asic->get_memory_clock)
8736234077dSRafał Miłecki 		seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
8740fcbe947SRafał Miłecki 	if (rdev->pm.current_vddc)
8750fcbe947SRafał Miłecki 		seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
876aa5120d2SRafał Miłecki 	if (rdev->asic->get_pcie_lanes)
877aa5120d2SRafał Miłecki 		seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
8787433874eSRafał Miłecki 
8797433874eSRafał Miłecki 	return 0;
8807433874eSRafał Miłecki }
8817433874eSRafał Miłecki 
8827433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
8837433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
8847433874eSRafał Miłecki };
8857433874eSRafał Miłecki #endif
8867433874eSRafał Miłecki 
887c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
8887433874eSRafał Miłecki {
8897433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
8907433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
8917433874eSRafał Miłecki #else
8927433874eSRafał Miłecki 	return 0;
8937433874eSRafał Miłecki #endif
8947433874eSRafał Miłecki }
895