blob: 58038f5cab38d4b0c31ac7c2abfbad9a18c533b2 [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
Matthew Garrett612e06c2010-04-27 17:16:58 -0400229 mutex_lock(&rdev->ddev->struct_mutex);
230 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400231 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400232
233 /* gui idle int has issues on older chips it seems */
234 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400235 if (rdev->irq.installed) {
236 /* wait for GPU idle */
237 rdev->pm.gui_idle = false;
238 rdev->irq.gui_idle = true;
239 radeon_irq_set(rdev);
240 wait_event_interruptible_timeout(
241 rdev->irq.idle_queue, rdev->pm.gui_idle,
242 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
243 rdev->irq.gui_idle = false;
244 radeon_irq_set(rdev);
245 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400246 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400247 if (rdev->cp.ready) {
248 struct radeon_fence *fence;
249 radeon_ring_alloc(rdev, 64);
250 radeon_fence_create(rdev, &fence);
251 radeon_fence_emit(rdev, fence);
252 radeon_ring_commit(rdev);
253 radeon_fence_wait(fence, false);
254 radeon_fence_unref(&fence);
255 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400256 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400257 radeon_unmap_vram_bos(rdev);
258
Alex Deucherce8f5372010-05-07 15:10:16 -0400259 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400260 for (i = 0; i < rdev->num_crtc; i++) {
261 if (rdev->pm.active_crtcs & (1 << i)) {
262 rdev->pm.req_vblank |= (1 << i);
263 drm_vblank_get(rdev->ddev, i);
264 }
265 }
266 }
Alex Deucher539d2412010-04-29 00:22:43 -0400267
Alex Deucherce8f5372010-05-07 15:10:16 -0400268 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400269
Alex Deucherce8f5372010-05-07 15:10:16 -0400270 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400271 for (i = 0; i < rdev->num_crtc; i++) {
272 if (rdev->pm.req_vblank & (1 << i)) {
273 rdev->pm.req_vblank &= ~(1 << i);
274 drm_vblank_put(rdev->ddev, i);
275 }
276 }
277 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400278
Alex Deuchera4248162010-04-24 14:50:23 -0400279 /* update display watermarks based on new power state */
280 radeon_update_bandwidth_info(rdev);
281 if (rdev->pm.active_crtc_count)
282 radeon_bandwidth_update(rdev);
283
Alex Deucherce8f5372010-05-07 15:10:16 -0400284 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400285
Alex Deuchera4248162010-04-24 14:50:23 -0400286 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400287 mutex_unlock(&rdev->vram_mutex);
288 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400289}
290
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400291static void radeon_pm_print_states(struct radeon_device *rdev)
292{
293 int i, j;
294 struct radeon_power_state *power_state;
295 struct radeon_pm_clock_info *clock_info;
296
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000297 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400298 for (i = 0; i < rdev->pm.num_power_states; i++) {
299 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000300 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400301 radeon_pm_state_type_name[power_state->type]);
302 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000303 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400304 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000305 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400306 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000307 DRM_DEBUG_DRIVER("\tSingle display only\n");
308 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400309 for (j = 0; j < power_state->num_clock_modes; j++) {
310 clock_info = &(power_state->clock_info[j]);
311 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000312 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400313 j,
314 clock_info->sclk * 10,
315 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
316 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000317 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400318 j,
319 clock_info->sclk * 10,
320 clock_info->mclk * 10,
321 clock_info->voltage.voltage,
322 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
323 }
324 }
325}
326
Alex Deucherce8f5372010-05-07 15:10:16 -0400327static ssize_t radeon_get_pm_profile(struct device *dev,
328 struct device_attribute *attr,
329 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400330{
331 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
332 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400333 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400334
Alex Deucherce8f5372010-05-07 15:10:16 -0400335 return snprintf(buf, PAGE_SIZE, "%s\n",
336 (cp == PM_PROFILE_AUTO) ? "auto" :
337 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100338 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400339 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400340}
341
Alex Deucherce8f5372010-05-07 15:10:16 -0400342static ssize_t radeon_set_pm_profile(struct device *dev,
343 struct device_attribute *attr,
344 const char *buf,
345 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400346{
347 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
348 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400349
350 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400351 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
352 if (strncmp("default", buf, strlen("default")) == 0)
353 rdev->pm.profile = PM_PROFILE_DEFAULT;
354 else if (strncmp("auto", buf, strlen("auto")) == 0)
355 rdev->pm.profile = PM_PROFILE_AUTO;
356 else if (strncmp("low", buf, strlen("low")) == 0)
357 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400358 else if (strncmp("mid", buf, strlen("mid")) == 0)
359 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400360 else if (strncmp("high", buf, strlen("high")) == 0)
361 rdev->pm.profile = PM_PROFILE_HIGH;
362 else {
363 DRM_ERROR("invalid power profile!\n");
364 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400365 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400366 radeon_pm_update_profile(rdev);
367 radeon_pm_set_clocks(rdev);
368 }
369fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400370 mutex_unlock(&rdev->pm.mutex);
371
372 return count;
373}
374
Alex Deucherce8f5372010-05-07 15:10:16 -0400375static ssize_t radeon_get_pm_method(struct device *dev,
376 struct device_attribute *attr,
377 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400378{
379 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
380 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400381 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400382
383 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400384 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400385}
386
Alex Deucherce8f5372010-05-07 15:10:16 -0400387static ssize_t radeon_set_pm_method(struct device *dev,
388 struct device_attribute *attr,
389 const char *buf,
390 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400391{
392 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
393 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400394
Alex Deucherce8f5372010-05-07 15:10:16 -0400395
396 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400397 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400398 rdev->pm.pm_method = PM_METHOD_DYNPM;
399 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
400 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400401 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400402 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000403 bool flush_wq = false;
404
Alex Deucherce8f5372010-05-07 15:10:16 -0400405 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000406 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
407 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
408 flush_wq = true;
409 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400410 /* disable dynpm */
411 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
412 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000413 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400414 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000415 if (flush_wq)
416 flush_workqueue(rdev->wq);
Alex Deucherce8f5372010-05-07 15:10:16 -0400417 } else {
418 DRM_ERROR("invalid power method!\n");
419 goto fail;
420 }
421 radeon_pm_compute_clocks(rdev);
422fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400423 return count;
424}
425
Alex Deucherce8f5372010-05-07 15:10:16 -0400426static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
427static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400428
Alex Deucher21a81222010-07-02 12:58:16 -0400429static ssize_t radeon_hwmon_show_temp(struct device *dev,
430 struct device_attribute *attr,
431 char *buf)
432{
433 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
434 struct radeon_device *rdev = ddev->dev_private;
435 u32 temp;
436
437 switch (rdev->pm.int_thermal_type) {
438 case THERMAL_TYPE_RV6XX:
439 temp = rv6xx_get_temp(rdev);
440 break;
441 case THERMAL_TYPE_RV770:
442 temp = rv770_get_temp(rdev);
443 break;
444 case THERMAL_TYPE_EVERGREEN:
445 temp = evergreen_get_temp(rdev);
446 break;
447 default:
448 temp = 0;
449 break;
450 }
451
452 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
453}
454
455static ssize_t radeon_hwmon_show_name(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
458{
459 return sprintf(buf, "radeon\n");
460}
461
462static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
463static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
464
465static struct attribute *hwmon_attributes[] = {
466 &sensor_dev_attr_temp1_input.dev_attr.attr,
467 &sensor_dev_attr_name.dev_attr.attr,
468 NULL
469};
470
471static const struct attribute_group hwmon_attrgroup = {
472 .attrs = hwmon_attributes,
473};
474
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200475static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400476{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200477 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400478
479 rdev->pm.int_hwmon_dev = NULL;
480
481 switch (rdev->pm.int_thermal_type) {
482 case THERMAL_TYPE_RV6XX:
483 case THERMAL_TYPE_RV770:
484 case THERMAL_TYPE_EVERGREEN:
485 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200486 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
487 err = PTR_ERR(rdev->pm.int_hwmon_dev);
488 dev_err(rdev->dev,
489 "Unable to register hwmon device: %d\n", err);
490 break;
491 }
Alex Deucher21a81222010-07-02 12:58:16 -0400492 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
493 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
494 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200495 if (err) {
496 dev_err(rdev->dev,
497 "Unable to create hwmon sysfs file: %d\n", err);
498 hwmon_device_unregister(rdev->dev);
499 }
Alex Deucher21a81222010-07-02 12:58:16 -0400500 break;
501 default:
502 break;
503 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200504
505 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400506}
507
508static void radeon_hwmon_fini(struct radeon_device *rdev)
509{
510 if (rdev->pm.int_hwmon_dev) {
511 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
512 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
513 }
514}
515
Alex Deucherce8f5372010-05-07 15:10:16 -0400516void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500517{
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000518 bool flush_wq = false;
519
Alex Deucherce8f5372010-05-07 15:10:16 -0400520 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000521 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
522 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
523 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
524 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
525 flush_wq = true;
526 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400527 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000528 if (flush_wq)
529 flush_workqueue(rdev->wq);
Alex Deucher56278a82009-12-28 13:58:44 -0500530}
531
Alex Deucherce8f5372010-05-07 15:10:16 -0400532void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100533{
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400534 /* asic init will reset the default power state */
535 mutex_lock(&rdev->pm.mutex);
536 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
537 rdev->pm.current_clock_mode_index = 0;
538 rdev->pm.current_sclk = rdev->clock.default_sclk;
539 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400540 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 +0000541 if (rdev->pm.pm_method == PM_METHOD_DYNPM
542 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
543 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
544 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
545 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
546 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400547 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400548 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100549}
550
Rafał Miłecki74338742009-11-03 00:53:02 +0100551int radeon_pm_init(struct radeon_device *rdev)
552{
Dave Airlie26481fb2010-05-18 19:00:14 +1000553 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200554
Alex Deucherce8f5372010-05-07 15:10:16 -0400555 /* default to profile method */
556 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400557 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400558 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
559 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
560 rdev->pm.dynpm_can_upclock = true;
561 rdev->pm.dynpm_can_downclock = true;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400562 rdev->pm.current_sclk = rdev->clock.default_sclk;
563 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400564 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100565
Alex Deucher56278a82009-12-28 13:58:44 -0500566 if (rdev->bios) {
567 if (rdev->is_atom_bios)
568 radeon_atombios_get_power_modes(rdev);
569 else
570 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400571 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400572 radeon_pm_init_profile(rdev);
Alex Deucher56278a82009-12-28 13:58:44 -0500573 }
574
Alex Deucher21a81222010-07-02 12:58:16 -0400575 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200576 ret = radeon_hwmon_init(rdev);
577 if (ret)
578 return ret;
Alex Deucherce8f5372010-05-07 15:10:16 -0400579 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400580 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000581 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
582 if (ret)
583 DRM_ERROR("failed to create device file for power profile\n");
584 ret = device_create_file(rdev->dev, &dev_attr_power_method);
585 if (ret)
586 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400587
588#ifdef CONFIG_ACPI
589 rdev->acpi_nb.notifier_call = radeon_acpi_event;
590 register_acpi_notifier(&rdev->acpi_nb);
591#endif
592 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
593
594 if (radeon_debugfs_pm_init(rdev)) {
595 DRM_ERROR("Failed to register debugfs file for PM!\n");
596 }
597
598 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100599 }
600
601 return 0;
602}
603
Alex Deucher29fb52c2010-03-11 10:01:17 -0500604void radeon_pm_fini(struct radeon_device *rdev)
605{
Alex Deucherce8f5372010-05-07 15:10:16 -0400606 if (rdev->pm.num_power_states > 1) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000607 bool flush_wq = false;
608
Alex Deuchera4248162010-04-24 14:50:23 -0400609 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400610 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
611 rdev->pm.profile = PM_PROFILE_DEFAULT;
612 radeon_pm_update_profile(rdev);
613 radeon_pm_set_clocks(rdev);
614 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
615 /* cancel work */
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000616 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
617 flush_wq = true;
Alex Deucherce8f5372010-05-07 15:10:16 -0400618 /* reset default clocks */
619 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
620 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
621 radeon_pm_set_clocks(rdev);
622 }
Alex Deuchera4248162010-04-24 14:50:23 -0400623 mutex_unlock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000624 if (flush_wq)
625 flush_workqueue(rdev->wq);
Alex Deucher58e21df2010-03-22 13:31:08 -0400626
Alex Deucherce8f5372010-05-07 15:10:16 -0400627 device_remove_file(rdev->dev, &dev_attr_power_profile);
628 device_remove_file(rdev->dev, &dev_attr_power_method);
629#ifdef CONFIG_ACPI
630 unregister_acpi_notifier(&rdev->acpi_nb);
631#endif
632 }
Alex Deuchera4248162010-04-24 14:50:23 -0400633
Alex Deucher21a81222010-07-02 12:58:16 -0400634 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500635 if (rdev->pm.i2c_bus)
636 radeon_i2c_destroy(rdev->pm.i2c_bus);
637}
638
Rafał Miłeckic913e232009-12-22 23:02:16 +0100639void radeon_pm_compute_clocks(struct radeon_device *rdev)
640{
641 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400642 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100643 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100644
Alex Deucherce8f5372010-05-07 15:10:16 -0400645 if (rdev->pm.num_power_states < 2)
646 return;
647
Rafał Miłeckic913e232009-12-22 23:02:16 +0100648 mutex_lock(&rdev->pm.mutex);
649
650 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400651 rdev->pm.active_crtc_count = 0;
652 list_for_each_entry(crtc,
653 &ddev->mode_config.crtc_list, head) {
654 radeon_crtc = to_radeon_crtc(crtc);
655 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100656 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400657 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100658 }
659 }
660
Alex Deucherce8f5372010-05-07 15:10:16 -0400661 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
662 radeon_pm_update_profile(rdev);
663 radeon_pm_set_clocks(rdev);
664 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
665 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
666 if (rdev->pm.active_crtc_count > 1) {
667 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
668 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400669
Alex Deucherce8f5372010-05-07 15:10:16 -0400670 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
671 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
672 radeon_pm_get_dynpm_state(rdev);
673 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100674
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000675 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400676 }
677 } else if (rdev->pm.active_crtc_count == 1) {
678 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100679
Alex Deucherce8f5372010-05-07 15:10:16 -0400680 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
681 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
682 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
683 radeon_pm_get_dynpm_state(rdev);
684 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100685
Alex Deucherce8f5372010-05-07 15:10:16 -0400686 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
687 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
688 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
689 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
690 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
691 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000692 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400693 }
694 } else { /* count == 0 */
695 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
696 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100697
Alex Deucherce8f5372010-05-07 15:10:16 -0400698 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
699 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
700 radeon_pm_get_dynpm_state(rdev);
701 radeon_pm_set_clocks(rdev);
702 }
703 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100704 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100705 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100706
707 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100708}
709
Alex Deucherce8f5372010-05-07 15:10:16 -0400710static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000711{
Alex Deucher539d2412010-04-29 00:22:43 -0400712 u32 stat_crtc = 0, vbl = 0, position = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000713 bool in_vbl = true;
714
Alex Deucherbae6b5622010-04-22 13:38:05 -0400715 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000716 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400717 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
718 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
719 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
720 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
Dave Airlief7352612010-02-18 15:58:36 +1000721 }
722 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400723 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
724 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
725 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
726 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400727 }
728 if (rdev->pm.active_crtcs & (1 << 2)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400729 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
730 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
731 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
732 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400733 }
734 if (rdev->pm.active_crtcs & (1 << 3)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400735 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
736 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
737 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
738 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400739 }
740 if (rdev->pm.active_crtcs & (1 << 4)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400741 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
742 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
743 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
744 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400745 }
746 if (rdev->pm.active_crtcs & (1 << 5)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400747 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
748 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
749 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
750 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400751 }
752 } else if (ASIC_IS_AVIVO(rdev)) {
753 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400754 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
755 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400756 }
757 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400758 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
759 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400760 }
Alex Deucher539d2412010-04-29 00:22:43 -0400761 if (position < vbl && position > 1)
762 in_vbl = false;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400763 } else {
764 if (rdev->pm.active_crtcs & (1 << 0)) {
765 stat_crtc = RREG32(RADEON_CRTC_STATUS);
766 if (!(stat_crtc & 1))
767 in_vbl = false;
768 }
769 if (rdev->pm.active_crtcs & (1 << 1)) {
770 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
771 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000772 in_vbl = false;
773 }
774 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400775
Alex Deucher539d2412010-04-29 00:22:43 -0400776 if (position < vbl && position > 1)
777 in_vbl = false;
778
Matthew Garrettf81f2022010-04-28 12:13:06 -0400779 return in_vbl;
780}
781
Alex Deucherce8f5372010-05-07 15:10:16 -0400782static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400783{
784 u32 stat_crtc = 0;
785 bool in_vbl = radeon_pm_in_vbl(rdev);
786
Dave Airlief7352612010-02-18 15:58:36 +1000787 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000788 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400789 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000790 return in_vbl;
791}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100792
Alex Deucherce8f5372010-05-07 15:10:16 -0400793static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100794{
795 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400796 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100797 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400798 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100799
Matthew Garrettd9932a32010-04-26 16:02:26 -0400800 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100801 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400802 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100803 unsigned long irq_flags;
804 int not_processed = 0;
805
806 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
807 if (!list_empty(&rdev->fence_drv.emited)) {
808 struct list_head *ptr;
809 list_for_each(ptr, &rdev->fence_drv.emited) {
810 /* count up to 3, that's enought info */
811 if (++not_processed >= 3)
812 break;
813 }
814 }
815 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
816
817 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400818 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
819 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
820 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
821 rdev->pm.dynpm_can_upclock) {
822 rdev->pm.dynpm_planned_action =
823 DYNPM_ACTION_UPCLOCK;
824 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100825 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
826 }
827 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400828 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
829 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
830 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
831 rdev->pm.dynpm_can_downclock) {
832 rdev->pm.dynpm_planned_action =
833 DYNPM_ACTION_DOWNCLOCK;
834 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100835 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
836 }
837 }
838
Alex Deucherd7311172010-05-03 01:13:14 -0400839 /* Note, radeon_pm_set_clocks is called with static_switch set
840 * to false since we want to wait for vbl to avoid flicker.
841 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400842 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
843 jiffies > rdev->pm.dynpm_action_timeout) {
844 radeon_pm_get_dynpm_state(rdev);
845 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100846 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000847
848 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
849 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100850 }
851 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400852 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100853}
854
Rafał Miłecki74338742009-11-03 00:53:02 +0100855/*
856 * Debugfs info
857 */
858#if defined(CONFIG_DEBUG_FS)
859
860static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
861{
862 struct drm_info_node *node = (struct drm_info_node *) m->private;
863 struct drm_device *dev = node->minor->dev;
864 struct radeon_device *rdev = dev->dev_private;
865
Rafał Miłecki62340772009-12-15 21:46:58 +0100866 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
867 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
868 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
869 if (rdev->asic->get_memory_clock)
870 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400871 if (rdev->pm.current_vddc)
872 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000873 if (rdev->asic->get_pcie_lanes)
874 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100875
876 return 0;
877}
878
879static struct drm_info_list radeon_pm_info_list[] = {
880 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
881};
882#endif
883
Rafał Miłeckic913e232009-12-22 23:02:16 +0100884static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100885{
886#if defined(CONFIG_DEBUG_FS)
887 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
888#else
889 return 0;
890#endif
891}