xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision 0975b16274bad1f0bd5c5fd6ab759c5a9ee11949)
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"
26ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
27ce8f5370SAlex Deucher #include <linux/acpi.h>
28ce8f5370SAlex Deucher #endif
29ce8f5370SAlex Deucher #include <linux/power_supply.h>
3021a8122aSAlex Deucher #include <linux/hwmon.h>
3121a8122aSAlex Deucher #include <linux/hwmon-sysfs.h>
327433874eSRafał Miłecki 
33c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
34c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3573a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
362031f77cSAlex Deucher #define RADEON_WAIT_IDLE_TIMEOUT 200
37c913e23aSRafał Miłecki 
38f712d0c7SRafał Miłecki static const char *radeon_pm_state_type_name[5] = {
39f712d0c7SRafał Miłecki 	"Default",
40f712d0c7SRafał Miłecki 	"Powersave",
41f712d0c7SRafał Miłecki 	"Battery",
42f712d0c7SRafał Miłecki 	"Balanced",
43f712d0c7SRafał Miłecki 	"Performance",
44f712d0c7SRafał Miłecki };
45f712d0c7SRafał Miłecki 
46ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
47c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
48ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
49ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
50ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
51ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
52ce8f5370SAlex Deucher 
53ce8f5370SAlex Deucher #define ACPI_AC_CLASS           "ac_adapter"
54ce8f5370SAlex Deucher 
55ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
56ce8f5370SAlex Deucher static int radeon_acpi_event(struct notifier_block *nb,
57ce8f5370SAlex Deucher 			     unsigned long val,
58ce8f5370SAlex Deucher 			     void *data)
59ce8f5370SAlex Deucher {
60ce8f5370SAlex Deucher 	struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
61ce8f5370SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
62ce8f5370SAlex Deucher 
63ce8f5370SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
64ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
65d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("pm: AC\n");
66ce8f5370SAlex Deucher 		else
67d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("pm: DC\n");
68ce8f5370SAlex Deucher 
69ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
70ce8f5370SAlex Deucher 			if (rdev->pm.profile == PM_PROFILE_AUTO) {
71ce8f5370SAlex Deucher 				mutex_lock(&rdev->pm.mutex);
72ce8f5370SAlex Deucher 				radeon_pm_update_profile(rdev);
73ce8f5370SAlex Deucher 				radeon_pm_set_clocks(rdev);
74ce8f5370SAlex Deucher 				mutex_unlock(&rdev->pm.mutex);
75ce8f5370SAlex Deucher 			}
76ce8f5370SAlex Deucher 		}
77ce8f5370SAlex Deucher 	}
78ce8f5370SAlex Deucher 
79ce8f5370SAlex Deucher 	return NOTIFY_OK;
80ce8f5370SAlex Deucher }
81ce8f5370SAlex Deucher #endif
82ce8f5370SAlex Deucher 
83ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
84ce8f5370SAlex Deucher {
85ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
86ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
87ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
88ce8f5370SAlex Deucher 		break;
89ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
90ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
91ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
92ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
93ce8f5370SAlex Deucher 			else
94ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
95ce8f5370SAlex Deucher 		} else {
96ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
97c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
98ce8f5370SAlex Deucher 			else
99c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
100ce8f5370SAlex Deucher 		}
101ce8f5370SAlex Deucher 		break;
102ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
103ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
104ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
105ce8f5370SAlex Deucher 		else
106ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
107ce8f5370SAlex Deucher 		break;
108c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
109c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
110c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
111c9e75b21SAlex Deucher 		else
112c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
113c9e75b21SAlex Deucher 		break;
114ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
115ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
116ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
117ce8f5370SAlex Deucher 		else
118ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
119ce8f5370SAlex Deucher 		break;
120ce8f5370SAlex Deucher 	}
121ce8f5370SAlex Deucher 
122ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
123ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
124ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
125ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
126ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
127ce8f5370SAlex Deucher 	} else {
128ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
129ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
130ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
131ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
132ce8f5370SAlex Deucher 	}
133ce8f5370SAlex Deucher }
134c913e23aSRafał Miłecki 
1355876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1365876dd24SMatthew Garrett {
1375876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1385876dd24SMatthew Garrett 
1395876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1405876dd24SMatthew Garrett 		return;
1415876dd24SMatthew Garrett 
1425876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1435876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1445876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1455876dd24SMatthew Garrett 	}
1465876dd24SMatthew Garrett }
1475876dd24SMatthew Garrett 
148ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
149ce8f5370SAlex Deucher {
150ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
151ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
152ce8f5370SAlex Deucher 		wait_event_timeout(
153ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
154ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
155ce8f5370SAlex Deucher 	}
156ce8f5370SAlex Deucher }
157ce8f5370SAlex Deucher 
158ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
159ce8f5370SAlex Deucher {
160ce8f5370SAlex Deucher 	u32 sclk, mclk;
16192645879SAlex Deucher 	bool misc_after = false;
162ce8f5370SAlex Deucher 
163ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
164ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
165ce8f5370SAlex Deucher 		return;
166ce8f5370SAlex Deucher 
167ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
168ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
169ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
1709ace9f7bSAlex Deucher 		if (sclk > rdev->pm.default_sclk)
1719ace9f7bSAlex Deucher 			sclk = rdev->pm.default_sclk;
172ce8f5370SAlex Deucher 
173ce8f5370SAlex Deucher 		mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
174ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].mclk;
1759ace9f7bSAlex Deucher 		if (mclk > rdev->pm.default_mclk)
1769ace9f7bSAlex Deucher 			mclk = rdev->pm.default_mclk;
177ce8f5370SAlex Deucher 
17892645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
17992645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
18092645879SAlex Deucher 			misc_after = true;
18192645879SAlex Deucher 
18292645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
18392645879SAlex Deucher 
18492645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
18592645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
18692645879SAlex Deucher 				return;
18792645879SAlex Deucher 		}
18892645879SAlex Deucher 
18992645879SAlex Deucher 		radeon_pm_prepare(rdev);
19092645879SAlex Deucher 
19192645879SAlex Deucher 		if (!misc_after)
192ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
193ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
194ce8f5370SAlex Deucher 
195ce8f5370SAlex Deucher 		/* set engine clock */
196ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
197ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
198ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
199ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
200ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
201d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
202ce8f5370SAlex Deucher 		}
203ce8f5370SAlex Deucher 
204ce8f5370SAlex Deucher 		/* set memory clock */
205ce8f5370SAlex Deucher 		if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
206ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
207ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
208ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
209ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
210d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
211ce8f5370SAlex Deucher 		}
21292645879SAlex Deucher 
21392645879SAlex Deucher 		if (misc_after)
21492645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
21592645879SAlex Deucher 			radeon_pm_misc(rdev);
21692645879SAlex Deucher 
217ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
218ce8f5370SAlex Deucher 
219ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
220ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
221ce8f5370SAlex Deucher 	} else
222d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
223ce8f5370SAlex Deucher }
224ce8f5370SAlex Deucher 
225ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
226a424816fSAlex Deucher {
2272aba631cSMatthew Garrett 	int i;
2282aba631cSMatthew Garrett 
2294e186b2dSAlex Deucher 	/* no need to take locks, etc. if nothing's going to change */
2304e186b2dSAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
2314e186b2dSAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
2324e186b2dSAlex Deucher 		return;
2334e186b2dSAlex Deucher 
234612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
235612e06ceSMatthew Garrett 	mutex_lock(&rdev->vram_mutex);
236a424816fSAlex Deucher 	mutex_lock(&rdev->cp.mutex);
2374f3218cbSAlex Deucher 
2384f3218cbSAlex Deucher 	/* gui idle int has issues on older chips it seems */
2394f3218cbSAlex Deucher 	if (rdev->family >= CHIP_R600) {
240ce8f5370SAlex Deucher 		if (rdev->irq.installed) {
241a424816fSAlex Deucher 			/* wait for GPU idle */
242a424816fSAlex Deucher 			rdev->pm.gui_idle = false;
243a424816fSAlex Deucher 			rdev->irq.gui_idle = true;
244a424816fSAlex Deucher 			radeon_irq_set(rdev);
245a424816fSAlex Deucher 			wait_event_interruptible_timeout(
246a424816fSAlex Deucher 				rdev->irq.idle_queue, rdev->pm.gui_idle,
247a424816fSAlex Deucher 				msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
248a424816fSAlex Deucher 			rdev->irq.gui_idle = false;
249a424816fSAlex Deucher 			radeon_irq_set(rdev);
250ce8f5370SAlex Deucher 		}
25101434b4bSMatthew Garrett 	} else {
252ce8f5370SAlex Deucher 		if (rdev->cp.ready) {
25301434b4bSMatthew Garrett 			struct radeon_fence *fence;
25401434b4bSMatthew Garrett 			radeon_ring_alloc(rdev, 64);
25501434b4bSMatthew Garrett 			radeon_fence_create(rdev, &fence);
25601434b4bSMatthew Garrett 			radeon_fence_emit(rdev, fence);
25701434b4bSMatthew Garrett 			radeon_ring_commit(rdev);
25801434b4bSMatthew Garrett 			radeon_fence_wait(fence, false);
25901434b4bSMatthew Garrett 			radeon_fence_unref(&fence);
2604f3218cbSAlex Deucher 		}
261ce8f5370SAlex Deucher 	}
2625876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2635876dd24SMatthew Garrett 
264ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2652aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2662aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2672aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2682aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2692aba631cSMatthew Garrett 			}
2702aba631cSMatthew Garrett 		}
2712aba631cSMatthew Garrett 	}
2722aba631cSMatthew Garrett 
273ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2742aba631cSMatthew Garrett 
275ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2762aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2772aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2782aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2792aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2802aba631cSMatthew Garrett 			}
2812aba631cSMatthew Garrett 		}
2822aba631cSMatthew Garrett 	}
283a424816fSAlex Deucher 
284a424816fSAlex Deucher 	/* update display watermarks based on new power state */
285a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
286a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
287a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
288a424816fSAlex Deucher 
289ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
2902aba631cSMatthew Garrett 
291a424816fSAlex Deucher 	mutex_unlock(&rdev->cp.mutex);
292612e06ceSMatthew Garrett 	mutex_unlock(&rdev->vram_mutex);
293612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
294a424816fSAlex Deucher }
295a424816fSAlex Deucher 
296f712d0c7SRafał Miłecki static void radeon_pm_print_states(struct radeon_device *rdev)
297f712d0c7SRafał Miłecki {
298f712d0c7SRafał Miłecki 	int i, j;
299f712d0c7SRafał Miłecki 	struct radeon_power_state *power_state;
300f712d0c7SRafał Miłecki 	struct radeon_pm_clock_info *clock_info;
301f712d0c7SRafał Miłecki 
302d9fdaafbSDave Airlie 	DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
303f712d0c7SRafał Miłecki 	for (i = 0; i < rdev->pm.num_power_states; i++) {
304f712d0c7SRafał Miłecki 		power_state = &rdev->pm.power_state[i];
305d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("State %d: %s\n", i,
306f712d0c7SRafał Miłecki 			radeon_pm_state_type_name[power_state->type]);
307f712d0c7SRafał Miłecki 		if (i == rdev->pm.default_power_state_index)
308d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tDefault");
309f712d0c7SRafał Miłecki 		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
310d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
311f712d0c7SRafał Miłecki 		if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
312d9fdaafbSDave Airlie 			DRM_DEBUG_DRIVER("\tSingle display only\n");
313d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
314f712d0c7SRafał Miłecki 		for (j = 0; j < power_state->num_clock_modes; j++) {
315f712d0c7SRafał Miłecki 			clock_info = &(power_state->clock_info[j]);
316f712d0c7SRafał Miłecki 			if (rdev->flags & RADEON_IS_IGP)
317d9fdaafbSDave Airlie 				DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
318f712d0c7SRafał Miłecki 					j,
319f712d0c7SRafał Miłecki 					clock_info->sclk * 10,
320f712d0c7SRafał Miłecki 					clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
321f712d0c7SRafał Miłecki 			else
322d9fdaafbSDave Airlie 				DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
323f712d0c7SRafał Miłecki 					j,
324f712d0c7SRafał Miłecki 					clock_info->sclk * 10,
325f712d0c7SRafał Miłecki 					clock_info->mclk * 10,
326f712d0c7SRafał Miłecki 					clock_info->voltage.voltage,
327f712d0c7SRafał Miłecki 					clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
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 {
368ce8f5370SAlex Deucher 			DRM_ERROR("invalid power profile!\n");
369ce8f5370SAlex Deucher 			goto fail;
370ce8f5370SAlex Deucher 		}
371ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
372ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
373ce8f5370SAlex Deucher 	}
374ce8f5370SAlex Deucher fail:
375a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
376a424816fSAlex Deucher 
377a424816fSAlex Deucher 	return count;
378a424816fSAlex Deucher }
379a424816fSAlex Deucher 
380ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
381ce8f5370SAlex Deucher 				    struct device_attribute *attr,
382ce8f5370SAlex Deucher 				    char *buf)
38356278a8eSAlex Deucher {
384ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
385ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
386ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
38756278a8eSAlex Deucher 
388ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
389ce8f5370SAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
39056278a8eSAlex Deucher }
39156278a8eSAlex Deucher 
392ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
393ce8f5370SAlex Deucher 				    struct device_attribute *attr,
394ce8f5370SAlex Deucher 				    const char *buf,
395ce8f5370SAlex Deucher 				    size_t count)
396d0d6cb81SRafał Miłecki {
397ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
398ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
399ce8f5370SAlex Deucher 
400ce8f5370SAlex Deucher 
401ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
402ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
403ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
404ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
405ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
406ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
407ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
408ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
409ce8f5370SAlex Deucher 		/* disable dynpm */
410ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
411ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
4123f53eb6fSRafael J. Wysocki 		rdev->pm.pm_method = PM_METHOD_PROFILE;
413ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
41432c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
415ce8f5370SAlex Deucher 	} else {
416ce8f5370SAlex Deucher 		DRM_ERROR("invalid power method!\n");
417ce8f5370SAlex Deucher 		goto fail;
418d0d6cb81SRafał Miłecki 	}
419ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
420ce8f5370SAlex Deucher fail:
421ce8f5370SAlex Deucher 	return count;
422ce8f5370SAlex Deucher }
423ce8f5370SAlex Deucher 
424ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
425ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
426ce8f5370SAlex Deucher 
42721a8122aSAlex Deucher static ssize_t radeon_hwmon_show_temp(struct device *dev,
42821a8122aSAlex Deucher 				      struct device_attribute *attr,
42921a8122aSAlex Deucher 				      char *buf)
43021a8122aSAlex Deucher {
43121a8122aSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
43221a8122aSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
43320d391d7SAlex Deucher 	int temp;
43421a8122aSAlex Deucher 
43521a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
43621a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
43721a8122aSAlex Deucher 		temp = rv6xx_get_temp(rdev);
43821a8122aSAlex Deucher 		break;
43921a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
44021a8122aSAlex Deucher 		temp = rv770_get_temp(rdev);
44121a8122aSAlex Deucher 		break;
44221a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
4434fddba1fSAlex Deucher 	case THERMAL_TYPE_NI:
44421a8122aSAlex Deucher 		temp = evergreen_get_temp(rdev);
44521a8122aSAlex Deucher 		break;
446e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
447e33df25fSAlex Deucher 		temp = sumo_get_temp(rdev);
448e33df25fSAlex Deucher 		break;
44921a8122aSAlex Deucher 	default:
45021a8122aSAlex Deucher 		temp = 0;
45121a8122aSAlex Deucher 		break;
45221a8122aSAlex Deucher 	}
45321a8122aSAlex Deucher 
45421a8122aSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%d\n", temp);
45521a8122aSAlex Deucher }
45621a8122aSAlex Deucher 
45721a8122aSAlex Deucher static ssize_t radeon_hwmon_show_name(struct device *dev,
45821a8122aSAlex Deucher 				      struct device_attribute *attr,
45921a8122aSAlex Deucher 				      char *buf)
46021a8122aSAlex Deucher {
46121a8122aSAlex Deucher 	return sprintf(buf, "radeon\n");
46221a8122aSAlex Deucher }
46321a8122aSAlex Deucher 
46421a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
46521a8122aSAlex Deucher static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
46621a8122aSAlex Deucher 
46721a8122aSAlex Deucher static struct attribute *hwmon_attributes[] = {
46821a8122aSAlex Deucher 	&sensor_dev_attr_temp1_input.dev_attr.attr,
46921a8122aSAlex Deucher 	&sensor_dev_attr_name.dev_attr.attr,
47021a8122aSAlex Deucher 	NULL
47121a8122aSAlex Deucher };
47221a8122aSAlex Deucher 
47321a8122aSAlex Deucher static const struct attribute_group hwmon_attrgroup = {
47421a8122aSAlex Deucher 	.attrs = hwmon_attributes,
47521a8122aSAlex Deucher };
47621a8122aSAlex Deucher 
4770d18abedSDan Carpenter static int radeon_hwmon_init(struct radeon_device *rdev)
47821a8122aSAlex Deucher {
4790d18abedSDan Carpenter 	int err = 0;
48021a8122aSAlex Deucher 
48121a8122aSAlex Deucher 	rdev->pm.int_hwmon_dev = NULL;
48221a8122aSAlex Deucher 
48321a8122aSAlex Deucher 	switch (rdev->pm.int_thermal_type) {
48421a8122aSAlex Deucher 	case THERMAL_TYPE_RV6XX:
48521a8122aSAlex Deucher 	case THERMAL_TYPE_RV770:
48621a8122aSAlex Deucher 	case THERMAL_TYPE_EVERGREEN:
487e33df25fSAlex Deucher 	case THERMAL_TYPE_SUMO:
48821a8122aSAlex Deucher 		rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
4890d18abedSDan Carpenter 		if (IS_ERR(rdev->pm.int_hwmon_dev)) {
4900d18abedSDan Carpenter 			err = PTR_ERR(rdev->pm.int_hwmon_dev);
4910d18abedSDan Carpenter 			dev_err(rdev->dev,
4920d18abedSDan Carpenter 				"Unable to register hwmon device: %d\n", err);
4930d18abedSDan Carpenter 			break;
4940d18abedSDan Carpenter 		}
49521a8122aSAlex Deucher 		dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
49621a8122aSAlex Deucher 		err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
49721a8122aSAlex Deucher 					 &hwmon_attrgroup);
4980d18abedSDan Carpenter 		if (err) {
4990d18abedSDan Carpenter 			dev_err(rdev->dev,
5000d18abedSDan Carpenter 				"Unable to create hwmon sysfs file: %d\n", err);
5010d18abedSDan Carpenter 			hwmon_device_unregister(rdev->dev);
5020d18abedSDan Carpenter 		}
50321a8122aSAlex Deucher 		break;
50421a8122aSAlex Deucher 	default:
50521a8122aSAlex Deucher 		break;
50621a8122aSAlex Deucher 	}
5070d18abedSDan Carpenter 
5080d18abedSDan Carpenter 	return err;
50921a8122aSAlex Deucher }
51021a8122aSAlex Deucher 
51121a8122aSAlex Deucher static void radeon_hwmon_fini(struct radeon_device *rdev)
51221a8122aSAlex Deucher {
51321a8122aSAlex Deucher 	if (rdev->pm.int_hwmon_dev) {
51421a8122aSAlex Deucher 		sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
51521a8122aSAlex Deucher 		hwmon_device_unregister(rdev->pm.int_hwmon_dev);
51621a8122aSAlex Deucher 	}
51721a8122aSAlex Deucher }
51821a8122aSAlex Deucher 
519ce8f5370SAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
520ce8f5370SAlex Deucher {
521ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
5223f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
5233f53eb6fSRafael J. Wysocki 		if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
5243f53eb6fSRafael J. Wysocki 			rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
5253f53eb6fSRafael J. Wysocki 	}
526ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
52732c87fcaSTejun Heo 
52832c87fcaSTejun Heo 	cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
529ce8f5370SAlex Deucher }
530ce8f5370SAlex Deucher 
531ce8f5370SAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
532ce8f5370SAlex Deucher {
533ed18a360SAlex Deucher 	/* set up the default clocks if the MC ucode is loaded */
534ed18a360SAlex Deucher 	if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
535ed18a360SAlex Deucher 		if (rdev->pm.default_vddc)
536ed18a360SAlex Deucher 			radeon_atom_set_voltage(rdev, rdev->pm.default_vddc);
537ed18a360SAlex Deucher 		if (rdev->pm.default_sclk)
538ed18a360SAlex Deucher 			radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
539ed18a360SAlex Deucher 		if (rdev->pm.default_mclk)
540ed18a360SAlex Deucher 			radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
541ed18a360SAlex Deucher 	}
542f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
543f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
544f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
545f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
5469ace9f7bSAlex Deucher 	rdev->pm.current_sclk = rdev->pm.default_sclk;
5479ace9f7bSAlex Deucher 	rdev->pm.current_mclk = rdev->pm.default_mclk;
5484d60173fSAlex Deucher 	rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
5493f53eb6fSRafael J. Wysocki 	if (rdev->pm.pm_method == PM_METHOD_DYNPM
5503f53eb6fSRafael J. Wysocki 	    && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
5513f53eb6fSRafael J. Wysocki 		rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
55232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
5533f53eb6fSRafael J. Wysocki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
5543f53eb6fSRafael J. Wysocki 	}
555f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
556ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
557d0d6cb81SRafał Miłecki }
558d0d6cb81SRafał Miłecki 
5597433874eSRafał Miłecki int radeon_pm_init(struct radeon_device *rdev)
5607433874eSRafał Miłecki {
56126481fb1SDave Airlie 	int ret;
5620d18abedSDan Carpenter 
563ce8f5370SAlex Deucher 	/* default to profile method */
564ce8f5370SAlex Deucher 	rdev->pm.pm_method = PM_METHOD_PROFILE;
565f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
566ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
567ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
568ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
569ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
5709ace9f7bSAlex Deucher 	rdev->pm.default_sclk = rdev->clock.default_sclk;
5719ace9f7bSAlex Deucher 	rdev->pm.default_mclk = rdev->clock.default_mclk;
572f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
573f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
57421a8122aSAlex Deucher 	rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
575c913e23aSRafał Miłecki 
57656278a8eSAlex Deucher 	if (rdev->bios) {
57756278a8eSAlex Deucher 		if (rdev->is_atom_bios)
57856278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
57956278a8eSAlex Deucher 		else
58056278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
581f712d0c7SRafał Miłecki 		radeon_pm_print_states(rdev);
582ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
583ed18a360SAlex Deucher 		/* set up the default clocks if the MC ucode is loaded */
584ed18a360SAlex Deucher 		if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
585ed18a360SAlex Deucher 			if (rdev->pm.default_vddc)
586ed18a360SAlex Deucher 				radeon_atom_set_voltage(rdev, rdev->pm.default_vddc);
587ed18a360SAlex Deucher 			if (rdev->pm.default_sclk)
588ed18a360SAlex Deucher 				radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
589ed18a360SAlex Deucher 			if (rdev->pm.default_mclk)
590ed18a360SAlex Deucher 				radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
591ed18a360SAlex Deucher 		}
59256278a8eSAlex Deucher 	}
59356278a8eSAlex Deucher 
59421a8122aSAlex Deucher 	/* set up the internal thermal sensor if applicable */
5950d18abedSDan Carpenter 	ret = radeon_hwmon_init(rdev);
5960d18abedSDan Carpenter 	if (ret)
5970d18abedSDan Carpenter 		return ret;
59832c87fcaSTejun Heo 
59932c87fcaSTejun Heo 	INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
60032c87fcaSTejun Heo 
601ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
602ce8f5370SAlex Deucher 		/* where's the best place to put these? */
60326481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
60426481fb1SDave Airlie 		if (ret)
60526481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
60626481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
60726481fb1SDave Airlie 		if (ret)
60826481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
609ce8f5370SAlex Deucher 
610ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
611ce8f5370SAlex Deucher 		rdev->acpi_nb.notifier_call = radeon_acpi_event;
612ce8f5370SAlex Deucher 		register_acpi_notifier(&rdev->acpi_nb);
613ce8f5370SAlex Deucher #endif
6147433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
615c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
6167433874eSRafał Miłecki 		}
6177433874eSRafał Miłecki 
618c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
619ce8f5370SAlex Deucher 	}
620c913e23aSRafał Miłecki 
6217433874eSRafał Miłecki 	return 0;
6227433874eSRafał Miłecki }
6237433874eSRafał Miłecki 
62429fb52caSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
62529fb52caSAlex Deucher {
626ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
627a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
628ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
629ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
630ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
631ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
632ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
633ce8f5370SAlex Deucher 			/* reset default clocks */
634ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
635ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
636ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
63758e21dffSAlex Deucher 		}
638ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
63932c87fcaSTejun Heo 
64032c87fcaSTejun Heo 		cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
64158e21dffSAlex Deucher 
642ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
643ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
644ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
645ce8f5370SAlex Deucher 		unregister_acpi_notifier(&rdev->acpi_nb);
646ce8f5370SAlex Deucher #endif
647ce8f5370SAlex Deucher 	}
648a424816fSAlex Deucher 
649*0975b162SAlex Deucher 	if (rdev->pm.power_state)
650*0975b162SAlex Deucher 		kfree(rdev->pm.power_state);
651*0975b162SAlex Deucher 
65221a8122aSAlex Deucher 	radeon_hwmon_fini(rdev);
65329fb52caSAlex Deucher }
65429fb52caSAlex Deucher 
655c913e23aSRafał Miłecki void radeon_pm_compute_clocks(struct radeon_device *rdev)
656c913e23aSRafał Miłecki {
657c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
658a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
659c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
660c913e23aSRafał Miłecki 
661ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
662ce8f5370SAlex Deucher 		return;
663ce8f5370SAlex Deucher 
664c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
665c913e23aSRafał Miłecki 
666c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
667a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
668a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
669a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
670a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
671a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
672c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
673a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
674c913e23aSRafał Miłecki 		}
675c913e23aSRafał Miłecki 	}
676c913e23aSRafał Miłecki 
677ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
678ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
679ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
680ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
681ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
682a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
683ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
684ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
685c913e23aSRafał Miłecki 
686ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
687ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
688ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
689ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
690c913e23aSRafał Miłecki 
691d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
692c913e23aSRafał Miłecki 				}
693a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
694c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
695c913e23aSRafał Miłecki 
696ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
697ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
698ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
699ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
700ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
701c913e23aSRafał Miłecki 
70232c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
703c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
704ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
705ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
70632c87fcaSTejun Heo 					schedule_delayed_work(&rdev->pm.dynpm_idle_work,
707c913e23aSRafał Miłecki 							      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
708d9fdaafbSDave Airlie 					DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
709c913e23aSRafał Miłecki 				}
710a48b9b4eSAlex Deucher 			} else { /* count == 0 */
711ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
712ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
713c913e23aSRafał Miłecki 
714ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
715ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
716ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
717ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
718ce8f5370SAlex Deucher 				}
719ce8f5370SAlex Deucher 			}
72073a6d3fcSRafał Miłecki 		}
721c913e23aSRafał Miłecki 	}
722c913e23aSRafał Miłecki 
723c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
724c913e23aSRafał Miłecki }
725c913e23aSRafał Miłecki 
726ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
727f735261bSDave Airlie {
72875fa0b08SMario Kleiner 	int  crtc, vpos, hpos, vbl_status;
729f735261bSDave Airlie 	bool in_vbl = true;
730f735261bSDave Airlie 
73175fa0b08SMario Kleiner 	/* Iterate over all active crtc's. All crtc's must be in vblank,
73275fa0b08SMario Kleiner 	 * otherwise return in_vbl == false.
73375fa0b08SMario Kleiner 	 */
73475fa0b08SMario Kleiner 	for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
73575fa0b08SMario Kleiner 		if (rdev->pm.active_crtcs & (1 << crtc)) {
736f5a80209SMario Kleiner 			vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
737f5a80209SMario Kleiner 			if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
738f5a80209SMario Kleiner 			    !(vbl_status & DRM_SCANOUTPOS_INVBL))
739f735261bSDave Airlie 				in_vbl = false;
740f735261bSDave Airlie 		}
741f735261bSDave Airlie 	}
742f81f2024SMatthew Garrett 
743f81f2024SMatthew Garrett 	return in_vbl;
744f81f2024SMatthew Garrett }
745f81f2024SMatthew Garrett 
746ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
747f81f2024SMatthew Garrett {
748f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
749f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
750f81f2024SMatthew Garrett 
751f735261bSDave Airlie 	if (in_vbl == false)
752d9fdaafbSDave Airlie 		DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
753bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
754f735261bSDave Airlie 	return in_vbl;
755f735261bSDave Airlie }
756c913e23aSRafał Miłecki 
757ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
758c913e23aSRafał Miłecki {
759c913e23aSRafał Miłecki 	struct radeon_device *rdev;
760d9932a32SMatthew Garrett 	int resched;
761c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
762ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
763c913e23aSRafał Miłecki 
764d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
765c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
766ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
767c913e23aSRafał Miłecki 		unsigned long irq_flags;
768c913e23aSRafał Miłecki 		int not_processed = 0;
769c913e23aSRafał Miłecki 
770c913e23aSRafał Miłecki 		read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
771c913e23aSRafał Miłecki 		if (!list_empty(&rdev->fence_drv.emited)) {
772c913e23aSRafał Miłecki 			struct list_head *ptr;
773c913e23aSRafał Miłecki 			list_for_each(ptr, &rdev->fence_drv.emited) {
774c913e23aSRafał Miłecki 				/* count up to 3, that's enought info */
775c913e23aSRafał Miłecki 				if (++not_processed >= 3)
776c913e23aSRafał Miłecki 					break;
777c913e23aSRafał Miłecki 			}
778c913e23aSRafał Miłecki 		}
779c913e23aSRafał Miłecki 		read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
780c913e23aSRafał Miłecki 
781c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
782ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
783ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
784ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
785ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
786ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
787ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
788ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
789c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
790c913e23aSRafał Miłecki 			}
791c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
792ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
793ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
794ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
795ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
796ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
797ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
798ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
799c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
800c913e23aSRafał Miłecki 			}
801c913e23aSRafał Miłecki 		}
802c913e23aSRafał Miłecki 
803d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
804d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
805d7311171SAlex Deucher 		 */
806ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
807ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
808ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
809ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
810c913e23aSRafał Miłecki 		}
811c913e23aSRafał Miłecki 
81232c87fcaSTejun Heo 		schedule_delayed_work(&rdev->pm.dynpm_idle_work,
813c913e23aSRafał Miłecki 				      msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
814c913e23aSRafał Miłecki 	}
8153f53eb6fSRafael J. Wysocki 	mutex_unlock(&rdev->pm.mutex);
8163f53eb6fSRafael J. Wysocki 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
8173f53eb6fSRafael J. Wysocki }
818c913e23aSRafał Miłecki 
8197433874eSRafał Miłecki /*
8207433874eSRafał Miłecki  * Debugfs info
8217433874eSRafał Miłecki  */
8227433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
8237433874eSRafał Miłecki 
8247433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
8257433874eSRafał Miłecki {
8267433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
8277433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
8287433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
8297433874eSRafał Miłecki 
8309ace9f7bSAlex Deucher 	seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
8316234077dSRafał Miłecki 	seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
8329ace9f7bSAlex Deucher 	seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
8336234077dSRafał Miłecki 	if (rdev->asic->get_memory_clock)
8346234077dSRafał Miłecki 		seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
8350fcbe947SRafał Miłecki 	if (rdev->pm.current_vddc)
8360fcbe947SRafał Miłecki 		seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
837aa5120d2SRafał Miłecki 	if (rdev->asic->get_pcie_lanes)
838aa5120d2SRafał Miłecki 		seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
8397433874eSRafał Miłecki 
8407433874eSRafał Miłecki 	return 0;
8417433874eSRafał Miłecki }
8427433874eSRafał Miłecki 
8437433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
8447433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
8457433874eSRafał Miłecki };
8467433874eSRafał Miłecki #endif
8477433874eSRafał Miłecki 
848c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
8497433874eSRafał Miłecki {
8507433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
8517433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
8527433874eSRafał Miłecki #else
8537433874eSRafał Miłecki 	return 0;
8547433874eSRafał Miłecki #endif
8557433874eSRafał Miłecki }
856