blob: 33cf4108386dbba4ef70a0e372eb992d1ff7e4d3 [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
Alex Deucher082452e2015-02-04 17:18:55 -0500588 switch (value) {
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300589 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{
Alex Deucher082452e2015-02-04 17:18:55 -0500611 return sprintf(buf, "%i\n", 255);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300612}
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
Alex Deucher082452e2015-02-04 17:18:55 -0500626 value = (value * 100) / 255;
627
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300628 err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
629 if (err)
630 return err;
631
632 return count;
633}
634
635static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
636 struct device_attribute *attr,
637 char *buf)
638{
639 struct radeon_device *rdev = dev_get_drvdata(dev);
640 int err;
641 u32 speed;
642
643 err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
644 if (err)
645 return err;
646
Alex Deucher082452e2015-02-04 17:18:55 -0500647 speed = (speed * 255) / 100;
648
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300649 return sprintf(buf, "%i\n", speed);
650}
651
Alex Deucherce8f5372010-05-07 15:10:16 -0400652static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
653static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400654static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400655static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
656 radeon_get_dpm_forced_performance_level,
657 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400658
Alex Deucher21a81222010-07-02 12:58:16 -0400659static ssize_t radeon_hwmon_show_temp(struct device *dev,
660 struct device_attribute *attr,
661 char *buf)
662{
Guenter Roeckec39f642013-11-22 21:52:00 -0800663 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher4f2f2032014-05-19 19:21:29 -0400664 struct drm_device *ddev = rdev->ddev;
Alex Deucher20d391d2011-02-01 16:12:34 -0500665 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400666
Alex Deucher4f2f2032014-05-19 19:21:29 -0400667 /* Can't get temperature when the card is off */
668 if ((rdev->flags & RADEON_IS_PX) &&
669 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
670 return -EINVAL;
671
Alex Deucher6bd1c382013-06-21 14:38:03 -0400672 if (rdev->asic->pm.get_temperature)
673 temp = radeon_get_temperature(rdev);
674 else
Alex Deucher21a81222010-07-02 12:58:16 -0400675 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400676
677 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
678}
679
Jean Delvare6ea4e842013-09-10 10:32:41 +0200680static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
681 struct device_attribute *attr,
682 char *buf)
683{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300684 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200685 int hyst = to_sensor_dev_attr(attr)->index;
686 int temp;
687
688 if (hyst)
689 temp = rdev->pm.dpm.thermal.min_temp;
690 else
691 temp = rdev->pm.dpm.thermal.max_temp;
692
693 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
694}
695
Alex Deucher21a81222010-07-02 12:58:16 -0400696static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200697static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
698static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300699static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
700static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
701static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
702static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
703
Alex Deucher21a81222010-07-02 12:58:16 -0400704
705static struct attribute *hwmon_attributes[] = {
706 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200707 &sensor_dev_attr_temp1_crit.dev_attr.attr,
708 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300709 &sensor_dev_attr_pwm1.dev_attr.attr,
710 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
711 &sensor_dev_attr_pwm1_min.dev_attr.attr,
712 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400713 NULL
714};
715
Jean Delvare6ea4e842013-09-10 10:32:41 +0200716static umode_t hwmon_attributes_visible(struct kobject *kobj,
717 struct attribute *attr, int index)
718{
719 struct device *dev = container_of(kobj, struct device, kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300720 struct radeon_device *rdev = dev_get_drvdata(dev);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300721 umode_t effective_mode = attr->mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200722
723 /* Skip limit attributes if DPM is not enabled */
724 if (rdev->pm.pm_method != PM_METHOD_DPM &&
725 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
726 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
727 return 0;
728
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300729 /* Skip fan attributes if fan is not present */
730 if (rdev->pm.no_fan &&
731 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
732 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
733 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
734 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
735 return 0;
736
737 /* mask fan attributes if we have no bindings for this asic to expose */
738 if ((!rdev->asic->dpm.get_fan_speed_percent &&
739 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
740 (!rdev->asic->dpm.fan_ctrl_get_mode &&
741 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
742 effective_mode &= ~S_IRUGO;
743
744 if ((!rdev->asic->dpm.set_fan_speed_percent &&
745 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
746 (!rdev->asic->dpm.fan_ctrl_set_mode &&
747 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
748 effective_mode &= ~S_IWUSR;
749
750 /* hide max/min values if we can't both query and manage the fan */
751 if ((!rdev->asic->dpm.set_fan_speed_percent &&
752 !rdev->asic->dpm.get_fan_speed_percent) &&
753 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
754 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
755 return 0;
756
757 return effective_mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200758}
759
Alex Deucher21a81222010-07-02 12:58:16 -0400760static const struct attribute_group hwmon_attrgroup = {
761 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200762 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400763};
764
Guenter Roeckec39f642013-11-22 21:52:00 -0800765static const struct attribute_group *hwmon_groups[] = {
766 &hwmon_attrgroup,
767 NULL
768};
769
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200770static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400771{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200772 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400773
774 switch (rdev->pm.int_thermal_type) {
775 case THERMAL_TYPE_RV6XX:
776 case THERMAL_TYPE_RV770:
777 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400778 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500779 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400780 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400781 case THERMAL_TYPE_CI:
782 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400783 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400784 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400785 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
786 "radeon", rdev,
787 hwmon_groups);
788 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
789 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200790 dev_err(rdev->dev,
791 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200792 }
Alex Deucher21a81222010-07-02 12:58:16 -0400793 break;
794 default:
795 break;
796 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200797
798 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400799}
800
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400801static void radeon_hwmon_fini(struct radeon_device *rdev)
802{
803 if (rdev->pm.int_hwmon_dev)
804 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
805}
806
Alex Deucherda321c82013-04-12 13:55:22 -0400807static void radeon_dpm_thermal_work_handler(struct work_struct *work)
808{
809 struct radeon_device *rdev =
810 container_of(work, struct radeon_device,
811 pm.dpm.thermal.work);
812 /* switch to the thermal state */
813 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
814
815 if (!rdev->pm.dpm_enabled)
816 return;
817
818 if (rdev->asic->pm.get_temperature) {
819 int temp = radeon_get_temperature(rdev);
820
821 if (temp < rdev->pm.dpm.thermal.min_temp)
822 /* switch back the user state */
823 dpm_state = rdev->pm.dpm.user_state;
824 } else {
825 if (rdev->pm.dpm.thermal.high_to_low)
826 /* switch back the user state */
827 dpm_state = rdev->pm.dpm.user_state;
828 }
Alex Deucher60320342013-07-24 14:59:48 -0400829 mutex_lock(&rdev->pm.mutex);
830 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
831 rdev->pm.dpm.thermal_active = true;
832 else
833 rdev->pm.dpm.thermal_active = false;
834 rdev->pm.dpm.state = dpm_state;
835 mutex_unlock(&rdev->pm.mutex);
836
837 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400838}
839
840static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
841 enum radeon_pm_state_type dpm_state)
842{
843 int i;
844 struct radeon_ps *ps;
845 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400846 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
847 true : false;
848
849 /* check if the vblank period is too short to adjust the mclk */
850 if (single_display && rdev->asic->dpm.vblank_too_short) {
851 if (radeon_dpm_vblank_too_short(rdev))
852 single_display = false;
853 }
Alex Deucherda321c82013-04-12 13:55:22 -0400854
Alex Deucher951caa62015-02-18 00:59:45 -0500855 /* 120hz tends to be problematic even if they are under the
856 * vblank limit.
857 */
858 if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
859 single_display = false;
860
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400861 /* certain older asics have a separare 3D performance state,
862 * so try that first if the user selected performance
863 */
864 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
865 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400866 /* balanced states don't exist at the moment */
867 if (dpm_state == POWER_STATE_TYPE_BALANCED)
868 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
869
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400870restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400871 /* Pick the best power state based on current conditions */
872 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
873 ps = &rdev->pm.dpm.ps[i];
874 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
875 switch (dpm_state) {
876 /* user states */
877 case POWER_STATE_TYPE_BATTERY:
878 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
879 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400880 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400881 return ps;
882 } else
883 return ps;
884 }
885 break;
886 case POWER_STATE_TYPE_BALANCED:
887 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
888 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400889 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400890 return ps;
891 } else
892 return ps;
893 }
894 break;
895 case POWER_STATE_TYPE_PERFORMANCE:
896 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
897 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400898 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400899 return ps;
900 } else
901 return ps;
902 }
903 break;
904 /* internal states */
905 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400906 if (rdev->pm.dpm.uvd_ps)
907 return rdev->pm.dpm.uvd_ps;
908 else
909 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400910 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
911 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
912 return ps;
913 break;
914 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
915 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
916 return ps;
917 break;
918 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
919 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
920 return ps;
921 break;
922 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
923 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
924 return ps;
925 break;
926 case POWER_STATE_TYPE_INTERNAL_BOOT:
927 return rdev->pm.dpm.boot_ps;
928 case POWER_STATE_TYPE_INTERNAL_THERMAL:
929 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
930 return ps;
931 break;
932 case POWER_STATE_TYPE_INTERNAL_ACPI:
933 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
934 return ps;
935 break;
936 case POWER_STATE_TYPE_INTERNAL_ULV:
937 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
938 return ps;
939 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400940 case POWER_STATE_TYPE_INTERNAL_3DPERF:
941 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
942 return ps;
943 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400944 default:
945 break;
946 }
947 }
948 /* use a fallback state if we didn't match */
949 switch (dpm_state) {
950 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400951 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
952 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400953 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
954 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
955 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400956 if (rdev->pm.dpm.uvd_ps) {
957 return rdev->pm.dpm.uvd_ps;
958 } else {
959 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
960 goto restart_search;
961 }
Alex Deucherda321c82013-04-12 13:55:22 -0400962 case POWER_STATE_TYPE_INTERNAL_THERMAL:
963 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
964 goto restart_search;
965 case POWER_STATE_TYPE_INTERNAL_ACPI:
966 dpm_state = POWER_STATE_TYPE_BATTERY;
967 goto restart_search;
968 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400969 case POWER_STATE_TYPE_BALANCED:
970 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400971 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
972 goto restart_search;
973 default:
974 break;
975 }
976
977 return NULL;
978}
979
980static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
981{
982 int i;
983 struct radeon_ps *ps;
984 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500985 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400986
987 /* if dpm init failed */
988 if (!rdev->pm.dpm_enabled)
989 return;
990
991 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
992 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400993 if ((!rdev->pm.dpm.thermal_active) &&
994 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400995 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
996 }
997 dpm_state = rdev->pm.dpm.state;
998
999 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1000 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -05001001 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -04001002 else
1003 return;
1004
Alex Deucherd22b7e42012-11-29 19:27:56 -05001005 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -04001006 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -04001007 /* vce just modifies an existing state so force a change */
1008 if (ps->vce_active != rdev->pm.dpm.vce_active)
1009 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -05001010 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1011 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
1012 * all we need to do is update the display configuration.
1013 */
1014 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1015 /* update display watermarks based on new power state */
1016 radeon_bandwidth_update(rdev);
1017 /* update displays */
1018 radeon_dpm_display_configuration_changed(rdev);
1019 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1020 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1021 }
1022 return;
1023 } else {
1024 /* for BTC+ if the num crtcs hasn't changed and state is the same,
1025 * nothing to do, if the num crtcs is > 1 and state is the same,
1026 * update display configuration.
1027 */
1028 if (rdev->pm.dpm.new_active_crtcs ==
1029 rdev->pm.dpm.current_active_crtcs) {
1030 return;
1031 } else {
1032 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1033 (rdev->pm.dpm.new_active_crtc_count > 1)) {
1034 /* update display watermarks based on new power state */
1035 radeon_bandwidth_update(rdev);
1036 /* update displays */
1037 radeon_dpm_display_configuration_changed(rdev);
1038 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1039 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1040 return;
1041 }
1042 }
Alex Deucherda321c82013-04-12 13:55:22 -04001043 }
Alex Deucherda321c82013-04-12 13:55:22 -04001044 }
1045
Alex Deucherb62d6282013-08-20 20:29:05 -04001046force:
Alex Deucher033a37d2013-10-23 18:35:43 -04001047 if (radeon_dpm == 1) {
1048 printk("switching from power state:\n");
1049 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1050 printk("switching to power state:\n");
1051 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1052 }
Alex Deucherb62d6282013-08-20 20:29:05 -04001053
Alex Deucherda321c82013-04-12 13:55:22 -04001054 mutex_lock(&rdev->ddev->struct_mutex);
1055 down_write(&rdev->pm.mclk_lock);
1056 mutex_lock(&rdev->ring_lock);
1057
Alex Deucherb62d6282013-08-20 20:29:05 -04001058 /* update whether vce is active */
1059 ps->vce_active = rdev->pm.dpm.vce_active;
1060
Alex Deucher89c9bc52013-01-16 14:40:26 -05001061 ret = radeon_dpm_pre_set_power_state(rdev);
1062 if (ret)
1063 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -05001064
Alex Deucherda321c82013-04-12 13:55:22 -04001065 /* update display watermarks based on new power state */
1066 radeon_bandwidth_update(rdev);
1067 /* update displays */
1068 radeon_dpm_display_configuration_changed(rdev);
1069
1070 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1071 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1072
1073 /* wait for the rings to drain */
1074 for (i = 0; i < RADEON_NUM_RINGS; i++) {
1075 struct radeon_ring *ring = &rdev->ring[i];
1076 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +01001077 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -04001078 }
1079
1080 /* program the new power state */
1081 radeon_dpm_set_power_state(rdev);
1082
1083 /* update current power state */
1084 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1085
Alex Deucher89c9bc52013-01-16 14:40:26 -05001086 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -05001087
Alex Deucher1cd8b212013-09-13 14:07:03 -04001088 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -04001089 if (rdev->pm.dpm.thermal_active) {
1090 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001091 /* force low perf level for thermal */
1092 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -04001093 /* save the user's level */
1094 rdev->pm.dpm.forced_level = level;
1095 } else {
1096 /* otherwise, user selected level */
1097 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
1098 }
Alex Deucher60320342013-07-24 14:59:48 -04001099 }
1100
Alex Deucher84dd1922013-01-16 12:52:04 -05001101done:
Alex Deucherda321c82013-04-12 13:55:22 -04001102 mutex_unlock(&rdev->ring_lock);
1103 up_write(&rdev->pm.mclk_lock);
1104 mutex_unlock(&rdev->ddev->struct_mutex);
1105}
1106
Alex Deucherce3537d2013-07-24 12:12:49 -04001107void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1108{
1109 enum radeon_pm_state_type dpm_state;
1110
Alex Deucher9e9d9762013-07-31 18:13:23 -04001111 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -04001112 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +01001113 /* don't powergate anything if we
1114 have active but pause streams */
1115 enable |= rdev->pm.dpm.sd > 0;
1116 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -04001117 /* enable/disable UVD */
1118 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -04001119 mutex_unlock(&rdev->pm.mutex);
1120 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -04001121 if (enable) {
1122 mutex_lock(&rdev->pm.mutex);
1123 rdev->pm.dpm.uvd_active = true;
Alex Deucher0690a222014-06-07 11:31:25 -04001124 /* disable this for now */
1125#if 0
Alex Deucher9e9d9762013-07-31 18:13:23 -04001126 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1127 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1128 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1129 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1130 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1131 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1132 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1133 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1134 else
Alex Deucher0690a222014-06-07 11:31:25 -04001135#endif
Alex Deucher9e9d9762013-07-31 18:13:23 -04001136 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1137 rdev->pm.dpm.state = dpm_state;
1138 mutex_unlock(&rdev->pm.mutex);
1139 } else {
1140 mutex_lock(&rdev->pm.mutex);
1141 rdev->pm.dpm.uvd_active = false;
1142 mutex_unlock(&rdev->pm.mutex);
1143 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001144
Alex Deucher9e9d9762013-07-31 18:13:23 -04001145 radeon_pm_compute_clocks(rdev);
1146 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001147}
1148
Alex Deucher03afe6f2013-08-23 11:56:26 -04001149void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1150{
1151 if (enable) {
1152 mutex_lock(&rdev->pm.mutex);
1153 rdev->pm.dpm.vce_active = true;
1154 /* XXX select vce level based on ring/task */
1155 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1156 mutex_unlock(&rdev->pm.mutex);
1157 } else {
1158 mutex_lock(&rdev->pm.mutex);
1159 rdev->pm.dpm.vce_active = false;
1160 mutex_unlock(&rdev->pm.mutex);
1161 }
1162
1163 radeon_pm_compute_clocks(rdev);
1164}
1165
Alex Deucherda321c82013-04-12 13:55:22 -04001166static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -05001167{
Alex Deucherce8f5372010-05-07 15:10:16 -04001168 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001169 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001170 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1171 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001172 }
Alex Deucherce8f5372010-05-07 15:10:16 -04001173 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001174
1175 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001176}
1177
Alex Deucherda321c82013-04-12 13:55:22 -04001178static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1179{
1180 mutex_lock(&rdev->pm.mutex);
1181 /* disable dpm */
1182 radeon_dpm_disable(rdev);
1183 /* reset the power state */
1184 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1185 rdev->pm.dpm_enabled = false;
1186 mutex_unlock(&rdev->pm.mutex);
1187}
1188
1189void radeon_pm_suspend(struct radeon_device *rdev)
1190{
1191 if (rdev->pm.pm_method == PM_METHOD_DPM)
1192 radeon_pm_suspend_dpm(rdev);
1193 else
1194 radeon_pm_suspend_old(rdev);
1195}
1196
1197static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001198{
Alex Deuchered18a362011-01-06 21:19:32 -05001199 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001200 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001201 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001202 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001203 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001204 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1205 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001206 if (rdev->pm.default_vddci)
1207 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1208 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001209 if (rdev->pm.default_sclk)
1210 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1211 if (rdev->pm.default_mclk)
1212 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1213 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001214 /* asic init will reset the default power state */
1215 mutex_lock(&rdev->pm.mutex);
1216 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1217 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001218 rdev->pm.current_sclk = rdev->pm.default_sclk;
1219 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001220 if (rdev->pm.power_state) {
1221 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1222 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1223 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001224 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1225 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1226 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001227 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1228 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001229 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001230 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001231 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001232}
1233
Alex Deucherda321c82013-04-12 13:55:22 -04001234static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001235{
Dave Airlie26481fb2010-05-18 19:00:14 +10001236 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001237
Alex Deucherda321c82013-04-12 13:55:22 -04001238 /* asic init will reset to the boot state */
1239 mutex_lock(&rdev->pm.mutex);
1240 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1241 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_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001246 rdev->pm.dpm_enabled = true;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001247 return;
1248
1249dpm_resume_fail:
1250 DRM_ERROR("radeon: dpm resume failed\n");
1251 if ((rdev->family >= CHIP_BARTS) &&
1252 (rdev->family <= CHIP_CAYMAN) &&
1253 rdev->mc_fw) {
1254 if (rdev->pm.default_vddc)
1255 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1256 SET_VOLTAGE_TYPE_ASIC_VDDC);
1257 if (rdev->pm.default_vddci)
1258 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1259 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1260 if (rdev->pm.default_sclk)
1261 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1262 if (rdev->pm.default_mclk)
1263 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001264 }
1265}
1266
1267void radeon_pm_resume(struct radeon_device *rdev)
1268{
1269 if (rdev->pm.pm_method == PM_METHOD_DPM)
1270 radeon_pm_resume_dpm(rdev);
1271 else
1272 radeon_pm_resume_old(rdev);
1273}
1274
1275static int radeon_pm_init_old(struct radeon_device *rdev)
1276{
1277 int ret;
1278
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001279 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001280 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1281 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1282 rdev->pm.dynpm_can_upclock = true;
1283 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001284 rdev->pm.default_sclk = rdev->clock.default_sclk;
1285 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001286 rdev->pm.current_sclk = rdev->clock.default_sclk;
1287 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001288 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001289
Alex Deucher56278a82009-12-28 13:58:44 -05001290 if (rdev->bios) {
1291 if (rdev->is_atom_bios)
1292 radeon_atombios_get_power_modes(rdev);
1293 else
1294 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001295 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001296 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001297 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001298 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001299 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001300 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001301 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001302 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1303 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001304 if (rdev->pm.default_vddci)
1305 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1306 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001307 if (rdev->pm.default_sclk)
1308 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1309 if (rdev->pm.default_mclk)
1310 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1311 }
Alex Deucher56278a82009-12-28 13:58:44 -05001312 }
1313
Alex Deucher21a81222010-07-02 12:58:16 -04001314 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001315 ret = radeon_hwmon_init(rdev);
1316 if (ret)
1317 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001318
1319 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1320
Alex Deucherce8f5372010-05-07 15:10:16 -04001321 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001322 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001323 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1324 if (ret)
1325 DRM_ERROR("failed to create device file for power profile\n");
1326 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1327 if (ret)
1328 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001329
Alex Deucherce8f5372010-05-07 15:10:16 -04001330 if (radeon_debugfs_pm_init(rdev)) {
1331 DRM_ERROR("Failed to register debugfs file for PM!\n");
1332 }
1333
1334 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001335 }
1336
1337 return 0;
1338}
1339
Alex Deucherda321c82013-04-12 13:55:22 -04001340static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1341{
1342 int i;
1343
1344 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1345 printk("== power state %d ==\n", i);
1346 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1347 }
1348}
1349
1350static int radeon_pm_init_dpm(struct radeon_device *rdev)
1351{
1352 int ret;
1353
Alex Deucher1cd8b212013-09-13 14:07:03 -04001354 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001355 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1356 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001357 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001358 rdev->pm.default_sclk = rdev->clock.default_sclk;
1359 rdev->pm.default_mclk = rdev->clock.default_mclk;
1360 rdev->pm.current_sclk = rdev->clock.default_sclk;
1361 rdev->pm.current_mclk = rdev->clock.default_mclk;
1362 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1363
1364 if (rdev->bios && rdev->is_atom_bios)
1365 radeon_atombios_get_power_modes(rdev);
1366 else
1367 return -EINVAL;
1368
1369 /* set up the internal thermal sensor if applicable */
1370 ret = radeon_hwmon_init(rdev);
1371 if (ret)
1372 return ret;
1373
1374 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1375 mutex_lock(&rdev->pm.mutex);
1376 radeon_dpm_init(rdev);
1377 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001378 if (radeon_dpm == 1)
1379 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001380 radeon_dpm_setup_asic(rdev);
1381 ret = radeon_dpm_enable(rdev);
1382 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001383 if (ret)
1384 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001385 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001386
Alex Deucherbb5abf92013-12-18 13:39:58 -05001387 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1388 if (ret)
1389 DRM_ERROR("failed to create device file for dpm state\n");
1390 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1391 if (ret)
1392 DRM_ERROR("failed to create device file for dpm state\n");
1393 /* XXX: these are noops for dpm but are here for backwards compat */
1394 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1395 if (ret)
1396 DRM_ERROR("failed to create device file for power profile\n");
1397 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1398 if (ret)
1399 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001400
Alex Deucherbb5abf92013-12-18 13:39:58 -05001401 if (radeon_debugfs_pm_init(rdev)) {
1402 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001403 }
1404
Alex Deucherbb5abf92013-12-18 13:39:58 -05001405 DRM_INFO("radeon: dpm initialized\n");
1406
Alex Deucherda321c82013-04-12 13:55:22 -04001407 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001408
1409dpm_failed:
1410 rdev->pm.dpm_enabled = false;
1411 if ((rdev->family >= CHIP_BARTS) &&
1412 (rdev->family <= CHIP_CAYMAN) &&
1413 rdev->mc_fw) {
1414 if (rdev->pm.default_vddc)
1415 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1416 SET_VOLTAGE_TYPE_ASIC_VDDC);
1417 if (rdev->pm.default_vddci)
1418 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1419 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1420 if (rdev->pm.default_sclk)
1421 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1422 if (rdev->pm.default_mclk)
1423 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1424 }
1425 DRM_ERROR("radeon: dpm initialization failed\n");
1426 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001427}
1428
Alex Deucher4369a692015-01-08 10:46:33 -05001429struct radeon_dpm_quirk {
1430 u32 chip_vendor;
1431 u32 chip_device;
1432 u32 subsys_vendor;
1433 u32 subsys_device;
1434};
1435
1436/* cards with dpm stability problems */
1437static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
1438 /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
1439 { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
1440 /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
1441 { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
1442 { 0, 0, 0, 0 },
1443};
1444
Alex Deucherda321c82013-04-12 13:55:22 -04001445int radeon_pm_init(struct radeon_device *rdev)
1446{
Alex Deucher4369a692015-01-08 10:46:33 -05001447 struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
1448 bool disable_dpm = false;
1449
1450 /* Apply dpm quirks */
1451 while (p && p->chip_device != 0) {
1452 if (rdev->pdev->vendor == p->chip_vendor &&
1453 rdev->pdev->device == p->chip_device &&
1454 rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1455 rdev->pdev->subsystem_device == p->subsys_device) {
1456 disable_dpm = true;
1457 break;
1458 }
1459 ++p;
1460 }
1461
Alex Deucherda321c82013-04-12 13:55:22 -04001462 /* enable dpm on rv6xx+ */
1463 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001464 case CHIP_RV610:
1465 case CHIP_RV630:
1466 case CHIP_RV620:
1467 case CHIP_RV635:
1468 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001469 case CHIP_RS780:
1470 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001471 case CHIP_RV770:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001472 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001473 if (!rdev->rlc_fw)
1474 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001475 else if ((rdev->family >= CHIP_RV770) &&
1476 (!(rdev->flags & RADEON_IS_IGP)) &&
1477 (!rdev->smc_fw))
1478 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001479 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001480 rdev->pm.pm_method = PM_METHOD_DPM;
1481 else
1482 rdev->pm.pm_method = PM_METHOD_PROFILE;
1483 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001484 case CHIP_RV730:
1485 case CHIP_RV710:
1486 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001487 case CHIP_CEDAR:
1488 case CHIP_REDWOOD:
1489 case CHIP_JUNIPER:
1490 case CHIP_CYPRESS:
1491 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001492 case CHIP_PALM:
1493 case CHIP_SUMO:
1494 case CHIP_SUMO2:
Alex Deucherc08abf12014-07-14 12:01:40 -04001495 case CHIP_BARTS:
1496 case CHIP_TURKS:
1497 case CHIP_CAICOS:
Alex Deucher8f500af2014-07-07 17:13:37 -04001498 case CHIP_CAYMAN:
Alex Deucher3a118982013-11-14 10:21:29 -05001499 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001500 case CHIP_TAHITI:
1501 case CHIP_PITCAIRN:
1502 case CHIP_VERDE:
1503 case CHIP_OLAND:
1504 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001505 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001506 case CHIP_KABINI:
1507 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001508 case CHIP_HAWAII:
Samuel Li7d032a42014-04-30 18:40:51 -04001509 case CHIP_MULLINS:
Alex Deucher5a16f762013-10-23 17:11:06 -04001510 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1511 if (!rdev->rlc_fw)
1512 rdev->pm.pm_method = PM_METHOD_PROFILE;
1513 else if ((rdev->family >= CHIP_RV770) &&
1514 (!(rdev->flags & RADEON_IS_IGP)) &&
1515 (!rdev->smc_fw))
1516 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher4369a692015-01-08 10:46:33 -05001517 else if (disable_dpm && (radeon_dpm == -1))
1518 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher5a16f762013-10-23 17:11:06 -04001519 else if (radeon_dpm == 0)
1520 rdev->pm.pm_method = PM_METHOD_PROFILE;
1521 else
1522 rdev->pm.pm_method = PM_METHOD_DPM;
1523 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001524 default:
1525 /* default to profile method */
1526 rdev->pm.pm_method = PM_METHOD_PROFILE;
1527 break;
1528 }
1529
1530 if (rdev->pm.pm_method == PM_METHOD_DPM)
1531 return radeon_pm_init_dpm(rdev);
1532 else
1533 return radeon_pm_init_old(rdev);
1534}
1535
Alex Deucher914a8982013-12-19 11:37:22 -05001536int radeon_pm_late_init(struct radeon_device *rdev)
1537{
1538 int ret = 0;
1539
1540 if (rdev->pm.pm_method == PM_METHOD_DPM) {
1541 mutex_lock(&rdev->pm.mutex);
1542 ret = radeon_dpm_late_enable(rdev);
1543 mutex_unlock(&rdev->pm.mutex);
1544 }
1545 return ret;
1546}
1547
Alex Deucherda321c82013-04-12 13:55:22 -04001548static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001549{
Alex Deucherce8f5372010-05-07 15:10:16 -04001550 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001551 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001552 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1553 rdev->pm.profile = PM_PROFILE_DEFAULT;
1554 radeon_pm_update_profile(rdev);
1555 radeon_pm_set_clocks(rdev);
1556 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001557 /* reset default clocks */
1558 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1559 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1560 radeon_pm_set_clocks(rdev);
1561 }
Alex Deuchera4248162010-04-24 14:50:23 -04001562 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001563
1564 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001565
Alex Deucherce8f5372010-05-07 15:10:16 -04001566 device_remove_file(rdev->dev, &dev_attr_power_profile);
1567 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001568 }
Alex Deuchera4248162010-04-24 14:50:23 -04001569
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001570 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001571 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001572}
1573
Alex Deucherda321c82013-04-12 13:55:22 -04001574static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1575{
1576 if (rdev->pm.num_power_states > 1) {
1577 mutex_lock(&rdev->pm.mutex);
1578 radeon_dpm_disable(rdev);
1579 mutex_unlock(&rdev->pm.mutex);
1580
1581 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001582 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001583 /* XXX backwards compat */
1584 device_remove_file(rdev->dev, &dev_attr_power_profile);
1585 device_remove_file(rdev->dev, &dev_attr_power_method);
1586 }
1587 radeon_dpm_fini(rdev);
1588
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001589 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001590 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001591}
1592
1593void radeon_pm_fini(struct radeon_device *rdev)
1594{
1595 if (rdev->pm.pm_method == PM_METHOD_DPM)
1596 radeon_pm_fini_dpm(rdev);
1597 else
1598 radeon_pm_fini_old(rdev);
1599}
1600
1601static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001602{
1603 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001604 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001605 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001606
Alex Deucherce8f5372010-05-07 15:10:16 -04001607 if (rdev->pm.num_power_states < 2)
1608 return;
1609
Rafał Miłeckic913e232009-12-22 23:02:16 +01001610 mutex_lock(&rdev->pm.mutex);
1611
1612 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001613 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001614 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1615 list_for_each_entry(crtc,
1616 &ddev->mode_config.crtc_list, head) {
1617 radeon_crtc = to_radeon_crtc(crtc);
1618 if (radeon_crtc->enabled) {
1619 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1620 rdev->pm.active_crtc_count++;
1621 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001622 }
1623 }
1624
Alex Deucherce8f5372010-05-07 15:10:16 -04001625 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1626 radeon_pm_update_profile(rdev);
1627 radeon_pm_set_clocks(rdev);
1628 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1629 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1630 if (rdev->pm.active_crtc_count > 1) {
1631 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1632 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001633
Alex Deucherce8f5372010-05-07 15:10:16 -04001634 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1635 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1636 radeon_pm_get_dynpm_state(rdev);
1637 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001638
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001639 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001640 }
1641 } else if (rdev->pm.active_crtc_count == 1) {
1642 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001643
Alex Deucherce8f5372010-05-07 15:10:16 -04001644 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1645 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1646 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1647 radeon_pm_get_dynpm_state(rdev);
1648 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001649
Tejun Heo32c87fc2011-01-03 14:49:32 +01001650 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1651 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001652 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1653 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001654 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1655 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001656 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001657 }
1658 } else { /* count == 0 */
1659 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1660 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001661
Alex Deucherce8f5372010-05-07 15:10:16 -04001662 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1663 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1664 radeon_pm_get_dynpm_state(rdev);
1665 radeon_pm_set_clocks(rdev);
1666 }
1667 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001668 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001669 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001670
1671 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001672}
1673
Alex Deucherda321c82013-04-12 13:55:22 -04001674static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1675{
1676 struct drm_device *ddev = rdev->ddev;
1677 struct drm_crtc *crtc;
1678 struct radeon_crtc *radeon_crtc;
1679
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001680 if (!rdev->pm.dpm_enabled)
1681 return;
1682
Alex Deucherda321c82013-04-12 13:55:22 -04001683 mutex_lock(&rdev->pm.mutex);
1684
Alex Deucher5ca302f2012-11-30 10:56:57 -05001685 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001686 rdev->pm.dpm.new_active_crtcs = 0;
1687 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001688 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1689 list_for_each_entry(crtc,
1690 &ddev->mode_config.crtc_list, head) {
1691 radeon_crtc = to_radeon_crtc(crtc);
1692 if (crtc->enabled) {
1693 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1694 rdev->pm.dpm.new_active_crtc_count++;
1695 }
Alex Deucherda321c82013-04-12 13:55:22 -04001696 }
1697 }
1698
Alex Deucher5ca302f2012-11-30 10:56:57 -05001699 /* update battery/ac status */
1700 if (power_supply_is_system_supplied() > 0)
1701 rdev->pm.dpm.ac_power = true;
1702 else
1703 rdev->pm.dpm.ac_power = false;
1704
Alex Deucherda321c82013-04-12 13:55:22 -04001705 radeon_dpm_change_power_state_locked(rdev);
1706
1707 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001708
Alex Deucherda321c82013-04-12 13:55:22 -04001709}
1710
1711void radeon_pm_compute_clocks(struct radeon_device *rdev)
1712{
1713 if (rdev->pm.pm_method == PM_METHOD_DPM)
1714 radeon_pm_compute_clocks_dpm(rdev);
1715 else
1716 radeon_pm_compute_clocks_old(rdev);
1717}
1718
Alex Deucherce8f5372010-05-07 15:10:16 -04001719static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001720{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001721 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001722 bool in_vbl = true;
1723
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001724 /* Iterate over all active crtc's. All crtc's must be in vblank,
1725 * otherwise return in_vbl == false.
1726 */
1727 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1728 if (rdev->pm.active_crtcs & (1 << crtc)) {
Ville Syrjäläabca9e42013-10-28 20:50:48 +02001729 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001730 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
Daniel Vetter3d3cbd82014-09-10 17:36:11 +02001731 !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
Dave Airlief7352612010-02-18 15:58:36 +10001732 in_vbl = false;
1733 }
1734 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001735
1736 return in_vbl;
1737}
1738
Alex Deucherce8f5372010-05-07 15:10:16 -04001739static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001740{
1741 u32 stat_crtc = 0;
1742 bool in_vbl = radeon_pm_in_vbl(rdev);
1743
Dave Airlief7352612010-02-18 15:58:36 +10001744 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001745 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001746 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001747 return in_vbl;
1748}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001749
Alex Deucherce8f5372010-05-07 15:10:16 -04001750static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001751{
1752 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001753 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001754 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001755 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001756
Matthew Garrettd9932a32010-04-26 16:02:26 -04001757 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001758 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001759 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001760 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001761 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001762
Alex Deucher74652802011-08-25 13:39:48 -04001763 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001764 struct radeon_ring *ring = &rdev->ring[i];
1765
1766 if (ring->ready) {
1767 not_processed += radeon_fence_count_emitted(rdev, i);
1768 if (not_processed >= 3)
1769 break;
1770 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001771 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001772
1773 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001774 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
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_upclock) {
1778 rdev->pm.dynpm_planned_action =
1779 DYNPM_ACTION_UPCLOCK;
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 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001784 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1785 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1786 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1787 rdev->pm.dynpm_can_downclock) {
1788 rdev->pm.dynpm_planned_action =
1789 DYNPM_ACTION_DOWNCLOCK;
1790 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001791 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1792 }
1793 }
1794
Alex Deucherd7311172010-05-03 01:13:14 -04001795 /* Note, radeon_pm_set_clocks is called with static_switch set
1796 * to false since we want to wait for vbl to avoid flicker.
1797 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001798 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1799 jiffies > rdev->pm.dynpm_action_timeout) {
1800 radeon_pm_get_dynpm_state(rdev);
1801 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001802 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001803
Tejun Heo32c87fc2011-01-03 14:49:32 +01001804 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1805 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001806 }
1807 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001808 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001809}
1810
Rafał Miłecki74338742009-11-03 00:53:02 +01001811/*
1812 * Debugfs info
1813 */
1814#if defined(CONFIG_DEBUG_FS)
1815
1816static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1817{
1818 struct drm_info_node *node = (struct drm_info_node *) m->private;
1819 struct drm_device *dev = node->minor->dev;
1820 struct radeon_device *rdev = dev->dev_private;
Alex Deucher4f2f2032014-05-19 19:21:29 -04001821 struct drm_device *ddev = rdev->ddev;
Rafał Miłecki74338742009-11-03 00:53:02 +01001822
Alex Deucher4f2f2032014-05-19 19:21:29 -04001823 if ((rdev->flags & RADEON_IS_PX) &&
1824 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1825 seq_printf(m, "PX asic powered off\n");
1826 } else if (rdev->pm.dpm_enabled) {
Alex Deucher1316b792013-06-28 09:28:39 -04001827 mutex_lock(&rdev->pm.mutex);
1828 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1829 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1830 else
Alex Deucher71375922013-07-02 09:11:39 -04001831 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001832 mutex_unlock(&rdev->pm.mutex);
1833 } else {
1834 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1835 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1836 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1837 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1838 else
1839 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1840 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1841 if (rdev->asic->pm.get_memory_clock)
1842 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1843 if (rdev->pm.current_vddc)
1844 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1845 if (rdev->asic->pm.get_pcie_lanes)
1846 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1847 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001848
1849 return 0;
1850}
1851
1852static struct drm_info_list radeon_pm_info_list[] = {
1853 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1854};
1855#endif
1856
Rafał Miłeckic913e232009-12-22 23:02:16 +01001857static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001858{
1859#if defined(CONFIG_DEBUG_FS)
1860 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1861#else
1862 return 0;
1863#endif
1864}