blob: 6fac8efe8340e2e99f83965201bab056de2997de [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 Deucher1c71bda2013-09-09 19:11:52 -040070 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
71 mutex_lock(&rdev->pm.mutex);
72 if (power_supply_is_system_supplied() > 0)
73 rdev->pm.dpm.ac_power = true;
74 else
75 rdev->pm.dpm.ac_power = false;
76 if (rdev->asic->dpm.enable_bapm)
77 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
78 mutex_unlock(&rdev->pm.mutex);
79 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
Alex Deucherc4917072012-07-31 17:14:35 -040080 if (rdev->pm.profile == PM_PROFILE_AUTO) {
81 mutex_lock(&rdev->pm.mutex);
82 radeon_pm_update_profile(rdev);
83 radeon_pm_set_clocks(rdev);
84 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040085 }
86 }
Alex Deucherce8f5372010-05-07 15:10:16 -040087}
Alex Deucherce8f5372010-05-07 15:10:16 -040088
89static void radeon_pm_update_profile(struct radeon_device *rdev)
90{
91 switch (rdev->pm.profile) {
92 case PM_PROFILE_DEFAULT:
93 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
94 break;
95 case PM_PROFILE_AUTO:
96 if (power_supply_is_system_supplied() > 0) {
97 if (rdev->pm.active_crtc_count > 1)
98 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
99 else
100 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
101 } else {
102 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400103 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400104 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400105 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400106 }
107 break;
108 case PM_PROFILE_LOW:
109 if (rdev->pm.active_crtc_count > 1)
110 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
111 else
112 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
113 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400114 case PM_PROFILE_MID:
115 if (rdev->pm.active_crtc_count > 1)
116 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
117 else
118 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
119 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400120 case PM_PROFILE_HIGH:
121 if (rdev->pm.active_crtc_count > 1)
122 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
123 else
124 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
125 break;
126 }
127
128 if (rdev->pm.active_crtc_count == 0) {
129 rdev->pm.requested_power_state_index =
130 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
131 rdev->pm.requested_clock_mode_index =
132 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
133 } else {
134 rdev->pm.requested_power_state_index =
135 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
136 rdev->pm.requested_clock_mode_index =
137 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
138 }
139}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100140
Matthew Garrett5876dd22010-04-26 15:52:20 -0400141static void radeon_unmap_vram_bos(struct radeon_device *rdev)
142{
143 struct radeon_bo *bo, *n;
144
145 if (list_empty(&rdev->gem.objects))
146 return;
147
148 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
149 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
150 ttm_bo_unmap_virtual(&bo->tbo);
151 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400152}
153
Alex Deucherce8f5372010-05-07 15:10:16 -0400154static void radeon_sync_with_vblank(struct radeon_device *rdev)
155{
156 if (rdev->pm.active_crtcs) {
157 rdev->pm.vblank_sync = false;
158 wait_event_timeout(
159 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
160 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
161 }
162}
163
164static void radeon_set_power_state(struct radeon_device *rdev)
165{
166 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400167 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400168
169 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
170 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
171 return;
172
173 if (radeon_gui_idle(rdev)) {
174 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
175 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500176 if (sclk > rdev->pm.default_sclk)
177 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400178
Alex Deucher27810fb2012-10-01 19:25:11 -0400179 /* starting with BTC, there is one state that is used for both
180 * MH and SH. Difference is that we always use the high clock index for
Alex Deucher7ae764b2013-02-11 08:44:48 -0500181 * mclk and vddci.
Alex Deucher27810fb2012-10-01 19:25:11 -0400182 */
183 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
184 (rdev->family >= CHIP_BARTS) &&
185 rdev->pm.active_crtc_count &&
186 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
187 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
188 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
189 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
190 else
191 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
192 clock_info[rdev->pm.requested_clock_mode_index].mclk;
193
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 */
Alex Deucher798bcf72012-02-23 17:53:48 -0500224 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400225 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{
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500246 int i, r;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400247
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);
Christian Königdb7fce32012-05-11 14:57:18 +0200254 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200255 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400256
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400257 /* wait for the rings to drain */
258 for (i = 0; i < RADEON_NUM_RINGS; i++) {
259 struct radeon_ring *ring = &rdev->ring[i];
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500260 if (!ring->ready) {
261 continue;
262 }
Christian König37615522014-02-18 15:58:31 +0100263 r = radeon_fence_wait_empty(rdev, i);
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500264 if (r) {
265 /* needs a GPU reset dont reset here */
266 mutex_unlock(&rdev->ring_lock);
267 up_write(&rdev->pm.mclk_lock);
268 mutex_unlock(&rdev->ddev->struct_mutex);
269 return;
270 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400271 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400272
Matthew Garrett5876dd22010-04-26 15:52:20 -0400273 radeon_unmap_vram_bos(rdev);
274
Alex Deucherce8f5372010-05-07 15:10:16 -0400275 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400276 for (i = 0; i < rdev->num_crtc; i++) {
277 if (rdev->pm.active_crtcs & (1 << i)) {
278 rdev->pm.req_vblank |= (1 << i);
279 drm_vblank_get(rdev->ddev, i);
280 }
281 }
282 }
Alex Deucher539d2412010-04-29 00:22:43 -0400283
Alex Deucherce8f5372010-05-07 15:10:16 -0400284 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400285
Alex Deucherce8f5372010-05-07 15:10:16 -0400286 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400287 for (i = 0; i < rdev->num_crtc; i++) {
288 if (rdev->pm.req_vblank & (1 << i)) {
289 rdev->pm.req_vblank &= ~(1 << i);
290 drm_vblank_put(rdev->ddev, i);
291 }
292 }
293 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400294
Alex Deuchera4248162010-04-24 14:50:23 -0400295 /* update display watermarks based on new power state */
296 radeon_update_bandwidth_info(rdev);
297 if (rdev->pm.active_crtc_count)
298 radeon_bandwidth_update(rdev);
299
Alex Deucherce8f5372010-05-07 15:10:16 -0400300 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400301
Christian Königd6999bc2012-05-09 15:34:45 +0200302 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200303 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400304 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400305}
306
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400307static void radeon_pm_print_states(struct radeon_device *rdev)
308{
309 int i, j;
310 struct radeon_power_state *power_state;
311 struct radeon_pm_clock_info *clock_info;
312
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000313 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400314 for (i = 0; i < rdev->pm.num_power_states; i++) {
315 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000316 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400317 radeon_pm_state_type_name[power_state->type]);
318 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000319 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400320 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000321 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400322 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000323 DRM_DEBUG_DRIVER("\tSingle display only\n");
324 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400325 for (j = 0; j < power_state->num_clock_modes; j++) {
326 clock_info = &(power_state->clock_info[j]);
327 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400328 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
329 j,
330 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400331 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400332 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
333 j,
334 clock_info->sclk * 10,
335 clock_info->mclk * 10,
336 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400337 }
338 }
339}
340
Alex Deucherce8f5372010-05-07 15:10:16 -0400341static ssize_t radeon_get_pm_profile(struct device *dev,
342 struct device_attribute *attr,
343 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400344{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200345 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400346 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400347 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400348
Alex Deucherce8f5372010-05-07 15:10:16 -0400349 return snprintf(buf, PAGE_SIZE, "%s\n",
350 (cp == PM_PROFILE_AUTO) ? "auto" :
351 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100352 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400353 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400354}
355
Alex Deucherce8f5372010-05-07 15:10:16 -0400356static ssize_t radeon_set_pm_profile(struct device *dev,
357 struct device_attribute *attr,
358 const char *buf,
359 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400360{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200361 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400362 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400363
364 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400365 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
366 if (strncmp("default", buf, strlen("default")) == 0)
367 rdev->pm.profile = PM_PROFILE_DEFAULT;
368 else if (strncmp("auto", buf, strlen("auto")) == 0)
369 rdev->pm.profile = PM_PROFILE_AUTO;
370 else if (strncmp("low", buf, strlen("low")) == 0)
371 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400372 else if (strncmp("mid", buf, strlen("mid")) == 0)
373 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400374 else if (strncmp("high", buf, strlen("high")) == 0)
375 rdev->pm.profile = PM_PROFILE_HIGH;
376 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000377 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400378 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400379 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400380 radeon_pm_update_profile(rdev);
381 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000382 } else
383 count = -EINVAL;
384
Alex Deucherce8f5372010-05-07 15:10:16 -0400385fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400386 mutex_unlock(&rdev->pm.mutex);
387
388 return count;
389}
390
Alex Deucherce8f5372010-05-07 15:10:16 -0400391static ssize_t radeon_get_pm_method(struct device *dev,
392 struct device_attribute *attr,
393 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400394{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200395 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400396 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400397 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400398
399 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherda321c82013-04-12 13:55:22 -0400400 (pm == PM_METHOD_DYNPM) ? "dynpm" :
401 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
Alex Deuchera4248162010-04-24 14:50:23 -0400402}
403
Alex Deucherce8f5372010-05-07 15:10:16 -0400404static ssize_t radeon_set_pm_method(struct device *dev,
405 struct device_attribute *attr,
406 const char *buf,
407 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400408{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200409 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400410 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400411
Alex Deucherda321c82013-04-12 13:55:22 -0400412 /* we don't support the legacy modes with dpm */
413 if (rdev->pm.pm_method == PM_METHOD_DPM) {
414 count = -EINVAL;
415 goto fail;
416 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400417
418 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400419 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400420 rdev->pm.pm_method = PM_METHOD_DYNPM;
421 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
422 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400423 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400424 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
425 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400426 /* disable dynpm */
427 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
428 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000429 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400430 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100431 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400432 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000433 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400434 goto fail;
435 }
436 radeon_pm_compute_clocks(rdev);
437fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400438 return count;
439}
440
Alex Deucherda321c82013-04-12 13:55:22 -0400441static ssize_t radeon_get_dpm_state(struct device *dev,
442 struct device_attribute *attr,
443 char *buf)
444{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200445 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400446 struct radeon_device *rdev = ddev->dev_private;
447 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
448
449 return snprintf(buf, PAGE_SIZE, "%s\n",
450 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
451 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
452}
453
454static ssize_t radeon_set_dpm_state(struct device *dev,
455 struct device_attribute *attr,
456 const char *buf,
457 size_t count)
458{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200459 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400460 struct radeon_device *rdev = ddev->dev_private;
461
462 mutex_lock(&rdev->pm.mutex);
463 if (strncmp("battery", buf, strlen("battery")) == 0)
464 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
465 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
466 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
467 else if (strncmp("performance", buf, strlen("performance")) == 0)
468 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
469 else {
470 mutex_unlock(&rdev->pm.mutex);
471 count = -EINVAL;
472 goto fail;
473 }
474 mutex_unlock(&rdev->pm.mutex);
475 radeon_pm_compute_clocks(rdev);
476fail:
477 return count;
478}
479
Alex Deucher70d01a52013-07-02 18:38:02 -0400480static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
481 struct device_attribute *attr,
482 char *buf)
483{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200484 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400485 struct radeon_device *rdev = ddev->dev_private;
486 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
487
488 return snprintf(buf, PAGE_SIZE, "%s\n",
489 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
490 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
491}
492
493static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
494 struct device_attribute *attr,
495 const char *buf,
496 size_t count)
497{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200498 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400499 struct radeon_device *rdev = ddev->dev_private;
500 enum radeon_dpm_forced_level level;
501 int ret = 0;
502
503 mutex_lock(&rdev->pm.mutex);
504 if (strncmp("low", buf, strlen("low")) == 0) {
505 level = RADEON_DPM_FORCED_LEVEL_LOW;
506 } else if (strncmp("high", buf, strlen("high")) == 0) {
507 level = RADEON_DPM_FORCED_LEVEL_HIGH;
508 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
509 level = RADEON_DPM_FORCED_LEVEL_AUTO;
510 } else {
Alex Deucher70d01a52013-07-02 18:38:02 -0400511 count = -EINVAL;
512 goto fail;
513 }
514 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher0a17af372013-10-23 17:22:29 -0400515 if (rdev->pm.dpm.thermal_active) {
516 count = -EINVAL;
517 goto fail;
518 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400519 ret = radeon_dpm_force_performance_level(rdev, level);
520 if (ret)
521 count = -EINVAL;
522 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400523fail:
Alex Deucher0a17af372013-10-23 17:22:29 -0400524 mutex_unlock(&rdev->pm.mutex);
525
Alex Deucher70d01a52013-07-02 18:38:02 -0400526 return count;
527}
528
Alex Deucherce8f5372010-05-07 15:10:16 -0400529static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
530static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400531static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400532static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
533 radeon_get_dpm_forced_performance_level,
534 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400535
Alex Deucher21a81222010-07-02 12:58:16 -0400536static ssize_t radeon_hwmon_show_temp(struct device *dev,
537 struct device_attribute *attr,
538 char *buf)
539{
Guenter Roeckec39f642013-11-22 21:52:00 -0800540 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher20d391d2011-02-01 16:12:34 -0500541 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400542
Alex Deucher6bd1c382013-06-21 14:38:03 -0400543 if (rdev->asic->pm.get_temperature)
544 temp = radeon_get_temperature(rdev);
545 else
Alex Deucher21a81222010-07-02 12:58:16 -0400546 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400547
548 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
549}
550
Jean Delvare6ea4e842013-09-10 10:32:41 +0200551static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
552 struct device_attribute *attr,
553 char *buf)
554{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300555 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200556 int hyst = to_sensor_dev_attr(attr)->index;
557 int temp;
558
559 if (hyst)
560 temp = rdev->pm.dpm.thermal.min_temp;
561 else
562 temp = rdev->pm.dpm.thermal.max_temp;
563
564 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
565}
566
Alex Deucher21a81222010-07-02 12:58:16 -0400567static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200568static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
569static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Alex Deucher21a81222010-07-02 12:58:16 -0400570
571static struct attribute *hwmon_attributes[] = {
572 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200573 &sensor_dev_attr_temp1_crit.dev_attr.attr,
574 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400575 NULL
576};
577
Jean Delvare6ea4e842013-09-10 10:32:41 +0200578static umode_t hwmon_attributes_visible(struct kobject *kobj,
579 struct attribute *attr, int index)
580{
581 struct device *dev = container_of(kobj, struct device, kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300582 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200583
584 /* Skip limit attributes if DPM is not enabled */
585 if (rdev->pm.pm_method != PM_METHOD_DPM &&
586 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
587 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
588 return 0;
589
590 return attr->mode;
591}
592
Alex Deucher21a81222010-07-02 12:58:16 -0400593static const struct attribute_group hwmon_attrgroup = {
594 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200595 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400596};
597
Guenter Roeckec39f642013-11-22 21:52:00 -0800598static const struct attribute_group *hwmon_groups[] = {
599 &hwmon_attrgroup,
600 NULL
601};
602
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200603static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400604{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200605 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400606
607 switch (rdev->pm.int_thermal_type) {
608 case THERMAL_TYPE_RV6XX:
609 case THERMAL_TYPE_RV770:
610 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400611 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500612 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400613 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400614 case THERMAL_TYPE_CI:
615 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400616 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400617 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400618 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
619 "radeon", rdev,
620 hwmon_groups);
621 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
622 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200623 dev_err(rdev->dev,
624 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200625 }
Alex Deucher21a81222010-07-02 12:58:16 -0400626 break;
627 default:
628 break;
629 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200630
631 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400632}
633
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400634static void radeon_hwmon_fini(struct radeon_device *rdev)
635{
636 if (rdev->pm.int_hwmon_dev)
637 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
638}
639
Alex Deucherda321c82013-04-12 13:55:22 -0400640static void radeon_dpm_thermal_work_handler(struct work_struct *work)
641{
642 struct radeon_device *rdev =
643 container_of(work, struct radeon_device,
644 pm.dpm.thermal.work);
645 /* switch to the thermal state */
646 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
647
648 if (!rdev->pm.dpm_enabled)
649 return;
650
651 if (rdev->asic->pm.get_temperature) {
652 int temp = radeon_get_temperature(rdev);
653
654 if (temp < rdev->pm.dpm.thermal.min_temp)
655 /* switch back the user state */
656 dpm_state = rdev->pm.dpm.user_state;
657 } else {
658 if (rdev->pm.dpm.thermal.high_to_low)
659 /* switch back the user state */
660 dpm_state = rdev->pm.dpm.user_state;
661 }
Alex Deucher60320342013-07-24 14:59:48 -0400662 mutex_lock(&rdev->pm.mutex);
663 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
664 rdev->pm.dpm.thermal_active = true;
665 else
666 rdev->pm.dpm.thermal_active = false;
667 rdev->pm.dpm.state = dpm_state;
668 mutex_unlock(&rdev->pm.mutex);
669
670 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400671}
672
673static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
674 enum radeon_pm_state_type dpm_state)
675{
676 int i;
677 struct radeon_ps *ps;
678 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400679 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
680 true : false;
681
682 /* check if the vblank period is too short to adjust the mclk */
683 if (single_display && rdev->asic->dpm.vblank_too_short) {
684 if (radeon_dpm_vblank_too_short(rdev))
685 single_display = false;
686 }
Alex Deucherda321c82013-04-12 13:55:22 -0400687
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400688 /* certain older asics have a separare 3D performance state,
689 * so try that first if the user selected performance
690 */
691 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
692 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400693 /* balanced states don't exist at the moment */
694 if (dpm_state == POWER_STATE_TYPE_BALANCED)
695 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
696
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400697restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400698 /* Pick the best power state based on current conditions */
699 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
700 ps = &rdev->pm.dpm.ps[i];
701 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
702 switch (dpm_state) {
703 /* user states */
704 case POWER_STATE_TYPE_BATTERY:
705 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
706 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400707 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400708 return ps;
709 } else
710 return ps;
711 }
712 break;
713 case POWER_STATE_TYPE_BALANCED:
714 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
715 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400716 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400717 return ps;
718 } else
719 return ps;
720 }
721 break;
722 case POWER_STATE_TYPE_PERFORMANCE:
723 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
724 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400725 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400726 return ps;
727 } else
728 return ps;
729 }
730 break;
731 /* internal states */
732 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400733 if (rdev->pm.dpm.uvd_ps)
734 return rdev->pm.dpm.uvd_ps;
735 else
736 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400737 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
738 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
739 return ps;
740 break;
741 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
742 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
743 return ps;
744 break;
745 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
746 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
747 return ps;
748 break;
749 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
750 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
751 return ps;
752 break;
753 case POWER_STATE_TYPE_INTERNAL_BOOT:
754 return rdev->pm.dpm.boot_ps;
755 case POWER_STATE_TYPE_INTERNAL_THERMAL:
756 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
757 return ps;
758 break;
759 case POWER_STATE_TYPE_INTERNAL_ACPI:
760 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
761 return ps;
762 break;
763 case POWER_STATE_TYPE_INTERNAL_ULV:
764 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
765 return ps;
766 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400767 case POWER_STATE_TYPE_INTERNAL_3DPERF:
768 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
769 return ps;
770 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400771 default:
772 break;
773 }
774 }
775 /* use a fallback state if we didn't match */
776 switch (dpm_state) {
777 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400778 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
779 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400780 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
781 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
782 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400783 if (rdev->pm.dpm.uvd_ps) {
784 return rdev->pm.dpm.uvd_ps;
785 } else {
786 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
787 goto restart_search;
788 }
Alex Deucherda321c82013-04-12 13:55:22 -0400789 case POWER_STATE_TYPE_INTERNAL_THERMAL:
790 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
791 goto restart_search;
792 case POWER_STATE_TYPE_INTERNAL_ACPI:
793 dpm_state = POWER_STATE_TYPE_BATTERY;
794 goto restart_search;
795 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400796 case POWER_STATE_TYPE_BALANCED:
797 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400798 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
799 goto restart_search;
800 default:
801 break;
802 }
803
804 return NULL;
805}
806
807static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
808{
809 int i;
810 struct radeon_ps *ps;
811 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500812 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400813
814 /* if dpm init failed */
815 if (!rdev->pm.dpm_enabled)
816 return;
817
818 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
819 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400820 if ((!rdev->pm.dpm.thermal_active) &&
821 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400822 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
823 }
824 dpm_state = rdev->pm.dpm.state;
825
826 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
827 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -0500828 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -0400829 else
830 return;
831
Alex Deucherd22b7e42012-11-29 19:27:56 -0500832 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -0400833 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -0400834 /* vce just modifies an existing state so force a change */
835 if (ps->vce_active != rdev->pm.dpm.vce_active)
836 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -0500837 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
838 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
839 * all we need to do is update the display configuration.
840 */
841 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
842 /* update display watermarks based on new power state */
843 radeon_bandwidth_update(rdev);
844 /* update displays */
845 radeon_dpm_display_configuration_changed(rdev);
846 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
847 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
848 }
849 return;
850 } else {
851 /* for BTC+ if the num crtcs hasn't changed and state is the same,
852 * nothing to do, if the num crtcs is > 1 and state is the same,
853 * update display configuration.
854 */
855 if (rdev->pm.dpm.new_active_crtcs ==
856 rdev->pm.dpm.current_active_crtcs) {
857 return;
858 } else {
859 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
860 (rdev->pm.dpm.new_active_crtc_count > 1)) {
861 /* update display watermarks based on new power state */
862 radeon_bandwidth_update(rdev);
863 /* update displays */
864 radeon_dpm_display_configuration_changed(rdev);
865 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
866 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
867 return;
868 }
869 }
Alex Deucherda321c82013-04-12 13:55:22 -0400870 }
Alex Deucherda321c82013-04-12 13:55:22 -0400871 }
872
Alex Deucherb62d6282013-08-20 20:29:05 -0400873force:
Alex Deucher033a37d2013-10-23 18:35:43 -0400874 if (radeon_dpm == 1) {
875 printk("switching from power state:\n");
876 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
877 printk("switching to power state:\n");
878 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
879 }
Alex Deucherb62d6282013-08-20 20:29:05 -0400880
Alex Deucherda321c82013-04-12 13:55:22 -0400881 mutex_lock(&rdev->ddev->struct_mutex);
882 down_write(&rdev->pm.mclk_lock);
883 mutex_lock(&rdev->ring_lock);
884
Alex Deucherb62d6282013-08-20 20:29:05 -0400885 /* update whether vce is active */
886 ps->vce_active = rdev->pm.dpm.vce_active;
887
Alex Deucher89c9bc52013-01-16 14:40:26 -0500888 ret = radeon_dpm_pre_set_power_state(rdev);
889 if (ret)
890 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -0500891
Alex Deucherda321c82013-04-12 13:55:22 -0400892 /* update display watermarks based on new power state */
893 radeon_bandwidth_update(rdev);
894 /* update displays */
895 radeon_dpm_display_configuration_changed(rdev);
896
897 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
898 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
899
900 /* wait for the rings to drain */
901 for (i = 0; i < RADEON_NUM_RINGS; i++) {
902 struct radeon_ring *ring = &rdev->ring[i];
903 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +0100904 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -0400905 }
906
907 /* program the new power state */
908 radeon_dpm_set_power_state(rdev);
909
910 /* update current power state */
911 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
912
Alex Deucher89c9bc52013-01-16 14:40:26 -0500913 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -0500914
Alex Deucher1cd8b212013-09-13 14:07:03 -0400915 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -0400916 if (rdev->pm.dpm.thermal_active) {
917 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -0400918 /* force low perf level for thermal */
919 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -0400920 /* save the user's level */
921 rdev->pm.dpm.forced_level = level;
922 } else {
923 /* otherwise, user selected level */
924 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
925 }
Alex Deucher60320342013-07-24 14:59:48 -0400926 }
927
Alex Deucher84dd1922013-01-16 12:52:04 -0500928done:
Alex Deucherda321c82013-04-12 13:55:22 -0400929 mutex_unlock(&rdev->ring_lock);
930 up_write(&rdev->pm.mclk_lock);
931 mutex_unlock(&rdev->ddev->struct_mutex);
932}
933
Alex Deucherce3537d2013-07-24 12:12:49 -0400934void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
935{
936 enum radeon_pm_state_type dpm_state;
937
Alex Deucher9e9d9762013-07-31 18:13:23 -0400938 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -0400939 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +0100940 /* don't powergate anything if we
941 have active but pause streams */
942 enable |= rdev->pm.dpm.sd > 0;
943 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -0400944 /* enable/disable UVD */
945 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -0400946 mutex_unlock(&rdev->pm.mutex);
947 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -0400948 if (enable) {
949 mutex_lock(&rdev->pm.mutex);
950 rdev->pm.dpm.uvd_active = true;
951 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
952 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
953 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
954 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
955 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
956 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
957 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
958 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
959 else
960 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
961 rdev->pm.dpm.state = dpm_state;
962 mutex_unlock(&rdev->pm.mutex);
963 } else {
964 mutex_lock(&rdev->pm.mutex);
965 rdev->pm.dpm.uvd_active = false;
966 mutex_unlock(&rdev->pm.mutex);
967 }
Alex Deucherce3537d2013-07-24 12:12:49 -0400968
Alex Deucher9e9d9762013-07-31 18:13:23 -0400969 radeon_pm_compute_clocks(rdev);
970 }
Alex Deucherce3537d2013-07-24 12:12:49 -0400971}
972
Alex Deucher03afe6f2013-08-23 11:56:26 -0400973void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
974{
975 if (enable) {
976 mutex_lock(&rdev->pm.mutex);
977 rdev->pm.dpm.vce_active = true;
978 /* XXX select vce level based on ring/task */
979 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
980 mutex_unlock(&rdev->pm.mutex);
981 } else {
982 mutex_lock(&rdev->pm.mutex);
983 rdev->pm.dpm.vce_active = false;
984 mutex_unlock(&rdev->pm.mutex);
985 }
986
987 radeon_pm_compute_clocks(rdev);
988}
989
Alex Deucherda321c82013-04-12 13:55:22 -0400990static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500991{
Alex Deucherce8f5372010-05-07 15:10:16 -0400992 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000993 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000994 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
995 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000996 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400997 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100998
999 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001000}
1001
Alex Deucherda321c82013-04-12 13:55:22 -04001002static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1003{
1004 mutex_lock(&rdev->pm.mutex);
1005 /* disable dpm */
1006 radeon_dpm_disable(rdev);
1007 /* reset the power state */
1008 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1009 rdev->pm.dpm_enabled = false;
1010 mutex_unlock(&rdev->pm.mutex);
1011}
1012
1013void radeon_pm_suspend(struct radeon_device *rdev)
1014{
1015 if (rdev->pm.pm_method == PM_METHOD_DPM)
1016 radeon_pm_suspend_dpm(rdev);
1017 else
1018 radeon_pm_suspend_old(rdev);
1019}
1020
1021static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001022{
Alex Deuchered18a362011-01-06 21:19:32 -05001023 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001024 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001025 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001026 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001027 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001028 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1029 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001030 if (rdev->pm.default_vddci)
1031 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1032 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001033 if (rdev->pm.default_sclk)
1034 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1035 if (rdev->pm.default_mclk)
1036 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1037 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001038 /* asic init will reset the default power state */
1039 mutex_lock(&rdev->pm.mutex);
1040 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1041 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001042 rdev->pm.current_sclk = rdev->pm.default_sclk;
1043 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001044 if (rdev->pm.power_state) {
1045 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1046 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1047 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001048 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1049 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1050 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001051 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1052 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001053 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001054 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001055 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001056}
1057
Alex Deucherda321c82013-04-12 13:55:22 -04001058static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001059{
Dave Airlie26481fb2010-05-18 19:00:14 +10001060 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001061
Alex Deucherda321c82013-04-12 13:55:22 -04001062 /* asic init will reset to the boot state */
1063 mutex_lock(&rdev->pm.mutex);
1064 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1065 radeon_dpm_setup_asic(rdev);
1066 ret = radeon_dpm_enable(rdev);
1067 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001068 if (ret)
1069 goto dpm_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001070 rdev->pm.dpm_enabled = true;
1071 radeon_pm_compute_clocks(rdev);
1072 return;
1073
1074dpm_resume_fail:
1075 DRM_ERROR("radeon: dpm resume failed\n");
1076 if ((rdev->family >= CHIP_BARTS) &&
1077 (rdev->family <= CHIP_CAYMAN) &&
1078 rdev->mc_fw) {
1079 if (rdev->pm.default_vddc)
1080 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1081 SET_VOLTAGE_TYPE_ASIC_VDDC);
1082 if (rdev->pm.default_vddci)
1083 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1084 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1085 if (rdev->pm.default_sclk)
1086 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1087 if (rdev->pm.default_mclk)
1088 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001089 }
1090}
1091
1092void radeon_pm_resume(struct radeon_device *rdev)
1093{
1094 if (rdev->pm.pm_method == PM_METHOD_DPM)
1095 radeon_pm_resume_dpm(rdev);
1096 else
1097 radeon_pm_resume_old(rdev);
1098}
1099
1100static int radeon_pm_init_old(struct radeon_device *rdev)
1101{
1102 int ret;
1103
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001104 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001105 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1106 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1107 rdev->pm.dynpm_can_upclock = true;
1108 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001109 rdev->pm.default_sclk = rdev->clock.default_sclk;
1110 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001111 rdev->pm.current_sclk = rdev->clock.default_sclk;
1112 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001113 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001114
Alex Deucher56278a82009-12-28 13:58:44 -05001115 if (rdev->bios) {
1116 if (rdev->is_atom_bios)
1117 radeon_atombios_get_power_modes(rdev);
1118 else
1119 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001120 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001121 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001122 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001123 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001124 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001125 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001126 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001127 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1128 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001129 if (rdev->pm.default_vddci)
1130 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1131 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001132 if (rdev->pm.default_sclk)
1133 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1134 if (rdev->pm.default_mclk)
1135 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1136 }
Alex Deucher56278a82009-12-28 13:58:44 -05001137 }
1138
Alex Deucher21a81222010-07-02 12:58:16 -04001139 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001140 ret = radeon_hwmon_init(rdev);
1141 if (ret)
1142 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001143
1144 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1145
Alex Deucherce8f5372010-05-07 15:10:16 -04001146 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001147 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001148 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1149 if (ret)
1150 DRM_ERROR("failed to create device file for power profile\n");
1151 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1152 if (ret)
1153 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001154
Alex Deucherce8f5372010-05-07 15:10:16 -04001155 if (radeon_debugfs_pm_init(rdev)) {
1156 DRM_ERROR("Failed to register debugfs file for PM!\n");
1157 }
1158
1159 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001160 }
1161
1162 return 0;
1163}
1164
Alex Deucherda321c82013-04-12 13:55:22 -04001165static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1166{
1167 int i;
1168
1169 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1170 printk("== power state %d ==\n", i);
1171 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1172 }
1173}
1174
1175static int radeon_pm_init_dpm(struct radeon_device *rdev)
1176{
1177 int ret;
1178
Alex Deucher1cd8b212013-09-13 14:07:03 -04001179 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001180 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1181 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001182 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001183 rdev->pm.default_sclk = rdev->clock.default_sclk;
1184 rdev->pm.default_mclk = rdev->clock.default_mclk;
1185 rdev->pm.current_sclk = rdev->clock.default_sclk;
1186 rdev->pm.current_mclk = rdev->clock.default_mclk;
1187 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1188
1189 if (rdev->bios && rdev->is_atom_bios)
1190 radeon_atombios_get_power_modes(rdev);
1191 else
1192 return -EINVAL;
1193
1194 /* set up the internal thermal sensor if applicable */
1195 ret = radeon_hwmon_init(rdev);
1196 if (ret)
1197 return ret;
1198
1199 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1200 mutex_lock(&rdev->pm.mutex);
1201 radeon_dpm_init(rdev);
1202 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001203 if (radeon_dpm == 1)
1204 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001205 radeon_dpm_setup_asic(rdev);
1206 ret = radeon_dpm_enable(rdev);
1207 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001208 if (ret)
1209 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001210 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001211
Alex Deucherbb5abf92013-12-18 13:39:58 -05001212 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1213 if (ret)
1214 DRM_ERROR("failed to create device file for dpm state\n");
1215 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1216 if (ret)
1217 DRM_ERROR("failed to create device file for dpm state\n");
1218 /* XXX: these are noops for dpm but are here for backwards compat */
1219 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1220 if (ret)
1221 DRM_ERROR("failed to create device file for power profile\n");
1222 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1223 if (ret)
1224 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001225
Alex Deucherbb5abf92013-12-18 13:39:58 -05001226 if (radeon_debugfs_pm_init(rdev)) {
1227 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001228 }
1229
Alex Deucherbb5abf92013-12-18 13:39:58 -05001230 DRM_INFO("radeon: dpm initialized\n");
1231
Alex Deucherda321c82013-04-12 13:55:22 -04001232 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001233
1234dpm_failed:
1235 rdev->pm.dpm_enabled = false;
1236 if ((rdev->family >= CHIP_BARTS) &&
1237 (rdev->family <= CHIP_CAYMAN) &&
1238 rdev->mc_fw) {
1239 if (rdev->pm.default_vddc)
1240 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1241 SET_VOLTAGE_TYPE_ASIC_VDDC);
1242 if (rdev->pm.default_vddci)
1243 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1244 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1245 if (rdev->pm.default_sclk)
1246 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1247 if (rdev->pm.default_mclk)
1248 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1249 }
1250 DRM_ERROR("radeon: dpm initialization failed\n");
1251 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001252}
1253
1254int radeon_pm_init(struct radeon_device *rdev)
1255{
1256 /* enable dpm on rv6xx+ */
1257 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001258 case CHIP_RV610:
1259 case CHIP_RV630:
1260 case CHIP_RV620:
1261 case CHIP_RV635:
1262 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001263 case CHIP_RS780:
1264 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001265 case CHIP_RV770:
Alex Deucher919cf552014-01-11 10:55:55 -05001266 case CHIP_BARTS:
1267 case CHIP_TURKS:
1268 case CHIP_CAICOS:
Alex Deucher69e0b572013-04-12 16:42:42 -04001269 case CHIP_CAYMAN:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001270 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001271 if (!rdev->rlc_fw)
1272 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001273 else if ((rdev->family >= CHIP_RV770) &&
1274 (!(rdev->flags & RADEON_IS_IGP)) &&
1275 (!rdev->smc_fw))
1276 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001277 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001278 rdev->pm.pm_method = PM_METHOD_DPM;
1279 else
1280 rdev->pm.pm_method = PM_METHOD_PROFILE;
1281 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001282 case CHIP_RV730:
1283 case CHIP_RV710:
1284 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001285 case CHIP_CEDAR:
1286 case CHIP_REDWOOD:
1287 case CHIP_JUNIPER:
1288 case CHIP_CYPRESS:
1289 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001290 case CHIP_PALM:
1291 case CHIP_SUMO:
1292 case CHIP_SUMO2:
Alex Deucher3a118982013-11-14 10:21:29 -05001293 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001294 case CHIP_TAHITI:
1295 case CHIP_PITCAIRN:
1296 case CHIP_VERDE:
1297 case CHIP_OLAND:
1298 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001299 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001300 case CHIP_KABINI:
1301 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001302 case CHIP_HAWAII:
Alex Deucher5a16f762013-10-23 17:11:06 -04001303 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1304 if (!rdev->rlc_fw)
1305 rdev->pm.pm_method = PM_METHOD_PROFILE;
1306 else if ((rdev->family >= CHIP_RV770) &&
1307 (!(rdev->flags & RADEON_IS_IGP)) &&
1308 (!rdev->smc_fw))
1309 rdev->pm.pm_method = PM_METHOD_PROFILE;
1310 else if (radeon_dpm == 0)
1311 rdev->pm.pm_method = PM_METHOD_PROFILE;
1312 else
1313 rdev->pm.pm_method = PM_METHOD_DPM;
1314 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001315 default:
1316 /* default to profile method */
1317 rdev->pm.pm_method = PM_METHOD_PROFILE;
1318 break;
1319 }
1320
1321 if (rdev->pm.pm_method == PM_METHOD_DPM)
1322 return radeon_pm_init_dpm(rdev);
1323 else
1324 return radeon_pm_init_old(rdev);
1325}
1326
Alex Deucher914a8982013-12-19 11:37:22 -05001327int radeon_pm_late_init(struct radeon_device *rdev)
1328{
1329 int ret = 0;
1330
1331 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1332 mutex_lock(&rdev->pm.mutex);
1333 ret = radeon_dpm_late_enable(rdev);
1334 mutex_unlock(&rdev->pm.mutex);
1335 }
1336 return ret;
1337}
1338
Alex Deucherda321c82013-04-12 13:55:22 -04001339static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001340{
Alex Deucherce8f5372010-05-07 15:10:16 -04001341 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001342 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001343 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1344 rdev->pm.profile = PM_PROFILE_DEFAULT;
1345 radeon_pm_update_profile(rdev);
1346 radeon_pm_set_clocks(rdev);
1347 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001348 /* reset default clocks */
1349 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1350 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1351 radeon_pm_set_clocks(rdev);
1352 }
Alex Deuchera4248162010-04-24 14:50:23 -04001353 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001354
1355 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001356
Alex Deucherce8f5372010-05-07 15:10:16 -04001357 device_remove_file(rdev->dev, &dev_attr_power_profile);
1358 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001359 }
Alex Deuchera4248162010-04-24 14:50:23 -04001360
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001361 radeon_hwmon_fini(rdev);
1362
Alex Deucher0975b162011-02-02 18:42:03 -05001363 if (rdev->pm.power_state)
1364 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001365}
1366
Alex Deucherda321c82013-04-12 13:55:22 -04001367static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1368{
1369 if (rdev->pm.num_power_states > 1) {
1370 mutex_lock(&rdev->pm.mutex);
1371 radeon_dpm_disable(rdev);
1372 mutex_unlock(&rdev->pm.mutex);
1373
1374 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001375 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001376 /* XXX backwards compat */
1377 device_remove_file(rdev->dev, &dev_attr_power_profile);
1378 device_remove_file(rdev->dev, &dev_attr_power_method);
1379 }
1380 radeon_dpm_fini(rdev);
1381
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001382 radeon_hwmon_fini(rdev);
1383
Alex Deucherda321c82013-04-12 13:55:22 -04001384 if (rdev->pm.power_state)
1385 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001386}
1387
1388void radeon_pm_fini(struct radeon_device *rdev)
1389{
1390 if (rdev->pm.pm_method == PM_METHOD_DPM)
1391 radeon_pm_fini_dpm(rdev);
1392 else
1393 radeon_pm_fini_old(rdev);
1394}
1395
1396static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001397{
1398 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001399 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001400 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001401
Alex Deucherce8f5372010-05-07 15:10:16 -04001402 if (rdev->pm.num_power_states < 2)
1403 return;
1404
Rafał Miłeckic913e232009-12-22 23:02:16 +01001405 mutex_lock(&rdev->pm.mutex);
1406
1407 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001408 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001409 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1410 list_for_each_entry(crtc,
1411 &ddev->mode_config.crtc_list, head) {
1412 radeon_crtc = to_radeon_crtc(crtc);
1413 if (radeon_crtc->enabled) {
1414 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1415 rdev->pm.active_crtc_count++;
1416 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001417 }
1418 }
1419
Alex Deucherce8f5372010-05-07 15:10:16 -04001420 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1421 radeon_pm_update_profile(rdev);
1422 radeon_pm_set_clocks(rdev);
1423 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1424 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1425 if (rdev->pm.active_crtc_count > 1) {
1426 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1427 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001428
Alex Deucherce8f5372010-05-07 15:10:16 -04001429 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1430 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1431 radeon_pm_get_dynpm_state(rdev);
1432 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001433
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001434 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001435 }
1436 } else if (rdev->pm.active_crtc_count == 1) {
1437 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001438
Alex Deucherce8f5372010-05-07 15:10:16 -04001439 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1440 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1441 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1442 radeon_pm_get_dynpm_state(rdev);
1443 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001444
Tejun Heo32c87fc2011-01-03 14:49:32 +01001445 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1446 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001447 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1448 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001449 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1450 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001451 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001452 }
1453 } else { /* count == 0 */
1454 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1455 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001456
Alex Deucherce8f5372010-05-07 15:10:16 -04001457 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1458 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1459 radeon_pm_get_dynpm_state(rdev);
1460 radeon_pm_set_clocks(rdev);
1461 }
1462 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001463 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001464 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001465
1466 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001467}
1468
Alex Deucherda321c82013-04-12 13:55:22 -04001469static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1470{
1471 struct drm_device *ddev = rdev->ddev;
1472 struct drm_crtc *crtc;
1473 struct radeon_crtc *radeon_crtc;
1474
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001475 if (!rdev->pm.dpm_enabled)
1476 return;
1477
Alex Deucherda321c82013-04-12 13:55:22 -04001478 mutex_lock(&rdev->pm.mutex);
1479
Alex Deucher5ca302f2012-11-30 10:56:57 -05001480 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001481 rdev->pm.dpm.new_active_crtcs = 0;
1482 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001483 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1484 list_for_each_entry(crtc,
1485 &ddev->mode_config.crtc_list, head) {
1486 radeon_crtc = to_radeon_crtc(crtc);
1487 if (crtc->enabled) {
1488 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1489 rdev->pm.dpm.new_active_crtc_count++;
1490 }
Alex Deucherda321c82013-04-12 13:55:22 -04001491 }
1492 }
1493
Alex Deucher5ca302f2012-11-30 10:56:57 -05001494 /* update battery/ac status */
1495 if (power_supply_is_system_supplied() > 0)
1496 rdev->pm.dpm.ac_power = true;
1497 else
1498 rdev->pm.dpm.ac_power = false;
1499
Alex Deucherda321c82013-04-12 13:55:22 -04001500 radeon_dpm_change_power_state_locked(rdev);
1501
1502 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001503
Alex Deucherda321c82013-04-12 13:55:22 -04001504}
1505
1506void radeon_pm_compute_clocks(struct radeon_device *rdev)
1507{
1508 if (rdev->pm.pm_method == PM_METHOD_DPM)
1509 radeon_pm_compute_clocks_dpm(rdev);
1510 else
1511 radeon_pm_compute_clocks_old(rdev);
1512}
1513
Alex Deucherce8f5372010-05-07 15:10:16 -04001514static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001515{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001516 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001517 bool in_vbl = true;
1518
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001519 /* Iterate over all active crtc's. All crtc's must be in vblank,
1520 * otherwise return in_vbl == false.
1521 */
1522 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1523 if (rdev->pm.active_crtcs & (1 << crtc)) {
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001524 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001525 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1526 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +10001527 in_vbl = false;
1528 }
1529 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001530
1531 return in_vbl;
1532}
1533
Alex Deucherce8f5372010-05-07 15:10:16 -04001534static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001535{
1536 u32 stat_crtc = 0;
1537 bool in_vbl = radeon_pm_in_vbl(rdev);
1538
Dave Airlief7352612010-02-18 15:58:36 +10001539 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001540 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001541 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001542 return in_vbl;
1543}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001544
Alex Deucherce8f5372010-05-07 15:10:16 -04001545static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001546{
1547 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001548 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001549 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001550 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001551
Matthew Garrettd9932a32010-04-26 16:02:26 -04001552 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001553 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001554 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001555 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001556 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001557
Alex Deucher74652802011-08-25 13:39:48 -04001558 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001559 struct radeon_ring *ring = &rdev->ring[i];
1560
1561 if (ring->ready) {
1562 not_processed += radeon_fence_count_emitted(rdev, i);
1563 if (not_processed >= 3)
1564 break;
1565 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001566 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001567
1568 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001569 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1570 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1571 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1572 rdev->pm.dynpm_can_upclock) {
1573 rdev->pm.dynpm_planned_action =
1574 DYNPM_ACTION_UPCLOCK;
1575 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001576 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1577 }
1578 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001579 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1580 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1581 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1582 rdev->pm.dynpm_can_downclock) {
1583 rdev->pm.dynpm_planned_action =
1584 DYNPM_ACTION_DOWNCLOCK;
1585 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001586 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1587 }
1588 }
1589
Alex Deucherd7311172010-05-03 01:13:14 -04001590 /* Note, radeon_pm_set_clocks is called with static_switch set
1591 * to false since we want to wait for vbl to avoid flicker.
1592 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001593 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1594 jiffies > rdev->pm.dynpm_action_timeout) {
1595 radeon_pm_get_dynpm_state(rdev);
1596 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001597 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001598
Tejun Heo32c87fc2011-01-03 14:49:32 +01001599 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1600 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001601 }
1602 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001603 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001604}
1605
Rafał Miłecki74338742009-11-03 00:53:02 +01001606/*
1607 * Debugfs info
1608 */
1609#if defined(CONFIG_DEBUG_FS)
1610
1611static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1612{
1613 struct drm_info_node *node = (struct drm_info_node *) m->private;
1614 struct drm_device *dev = node->minor->dev;
1615 struct radeon_device *rdev = dev->dev_private;
1616
Alex Deucher1316b792013-06-28 09:28:39 -04001617 if (rdev->pm.dpm_enabled) {
1618 mutex_lock(&rdev->pm.mutex);
1619 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1620 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1621 else
Alex Deucher71375922013-07-02 09:11:39 -04001622 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001623 mutex_unlock(&rdev->pm.mutex);
1624 } else {
1625 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1626 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1627 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1628 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1629 else
1630 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1631 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1632 if (rdev->asic->pm.get_memory_clock)
1633 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1634 if (rdev->pm.current_vddc)
1635 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1636 if (rdev->asic->pm.get_pcie_lanes)
1637 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1638 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001639
1640 return 0;
1641}
1642
1643static struct drm_info_list radeon_pm_info_list[] = {
1644 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1645};
1646#endif
1647
Rafał Miłeckic913e232009-12-22 23:02:16 +01001648static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001649{
1650#if defined(CONFIG_DEBUG_FS)
1651 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1652#else
1653 return 0;
1654#endif
1655}