blob: 91e1bd246cade9bc0cbc5a0402d726e11deac9a3 [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"
Oleg Chernovskiy99736702014-12-08 00:10:45 +030027#include "r600_dpm.h"
Alex Deucherce8f5372010-05-07 15:10:16 -040028#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040029#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010031
Rafał Miłeckic913e232009-12-22 23:02:16 +010032#define RADEON_IDLE_LOOP_MS 100
33#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010034#define RADEON_WAIT_VBLANK_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010035
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040036static const char *radeon_pm_state_type_name[5] = {
Alex Deuchereb2c27a2012-10-01 18:28:09 -040037 "",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040038 "Powersave",
39 "Battery",
40 "Balanced",
41 "Performance",
42};
43
Alex Deucherce8f5372010-05-07 15:10:16 -040044static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010045static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040046static bool radeon_pm_in_vbl(struct radeon_device *rdev);
47static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
48static void radeon_pm_update_profile(struct radeon_device *rdev);
49static void radeon_pm_set_clocks(struct radeon_device *rdev);
50
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040051int radeon_pm_get_type_index(struct radeon_device *rdev,
52 enum radeon_pm_state_type ps_type,
53 int instance)
54{
55 int i;
56 int found_instance = -1;
57
58 for (i = 0; i < rdev->pm.num_power_states; i++) {
59 if (rdev->pm.power_state[i].type == ps_type) {
60 found_instance++;
61 if (found_instance == instance)
62 return i;
63 }
64 }
65 /* return default if no match */
66 return rdev->pm.default_power_state_index;
67}
68
Alex Deucherc4917072012-07-31 17:14:35 -040069void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
Alex Deucherce8f5372010-05-07 15:10:16 -040070{
Alex Deucher1c71bda2013-09-09 19:11:52 -040071 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
72 mutex_lock(&rdev->pm.mutex);
73 if (power_supply_is_system_supplied() > 0)
74 rdev->pm.dpm.ac_power = true;
75 else
76 rdev->pm.dpm.ac_power = false;
Alex Deucher96682952014-06-18 14:23:46 -040077 if (rdev->family == CHIP_ARUBA) {
78 if (rdev->asic->dpm.enable_bapm)
79 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
80 }
Alex Deucher1c71bda2013-09-09 19:11:52 -040081 mutex_unlock(&rdev->pm.mutex);
82 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
Alex Deucherc4917072012-07-31 17:14:35 -040083 if (rdev->pm.profile == PM_PROFILE_AUTO) {
84 mutex_lock(&rdev->pm.mutex);
85 radeon_pm_update_profile(rdev);
86 radeon_pm_set_clocks(rdev);
87 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040088 }
89 }
Alex Deucherce8f5372010-05-07 15:10:16 -040090}
Alex Deucherce8f5372010-05-07 15:10:16 -040091
92static void radeon_pm_update_profile(struct radeon_device *rdev)
93{
94 switch (rdev->pm.profile) {
95 case PM_PROFILE_DEFAULT:
96 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
97 break;
98 case PM_PROFILE_AUTO:
99 if (power_supply_is_system_supplied() > 0) {
100 if (rdev->pm.active_crtc_count > 1)
101 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
102 else
103 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
104 } else {
105 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400106 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400107 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400108 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400109 }
110 break;
111 case PM_PROFILE_LOW:
112 if (rdev->pm.active_crtc_count > 1)
113 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
114 else
115 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
116 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400117 case PM_PROFILE_MID:
118 if (rdev->pm.active_crtc_count > 1)
119 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
120 else
121 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
122 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400123 case PM_PROFILE_HIGH:
124 if (rdev->pm.active_crtc_count > 1)
125 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
126 else
127 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
128 break;
129 }
130
131 if (rdev->pm.active_crtc_count == 0) {
132 rdev->pm.requested_power_state_index =
133 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
134 rdev->pm.requested_clock_mode_index =
135 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
136 } else {
137 rdev->pm.requested_power_state_index =
138 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
139 rdev->pm.requested_clock_mode_index =
140 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
141 }
142}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100143
Matthew Garrett5876dd22010-04-26 15:52:20 -0400144static void radeon_unmap_vram_bos(struct radeon_device *rdev)
145{
146 struct radeon_bo *bo, *n;
147
148 if (list_empty(&rdev->gem.objects))
149 return;
150
151 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
152 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
153 ttm_bo_unmap_virtual(&bo->tbo);
154 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400155}
156
Alex Deucherce8f5372010-05-07 15:10:16 -0400157static void radeon_sync_with_vblank(struct radeon_device *rdev)
158{
159 if (rdev->pm.active_crtcs) {
160 rdev->pm.vblank_sync = false;
161 wait_event_timeout(
162 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
163 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
164 }
165}
166
167static void radeon_set_power_state(struct radeon_device *rdev)
168{
169 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400170 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400171
172 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
173 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
174 return;
175
176 if (radeon_gui_idle(rdev)) {
177 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
178 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500179 if (sclk > rdev->pm.default_sclk)
180 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400181
Alex Deucher27810fb2012-10-01 19:25:11 -0400182 /* starting with BTC, there is one state that is used for both
183 * MH and SH. Difference is that we always use the high clock index for
Alex Deucher7ae764b2013-02-11 08:44:48 -0500184 * mclk and vddci.
Alex Deucher27810fb2012-10-01 19:25:11 -0400185 */
186 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
187 (rdev->family >= CHIP_BARTS) &&
188 rdev->pm.active_crtc_count &&
189 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
190 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
191 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
192 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
193 else
194 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
195 clock_info[rdev->pm.requested_clock_mode_index].mclk;
196
Alex Deucher9ace9f72011-01-06 21:19:26 -0500197 if (mclk > rdev->pm.default_mclk)
198 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400199
Alex Deucher92645872010-05-27 17:01:41 -0400200 /* upvolt before raising clocks, downvolt after lowering clocks */
201 if (sclk < rdev->pm.current_sclk)
202 misc_after = true;
203
204 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400205
206 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400207 if (!radeon_pm_in_vbl(rdev))
208 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400209 }
210
Alex Deucher92645872010-05-27 17:01:41 -0400211 radeon_pm_prepare(rdev);
212
213 if (!misc_after)
214 /* voltage, pcie lanes, etc.*/
215 radeon_pm_misc(rdev);
216
217 /* set engine clock */
218 if (sclk != rdev->pm.current_sclk) {
219 radeon_pm_debug_check_in_vbl(rdev, false);
220 radeon_set_engine_clock(rdev, sclk);
221 radeon_pm_debug_check_in_vbl(rdev, true);
222 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000223 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400224 }
225
226 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500227 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400228 radeon_pm_debug_check_in_vbl(rdev, false);
229 radeon_set_memory_clock(rdev, mclk);
230 radeon_pm_debug_check_in_vbl(rdev, true);
231 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000232 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400233 }
234
235 if (misc_after)
236 /* voltage, pcie lanes, etc.*/
237 radeon_pm_misc(rdev);
238
239 radeon_pm_finish(rdev);
240
Alex Deucherce8f5372010-05-07 15:10:16 -0400241 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
242 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
243 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000244 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400245}
246
247static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400248{
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500249 int i, r;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400250
Alex Deucher4e186b22010-08-13 10:53:35 -0400251 /* no need to take locks, etc. if nothing's going to change */
252 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
253 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
254 return;
255
Matthew Garrett612e06c2010-04-27 17:16:58 -0400256 mutex_lock(&rdev->ddev->struct_mutex);
Christian Königdb7fce32012-05-11 14:57:18 +0200257 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200258 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400259
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400260 /* wait for the rings to drain */
261 for (i = 0; i < RADEON_NUM_RINGS; i++) {
262 struct radeon_ring *ring = &rdev->ring[i];
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500263 if (!ring->ready) {
264 continue;
265 }
Christian König37615522014-02-18 15:58:31 +0100266 r = radeon_fence_wait_empty(rdev, i);
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500267 if (r) {
268 /* needs a GPU reset dont reset here */
269 mutex_unlock(&rdev->ring_lock);
270 up_write(&rdev->pm.mclk_lock);
271 mutex_unlock(&rdev->ddev->struct_mutex);
272 return;
273 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400274 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400275
Matthew Garrett5876dd22010-04-26 15:52:20 -0400276 radeon_unmap_vram_bos(rdev);
277
Alex Deucherce8f5372010-05-07 15:10:16 -0400278 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400279 for (i = 0; i < rdev->num_crtc; i++) {
280 if (rdev->pm.active_crtcs & (1 << i)) {
281 rdev->pm.req_vblank |= (1 << i);
282 drm_vblank_get(rdev->ddev, i);
283 }
284 }
285 }
Alex Deucher539d2412010-04-29 00:22:43 -0400286
Alex Deucherce8f5372010-05-07 15:10:16 -0400287 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400288
Alex Deucherce8f5372010-05-07 15:10:16 -0400289 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400290 for (i = 0; i < rdev->num_crtc; i++) {
291 if (rdev->pm.req_vblank & (1 << i)) {
292 rdev->pm.req_vblank &= ~(1 << i);
293 drm_vblank_put(rdev->ddev, i);
294 }
295 }
296 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400297
Alex Deuchera4248162010-04-24 14:50:23 -0400298 /* update display watermarks based on new power state */
299 radeon_update_bandwidth_info(rdev);
300 if (rdev->pm.active_crtc_count)
301 radeon_bandwidth_update(rdev);
302
Alex Deucherce8f5372010-05-07 15:10:16 -0400303 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400304
Christian Königd6999bc2012-05-09 15:34:45 +0200305 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200306 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400307 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400308}
309
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400310static void radeon_pm_print_states(struct radeon_device *rdev)
311{
312 int i, j;
313 struct radeon_power_state *power_state;
314 struct radeon_pm_clock_info *clock_info;
315
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000316 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400317 for (i = 0; i < rdev->pm.num_power_states; i++) {
318 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000319 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400320 radeon_pm_state_type_name[power_state->type]);
321 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000322 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400323 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000324 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400325 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000326 DRM_DEBUG_DRIVER("\tSingle display only\n");
327 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400328 for (j = 0; j < power_state->num_clock_modes; j++) {
329 clock_info = &(power_state->clock_info[j]);
330 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400331 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
332 j,
333 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400334 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400335 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
336 j,
337 clock_info->sclk * 10,
338 clock_info->mclk * 10,
339 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400340 }
341 }
342}
343
Alex Deucherce8f5372010-05-07 15:10:16 -0400344static ssize_t radeon_get_pm_profile(struct device *dev,
345 struct device_attribute *attr,
346 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400347{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200348 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400349 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400350 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400351
Alex Deucherce8f5372010-05-07 15:10:16 -0400352 return snprintf(buf, PAGE_SIZE, "%s\n",
353 (cp == PM_PROFILE_AUTO) ? "auto" :
354 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100355 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400356 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400357}
358
Alex Deucherce8f5372010-05-07 15:10:16 -0400359static ssize_t radeon_set_pm_profile(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf,
362 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400363{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200364 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400365 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400366
Alex Deucher4f2f2032014-05-19 19:21:29 -0400367 /* Can't set profile when the card is off */
368 if ((rdev->flags & RADEON_IS_PX) &&
369 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
370 return -EINVAL;
371
Alex Deuchera4248162010-04-24 14:50:23 -0400372 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400373 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
374 if (strncmp("default", buf, strlen("default")) == 0)
375 rdev->pm.profile = PM_PROFILE_DEFAULT;
376 else if (strncmp("auto", buf, strlen("auto")) == 0)
377 rdev->pm.profile = PM_PROFILE_AUTO;
378 else if (strncmp("low", buf, strlen("low")) == 0)
379 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400380 else if (strncmp("mid", buf, strlen("mid")) == 0)
381 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400382 else if (strncmp("high", buf, strlen("high")) == 0)
383 rdev->pm.profile = PM_PROFILE_HIGH;
384 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000385 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400386 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400387 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400388 radeon_pm_update_profile(rdev);
389 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000390 } else
391 count = -EINVAL;
392
Alex Deucherce8f5372010-05-07 15:10:16 -0400393fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400394 mutex_unlock(&rdev->pm.mutex);
395
396 return count;
397}
398
Alex Deucherce8f5372010-05-07 15:10:16 -0400399static ssize_t radeon_get_pm_method(struct device *dev,
400 struct device_attribute *attr,
401 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400402{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200403 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400404 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400405 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400406
407 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherda321c82013-04-12 13:55:22 -0400408 (pm == PM_METHOD_DYNPM) ? "dynpm" :
409 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
Alex Deuchera4248162010-04-24 14:50:23 -0400410}
411
Alex Deucherce8f5372010-05-07 15:10:16 -0400412static ssize_t radeon_set_pm_method(struct device *dev,
413 struct device_attribute *attr,
414 const char *buf,
415 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400416{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200417 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400418 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400419
Alex Deucher4f2f2032014-05-19 19:21:29 -0400420 /* Can't set method when the card is off */
421 if ((rdev->flags & RADEON_IS_PX) &&
422 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
423 count = -EINVAL;
424 goto fail;
425 }
426
Alex Deucherda321c82013-04-12 13:55:22 -0400427 /* we don't support the legacy modes with dpm */
428 if (rdev->pm.pm_method == PM_METHOD_DPM) {
429 count = -EINVAL;
430 goto fail;
431 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400432
433 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400434 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400435 rdev->pm.pm_method = PM_METHOD_DYNPM;
436 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
437 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400438 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400439 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
440 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400441 /* disable dynpm */
442 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
443 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000444 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400445 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100446 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400447 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000448 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400449 goto fail;
450 }
451 radeon_pm_compute_clocks(rdev);
452fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400453 return count;
454}
455
Alex Deucherda321c82013-04-12 13:55:22 -0400456static ssize_t radeon_get_dpm_state(struct device *dev,
457 struct device_attribute *attr,
458 char *buf)
459{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200460 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400461 struct radeon_device *rdev = ddev->dev_private;
462 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
463
464 return snprintf(buf, PAGE_SIZE, "%s\n",
465 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
466 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
467}
468
469static ssize_t radeon_set_dpm_state(struct device *dev,
470 struct device_attribute *attr,
471 const char *buf,
472 size_t count)
473{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200474 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400475 struct radeon_device *rdev = ddev->dev_private;
476
477 mutex_lock(&rdev->pm.mutex);
478 if (strncmp("battery", buf, strlen("battery")) == 0)
479 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
480 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
481 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
482 else if (strncmp("performance", buf, strlen("performance")) == 0)
483 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
484 else {
485 mutex_unlock(&rdev->pm.mutex);
486 count = -EINVAL;
487 goto fail;
488 }
489 mutex_unlock(&rdev->pm.mutex);
Pali Rohárb07a6572014-08-11 19:01:58 +0200490
491 /* Can't set dpm state when the card is off */
492 if (!(rdev->flags & RADEON_IS_PX) ||
493 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
494 radeon_pm_compute_clocks(rdev);
495
Alex Deucherda321c82013-04-12 13:55:22 -0400496fail:
497 return count;
498}
499
Alex Deucher70d01a52013-07-02 18:38:02 -0400500static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
501 struct device_attribute *attr,
502 char *buf)
503{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200504 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400505 struct radeon_device *rdev = ddev->dev_private;
506 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
507
Alex Deucher4f2f2032014-05-19 19:21:29 -0400508 if ((rdev->flags & RADEON_IS_PX) &&
509 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
510 return snprintf(buf, PAGE_SIZE, "off\n");
511
Alex Deucher70d01a52013-07-02 18:38:02 -0400512 return snprintf(buf, PAGE_SIZE, "%s\n",
513 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
514 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
515}
516
517static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf,
520 size_t count)
521{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200522 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400523 struct radeon_device *rdev = ddev->dev_private;
524 enum radeon_dpm_forced_level level;
525 int ret = 0;
526
Alex Deucher4f2f2032014-05-19 19:21:29 -0400527 /* Can't force performance level when the card is off */
528 if ((rdev->flags & RADEON_IS_PX) &&
529 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
530 return -EINVAL;
531
Alex Deucher70d01a52013-07-02 18:38:02 -0400532 mutex_lock(&rdev->pm.mutex);
533 if (strncmp("low", buf, strlen("low")) == 0) {
534 level = RADEON_DPM_FORCED_LEVEL_LOW;
535 } else if (strncmp("high", buf, strlen("high")) == 0) {
536 level = RADEON_DPM_FORCED_LEVEL_HIGH;
537 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
538 level = RADEON_DPM_FORCED_LEVEL_AUTO;
539 } else {
Alex Deucher70d01a52013-07-02 18:38:02 -0400540 count = -EINVAL;
541 goto fail;
542 }
543 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher0a17af372013-10-23 17:22:29 -0400544 if (rdev->pm.dpm.thermal_active) {
545 count = -EINVAL;
546 goto fail;
547 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400548 ret = radeon_dpm_force_performance_level(rdev, level);
549 if (ret)
550 count = -EINVAL;
551 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400552fail:
Alex Deucher0a17af372013-10-23 17:22:29 -0400553 mutex_unlock(&rdev->pm.mutex);
554
Alex Deucher70d01a52013-07-02 18:38:02 -0400555 return count;
556}
557
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300558static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
559 struct device_attribute *attr,
560 char *buf)
561{
562 struct radeon_device *rdev = dev_get_drvdata(dev);
563 u32 pwm_mode = 0;
564
565 if (rdev->asic->dpm.fan_ctrl_get_mode)
566 pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
567
568 /* never 0 (full-speed), fuse or smc-controlled always */
569 return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
570}
571
572static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
573 struct device_attribute *attr,
574 const char *buf,
575 size_t count)
576{
577 struct radeon_device *rdev = dev_get_drvdata(dev);
578 int err;
579 int value;
580
581 if(!rdev->asic->dpm.fan_ctrl_set_mode)
582 return -EINVAL;
583
584 err = kstrtoint(buf, 10, &value);
585 if (err)
586 return err;
587
588 switch(value) {
589 case 1: /* manual, percent-based */
590 rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
591 break;
592 default: /* disable */
593 rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
594 break;
595 }
596
597 return count;
598}
599
600static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
601 struct device_attribute *attr,
602 char *buf)
603{
604 return sprintf(buf, "%i\n", 0);
605}
606
607static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
608 struct device_attribute *attr,
609 char *buf)
610{
611 return sprintf(buf, "%i\n", 100); /* pwm uses percent-based fan-control */
612}
613
614static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
615 struct device_attribute *attr,
616 const char *buf, size_t count)
617{
618 struct radeon_device *rdev = dev_get_drvdata(dev);
619 int err;
620 u32 value;
621
622 err = kstrtou32(buf, 10, &value);
623 if (err)
624 return err;
625
626 err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
627 if (err)
628 return err;
629
630 return count;
631}
632
633static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
634 struct device_attribute *attr,
635 char *buf)
636{
637 struct radeon_device *rdev = dev_get_drvdata(dev);
638 int err;
639 u32 speed;
640
641 err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
642 if (err)
643 return err;
644
645 return sprintf(buf, "%i\n", speed);
646}
647
Alex Deucherce8f5372010-05-07 15:10:16 -0400648static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
649static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400650static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400651static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
652 radeon_get_dpm_forced_performance_level,
653 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400654
Alex Deucher21a81222010-07-02 12:58:16 -0400655static ssize_t radeon_hwmon_show_temp(struct device *dev,
656 struct device_attribute *attr,
657 char *buf)
658{
Guenter Roeckec39f642013-11-22 21:52:00 -0800659 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher4f2f2032014-05-19 19:21:29 -0400660 struct drm_device *ddev = rdev->ddev;
Alex Deucher20d391d2011-02-01 16:12:34 -0500661 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400662
Alex Deucher4f2f2032014-05-19 19:21:29 -0400663 /* Can't get temperature when the card is off */
664 if ((rdev->flags & RADEON_IS_PX) &&
665 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
666 return -EINVAL;
667
Alex Deucher6bd1c382013-06-21 14:38:03 -0400668 if (rdev->asic->pm.get_temperature)
669 temp = radeon_get_temperature(rdev);
670 else
Alex Deucher21a81222010-07-02 12:58:16 -0400671 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400672
673 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
674}
675
Jean Delvare6ea4e842013-09-10 10:32:41 +0200676static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
677 struct device_attribute *attr,
678 char *buf)
679{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300680 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200681 int hyst = to_sensor_dev_attr(attr)->index;
682 int temp;
683
684 if (hyst)
685 temp = rdev->pm.dpm.thermal.min_temp;
686 else
687 temp = rdev->pm.dpm.thermal.max_temp;
688
689 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
690}
691
Alex Deucher21a81222010-07-02 12:58:16 -0400692static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200693static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
694static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300695static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
696static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
697static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
698static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
699
Alex Deucher21a81222010-07-02 12:58:16 -0400700
701static struct attribute *hwmon_attributes[] = {
702 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200703 &sensor_dev_attr_temp1_crit.dev_attr.attr,
704 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300705 &sensor_dev_attr_pwm1.dev_attr.attr,
706 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
707 &sensor_dev_attr_pwm1_min.dev_attr.attr,
708 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400709 NULL
710};
711
Jean Delvare6ea4e842013-09-10 10:32:41 +0200712static umode_t hwmon_attributes_visible(struct kobject *kobj,
713 struct attribute *attr, int index)
714{
715 struct device *dev = container_of(kobj, struct device, kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300716 struct radeon_device *rdev = dev_get_drvdata(dev);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300717 umode_t effective_mode = attr->mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200718
719 /* Skip limit attributes if DPM is not enabled */
720 if (rdev->pm.pm_method != PM_METHOD_DPM &&
721 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
722 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
723 return 0;
724
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300725 /* Skip fan attributes if fan is not present */
726 if (rdev->pm.no_fan &&
727 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
728 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
729 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
730 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
731 return 0;
732
733 /* mask fan attributes if we have no bindings for this asic to expose */
734 if ((!rdev->asic->dpm.get_fan_speed_percent &&
735 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
736 (!rdev->asic->dpm.fan_ctrl_get_mode &&
737 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
738 effective_mode &= ~S_IRUGO;
739
740 if ((!rdev->asic->dpm.set_fan_speed_percent &&
741 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
742 (!rdev->asic->dpm.fan_ctrl_set_mode &&
743 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
744 effective_mode &= ~S_IWUSR;
745
746 /* hide max/min values if we can't both query and manage the fan */
747 if ((!rdev->asic->dpm.set_fan_speed_percent &&
748 !rdev->asic->dpm.get_fan_speed_percent) &&
749 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
750 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
751 return 0;
752
753 return effective_mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200754}
755
Alex Deucher21a81222010-07-02 12:58:16 -0400756static const struct attribute_group hwmon_attrgroup = {
757 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200758 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400759};
760
Guenter Roeckec39f642013-11-22 21:52:00 -0800761static const struct attribute_group *hwmon_groups[] = {
762 &hwmon_attrgroup,
763 NULL
764};
765
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200766static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400767{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200768 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400769
770 switch (rdev->pm.int_thermal_type) {
771 case THERMAL_TYPE_RV6XX:
772 case THERMAL_TYPE_RV770:
773 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400774 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500775 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400776 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400777 case THERMAL_TYPE_CI:
778 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400779 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400780 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400781 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
782 "radeon", rdev,
783 hwmon_groups);
784 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
785 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200786 dev_err(rdev->dev,
787 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200788 }
Alex Deucher21a81222010-07-02 12:58:16 -0400789 break;
790 default:
791 break;
792 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200793
794 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400795}
796
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400797static void radeon_hwmon_fini(struct radeon_device *rdev)
798{
799 if (rdev->pm.int_hwmon_dev)
800 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
801}
802
Alex Deucherda321c82013-04-12 13:55:22 -0400803static void radeon_dpm_thermal_work_handler(struct work_struct *work)
804{
805 struct radeon_device *rdev =
806 container_of(work, struct radeon_device,
807 pm.dpm.thermal.work);
808 /* switch to the thermal state */
809 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
810
811 if (!rdev->pm.dpm_enabled)
812 return;
813
814 if (rdev->asic->pm.get_temperature) {
815 int temp = radeon_get_temperature(rdev);
816
817 if (temp < rdev->pm.dpm.thermal.min_temp)
818 /* switch back the user state */
819 dpm_state = rdev->pm.dpm.user_state;
820 } else {
821 if (rdev->pm.dpm.thermal.high_to_low)
822 /* switch back the user state */
823 dpm_state = rdev->pm.dpm.user_state;
824 }
Alex Deucher60320342013-07-24 14:59:48 -0400825 mutex_lock(&rdev->pm.mutex);
826 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
827 rdev->pm.dpm.thermal_active = true;
828 else
829 rdev->pm.dpm.thermal_active = false;
830 rdev->pm.dpm.state = dpm_state;
831 mutex_unlock(&rdev->pm.mutex);
832
833 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400834}
835
836static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
837 enum radeon_pm_state_type dpm_state)
838{
839 int i;
840 struct radeon_ps *ps;
841 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400842 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
843 true : false;
844
845 /* check if the vblank period is too short to adjust the mclk */
846 if (single_display && rdev->asic->dpm.vblank_too_short) {
847 if (radeon_dpm_vblank_too_short(rdev))
848 single_display = false;
849 }
Alex Deucherda321c82013-04-12 13:55:22 -0400850
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400851 /* certain older asics have a separare 3D performance state,
852 * so try that first if the user selected performance
853 */
854 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
855 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400856 /* balanced states don't exist at the moment */
857 if (dpm_state == POWER_STATE_TYPE_BALANCED)
858 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
859
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400860restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400861 /* Pick the best power state based on current conditions */
862 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
863 ps = &rdev->pm.dpm.ps[i];
864 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
865 switch (dpm_state) {
866 /* user states */
867 case POWER_STATE_TYPE_BATTERY:
868 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
869 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400870 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400871 return ps;
872 } else
873 return ps;
874 }
875 break;
876 case POWER_STATE_TYPE_BALANCED:
877 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
878 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400879 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400880 return ps;
881 } else
882 return ps;
883 }
884 break;
885 case POWER_STATE_TYPE_PERFORMANCE:
886 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
887 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400888 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400889 return ps;
890 } else
891 return ps;
892 }
893 break;
894 /* internal states */
895 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400896 if (rdev->pm.dpm.uvd_ps)
897 return rdev->pm.dpm.uvd_ps;
898 else
899 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400900 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
901 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
902 return ps;
903 break;
904 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
905 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
906 return ps;
907 break;
908 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
909 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
910 return ps;
911 break;
912 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
913 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
914 return ps;
915 break;
916 case POWER_STATE_TYPE_INTERNAL_BOOT:
917 return rdev->pm.dpm.boot_ps;
918 case POWER_STATE_TYPE_INTERNAL_THERMAL:
919 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
920 return ps;
921 break;
922 case POWER_STATE_TYPE_INTERNAL_ACPI:
923 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
924 return ps;
925 break;
926 case POWER_STATE_TYPE_INTERNAL_ULV:
927 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
928 return ps;
929 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400930 case POWER_STATE_TYPE_INTERNAL_3DPERF:
931 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
932 return ps;
933 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400934 default:
935 break;
936 }
937 }
938 /* use a fallback state if we didn't match */
939 switch (dpm_state) {
940 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400941 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
942 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400943 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
944 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
945 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400946 if (rdev->pm.dpm.uvd_ps) {
947 return rdev->pm.dpm.uvd_ps;
948 } else {
949 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
950 goto restart_search;
951 }
Alex Deucherda321c82013-04-12 13:55:22 -0400952 case POWER_STATE_TYPE_INTERNAL_THERMAL:
953 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
954 goto restart_search;
955 case POWER_STATE_TYPE_INTERNAL_ACPI:
956 dpm_state = POWER_STATE_TYPE_BATTERY;
957 goto restart_search;
958 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400959 case POWER_STATE_TYPE_BALANCED:
960 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400961 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
962 goto restart_search;
963 default:
964 break;
965 }
966
967 return NULL;
968}
969
970static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
971{
972 int i;
973 struct radeon_ps *ps;
974 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500975 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400976
977 /* if dpm init failed */
978 if (!rdev->pm.dpm_enabled)
979 return;
980
981 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
982 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400983 if ((!rdev->pm.dpm.thermal_active) &&
984 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400985 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
986 }
987 dpm_state = rdev->pm.dpm.state;
988
989 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
990 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -0500991 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -0400992 else
993 return;
994
Alex Deucherd22b7e42012-11-29 19:27:56 -0500995 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -0400996 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -0400997 /* vce just modifies an existing state so force a change */
998 if (ps->vce_active != rdev->pm.dpm.vce_active)
999 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -05001000 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1001 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
1002 * all we need to do is update the display configuration.
1003 */
1004 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1005 /* update display watermarks based on new power state */
1006 radeon_bandwidth_update(rdev);
1007 /* update displays */
1008 radeon_dpm_display_configuration_changed(rdev);
1009 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1010 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1011 }
1012 return;
1013 } else {
1014 /* for BTC+ if the num crtcs hasn't changed and state is the same,
1015 * nothing to do, if the num crtcs is > 1 and state is the same,
1016 * update display configuration.
1017 */
1018 if (rdev->pm.dpm.new_active_crtcs ==
1019 rdev->pm.dpm.current_active_crtcs) {
1020 return;
1021 } else {
1022 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1023 (rdev->pm.dpm.new_active_crtc_count > 1)) {
1024 /* update display watermarks based on new power state */
1025 radeon_bandwidth_update(rdev);
1026 /* update displays */
1027 radeon_dpm_display_configuration_changed(rdev);
1028 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1029 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1030 return;
1031 }
1032 }
Alex Deucherda321c82013-04-12 13:55:22 -04001033 }
Alex Deucherda321c82013-04-12 13:55:22 -04001034 }
1035
Alex Deucherb62d6282013-08-20 20:29:05 -04001036force:
Alex Deucher033a37d2013-10-23 18:35:43 -04001037 if (radeon_dpm == 1) {
1038 printk("switching from power state:\n");
1039 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1040 printk("switching to power state:\n");
1041 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1042 }
Alex Deucherb62d6282013-08-20 20:29:05 -04001043
Alex Deucherda321c82013-04-12 13:55:22 -04001044 mutex_lock(&rdev->ddev->struct_mutex);
1045 down_write(&rdev->pm.mclk_lock);
1046 mutex_lock(&rdev->ring_lock);
1047
Alex Deucherb62d6282013-08-20 20:29:05 -04001048 /* update whether vce is active */
1049 ps->vce_active = rdev->pm.dpm.vce_active;
1050
Alex Deucher89c9bc52013-01-16 14:40:26 -05001051 ret = radeon_dpm_pre_set_power_state(rdev);
1052 if (ret)
1053 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -05001054
Alex Deucherda321c82013-04-12 13:55:22 -04001055 /* update display watermarks based on new power state */
1056 radeon_bandwidth_update(rdev);
1057 /* update displays */
1058 radeon_dpm_display_configuration_changed(rdev);
1059
1060 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1061 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1062
1063 /* wait for the rings to drain */
1064 for (i = 0; i < RADEON_NUM_RINGS; i++) {
1065 struct radeon_ring *ring = &rdev->ring[i];
1066 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +01001067 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -04001068 }
1069
1070 /* program the new power state */
1071 radeon_dpm_set_power_state(rdev);
1072
1073 /* update current power state */
1074 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1075
Alex Deucher89c9bc52013-01-16 14:40:26 -05001076 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -05001077
Alex Deucher1cd8b212013-09-13 14:07:03 -04001078 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -04001079 if (rdev->pm.dpm.thermal_active) {
1080 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001081 /* force low perf level for thermal */
1082 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -04001083 /* save the user's level */
1084 rdev->pm.dpm.forced_level = level;
1085 } else {
1086 /* otherwise, user selected level */
1087 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
1088 }
Alex Deucher60320342013-07-24 14:59:48 -04001089 }
1090
Alex Deucher84dd1922013-01-16 12:52:04 -05001091done:
Alex Deucherda321c82013-04-12 13:55:22 -04001092 mutex_unlock(&rdev->ring_lock);
1093 up_write(&rdev->pm.mclk_lock);
1094 mutex_unlock(&rdev->ddev->struct_mutex);
1095}
1096
Alex Deucherce3537d2013-07-24 12:12:49 -04001097void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1098{
1099 enum radeon_pm_state_type dpm_state;
1100
Alex Deucher9e9d9762013-07-31 18:13:23 -04001101 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -04001102 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +01001103 /* don't powergate anything if we
1104 have active but pause streams */
1105 enable |= rdev->pm.dpm.sd > 0;
1106 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -04001107 /* enable/disable UVD */
1108 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -04001109 mutex_unlock(&rdev->pm.mutex);
1110 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -04001111 if (enable) {
1112 mutex_lock(&rdev->pm.mutex);
1113 rdev->pm.dpm.uvd_active = true;
Alex Deucher0690a222014-06-07 11:31:25 -04001114 /* disable this for now */
1115#if 0
Alex Deucher9e9d9762013-07-31 18:13:23 -04001116 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1117 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1118 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1119 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1120 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1121 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1122 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1123 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1124 else
Alex Deucher0690a222014-06-07 11:31:25 -04001125#endif
Alex Deucher9e9d9762013-07-31 18:13:23 -04001126 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1127 rdev->pm.dpm.state = dpm_state;
1128 mutex_unlock(&rdev->pm.mutex);
1129 } else {
1130 mutex_lock(&rdev->pm.mutex);
1131 rdev->pm.dpm.uvd_active = false;
1132 mutex_unlock(&rdev->pm.mutex);
1133 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001134
Alex Deucher9e9d9762013-07-31 18:13:23 -04001135 radeon_pm_compute_clocks(rdev);
1136 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001137}
1138
Alex Deucher03afe6f2013-08-23 11:56:26 -04001139void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1140{
1141 if (enable) {
1142 mutex_lock(&rdev->pm.mutex);
1143 rdev->pm.dpm.vce_active = true;
1144 /* XXX select vce level based on ring/task */
1145 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1146 mutex_unlock(&rdev->pm.mutex);
1147 } else {
1148 mutex_lock(&rdev->pm.mutex);
1149 rdev->pm.dpm.vce_active = false;
1150 mutex_unlock(&rdev->pm.mutex);
1151 }
1152
1153 radeon_pm_compute_clocks(rdev);
1154}
1155
Alex Deucherda321c82013-04-12 13:55:22 -04001156static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -05001157{
Alex Deucherce8f5372010-05-07 15:10:16 -04001158 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001159 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001160 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1161 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001162 }
Alex Deucherce8f5372010-05-07 15:10:16 -04001163 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001164
1165 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001166}
1167
Alex Deucherda321c82013-04-12 13:55:22 -04001168static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1169{
1170 mutex_lock(&rdev->pm.mutex);
1171 /* disable dpm */
1172 radeon_dpm_disable(rdev);
1173 /* reset the power state */
1174 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1175 rdev->pm.dpm_enabled = false;
1176 mutex_unlock(&rdev->pm.mutex);
1177}
1178
1179void radeon_pm_suspend(struct radeon_device *rdev)
1180{
1181 if (rdev->pm.pm_method == PM_METHOD_DPM)
1182 radeon_pm_suspend_dpm(rdev);
1183 else
1184 radeon_pm_suspend_old(rdev);
1185}
1186
1187static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001188{
Alex Deuchered18a362011-01-06 21:19:32 -05001189 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001190 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001191 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001192 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001193 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001194 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1195 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001196 if (rdev->pm.default_vddci)
1197 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1198 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001199 if (rdev->pm.default_sclk)
1200 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1201 if (rdev->pm.default_mclk)
1202 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1203 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001204 /* asic init will reset the default power state */
1205 mutex_lock(&rdev->pm.mutex);
1206 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1207 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001208 rdev->pm.current_sclk = rdev->pm.default_sclk;
1209 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001210 if (rdev->pm.power_state) {
1211 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1212 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1213 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001214 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1215 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1216 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001217 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1218 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001219 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001220 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001221 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001222}
1223
Alex Deucherda321c82013-04-12 13:55:22 -04001224static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001225{
Dave Airlie26481fb2010-05-18 19:00:14 +10001226 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001227
Alex Deucherda321c82013-04-12 13:55:22 -04001228 /* asic init will reset to the boot state */
1229 mutex_lock(&rdev->pm.mutex);
1230 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1231 radeon_dpm_setup_asic(rdev);
1232 ret = radeon_dpm_enable(rdev);
1233 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001234 if (ret)
1235 goto dpm_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001236 rdev->pm.dpm_enabled = true;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001237 return;
1238
1239dpm_resume_fail:
1240 DRM_ERROR("radeon: dpm resume failed\n");
1241 if ((rdev->family >= CHIP_BARTS) &&
1242 (rdev->family <= CHIP_CAYMAN) &&
1243 rdev->mc_fw) {
1244 if (rdev->pm.default_vddc)
1245 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1246 SET_VOLTAGE_TYPE_ASIC_VDDC);
1247 if (rdev->pm.default_vddci)
1248 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1249 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1250 if (rdev->pm.default_sclk)
1251 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1252 if (rdev->pm.default_mclk)
1253 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001254 }
1255}
1256
1257void radeon_pm_resume(struct radeon_device *rdev)
1258{
1259 if (rdev->pm.pm_method == PM_METHOD_DPM)
1260 radeon_pm_resume_dpm(rdev);
1261 else
1262 radeon_pm_resume_old(rdev);
1263}
1264
1265static int radeon_pm_init_old(struct radeon_device *rdev)
1266{
1267 int ret;
1268
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001269 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001270 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1271 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1272 rdev->pm.dynpm_can_upclock = true;
1273 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001274 rdev->pm.default_sclk = rdev->clock.default_sclk;
1275 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001276 rdev->pm.current_sclk = rdev->clock.default_sclk;
1277 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001278 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001279
Alex Deucher56278a82009-12-28 13:58:44 -05001280 if (rdev->bios) {
1281 if (rdev->is_atom_bios)
1282 radeon_atombios_get_power_modes(rdev);
1283 else
1284 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001285 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001286 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001287 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001288 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001289 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001290 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001291 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001292 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1293 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001294 if (rdev->pm.default_vddci)
1295 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1296 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001297 if (rdev->pm.default_sclk)
1298 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1299 if (rdev->pm.default_mclk)
1300 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1301 }
Alex Deucher56278a82009-12-28 13:58:44 -05001302 }
1303
Alex Deucher21a81222010-07-02 12:58:16 -04001304 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001305 ret = radeon_hwmon_init(rdev);
1306 if (ret)
1307 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001308
1309 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1310
Alex Deucherce8f5372010-05-07 15:10:16 -04001311 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001312 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001313 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1314 if (ret)
1315 DRM_ERROR("failed to create device file for power profile\n");
1316 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1317 if (ret)
1318 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001319
Alex Deucherce8f5372010-05-07 15:10:16 -04001320 if (radeon_debugfs_pm_init(rdev)) {
1321 DRM_ERROR("Failed to register debugfs file for PM!\n");
1322 }
1323
1324 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001325 }
1326
1327 return 0;
1328}
1329
Alex Deucherda321c82013-04-12 13:55:22 -04001330static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1331{
1332 int i;
1333
1334 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1335 printk("== power state %d ==\n", i);
1336 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1337 }
1338}
1339
1340static int radeon_pm_init_dpm(struct radeon_device *rdev)
1341{
1342 int ret;
1343
Alex Deucher1cd8b212013-09-13 14:07:03 -04001344 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001345 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1346 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001347 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001348 rdev->pm.default_sclk = rdev->clock.default_sclk;
1349 rdev->pm.default_mclk = rdev->clock.default_mclk;
1350 rdev->pm.current_sclk = rdev->clock.default_sclk;
1351 rdev->pm.current_mclk = rdev->clock.default_mclk;
1352 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1353
1354 if (rdev->bios && rdev->is_atom_bios)
1355 radeon_atombios_get_power_modes(rdev);
1356 else
1357 return -EINVAL;
1358
1359 /* set up the internal thermal sensor if applicable */
1360 ret = radeon_hwmon_init(rdev);
1361 if (ret)
1362 return ret;
1363
1364 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1365 mutex_lock(&rdev->pm.mutex);
1366 radeon_dpm_init(rdev);
1367 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001368 if (radeon_dpm == 1)
1369 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001370 radeon_dpm_setup_asic(rdev);
1371 ret = radeon_dpm_enable(rdev);
1372 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001373 if (ret)
1374 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001375 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001376
Alex Deucherbb5abf92013-12-18 13:39:58 -05001377 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1378 if (ret)
1379 DRM_ERROR("failed to create device file for dpm state\n");
1380 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1381 if (ret)
1382 DRM_ERROR("failed to create device file for dpm state\n");
1383 /* XXX: these are noops for dpm but are here for backwards compat */
1384 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1385 if (ret)
1386 DRM_ERROR("failed to create device file for power profile\n");
1387 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1388 if (ret)
1389 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001390
Alex Deucherbb5abf92013-12-18 13:39:58 -05001391 if (radeon_debugfs_pm_init(rdev)) {
1392 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001393 }
1394
Alex Deucherbb5abf92013-12-18 13:39:58 -05001395 DRM_INFO("radeon: dpm initialized\n");
1396
Alex Deucherda321c82013-04-12 13:55:22 -04001397 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001398
1399dpm_failed:
1400 rdev->pm.dpm_enabled = false;
1401 if ((rdev->family >= CHIP_BARTS) &&
1402 (rdev->family <= CHIP_CAYMAN) &&
1403 rdev->mc_fw) {
1404 if (rdev->pm.default_vddc)
1405 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1406 SET_VOLTAGE_TYPE_ASIC_VDDC);
1407 if (rdev->pm.default_vddci)
1408 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1409 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1410 if (rdev->pm.default_sclk)
1411 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1412 if (rdev->pm.default_mclk)
1413 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1414 }
1415 DRM_ERROR("radeon: dpm initialization failed\n");
1416 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001417}
1418
Alex Deucher4369a692015-01-08 10:46:33 -05001419struct radeon_dpm_quirk {
1420 u32 chip_vendor;
1421 u32 chip_device;
1422 u32 subsys_vendor;
1423 u32 subsys_device;
1424};
1425
1426/* cards with dpm stability problems */
1427static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
1428 /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
1429 { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
1430 /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
1431 { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
1432 { 0, 0, 0, 0 },
1433};
1434
Alex Deucherda321c82013-04-12 13:55:22 -04001435int radeon_pm_init(struct radeon_device *rdev)
1436{
Alex Deucher4369a692015-01-08 10:46:33 -05001437 struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
1438 bool disable_dpm = false;
1439
1440 /* Apply dpm quirks */
1441 while (p && p->chip_device != 0) {
1442 if (rdev->pdev->vendor == p->chip_vendor &&
1443 rdev->pdev->device == p->chip_device &&
1444 rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1445 rdev->pdev->subsystem_device == p->subsys_device) {
1446 disable_dpm = true;
1447 break;
1448 }
1449 ++p;
1450 }
1451
Alex Deucherda321c82013-04-12 13:55:22 -04001452 /* enable dpm on rv6xx+ */
1453 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001454 case CHIP_RV610:
1455 case CHIP_RV630:
1456 case CHIP_RV620:
1457 case CHIP_RV635:
1458 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001459 case CHIP_RS780:
1460 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001461 case CHIP_RV770:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001462 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001463 if (!rdev->rlc_fw)
1464 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001465 else if ((rdev->family >= CHIP_RV770) &&
1466 (!(rdev->flags & RADEON_IS_IGP)) &&
1467 (!rdev->smc_fw))
1468 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001469 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001470 rdev->pm.pm_method = PM_METHOD_DPM;
1471 else
1472 rdev->pm.pm_method = PM_METHOD_PROFILE;
1473 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001474 case CHIP_RV730:
1475 case CHIP_RV710:
1476 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001477 case CHIP_CEDAR:
1478 case CHIP_REDWOOD:
1479 case CHIP_JUNIPER:
1480 case CHIP_CYPRESS:
1481 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001482 case CHIP_PALM:
1483 case CHIP_SUMO:
1484 case CHIP_SUMO2:
Alex Deucherc08abf12014-07-14 12:01:40 -04001485 case CHIP_BARTS:
1486 case CHIP_TURKS:
1487 case CHIP_CAICOS:
Alex Deucher8f500af2014-07-07 17:13:37 -04001488 case CHIP_CAYMAN:
Alex Deucher3a118982013-11-14 10:21:29 -05001489 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001490 case CHIP_TAHITI:
1491 case CHIP_PITCAIRN:
1492 case CHIP_VERDE:
1493 case CHIP_OLAND:
1494 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001495 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001496 case CHIP_KABINI:
1497 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001498 case CHIP_HAWAII:
Samuel Li7d032a42014-04-30 18:40:51 -04001499 case CHIP_MULLINS:
Alex Deucher5a16f762013-10-23 17:11:06 -04001500 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1501 if (!rdev->rlc_fw)
1502 rdev->pm.pm_method = PM_METHOD_PROFILE;
1503 else if ((rdev->family >= CHIP_RV770) &&
1504 (!(rdev->flags & RADEON_IS_IGP)) &&
1505 (!rdev->smc_fw))
1506 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher4369a692015-01-08 10:46:33 -05001507 else if (disable_dpm && (radeon_dpm == -1))
1508 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher5a16f762013-10-23 17:11:06 -04001509 else if (radeon_dpm == 0)
1510 rdev->pm.pm_method = PM_METHOD_PROFILE;
1511 else
1512 rdev->pm.pm_method = PM_METHOD_DPM;
1513 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001514 default:
1515 /* default to profile method */
1516 rdev->pm.pm_method = PM_METHOD_PROFILE;
1517 break;
1518 }
1519
1520 if (rdev->pm.pm_method == PM_METHOD_DPM)
1521 return radeon_pm_init_dpm(rdev);
1522 else
1523 return radeon_pm_init_old(rdev);
1524}
1525
Alex Deucher914a8982013-12-19 11:37:22 -05001526int radeon_pm_late_init(struct radeon_device *rdev)
1527{
1528 int ret = 0;
1529
1530 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1531 mutex_lock(&rdev->pm.mutex);
1532 ret = radeon_dpm_late_enable(rdev);
1533 mutex_unlock(&rdev->pm.mutex);
1534 }
1535 return ret;
1536}
1537
Alex Deucherda321c82013-04-12 13:55:22 -04001538static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001539{
Alex Deucherce8f5372010-05-07 15:10:16 -04001540 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001541 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001542 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1543 rdev->pm.profile = PM_PROFILE_DEFAULT;
1544 radeon_pm_update_profile(rdev);
1545 radeon_pm_set_clocks(rdev);
1546 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001547 /* reset default clocks */
1548 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1549 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1550 radeon_pm_set_clocks(rdev);
1551 }
Alex Deuchera4248162010-04-24 14:50:23 -04001552 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001553
1554 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001555
Alex Deucherce8f5372010-05-07 15:10:16 -04001556 device_remove_file(rdev->dev, &dev_attr_power_profile);
1557 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001558 }
Alex Deuchera4248162010-04-24 14:50:23 -04001559
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001560 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001561 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001562}
1563
Alex Deucherda321c82013-04-12 13:55:22 -04001564static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1565{
1566 if (rdev->pm.num_power_states > 1) {
1567 mutex_lock(&rdev->pm.mutex);
1568 radeon_dpm_disable(rdev);
1569 mutex_unlock(&rdev->pm.mutex);
1570
1571 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001572 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001573 /* XXX backwards compat */
1574 device_remove_file(rdev->dev, &dev_attr_power_profile);
1575 device_remove_file(rdev->dev, &dev_attr_power_method);
1576 }
1577 radeon_dpm_fini(rdev);
1578
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001579 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001580 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001581}
1582
1583void radeon_pm_fini(struct radeon_device *rdev)
1584{
1585 if (rdev->pm.pm_method == PM_METHOD_DPM)
1586 radeon_pm_fini_dpm(rdev);
1587 else
1588 radeon_pm_fini_old(rdev);
1589}
1590
1591static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001592{
1593 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001594 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001595 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001596
Alex Deucherce8f5372010-05-07 15:10:16 -04001597 if (rdev->pm.num_power_states < 2)
1598 return;
1599
Rafał Miłeckic913e232009-12-22 23:02:16 +01001600 mutex_lock(&rdev->pm.mutex);
1601
1602 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001603 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001604 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1605 list_for_each_entry(crtc,
1606 &ddev->mode_config.crtc_list, head) {
1607 radeon_crtc = to_radeon_crtc(crtc);
1608 if (radeon_crtc->enabled) {
1609 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1610 rdev->pm.active_crtc_count++;
1611 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001612 }
1613 }
1614
Alex Deucherce8f5372010-05-07 15:10:16 -04001615 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1616 radeon_pm_update_profile(rdev);
1617 radeon_pm_set_clocks(rdev);
1618 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1619 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1620 if (rdev->pm.active_crtc_count > 1) {
1621 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1622 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001623
Alex Deucherce8f5372010-05-07 15:10:16 -04001624 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1625 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1626 radeon_pm_get_dynpm_state(rdev);
1627 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001628
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001629 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001630 }
1631 } else if (rdev->pm.active_crtc_count == 1) {
1632 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001633
Alex Deucherce8f5372010-05-07 15:10:16 -04001634 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1635 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1636 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1637 radeon_pm_get_dynpm_state(rdev);
1638 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001639
Tejun Heo32c87fc2011-01-03 14:49:32 +01001640 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1641 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001642 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1643 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001644 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1645 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001646 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001647 }
1648 } else { /* count == 0 */
1649 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1650 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001651
Alex Deucherce8f5372010-05-07 15:10:16 -04001652 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1653 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1654 radeon_pm_get_dynpm_state(rdev);
1655 radeon_pm_set_clocks(rdev);
1656 }
1657 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001658 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001659 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001660
1661 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001662}
1663
Alex Deucherda321c82013-04-12 13:55:22 -04001664static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1665{
1666 struct drm_device *ddev = rdev->ddev;
1667 struct drm_crtc *crtc;
1668 struct radeon_crtc *radeon_crtc;
1669
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001670 if (!rdev->pm.dpm_enabled)
1671 return;
1672
Alex Deucherda321c82013-04-12 13:55:22 -04001673 mutex_lock(&rdev->pm.mutex);
1674
Alex Deucher5ca302f2012-11-30 10:56:57 -05001675 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001676 rdev->pm.dpm.new_active_crtcs = 0;
1677 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001678 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1679 list_for_each_entry(crtc,
1680 &ddev->mode_config.crtc_list, head) {
1681 radeon_crtc = to_radeon_crtc(crtc);
1682 if (crtc->enabled) {
1683 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1684 rdev->pm.dpm.new_active_crtc_count++;
1685 }
Alex Deucherda321c82013-04-12 13:55:22 -04001686 }
1687 }
1688
Alex Deucher5ca302f2012-11-30 10:56:57 -05001689 /* update battery/ac status */
1690 if (power_supply_is_system_supplied() > 0)
1691 rdev->pm.dpm.ac_power = true;
1692 else
1693 rdev->pm.dpm.ac_power = false;
1694
Alex Deucherda321c82013-04-12 13:55:22 -04001695 radeon_dpm_change_power_state_locked(rdev);
1696
1697 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001698
Alex Deucherda321c82013-04-12 13:55:22 -04001699}
1700
1701void radeon_pm_compute_clocks(struct radeon_device *rdev)
1702{
1703 if (rdev->pm.pm_method == PM_METHOD_DPM)
1704 radeon_pm_compute_clocks_dpm(rdev);
1705 else
1706 radeon_pm_compute_clocks_old(rdev);
1707}
1708
Alex Deucherce8f5372010-05-07 15:10:16 -04001709static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001710{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001711 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001712 bool in_vbl = true;
1713
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001714 /* Iterate over all active crtc's. All crtc's must be in vblank,
1715 * otherwise return in_vbl == false.
1716 */
1717 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1718 if (rdev->pm.active_crtcs & (1 << crtc)) {
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001719 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001720 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
Daniel Vetter3d3cbd82014-09-10 17:36:11 +02001721 !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
Dave Airlief7352612010-02-18 15:58:36 +10001722 in_vbl = false;
1723 }
1724 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001725
1726 return in_vbl;
1727}
1728
Alex Deucherce8f5372010-05-07 15:10:16 -04001729static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001730{
1731 u32 stat_crtc = 0;
1732 bool in_vbl = radeon_pm_in_vbl(rdev);
1733
Dave Airlief7352612010-02-18 15:58:36 +10001734 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001735 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001736 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001737 return in_vbl;
1738}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001739
Alex Deucherce8f5372010-05-07 15:10:16 -04001740static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001741{
1742 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001743 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001744 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001745 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001746
Matthew Garrettd9932a32010-04-26 16:02:26 -04001747 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001748 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001749 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001750 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001751 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001752
Alex Deucher74652802011-08-25 13:39:48 -04001753 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001754 struct radeon_ring *ring = &rdev->ring[i];
1755
1756 if (ring->ready) {
1757 not_processed += radeon_fence_count_emitted(rdev, i);
1758 if (not_processed >= 3)
1759 break;
1760 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001761 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001762
1763 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001764 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1765 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1766 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1767 rdev->pm.dynpm_can_upclock) {
1768 rdev->pm.dynpm_planned_action =
1769 DYNPM_ACTION_UPCLOCK;
1770 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001771 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1772 }
1773 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001774 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1775 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1776 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1777 rdev->pm.dynpm_can_downclock) {
1778 rdev->pm.dynpm_planned_action =
1779 DYNPM_ACTION_DOWNCLOCK;
1780 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001781 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1782 }
1783 }
1784
Alex Deucherd7311172010-05-03 01:13:14 -04001785 /* Note, radeon_pm_set_clocks is called with static_switch set
1786 * to false since we want to wait for vbl to avoid flicker.
1787 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001788 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1789 jiffies > rdev->pm.dynpm_action_timeout) {
1790 radeon_pm_get_dynpm_state(rdev);
1791 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001792 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001793
Tejun Heo32c87fc2011-01-03 14:49:32 +01001794 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1795 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001796 }
1797 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001798 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001799}
1800
Rafał Miłecki74338742009-11-03 00:53:02 +01001801/*
1802 * Debugfs info
1803 */
1804#if defined(CONFIG_DEBUG_FS)
1805
1806static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1807{
1808 struct drm_info_node *node = (struct drm_info_node *) m->private;
1809 struct drm_device *dev = node->minor->dev;
1810 struct radeon_device *rdev = dev->dev_private;
Alex Deucher4f2f2032014-05-19 19:21:29 -04001811 struct drm_device *ddev = rdev->ddev;
Rafał Miłecki74338742009-11-03 00:53:02 +01001812
Alex Deucher4f2f2032014-05-19 19:21:29 -04001813 if ((rdev->flags & RADEON_IS_PX) &&
1814 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1815 seq_printf(m, "PX asic powered off\n");
1816 } else if (rdev->pm.dpm_enabled) {
Alex Deucher1316b792013-06-28 09:28:39 -04001817 mutex_lock(&rdev->pm.mutex);
1818 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1819 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1820 else
Alex Deucher71375922013-07-02 09:11:39 -04001821 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001822 mutex_unlock(&rdev->pm.mutex);
1823 } else {
1824 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1825 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1826 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1827 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1828 else
1829 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1830 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1831 if (rdev->asic->pm.get_memory_clock)
1832 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1833 if (rdev->pm.current_vddc)
1834 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1835 if (rdev->asic->pm.get_pcie_lanes)
1836 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1837 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001838
1839 return 0;
1840}
1841
1842static struct drm_info_list radeon_pm_info_list[] = {
1843 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1844};
1845#endif
1846
Rafał Miłeckic913e232009-12-22 23:02:16 +01001847static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001848{
1849#if defined(CONFIG_DEBUG_FS)
1850 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1851#else
1852 return 0;
1853#endif
1854}