blob: aa14dbb7e4fbac2a81070f3d14abb28a24c3db30 [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 */
David Howells760285e2012-10-02 18:01:07 +010023#include <drm/drmP.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010024#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#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040028#include <linux/hwmon.h>
29#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010030
Rafał Miłeckic913e232009-12-22 23:02:16 +010031#define RADEON_IDLE_LOOP_MS 100
32#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010033#define RADEON_WAIT_VBLANK_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010034
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040035static const char *radeon_pm_state_type_name[5] = {
Alex Deuchereb2c27a2012-10-01 18:28:09 -040036 "",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040037 "Powersave",
38 "Battery",
39 "Balanced",
40 "Performance",
41};
42
Alex Deucherce8f5372010-05-07 15:10:16 -040043static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010044static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040045static bool radeon_pm_in_vbl(struct radeon_device *rdev);
46static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
47static void radeon_pm_update_profile(struct radeon_device *rdev);
48static void radeon_pm_set_clocks(struct radeon_device *rdev);
49
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040050int radeon_pm_get_type_index(struct radeon_device *rdev,
51 enum radeon_pm_state_type ps_type,
52 int instance)
53{
54 int i;
55 int found_instance = -1;
56
57 for (i = 0; i < rdev->pm.num_power_states; i++) {
58 if (rdev->pm.power_state[i].type == ps_type) {
59 found_instance++;
60 if (found_instance == instance)
61 return i;
62 }
63 }
64 /* return default if no match */
65 return rdev->pm.default_power_state_index;
66}
67
Alex Deucherc4917072012-07-31 17:14:35 -040068void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
Alex Deucherce8f5372010-05-07 15:10:16 -040069{
Alex Deucherc4917072012-07-31 17:14:35 -040070 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
71 if (rdev->pm.profile == PM_PROFILE_AUTO) {
72 mutex_lock(&rdev->pm.mutex);
73 radeon_pm_update_profile(rdev);
74 radeon_pm_set_clocks(rdev);
75 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040076 }
77 }
Alex Deucherce8f5372010-05-07 15:10:16 -040078}
Alex Deucherce8f5372010-05-07 15:10:16 -040079
80static void radeon_pm_update_profile(struct radeon_device *rdev)
81{
82 switch (rdev->pm.profile) {
83 case PM_PROFILE_DEFAULT:
84 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
85 break;
86 case PM_PROFILE_AUTO:
87 if (power_supply_is_system_supplied() > 0) {
88 if (rdev->pm.active_crtc_count > 1)
89 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
90 else
91 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
92 } else {
93 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -040094 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040095 else
Alex Deucherc9e75b22010-06-02 17:56:01 -040096 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040097 }
98 break;
99 case PM_PROFILE_LOW:
100 if (rdev->pm.active_crtc_count > 1)
101 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
102 else
103 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
104 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400105 case PM_PROFILE_MID:
106 if (rdev->pm.active_crtc_count > 1)
107 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
108 else
109 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
110 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400111 case PM_PROFILE_HIGH:
112 if (rdev->pm.active_crtc_count > 1)
113 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
114 else
115 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
116 break;
117 }
118
119 if (rdev->pm.active_crtc_count == 0) {
120 rdev->pm.requested_power_state_index =
121 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
122 rdev->pm.requested_clock_mode_index =
123 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
124 } else {
125 rdev->pm.requested_power_state_index =
126 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
127 rdev->pm.requested_clock_mode_index =
128 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
129 }
130}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100131
Matthew Garrett5876dd22010-04-26 15:52:20 -0400132static void radeon_unmap_vram_bos(struct radeon_device *rdev)
133{
134 struct radeon_bo *bo, *n;
135
136 if (list_empty(&rdev->gem.objects))
137 return;
138
139 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
140 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
141 ttm_bo_unmap_virtual(&bo->tbo);
142 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400143}
144
Alex Deucherce8f5372010-05-07 15:10:16 -0400145static void radeon_sync_with_vblank(struct radeon_device *rdev)
146{
147 if (rdev->pm.active_crtcs) {
148 rdev->pm.vblank_sync = false;
149 wait_event_timeout(
150 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
151 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
152 }
153}
154
155static void radeon_set_power_state(struct radeon_device *rdev)
156{
157 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400158 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400159
160 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
161 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
162 return;
163
164 if (radeon_gui_idle(rdev)) {
165 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
166 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500167 if (sclk > rdev->pm.default_sclk)
168 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400169
Alex Deucher27810fb2012-10-01 19:25:11 -0400170 /* starting with BTC, there is one state that is used for both
171 * MH and SH. Difference is that we always use the high clock index for
172 * mclk.
173 */
174 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
175 (rdev->family >= CHIP_BARTS) &&
176 rdev->pm.active_crtc_count &&
177 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
178 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
179 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
180 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
181 else
182 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
183 clock_info[rdev->pm.requested_clock_mode_index].mclk;
184
Alex Deucher9ace9f72011-01-06 21:19:26 -0500185 if (mclk > rdev->pm.default_mclk)
186 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400187
Alex Deucher92645872010-05-27 17:01:41 -0400188 /* upvolt before raising clocks, downvolt after lowering clocks */
189 if (sclk < rdev->pm.current_sclk)
190 misc_after = true;
191
192 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400193
194 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400195 if (!radeon_pm_in_vbl(rdev))
196 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400197 }
198
Alex Deucher92645872010-05-27 17:01:41 -0400199 radeon_pm_prepare(rdev);
200
201 if (!misc_after)
202 /* voltage, pcie lanes, etc.*/
203 radeon_pm_misc(rdev);
204
205 /* set engine clock */
206 if (sclk != rdev->pm.current_sclk) {
207 radeon_pm_debug_check_in_vbl(rdev, false);
208 radeon_set_engine_clock(rdev, sclk);
209 radeon_pm_debug_check_in_vbl(rdev, true);
210 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000211 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400212 }
213
214 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500215 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400216 radeon_pm_debug_check_in_vbl(rdev, false);
217 radeon_set_memory_clock(rdev, mclk);
218 radeon_pm_debug_check_in_vbl(rdev, true);
219 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000220 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400221 }
222
223 if (misc_after)
224 /* voltage, pcie lanes, etc.*/
225 radeon_pm_misc(rdev);
226
227 radeon_pm_finish(rdev);
228
Alex Deucherce8f5372010-05-07 15:10:16 -0400229 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
230 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
231 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000232 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400233}
234
235static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400236{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400237 int i;
238
Alex Deucher4e186b22010-08-13 10:53:35 -0400239 /* no need to take locks, etc. if nothing's going to change */
240 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
241 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
242 return;
243
Matthew Garrett612e06c2010-04-27 17:16:58 -0400244 mutex_lock(&rdev->ddev->struct_mutex);
Christian Königdb7fce32012-05-11 14:57:18 +0200245 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200246 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400247
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400248 /* wait for the rings to drain */
249 for (i = 0; i < RADEON_NUM_RINGS; i++) {
250 struct radeon_ring *ring = &rdev->ring[i];
251 if (ring->ready)
252 radeon_fence_wait_empty_locked(rdev, i);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400253 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400254
Matthew Garrett5876dd22010-04-26 15:52:20 -0400255 radeon_unmap_vram_bos(rdev);
256
Alex Deucherce8f5372010-05-07 15:10:16 -0400257 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400258 for (i = 0; i < rdev->num_crtc; i++) {
259 if (rdev->pm.active_crtcs & (1 << i)) {
260 rdev->pm.req_vblank |= (1 << i);
261 drm_vblank_get(rdev->ddev, i);
262 }
263 }
264 }
Alex Deucher539d2412010-04-29 00:22:43 -0400265
Alex Deucherce8f5372010-05-07 15:10:16 -0400266 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400267
Alex Deucherce8f5372010-05-07 15:10:16 -0400268 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400269 for (i = 0; i < rdev->num_crtc; i++) {
270 if (rdev->pm.req_vblank & (1 << i)) {
271 rdev->pm.req_vblank &= ~(1 << i);
272 drm_vblank_put(rdev->ddev, i);
273 }
274 }
275 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400276
Alex Deuchera4248162010-04-24 14:50:23 -0400277 /* update display watermarks based on new power state */
278 radeon_update_bandwidth_info(rdev);
279 if (rdev->pm.active_crtc_count)
280 radeon_bandwidth_update(rdev);
281
Alex Deucherce8f5372010-05-07 15:10:16 -0400282 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400283
Christian Königd6999bc2012-05-09 15:34:45 +0200284 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200285 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400286 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400287}
288
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400289static void radeon_pm_print_states(struct radeon_device *rdev)
290{
291 int i, j;
292 struct radeon_power_state *power_state;
293 struct radeon_pm_clock_info *clock_info;
294
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000295 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400296 for (i = 0; i < rdev->pm.num_power_states; i++) {
297 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000298 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400299 radeon_pm_state_type_name[power_state->type]);
300 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000301 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400302 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000303 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400304 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000305 DRM_DEBUG_DRIVER("\tSingle display only\n");
306 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400307 for (j = 0; j < power_state->num_clock_modes; j++) {
308 clock_info = &(power_state->clock_info[j]);
309 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400310 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
311 j,
312 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400313 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400314 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
315 j,
316 clock_info->sclk * 10,
317 clock_info->mclk * 10,
318 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400319 }
320 }
321}
322
Alex Deucherce8f5372010-05-07 15:10:16 -0400323static ssize_t radeon_get_pm_profile(struct device *dev,
324 struct device_attribute *attr,
325 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400326{
327 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
328 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400329 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400330
Alex Deucherce8f5372010-05-07 15:10:16 -0400331 return snprintf(buf, PAGE_SIZE, "%s\n",
332 (cp == PM_PROFILE_AUTO) ? "auto" :
333 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100334 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400335 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400336}
337
Alex Deucherce8f5372010-05-07 15:10:16 -0400338static ssize_t radeon_set_pm_profile(struct device *dev,
339 struct device_attribute *attr,
340 const char *buf,
341 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400342{
343 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
344 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400345
346 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400347 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
348 if (strncmp("default", buf, strlen("default")) == 0)
349 rdev->pm.profile = PM_PROFILE_DEFAULT;
350 else if (strncmp("auto", buf, strlen("auto")) == 0)
351 rdev->pm.profile = PM_PROFILE_AUTO;
352 else if (strncmp("low", buf, strlen("low")) == 0)
353 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400354 else if (strncmp("mid", buf, strlen("mid")) == 0)
355 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400356 else if (strncmp("high", buf, strlen("high")) == 0)
357 rdev->pm.profile = PM_PROFILE_HIGH;
358 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000359 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400360 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400361 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400362 radeon_pm_update_profile(rdev);
363 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000364 } else
365 count = -EINVAL;
366
Alex Deucherce8f5372010-05-07 15:10:16 -0400367fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400368 mutex_unlock(&rdev->pm.mutex);
369
370 return count;
371}
372
Alex Deucherce8f5372010-05-07 15:10:16 -0400373static ssize_t radeon_get_pm_method(struct device *dev,
374 struct device_attribute *attr,
375 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400376{
377 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
378 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400379 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400380
381 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400382 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400383}
384
Alex Deucherce8f5372010-05-07 15:10:16 -0400385static ssize_t radeon_set_pm_method(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf,
388 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400389{
390 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
391 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400392
Alex Deucherce8f5372010-05-07 15:10:16 -0400393
394 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400395 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400396 rdev->pm.pm_method = PM_METHOD_DYNPM;
397 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
398 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400399 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400400 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
401 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400402 /* disable dynpm */
403 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
404 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000405 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400406 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100407 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400408 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000409 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400410 goto fail;
411 }
412 radeon_pm_compute_clocks(rdev);
413fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400414 return count;
415}
416
Alex Deucherce8f5372010-05-07 15:10:16 -0400417static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
418static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400419
Alex Deucher21a81222010-07-02 12:58:16 -0400420static ssize_t radeon_hwmon_show_temp(struct device *dev,
421 struct device_attribute *attr,
422 char *buf)
423{
424 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
425 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500426 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400427
428 switch (rdev->pm.int_thermal_type) {
429 case THERMAL_TYPE_RV6XX:
430 temp = rv6xx_get_temp(rdev);
431 break;
432 case THERMAL_TYPE_RV770:
433 temp = rv770_get_temp(rdev);
434 break;
435 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500436 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400437 temp = evergreen_get_temp(rdev);
438 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500439 case THERMAL_TYPE_SUMO:
440 temp = sumo_get_temp(rdev);
441 break;
Alex Deucher1bd47d22012-03-20 17:18:10 -0400442 case THERMAL_TYPE_SI:
443 temp = si_get_temp(rdev);
444 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400445 default:
446 temp = 0;
447 break;
448 }
449
450 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
451}
452
453static ssize_t radeon_hwmon_show_name(struct device *dev,
454 struct device_attribute *attr,
455 char *buf)
456{
457 return sprintf(buf, "radeon\n");
458}
459
460static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
461static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
462
463static struct attribute *hwmon_attributes[] = {
464 &sensor_dev_attr_temp1_input.dev_attr.attr,
465 &sensor_dev_attr_name.dev_attr.attr,
466 NULL
467};
468
469static const struct attribute_group hwmon_attrgroup = {
470 .attrs = hwmon_attributes,
471};
472
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200473static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400474{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200475 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400476
477 rdev->pm.int_hwmon_dev = NULL;
478
479 switch (rdev->pm.int_thermal_type) {
480 case THERMAL_TYPE_RV6XX:
481 case THERMAL_TYPE_RV770:
482 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400483 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500484 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400485 case THERMAL_TYPE_SI:
Alex Deucher5d7486c2012-03-20 17:18:29 -0400486 /* No support for TN yet */
487 if (rdev->family == CHIP_ARUBA)
488 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400489 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200490 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
491 err = PTR_ERR(rdev->pm.int_hwmon_dev);
492 dev_err(rdev->dev,
493 "Unable to register hwmon device: %d\n", err);
494 break;
495 }
Alex Deucher21a81222010-07-02 12:58:16 -0400496 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
497 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
498 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200499 if (err) {
500 dev_err(rdev->dev,
501 "Unable to create hwmon sysfs file: %d\n", err);
502 hwmon_device_unregister(rdev->dev);
503 }
Alex Deucher21a81222010-07-02 12:58:16 -0400504 break;
505 default:
506 break;
507 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200508
509 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400510}
511
512static void radeon_hwmon_fini(struct radeon_device *rdev)
513{
514 if (rdev->pm.int_hwmon_dev) {
515 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
516 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
517 }
518}
519
Alex Deucherce8f5372010-05-07 15:10:16 -0400520void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500521{
Alex Deucherce8f5372010-05-07 15:10:16 -0400522 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000523 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000524 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
525 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000526 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400527 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100528
529 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
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 Deuchered18a362011-01-06 21:19:32 -0500534 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -0400535 if ((rdev->family >= CHIP_BARTS) &&
536 (rdev->family <= CHIP_CAYMAN) &&
537 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -0500538 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400539 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
540 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400541 if (rdev->pm.default_vddci)
542 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
543 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500544 if (rdev->pm.default_sclk)
545 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
546 if (rdev->pm.default_mclk)
547 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
548 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400549 /* asic init will reset the default power state */
550 mutex_lock(&rdev->pm.mutex);
551 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
552 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500553 rdev->pm.current_sclk = rdev->pm.default_sclk;
554 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400555 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 -0400556 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 +0000557 if (rdev->pm.pm_method == PM_METHOD_DYNPM
558 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
559 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100560 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
561 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000562 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400563 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400564 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100565}
566
Rafał Miłecki74338742009-11-03 00:53:02 +0100567int radeon_pm_init(struct radeon_device *rdev)
568{
Dave Airlie26481fb2010-05-18 19:00:14 +1000569 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200570
Alex Deucherce8f5372010-05-07 15:10:16 -0400571 /* default to profile method */
572 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400573 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400574 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
575 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
576 rdev->pm.dynpm_can_upclock = true;
577 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500578 rdev->pm.default_sclk = rdev->clock.default_sclk;
579 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400580 rdev->pm.current_sclk = rdev->clock.default_sclk;
581 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400582 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100583
Alex Deucher56278a82009-12-28 13:58:44 -0500584 if (rdev->bios) {
585 if (rdev->is_atom_bios)
586 radeon_atombios_get_power_modes(rdev);
587 else
588 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400589 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400590 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500591 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -0400592 if ((rdev->family >= CHIP_BARTS) &&
593 (rdev->family <= CHIP_CAYMAN) &&
594 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -0500595 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400596 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
597 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -0400598 if (rdev->pm.default_vddci)
599 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
600 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500601 if (rdev->pm.default_sclk)
602 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
603 if (rdev->pm.default_mclk)
604 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
605 }
Alex Deucher56278a82009-12-28 13:58:44 -0500606 }
607
Alex Deucher21a81222010-07-02 12:58:16 -0400608 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200609 ret = radeon_hwmon_init(rdev);
610 if (ret)
611 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100612
613 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
614
Alex Deucherce8f5372010-05-07 15:10:16 -0400615 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400616 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000617 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
618 if (ret)
619 DRM_ERROR("failed to create device file for power profile\n");
620 ret = device_create_file(rdev->dev, &dev_attr_power_method);
621 if (ret)
622 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400623
Alex Deucherce8f5372010-05-07 15:10:16 -0400624 if (radeon_debugfs_pm_init(rdev)) {
625 DRM_ERROR("Failed to register debugfs file for PM!\n");
626 }
627
628 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100629 }
630
631 return 0;
632}
633
Alex Deucher29fb52c2010-03-11 10:01:17 -0500634void radeon_pm_fini(struct radeon_device *rdev)
635{
Alex Deucherce8f5372010-05-07 15:10:16 -0400636 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400637 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400638 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
639 rdev->pm.profile = PM_PROFILE_DEFAULT;
640 radeon_pm_update_profile(rdev);
641 radeon_pm_set_clocks(rdev);
642 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400643 /* reset default clocks */
644 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
645 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
646 radeon_pm_set_clocks(rdev);
647 }
Alex Deuchera4248162010-04-24 14:50:23 -0400648 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100649
650 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400651
Alex Deucherce8f5372010-05-07 15:10:16 -0400652 device_remove_file(rdev->dev, &dev_attr_power_profile);
653 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -0400654 }
Alex Deuchera4248162010-04-24 14:50:23 -0400655
Alex Deucher0975b162011-02-02 18:42:03 -0500656 if (rdev->pm.power_state)
657 kfree(rdev->pm.power_state);
658
Alex Deucher21a81222010-07-02 12:58:16 -0400659 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500660}
661
Rafał Miłeckic913e232009-12-22 23:02:16 +0100662void radeon_pm_compute_clocks(struct radeon_device *rdev)
663{
664 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400665 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100666 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100667
Alex Deucherce8f5372010-05-07 15:10:16 -0400668 if (rdev->pm.num_power_states < 2)
669 return;
670
Rafał Miłeckic913e232009-12-22 23:02:16 +0100671 mutex_lock(&rdev->pm.mutex);
672
673 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400674 rdev->pm.active_crtc_count = 0;
675 list_for_each_entry(crtc,
676 &ddev->mode_config.crtc_list, head) {
677 radeon_crtc = to_radeon_crtc(crtc);
678 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100679 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400680 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100681 }
682 }
683
Alex Deucherce8f5372010-05-07 15:10:16 -0400684 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
685 radeon_pm_update_profile(rdev);
686 radeon_pm_set_clocks(rdev);
687 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
688 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
689 if (rdev->pm.active_crtc_count > 1) {
690 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
691 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400692
Alex Deucherce8f5372010-05-07 15:10:16 -0400693 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
694 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
695 radeon_pm_get_dynpm_state(rdev);
696 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100697
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000698 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400699 }
700 } else if (rdev->pm.active_crtc_count == 1) {
701 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100702
Alex Deucherce8f5372010-05-07 15:10:16 -0400703 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
704 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
705 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
706 radeon_pm_get_dynpm_state(rdev);
707 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100708
Tejun Heo32c87fc2011-01-03 14:49:32 +0100709 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
710 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400711 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
712 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100713 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
714 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000715 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400716 }
717 } else { /* count == 0 */
718 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
719 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100720
Alex Deucherce8f5372010-05-07 15:10:16 -0400721 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
722 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
723 radeon_pm_get_dynpm_state(rdev);
724 radeon_pm_set_clocks(rdev);
725 }
726 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100727 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100728 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100729
730 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100731}
732
Alex Deucherce8f5372010-05-07 15:10:16 -0400733static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000734{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400735 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000736 bool in_vbl = true;
737
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400738 /* Iterate over all active crtc's. All crtc's must be in vblank,
739 * otherwise return in_vbl == false.
740 */
741 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
742 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200743 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
744 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
745 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000746 in_vbl = false;
747 }
748 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400749
750 return in_vbl;
751}
752
Alex Deucherce8f5372010-05-07 15:10:16 -0400753static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400754{
755 u32 stat_crtc = 0;
756 bool in_vbl = radeon_pm_in_vbl(rdev);
757
Dave Airlief7352612010-02-18 15:58:36 +1000758 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000759 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400760 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000761 return in_vbl;
762}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100763
Alex Deucherce8f5372010-05-07 15:10:16 -0400764static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100765{
766 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400767 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100768 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400769 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100770
Matthew Garrettd9932a32010-04-26 16:02:26 -0400771 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100772 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400773 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100774 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -0400775 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100776
Alex Deucher74652802011-08-25 13:39:48 -0400777 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -0400778 struct radeon_ring *ring = &rdev->ring[i];
779
780 if (ring->ready) {
781 not_processed += radeon_fence_count_emitted(rdev, i);
782 if (not_processed >= 3)
783 break;
784 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100785 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100786
787 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400788 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
789 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
790 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
791 rdev->pm.dynpm_can_upclock) {
792 rdev->pm.dynpm_planned_action =
793 DYNPM_ACTION_UPCLOCK;
794 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100795 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
796 }
797 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400798 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
799 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
800 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
801 rdev->pm.dynpm_can_downclock) {
802 rdev->pm.dynpm_planned_action =
803 DYNPM_ACTION_DOWNCLOCK;
804 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100805 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
806 }
807 }
808
Alex Deucherd7311172010-05-03 01:13:14 -0400809 /* Note, radeon_pm_set_clocks is called with static_switch set
810 * to false since we want to wait for vbl to avoid flicker.
811 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400812 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
813 jiffies > rdev->pm.dynpm_action_timeout) {
814 radeon_pm_get_dynpm_state(rdev);
815 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100816 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000817
Tejun Heo32c87fc2011-01-03 14:49:32 +0100818 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
819 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100820 }
821 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400822 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100823}
824
Rafał Miłecki74338742009-11-03 00:53:02 +0100825/*
826 * Debugfs info
827 */
828#if defined(CONFIG_DEBUG_FS)
829
830static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
831{
832 struct drm_info_node *node = (struct drm_info_node *) m->private;
833 struct drm_device *dev = node->minor->dev;
834 struct radeon_device *rdev = dev->dev_private;
835
Alex Deucher9ace9f72011-01-06 21:19:26 -0500836 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100837 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500838 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Alex Deucher798bcf72012-02-23 17:53:48 -0500839 if (rdev->asic->pm.get_memory_clock)
Rafał Miłecki62340772009-12-15 21:46:58 +0100840 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400841 if (rdev->pm.current_vddc)
842 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Alex Deucher798bcf72012-02-23 17:53:48 -0500843 if (rdev->asic->pm.get_pcie_lanes)
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000844 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100845
846 return 0;
847}
848
849static struct drm_info_list radeon_pm_info_list[] = {
850 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
851};
852#endif
853
Rafał Miłeckic913e232009-12-22 23:02:16 +0100854static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100855{
856#if defined(CONFIG_DEBUG_FS)
857 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
858#else
859 return 0;
860#endif
861}