blob: aaa19dc418a0f0bf194d789592de943d3e5c2dca [file] [log] [blame]
Rafał Miłecki74338742009-11-03 00:53:02 +01001/*
2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
16 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
18 * OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Authors: Rafał Miłecki <zajec5@gmail.com>
Alex Deucher56278a82009-12-28 13:58:44 -050021 * Alex Deucher <alexdeucher@gmail.com>
Rafał Miłecki74338742009-11-03 00:53:02 +010022 */
23#include "drmP.h"
24#include "radeon.h"
Dave Airlief7352612010-02-18 15:58:36 +100025#include "avivod.h"
Alex Deucher8a83ec52011-04-12 14:49:23 -040026#include "atom.h"
Alex Deucherce8f5372010-05-07 15:10:16 -040027#ifdef CONFIG_ACPI
28#include <linux/acpi.h>
29#endif
30#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040031#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010033
Rafał Miłeckic913e232009-12-22 23:02:16 +010034#define RADEON_IDLE_LOOP_MS 100
35#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010036#define RADEON_WAIT_VBLANK_TIMEOUT 200
Alex Deucher2031f772010-04-22 12:52:11 -040037#define RADEON_WAIT_IDLE_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010038
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040039static const char *radeon_pm_state_type_name[5] = {
40 "Default",
41 "Powersave",
42 "Battery",
43 "Balanced",
44 "Performance",
45};
46
Alex Deucherce8f5372010-05-07 15:10:16 -040047static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010048static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040049static bool radeon_pm_in_vbl(struct radeon_device *rdev);
50static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
51static void radeon_pm_update_profile(struct radeon_device *rdev);
52static void radeon_pm_set_clocks(struct radeon_device *rdev);
53
54#define ACPI_AC_CLASS "ac_adapter"
55
56#ifdef CONFIG_ACPI
57static int radeon_acpi_event(struct notifier_block *nb,
58 unsigned long val,
59 void *data)
60{
61 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
62 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
63
64 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
65 if (power_supply_is_system_supplied() > 0)
Dave Airlied9fdaaf2010-08-02 10:42:55 +100066 DRM_DEBUG_DRIVER("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040067 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +100068 DRM_DEBUG_DRIVER("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040069
70 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
71 if (rdev->pm.profile == PM_PROFILE_AUTO) {
72 mutex_lock(&rdev->pm.mutex);
73 radeon_pm_update_profile(rdev);
74 radeon_pm_set_clocks(rdev);
75 mutex_unlock(&rdev->pm.mutex);
76 }
77 }
78 }
79
80 return NOTIFY_OK;
81}
82#endif
83
84static void radeon_pm_update_profile(struct radeon_device *rdev)
85{
86 switch (rdev->pm.profile) {
87 case PM_PROFILE_DEFAULT:
88 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
89 break;
90 case PM_PROFILE_AUTO:
91 if (power_supply_is_system_supplied() > 0) {
92 if (rdev->pm.active_crtc_count > 1)
93 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
94 else
95 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
96 } else {
97 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -040098 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040099 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400100 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400101 }
102 break;
103 case PM_PROFILE_LOW:
104 if (rdev->pm.active_crtc_count > 1)
105 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
106 else
107 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
108 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400109 case PM_PROFILE_MID:
110 if (rdev->pm.active_crtc_count > 1)
111 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
112 else
113 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
114 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400115 case PM_PROFILE_HIGH:
116 if (rdev->pm.active_crtc_count > 1)
117 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
118 else
119 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
120 break;
121 }
122
123 if (rdev->pm.active_crtc_count == 0) {
124 rdev->pm.requested_power_state_index =
125 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
126 rdev->pm.requested_clock_mode_index =
127 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
128 } else {
129 rdev->pm.requested_power_state_index =
130 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
131 rdev->pm.requested_clock_mode_index =
132 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
133 }
134}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100135
Matthew Garrett5876dd22010-04-26 15:52:20 -0400136static void radeon_unmap_vram_bos(struct radeon_device *rdev)
137{
138 struct radeon_bo *bo, *n;
139
140 if (list_empty(&rdev->gem.objects))
141 return;
142
143 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
144 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
145 ttm_bo_unmap_virtual(&bo->tbo);
146 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400147}
148
Alex Deucherce8f5372010-05-07 15:10:16 -0400149static void radeon_sync_with_vblank(struct radeon_device *rdev)
150{
151 if (rdev->pm.active_crtcs) {
152 rdev->pm.vblank_sync = false;
153 wait_event_timeout(
154 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
155 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
156 }
157}
158
159static void radeon_set_power_state(struct radeon_device *rdev)
160{
161 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400162 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400163
164 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
165 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
166 return;
167
168 if (radeon_gui_idle(rdev)) {
169 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
170 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500171 if (sclk > rdev->pm.default_sclk)
172 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400173
174 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
175 clock_info[rdev->pm.requested_clock_mode_index].mclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500176 if (mclk > rdev->pm.default_mclk)
177 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400178
Alex Deucher92645872010-05-27 17:01:41 -0400179 /* upvolt before raising clocks, downvolt after lowering clocks */
180 if (sclk < rdev->pm.current_sclk)
181 misc_after = true;
182
183 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400184
185 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400186 if (!radeon_pm_in_vbl(rdev))
187 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400188 }
189
Alex Deucher92645872010-05-27 17:01:41 -0400190 radeon_pm_prepare(rdev);
191
192 if (!misc_after)
193 /* voltage, pcie lanes, etc.*/
194 radeon_pm_misc(rdev);
195
196 /* set engine clock */
197 if (sclk != rdev->pm.current_sclk) {
198 radeon_pm_debug_check_in_vbl(rdev, false);
199 radeon_set_engine_clock(rdev, sclk);
200 radeon_pm_debug_check_in_vbl(rdev, true);
201 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000202 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400203 }
204
205 /* set memory clock */
206 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
207 radeon_pm_debug_check_in_vbl(rdev, false);
208 radeon_set_memory_clock(rdev, mclk);
209 radeon_pm_debug_check_in_vbl(rdev, true);
210 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000211 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400212 }
213
214 if (misc_after)
215 /* voltage, pcie lanes, etc.*/
216 radeon_pm_misc(rdev);
217
218 radeon_pm_finish(rdev);
219
Alex Deucherce8f5372010-05-07 15:10:16 -0400220 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
221 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
222 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000223 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400224}
225
226static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400227{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400228 int i;
229
Alex Deucher4e186b22010-08-13 10:53:35 -0400230 /* no need to take locks, etc. if nothing's going to change */
231 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
232 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
233 return;
234
Matthew Garrett612e06c2010-04-27 17:16:58 -0400235 mutex_lock(&rdev->ddev->struct_mutex);
236 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400237 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400238
239 /* gui idle int has issues on older chips it seems */
240 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400241 if (rdev->irq.installed) {
242 /* wait for GPU idle */
243 rdev->pm.gui_idle = false;
244 rdev->irq.gui_idle = true;
245 radeon_irq_set(rdev);
246 wait_event_interruptible_timeout(
247 rdev->irq.idle_queue, rdev->pm.gui_idle,
248 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
249 rdev->irq.gui_idle = false;
250 radeon_irq_set(rdev);
251 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400252 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400253 if (rdev->cp.ready) {
254 struct radeon_fence *fence;
255 radeon_ring_alloc(rdev, 64);
256 radeon_fence_create(rdev, &fence);
257 radeon_fence_emit(rdev, fence);
258 radeon_ring_commit(rdev);
259 radeon_fence_wait(fence, false);
260 radeon_fence_unref(&fence);
261 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400262 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400263 radeon_unmap_vram_bos(rdev);
264
Alex Deucherce8f5372010-05-07 15:10:16 -0400265 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400266 for (i = 0; i < rdev->num_crtc; i++) {
267 if (rdev->pm.active_crtcs & (1 << i)) {
268 rdev->pm.req_vblank |= (1 << i);
269 drm_vblank_get(rdev->ddev, i);
270 }
271 }
272 }
Alex Deucher539d2412010-04-29 00:22:43 -0400273
Alex Deucherce8f5372010-05-07 15:10:16 -0400274 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400275
Alex Deucherce8f5372010-05-07 15:10:16 -0400276 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400277 for (i = 0; i < rdev->num_crtc; i++) {
278 if (rdev->pm.req_vblank & (1 << i)) {
279 rdev->pm.req_vblank &= ~(1 << i);
280 drm_vblank_put(rdev->ddev, i);
281 }
282 }
283 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400284
Alex Deuchera4248162010-04-24 14:50:23 -0400285 /* update display watermarks based on new power state */
286 radeon_update_bandwidth_info(rdev);
287 if (rdev->pm.active_crtc_count)
288 radeon_bandwidth_update(rdev);
289
Alex Deucherce8f5372010-05-07 15:10:16 -0400290 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400291
Alex Deuchera4248162010-04-24 14:50:23 -0400292 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400293 mutex_unlock(&rdev->vram_mutex);
294 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400295}
296
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400297static void radeon_pm_print_states(struct radeon_device *rdev)
298{
299 int i, j;
300 struct radeon_power_state *power_state;
301 struct radeon_pm_clock_info *clock_info;
302
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000303 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400304 for (i = 0; i < rdev->pm.num_power_states; i++) {
305 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000306 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400307 radeon_pm_state_type_name[power_state->type]);
308 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000309 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400310 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000311 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400312 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000313 DRM_DEBUG_DRIVER("\tSingle display only\n");
314 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400315 for (j = 0; j < power_state->num_clock_modes; j++) {
316 clock_info = &(power_state->clock_info[j]);
317 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000318 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400319 j,
320 clock_info->sclk * 10,
321 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
322 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000323 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400324 j,
325 clock_info->sclk * 10,
326 clock_info->mclk * 10,
327 clock_info->voltage.voltage,
328 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
329 }
330 }
331}
332
Alex Deucherce8f5372010-05-07 15:10:16 -0400333static ssize_t radeon_get_pm_profile(struct device *dev,
334 struct device_attribute *attr,
335 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400336{
337 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
338 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400339 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400340
Alex Deucherce8f5372010-05-07 15:10:16 -0400341 return snprintf(buf, PAGE_SIZE, "%s\n",
342 (cp == PM_PROFILE_AUTO) ? "auto" :
343 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100344 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400345 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400346}
347
Alex Deucherce8f5372010-05-07 15:10:16 -0400348static ssize_t radeon_set_pm_profile(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf,
351 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400352{
353 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
354 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400355
356 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400357 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
358 if (strncmp("default", buf, strlen("default")) == 0)
359 rdev->pm.profile = PM_PROFILE_DEFAULT;
360 else if (strncmp("auto", buf, strlen("auto")) == 0)
361 rdev->pm.profile = PM_PROFILE_AUTO;
362 else if (strncmp("low", buf, strlen("low")) == 0)
363 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400364 else if (strncmp("mid", buf, strlen("mid")) == 0)
365 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400366 else if (strncmp("high", buf, strlen("high")) == 0)
367 rdev->pm.profile = PM_PROFILE_HIGH;
368 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000369 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400370 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400371 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400372 radeon_pm_update_profile(rdev);
373 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000374 } else
375 count = -EINVAL;
376
Alex Deucherce8f5372010-05-07 15:10:16 -0400377fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400378 mutex_unlock(&rdev->pm.mutex);
379
380 return count;
381}
382
Alex Deucherce8f5372010-05-07 15:10:16 -0400383static ssize_t radeon_get_pm_method(struct device *dev,
384 struct device_attribute *attr,
385 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400386{
387 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
388 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400389 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400390
391 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400392 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400393}
394
Alex Deucherce8f5372010-05-07 15:10:16 -0400395static ssize_t radeon_set_pm_method(struct device *dev,
396 struct device_attribute *attr,
397 const char *buf,
398 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400399{
400 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
401 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400402
Alex Deucherce8f5372010-05-07 15:10:16 -0400403
404 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400405 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400406 rdev->pm.pm_method = PM_METHOD_DYNPM;
407 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
408 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400409 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400410 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
411 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400412 /* disable dynpm */
413 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
414 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000415 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400416 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100417 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400418 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000419 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400420 goto fail;
421 }
422 radeon_pm_compute_clocks(rdev);
423fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400424 return count;
425}
426
Alex Deucherce8f5372010-05-07 15:10:16 -0400427static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
428static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400429
Alex Deucher21a81222010-07-02 12:58:16 -0400430static ssize_t radeon_hwmon_show_temp(struct device *dev,
431 struct device_attribute *attr,
432 char *buf)
433{
434 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
435 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500436 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400437
438 switch (rdev->pm.int_thermal_type) {
439 case THERMAL_TYPE_RV6XX:
440 temp = rv6xx_get_temp(rdev);
441 break;
442 case THERMAL_TYPE_RV770:
443 temp = rv770_get_temp(rdev);
444 break;
445 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500446 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400447 temp = evergreen_get_temp(rdev);
448 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500449 case THERMAL_TYPE_SUMO:
450 temp = sumo_get_temp(rdev);
451 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400452 default:
453 temp = 0;
454 break;
455 }
456
457 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
458}
459
460static ssize_t radeon_hwmon_show_name(struct device *dev,
461 struct device_attribute *attr,
462 char *buf)
463{
464 return sprintf(buf, "radeon\n");
465}
466
467static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
468static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
469
470static struct attribute *hwmon_attributes[] = {
471 &sensor_dev_attr_temp1_input.dev_attr.attr,
472 &sensor_dev_attr_name.dev_attr.attr,
473 NULL
474};
475
476static const struct attribute_group hwmon_attrgroup = {
477 .attrs = hwmon_attributes,
478};
479
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200480static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400481{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200482 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400483
484 rdev->pm.int_hwmon_dev = NULL;
485
486 switch (rdev->pm.int_thermal_type) {
487 case THERMAL_TYPE_RV6XX:
488 case THERMAL_TYPE_RV770:
489 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400490 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500491 case THERMAL_TYPE_SUMO:
Alex Deucher21a81222010-07-02 12:58:16 -0400492 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200493 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
494 err = PTR_ERR(rdev->pm.int_hwmon_dev);
495 dev_err(rdev->dev,
496 "Unable to register hwmon device: %d\n", err);
497 break;
498 }
Alex Deucher21a81222010-07-02 12:58:16 -0400499 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
500 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
501 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200502 if (err) {
503 dev_err(rdev->dev,
504 "Unable to create hwmon sysfs file: %d\n", err);
505 hwmon_device_unregister(rdev->dev);
506 }
Alex Deucher21a81222010-07-02 12:58:16 -0400507 break;
508 default:
509 break;
510 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200511
512 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400513}
514
515static void radeon_hwmon_fini(struct radeon_device *rdev)
516{
517 if (rdev->pm.int_hwmon_dev) {
518 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
519 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
520 }
521}
522
Alex Deucherce8f5372010-05-07 15:10:16 -0400523void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500524{
Alex Deucherce8f5372010-05-07 15:10:16 -0400525 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000526 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000527 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
528 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000529 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400530 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100531
532 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500533}
534
Alex Deucherce8f5372010-05-07 15:10:16 -0400535void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100536{
Alex Deuchered18a362011-01-06 21:19:32 -0500537 /* set up the default clocks if the MC ucode is loaded */
538 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
539 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400540 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
541 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400542 if (rdev->pm.default_vddci)
543 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
544 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500545 if (rdev->pm.default_sclk)
546 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
547 if (rdev->pm.default_mclk)
548 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
549 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400550 /* asic init will reset the default power state */
551 mutex_lock(&rdev->pm.mutex);
552 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
553 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500554 rdev->pm.current_sclk = rdev->pm.default_sclk;
555 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400556 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
Alex Deucher2feea492011-04-12 14:49:24 -0400557 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000558 if (rdev->pm.pm_method == PM_METHOD_DYNPM
559 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
560 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100561 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
562 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000563 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400564 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400565 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100566}
567
Rafał Miłecki74338742009-11-03 00:53:02 +0100568int radeon_pm_init(struct radeon_device *rdev)
569{
Dave Airlie26481fb2010-05-18 19:00:14 +1000570 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200571
Alex Deucherce8f5372010-05-07 15:10:16 -0400572 /* default to profile method */
573 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400574 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400575 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
576 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
577 rdev->pm.dynpm_can_upclock = true;
578 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500579 rdev->pm.default_sclk = rdev->clock.default_sclk;
580 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400581 rdev->pm.current_sclk = rdev->clock.default_sclk;
582 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400583 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100584
Alex Deucher56278a82009-12-28 13:58:44 -0500585 if (rdev->bios) {
586 if (rdev->is_atom_bios)
587 radeon_atombios_get_power_modes(rdev);
588 else
589 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400590 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400591 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500592 /* set up the default clocks if the MC ucode is loaded */
593 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
594 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400595 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
596 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deuchered18a362011-01-06 21:19:32 -0500597 if (rdev->pm.default_sclk)
598 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
599 if (rdev->pm.default_mclk)
600 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
601 }
Alex Deucher56278a82009-12-28 13:58:44 -0500602 }
603
Alex Deucher21a81222010-07-02 12:58:16 -0400604 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200605 ret = radeon_hwmon_init(rdev);
606 if (ret)
607 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100608
609 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
610
Alex Deucherce8f5372010-05-07 15:10:16 -0400611 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400612 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000613 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
614 if (ret)
615 DRM_ERROR("failed to create device file for power profile\n");
616 ret = device_create_file(rdev->dev, &dev_attr_power_method);
617 if (ret)
618 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400619
620#ifdef CONFIG_ACPI
621 rdev->acpi_nb.notifier_call = radeon_acpi_event;
622 register_acpi_notifier(&rdev->acpi_nb);
623#endif
Alex Deucherce8f5372010-05-07 15:10:16 -0400624 if (radeon_debugfs_pm_init(rdev)) {
625 DRM_ERROR("Failed to register debugfs file for PM!\n");
626 }
627
628 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100629 }
630
631 return 0;
632}
633
Alex Deucher29fb52c2010-03-11 10:01:17 -0500634void radeon_pm_fini(struct radeon_device *rdev)
635{
Alex Deucherce8f5372010-05-07 15:10:16 -0400636 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400637 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400638 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
639 rdev->pm.profile = PM_PROFILE_DEFAULT;
640 radeon_pm_update_profile(rdev);
641 radeon_pm_set_clocks(rdev);
642 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400643 /* reset default clocks */
644 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
645 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
646 radeon_pm_set_clocks(rdev);
647 }
Alex Deuchera4248162010-04-24 14:50:23 -0400648 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100649
650 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400651
Alex Deucherce8f5372010-05-07 15:10:16 -0400652 device_remove_file(rdev->dev, &dev_attr_power_profile);
653 device_remove_file(rdev->dev, &dev_attr_power_method);
654#ifdef CONFIG_ACPI
655 unregister_acpi_notifier(&rdev->acpi_nb);
656#endif
657 }
Alex Deuchera4248162010-04-24 14:50:23 -0400658
Alex Deucher0975b162011-02-02 18:42:03 -0500659 if (rdev->pm.power_state)
660 kfree(rdev->pm.power_state);
661
Alex Deucher21a81222010-07-02 12:58:16 -0400662 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500663}
664
Rafał Miłeckic913e232009-12-22 23:02:16 +0100665void radeon_pm_compute_clocks(struct radeon_device *rdev)
666{
667 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400668 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100669 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100670
Alex Deucherce8f5372010-05-07 15:10:16 -0400671 if (rdev->pm.num_power_states < 2)
672 return;
673
Rafał Miłeckic913e232009-12-22 23:02:16 +0100674 mutex_lock(&rdev->pm.mutex);
675
676 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400677 rdev->pm.active_crtc_count = 0;
678 list_for_each_entry(crtc,
679 &ddev->mode_config.crtc_list, head) {
680 radeon_crtc = to_radeon_crtc(crtc);
681 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100682 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400683 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100684 }
685 }
686
Alex Deucherce8f5372010-05-07 15:10:16 -0400687 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
688 radeon_pm_update_profile(rdev);
689 radeon_pm_set_clocks(rdev);
690 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
691 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
692 if (rdev->pm.active_crtc_count > 1) {
693 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
694 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400695
Alex Deucherce8f5372010-05-07 15:10:16 -0400696 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
697 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
698 radeon_pm_get_dynpm_state(rdev);
699 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100700
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000701 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400702 }
703 } else if (rdev->pm.active_crtc_count == 1) {
704 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100705
Alex Deucherce8f5372010-05-07 15:10:16 -0400706 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
707 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
708 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
709 radeon_pm_get_dynpm_state(rdev);
710 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100711
Tejun Heo32c87fc2011-01-03 14:49:32 +0100712 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
713 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400714 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
715 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100716 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
717 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000718 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400719 }
720 } else { /* count == 0 */
721 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
722 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100723
Alex Deucherce8f5372010-05-07 15:10:16 -0400724 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
725 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
726 radeon_pm_get_dynpm_state(rdev);
727 radeon_pm_set_clocks(rdev);
728 }
729 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100730 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100731 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100732
733 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100734}
735
Alex Deucherce8f5372010-05-07 15:10:16 -0400736static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000737{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400738 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000739 bool in_vbl = true;
740
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400741 /* Iterate over all active crtc's. All crtc's must be in vblank,
742 * otherwise return in_vbl == false.
743 */
744 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
745 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200746 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
747 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
748 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000749 in_vbl = false;
750 }
751 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400752
753 return in_vbl;
754}
755
Alex Deucherce8f5372010-05-07 15:10:16 -0400756static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400757{
758 u32 stat_crtc = 0;
759 bool in_vbl = radeon_pm_in_vbl(rdev);
760
Dave Airlief7352612010-02-18 15:58:36 +1000761 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000762 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400763 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000764 return in_vbl;
765}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100766
Alex Deucherce8f5372010-05-07 15:10:16 -0400767static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100768{
769 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400770 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100771 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400772 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100773
Matthew Garrettd9932a32010-04-26 16:02:26 -0400774 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100775 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400776 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100777 unsigned long irq_flags;
778 int not_processed = 0;
779
780 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
781 if (!list_empty(&rdev->fence_drv.emited)) {
782 struct list_head *ptr;
783 list_for_each(ptr, &rdev->fence_drv.emited) {
784 /* count up to 3, that's enought info */
785 if (++not_processed >= 3)
786 break;
787 }
788 }
789 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
790
791 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400792 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
793 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
794 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
795 rdev->pm.dynpm_can_upclock) {
796 rdev->pm.dynpm_planned_action =
797 DYNPM_ACTION_UPCLOCK;
798 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100799 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
800 }
801 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400802 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
803 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
804 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
805 rdev->pm.dynpm_can_downclock) {
806 rdev->pm.dynpm_planned_action =
807 DYNPM_ACTION_DOWNCLOCK;
808 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100809 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
810 }
811 }
812
Alex Deucherd7311172010-05-03 01:13:14 -0400813 /* Note, radeon_pm_set_clocks is called with static_switch set
814 * to false since we want to wait for vbl to avoid flicker.
815 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400816 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
817 jiffies > rdev->pm.dynpm_action_timeout) {
818 radeon_pm_get_dynpm_state(rdev);
819 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100820 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000821
Tejun Heo32c87fc2011-01-03 14:49:32 +0100822 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
823 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100824 }
825 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400826 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100827}
828
Rafał Miłecki74338742009-11-03 00:53:02 +0100829/*
830 * Debugfs info
831 */
832#if defined(CONFIG_DEBUG_FS)
833
834static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
835{
836 struct drm_info_node *node = (struct drm_info_node *) m->private;
837 struct drm_device *dev = node->minor->dev;
838 struct radeon_device *rdev = dev->dev_private;
839
Alex Deucher9ace9f72011-01-06 21:19:26 -0500840 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100841 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500842 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100843 if (rdev->asic->get_memory_clock)
844 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400845 if (rdev->pm.current_vddc)
846 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000847 if (rdev->asic->get_pcie_lanes)
848 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100849
850 return 0;
851}
852
853static struct drm_info_list radeon_pm_info_list[] = {
854 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
855};
856#endif
857
Rafał Miłeckic913e232009-12-22 23:02:16 +0100858static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100859{
860#if defined(CONFIG_DEBUG_FS)
861 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
862#else
863 return 0;
864#endif
865}