blob: ac4efb0f1cb4649433e45b79e25dff73e1665ad6 [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 Deucherce8f5372010-05-07 15:10:16 -040026#ifdef CONFIG_ACPI
27#include <linux/acpi.h>
28#endif
29#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040030#include <linux/hwmon.h>
31#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010032
Rafał Miłeckic913e232009-12-22 23:02:16 +010033#define RADEON_IDLE_LOOP_MS 100
34#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010035#define RADEON_WAIT_VBLANK_TIMEOUT 200
Alex Deucher2031f772010-04-22 12:52:11 -040036#define RADEON_WAIT_IDLE_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010037
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040038static const char *radeon_pm_state_type_name[5] = {
39 "Default",
40 "Powersave",
41 "Battery",
42 "Balanced",
43 "Performance",
44};
45
Alex Deucherce8f5372010-05-07 15:10:16 -040046static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010047static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040048static bool radeon_pm_in_vbl(struct radeon_device *rdev);
49static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
50static void radeon_pm_update_profile(struct radeon_device *rdev);
51static void radeon_pm_set_clocks(struct radeon_device *rdev);
52
53#define ACPI_AC_CLASS "ac_adapter"
54
55#ifdef CONFIG_ACPI
56static int radeon_acpi_event(struct notifier_block *nb,
57 unsigned long val,
58 void *data)
59{
60 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
61 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
62
63 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
64 if (power_supply_is_system_supplied() > 0)
Dave Airlied9fdaaf2010-08-02 10:42:55 +100065 DRM_DEBUG_DRIVER("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040066 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +100067 DRM_DEBUG_DRIVER("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040068
69 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
70 if (rdev->pm.profile == PM_PROFILE_AUTO) {
71 mutex_lock(&rdev->pm.mutex);
72 radeon_pm_update_profile(rdev);
73 radeon_pm_set_clocks(rdev);
74 mutex_unlock(&rdev->pm.mutex);
75 }
76 }
77 }
78
79 return NOTIFY_OK;
80}
81#endif
82
83static void radeon_pm_update_profile(struct radeon_device *rdev)
84{
85 switch (rdev->pm.profile) {
86 case PM_PROFILE_DEFAULT:
87 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
88 break;
89 case PM_PROFILE_AUTO:
90 if (power_supply_is_system_supplied() > 0) {
91 if (rdev->pm.active_crtc_count > 1)
92 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
93 else
94 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
95 } else {
96 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -040097 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040098 else
Alex Deucherc9e75b22010-06-02 17:56:01 -040099 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400100 }
101 break;
102 case PM_PROFILE_LOW:
103 if (rdev->pm.active_crtc_count > 1)
104 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
105 else
106 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
107 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400108 case PM_PROFILE_MID:
109 if (rdev->pm.active_crtc_count > 1)
110 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
111 else
112 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
113 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400114 case PM_PROFILE_HIGH:
115 if (rdev->pm.active_crtc_count > 1)
116 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
117 else
118 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
119 break;
120 }
121
122 if (rdev->pm.active_crtc_count == 0) {
123 rdev->pm.requested_power_state_index =
124 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
125 rdev->pm.requested_clock_mode_index =
126 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
127 } else {
128 rdev->pm.requested_power_state_index =
129 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
130 rdev->pm.requested_clock_mode_index =
131 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
132 }
133}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100134
Matthew Garrett5876dd22010-04-26 15:52:20 -0400135static void radeon_unmap_vram_bos(struct radeon_device *rdev)
136{
137 struct radeon_bo *bo, *n;
138
139 if (list_empty(&rdev->gem.objects))
140 return;
141
142 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
143 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
144 ttm_bo_unmap_virtual(&bo->tbo);
145 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400146}
147
Alex Deucherce8f5372010-05-07 15:10:16 -0400148static void radeon_sync_with_vblank(struct radeon_device *rdev)
149{
150 if (rdev->pm.active_crtcs) {
151 rdev->pm.vblank_sync = false;
152 wait_event_timeout(
153 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
154 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
155 }
156}
157
158static void radeon_set_power_state(struct radeon_device *rdev)
159{
160 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400161 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400162
163 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
164 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
165 return;
166
167 if (radeon_gui_idle(rdev)) {
168 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
169 clock_info[rdev->pm.requested_clock_mode_index].sclk;
170 if (sclk > rdev->clock.default_sclk)
171 sclk = rdev->clock.default_sclk;
172
173 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
174 clock_info[rdev->pm.requested_clock_mode_index].mclk;
175 if (mclk > rdev->clock.default_mclk)
176 mclk = rdev->clock.default_mclk;
177
Alex Deucher92645872010-05-27 17:01:41 -0400178 /* upvolt before raising clocks, downvolt after lowering clocks */
179 if (sclk < rdev->pm.current_sclk)
180 misc_after = true;
181
182 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400183
184 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400185 if (!radeon_pm_in_vbl(rdev))
186 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400187 }
188
Alex Deucher92645872010-05-27 17:01:41 -0400189 radeon_pm_prepare(rdev);
190
191 if (!misc_after)
192 /* voltage, pcie lanes, etc.*/
193 radeon_pm_misc(rdev);
194
195 /* set engine clock */
196 if (sclk != rdev->pm.current_sclk) {
197 radeon_pm_debug_check_in_vbl(rdev, false);
198 radeon_set_engine_clock(rdev, sclk);
199 radeon_pm_debug_check_in_vbl(rdev, true);
200 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000201 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400202 }
203
204 /* set memory clock */
205 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
206 radeon_pm_debug_check_in_vbl(rdev, false);
207 radeon_set_memory_clock(rdev, mclk);
208 radeon_pm_debug_check_in_vbl(rdev, true);
209 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000210 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400211 }
212
213 if (misc_after)
214 /* voltage, pcie lanes, etc.*/
215 radeon_pm_misc(rdev);
216
217 radeon_pm_finish(rdev);
218
Alex Deucherce8f5372010-05-07 15:10:16 -0400219 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
220 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
221 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000222 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400223}
224
225static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400226{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400227 int i;
228
Alex Deucher4e186b22010-08-13 10:53:35 -0400229 /* no need to take locks, etc. if nothing's going to change */
230 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
231 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
232 return;
233
Matthew Garrett612e06c2010-04-27 17:16:58 -0400234 mutex_lock(&rdev->ddev->struct_mutex);
235 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400236 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400237
238 /* gui idle int has issues on older chips it seems */
239 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400240 if (rdev->irq.installed) {
241 /* wait for GPU idle */
242 rdev->pm.gui_idle = false;
243 rdev->irq.gui_idle = true;
244 radeon_irq_set(rdev);
245 wait_event_interruptible_timeout(
246 rdev->irq.idle_queue, rdev->pm.gui_idle,
247 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
248 rdev->irq.gui_idle = false;
249 radeon_irq_set(rdev);
250 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400251 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400252 if (rdev->cp.ready) {
253 struct radeon_fence *fence;
254 radeon_ring_alloc(rdev, 64);
255 radeon_fence_create(rdev, &fence);
256 radeon_fence_emit(rdev, fence);
257 radeon_ring_commit(rdev);
258 radeon_fence_wait(fence, false);
259 radeon_fence_unref(&fence);
260 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400261 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400262 radeon_unmap_vram_bos(rdev);
263
Alex Deucherce8f5372010-05-07 15:10:16 -0400264 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400265 for (i = 0; i < rdev->num_crtc; i++) {
266 if (rdev->pm.active_crtcs & (1 << i)) {
267 rdev->pm.req_vblank |= (1 << i);
268 drm_vblank_get(rdev->ddev, i);
269 }
270 }
271 }
Alex Deucher539d2412010-04-29 00:22:43 -0400272
Alex Deucherce8f5372010-05-07 15:10:16 -0400273 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400274
Alex Deucherce8f5372010-05-07 15:10:16 -0400275 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400276 for (i = 0; i < rdev->num_crtc; i++) {
277 if (rdev->pm.req_vblank & (1 << i)) {
278 rdev->pm.req_vblank &= ~(1 << i);
279 drm_vblank_put(rdev->ddev, i);
280 }
281 }
282 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400283
Alex Deuchera4248162010-04-24 14:50:23 -0400284 /* update display watermarks based on new power state */
285 radeon_update_bandwidth_info(rdev);
286 if (rdev->pm.active_crtc_count)
287 radeon_bandwidth_update(rdev);
288
Alex Deucherce8f5372010-05-07 15:10:16 -0400289 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400290
Alex Deuchera4248162010-04-24 14:50:23 -0400291 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400292 mutex_unlock(&rdev->vram_mutex);
293 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400294}
295
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400296static void radeon_pm_print_states(struct radeon_device *rdev)
297{
298 int i, j;
299 struct radeon_power_state *power_state;
300 struct radeon_pm_clock_info *clock_info;
301
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000302 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400303 for (i = 0; i < rdev->pm.num_power_states; i++) {
304 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000305 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400306 radeon_pm_state_type_name[power_state->type]);
307 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000308 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400309 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000310 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400311 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000312 DRM_DEBUG_DRIVER("\tSingle display only\n");
313 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400314 for (j = 0; j < power_state->num_clock_modes; j++) {
315 clock_info = &(power_state->clock_info[j]);
316 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000317 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400318 j,
319 clock_info->sclk * 10,
320 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
321 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000322 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400323 j,
324 clock_info->sclk * 10,
325 clock_info->mclk * 10,
326 clock_info->voltage.voltage,
327 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
328 }
329 }
330}
331
Alex Deucherce8f5372010-05-07 15:10:16 -0400332static ssize_t radeon_get_pm_profile(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400335{
336 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
337 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400338 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400339
Alex Deucherce8f5372010-05-07 15:10:16 -0400340 return snprintf(buf, PAGE_SIZE, "%s\n",
341 (cp == PM_PROFILE_AUTO) ? "auto" :
342 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100343 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400344 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400345}
346
Alex Deucherce8f5372010-05-07 15:10:16 -0400347static ssize_t radeon_set_pm_profile(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf,
350 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400351{
352 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
353 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400354
355 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400356 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
357 if (strncmp("default", buf, strlen("default")) == 0)
358 rdev->pm.profile = PM_PROFILE_DEFAULT;
359 else if (strncmp("auto", buf, strlen("auto")) == 0)
360 rdev->pm.profile = PM_PROFILE_AUTO;
361 else if (strncmp("low", buf, strlen("low")) == 0)
362 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400363 else if (strncmp("mid", buf, strlen("mid")) == 0)
364 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400365 else if (strncmp("high", buf, strlen("high")) == 0)
366 rdev->pm.profile = PM_PROFILE_HIGH;
367 else {
368 DRM_ERROR("invalid power profile!\n");
369 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400370 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400371 radeon_pm_update_profile(rdev);
372 radeon_pm_set_clocks(rdev);
373 }
374fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400375 mutex_unlock(&rdev->pm.mutex);
376
377 return count;
378}
379
Alex Deucherce8f5372010-05-07 15:10:16 -0400380static ssize_t radeon_get_pm_method(struct device *dev,
381 struct device_attribute *attr,
382 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400383{
384 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
385 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400386 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400387
388 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400389 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400390}
391
Alex Deucherce8f5372010-05-07 15:10:16 -0400392static ssize_t radeon_set_pm_method(struct device *dev,
393 struct device_attribute *attr,
394 const char *buf,
395 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400396{
397 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
398 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400399
Alex Deucherce8f5372010-05-07 15:10:16 -0400400
401 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400402 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400403 rdev->pm.pm_method = PM_METHOD_DYNPM;
404 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
405 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400406 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400407 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000408 bool flush_wq = false;
409
Alex Deucherce8f5372010-05-07 15:10:16 -0400410 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000411 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
412 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
413 flush_wq = true;
414 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400415 /* disable dynpm */
416 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
417 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000418 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400419 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000420 if (flush_wq)
421 flush_workqueue(rdev->wq);
Alex Deucherce8f5372010-05-07 15:10:16 -0400422 } else {
423 DRM_ERROR("invalid power method!\n");
424 goto fail;
425 }
426 radeon_pm_compute_clocks(rdev);
427fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400428 return count;
429}
430
Alex Deucherce8f5372010-05-07 15:10:16 -0400431static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
432static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400433
Alex Deucher21a81222010-07-02 12:58:16 -0400434static ssize_t radeon_hwmon_show_temp(struct device *dev,
435 struct device_attribute *attr,
436 char *buf)
437{
438 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
439 struct radeon_device *rdev = ddev->dev_private;
440 u32 temp;
441
442 switch (rdev->pm.int_thermal_type) {
443 case THERMAL_TYPE_RV6XX:
444 temp = rv6xx_get_temp(rdev);
445 break;
446 case THERMAL_TYPE_RV770:
447 temp = rv770_get_temp(rdev);
448 break;
449 case THERMAL_TYPE_EVERGREEN:
450 temp = evergreen_get_temp(rdev);
451 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500452 case THERMAL_TYPE_SUMO:
453 temp = sumo_get_temp(rdev);
454 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400455 default:
456 temp = 0;
457 break;
458 }
459
460 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
461}
462
463static ssize_t radeon_hwmon_show_name(struct device *dev,
464 struct device_attribute *attr,
465 char *buf)
466{
467 return sprintf(buf, "radeon\n");
468}
469
470static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
471static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
472
473static struct attribute *hwmon_attributes[] = {
474 &sensor_dev_attr_temp1_input.dev_attr.attr,
475 &sensor_dev_attr_name.dev_attr.attr,
476 NULL
477};
478
479static const struct attribute_group hwmon_attrgroup = {
480 .attrs = hwmon_attributes,
481};
482
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200483static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400484{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200485 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400486
487 rdev->pm.int_hwmon_dev = NULL;
488
489 switch (rdev->pm.int_thermal_type) {
490 case THERMAL_TYPE_RV6XX:
491 case THERMAL_TYPE_RV770:
492 case THERMAL_TYPE_EVERGREEN:
Alex Deuchere33df252010-11-22 17:56:32 -0500493 case THERMAL_TYPE_SUMO:
Alex Deucher21a81222010-07-02 12:58:16 -0400494 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200495 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
496 err = PTR_ERR(rdev->pm.int_hwmon_dev);
497 dev_err(rdev->dev,
498 "Unable to register hwmon device: %d\n", err);
499 break;
500 }
Alex Deucher21a81222010-07-02 12:58:16 -0400501 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
502 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
503 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200504 if (err) {
505 dev_err(rdev->dev,
506 "Unable to create hwmon sysfs file: %d\n", err);
507 hwmon_device_unregister(rdev->dev);
508 }
Alex Deucher21a81222010-07-02 12:58:16 -0400509 break;
510 default:
511 break;
512 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200513
514 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400515}
516
517static void radeon_hwmon_fini(struct radeon_device *rdev)
518{
519 if (rdev->pm.int_hwmon_dev) {
520 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
521 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
522 }
523}
524
Alex Deucherce8f5372010-05-07 15:10:16 -0400525void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500526{
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000527 bool flush_wq = false;
528
Alex Deucherce8f5372010-05-07 15:10:16 -0400529 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000530 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
531 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
532 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
533 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
534 flush_wq = true;
535 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400536 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000537 if (flush_wq)
538 flush_workqueue(rdev->wq);
Alex Deucher56278a82009-12-28 13:58:44 -0500539}
540
Alex Deucherce8f5372010-05-07 15:10:16 -0400541void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100542{
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400543 /* asic init will reset the default power state */
544 mutex_lock(&rdev->pm.mutex);
545 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
546 rdev->pm.current_clock_mode_index = 0;
547 rdev->pm.current_sclk = rdev->clock.default_sclk;
548 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400549 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000550 if (rdev->pm.pm_method == PM_METHOD_DYNPM
551 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
552 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
553 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
554 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
555 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400556 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400557 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100558}
559
Rafał Miłecki74338742009-11-03 00:53:02 +0100560int radeon_pm_init(struct radeon_device *rdev)
561{
Dave Airlie26481fb2010-05-18 19:00:14 +1000562 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200563
Alex Deucherce8f5372010-05-07 15:10:16 -0400564 /* default to profile method */
565 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400566 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400567 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
568 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
569 rdev->pm.dynpm_can_upclock = true;
570 rdev->pm.dynpm_can_downclock = true;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400571 rdev->pm.current_sclk = rdev->clock.default_sclk;
572 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400573 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100574
Alex Deucher56278a82009-12-28 13:58:44 -0500575 if (rdev->bios) {
576 if (rdev->is_atom_bios)
577 radeon_atombios_get_power_modes(rdev);
578 else
579 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400580 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400581 radeon_pm_init_profile(rdev);
Alex Deucher56278a82009-12-28 13:58:44 -0500582 }
583
Alex Deucher21a81222010-07-02 12:58:16 -0400584 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200585 ret = radeon_hwmon_init(rdev);
586 if (ret)
587 return ret;
Alex Deucherce8f5372010-05-07 15:10:16 -0400588 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400589 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000590 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
591 if (ret)
592 DRM_ERROR("failed to create device file for power profile\n");
593 ret = device_create_file(rdev->dev, &dev_attr_power_method);
594 if (ret)
595 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400596
597#ifdef CONFIG_ACPI
598 rdev->acpi_nb.notifier_call = radeon_acpi_event;
599 register_acpi_notifier(&rdev->acpi_nb);
600#endif
601 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
602
603 if (radeon_debugfs_pm_init(rdev)) {
604 DRM_ERROR("Failed to register debugfs file for PM!\n");
605 }
606
607 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100608 }
609
610 return 0;
611}
612
Alex Deucher29fb52c2010-03-11 10:01:17 -0500613void radeon_pm_fini(struct radeon_device *rdev)
614{
Alex Deucherce8f5372010-05-07 15:10:16 -0400615 if (rdev->pm.num_power_states > 1) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000616 bool flush_wq = false;
617
Alex Deuchera4248162010-04-24 14:50:23 -0400618 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400619 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
620 rdev->pm.profile = PM_PROFILE_DEFAULT;
621 radeon_pm_update_profile(rdev);
622 radeon_pm_set_clocks(rdev);
623 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
624 /* cancel work */
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000625 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
626 flush_wq = true;
Alex Deucherce8f5372010-05-07 15:10:16 -0400627 /* reset default clocks */
628 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
629 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
630 radeon_pm_set_clocks(rdev);
631 }
Alex Deuchera4248162010-04-24 14:50:23 -0400632 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000633 if (flush_wq)
634 flush_workqueue(rdev->wq);
Alex Deucher58e21df2010-03-22 13:31:08 -0400635
Alex Deucherce8f5372010-05-07 15:10:16 -0400636 device_remove_file(rdev->dev, &dev_attr_power_profile);
637 device_remove_file(rdev->dev, &dev_attr_power_method);
638#ifdef CONFIG_ACPI
639 unregister_acpi_notifier(&rdev->acpi_nb);
640#endif
641 }
Alex Deuchera4248162010-04-24 14:50:23 -0400642
Alex Deucher21a81222010-07-02 12:58:16 -0400643 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500644}
645
Rafał Miłeckic913e232009-12-22 23:02:16 +0100646void radeon_pm_compute_clocks(struct radeon_device *rdev)
647{
648 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400649 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100650 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100651
Alex Deucherce8f5372010-05-07 15:10:16 -0400652 if (rdev->pm.num_power_states < 2)
653 return;
654
Rafał Miłeckic913e232009-12-22 23:02:16 +0100655 mutex_lock(&rdev->pm.mutex);
656
657 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400658 rdev->pm.active_crtc_count = 0;
659 list_for_each_entry(crtc,
660 &ddev->mode_config.crtc_list, head) {
661 radeon_crtc = to_radeon_crtc(crtc);
662 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100663 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400664 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100665 }
666 }
667
Alex Deucherce8f5372010-05-07 15:10:16 -0400668 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
669 radeon_pm_update_profile(rdev);
670 radeon_pm_set_clocks(rdev);
671 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
672 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
673 if (rdev->pm.active_crtc_count > 1) {
674 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
675 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400676
Alex Deucherce8f5372010-05-07 15:10:16 -0400677 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
678 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
679 radeon_pm_get_dynpm_state(rdev);
680 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100681
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000682 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400683 }
684 } else if (rdev->pm.active_crtc_count == 1) {
685 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100686
Alex Deucherce8f5372010-05-07 15:10:16 -0400687 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
688 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
689 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
690 radeon_pm_get_dynpm_state(rdev);
691 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100692
Alex Deucherce8f5372010-05-07 15:10:16 -0400693 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
694 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
695 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
696 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
697 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
698 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000699 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400700 }
701 } else { /* count == 0 */
702 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
703 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100704
Alex Deucherce8f5372010-05-07 15:10:16 -0400705 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
706 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
707 radeon_pm_get_dynpm_state(rdev);
708 radeon_pm_set_clocks(rdev);
709 }
710 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100711 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100712 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100713
714 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100715}
716
Alex Deucherce8f5372010-05-07 15:10:16 -0400717static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000718{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400719 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000720 bool in_vbl = true;
721
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400722 /* Iterate over all active crtc's. All crtc's must be in vblank,
723 * otherwise return in_vbl == false.
724 */
725 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
726 if (rdev->pm.active_crtcs & (1 << crtc)) {
727 vbl_status = radeon_get_crtc_scanoutpos(rdev, crtc, &vpos, &hpos);
728 if ((vbl_status & RADEON_SCANOUTPOS_VALID) &&
729 !(vbl_status & RADEON_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000730 in_vbl = false;
731 }
732 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400733
734 return in_vbl;
735}
736
Alex Deucherce8f5372010-05-07 15:10:16 -0400737static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400738{
739 u32 stat_crtc = 0;
740 bool in_vbl = radeon_pm_in_vbl(rdev);
741
Dave Airlief7352612010-02-18 15:58:36 +1000742 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000743 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b562010-04-22 13:38:05 -0400744 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000745 return in_vbl;
746}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100747
Alex Deucherce8f5372010-05-07 15:10:16 -0400748static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100749{
750 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400751 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100752 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400753 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100754
Matthew Garrettd9932a32010-04-26 16:02:26 -0400755 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100756 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400757 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100758 unsigned long irq_flags;
759 int not_processed = 0;
760
761 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
762 if (!list_empty(&rdev->fence_drv.emited)) {
763 struct list_head *ptr;
764 list_for_each(ptr, &rdev->fence_drv.emited) {
765 /* count up to 3, that's enought info */
766 if (++not_processed >= 3)
767 break;
768 }
769 }
770 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
771
772 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400773 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
774 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
775 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
776 rdev->pm.dynpm_can_upclock) {
777 rdev->pm.dynpm_planned_action =
778 DYNPM_ACTION_UPCLOCK;
779 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100780 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
781 }
782 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400783 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
784 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
785 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
786 rdev->pm.dynpm_can_downclock) {
787 rdev->pm.dynpm_planned_action =
788 DYNPM_ACTION_DOWNCLOCK;
789 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100790 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
791 }
792 }
793
Alex Deucherd7311172010-05-03 01:13:14 -0400794 /* Note, radeon_pm_set_clocks is called with static_switch set
795 * to false since we want to wait for vbl to avoid flicker.
796 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400797 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
798 jiffies > rdev->pm.dynpm_action_timeout) {
799 radeon_pm_get_dynpm_state(rdev);
800 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100801 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000802
803 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
804 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100805 }
806 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400807 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100808}
809
Rafał Miłecki74338742009-11-03 00:53:02 +0100810/*
811 * Debugfs info
812 */
813#if defined(CONFIG_DEBUG_FS)
814
815static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
816{
817 struct drm_info_node *node = (struct drm_info_node *) m->private;
818 struct drm_device *dev = node->minor->dev;
819 struct radeon_device *rdev = dev->dev_private;
820
Rafał Miłecki62340772009-12-15 21:46:58 +0100821 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
822 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
823 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
824 if (rdev->asic->get_memory_clock)
825 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400826 if (rdev->pm.current_vddc)
827 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000828 if (rdev->asic->get_pcie_lanes)
829 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100830
831 return 0;
832}
833
834static struct drm_info_list radeon_pm_info_list[] = {
835 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
836};
837#endif
838
Rafał Miłeckic913e232009-12-22 23:02:16 +0100839static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100840{
841#if defined(CONFIG_DEBUG_FS)
842 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
843#else
844 return 0;
845#endif
846}