blob: a06afee460bed81b582ebe4f3fbd7769d7cde14b [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;
Alex Deucher96682952014-06-18 14:23:46 -040076 if (rdev->family == CHIP_ARUBA) {
77 if (rdev->asic->dpm.enable_bapm)
78 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
79 }
Alex Deucher1c71bda2013-09-09 19:11:52 -040080 mutex_unlock(&rdev->pm.mutex);
81 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
Alex Deucherc4917072012-07-31 17:14:35 -040082 if (rdev->pm.profile == PM_PROFILE_AUTO) {
83 mutex_lock(&rdev->pm.mutex);
84 radeon_pm_update_profile(rdev);
85 radeon_pm_set_clocks(rdev);
86 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040087 }
88 }
Alex Deucherce8f5372010-05-07 15:10:16 -040089}
Alex Deucherce8f5372010-05-07 15:10:16 -040090
91static void radeon_pm_update_profile(struct radeon_device *rdev)
92{
93 switch (rdev->pm.profile) {
94 case PM_PROFILE_DEFAULT:
95 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
96 break;
97 case PM_PROFILE_AUTO:
98 if (power_supply_is_system_supplied() > 0) {
99 if (rdev->pm.active_crtc_count > 1)
100 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
101 else
102 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
103 } else {
104 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400105 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400106 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400107 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400108 }
109 break;
110 case PM_PROFILE_LOW:
111 if (rdev->pm.active_crtc_count > 1)
112 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
113 else
114 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
115 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400116 case PM_PROFILE_MID:
117 if (rdev->pm.active_crtc_count > 1)
118 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
119 else
120 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
121 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400122 case PM_PROFILE_HIGH:
123 if (rdev->pm.active_crtc_count > 1)
124 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
125 else
126 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
127 break;
128 }
129
130 if (rdev->pm.active_crtc_count == 0) {
131 rdev->pm.requested_power_state_index =
132 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
133 rdev->pm.requested_clock_mode_index =
134 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
135 } else {
136 rdev->pm.requested_power_state_index =
137 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
138 rdev->pm.requested_clock_mode_index =
139 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
140 }
141}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100142
Matthew Garrett5876dd22010-04-26 15:52:20 -0400143static void radeon_unmap_vram_bos(struct radeon_device *rdev)
144{
145 struct radeon_bo *bo, *n;
146
147 if (list_empty(&rdev->gem.objects))
148 return;
149
150 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
151 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
152 ttm_bo_unmap_virtual(&bo->tbo);
153 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400154}
155
Alex Deucherce8f5372010-05-07 15:10:16 -0400156static void radeon_sync_with_vblank(struct radeon_device *rdev)
157{
158 if (rdev->pm.active_crtcs) {
159 rdev->pm.vblank_sync = false;
160 wait_event_timeout(
161 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
162 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
163 }
164}
165
166static void radeon_set_power_state(struct radeon_device *rdev)
167{
168 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400169 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400170
171 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
172 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
173 return;
174
175 if (radeon_gui_idle(rdev)) {
176 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
177 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500178 if (sclk > rdev->pm.default_sclk)
179 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400180
Alex Deucher27810fb2012-10-01 19:25:11 -0400181 /* starting with BTC, there is one state that is used for both
182 * MH and SH. Difference is that we always use the high clock index for
Alex Deucher7ae764b2013-02-11 08:44:48 -0500183 * mclk and vddci.
Alex Deucher27810fb2012-10-01 19:25:11 -0400184 */
185 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
186 (rdev->family >= CHIP_BARTS) &&
187 rdev->pm.active_crtc_count &&
188 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
189 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
190 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
191 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
192 else
193 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
194 clock_info[rdev->pm.requested_clock_mode_index].mclk;
195
Alex Deucher9ace9f72011-01-06 21:19:26 -0500196 if (mclk > rdev->pm.default_mclk)
197 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400198
Alex Deucher92645872010-05-27 17:01:41 -0400199 /* upvolt before raising clocks, downvolt after lowering clocks */
200 if (sclk < rdev->pm.current_sclk)
201 misc_after = true;
202
203 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400204
205 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400206 if (!radeon_pm_in_vbl(rdev))
207 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400208 }
209
Alex Deucher92645872010-05-27 17:01:41 -0400210 radeon_pm_prepare(rdev);
211
212 if (!misc_after)
213 /* voltage, pcie lanes, etc.*/
214 radeon_pm_misc(rdev);
215
216 /* set engine clock */
217 if (sclk != rdev->pm.current_sclk) {
218 radeon_pm_debug_check_in_vbl(rdev, false);
219 radeon_set_engine_clock(rdev, sclk);
220 radeon_pm_debug_check_in_vbl(rdev, true);
221 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000222 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400223 }
224
225 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500226 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400227 radeon_pm_debug_check_in_vbl(rdev, false);
228 radeon_set_memory_clock(rdev, mclk);
229 radeon_pm_debug_check_in_vbl(rdev, true);
230 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000231 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400232 }
233
234 if (misc_after)
235 /* voltage, pcie lanes, etc.*/
236 radeon_pm_misc(rdev);
237
238 radeon_pm_finish(rdev);
239
Alex Deucherce8f5372010-05-07 15:10:16 -0400240 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
241 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
242 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000243 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400244}
245
246static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400247{
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500248 int i, r;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400249
Alex Deucher4e186b22010-08-13 10:53:35 -0400250 /* no need to take locks, etc. if nothing's going to change */
251 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
252 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
253 return;
254
Matthew Garrett612e06c2010-04-27 17:16:58 -0400255 mutex_lock(&rdev->ddev->struct_mutex);
Christian Königdb7fce32012-05-11 14:57:18 +0200256 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200257 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400258
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400259 /* wait for the rings to drain */
260 for (i = 0; i < RADEON_NUM_RINGS; i++) {
261 struct radeon_ring *ring = &rdev->ring[i];
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500262 if (!ring->ready) {
263 continue;
264 }
Christian König37615522014-02-18 15:58:31 +0100265 r = radeon_fence_wait_empty(rdev, i);
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500266 if (r) {
267 /* needs a GPU reset dont reset here */
268 mutex_unlock(&rdev->ring_lock);
269 up_write(&rdev->pm.mclk_lock);
270 mutex_unlock(&rdev->ddev->struct_mutex);
271 return;
272 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400273 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400274
Matthew Garrett5876dd22010-04-26 15:52:20 -0400275 radeon_unmap_vram_bos(rdev);
276
Alex Deucherce8f5372010-05-07 15:10:16 -0400277 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400278 for (i = 0; i < rdev->num_crtc; i++) {
279 if (rdev->pm.active_crtcs & (1 << i)) {
280 rdev->pm.req_vblank |= (1 << i);
281 drm_vblank_get(rdev->ddev, i);
282 }
283 }
284 }
Alex Deucher539d2412010-04-29 00:22:43 -0400285
Alex Deucherce8f5372010-05-07 15:10:16 -0400286 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400287
Alex Deucherce8f5372010-05-07 15:10:16 -0400288 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400289 for (i = 0; i < rdev->num_crtc; i++) {
290 if (rdev->pm.req_vblank & (1 << i)) {
291 rdev->pm.req_vblank &= ~(1 << i);
292 drm_vblank_put(rdev->ddev, i);
293 }
294 }
295 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400296
Alex Deuchera4248162010-04-24 14:50:23 -0400297 /* update display watermarks based on new power state */
298 radeon_update_bandwidth_info(rdev);
299 if (rdev->pm.active_crtc_count)
300 radeon_bandwidth_update(rdev);
301
Alex Deucherce8f5372010-05-07 15:10:16 -0400302 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400303
Christian Königd6999bc2012-05-09 15:34:45 +0200304 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200305 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400306 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400307}
308
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400309static void radeon_pm_print_states(struct radeon_device *rdev)
310{
311 int i, j;
312 struct radeon_power_state *power_state;
313 struct radeon_pm_clock_info *clock_info;
314
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000315 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400316 for (i = 0; i < rdev->pm.num_power_states; i++) {
317 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000318 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400319 radeon_pm_state_type_name[power_state->type]);
320 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000321 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400322 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000323 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400324 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000325 DRM_DEBUG_DRIVER("\tSingle display only\n");
326 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400327 for (j = 0; j < power_state->num_clock_modes; j++) {
328 clock_info = &(power_state->clock_info[j]);
329 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400330 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
331 j,
332 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400333 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400334 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
335 j,
336 clock_info->sclk * 10,
337 clock_info->mclk * 10,
338 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400339 }
340 }
341}
342
Alex Deucherce8f5372010-05-07 15:10:16 -0400343static ssize_t radeon_get_pm_profile(struct device *dev,
344 struct device_attribute *attr,
345 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400346{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200347 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400348 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400349 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400350
Alex Deucherce8f5372010-05-07 15:10:16 -0400351 return snprintf(buf, PAGE_SIZE, "%s\n",
352 (cp == PM_PROFILE_AUTO) ? "auto" :
353 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100354 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400355 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400356}
357
Alex Deucherce8f5372010-05-07 15:10:16 -0400358static ssize_t radeon_set_pm_profile(struct device *dev,
359 struct device_attribute *attr,
360 const char *buf,
361 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400362{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200363 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400364 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400365
Alex Deucher4f2f2032014-05-19 19:21:29 -0400366 /* Can't set profile when the card is off */
367 if ((rdev->flags & RADEON_IS_PX) &&
368 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
369 return -EINVAL;
370
Alex Deuchera4248162010-04-24 14:50:23 -0400371 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400372 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
373 if (strncmp("default", buf, strlen("default")) == 0)
374 rdev->pm.profile = PM_PROFILE_DEFAULT;
375 else if (strncmp("auto", buf, strlen("auto")) == 0)
376 rdev->pm.profile = PM_PROFILE_AUTO;
377 else if (strncmp("low", buf, strlen("low")) == 0)
378 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400379 else if (strncmp("mid", buf, strlen("mid")) == 0)
380 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400381 else if (strncmp("high", buf, strlen("high")) == 0)
382 rdev->pm.profile = PM_PROFILE_HIGH;
383 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000384 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400385 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400386 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400387 radeon_pm_update_profile(rdev);
388 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000389 } else
390 count = -EINVAL;
391
Alex Deucherce8f5372010-05-07 15:10:16 -0400392fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400393 mutex_unlock(&rdev->pm.mutex);
394
395 return count;
396}
397
Alex Deucherce8f5372010-05-07 15:10:16 -0400398static ssize_t radeon_get_pm_method(struct device *dev,
399 struct device_attribute *attr,
400 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400401{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200402 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400403 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400404 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400405
406 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherda321c82013-04-12 13:55:22 -0400407 (pm == PM_METHOD_DYNPM) ? "dynpm" :
408 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
Alex Deuchera4248162010-04-24 14:50:23 -0400409}
410
Alex Deucherce8f5372010-05-07 15:10:16 -0400411static ssize_t radeon_set_pm_method(struct device *dev,
412 struct device_attribute *attr,
413 const char *buf,
414 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400415{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200416 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400417 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400418
Alex Deucher4f2f2032014-05-19 19:21:29 -0400419 /* Can't set method when the card is off */
420 if ((rdev->flags & RADEON_IS_PX) &&
421 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
422 count = -EINVAL;
423 goto fail;
424 }
425
Alex Deucherda321c82013-04-12 13:55:22 -0400426 /* we don't support the legacy modes with dpm */
427 if (rdev->pm.pm_method == PM_METHOD_DPM) {
428 count = -EINVAL;
429 goto fail;
430 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400431
432 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400433 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400434 rdev->pm.pm_method = PM_METHOD_DYNPM;
435 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
436 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400437 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400438 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
439 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400440 /* disable dynpm */
441 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
442 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000443 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400444 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100445 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400446 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000447 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400448 goto fail;
449 }
450 radeon_pm_compute_clocks(rdev);
451fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400452 return count;
453}
454
Alex Deucherda321c82013-04-12 13:55:22 -0400455static ssize_t radeon_get_dpm_state(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
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 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
462
Alex Deucher4f2f2032014-05-19 19:21:29 -0400463 if ((rdev->flags & RADEON_IS_PX) &&
464 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
465 return snprintf(buf, PAGE_SIZE, "off\n");
466
Alex Deucherda321c82013-04-12 13:55:22 -0400467 return snprintf(buf, PAGE_SIZE, "%s\n",
468 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
469 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
470}
471
472static ssize_t radeon_set_dpm_state(struct device *dev,
473 struct device_attribute *attr,
474 const char *buf,
475 size_t count)
476{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200477 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400478 struct radeon_device *rdev = ddev->dev_private;
479
Alex Deucher4f2f2032014-05-19 19:21:29 -0400480 /* Can't set dpm state when the card is off */
481 if ((rdev->flags & RADEON_IS_PX) &&
482 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
483 return -EINVAL;
484
Alex Deucherda321c82013-04-12 13:55:22 -0400485 mutex_lock(&rdev->pm.mutex);
486 if (strncmp("battery", buf, strlen("battery")) == 0)
487 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
488 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
489 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
490 else if (strncmp("performance", buf, strlen("performance")) == 0)
491 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
492 else {
493 mutex_unlock(&rdev->pm.mutex);
494 count = -EINVAL;
495 goto fail;
496 }
497 mutex_unlock(&rdev->pm.mutex);
498 radeon_pm_compute_clocks(rdev);
499fail:
500 return count;
501}
502
Alex Deucher70d01a52013-07-02 18:38:02 -0400503static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
504 struct device_attribute *attr,
505 char *buf)
506{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200507 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400508 struct radeon_device *rdev = ddev->dev_private;
509 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
510
Alex Deucher4f2f2032014-05-19 19:21:29 -0400511 if ((rdev->flags & RADEON_IS_PX) &&
512 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
513 return snprintf(buf, PAGE_SIZE, "off\n");
514
Alex Deucher70d01a52013-07-02 18:38:02 -0400515 return snprintf(buf, PAGE_SIZE, "%s\n",
516 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
517 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
518}
519
520static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
521 struct device_attribute *attr,
522 const char *buf,
523 size_t count)
524{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200525 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400526 struct radeon_device *rdev = ddev->dev_private;
527 enum radeon_dpm_forced_level level;
528 int ret = 0;
529
Alex Deucher4f2f2032014-05-19 19:21:29 -0400530 /* Can't force performance level when the card is off */
531 if ((rdev->flags & RADEON_IS_PX) &&
532 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
533 return -EINVAL;
534
Alex Deucher70d01a52013-07-02 18:38:02 -0400535 mutex_lock(&rdev->pm.mutex);
536 if (strncmp("low", buf, strlen("low")) == 0) {
537 level = RADEON_DPM_FORCED_LEVEL_LOW;
538 } else if (strncmp("high", buf, strlen("high")) == 0) {
539 level = RADEON_DPM_FORCED_LEVEL_HIGH;
540 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
541 level = RADEON_DPM_FORCED_LEVEL_AUTO;
542 } else {
Alex Deucher70d01a52013-07-02 18:38:02 -0400543 count = -EINVAL;
544 goto fail;
545 }
546 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher0a17af372013-10-23 17:22:29 -0400547 if (rdev->pm.dpm.thermal_active) {
548 count = -EINVAL;
549 goto fail;
550 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400551 ret = radeon_dpm_force_performance_level(rdev, level);
552 if (ret)
553 count = -EINVAL;
554 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400555fail:
Alex Deucher0a17af372013-10-23 17:22:29 -0400556 mutex_unlock(&rdev->pm.mutex);
557
Alex Deucher70d01a52013-07-02 18:38:02 -0400558 return count;
559}
560
Alex Deucherce8f5372010-05-07 15:10:16 -0400561static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
562static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400563static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400564static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
565 radeon_get_dpm_forced_performance_level,
566 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400567
Alex Deucher21a81222010-07-02 12:58:16 -0400568static ssize_t radeon_hwmon_show_temp(struct device *dev,
569 struct device_attribute *attr,
570 char *buf)
571{
Guenter Roeckec39f642013-11-22 21:52:00 -0800572 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher4f2f2032014-05-19 19:21:29 -0400573 struct drm_device *ddev = rdev->ddev;
Alex Deucher20d391d2011-02-01 16:12:34 -0500574 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400575
Alex Deucher4f2f2032014-05-19 19:21:29 -0400576 /* Can't get temperature when the card is off */
577 if ((rdev->flags & RADEON_IS_PX) &&
578 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
579 return -EINVAL;
580
Alex Deucher6bd1c382013-06-21 14:38:03 -0400581 if (rdev->asic->pm.get_temperature)
582 temp = radeon_get_temperature(rdev);
583 else
Alex Deucher21a81222010-07-02 12:58:16 -0400584 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400585
586 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
587}
588
Jean Delvare6ea4e842013-09-10 10:32:41 +0200589static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
590 struct device_attribute *attr,
591 char *buf)
592{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300593 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200594 int hyst = to_sensor_dev_attr(attr)->index;
595 int temp;
596
597 if (hyst)
598 temp = rdev->pm.dpm.thermal.min_temp;
599 else
600 temp = rdev->pm.dpm.thermal.max_temp;
601
602 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
603}
604
Alex Deucher21a81222010-07-02 12:58:16 -0400605static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200606static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
607static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Alex Deucher21a81222010-07-02 12:58:16 -0400608
609static struct attribute *hwmon_attributes[] = {
610 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200611 &sensor_dev_attr_temp1_crit.dev_attr.attr,
612 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400613 NULL
614};
615
Jean Delvare6ea4e842013-09-10 10:32:41 +0200616static umode_t hwmon_attributes_visible(struct kobject *kobj,
617 struct attribute *attr, int index)
618{
619 struct device *dev = container_of(kobj, struct device, kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300620 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200621
622 /* Skip limit attributes if DPM is not enabled */
623 if (rdev->pm.pm_method != PM_METHOD_DPM &&
624 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
625 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
626 return 0;
627
628 return attr->mode;
629}
630
Alex Deucher21a81222010-07-02 12:58:16 -0400631static const struct attribute_group hwmon_attrgroup = {
632 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200633 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400634};
635
Guenter Roeckec39f642013-11-22 21:52:00 -0800636static const struct attribute_group *hwmon_groups[] = {
637 &hwmon_attrgroup,
638 NULL
639};
640
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200641static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400642{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200643 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400644
645 switch (rdev->pm.int_thermal_type) {
646 case THERMAL_TYPE_RV6XX:
647 case THERMAL_TYPE_RV770:
648 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400649 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500650 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400651 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400652 case THERMAL_TYPE_CI:
653 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400654 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400655 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400656 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
657 "radeon", rdev,
658 hwmon_groups);
659 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
660 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200661 dev_err(rdev->dev,
662 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200663 }
Alex Deucher21a81222010-07-02 12:58:16 -0400664 break;
665 default:
666 break;
667 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200668
669 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400670}
671
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400672static void radeon_hwmon_fini(struct radeon_device *rdev)
673{
674 if (rdev->pm.int_hwmon_dev)
675 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
676}
677
Alex Deucherda321c82013-04-12 13:55:22 -0400678static void radeon_dpm_thermal_work_handler(struct work_struct *work)
679{
680 struct radeon_device *rdev =
681 container_of(work, struct radeon_device,
682 pm.dpm.thermal.work);
683 /* switch to the thermal state */
684 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
685
686 if (!rdev->pm.dpm_enabled)
687 return;
688
689 if (rdev->asic->pm.get_temperature) {
690 int temp = radeon_get_temperature(rdev);
691
692 if (temp < rdev->pm.dpm.thermal.min_temp)
693 /* switch back the user state */
694 dpm_state = rdev->pm.dpm.user_state;
695 } else {
696 if (rdev->pm.dpm.thermal.high_to_low)
697 /* switch back the user state */
698 dpm_state = rdev->pm.dpm.user_state;
699 }
Alex Deucher60320342013-07-24 14:59:48 -0400700 mutex_lock(&rdev->pm.mutex);
701 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
702 rdev->pm.dpm.thermal_active = true;
703 else
704 rdev->pm.dpm.thermal_active = false;
705 rdev->pm.dpm.state = dpm_state;
706 mutex_unlock(&rdev->pm.mutex);
707
708 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400709}
710
711static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
712 enum radeon_pm_state_type dpm_state)
713{
714 int i;
715 struct radeon_ps *ps;
716 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400717 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
718 true : false;
719
720 /* check if the vblank period is too short to adjust the mclk */
721 if (single_display && rdev->asic->dpm.vblank_too_short) {
722 if (radeon_dpm_vblank_too_short(rdev))
723 single_display = false;
724 }
Alex Deucherda321c82013-04-12 13:55:22 -0400725
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400726 /* certain older asics have a separare 3D performance state,
727 * so try that first if the user selected performance
728 */
729 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
730 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400731 /* balanced states don't exist at the moment */
732 if (dpm_state == POWER_STATE_TYPE_BALANCED)
733 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
734
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400735restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400736 /* Pick the best power state based on current conditions */
737 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
738 ps = &rdev->pm.dpm.ps[i];
739 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
740 switch (dpm_state) {
741 /* user states */
742 case POWER_STATE_TYPE_BATTERY:
743 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
744 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400745 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400746 return ps;
747 } else
748 return ps;
749 }
750 break;
751 case POWER_STATE_TYPE_BALANCED:
752 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
753 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400754 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400755 return ps;
756 } else
757 return ps;
758 }
759 break;
760 case POWER_STATE_TYPE_PERFORMANCE:
761 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
762 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400763 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400764 return ps;
765 } else
766 return ps;
767 }
768 break;
769 /* internal states */
770 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400771 if (rdev->pm.dpm.uvd_ps)
772 return rdev->pm.dpm.uvd_ps;
773 else
774 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400775 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
776 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
777 return ps;
778 break;
779 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
780 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
781 return ps;
782 break;
783 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
784 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
785 return ps;
786 break;
787 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
788 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
789 return ps;
790 break;
791 case POWER_STATE_TYPE_INTERNAL_BOOT:
792 return rdev->pm.dpm.boot_ps;
793 case POWER_STATE_TYPE_INTERNAL_THERMAL:
794 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
795 return ps;
796 break;
797 case POWER_STATE_TYPE_INTERNAL_ACPI:
798 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
799 return ps;
800 break;
801 case POWER_STATE_TYPE_INTERNAL_ULV:
802 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
803 return ps;
804 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400805 case POWER_STATE_TYPE_INTERNAL_3DPERF:
806 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
807 return ps;
808 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400809 default:
810 break;
811 }
812 }
813 /* use a fallback state if we didn't match */
814 switch (dpm_state) {
815 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400816 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
817 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400818 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
819 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
820 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400821 if (rdev->pm.dpm.uvd_ps) {
822 return rdev->pm.dpm.uvd_ps;
823 } else {
824 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
825 goto restart_search;
826 }
Alex Deucherda321c82013-04-12 13:55:22 -0400827 case POWER_STATE_TYPE_INTERNAL_THERMAL:
828 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
829 goto restart_search;
830 case POWER_STATE_TYPE_INTERNAL_ACPI:
831 dpm_state = POWER_STATE_TYPE_BATTERY;
832 goto restart_search;
833 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400834 case POWER_STATE_TYPE_BALANCED:
835 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400836 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
837 goto restart_search;
838 default:
839 break;
840 }
841
842 return NULL;
843}
844
845static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
846{
847 int i;
848 struct radeon_ps *ps;
849 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500850 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400851
852 /* if dpm init failed */
853 if (!rdev->pm.dpm_enabled)
854 return;
855
856 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
857 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400858 if ((!rdev->pm.dpm.thermal_active) &&
859 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400860 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
861 }
862 dpm_state = rdev->pm.dpm.state;
863
864 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
865 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -0500866 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -0400867 else
868 return;
869
Alex Deucherd22b7e42012-11-29 19:27:56 -0500870 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -0400871 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -0400872 /* vce just modifies an existing state so force a change */
873 if (ps->vce_active != rdev->pm.dpm.vce_active)
874 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -0500875 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
876 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
877 * all we need to do is update the display configuration.
878 */
879 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
880 /* update display watermarks based on new power state */
881 radeon_bandwidth_update(rdev);
882 /* update displays */
883 radeon_dpm_display_configuration_changed(rdev);
884 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
885 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
886 }
887 return;
888 } else {
889 /* for BTC+ if the num crtcs hasn't changed and state is the same,
890 * nothing to do, if the num crtcs is > 1 and state is the same,
891 * update display configuration.
892 */
893 if (rdev->pm.dpm.new_active_crtcs ==
894 rdev->pm.dpm.current_active_crtcs) {
895 return;
896 } else {
897 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
898 (rdev->pm.dpm.new_active_crtc_count > 1)) {
899 /* update display watermarks based on new power state */
900 radeon_bandwidth_update(rdev);
901 /* update displays */
902 radeon_dpm_display_configuration_changed(rdev);
903 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
904 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
905 return;
906 }
907 }
Alex Deucherda321c82013-04-12 13:55:22 -0400908 }
Alex Deucherda321c82013-04-12 13:55:22 -0400909 }
910
Alex Deucherb62d6282013-08-20 20:29:05 -0400911force:
Alex Deucher033a37d2013-10-23 18:35:43 -0400912 if (radeon_dpm == 1) {
913 printk("switching from power state:\n");
914 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
915 printk("switching to power state:\n");
916 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
917 }
Alex Deucherb62d6282013-08-20 20:29:05 -0400918
Alex Deucherda321c82013-04-12 13:55:22 -0400919 mutex_lock(&rdev->ddev->struct_mutex);
920 down_write(&rdev->pm.mclk_lock);
921 mutex_lock(&rdev->ring_lock);
922
Alex Deucherb62d6282013-08-20 20:29:05 -0400923 /* update whether vce is active */
924 ps->vce_active = rdev->pm.dpm.vce_active;
925
Alex Deucher89c9bc52013-01-16 14:40:26 -0500926 ret = radeon_dpm_pre_set_power_state(rdev);
927 if (ret)
928 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -0500929
Alex Deucherda321c82013-04-12 13:55:22 -0400930 /* update display watermarks based on new power state */
931 radeon_bandwidth_update(rdev);
932 /* update displays */
933 radeon_dpm_display_configuration_changed(rdev);
934
935 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
936 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
937
938 /* wait for the rings to drain */
939 for (i = 0; i < RADEON_NUM_RINGS; i++) {
940 struct radeon_ring *ring = &rdev->ring[i];
941 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +0100942 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -0400943 }
944
945 /* program the new power state */
946 radeon_dpm_set_power_state(rdev);
947
948 /* update current power state */
949 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
950
Alex Deucher89c9bc52013-01-16 14:40:26 -0500951 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -0500952
Alex Deucher1cd8b212013-09-13 14:07:03 -0400953 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -0400954 if (rdev->pm.dpm.thermal_active) {
955 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -0400956 /* force low perf level for thermal */
957 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -0400958 /* save the user's level */
959 rdev->pm.dpm.forced_level = level;
960 } else {
961 /* otherwise, user selected level */
962 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
963 }
Alex Deucher60320342013-07-24 14:59:48 -0400964 }
965
Alex Deucher84dd1922013-01-16 12:52:04 -0500966done:
Alex Deucherda321c82013-04-12 13:55:22 -0400967 mutex_unlock(&rdev->ring_lock);
968 up_write(&rdev->pm.mclk_lock);
969 mutex_unlock(&rdev->ddev->struct_mutex);
970}
971
Alex Deucherce3537d2013-07-24 12:12:49 -0400972void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
973{
974 enum radeon_pm_state_type dpm_state;
975
Alex Deucher9e9d9762013-07-31 18:13:23 -0400976 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -0400977 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +0100978 /* don't powergate anything if we
979 have active but pause streams */
980 enable |= rdev->pm.dpm.sd > 0;
981 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -0400982 /* enable/disable UVD */
983 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -0400984 mutex_unlock(&rdev->pm.mutex);
985 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -0400986 if (enable) {
987 mutex_lock(&rdev->pm.mutex);
988 rdev->pm.dpm.uvd_active = true;
Alex Deucher0690a222014-06-07 11:31:25 -0400989 /* disable this for now */
990#if 0
Alex Deucher9e9d9762013-07-31 18:13:23 -0400991 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
992 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
993 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
994 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
995 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
996 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
997 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
998 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
999 else
Alex Deucher0690a222014-06-07 11:31:25 -04001000#endif
Alex Deucher9e9d9762013-07-31 18:13:23 -04001001 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1002 rdev->pm.dpm.state = dpm_state;
1003 mutex_unlock(&rdev->pm.mutex);
1004 } else {
1005 mutex_lock(&rdev->pm.mutex);
1006 rdev->pm.dpm.uvd_active = false;
1007 mutex_unlock(&rdev->pm.mutex);
1008 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001009
Alex Deucher9e9d9762013-07-31 18:13:23 -04001010 radeon_pm_compute_clocks(rdev);
1011 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001012}
1013
Alex Deucher03afe6f2013-08-23 11:56:26 -04001014void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1015{
1016 if (enable) {
1017 mutex_lock(&rdev->pm.mutex);
1018 rdev->pm.dpm.vce_active = true;
1019 /* XXX select vce level based on ring/task */
1020 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1021 mutex_unlock(&rdev->pm.mutex);
1022 } else {
1023 mutex_lock(&rdev->pm.mutex);
1024 rdev->pm.dpm.vce_active = false;
1025 mutex_unlock(&rdev->pm.mutex);
1026 }
1027
1028 radeon_pm_compute_clocks(rdev);
1029}
1030
Alex Deucherda321c82013-04-12 13:55:22 -04001031static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -05001032{
Alex Deucherce8f5372010-05-07 15:10:16 -04001033 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001034 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001035 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1036 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001037 }
Alex Deucherce8f5372010-05-07 15:10:16 -04001038 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001039
1040 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001041}
1042
Alex Deucherda321c82013-04-12 13:55:22 -04001043static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1044{
1045 mutex_lock(&rdev->pm.mutex);
1046 /* disable dpm */
1047 radeon_dpm_disable(rdev);
1048 /* reset the power state */
1049 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1050 rdev->pm.dpm_enabled = false;
1051 mutex_unlock(&rdev->pm.mutex);
1052}
1053
1054void radeon_pm_suspend(struct radeon_device *rdev)
1055{
1056 if (rdev->pm.pm_method == PM_METHOD_DPM)
1057 radeon_pm_suspend_dpm(rdev);
1058 else
1059 radeon_pm_suspend_old(rdev);
1060}
1061
1062static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001063{
Alex Deuchered18a362011-01-06 21:19:32 -05001064 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001065 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001066 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001067 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001068 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001069 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1070 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001071 if (rdev->pm.default_vddci)
1072 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1073 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001074 if (rdev->pm.default_sclk)
1075 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1076 if (rdev->pm.default_mclk)
1077 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1078 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001079 /* asic init will reset the default power state */
1080 mutex_lock(&rdev->pm.mutex);
1081 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1082 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001083 rdev->pm.current_sclk = rdev->pm.default_sclk;
1084 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001085 if (rdev->pm.power_state) {
1086 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1087 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1088 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001089 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1090 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1091 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001092 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1093 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001094 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001095 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001096 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001097}
1098
Alex Deucherda321c82013-04-12 13:55:22 -04001099static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001100{
Dave Airlie26481fb2010-05-18 19:00:14 +10001101 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001102
Alex Deucherda321c82013-04-12 13:55:22 -04001103 /* asic init will reset to the boot state */
1104 mutex_lock(&rdev->pm.mutex);
1105 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1106 radeon_dpm_setup_asic(rdev);
1107 ret = radeon_dpm_enable(rdev);
1108 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001109 if (ret)
1110 goto dpm_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001111 rdev->pm.dpm_enabled = true;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001112 return;
1113
1114dpm_resume_fail:
1115 DRM_ERROR("radeon: dpm resume failed\n");
1116 if ((rdev->family >= CHIP_BARTS) &&
1117 (rdev->family <= CHIP_CAYMAN) &&
1118 rdev->mc_fw) {
1119 if (rdev->pm.default_vddc)
1120 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1121 SET_VOLTAGE_TYPE_ASIC_VDDC);
1122 if (rdev->pm.default_vddci)
1123 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1124 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1125 if (rdev->pm.default_sclk)
1126 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1127 if (rdev->pm.default_mclk)
1128 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001129 }
1130}
1131
1132void radeon_pm_resume(struct radeon_device *rdev)
1133{
1134 if (rdev->pm.pm_method == PM_METHOD_DPM)
1135 radeon_pm_resume_dpm(rdev);
1136 else
1137 radeon_pm_resume_old(rdev);
1138}
1139
1140static int radeon_pm_init_old(struct radeon_device *rdev)
1141{
1142 int ret;
1143
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001144 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001145 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1146 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1147 rdev->pm.dynpm_can_upclock = true;
1148 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001149 rdev->pm.default_sclk = rdev->clock.default_sclk;
1150 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001151 rdev->pm.current_sclk = rdev->clock.default_sclk;
1152 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001153 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001154
Alex Deucher56278a82009-12-28 13:58:44 -05001155 if (rdev->bios) {
1156 if (rdev->is_atom_bios)
1157 radeon_atombios_get_power_modes(rdev);
1158 else
1159 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001160 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001161 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001162 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001163 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001164 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001165 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001166 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001167 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1168 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001169 if (rdev->pm.default_vddci)
1170 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1171 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001172 if (rdev->pm.default_sclk)
1173 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1174 if (rdev->pm.default_mclk)
1175 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1176 }
Alex Deucher56278a82009-12-28 13:58:44 -05001177 }
1178
Alex Deucher21a81222010-07-02 12:58:16 -04001179 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001180 ret = radeon_hwmon_init(rdev);
1181 if (ret)
1182 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001183
1184 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1185
Alex Deucherce8f5372010-05-07 15:10:16 -04001186 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001187 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001188 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1189 if (ret)
1190 DRM_ERROR("failed to create device file for power profile\n");
1191 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1192 if (ret)
1193 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001194
Alex Deucherce8f5372010-05-07 15:10:16 -04001195 if (radeon_debugfs_pm_init(rdev)) {
1196 DRM_ERROR("Failed to register debugfs file for PM!\n");
1197 }
1198
1199 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001200 }
1201
1202 return 0;
1203}
1204
Alex Deucherda321c82013-04-12 13:55:22 -04001205static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1206{
1207 int i;
1208
1209 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1210 printk("== power state %d ==\n", i);
1211 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1212 }
1213}
1214
1215static int radeon_pm_init_dpm(struct radeon_device *rdev)
1216{
1217 int ret;
1218
Alex Deucher1cd8b212013-09-13 14:07:03 -04001219 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001220 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1221 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001222 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001223 rdev->pm.default_sclk = rdev->clock.default_sclk;
1224 rdev->pm.default_mclk = rdev->clock.default_mclk;
1225 rdev->pm.current_sclk = rdev->clock.default_sclk;
1226 rdev->pm.current_mclk = rdev->clock.default_mclk;
1227 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1228
1229 if (rdev->bios && rdev->is_atom_bios)
1230 radeon_atombios_get_power_modes(rdev);
1231 else
1232 return -EINVAL;
1233
1234 /* set up the internal thermal sensor if applicable */
1235 ret = radeon_hwmon_init(rdev);
1236 if (ret)
1237 return ret;
1238
1239 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1240 mutex_lock(&rdev->pm.mutex);
1241 radeon_dpm_init(rdev);
1242 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001243 if (radeon_dpm == 1)
1244 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001245 radeon_dpm_setup_asic(rdev);
1246 ret = radeon_dpm_enable(rdev);
1247 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001248 if (ret)
1249 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001250 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001251
Alex Deucherbb5abf92013-12-18 13:39:58 -05001252 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1253 if (ret)
1254 DRM_ERROR("failed to create device file for dpm state\n");
1255 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1256 if (ret)
1257 DRM_ERROR("failed to create device file for dpm state\n");
1258 /* XXX: these are noops for dpm but are here for backwards compat */
1259 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1260 if (ret)
1261 DRM_ERROR("failed to create device file for power profile\n");
1262 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1263 if (ret)
1264 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001265
Alex Deucherbb5abf92013-12-18 13:39:58 -05001266 if (radeon_debugfs_pm_init(rdev)) {
1267 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001268 }
1269
Alex Deucherbb5abf92013-12-18 13:39:58 -05001270 DRM_INFO("radeon: dpm initialized\n");
1271
Alex Deucherda321c82013-04-12 13:55:22 -04001272 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001273
1274dpm_failed:
1275 rdev->pm.dpm_enabled = false;
1276 if ((rdev->family >= CHIP_BARTS) &&
1277 (rdev->family <= CHIP_CAYMAN) &&
1278 rdev->mc_fw) {
1279 if (rdev->pm.default_vddc)
1280 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1281 SET_VOLTAGE_TYPE_ASIC_VDDC);
1282 if (rdev->pm.default_vddci)
1283 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1284 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1285 if (rdev->pm.default_sclk)
1286 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1287 if (rdev->pm.default_mclk)
1288 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1289 }
1290 DRM_ERROR("radeon: dpm initialization failed\n");
1291 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001292}
1293
1294int radeon_pm_init(struct radeon_device *rdev)
1295{
1296 /* enable dpm on rv6xx+ */
1297 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001298 case CHIP_RV610:
1299 case CHIP_RV630:
1300 case CHIP_RV620:
1301 case CHIP_RV635:
1302 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001303 case CHIP_RS780:
1304 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001305 case CHIP_RV770:
Alex Deucher919cf552014-01-11 10:55:55 -05001306 case CHIP_BARTS:
1307 case CHIP_TURKS:
1308 case CHIP_CAICOS:
Alex Deucher69e0b572013-04-12 16:42:42 -04001309 case CHIP_CAYMAN:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001310 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001311 if (!rdev->rlc_fw)
1312 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001313 else if ((rdev->family >= CHIP_RV770) &&
1314 (!(rdev->flags & RADEON_IS_IGP)) &&
1315 (!rdev->smc_fw))
1316 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001317 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001318 rdev->pm.pm_method = PM_METHOD_DPM;
1319 else
1320 rdev->pm.pm_method = PM_METHOD_PROFILE;
1321 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001322 case CHIP_RV730:
1323 case CHIP_RV710:
1324 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001325 case CHIP_CEDAR:
1326 case CHIP_REDWOOD:
1327 case CHIP_JUNIPER:
1328 case CHIP_CYPRESS:
1329 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001330 case CHIP_PALM:
1331 case CHIP_SUMO:
1332 case CHIP_SUMO2:
Alex Deucher3a118982013-11-14 10:21:29 -05001333 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001334 case CHIP_TAHITI:
1335 case CHIP_PITCAIRN:
1336 case CHIP_VERDE:
1337 case CHIP_OLAND:
1338 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001339 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001340 case CHIP_KABINI:
1341 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001342 case CHIP_HAWAII:
Samuel Li7d032a42014-04-30 18:40:51 -04001343 case CHIP_MULLINS:
Alex Deucher5a16f762013-10-23 17:11:06 -04001344 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1345 if (!rdev->rlc_fw)
1346 rdev->pm.pm_method = PM_METHOD_PROFILE;
1347 else if ((rdev->family >= CHIP_RV770) &&
1348 (!(rdev->flags & RADEON_IS_IGP)) &&
1349 (!rdev->smc_fw))
1350 rdev->pm.pm_method = PM_METHOD_PROFILE;
1351 else if (radeon_dpm == 0)
1352 rdev->pm.pm_method = PM_METHOD_PROFILE;
1353 else
1354 rdev->pm.pm_method = PM_METHOD_DPM;
1355 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001356 default:
1357 /* default to profile method */
1358 rdev->pm.pm_method = PM_METHOD_PROFILE;
1359 break;
1360 }
1361
1362 if (rdev->pm.pm_method == PM_METHOD_DPM)
1363 return radeon_pm_init_dpm(rdev);
1364 else
1365 return radeon_pm_init_old(rdev);
1366}
1367
Alex Deucher914a8982013-12-19 11:37:22 -05001368int radeon_pm_late_init(struct radeon_device *rdev)
1369{
1370 int ret = 0;
1371
1372 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1373 mutex_lock(&rdev->pm.mutex);
1374 ret = radeon_dpm_late_enable(rdev);
1375 mutex_unlock(&rdev->pm.mutex);
1376 }
1377 return ret;
1378}
1379
Alex Deucherda321c82013-04-12 13:55:22 -04001380static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001381{
Alex Deucherce8f5372010-05-07 15:10:16 -04001382 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001383 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001384 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1385 rdev->pm.profile = PM_PROFILE_DEFAULT;
1386 radeon_pm_update_profile(rdev);
1387 radeon_pm_set_clocks(rdev);
1388 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001389 /* reset default clocks */
1390 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1391 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1392 radeon_pm_set_clocks(rdev);
1393 }
Alex Deuchera4248162010-04-24 14:50:23 -04001394 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001395
1396 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001397
Alex Deucherce8f5372010-05-07 15:10:16 -04001398 device_remove_file(rdev->dev, &dev_attr_power_profile);
1399 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001400 }
Alex Deuchera4248162010-04-24 14:50:23 -04001401
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001402 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001403 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001404}
1405
Alex Deucherda321c82013-04-12 13:55:22 -04001406static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1407{
1408 if (rdev->pm.num_power_states > 1) {
1409 mutex_lock(&rdev->pm.mutex);
1410 radeon_dpm_disable(rdev);
1411 mutex_unlock(&rdev->pm.mutex);
1412
1413 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001414 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001415 /* XXX backwards compat */
1416 device_remove_file(rdev->dev, &dev_attr_power_profile);
1417 device_remove_file(rdev->dev, &dev_attr_power_method);
1418 }
1419 radeon_dpm_fini(rdev);
1420
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001421 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001422 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001423}
1424
1425void radeon_pm_fini(struct radeon_device *rdev)
1426{
1427 if (rdev->pm.pm_method == PM_METHOD_DPM)
1428 radeon_pm_fini_dpm(rdev);
1429 else
1430 radeon_pm_fini_old(rdev);
1431}
1432
1433static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001434{
1435 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001436 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001437 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001438
Alex Deucherce8f5372010-05-07 15:10:16 -04001439 if (rdev->pm.num_power_states < 2)
1440 return;
1441
Rafał Miłeckic913e232009-12-22 23:02:16 +01001442 mutex_lock(&rdev->pm.mutex);
1443
1444 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001445 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001446 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1447 list_for_each_entry(crtc,
1448 &ddev->mode_config.crtc_list, head) {
1449 radeon_crtc = to_radeon_crtc(crtc);
1450 if (radeon_crtc->enabled) {
1451 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1452 rdev->pm.active_crtc_count++;
1453 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001454 }
1455 }
1456
Alex Deucherce8f5372010-05-07 15:10:16 -04001457 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1458 radeon_pm_update_profile(rdev);
1459 radeon_pm_set_clocks(rdev);
1460 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1461 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1462 if (rdev->pm.active_crtc_count > 1) {
1463 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1464 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001465
Alex Deucherce8f5372010-05-07 15:10:16 -04001466 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1467 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1468 radeon_pm_get_dynpm_state(rdev);
1469 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001470
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001471 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001472 }
1473 } else if (rdev->pm.active_crtc_count == 1) {
1474 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001475
Alex Deucherce8f5372010-05-07 15:10:16 -04001476 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1477 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1478 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1479 radeon_pm_get_dynpm_state(rdev);
1480 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001481
Tejun Heo32c87fc2011-01-03 14:49:32 +01001482 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1483 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001484 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1485 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001486 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1487 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001488 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001489 }
1490 } else { /* count == 0 */
1491 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1492 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001493
Alex Deucherce8f5372010-05-07 15:10:16 -04001494 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1495 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1496 radeon_pm_get_dynpm_state(rdev);
1497 radeon_pm_set_clocks(rdev);
1498 }
1499 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001500 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001501 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001502
1503 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001504}
1505
Alex Deucherda321c82013-04-12 13:55:22 -04001506static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1507{
1508 struct drm_device *ddev = rdev->ddev;
1509 struct drm_crtc *crtc;
1510 struct radeon_crtc *radeon_crtc;
1511
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001512 if (!rdev->pm.dpm_enabled)
1513 return;
1514
Alex Deucherda321c82013-04-12 13:55:22 -04001515 mutex_lock(&rdev->pm.mutex);
1516
Alex Deucher5ca302f2012-11-30 10:56:57 -05001517 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001518 rdev->pm.dpm.new_active_crtcs = 0;
1519 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001520 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1521 list_for_each_entry(crtc,
1522 &ddev->mode_config.crtc_list, head) {
1523 radeon_crtc = to_radeon_crtc(crtc);
1524 if (crtc->enabled) {
1525 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1526 rdev->pm.dpm.new_active_crtc_count++;
1527 }
Alex Deucherda321c82013-04-12 13:55:22 -04001528 }
1529 }
1530
Alex Deucher5ca302f2012-11-30 10:56:57 -05001531 /* update battery/ac status */
1532 if (power_supply_is_system_supplied() > 0)
1533 rdev->pm.dpm.ac_power = true;
1534 else
1535 rdev->pm.dpm.ac_power = false;
1536
Alex Deucherda321c82013-04-12 13:55:22 -04001537 radeon_dpm_change_power_state_locked(rdev);
1538
1539 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001540
Alex Deucherda321c82013-04-12 13:55:22 -04001541}
1542
1543void radeon_pm_compute_clocks(struct radeon_device *rdev)
1544{
1545 if (rdev->pm.pm_method == PM_METHOD_DPM)
1546 radeon_pm_compute_clocks_dpm(rdev);
1547 else
1548 radeon_pm_compute_clocks_old(rdev);
1549}
1550
Alex Deucherce8f5372010-05-07 15:10:16 -04001551static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001552{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001553 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001554 bool in_vbl = true;
1555
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001556 /* Iterate over all active crtc's. All crtc's must be in vblank,
1557 * otherwise return in_vbl == false.
1558 */
1559 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1560 if (rdev->pm.active_crtcs & (1 << crtc)) {
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001561 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001562 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1563 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +10001564 in_vbl = false;
1565 }
1566 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001567
1568 return in_vbl;
1569}
1570
Alex Deucherce8f5372010-05-07 15:10:16 -04001571static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001572{
1573 u32 stat_crtc = 0;
1574 bool in_vbl = radeon_pm_in_vbl(rdev);
1575
Dave Airlief7352612010-02-18 15:58:36 +10001576 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001577 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001578 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001579 return in_vbl;
1580}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001581
Alex Deucherce8f5372010-05-07 15:10:16 -04001582static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001583{
1584 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001585 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001586 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001587 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001588
Matthew Garrettd9932a32010-04-26 16:02:26 -04001589 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001590 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001591 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001592 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001593 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001594
Alex Deucher74652802011-08-25 13:39:48 -04001595 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001596 struct radeon_ring *ring = &rdev->ring[i];
1597
1598 if (ring->ready) {
1599 not_processed += radeon_fence_count_emitted(rdev, i);
1600 if (not_processed >= 3)
1601 break;
1602 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001603 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001604
1605 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001606 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1607 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1608 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1609 rdev->pm.dynpm_can_upclock) {
1610 rdev->pm.dynpm_planned_action =
1611 DYNPM_ACTION_UPCLOCK;
1612 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001613 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1614 }
1615 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001616 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1617 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1618 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1619 rdev->pm.dynpm_can_downclock) {
1620 rdev->pm.dynpm_planned_action =
1621 DYNPM_ACTION_DOWNCLOCK;
1622 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001623 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1624 }
1625 }
1626
Alex Deucherd7311172010-05-03 01:13:14 -04001627 /* Note, radeon_pm_set_clocks is called with static_switch set
1628 * to false since we want to wait for vbl to avoid flicker.
1629 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001630 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1631 jiffies > rdev->pm.dynpm_action_timeout) {
1632 radeon_pm_get_dynpm_state(rdev);
1633 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001634 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001635
Tejun Heo32c87fc2011-01-03 14:49:32 +01001636 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1637 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001638 }
1639 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001640 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001641}
1642
Rafał Miłecki74338742009-11-03 00:53:02 +01001643/*
1644 * Debugfs info
1645 */
1646#if defined(CONFIG_DEBUG_FS)
1647
1648static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1649{
1650 struct drm_info_node *node = (struct drm_info_node *) m->private;
1651 struct drm_device *dev = node->minor->dev;
1652 struct radeon_device *rdev = dev->dev_private;
Alex Deucher4f2f2032014-05-19 19:21:29 -04001653 struct drm_device *ddev = rdev->ddev;
Rafał Miłecki74338742009-11-03 00:53:02 +01001654
Alex Deucher4f2f2032014-05-19 19:21:29 -04001655 if ((rdev->flags & RADEON_IS_PX) &&
1656 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1657 seq_printf(m, "PX asic powered off\n");
1658 } else if (rdev->pm.dpm_enabled) {
Alex Deucher1316b792013-06-28 09:28:39 -04001659 mutex_lock(&rdev->pm.mutex);
1660 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1661 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1662 else
Alex Deucher71375922013-07-02 09:11:39 -04001663 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001664 mutex_unlock(&rdev->pm.mutex);
1665 } else {
1666 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1667 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1668 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1669 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1670 else
1671 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1672 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1673 if (rdev->asic->pm.get_memory_clock)
1674 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1675 if (rdev->pm.current_vddc)
1676 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1677 if (rdev->asic->pm.get_pcie_lanes)
1678 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1679 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001680
1681 return 0;
1682}
1683
1684static struct drm_info_list radeon_pm_info_list[] = {
1685 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1686};
1687#endif
1688
Rafał Miłeckic913e232009-12-22 23:02:16 +01001689static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001690{
1691#if defined(CONFIG_DEBUG_FS)
1692 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1693#else
1694 return 0;
1695#endif
1696}