xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_pm.c (revision c9e75b2125b563e67663f78ad53ea9387a9a7aa1)
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>
307433874eSRafał Miłecki 
31c913e23aSRafał Miłecki #define RADEON_IDLE_LOOP_MS 100
32c913e23aSRafał Miłecki #define RADEON_RECLOCK_DELAY_MS 200
3373a6d3fcSRafał Miłecki #define RADEON_WAIT_VBLANK_TIMEOUT 200
342031f77cSAlex Deucher #define RADEON_WAIT_IDLE_TIMEOUT 200
35c913e23aSRafał Miłecki 
36ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work);
37c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev);
38ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev);
39ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
40ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev);
41ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev);
42ce8f5370SAlex Deucher 
43ce8f5370SAlex Deucher #define ACPI_AC_CLASS           "ac_adapter"
44ce8f5370SAlex Deucher 
45ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
46ce8f5370SAlex Deucher static int radeon_acpi_event(struct notifier_block *nb,
47ce8f5370SAlex Deucher 			     unsigned long val,
48ce8f5370SAlex Deucher 			     void *data)
49ce8f5370SAlex Deucher {
50ce8f5370SAlex Deucher 	struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
51ce8f5370SAlex Deucher 	struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
52ce8f5370SAlex Deucher 
53ce8f5370SAlex Deucher 	if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
54ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0)
55ce8a3eb2SAlex Deucher 			DRM_DEBUG("pm: AC\n");
56ce8f5370SAlex Deucher 		else
57ce8a3eb2SAlex Deucher 			DRM_DEBUG("pm: DC\n");
58ce8f5370SAlex Deucher 
59ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
60ce8f5370SAlex Deucher 			if (rdev->pm.profile == PM_PROFILE_AUTO) {
61ce8f5370SAlex Deucher 				mutex_lock(&rdev->pm.mutex);
62ce8f5370SAlex Deucher 				radeon_pm_update_profile(rdev);
63ce8f5370SAlex Deucher 				radeon_pm_set_clocks(rdev);
64ce8f5370SAlex Deucher 				mutex_unlock(&rdev->pm.mutex);
65ce8f5370SAlex Deucher 			}
66ce8f5370SAlex Deucher 		}
67ce8f5370SAlex Deucher 	}
68ce8f5370SAlex Deucher 
69ce8f5370SAlex Deucher 	return NOTIFY_OK;
70ce8f5370SAlex Deucher }
71ce8f5370SAlex Deucher #endif
72ce8f5370SAlex Deucher 
73ce8f5370SAlex Deucher static void radeon_pm_update_profile(struct radeon_device *rdev)
74ce8f5370SAlex Deucher {
75ce8f5370SAlex Deucher 	switch (rdev->pm.profile) {
76ce8f5370SAlex Deucher 	case PM_PROFILE_DEFAULT:
77ce8f5370SAlex Deucher 		rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
78ce8f5370SAlex Deucher 		break;
79ce8f5370SAlex Deucher 	case PM_PROFILE_AUTO:
80ce8f5370SAlex Deucher 		if (power_supply_is_system_supplied() > 0) {
81ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
82ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
83ce8f5370SAlex Deucher 			else
84ce8f5370SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
85ce8f5370SAlex Deucher 		} else {
86ce8f5370SAlex Deucher 			if (rdev->pm.active_crtc_count > 1)
87*c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
88ce8f5370SAlex Deucher 			else
89*c9e75b21SAlex Deucher 				rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
90ce8f5370SAlex Deucher 		}
91ce8f5370SAlex Deucher 		break;
92ce8f5370SAlex Deucher 	case PM_PROFILE_LOW:
93ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
94ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
95ce8f5370SAlex Deucher 		else
96ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
97ce8f5370SAlex Deucher 		break;
98*c9e75b21SAlex Deucher 	case PM_PROFILE_MID:
99*c9e75b21SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
100*c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
101*c9e75b21SAlex Deucher 		else
102*c9e75b21SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
103*c9e75b21SAlex Deucher 		break;
104ce8f5370SAlex Deucher 	case PM_PROFILE_HIGH:
105ce8f5370SAlex Deucher 		if (rdev->pm.active_crtc_count > 1)
106ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
107ce8f5370SAlex Deucher 		else
108ce8f5370SAlex Deucher 			rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
109ce8f5370SAlex Deucher 		break;
110ce8f5370SAlex Deucher 	}
111ce8f5370SAlex Deucher 
112ce8f5370SAlex Deucher 	if (rdev->pm.active_crtc_count == 0) {
113ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
114ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
115ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
116ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
117ce8f5370SAlex Deucher 	} else {
118ce8f5370SAlex Deucher 		rdev->pm.requested_power_state_index =
119ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
120ce8f5370SAlex Deucher 		rdev->pm.requested_clock_mode_index =
121ce8f5370SAlex Deucher 			rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
122ce8f5370SAlex Deucher 	}
123ce8f5370SAlex Deucher }
124c913e23aSRafał Miłecki 
1255876dd24SMatthew Garrett static void radeon_unmap_vram_bos(struct radeon_device *rdev)
1265876dd24SMatthew Garrett {
1275876dd24SMatthew Garrett 	struct radeon_bo *bo, *n;
1285876dd24SMatthew Garrett 
1295876dd24SMatthew Garrett 	if (list_empty(&rdev->gem.objects))
1305876dd24SMatthew Garrett 		return;
1315876dd24SMatthew Garrett 
1325876dd24SMatthew Garrett 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
1335876dd24SMatthew Garrett 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
1345876dd24SMatthew Garrett 			ttm_bo_unmap_virtual(&bo->tbo);
1355876dd24SMatthew Garrett 	}
1365876dd24SMatthew Garrett }
1375876dd24SMatthew Garrett 
138ce8f5370SAlex Deucher static void radeon_sync_with_vblank(struct radeon_device *rdev)
139ce8f5370SAlex Deucher {
140ce8f5370SAlex Deucher 	if (rdev->pm.active_crtcs) {
141ce8f5370SAlex Deucher 		rdev->pm.vblank_sync = false;
142ce8f5370SAlex Deucher 		wait_event_timeout(
143ce8f5370SAlex Deucher 			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
144ce8f5370SAlex Deucher 			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
145ce8f5370SAlex Deucher 	}
146ce8f5370SAlex Deucher }
147ce8f5370SAlex Deucher 
148ce8f5370SAlex Deucher static void radeon_set_power_state(struct radeon_device *rdev)
149ce8f5370SAlex Deucher {
150ce8f5370SAlex Deucher 	u32 sclk, mclk;
15192645879SAlex Deucher 	bool misc_after = false;
152ce8f5370SAlex Deucher 
153ce8f5370SAlex Deucher 	if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
154ce8f5370SAlex Deucher 	    (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
155ce8f5370SAlex Deucher 		return;
156ce8f5370SAlex Deucher 
157ce8f5370SAlex Deucher 	if (radeon_gui_idle(rdev)) {
158ce8f5370SAlex Deucher 		sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
159ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].sclk;
160ce8f5370SAlex Deucher 		if (sclk > rdev->clock.default_sclk)
161ce8f5370SAlex Deucher 			sclk = rdev->clock.default_sclk;
162ce8f5370SAlex Deucher 
163ce8f5370SAlex Deucher 		mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
164ce8f5370SAlex Deucher 			clock_info[rdev->pm.requested_clock_mode_index].mclk;
165ce8f5370SAlex Deucher 		if (mclk > rdev->clock.default_mclk)
166ce8f5370SAlex Deucher 			mclk = rdev->clock.default_mclk;
167ce8f5370SAlex Deucher 
16892645879SAlex Deucher 		/* upvolt before raising clocks, downvolt after lowering clocks */
16992645879SAlex Deucher 		if (sclk < rdev->pm.current_sclk)
17092645879SAlex Deucher 			misc_after = true;
17192645879SAlex Deucher 
17292645879SAlex Deucher 		radeon_sync_with_vblank(rdev);
17392645879SAlex Deucher 
17492645879SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
17592645879SAlex Deucher 			if (!radeon_pm_in_vbl(rdev))
17692645879SAlex Deucher 				return;
17792645879SAlex Deucher 		}
17892645879SAlex Deucher 
17992645879SAlex Deucher 		radeon_pm_prepare(rdev);
18092645879SAlex Deucher 
18192645879SAlex Deucher 		if (!misc_after)
182ce8f5370SAlex Deucher 			/* voltage, pcie lanes, etc.*/
183ce8f5370SAlex Deucher 			radeon_pm_misc(rdev);
184ce8f5370SAlex Deucher 
185ce8f5370SAlex Deucher 		/* set engine clock */
186ce8f5370SAlex Deucher 		if (sclk != rdev->pm.current_sclk) {
187ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
188ce8f5370SAlex Deucher 			radeon_set_engine_clock(rdev, sclk);
189ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
190ce8f5370SAlex Deucher 			rdev->pm.current_sclk = sclk;
191ce8a3eb2SAlex Deucher 			DRM_DEBUG("Setting: e: %d\n", sclk);
192ce8f5370SAlex Deucher 		}
193ce8f5370SAlex Deucher 
194ce8f5370SAlex Deucher 		/* set memory clock */
195ce8f5370SAlex Deucher 		if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
196ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, false);
197ce8f5370SAlex Deucher 			radeon_set_memory_clock(rdev, mclk);
198ce8f5370SAlex Deucher 			radeon_pm_debug_check_in_vbl(rdev, true);
199ce8f5370SAlex Deucher 			rdev->pm.current_mclk = mclk;
200ce8a3eb2SAlex Deucher 			DRM_DEBUG("Setting: m: %d\n", mclk);
201ce8f5370SAlex Deucher 		}
20292645879SAlex Deucher 
20392645879SAlex Deucher 		if (misc_after)
20492645879SAlex Deucher 			/* voltage, pcie lanes, etc.*/
20592645879SAlex Deucher 			radeon_pm_misc(rdev);
20692645879SAlex Deucher 
207ce8f5370SAlex Deucher 		radeon_pm_finish(rdev);
208ce8f5370SAlex Deucher 
209ce8f5370SAlex Deucher 		rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
210ce8f5370SAlex Deucher 		rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
211ce8f5370SAlex Deucher 	} else
212ce8a3eb2SAlex Deucher 		DRM_DEBUG("pm: GUI not idle!!!\n");
213ce8f5370SAlex Deucher }
214ce8f5370SAlex Deucher 
215ce8f5370SAlex Deucher static void radeon_pm_set_clocks(struct radeon_device *rdev)
216a424816fSAlex Deucher {
2172aba631cSMatthew Garrett 	int i;
2182aba631cSMatthew Garrett 
219612e06ceSMatthew Garrett 	mutex_lock(&rdev->ddev->struct_mutex);
220612e06ceSMatthew Garrett 	mutex_lock(&rdev->vram_mutex);
221a424816fSAlex Deucher 	mutex_lock(&rdev->cp.mutex);
2224f3218cbSAlex Deucher 
2234f3218cbSAlex Deucher 	/* gui idle int has issues on older chips it seems */
2244f3218cbSAlex Deucher 	if (rdev->family >= CHIP_R600) {
225ce8f5370SAlex Deucher 		if (rdev->irq.installed) {
226a424816fSAlex Deucher 			/* wait for GPU idle */
227a424816fSAlex Deucher 			rdev->pm.gui_idle = false;
228a424816fSAlex Deucher 			rdev->irq.gui_idle = true;
229a424816fSAlex Deucher 			radeon_irq_set(rdev);
230a424816fSAlex Deucher 			wait_event_interruptible_timeout(
231a424816fSAlex Deucher 				rdev->irq.idle_queue, rdev->pm.gui_idle,
232a424816fSAlex Deucher 				msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
233a424816fSAlex Deucher 			rdev->irq.gui_idle = false;
234a424816fSAlex Deucher 			radeon_irq_set(rdev);
235ce8f5370SAlex Deucher 		}
23601434b4bSMatthew Garrett 	} else {
237ce8f5370SAlex Deucher 		if (rdev->cp.ready) {
23801434b4bSMatthew Garrett 			struct radeon_fence *fence;
23901434b4bSMatthew Garrett 			radeon_ring_alloc(rdev, 64);
24001434b4bSMatthew Garrett 			radeon_fence_create(rdev, &fence);
24101434b4bSMatthew Garrett 			radeon_fence_emit(rdev, fence);
24201434b4bSMatthew Garrett 			radeon_ring_commit(rdev);
24301434b4bSMatthew Garrett 			radeon_fence_wait(fence, false);
24401434b4bSMatthew Garrett 			radeon_fence_unref(&fence);
2454f3218cbSAlex Deucher 		}
246ce8f5370SAlex Deucher 	}
2475876dd24SMatthew Garrett 	radeon_unmap_vram_bos(rdev);
2485876dd24SMatthew Garrett 
249ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2502aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2512aba631cSMatthew Garrett 			if (rdev->pm.active_crtcs & (1 << i)) {
2522aba631cSMatthew Garrett 				rdev->pm.req_vblank |= (1 << i);
2532aba631cSMatthew Garrett 				drm_vblank_get(rdev->ddev, i);
2542aba631cSMatthew Garrett 			}
2552aba631cSMatthew Garrett 		}
2562aba631cSMatthew Garrett 	}
2572aba631cSMatthew Garrett 
258ce8f5370SAlex Deucher 	radeon_set_power_state(rdev);
2592aba631cSMatthew Garrett 
260ce8f5370SAlex Deucher 	if (rdev->irq.installed) {
2612aba631cSMatthew Garrett 		for (i = 0; i < rdev->num_crtc; i++) {
2622aba631cSMatthew Garrett 			if (rdev->pm.req_vblank & (1 << i)) {
2632aba631cSMatthew Garrett 				rdev->pm.req_vblank &= ~(1 << i);
2642aba631cSMatthew Garrett 				drm_vblank_put(rdev->ddev, i);
2652aba631cSMatthew Garrett 			}
2662aba631cSMatthew Garrett 		}
2672aba631cSMatthew Garrett 	}
268a424816fSAlex Deucher 
269a424816fSAlex Deucher 	/* update display watermarks based on new power state */
270a424816fSAlex Deucher 	radeon_update_bandwidth_info(rdev);
271a424816fSAlex Deucher 	if (rdev->pm.active_crtc_count)
272a424816fSAlex Deucher 		radeon_bandwidth_update(rdev);
273a424816fSAlex Deucher 
274ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
2752aba631cSMatthew Garrett 
276a424816fSAlex Deucher 	mutex_unlock(&rdev->cp.mutex);
277612e06ceSMatthew Garrett 	mutex_unlock(&rdev->vram_mutex);
278612e06ceSMatthew Garrett 	mutex_unlock(&rdev->ddev->struct_mutex);
279a424816fSAlex Deucher }
280a424816fSAlex Deucher 
281ce8f5370SAlex Deucher static ssize_t radeon_get_pm_profile(struct device *dev,
282a424816fSAlex Deucher 				     struct device_attribute *attr,
283a424816fSAlex Deucher 				     char *buf)
284a424816fSAlex Deucher {
285a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
286a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
287ce8f5370SAlex Deucher 	int cp = rdev->pm.profile;
288a424816fSAlex Deucher 
289a424816fSAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
290ce8f5370SAlex Deucher 			(cp == PM_PROFILE_AUTO) ? "auto" :
291ce8f5370SAlex Deucher 			(cp == PM_PROFILE_LOW) ? "low" :
292ce8f5370SAlex Deucher 			(cp == PM_PROFILE_HIGH) ? "high" : "default");
293a424816fSAlex Deucher }
294a424816fSAlex Deucher 
295ce8f5370SAlex Deucher static ssize_t radeon_set_pm_profile(struct device *dev,
296a424816fSAlex Deucher 				     struct device_attribute *attr,
297a424816fSAlex Deucher 				     const char *buf,
298a424816fSAlex Deucher 				     size_t count)
299a424816fSAlex Deucher {
300a424816fSAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
301a424816fSAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
302a424816fSAlex Deucher 
303a424816fSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
304ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
305ce8f5370SAlex Deucher 		if (strncmp("default", buf, strlen("default")) == 0)
306ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
307ce8f5370SAlex Deucher 		else if (strncmp("auto", buf, strlen("auto")) == 0)
308ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_AUTO;
309ce8f5370SAlex Deucher 		else if (strncmp("low", buf, strlen("low")) == 0)
310ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_LOW;
311*c9e75b21SAlex Deucher 		else if (strncmp("mid", buf, strlen("mid")) == 0)
312*c9e75b21SAlex Deucher 			rdev->pm.profile = PM_PROFILE_MID;
313ce8f5370SAlex Deucher 		else if (strncmp("high", buf, strlen("high")) == 0)
314ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_HIGH;
315ce8f5370SAlex Deucher 		else {
316ce8f5370SAlex Deucher 			DRM_ERROR("invalid power profile!\n");
317ce8f5370SAlex Deucher 			goto fail;
318ce8f5370SAlex Deucher 		}
319ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
320ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
321ce8f5370SAlex Deucher 	}
322ce8f5370SAlex Deucher fail:
323a424816fSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
324a424816fSAlex Deucher 
325a424816fSAlex Deucher 	return count;
326a424816fSAlex Deucher }
327a424816fSAlex Deucher 
328ce8f5370SAlex Deucher static ssize_t radeon_get_pm_method(struct device *dev,
329ce8f5370SAlex Deucher 				    struct device_attribute *attr,
330ce8f5370SAlex Deucher 				    char *buf)
33156278a8eSAlex Deucher {
332ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
333ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
334ce8f5370SAlex Deucher 	int pm = rdev->pm.pm_method;
33556278a8eSAlex Deucher 
336ce8f5370SAlex Deucher 	return snprintf(buf, PAGE_SIZE, "%s\n",
337ce8f5370SAlex Deucher 			(pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
33856278a8eSAlex Deucher }
33956278a8eSAlex Deucher 
340ce8f5370SAlex Deucher static ssize_t radeon_set_pm_method(struct device *dev,
341ce8f5370SAlex Deucher 				    struct device_attribute *attr,
342ce8f5370SAlex Deucher 				    const char *buf,
343ce8f5370SAlex Deucher 				    size_t count)
344d0d6cb81SRafał Miłecki {
345ce8f5370SAlex Deucher 	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
346ce8f5370SAlex Deucher 	struct radeon_device *rdev = ddev->dev_private;
347ce8f5370SAlex Deucher 
348ce8f5370SAlex Deucher 
349ce8f5370SAlex Deucher 	if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
350ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
351ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_DYNPM;
352ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
353ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
354ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
355ce8f5370SAlex Deucher 	} else if (strncmp("profile", buf, strlen("profile")) == 0) {
356ce8f5370SAlex Deucher 		mutex_lock(&rdev->pm.mutex);
357ce8f5370SAlex Deucher 		rdev->pm.pm_method = PM_METHOD_PROFILE;
358ce8f5370SAlex Deucher 		/* disable dynpm */
359ce8f5370SAlex Deucher 		rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
360ce8f5370SAlex Deucher 		rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
361ce8f5370SAlex Deucher 		cancel_delayed_work(&rdev->pm.dynpm_idle_work);
362ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
363ce8f5370SAlex Deucher 	} else {
364ce8f5370SAlex Deucher 		DRM_ERROR("invalid power method!\n");
365ce8f5370SAlex Deucher 		goto fail;
366d0d6cb81SRafał Miłecki 	}
367ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
368ce8f5370SAlex Deucher fail:
369ce8f5370SAlex Deucher 	return count;
370ce8f5370SAlex Deucher }
371ce8f5370SAlex Deucher 
372ce8f5370SAlex Deucher static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
373ce8f5370SAlex Deucher static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
374ce8f5370SAlex Deucher 
375ce8f5370SAlex Deucher void radeon_pm_suspend(struct radeon_device *rdev)
376ce8f5370SAlex Deucher {
377ce8f5370SAlex Deucher 	mutex_lock(&rdev->pm.mutex);
378ce8f5370SAlex Deucher 	cancel_delayed_work(&rdev->pm.dynpm_idle_work);
379ce8f5370SAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
380ce8f5370SAlex Deucher }
381ce8f5370SAlex Deucher 
382ce8f5370SAlex Deucher void radeon_pm_resume(struct radeon_device *rdev)
383ce8f5370SAlex Deucher {
384f8ed8b4cSAlex Deucher 	/* asic init will reset the default power state */
385f8ed8b4cSAlex Deucher 	mutex_lock(&rdev->pm.mutex);
386f8ed8b4cSAlex Deucher 	rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
387f8ed8b4cSAlex Deucher 	rdev->pm.current_clock_mode_index = 0;
388f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
389f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
390f8ed8b4cSAlex Deucher 	mutex_unlock(&rdev->pm.mutex);
391ce8f5370SAlex Deucher 	radeon_pm_compute_clocks(rdev);
392d0d6cb81SRafał Miłecki }
393d0d6cb81SRafał Miłecki 
3947433874eSRafał Miłecki int radeon_pm_init(struct radeon_device *rdev)
3957433874eSRafał Miłecki {
39626481fb1SDave Airlie 	int ret;
397ce8f5370SAlex Deucher 	/* default to profile method */
398ce8f5370SAlex Deucher 	rdev->pm.pm_method = PM_METHOD_PROFILE;
399f8ed8b4cSAlex Deucher 	rdev->pm.profile = PM_PROFILE_DEFAULT;
400ce8f5370SAlex Deucher 	rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
401ce8f5370SAlex Deucher 	rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
402ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_upclock = true;
403ce8f5370SAlex Deucher 	rdev->pm.dynpm_can_downclock = true;
404f8ed8b4cSAlex Deucher 	rdev->pm.current_sclk = rdev->clock.default_sclk;
405f8ed8b4cSAlex Deucher 	rdev->pm.current_mclk = rdev->clock.default_mclk;
406c913e23aSRafał Miłecki 
40756278a8eSAlex Deucher 	if (rdev->bios) {
40856278a8eSAlex Deucher 		if (rdev->is_atom_bios)
40956278a8eSAlex Deucher 			radeon_atombios_get_power_modes(rdev);
41056278a8eSAlex Deucher 		else
41156278a8eSAlex Deucher 			radeon_combios_get_power_modes(rdev);
412ce8f5370SAlex Deucher 		radeon_pm_init_profile(rdev);
41356278a8eSAlex Deucher 	}
41456278a8eSAlex Deucher 
415ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
416ce8f5370SAlex Deucher 		/* where's the best place to put these? */
41726481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_profile);
41826481fb1SDave Airlie 		if (ret)
41926481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power profile\n");
42026481fb1SDave Airlie 		ret = device_create_file(rdev->dev, &dev_attr_power_method);
42126481fb1SDave Airlie 		if (ret)
42226481fb1SDave Airlie 			DRM_ERROR("failed to create device file for power method\n");
423ce8f5370SAlex Deucher 
424ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
425ce8f5370SAlex Deucher 		rdev->acpi_nb.notifier_call = radeon_acpi_event;
426ce8f5370SAlex Deucher 		register_acpi_notifier(&rdev->acpi_nb);
427ce8f5370SAlex Deucher #endif
428ce8f5370SAlex Deucher 		INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
429ce8f5370SAlex Deucher 
4307433874eSRafał Miłecki 		if (radeon_debugfs_pm_init(rdev)) {
431c142c3e5SRafał Miłecki 			DRM_ERROR("Failed to register debugfs file for PM!\n");
4327433874eSRafał Miłecki 		}
4337433874eSRafał Miłecki 
434c913e23aSRafał Miłecki 		DRM_INFO("radeon: power management initialized\n");
435ce8f5370SAlex Deucher 	}
436c913e23aSRafał Miłecki 
4377433874eSRafał Miłecki 	return 0;
4387433874eSRafał Miłecki }
4397433874eSRafał Miłecki 
44029fb52caSAlex Deucher void radeon_pm_fini(struct radeon_device *rdev)
44129fb52caSAlex Deucher {
442ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states > 1) {
443a424816fSAlex Deucher 		mutex_lock(&rdev->pm.mutex);
444ce8f5370SAlex Deucher 		if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
445ce8f5370SAlex Deucher 			rdev->pm.profile = PM_PROFILE_DEFAULT;
446ce8f5370SAlex Deucher 			radeon_pm_update_profile(rdev);
447ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
448ce8f5370SAlex Deucher 		} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
449ce8f5370SAlex Deucher 			/* cancel work */
450ce8f5370SAlex Deucher 			cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
451ce8f5370SAlex Deucher 			/* reset default clocks */
452ce8f5370SAlex Deucher 			rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
453ce8f5370SAlex Deucher 			rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
454ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
45558e21dffSAlex Deucher 		}
456ce8f5370SAlex Deucher 		mutex_unlock(&rdev->pm.mutex);
45758e21dffSAlex Deucher 
458ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_profile);
459ce8f5370SAlex Deucher 		device_remove_file(rdev->dev, &dev_attr_power_method);
460ce8f5370SAlex Deucher #ifdef CONFIG_ACPI
461ce8f5370SAlex Deucher 		unregister_acpi_notifier(&rdev->acpi_nb);
462ce8f5370SAlex Deucher #endif
463ce8f5370SAlex Deucher 	}
464a424816fSAlex Deucher 
46529fb52caSAlex Deucher 	if (rdev->pm.i2c_bus)
46629fb52caSAlex Deucher 		radeon_i2c_destroy(rdev->pm.i2c_bus);
46729fb52caSAlex Deucher }
46829fb52caSAlex Deucher 
469c913e23aSRafał Miłecki void radeon_pm_compute_clocks(struct radeon_device *rdev)
470c913e23aSRafał Miłecki {
471c913e23aSRafał Miłecki 	struct drm_device *ddev = rdev->ddev;
472a48b9b4eSAlex Deucher 	struct drm_crtc *crtc;
473c913e23aSRafał Miłecki 	struct radeon_crtc *radeon_crtc;
474c913e23aSRafał Miłecki 
475ce8f5370SAlex Deucher 	if (rdev->pm.num_power_states < 2)
476ce8f5370SAlex Deucher 		return;
477ce8f5370SAlex Deucher 
478c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
479c913e23aSRafał Miłecki 
480c913e23aSRafał Miłecki 	rdev->pm.active_crtcs = 0;
481a48b9b4eSAlex Deucher 	rdev->pm.active_crtc_count = 0;
482a48b9b4eSAlex Deucher 	list_for_each_entry(crtc,
483a48b9b4eSAlex Deucher 		&ddev->mode_config.crtc_list, head) {
484a48b9b4eSAlex Deucher 		radeon_crtc = to_radeon_crtc(crtc);
485a48b9b4eSAlex Deucher 		if (radeon_crtc->enabled) {
486c913e23aSRafał Miłecki 			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
487a48b9b4eSAlex Deucher 			rdev->pm.active_crtc_count++;
488c913e23aSRafał Miłecki 		}
489c913e23aSRafał Miłecki 	}
490c913e23aSRafał Miłecki 
491ce8f5370SAlex Deucher 	if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
492ce8f5370SAlex Deucher 		radeon_pm_update_profile(rdev);
493ce8f5370SAlex Deucher 		radeon_pm_set_clocks(rdev);
494ce8f5370SAlex Deucher 	} else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
495ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
496a48b9b4eSAlex Deucher 			if (rdev->pm.active_crtc_count > 1) {
497ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
498ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
499c913e23aSRafał Miłecki 
500ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
501ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
502ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
503ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
504c913e23aSRafał Miłecki 
505c913e23aSRafał Miłecki 					DRM_DEBUG("radeon: dynamic power management deactivated\n");
506c913e23aSRafał Miłecki 				}
507a48b9b4eSAlex Deucher 			} else if (rdev->pm.active_crtc_count == 1) {
508c913e23aSRafał Miłecki 				/* TODO: Increase clocks if needed for current mode */
509c913e23aSRafał Miłecki 
510ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
511ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
512ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
513ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
514ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
515c913e23aSRafał Miłecki 
516ce8f5370SAlex Deucher 					queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
517c913e23aSRafał Miłecki 							   msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
518ce8f5370SAlex Deucher 				} else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
519ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
520ce8f5370SAlex Deucher 					queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
521c913e23aSRafał Miłecki 							   msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
522c913e23aSRafał Miłecki 					DRM_DEBUG("radeon: dynamic power management activated\n");
523c913e23aSRafał Miłecki 				}
524a48b9b4eSAlex Deucher 			} else { /* count == 0 */
525ce8f5370SAlex Deucher 				if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
526ce8f5370SAlex Deucher 					cancel_delayed_work(&rdev->pm.dynpm_idle_work);
527c913e23aSRafał Miłecki 
528ce8f5370SAlex Deucher 					rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
529ce8f5370SAlex Deucher 					rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
530ce8f5370SAlex Deucher 					radeon_pm_get_dynpm_state(rdev);
531ce8f5370SAlex Deucher 					radeon_pm_set_clocks(rdev);
532ce8f5370SAlex Deucher 				}
533ce8f5370SAlex Deucher 			}
53473a6d3fcSRafał Miłecki 		}
535c913e23aSRafał Miłecki 	}
536c913e23aSRafał Miłecki 
537c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
538c913e23aSRafał Miłecki }
539c913e23aSRafał Miłecki 
540ce8f5370SAlex Deucher static bool radeon_pm_in_vbl(struct radeon_device *rdev)
541f735261bSDave Airlie {
542539d2418SAlex Deucher 	u32 stat_crtc = 0, vbl = 0, position = 0;
543f735261bSDave Airlie 	bool in_vbl = true;
544f735261bSDave Airlie 
545bae6b562SAlex Deucher 	if (ASIC_IS_DCE4(rdev)) {
546f735261bSDave Airlie 		if (rdev->pm.active_crtcs & (1 << 0)) {
547539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
548539d2418SAlex Deucher 				     EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
549539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
550539d2418SAlex Deucher 					  EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
551f735261bSDave Airlie 		}
552f735261bSDave Airlie 		if (rdev->pm.active_crtcs & (1 << 1)) {
553539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
554539d2418SAlex Deucher 				     EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
555539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
556539d2418SAlex Deucher 					  EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
557bae6b562SAlex Deucher 		}
558bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 2)) {
559539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
560539d2418SAlex Deucher 				     EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
561539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
562539d2418SAlex Deucher 					  EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
563bae6b562SAlex Deucher 		}
564bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 3)) {
565539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
566539d2418SAlex Deucher 				     EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
567539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
568539d2418SAlex Deucher 					  EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
569bae6b562SAlex Deucher 		}
570bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 4)) {
571539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
572539d2418SAlex Deucher 				     EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
573539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
574539d2418SAlex Deucher 					  EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
575bae6b562SAlex Deucher 		}
576bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 5)) {
577539d2418SAlex Deucher 			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
578539d2418SAlex Deucher 				     EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
579539d2418SAlex Deucher 			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
580539d2418SAlex Deucher 					  EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
581bae6b562SAlex Deucher 		}
582bae6b562SAlex Deucher 	} else if (ASIC_IS_AVIVO(rdev)) {
583bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 0)) {
584539d2418SAlex Deucher 			vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
585539d2418SAlex Deucher 			position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
586bae6b562SAlex Deucher 		}
587bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 1)) {
588539d2418SAlex Deucher 			vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
589539d2418SAlex Deucher 			position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
590bae6b562SAlex Deucher 		}
591539d2418SAlex Deucher 		if (position < vbl && position > 1)
592539d2418SAlex Deucher 			in_vbl = false;
593bae6b562SAlex Deucher 	} else {
594bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 0)) {
595bae6b562SAlex Deucher 			stat_crtc = RREG32(RADEON_CRTC_STATUS);
596bae6b562SAlex Deucher 			if (!(stat_crtc & 1))
597bae6b562SAlex Deucher 				in_vbl = false;
598bae6b562SAlex Deucher 		}
599bae6b562SAlex Deucher 		if (rdev->pm.active_crtcs & (1 << 1)) {
600bae6b562SAlex Deucher 			stat_crtc = RREG32(RADEON_CRTC2_STATUS);
601bae6b562SAlex Deucher 			if (!(stat_crtc & 1))
602f735261bSDave Airlie 				in_vbl = false;
603f735261bSDave Airlie 		}
604f735261bSDave Airlie 	}
605f81f2024SMatthew Garrett 
606539d2418SAlex Deucher 	if (position < vbl && position > 1)
607539d2418SAlex Deucher 		in_vbl = false;
608539d2418SAlex Deucher 
609f81f2024SMatthew Garrett 	return in_vbl;
610f81f2024SMatthew Garrett }
611f81f2024SMatthew Garrett 
612ce8f5370SAlex Deucher static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
613f81f2024SMatthew Garrett {
614f81f2024SMatthew Garrett 	u32 stat_crtc = 0;
615f81f2024SMatthew Garrett 	bool in_vbl = radeon_pm_in_vbl(rdev);
616f81f2024SMatthew Garrett 
617f735261bSDave Airlie 	if (in_vbl == false)
618ce8a3eb2SAlex Deucher 		DRM_DEBUG("not in vbl for pm change %08x at %s\n", stat_crtc,
619bae6b562SAlex Deucher 			 finish ? "exit" : "entry");
620f735261bSDave Airlie 	return in_vbl;
621f735261bSDave Airlie }
622c913e23aSRafał Miłecki 
623ce8f5370SAlex Deucher static void radeon_dynpm_idle_work_handler(struct work_struct *work)
624c913e23aSRafał Miłecki {
625c913e23aSRafał Miłecki 	struct radeon_device *rdev;
626d9932a32SMatthew Garrett 	int resched;
627c913e23aSRafał Miłecki 	rdev = container_of(work, struct radeon_device,
628ce8f5370SAlex Deucher 				pm.dynpm_idle_work.work);
629c913e23aSRafał Miłecki 
630d9932a32SMatthew Garrett 	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
631c913e23aSRafał Miłecki 	mutex_lock(&rdev->pm.mutex);
632ce8f5370SAlex Deucher 	if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
633c913e23aSRafał Miłecki 		unsigned long irq_flags;
634c913e23aSRafał Miłecki 		int not_processed = 0;
635c913e23aSRafał Miłecki 
636c913e23aSRafał Miłecki 		read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
637c913e23aSRafał Miłecki 		if (!list_empty(&rdev->fence_drv.emited)) {
638c913e23aSRafał Miłecki 			struct list_head *ptr;
639c913e23aSRafał Miłecki 			list_for_each(ptr, &rdev->fence_drv.emited) {
640c913e23aSRafał Miłecki 				/* count up to 3, that's enought info */
641c913e23aSRafał Miłecki 				if (++not_processed >= 3)
642c913e23aSRafał Miłecki 					break;
643c913e23aSRafał Miłecki 			}
644c913e23aSRafał Miłecki 		}
645c913e23aSRafał Miłecki 		read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
646c913e23aSRafał Miłecki 
647c913e23aSRafał Miłecki 		if (not_processed >= 3) { /* should upclock */
648ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
649ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
650ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
651ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_upclock) {
652ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
653ce8f5370SAlex Deucher 					DYNPM_ACTION_UPCLOCK;
654ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
655c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
656c913e23aSRafał Miłecki 			}
657c913e23aSRafał Miłecki 		} else if (not_processed == 0) { /* should downclock */
658ce8f5370SAlex Deucher 			if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
659ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
660ce8f5370SAlex Deucher 			} else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
661ce8f5370SAlex Deucher 				   rdev->pm.dynpm_can_downclock) {
662ce8f5370SAlex Deucher 				rdev->pm.dynpm_planned_action =
663ce8f5370SAlex Deucher 					DYNPM_ACTION_DOWNCLOCK;
664ce8f5370SAlex Deucher 				rdev->pm.dynpm_action_timeout = jiffies +
665c913e23aSRafał Miłecki 				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
666c913e23aSRafał Miłecki 			}
667c913e23aSRafał Miłecki 		}
668c913e23aSRafał Miłecki 
669d7311171SAlex Deucher 		/* Note, radeon_pm_set_clocks is called with static_switch set
670d7311171SAlex Deucher 		 * to false since we want to wait for vbl to avoid flicker.
671d7311171SAlex Deucher 		 */
672ce8f5370SAlex Deucher 		if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
673ce8f5370SAlex Deucher 		    jiffies > rdev->pm.dynpm_action_timeout) {
674ce8f5370SAlex Deucher 			radeon_pm_get_dynpm_state(rdev);
675ce8f5370SAlex Deucher 			radeon_pm_set_clocks(rdev);
676c913e23aSRafał Miłecki 		}
677c913e23aSRafał Miłecki 	}
678c913e23aSRafał Miłecki 	mutex_unlock(&rdev->pm.mutex);
679d9932a32SMatthew Garrett 	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
680c913e23aSRafał Miłecki 
681ce8f5370SAlex Deucher 	queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
682c913e23aSRafał Miłecki 					msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
683c913e23aSRafał Miłecki }
684c913e23aSRafał Miłecki 
6857433874eSRafał Miłecki /*
6867433874eSRafał Miłecki  * Debugfs info
6877433874eSRafał Miłecki  */
6887433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
6897433874eSRafał Miłecki 
6907433874eSRafał Miłecki static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
6917433874eSRafał Miłecki {
6927433874eSRafał Miłecki 	struct drm_info_node *node = (struct drm_info_node *) m->private;
6937433874eSRafał Miłecki 	struct drm_device *dev = node->minor->dev;
6947433874eSRafał Miłecki 	struct radeon_device *rdev = dev->dev_private;
6957433874eSRafał Miłecki 
6966234077dSRafał Miłecki 	seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
6976234077dSRafał Miłecki 	seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
6986234077dSRafał Miłecki 	seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
6996234077dSRafał Miłecki 	if (rdev->asic->get_memory_clock)
7006234077dSRafał Miłecki 		seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
701aa5120d2SRafał Miłecki 	if (rdev->asic->get_pcie_lanes)
702aa5120d2SRafał Miłecki 		seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
7037433874eSRafał Miłecki 
7047433874eSRafał Miłecki 	return 0;
7057433874eSRafał Miłecki }
7067433874eSRafał Miłecki 
7077433874eSRafał Miłecki static struct drm_info_list radeon_pm_info_list[] = {
7087433874eSRafał Miłecki 	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
7097433874eSRafał Miłecki };
7107433874eSRafał Miłecki #endif
7117433874eSRafał Miłecki 
712c913e23aSRafał Miłecki static int radeon_debugfs_pm_init(struct radeon_device *rdev)
7137433874eSRafał Miłecki {
7147433874eSRafał Miłecki #if defined(CONFIG_DEBUG_FS)
7157433874eSRafał Miłecki 	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
7167433874eSRafał Miłecki #else
7177433874eSRafał Miłecki 	return 0;
7187433874eSRafał Miłecki #endif
7197433874eSRafał Miłecki }
720