blob: 164898b0010c68b350c175b705ef3534af5be69a [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
463 return snprintf(buf, PAGE_SIZE, "%s\n",
464 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
465 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
466}
467
468static ssize_t radeon_set_dpm_state(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf,
471 size_t count)
472{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200473 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400474 struct radeon_device *rdev = ddev->dev_private;
475
476 mutex_lock(&rdev->pm.mutex);
477 if (strncmp("battery", buf, strlen("battery")) == 0)
478 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
479 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
480 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
481 else if (strncmp("performance", buf, strlen("performance")) == 0)
482 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
483 else {
484 mutex_unlock(&rdev->pm.mutex);
485 count = -EINVAL;
486 goto fail;
487 }
488 mutex_unlock(&rdev->pm.mutex);
Pali Rohárb07a6572014-08-11 19:01:58 +0200489
490 /* Can't set dpm state when the card is off */
491 if (!(rdev->flags & RADEON_IS_PX) ||
492 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
493 radeon_pm_compute_clocks(rdev);
494
Alex Deucherda321c82013-04-12 13:55:22 -0400495fail:
496 return count;
497}
498
Alex Deucher70d01a52013-07-02 18:38:02 -0400499static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
500 struct device_attribute *attr,
501 char *buf)
502{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200503 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400504 struct radeon_device *rdev = ddev->dev_private;
505 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
506
Alex Deucher4f2f2032014-05-19 19:21:29 -0400507 if ((rdev->flags & RADEON_IS_PX) &&
508 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
509 return snprintf(buf, PAGE_SIZE, "off\n");
510
Alex Deucher70d01a52013-07-02 18:38:02 -0400511 return snprintf(buf, PAGE_SIZE, "%s\n",
512 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
513 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
514}
515
516static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf,
519 size_t count)
520{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200521 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400522 struct radeon_device *rdev = ddev->dev_private;
523 enum radeon_dpm_forced_level level;
524 int ret = 0;
525
Alex Deucher4f2f2032014-05-19 19:21:29 -0400526 /* Can't force performance level when the card is off */
527 if ((rdev->flags & RADEON_IS_PX) &&
528 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
529 return -EINVAL;
530
Alex Deucher70d01a52013-07-02 18:38:02 -0400531 mutex_lock(&rdev->pm.mutex);
532 if (strncmp("low", buf, strlen("low")) == 0) {
533 level = RADEON_DPM_FORCED_LEVEL_LOW;
534 } else if (strncmp("high", buf, strlen("high")) == 0) {
535 level = RADEON_DPM_FORCED_LEVEL_HIGH;
536 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
537 level = RADEON_DPM_FORCED_LEVEL_AUTO;
538 } else {
Alex Deucher70d01a52013-07-02 18:38:02 -0400539 count = -EINVAL;
540 goto fail;
541 }
542 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher0a17af372013-10-23 17:22:29 -0400543 if (rdev->pm.dpm.thermal_active) {
544 count = -EINVAL;
545 goto fail;
546 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400547 ret = radeon_dpm_force_performance_level(rdev, level);
548 if (ret)
549 count = -EINVAL;
550 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400551fail:
Alex Deucher0a17af372013-10-23 17:22:29 -0400552 mutex_unlock(&rdev->pm.mutex);
553
Alex Deucher70d01a52013-07-02 18:38:02 -0400554 return count;
555}
556
Alex Deucherce8f5372010-05-07 15:10:16 -0400557static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
558static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400559static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400560static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
561 radeon_get_dpm_forced_performance_level,
562 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400563
Alex Deucher21a81222010-07-02 12:58:16 -0400564static ssize_t radeon_hwmon_show_temp(struct device *dev,
565 struct device_attribute *attr,
566 char *buf)
567{
Guenter Roeckec39f642013-11-22 21:52:00 -0800568 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher4f2f2032014-05-19 19:21:29 -0400569 struct drm_device *ddev = rdev->ddev;
Alex Deucher20d391d2011-02-01 16:12:34 -0500570 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400571
Alex Deucher4f2f2032014-05-19 19:21:29 -0400572 /* Can't get temperature when the card is off */
573 if ((rdev->flags & RADEON_IS_PX) &&
574 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
575 return -EINVAL;
576
Alex Deucher6bd1c382013-06-21 14:38:03 -0400577 if (rdev->asic->pm.get_temperature)
578 temp = radeon_get_temperature(rdev);
579 else
Alex Deucher21a81222010-07-02 12:58:16 -0400580 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400581
582 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
583}
584
Jean Delvare6ea4e842013-09-10 10:32:41 +0200585static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
586 struct device_attribute *attr,
587 char *buf)
588{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300589 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200590 int hyst = to_sensor_dev_attr(attr)->index;
591 int temp;
592
593 if (hyst)
594 temp = rdev->pm.dpm.thermal.min_temp;
595 else
596 temp = rdev->pm.dpm.thermal.max_temp;
597
598 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
599}
600
Alex Deucher21a81222010-07-02 12:58:16 -0400601static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200602static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
603static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Alex Deucher21a81222010-07-02 12:58:16 -0400604
605static struct attribute *hwmon_attributes[] = {
606 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200607 &sensor_dev_attr_temp1_crit.dev_attr.attr,
608 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400609 NULL
610};
611
Jean Delvare6ea4e842013-09-10 10:32:41 +0200612static umode_t hwmon_attributes_visible(struct kobject *kobj,
613 struct attribute *attr, int index)
614{
615 struct device *dev = container_of(kobj, struct device, kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300616 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200617
618 /* Skip limit attributes if DPM is not enabled */
619 if (rdev->pm.pm_method != PM_METHOD_DPM &&
620 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
621 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
622 return 0;
623
624 return attr->mode;
625}
626
Alex Deucher21a81222010-07-02 12:58:16 -0400627static const struct attribute_group hwmon_attrgroup = {
628 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200629 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400630};
631
Guenter Roeckec39f642013-11-22 21:52:00 -0800632static const struct attribute_group *hwmon_groups[] = {
633 &hwmon_attrgroup,
634 NULL
635};
636
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200637static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400638{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200639 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400640
641 switch (rdev->pm.int_thermal_type) {
642 case THERMAL_TYPE_RV6XX:
643 case THERMAL_TYPE_RV770:
644 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400645 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500646 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400647 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400648 case THERMAL_TYPE_CI:
649 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400650 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400651 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400652 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
653 "radeon", rdev,
654 hwmon_groups);
655 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
656 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200657 dev_err(rdev->dev,
658 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200659 }
Alex Deucher21a81222010-07-02 12:58:16 -0400660 break;
661 default:
662 break;
663 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200664
665 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400666}
667
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400668static void radeon_hwmon_fini(struct radeon_device *rdev)
669{
670 if (rdev->pm.int_hwmon_dev)
671 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
672}
673
Alex Deucherda321c82013-04-12 13:55:22 -0400674static void radeon_dpm_thermal_work_handler(struct work_struct *work)
675{
676 struct radeon_device *rdev =
677 container_of(work, struct radeon_device,
678 pm.dpm.thermal.work);
679 /* switch to the thermal state */
680 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
681
682 if (!rdev->pm.dpm_enabled)
683 return;
684
685 if (rdev->asic->pm.get_temperature) {
686 int temp = radeon_get_temperature(rdev);
687
688 if (temp < rdev->pm.dpm.thermal.min_temp)
689 /* switch back the user state */
690 dpm_state = rdev->pm.dpm.user_state;
691 } else {
692 if (rdev->pm.dpm.thermal.high_to_low)
693 /* switch back the user state */
694 dpm_state = rdev->pm.dpm.user_state;
695 }
Alex Deucher60320342013-07-24 14:59:48 -0400696 mutex_lock(&rdev->pm.mutex);
697 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
698 rdev->pm.dpm.thermal_active = true;
699 else
700 rdev->pm.dpm.thermal_active = false;
701 rdev->pm.dpm.state = dpm_state;
702 mutex_unlock(&rdev->pm.mutex);
703
704 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400705}
706
707static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
708 enum radeon_pm_state_type dpm_state)
709{
710 int i;
711 struct radeon_ps *ps;
712 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400713 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
714 true : false;
715
716 /* check if the vblank period is too short to adjust the mclk */
717 if (single_display && rdev->asic->dpm.vblank_too_short) {
718 if (radeon_dpm_vblank_too_short(rdev))
719 single_display = false;
720 }
Alex Deucherda321c82013-04-12 13:55:22 -0400721
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400722 /* certain older asics have a separare 3D performance state,
723 * so try that first if the user selected performance
724 */
725 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
726 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400727 /* balanced states don't exist at the moment */
728 if (dpm_state == POWER_STATE_TYPE_BALANCED)
729 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
730
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400731restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400732 /* Pick the best power state based on current conditions */
733 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
734 ps = &rdev->pm.dpm.ps[i];
735 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
736 switch (dpm_state) {
737 /* user states */
738 case POWER_STATE_TYPE_BATTERY:
739 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
740 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400741 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400742 return ps;
743 } else
744 return ps;
745 }
746 break;
747 case POWER_STATE_TYPE_BALANCED:
748 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
749 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400750 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400751 return ps;
752 } else
753 return ps;
754 }
755 break;
756 case POWER_STATE_TYPE_PERFORMANCE:
757 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
758 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400759 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400760 return ps;
761 } else
762 return ps;
763 }
764 break;
765 /* internal states */
766 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400767 if (rdev->pm.dpm.uvd_ps)
768 return rdev->pm.dpm.uvd_ps;
769 else
770 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400771 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
772 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
773 return ps;
774 break;
775 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
776 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
777 return ps;
778 break;
779 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
780 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
781 return ps;
782 break;
783 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
784 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
785 return ps;
786 break;
787 case POWER_STATE_TYPE_INTERNAL_BOOT:
788 return rdev->pm.dpm.boot_ps;
789 case POWER_STATE_TYPE_INTERNAL_THERMAL:
790 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
791 return ps;
792 break;
793 case POWER_STATE_TYPE_INTERNAL_ACPI:
794 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
795 return ps;
796 break;
797 case POWER_STATE_TYPE_INTERNAL_ULV:
798 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
799 return ps;
800 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400801 case POWER_STATE_TYPE_INTERNAL_3DPERF:
802 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
803 return ps;
804 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400805 default:
806 break;
807 }
808 }
809 /* use a fallback state if we didn't match */
810 switch (dpm_state) {
811 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400812 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
813 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400814 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
815 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
816 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400817 if (rdev->pm.dpm.uvd_ps) {
818 return rdev->pm.dpm.uvd_ps;
819 } else {
820 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
821 goto restart_search;
822 }
Alex Deucherda321c82013-04-12 13:55:22 -0400823 case POWER_STATE_TYPE_INTERNAL_THERMAL:
824 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
825 goto restart_search;
826 case POWER_STATE_TYPE_INTERNAL_ACPI:
827 dpm_state = POWER_STATE_TYPE_BATTERY;
828 goto restart_search;
829 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400830 case POWER_STATE_TYPE_BALANCED:
831 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400832 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
833 goto restart_search;
834 default:
835 break;
836 }
837
838 return NULL;
839}
840
841static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
842{
843 int i;
844 struct radeon_ps *ps;
845 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500846 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400847
848 /* if dpm init failed */
849 if (!rdev->pm.dpm_enabled)
850 return;
851
852 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
853 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400854 if ((!rdev->pm.dpm.thermal_active) &&
855 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400856 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
857 }
858 dpm_state = rdev->pm.dpm.state;
859
860 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
861 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -0500862 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -0400863 else
864 return;
865
Alex Deucherd22b7e42012-11-29 19:27:56 -0500866 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -0400867 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -0400868 /* vce just modifies an existing state so force a change */
869 if (ps->vce_active != rdev->pm.dpm.vce_active)
870 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -0500871 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
872 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
873 * all we need to do is update the display configuration.
874 */
875 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
876 /* update display watermarks based on new power state */
877 radeon_bandwidth_update(rdev);
878 /* update displays */
879 radeon_dpm_display_configuration_changed(rdev);
880 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
881 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
882 }
883 return;
884 } else {
885 /* for BTC+ if the num crtcs hasn't changed and state is the same,
886 * nothing to do, if the num crtcs is > 1 and state is the same,
887 * update display configuration.
888 */
889 if (rdev->pm.dpm.new_active_crtcs ==
890 rdev->pm.dpm.current_active_crtcs) {
891 return;
892 } else {
893 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
894 (rdev->pm.dpm.new_active_crtc_count > 1)) {
895 /* update display watermarks based on new power state */
896 radeon_bandwidth_update(rdev);
897 /* update displays */
898 radeon_dpm_display_configuration_changed(rdev);
899 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
900 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
901 return;
902 }
903 }
Alex Deucherda321c82013-04-12 13:55:22 -0400904 }
Alex Deucherda321c82013-04-12 13:55:22 -0400905 }
906
Alex Deucherb62d6282013-08-20 20:29:05 -0400907force:
Alex Deucher033a37d2013-10-23 18:35:43 -0400908 if (radeon_dpm == 1) {
909 printk("switching from power state:\n");
910 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
911 printk("switching to power state:\n");
912 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
913 }
Alex Deucherb62d6282013-08-20 20:29:05 -0400914
Alex Deucherda321c82013-04-12 13:55:22 -0400915 mutex_lock(&rdev->ddev->struct_mutex);
916 down_write(&rdev->pm.mclk_lock);
917 mutex_lock(&rdev->ring_lock);
918
Alex Deucherb62d6282013-08-20 20:29:05 -0400919 /* update whether vce is active */
920 ps->vce_active = rdev->pm.dpm.vce_active;
921
Alex Deucher89c9bc52013-01-16 14:40:26 -0500922 ret = radeon_dpm_pre_set_power_state(rdev);
923 if (ret)
924 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -0500925
Alex Deucherda321c82013-04-12 13:55:22 -0400926 /* update display watermarks based on new power state */
927 radeon_bandwidth_update(rdev);
928 /* update displays */
929 radeon_dpm_display_configuration_changed(rdev);
930
931 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
932 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
933
934 /* wait for the rings to drain */
935 for (i = 0; i < RADEON_NUM_RINGS; i++) {
936 struct radeon_ring *ring = &rdev->ring[i];
937 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +0100938 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -0400939 }
940
941 /* program the new power state */
942 radeon_dpm_set_power_state(rdev);
943
944 /* update current power state */
945 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
946
Alex Deucher89c9bc52013-01-16 14:40:26 -0500947 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -0500948
Alex Deucher1cd8b212013-09-13 14:07:03 -0400949 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -0400950 if (rdev->pm.dpm.thermal_active) {
951 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -0400952 /* force low perf level for thermal */
953 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -0400954 /* save the user's level */
955 rdev->pm.dpm.forced_level = level;
956 } else {
957 /* otherwise, user selected level */
958 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
959 }
Alex Deucher60320342013-07-24 14:59:48 -0400960 }
961
Alex Deucher84dd1922013-01-16 12:52:04 -0500962done:
Alex Deucherda321c82013-04-12 13:55:22 -0400963 mutex_unlock(&rdev->ring_lock);
964 up_write(&rdev->pm.mclk_lock);
965 mutex_unlock(&rdev->ddev->struct_mutex);
966}
967
Alex Deucherce3537d2013-07-24 12:12:49 -0400968void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
969{
970 enum radeon_pm_state_type dpm_state;
971
Alex Deucher9e9d9762013-07-31 18:13:23 -0400972 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -0400973 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +0100974 /* don't powergate anything if we
975 have active but pause streams */
976 enable |= rdev->pm.dpm.sd > 0;
977 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -0400978 /* enable/disable UVD */
979 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -0400980 mutex_unlock(&rdev->pm.mutex);
981 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -0400982 if (enable) {
983 mutex_lock(&rdev->pm.mutex);
984 rdev->pm.dpm.uvd_active = true;
Alex Deucher0690a222014-06-07 11:31:25 -0400985 /* disable this for now */
986#if 0
Alex Deucher9e9d9762013-07-31 18:13:23 -0400987 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
988 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
989 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
990 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
991 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
992 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
993 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
994 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
995 else
Alex Deucher0690a222014-06-07 11:31:25 -0400996#endif
Alex Deucher9e9d9762013-07-31 18:13:23 -0400997 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
998 rdev->pm.dpm.state = dpm_state;
999 mutex_unlock(&rdev->pm.mutex);
1000 } else {
1001 mutex_lock(&rdev->pm.mutex);
1002 rdev->pm.dpm.uvd_active = false;
1003 mutex_unlock(&rdev->pm.mutex);
1004 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001005
Alex Deucher9e9d9762013-07-31 18:13:23 -04001006 radeon_pm_compute_clocks(rdev);
1007 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001008}
1009
Alex Deucher03afe6f2013-08-23 11:56:26 -04001010void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1011{
1012 if (enable) {
1013 mutex_lock(&rdev->pm.mutex);
1014 rdev->pm.dpm.vce_active = true;
1015 /* XXX select vce level based on ring/task */
1016 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1017 mutex_unlock(&rdev->pm.mutex);
1018 } else {
1019 mutex_lock(&rdev->pm.mutex);
1020 rdev->pm.dpm.vce_active = false;
1021 mutex_unlock(&rdev->pm.mutex);
1022 }
1023
1024 radeon_pm_compute_clocks(rdev);
1025}
1026
Alex Deucherda321c82013-04-12 13:55:22 -04001027static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -05001028{
Alex Deucherce8f5372010-05-07 15:10:16 -04001029 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001030 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001031 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1032 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001033 }
Alex Deucherce8f5372010-05-07 15:10:16 -04001034 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001035
1036 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001037}
1038
Alex Deucherda321c82013-04-12 13:55:22 -04001039static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1040{
1041 mutex_lock(&rdev->pm.mutex);
1042 /* disable dpm */
1043 radeon_dpm_disable(rdev);
1044 /* reset the power state */
1045 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1046 rdev->pm.dpm_enabled = false;
1047 mutex_unlock(&rdev->pm.mutex);
1048}
1049
1050void radeon_pm_suspend(struct radeon_device *rdev)
1051{
1052 if (rdev->pm.pm_method == PM_METHOD_DPM)
1053 radeon_pm_suspend_dpm(rdev);
1054 else
1055 radeon_pm_suspend_old(rdev);
1056}
1057
1058static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001059{
Alex Deuchered18a362011-01-06 21:19:32 -05001060 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001061 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001062 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001063 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001064 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001065 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1066 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001067 if (rdev->pm.default_vddci)
1068 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1069 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001070 if (rdev->pm.default_sclk)
1071 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1072 if (rdev->pm.default_mclk)
1073 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1074 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001075 /* asic init will reset the default power state */
1076 mutex_lock(&rdev->pm.mutex);
1077 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1078 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001079 rdev->pm.current_sclk = rdev->pm.default_sclk;
1080 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001081 if (rdev->pm.power_state) {
1082 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1083 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1084 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001085 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1086 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1087 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001088 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1089 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001090 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001091 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001092 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001093}
1094
Alex Deucherda321c82013-04-12 13:55:22 -04001095static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001096{
Dave Airlie26481fb2010-05-18 19:00:14 +10001097 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001098
Alex Deucherda321c82013-04-12 13:55:22 -04001099 /* asic init will reset to the boot state */
1100 mutex_lock(&rdev->pm.mutex);
1101 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1102 radeon_dpm_setup_asic(rdev);
1103 ret = radeon_dpm_enable(rdev);
1104 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001105 if (ret)
1106 goto dpm_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001107 rdev->pm.dpm_enabled = true;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001108 return;
1109
1110dpm_resume_fail:
1111 DRM_ERROR("radeon: dpm resume failed\n");
1112 if ((rdev->family >= CHIP_BARTS) &&
1113 (rdev->family <= CHIP_CAYMAN) &&
1114 rdev->mc_fw) {
1115 if (rdev->pm.default_vddc)
1116 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1117 SET_VOLTAGE_TYPE_ASIC_VDDC);
1118 if (rdev->pm.default_vddci)
1119 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1120 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1121 if (rdev->pm.default_sclk)
1122 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1123 if (rdev->pm.default_mclk)
1124 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001125 }
1126}
1127
1128void radeon_pm_resume(struct radeon_device *rdev)
1129{
1130 if (rdev->pm.pm_method == PM_METHOD_DPM)
1131 radeon_pm_resume_dpm(rdev);
1132 else
1133 radeon_pm_resume_old(rdev);
1134}
1135
1136static int radeon_pm_init_old(struct radeon_device *rdev)
1137{
1138 int ret;
1139
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001140 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001141 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1142 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1143 rdev->pm.dynpm_can_upclock = true;
1144 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001145 rdev->pm.default_sclk = rdev->clock.default_sclk;
1146 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001147 rdev->pm.current_sclk = rdev->clock.default_sclk;
1148 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001149 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001150
Alex Deucher56278a82009-12-28 13:58:44 -05001151 if (rdev->bios) {
1152 if (rdev->is_atom_bios)
1153 radeon_atombios_get_power_modes(rdev);
1154 else
1155 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001156 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001157 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001158 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001159 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001160 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001161 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001162 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001163 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1164 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001165 if (rdev->pm.default_vddci)
1166 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1167 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001168 if (rdev->pm.default_sclk)
1169 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1170 if (rdev->pm.default_mclk)
1171 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1172 }
Alex Deucher56278a82009-12-28 13:58:44 -05001173 }
1174
Alex Deucher21a81222010-07-02 12:58:16 -04001175 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001176 ret = radeon_hwmon_init(rdev);
1177 if (ret)
1178 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001179
1180 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1181
Alex Deucherce8f5372010-05-07 15:10:16 -04001182 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001183 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001184 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1185 if (ret)
1186 DRM_ERROR("failed to create device file for power profile\n");
1187 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1188 if (ret)
1189 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001190
Alex Deucherce8f5372010-05-07 15:10:16 -04001191 if (radeon_debugfs_pm_init(rdev)) {
1192 DRM_ERROR("Failed to register debugfs file for PM!\n");
1193 }
1194
1195 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001196 }
1197
1198 return 0;
1199}
1200
Alex Deucherda321c82013-04-12 13:55:22 -04001201static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1202{
1203 int i;
1204
1205 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1206 printk("== power state %d ==\n", i);
1207 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1208 }
1209}
1210
1211static int radeon_pm_init_dpm(struct radeon_device *rdev)
1212{
1213 int ret;
1214
Alex Deucher1cd8b212013-09-13 14:07:03 -04001215 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001216 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1217 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001218 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001219 rdev->pm.default_sclk = rdev->clock.default_sclk;
1220 rdev->pm.default_mclk = rdev->clock.default_mclk;
1221 rdev->pm.current_sclk = rdev->clock.default_sclk;
1222 rdev->pm.current_mclk = rdev->clock.default_mclk;
1223 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1224
1225 if (rdev->bios && rdev->is_atom_bios)
1226 radeon_atombios_get_power_modes(rdev);
1227 else
1228 return -EINVAL;
1229
1230 /* set up the internal thermal sensor if applicable */
1231 ret = radeon_hwmon_init(rdev);
1232 if (ret)
1233 return ret;
1234
1235 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1236 mutex_lock(&rdev->pm.mutex);
1237 radeon_dpm_init(rdev);
1238 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001239 if (radeon_dpm == 1)
1240 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001241 radeon_dpm_setup_asic(rdev);
1242 ret = radeon_dpm_enable(rdev);
1243 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001244 if (ret)
1245 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001246 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001247
Alex Deucherbb5abf92013-12-18 13:39:58 -05001248 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1249 if (ret)
1250 DRM_ERROR("failed to create device file for dpm state\n");
1251 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1252 if (ret)
1253 DRM_ERROR("failed to create device file for dpm state\n");
1254 /* XXX: these are noops for dpm but are here for backwards compat */
1255 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1256 if (ret)
1257 DRM_ERROR("failed to create device file for power profile\n");
1258 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1259 if (ret)
1260 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001261
Alex Deucherbb5abf92013-12-18 13:39:58 -05001262 if (radeon_debugfs_pm_init(rdev)) {
1263 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001264 }
1265
Alex Deucherbb5abf92013-12-18 13:39:58 -05001266 DRM_INFO("radeon: dpm initialized\n");
1267
Alex Deucherda321c82013-04-12 13:55:22 -04001268 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001269
1270dpm_failed:
1271 rdev->pm.dpm_enabled = false;
1272 if ((rdev->family >= CHIP_BARTS) &&
1273 (rdev->family <= CHIP_CAYMAN) &&
1274 rdev->mc_fw) {
1275 if (rdev->pm.default_vddc)
1276 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1277 SET_VOLTAGE_TYPE_ASIC_VDDC);
1278 if (rdev->pm.default_vddci)
1279 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1280 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1281 if (rdev->pm.default_sclk)
1282 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1283 if (rdev->pm.default_mclk)
1284 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1285 }
1286 DRM_ERROR("radeon: dpm initialization failed\n");
1287 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001288}
1289
1290int radeon_pm_init(struct radeon_device *rdev)
1291{
1292 /* enable dpm on rv6xx+ */
1293 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001294 case CHIP_RV610:
1295 case CHIP_RV630:
1296 case CHIP_RV620:
1297 case CHIP_RV635:
1298 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001299 case CHIP_RS780:
1300 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001301 case CHIP_RV770:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001302 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001303 if (!rdev->rlc_fw)
1304 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001305 else if ((rdev->family >= CHIP_RV770) &&
1306 (!(rdev->flags & RADEON_IS_IGP)) &&
1307 (!rdev->smc_fw))
1308 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001309 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001310 rdev->pm.pm_method = PM_METHOD_DPM;
1311 else
1312 rdev->pm.pm_method = PM_METHOD_PROFILE;
1313 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001314 case CHIP_RV730:
1315 case CHIP_RV710:
1316 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001317 case CHIP_CEDAR:
1318 case CHIP_REDWOOD:
1319 case CHIP_JUNIPER:
1320 case CHIP_CYPRESS:
1321 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001322 case CHIP_PALM:
1323 case CHIP_SUMO:
1324 case CHIP_SUMO2:
Alex Deucherc08abf12014-07-14 12:01:40 -04001325 case CHIP_BARTS:
1326 case CHIP_TURKS:
1327 case CHIP_CAICOS:
Alex Deucher8f500af2014-07-07 17:13:37 -04001328 case CHIP_CAYMAN:
Alex Deucher3a118982013-11-14 10:21:29 -05001329 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001330 case CHIP_TAHITI:
1331 case CHIP_PITCAIRN:
1332 case CHIP_VERDE:
1333 case CHIP_OLAND:
1334 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001335 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001336 case CHIP_KABINI:
1337 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001338 case CHIP_HAWAII:
Samuel Li7d032a42014-04-30 18:40:51 -04001339 case CHIP_MULLINS:
Alex Deucher5a16f762013-10-23 17:11:06 -04001340 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1341 if (!rdev->rlc_fw)
1342 rdev->pm.pm_method = PM_METHOD_PROFILE;
1343 else if ((rdev->family >= CHIP_RV770) &&
1344 (!(rdev->flags & RADEON_IS_IGP)) &&
1345 (!rdev->smc_fw))
1346 rdev->pm.pm_method = PM_METHOD_PROFILE;
1347 else if (radeon_dpm == 0)
1348 rdev->pm.pm_method = PM_METHOD_PROFILE;
1349 else
1350 rdev->pm.pm_method = PM_METHOD_DPM;
1351 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001352 default:
1353 /* default to profile method */
1354 rdev->pm.pm_method = PM_METHOD_PROFILE;
1355 break;
1356 }
1357
1358 if (rdev->pm.pm_method == PM_METHOD_DPM)
1359 return radeon_pm_init_dpm(rdev);
1360 else
1361 return radeon_pm_init_old(rdev);
1362}
1363
Alex Deucher914a8982013-12-19 11:37:22 -05001364int radeon_pm_late_init(struct radeon_device *rdev)
1365{
1366 int ret = 0;
1367
1368 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1369 mutex_lock(&rdev->pm.mutex);
1370 ret = radeon_dpm_late_enable(rdev);
1371 mutex_unlock(&rdev->pm.mutex);
1372 }
1373 return ret;
1374}
1375
Alex Deucherda321c82013-04-12 13:55:22 -04001376static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001377{
Alex Deucherce8f5372010-05-07 15:10:16 -04001378 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001379 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001380 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1381 rdev->pm.profile = PM_PROFILE_DEFAULT;
1382 radeon_pm_update_profile(rdev);
1383 radeon_pm_set_clocks(rdev);
1384 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001385 /* reset default clocks */
1386 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1387 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1388 radeon_pm_set_clocks(rdev);
1389 }
Alex Deuchera4248162010-04-24 14:50:23 -04001390 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001391
1392 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001393
Alex Deucherce8f5372010-05-07 15:10:16 -04001394 device_remove_file(rdev->dev, &dev_attr_power_profile);
1395 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001396 }
Alex Deuchera4248162010-04-24 14:50:23 -04001397
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001398 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001399 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001400}
1401
Alex Deucherda321c82013-04-12 13:55:22 -04001402static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1403{
1404 if (rdev->pm.num_power_states > 1) {
1405 mutex_lock(&rdev->pm.mutex);
1406 radeon_dpm_disable(rdev);
1407 mutex_unlock(&rdev->pm.mutex);
1408
1409 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001410 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001411 /* XXX backwards compat */
1412 device_remove_file(rdev->dev, &dev_attr_power_profile);
1413 device_remove_file(rdev->dev, &dev_attr_power_method);
1414 }
1415 radeon_dpm_fini(rdev);
1416
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001417 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001418 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001419}
1420
1421void radeon_pm_fini(struct radeon_device *rdev)
1422{
1423 if (rdev->pm.pm_method == PM_METHOD_DPM)
1424 radeon_pm_fini_dpm(rdev);
1425 else
1426 radeon_pm_fini_old(rdev);
1427}
1428
1429static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001430{
1431 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001432 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001433 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001434
Alex Deucherce8f5372010-05-07 15:10:16 -04001435 if (rdev->pm.num_power_states < 2)
1436 return;
1437
Rafał Miłeckic913e232009-12-22 23:02:16 +01001438 mutex_lock(&rdev->pm.mutex);
1439
1440 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001441 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001442 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1443 list_for_each_entry(crtc,
1444 &ddev->mode_config.crtc_list, head) {
1445 radeon_crtc = to_radeon_crtc(crtc);
1446 if (radeon_crtc->enabled) {
1447 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1448 rdev->pm.active_crtc_count++;
1449 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001450 }
1451 }
1452
Alex Deucherce8f5372010-05-07 15:10:16 -04001453 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1454 radeon_pm_update_profile(rdev);
1455 radeon_pm_set_clocks(rdev);
1456 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1457 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1458 if (rdev->pm.active_crtc_count > 1) {
1459 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1460 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001461
Alex Deucherce8f5372010-05-07 15:10:16 -04001462 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1463 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1464 radeon_pm_get_dynpm_state(rdev);
1465 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001466
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001467 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001468 }
1469 } else if (rdev->pm.active_crtc_count == 1) {
1470 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001471
Alex Deucherce8f5372010-05-07 15:10:16 -04001472 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1473 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1474 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1475 radeon_pm_get_dynpm_state(rdev);
1476 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001477
Tejun Heo32c87fc2011-01-03 14:49:32 +01001478 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1479 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001480 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1481 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001482 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1483 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001484 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001485 }
1486 } else { /* count == 0 */
1487 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1488 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001489
Alex Deucherce8f5372010-05-07 15:10:16 -04001490 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1491 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1492 radeon_pm_get_dynpm_state(rdev);
1493 radeon_pm_set_clocks(rdev);
1494 }
1495 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001496 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001497 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001498
1499 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001500}
1501
Alex Deucherda321c82013-04-12 13:55:22 -04001502static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1503{
1504 struct drm_device *ddev = rdev->ddev;
1505 struct drm_crtc *crtc;
1506 struct radeon_crtc *radeon_crtc;
1507
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001508 if (!rdev->pm.dpm_enabled)
1509 return;
1510
Alex Deucherda321c82013-04-12 13:55:22 -04001511 mutex_lock(&rdev->pm.mutex);
1512
Alex Deucher5ca302f2012-11-30 10:56:57 -05001513 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001514 rdev->pm.dpm.new_active_crtcs = 0;
1515 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001516 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1517 list_for_each_entry(crtc,
1518 &ddev->mode_config.crtc_list, head) {
1519 radeon_crtc = to_radeon_crtc(crtc);
1520 if (crtc->enabled) {
1521 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1522 rdev->pm.dpm.new_active_crtc_count++;
1523 }
Alex Deucherda321c82013-04-12 13:55:22 -04001524 }
1525 }
1526
Alex Deucher5ca302f2012-11-30 10:56:57 -05001527 /* update battery/ac status */
1528 if (power_supply_is_system_supplied() > 0)
1529 rdev->pm.dpm.ac_power = true;
1530 else
1531 rdev->pm.dpm.ac_power = false;
1532
Alex Deucherda321c82013-04-12 13:55:22 -04001533 radeon_dpm_change_power_state_locked(rdev);
1534
1535 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001536
Alex Deucherda321c82013-04-12 13:55:22 -04001537}
1538
1539void radeon_pm_compute_clocks(struct radeon_device *rdev)
1540{
1541 if (rdev->pm.pm_method == PM_METHOD_DPM)
1542 radeon_pm_compute_clocks_dpm(rdev);
1543 else
1544 radeon_pm_compute_clocks_old(rdev);
1545}
1546
Alex Deucherce8f5372010-05-07 15:10:16 -04001547static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001548{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001549 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001550 bool in_vbl = true;
1551
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001552 /* Iterate over all active crtc's. All crtc's must be in vblank,
1553 * otherwise return in_vbl == false.
1554 */
1555 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1556 if (rdev->pm.active_crtcs & (1 << crtc)) {
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001557 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001558 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1559 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +10001560 in_vbl = false;
1561 }
1562 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001563
1564 return in_vbl;
1565}
1566
Alex Deucherce8f5372010-05-07 15:10:16 -04001567static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001568{
1569 u32 stat_crtc = 0;
1570 bool in_vbl = radeon_pm_in_vbl(rdev);
1571
Dave Airlief7352612010-02-18 15:58:36 +10001572 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001573 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001574 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001575 return in_vbl;
1576}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001577
Alex Deucherce8f5372010-05-07 15:10:16 -04001578static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001579{
1580 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001581 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001582 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001583 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001584
Matthew Garrettd9932a32010-04-26 16:02:26 -04001585 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001586 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001587 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001588 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001589 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001590
Alex Deucher74652802011-08-25 13:39:48 -04001591 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001592 struct radeon_ring *ring = &rdev->ring[i];
1593
1594 if (ring->ready) {
1595 not_processed += radeon_fence_count_emitted(rdev, i);
1596 if (not_processed >= 3)
1597 break;
1598 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001599 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001600
1601 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001602 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1603 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1604 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1605 rdev->pm.dynpm_can_upclock) {
1606 rdev->pm.dynpm_planned_action =
1607 DYNPM_ACTION_UPCLOCK;
1608 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001609 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1610 }
1611 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001612 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1613 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1614 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1615 rdev->pm.dynpm_can_downclock) {
1616 rdev->pm.dynpm_planned_action =
1617 DYNPM_ACTION_DOWNCLOCK;
1618 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001619 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1620 }
1621 }
1622
Alex Deucherd7311172010-05-03 01:13:14 -04001623 /* Note, radeon_pm_set_clocks is called with static_switch set
1624 * to false since we want to wait for vbl to avoid flicker.
1625 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001626 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1627 jiffies > rdev->pm.dynpm_action_timeout) {
1628 radeon_pm_get_dynpm_state(rdev);
1629 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001630 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001631
Tejun Heo32c87fc2011-01-03 14:49:32 +01001632 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1633 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001634 }
1635 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001636 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001637}
1638
Rafał Miłecki74338742009-11-03 00:53:02 +01001639/*
1640 * Debugfs info
1641 */
1642#if defined(CONFIG_DEBUG_FS)
1643
1644static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1645{
1646 struct drm_info_node *node = (struct drm_info_node *) m->private;
1647 struct drm_device *dev = node->minor->dev;
1648 struct radeon_device *rdev = dev->dev_private;
Alex Deucher4f2f2032014-05-19 19:21:29 -04001649 struct drm_device *ddev = rdev->ddev;
Rafał Miłecki74338742009-11-03 00:53:02 +01001650
Alex Deucher4f2f2032014-05-19 19:21:29 -04001651 if ((rdev->flags & RADEON_IS_PX) &&
1652 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1653 seq_printf(m, "PX asic powered off\n");
1654 } else if (rdev->pm.dpm_enabled) {
Alex Deucher1316b792013-06-28 09:28:39 -04001655 mutex_lock(&rdev->pm.mutex);
1656 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1657 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1658 else
Alex Deucher71375922013-07-02 09:11:39 -04001659 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001660 mutex_unlock(&rdev->pm.mutex);
1661 } else {
1662 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1663 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1664 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1665 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1666 else
1667 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1668 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1669 if (rdev->asic->pm.get_memory_clock)
1670 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1671 if (rdev->pm.current_vddc)
1672 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1673 if (rdev->asic->pm.get_pcie_lanes)
1674 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1675 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001676
1677 return 0;
1678}
1679
1680static struct drm_info_list radeon_pm_info_list[] = {
1681 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1682};
1683#endif
1684
Rafał Miłeckic913e232009-12-22 23:02:16 +01001685static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001686{
1687#if defined(CONFIG_DEBUG_FS)
1688 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1689#else
1690 return 0;
1691#endif
1692}