blob: 78a665bd95198483862ba0c124453a1d14a87d48 [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
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040056int radeon_pm_get_type_index(struct radeon_device *rdev,
57 enum radeon_pm_state_type ps_type,
58 int instance)
59{
60 int i;
61 int found_instance = -1;
62
63 for (i = 0; i < rdev->pm.num_power_states; i++) {
64 if (rdev->pm.power_state[i].type == ps_type) {
65 found_instance++;
66 if (found_instance == instance)
67 return i;
68 }
69 }
70 /* return default if no match */
71 return rdev->pm.default_power_state_index;
72}
73
Alex Deucherce8f5372010-05-07 15:10:16 -040074#ifdef CONFIG_ACPI
75static int radeon_acpi_event(struct notifier_block *nb,
76 unsigned long val,
77 void *data)
78{
79 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
80 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
81
82 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
83 if (power_supply_is_system_supplied() > 0)
Dave Airlied9fdaaf2010-08-02 10:42:55 +100084 DRM_DEBUG_DRIVER("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040085 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +100086 DRM_DEBUG_DRIVER("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040087
88 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
89 if (rdev->pm.profile == PM_PROFILE_AUTO) {
90 mutex_lock(&rdev->pm.mutex);
91 radeon_pm_update_profile(rdev);
92 radeon_pm_set_clocks(rdev);
93 mutex_unlock(&rdev->pm.mutex);
94 }
95 }
96 }
97
98 return NOTIFY_OK;
99}
100#endif
101
102static void radeon_pm_update_profile(struct radeon_device *rdev)
103{
104 switch (rdev->pm.profile) {
105 case PM_PROFILE_DEFAULT:
106 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
107 break;
108 case PM_PROFILE_AUTO:
109 if (power_supply_is_system_supplied() > 0) {
110 if (rdev->pm.active_crtc_count > 1)
111 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
112 else
113 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
114 } else {
115 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400116 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400117 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400118 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400119 }
120 break;
121 case PM_PROFILE_LOW:
122 if (rdev->pm.active_crtc_count > 1)
123 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
124 else
125 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
126 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400127 case PM_PROFILE_MID:
128 if (rdev->pm.active_crtc_count > 1)
129 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
130 else
131 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
132 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400133 case PM_PROFILE_HIGH:
134 if (rdev->pm.active_crtc_count > 1)
135 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
136 else
137 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
138 break;
139 }
140
141 if (rdev->pm.active_crtc_count == 0) {
142 rdev->pm.requested_power_state_index =
143 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
144 rdev->pm.requested_clock_mode_index =
145 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
146 } else {
147 rdev->pm.requested_power_state_index =
148 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
149 rdev->pm.requested_clock_mode_index =
150 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
151 }
152}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100153
Matthew Garrett5876dd22010-04-26 15:52:20 -0400154static void radeon_unmap_vram_bos(struct radeon_device *rdev)
155{
156 struct radeon_bo *bo, *n;
157
158 if (list_empty(&rdev->gem.objects))
159 return;
160
161 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
162 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
163 ttm_bo_unmap_virtual(&bo->tbo);
164 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400165}
166
Alex Deucherce8f5372010-05-07 15:10:16 -0400167static void radeon_sync_with_vblank(struct radeon_device *rdev)
168{
169 if (rdev->pm.active_crtcs) {
170 rdev->pm.vblank_sync = false;
171 wait_event_timeout(
172 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
173 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
174 }
175}
176
177static void radeon_set_power_state(struct radeon_device *rdev)
178{
179 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400180 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400181
182 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
183 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
184 return;
185
186 if (radeon_gui_idle(rdev)) {
187 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
188 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500189 if (sclk > rdev->pm.default_sclk)
190 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400191
192 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
193 clock_info[rdev->pm.requested_clock_mode_index].mclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500194 if (mclk > rdev->pm.default_mclk)
195 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400196
Alex Deucher92645872010-05-27 17:01:41 -0400197 /* upvolt before raising clocks, downvolt after lowering clocks */
198 if (sclk < rdev->pm.current_sclk)
199 misc_after = true;
200
201 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400202
203 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400204 if (!radeon_pm_in_vbl(rdev))
205 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400206 }
207
Alex Deucher92645872010-05-27 17:01:41 -0400208 radeon_pm_prepare(rdev);
209
210 if (!misc_after)
211 /* voltage, pcie lanes, etc.*/
212 radeon_pm_misc(rdev);
213
214 /* set engine clock */
215 if (sclk != rdev->pm.current_sclk) {
216 radeon_pm_debug_check_in_vbl(rdev, false);
217 radeon_set_engine_clock(rdev, sclk);
218 radeon_pm_debug_check_in_vbl(rdev, true);
219 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000220 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400221 }
222
223 /* set memory clock */
224 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
225 radeon_pm_debug_check_in_vbl(rdev, false);
226 radeon_set_memory_clock(rdev, mclk);
227 radeon_pm_debug_check_in_vbl(rdev, true);
228 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000229 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400230 }
231
232 if (misc_after)
233 /* voltage, pcie lanes, etc.*/
234 radeon_pm_misc(rdev);
235
236 radeon_pm_finish(rdev);
237
Alex Deucherce8f5372010-05-07 15:10:16 -0400238 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
239 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
240 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000241 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400242}
243
244static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400245{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400246 int i;
247
Alex Deucher4e186b22010-08-13 10:53:35 -0400248 /* no need to take locks, etc. if nothing's going to change */
249 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
250 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
251 return;
252
Matthew Garrett612e06c2010-04-27 17:16:58 -0400253 mutex_lock(&rdev->ddev->struct_mutex);
254 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400255 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400256
257 /* gui idle int has issues on older chips it seems */
258 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400259 if (rdev->irq.installed) {
260 /* wait for GPU idle */
261 rdev->pm.gui_idle = false;
262 rdev->irq.gui_idle = true;
263 radeon_irq_set(rdev);
264 wait_event_interruptible_timeout(
265 rdev->irq.idle_queue, rdev->pm.gui_idle,
266 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
267 rdev->irq.gui_idle = false;
268 radeon_irq_set(rdev);
269 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400270 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400271 if (rdev->cp.ready) {
272 struct radeon_fence *fence;
273 radeon_ring_alloc(rdev, 64);
274 radeon_fence_create(rdev, &fence);
275 radeon_fence_emit(rdev, fence);
276 radeon_ring_commit(rdev);
277 radeon_fence_wait(fence, false);
278 radeon_fence_unref(&fence);
279 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400280 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400281 radeon_unmap_vram_bos(rdev);
282
Alex Deucherce8f5372010-05-07 15:10:16 -0400283 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400284 for (i = 0; i < rdev->num_crtc; i++) {
285 if (rdev->pm.active_crtcs & (1 << i)) {
286 rdev->pm.req_vblank |= (1 << i);
287 drm_vblank_get(rdev->ddev, i);
288 }
289 }
290 }
Alex Deucher539d2412010-04-29 00:22:43 -0400291
Alex Deucherce8f5372010-05-07 15:10:16 -0400292 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400293
Alex Deucherce8f5372010-05-07 15:10:16 -0400294 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400295 for (i = 0; i < rdev->num_crtc; i++) {
296 if (rdev->pm.req_vblank & (1 << i)) {
297 rdev->pm.req_vblank &= ~(1 << i);
298 drm_vblank_put(rdev->ddev, i);
299 }
300 }
301 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400302
Alex Deuchera4248162010-04-24 14:50:23 -0400303 /* update display watermarks based on new power state */
304 radeon_update_bandwidth_info(rdev);
305 if (rdev->pm.active_crtc_count)
306 radeon_bandwidth_update(rdev);
307
Alex Deucherce8f5372010-05-07 15:10:16 -0400308 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400309
Alex Deuchera4248162010-04-24 14:50:23 -0400310 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400311 mutex_unlock(&rdev->vram_mutex);
312 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400313}
314
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400315static void radeon_pm_print_states(struct radeon_device *rdev)
316{
317 int i, j;
318 struct radeon_power_state *power_state;
319 struct radeon_pm_clock_info *clock_info;
320
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000321 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400322 for (i = 0; i < rdev->pm.num_power_states; i++) {
323 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000324 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400325 radeon_pm_state_type_name[power_state->type]);
326 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000327 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400328 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000329 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400330 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000331 DRM_DEBUG_DRIVER("\tSingle display only\n");
332 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400333 for (j = 0; j < power_state->num_clock_modes; j++) {
334 clock_info = &(power_state->clock_info[j]);
335 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000336 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400337 j,
338 clock_info->sclk * 10,
339 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
340 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000341 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400342 j,
343 clock_info->sclk * 10,
344 clock_info->mclk * 10,
345 clock_info->voltage.voltage,
346 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
347 }
348 }
349}
350
Alex Deucherce8f5372010-05-07 15:10:16 -0400351static ssize_t radeon_get_pm_profile(struct device *dev,
352 struct device_attribute *attr,
353 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400354{
355 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
356 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400357 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400358
Alex Deucherce8f5372010-05-07 15:10:16 -0400359 return snprintf(buf, PAGE_SIZE, "%s\n",
360 (cp == PM_PROFILE_AUTO) ? "auto" :
361 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100362 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400363 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400364}
365
Alex Deucherce8f5372010-05-07 15:10:16 -0400366static ssize_t radeon_set_pm_profile(struct device *dev,
367 struct device_attribute *attr,
368 const char *buf,
369 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400370{
371 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
372 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400373
374 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400375 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
376 if (strncmp("default", buf, strlen("default")) == 0)
377 rdev->pm.profile = PM_PROFILE_DEFAULT;
378 else if (strncmp("auto", buf, strlen("auto")) == 0)
379 rdev->pm.profile = PM_PROFILE_AUTO;
380 else if (strncmp("low", buf, strlen("low")) == 0)
381 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400382 else if (strncmp("mid", buf, strlen("mid")) == 0)
383 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400384 else if (strncmp("high", buf, strlen("high")) == 0)
385 rdev->pm.profile = PM_PROFILE_HIGH;
386 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000387 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400388 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400389 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400390 radeon_pm_update_profile(rdev);
391 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000392 } else
393 count = -EINVAL;
394
Alex Deucherce8f5372010-05-07 15:10:16 -0400395fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400396 mutex_unlock(&rdev->pm.mutex);
397
398 return count;
399}
400
Alex Deucherce8f5372010-05-07 15:10:16 -0400401static ssize_t radeon_get_pm_method(struct device *dev,
402 struct device_attribute *attr,
403 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400404{
405 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
406 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400407 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400408
409 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400410 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400411}
412
Alex Deucherce8f5372010-05-07 15:10:16 -0400413static ssize_t radeon_set_pm_method(struct device *dev,
414 struct device_attribute *attr,
415 const char *buf,
416 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400417{
418 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
419 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400420
Alex Deucherce8f5372010-05-07 15:10:16 -0400421
422 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400423 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400424 rdev->pm.pm_method = PM_METHOD_DYNPM;
425 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
426 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400427 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400428 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
429 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400430 /* disable dynpm */
431 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
432 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000433 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400434 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100435 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400436 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000437 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400438 goto fail;
439 }
440 radeon_pm_compute_clocks(rdev);
441fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400442 return count;
443}
444
Alex Deucherce8f5372010-05-07 15:10:16 -0400445static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
446static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400447
Alex Deucher21a81222010-07-02 12:58:16 -0400448static ssize_t radeon_hwmon_show_temp(struct device *dev,
449 struct device_attribute *attr,
450 char *buf)
451{
452 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
453 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500454 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400455
456 switch (rdev->pm.int_thermal_type) {
457 case THERMAL_TYPE_RV6XX:
458 temp = rv6xx_get_temp(rdev);
459 break;
460 case THERMAL_TYPE_RV770:
461 temp = rv770_get_temp(rdev);
462 break;
463 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500464 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400465 temp = evergreen_get_temp(rdev);
466 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500467 case THERMAL_TYPE_SUMO:
468 temp = sumo_get_temp(rdev);
469 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400470 default:
471 temp = 0;
472 break;
473 }
474
475 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
476}
477
478static ssize_t radeon_hwmon_show_name(struct device *dev,
479 struct device_attribute *attr,
480 char *buf)
481{
482 return sprintf(buf, "radeon\n");
483}
484
485static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
486static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
487
488static struct attribute *hwmon_attributes[] = {
489 &sensor_dev_attr_temp1_input.dev_attr.attr,
490 &sensor_dev_attr_name.dev_attr.attr,
491 NULL
492};
493
494static const struct attribute_group hwmon_attrgroup = {
495 .attrs = hwmon_attributes,
496};
497
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200498static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400499{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200500 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400501
502 rdev->pm.int_hwmon_dev = NULL;
503
504 switch (rdev->pm.int_thermal_type) {
505 case THERMAL_TYPE_RV6XX:
506 case THERMAL_TYPE_RV770:
507 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400508 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500509 case THERMAL_TYPE_SUMO:
Alex Deucher21a81222010-07-02 12:58:16 -0400510 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200511 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
512 err = PTR_ERR(rdev->pm.int_hwmon_dev);
513 dev_err(rdev->dev,
514 "Unable to register hwmon device: %d\n", err);
515 break;
516 }
Alex Deucher21a81222010-07-02 12:58:16 -0400517 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
518 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
519 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200520 if (err) {
521 dev_err(rdev->dev,
522 "Unable to create hwmon sysfs file: %d\n", err);
523 hwmon_device_unregister(rdev->dev);
524 }
Alex Deucher21a81222010-07-02 12:58:16 -0400525 break;
526 default:
527 break;
528 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200529
530 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400531}
532
533static void radeon_hwmon_fini(struct radeon_device *rdev)
534{
535 if (rdev->pm.int_hwmon_dev) {
536 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
537 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
538 }
539}
540
Alex Deucherce8f5372010-05-07 15:10:16 -0400541void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500542{
Alex Deucherce8f5372010-05-07 15:10:16 -0400543 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000544 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000545 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
546 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000547 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400548 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100549
550 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500551}
552
Alex Deucherce8f5372010-05-07 15:10:16 -0400553void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100554{
Alex Deuchered18a362011-01-06 21:19:32 -0500555 /* set up the default clocks if the MC ucode is loaded */
556 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
557 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400558 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
559 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400560 if (rdev->pm.default_vddci)
561 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
562 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500563 if (rdev->pm.default_sclk)
564 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
565 if (rdev->pm.default_mclk)
566 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
567 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400568 /* asic init will reset the default power state */
569 mutex_lock(&rdev->pm.mutex);
570 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
571 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500572 rdev->pm.current_sclk = rdev->pm.default_sclk;
573 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400574 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 -0400575 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 +0000576 if (rdev->pm.pm_method == PM_METHOD_DYNPM
577 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
578 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100579 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
580 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000581 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400582 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400583 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100584}
585
Rafał Miłecki74338742009-11-03 00:53:02 +0100586int radeon_pm_init(struct radeon_device *rdev)
587{
Dave Airlie26481fb2010-05-18 19:00:14 +1000588 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200589
Alex Deucherce8f5372010-05-07 15:10:16 -0400590 /* default to profile method */
591 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400592 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400593 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
594 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
595 rdev->pm.dynpm_can_upclock = true;
596 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500597 rdev->pm.default_sclk = rdev->clock.default_sclk;
598 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400599 rdev->pm.current_sclk = rdev->clock.default_sclk;
600 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400601 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100602
Alex Deucher56278a82009-12-28 13:58:44 -0500603 if (rdev->bios) {
604 if (rdev->is_atom_bios)
605 radeon_atombios_get_power_modes(rdev);
606 else
607 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400608 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400609 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500610 /* set up the default clocks if the MC ucode is loaded */
611 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
612 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400613 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
614 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -0400615 if (rdev->pm.default_vddci)
616 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
617 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500618 if (rdev->pm.default_sclk)
619 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
620 if (rdev->pm.default_mclk)
621 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
622 }
Alex Deucher56278a82009-12-28 13:58:44 -0500623 }
624
Alex Deucher21a81222010-07-02 12:58:16 -0400625 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200626 ret = radeon_hwmon_init(rdev);
627 if (ret)
628 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100629
630 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
631
Alex Deucherce8f5372010-05-07 15:10:16 -0400632 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400633 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000634 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
635 if (ret)
636 DRM_ERROR("failed to create device file for power profile\n");
637 ret = device_create_file(rdev->dev, &dev_attr_power_method);
638 if (ret)
639 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400640
641#ifdef CONFIG_ACPI
642 rdev->acpi_nb.notifier_call = radeon_acpi_event;
643 register_acpi_notifier(&rdev->acpi_nb);
644#endif
Alex Deucherce8f5372010-05-07 15:10:16 -0400645 if (radeon_debugfs_pm_init(rdev)) {
646 DRM_ERROR("Failed to register debugfs file for PM!\n");
647 }
648
649 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100650 }
651
652 return 0;
653}
654
Alex Deucher29fb52c2010-03-11 10:01:17 -0500655void radeon_pm_fini(struct radeon_device *rdev)
656{
Alex Deucherce8f5372010-05-07 15:10:16 -0400657 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400658 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400659 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
660 rdev->pm.profile = PM_PROFILE_DEFAULT;
661 radeon_pm_update_profile(rdev);
662 radeon_pm_set_clocks(rdev);
663 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400664 /* reset default clocks */
665 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
666 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
667 radeon_pm_set_clocks(rdev);
668 }
Alex Deuchera4248162010-04-24 14:50:23 -0400669 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100670
671 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400672
Alex Deucherce8f5372010-05-07 15:10:16 -0400673 device_remove_file(rdev->dev, &dev_attr_power_profile);
674 device_remove_file(rdev->dev, &dev_attr_power_method);
675#ifdef CONFIG_ACPI
676 unregister_acpi_notifier(&rdev->acpi_nb);
677#endif
678 }
Alex Deuchera4248162010-04-24 14:50:23 -0400679
Alex Deucher0975b162011-02-02 18:42:03 -0500680 if (rdev->pm.power_state)
681 kfree(rdev->pm.power_state);
682
Alex Deucher21a81222010-07-02 12:58:16 -0400683 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500684}
685
Rafał Miłeckic913e232009-12-22 23:02:16 +0100686void radeon_pm_compute_clocks(struct radeon_device *rdev)
687{
688 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400689 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100690 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100691
Alex Deucherce8f5372010-05-07 15:10:16 -0400692 if (rdev->pm.num_power_states < 2)
693 return;
694
Rafał Miłeckic913e232009-12-22 23:02:16 +0100695 mutex_lock(&rdev->pm.mutex);
696
697 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400698 rdev->pm.active_crtc_count = 0;
699 list_for_each_entry(crtc,
700 &ddev->mode_config.crtc_list, head) {
701 radeon_crtc = to_radeon_crtc(crtc);
702 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100703 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400704 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100705 }
706 }
707
Alex Deucherce8f5372010-05-07 15:10:16 -0400708 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
709 radeon_pm_update_profile(rdev);
710 radeon_pm_set_clocks(rdev);
711 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
712 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
713 if (rdev->pm.active_crtc_count > 1) {
714 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
715 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400716
Alex Deucherce8f5372010-05-07 15:10:16 -0400717 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
718 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
719 radeon_pm_get_dynpm_state(rdev);
720 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100721
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000722 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400723 }
724 } else if (rdev->pm.active_crtc_count == 1) {
725 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100726
Alex Deucherce8f5372010-05-07 15:10:16 -0400727 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
728 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
729 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
730 radeon_pm_get_dynpm_state(rdev);
731 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100732
Tejun Heo32c87fc2011-01-03 14:49:32 +0100733 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
734 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400735 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
736 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100737 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
738 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000739 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400740 }
741 } else { /* count == 0 */
742 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
743 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100744
Alex Deucherce8f5372010-05-07 15:10:16 -0400745 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
746 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
747 radeon_pm_get_dynpm_state(rdev);
748 radeon_pm_set_clocks(rdev);
749 }
750 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100751 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100752 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100753
754 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100755}
756
Alex Deucherce8f5372010-05-07 15:10:16 -0400757static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000758{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400759 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000760 bool in_vbl = true;
761
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400762 /* Iterate over all active crtc's. All crtc's must be in vblank,
763 * otherwise return in_vbl == false.
764 */
765 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
766 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200767 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
768 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
769 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000770 in_vbl = false;
771 }
772 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400773
774 return in_vbl;
775}
776
Alex Deucherce8f5372010-05-07 15:10:16 -0400777static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400778{
779 u32 stat_crtc = 0;
780 bool in_vbl = radeon_pm_in_vbl(rdev);
781
Dave Airlief7352612010-02-18 15:58:36 +1000782 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000783 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400784 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000785 return in_vbl;
786}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100787
Alex Deucherce8f5372010-05-07 15:10:16 -0400788static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100789{
790 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400791 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100792 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400793 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100794
Matthew Garrettd9932a32010-04-26 16:02:26 -0400795 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100796 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400797 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100798 unsigned long irq_flags;
799 int not_processed = 0;
800
801 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
802 if (!list_empty(&rdev->fence_drv.emited)) {
803 struct list_head *ptr;
804 list_for_each(ptr, &rdev->fence_drv.emited) {
805 /* count up to 3, that's enought info */
806 if (++not_processed >= 3)
807 break;
808 }
809 }
810 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
811
812 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400813 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
814 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
815 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
816 rdev->pm.dynpm_can_upclock) {
817 rdev->pm.dynpm_planned_action =
818 DYNPM_ACTION_UPCLOCK;
819 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100820 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
821 }
822 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400823 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
824 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
825 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
826 rdev->pm.dynpm_can_downclock) {
827 rdev->pm.dynpm_planned_action =
828 DYNPM_ACTION_DOWNCLOCK;
829 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100830 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
831 }
832 }
833
Alex Deucherd7311172010-05-03 01:13:14 -0400834 /* Note, radeon_pm_set_clocks is called with static_switch set
835 * to false since we want to wait for vbl to avoid flicker.
836 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400837 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
838 jiffies > rdev->pm.dynpm_action_timeout) {
839 radeon_pm_get_dynpm_state(rdev);
840 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100841 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000842
Tejun Heo32c87fc2011-01-03 14:49:32 +0100843 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
844 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100845 }
846 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400847 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100848}
849
Rafał Miłecki74338742009-11-03 00:53:02 +0100850/*
851 * Debugfs info
852 */
853#if defined(CONFIG_DEBUG_FS)
854
855static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
856{
857 struct drm_info_node *node = (struct drm_info_node *) m->private;
858 struct drm_device *dev = node->minor->dev;
859 struct radeon_device *rdev = dev->dev_private;
860
Alex Deucher9ace9f72011-01-06 21:19:26 -0500861 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100862 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500863 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100864 if (rdev->asic->get_memory_clock)
865 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400866 if (rdev->pm.current_vddc)
867 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000868 if (rdev->asic->get_pcie_lanes)
869 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100870
871 return 0;
872}
873
874static struct drm_info_list radeon_pm_info_list[] = {
875 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
876};
877#endif
878
Rafał Miłeckic913e232009-12-22 23:02:16 +0100879static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100880{
881#if defined(CONFIG_DEBUG_FS)
882 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
883#else
884 return 0;
885#endif
886}