blob: 326ad068c15aa63ef38a6c85fbcc368ad52bd801 [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);
Lucas Stach1c331f72016-10-23 01:05:33 +020050static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040051
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040052int radeon_pm_get_type_index(struct radeon_device *rdev,
53 enum radeon_pm_state_type ps_type,
54 int instance)
55{
56 int i;
57 int found_instance = -1;
58
59 for (i = 0; i < rdev->pm.num_power_states; i++) {
60 if (rdev->pm.power_state[i].type == ps_type) {
61 found_instance++;
62 if (found_instance == instance)
63 return i;
64 }
65 }
66 /* return default if no match */
67 return rdev->pm.default_power_state_index;
68}
69
Alex Deucherc4917072012-07-31 17:14:35 -040070void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
Alex Deucherce8f5372010-05-07 15:10:16 -040071{
Alex Deucher1c71bda2013-09-09 19:11:52 -040072 if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
73 mutex_lock(&rdev->pm.mutex);
74 if (power_supply_is_system_supplied() > 0)
75 rdev->pm.dpm.ac_power = true;
76 else
77 rdev->pm.dpm.ac_power = false;
Alex Deucher96682952014-06-18 14:23:46 -040078 if (rdev->family == CHIP_ARUBA) {
79 if (rdev->asic->dpm.enable_bapm)
80 radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
81 }
Alex Deucher1c71bda2013-09-09 19:11:52 -040082 mutex_unlock(&rdev->pm.mutex);
Lucas Stach1c331f72016-10-23 01:05:33 +020083 /* allow new DPM state to be picked */
84 radeon_pm_compute_clocks_dpm(rdev);
Jérome Glisse3cf8bb12016-03-16 12:56:45 +010085 } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
Alex Deucherc4917072012-07-31 17:14:35 -040086 if (rdev->pm.profile == PM_PROFILE_AUTO) {
87 mutex_lock(&rdev->pm.mutex);
88 radeon_pm_update_profile(rdev);
89 radeon_pm_set_clocks(rdev);
90 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040091 }
92 }
Alex Deucherce8f5372010-05-07 15:10:16 -040093}
Alex Deucherce8f5372010-05-07 15:10:16 -040094
95static void radeon_pm_update_profile(struct radeon_device *rdev)
96{
97 switch (rdev->pm.profile) {
98 case PM_PROFILE_DEFAULT:
99 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
100 break;
101 case PM_PROFILE_AUTO:
102 if (power_supply_is_system_supplied() > 0) {
103 if (rdev->pm.active_crtc_count > 1)
104 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
105 else
106 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
107 } else {
108 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400109 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400110 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400111 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400112 }
113 break;
114 case PM_PROFILE_LOW:
115 if (rdev->pm.active_crtc_count > 1)
116 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
117 else
118 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
119 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400120 case PM_PROFILE_MID:
121 if (rdev->pm.active_crtc_count > 1)
122 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
123 else
124 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
125 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400126 case PM_PROFILE_HIGH:
127 if (rdev->pm.active_crtc_count > 1)
128 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
129 else
130 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
131 break;
132 }
133
134 if (rdev->pm.active_crtc_count == 0) {
135 rdev->pm.requested_power_state_index =
136 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
137 rdev->pm.requested_clock_mode_index =
138 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
139 } else {
140 rdev->pm.requested_power_state_index =
141 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
142 rdev->pm.requested_clock_mode_index =
143 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
144 }
145}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100146
Matthew Garrett5876dd22010-04-26 15:52:20 -0400147static void radeon_unmap_vram_bos(struct radeon_device *rdev)
148{
149 struct radeon_bo *bo, *n;
150
151 if (list_empty(&rdev->gem.objects))
152 return;
153
154 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
155 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
156 ttm_bo_unmap_virtual(&bo->tbo);
157 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400158}
159
Alex Deucherce8f5372010-05-07 15:10:16 -0400160static void radeon_sync_with_vblank(struct radeon_device *rdev)
161{
162 if (rdev->pm.active_crtcs) {
163 rdev->pm.vblank_sync = false;
164 wait_event_timeout(
165 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
166 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
167 }
168}
169
170static void radeon_set_power_state(struct radeon_device *rdev)
171{
172 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400173 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400174
175 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
176 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
177 return;
178
179 if (radeon_gui_idle(rdev)) {
180 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
181 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500182 if (sclk > rdev->pm.default_sclk)
183 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400184
Alex Deucher27810fb2012-10-01 19:25:11 -0400185 /* starting with BTC, there is one state that is used for both
186 * MH and SH. Difference is that we always use the high clock index for
Alex Deucher7ae764b2013-02-11 08:44:48 -0500187 * mclk and vddci.
Alex Deucher27810fb2012-10-01 19:25:11 -0400188 */
189 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
190 (rdev->family >= CHIP_BARTS) &&
191 rdev->pm.active_crtc_count &&
192 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
193 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
194 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
195 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
196 else
197 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
198 clock_info[rdev->pm.requested_clock_mode_index].mclk;
199
Alex Deucher9ace9f72011-01-06 21:19:26 -0500200 if (mclk > rdev->pm.default_mclk)
201 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400202
Alex Deucher92645872010-05-27 17:01:41 -0400203 /* upvolt before raising clocks, downvolt after lowering clocks */
204 if (sclk < rdev->pm.current_sclk)
205 misc_after = true;
206
207 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400208
209 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400210 if (!radeon_pm_in_vbl(rdev))
211 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400212 }
213
Alex Deucher92645872010-05-27 17:01:41 -0400214 radeon_pm_prepare(rdev);
215
216 if (!misc_after)
217 /* voltage, pcie lanes, etc.*/
218 radeon_pm_misc(rdev);
219
220 /* set engine clock */
221 if (sclk != rdev->pm.current_sclk) {
222 radeon_pm_debug_check_in_vbl(rdev, false);
223 radeon_set_engine_clock(rdev, sclk);
224 radeon_pm_debug_check_in_vbl(rdev, true);
225 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000226 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400227 }
228
229 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500230 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400231 radeon_pm_debug_check_in_vbl(rdev, false);
232 radeon_set_memory_clock(rdev, mclk);
233 radeon_pm_debug_check_in_vbl(rdev, true);
234 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000235 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400236 }
237
238 if (misc_after)
239 /* voltage, pcie lanes, etc.*/
240 radeon_pm_misc(rdev);
241
242 radeon_pm_finish(rdev);
243
Alex Deucherce8f5372010-05-07 15:10:16 -0400244 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
245 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
246 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000247 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400248}
249
250static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400251{
Gustavo Padovana782bca2016-06-06 11:41:44 -0300252 struct drm_crtc *crtc;
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500253 int i, r;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400254
Alex Deucher4e186b22010-08-13 10:53:35 -0400255 /* no need to take locks, etc. if nothing's going to change */
256 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
257 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
258 return;
259
Christian Königdb7fce32012-05-11 14:57:18 +0200260 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200261 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400262
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400263 /* wait for the rings to drain */
264 for (i = 0; i < RADEON_NUM_RINGS; i++) {
265 struct radeon_ring *ring = &rdev->ring[i];
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500266 if (!ring->ready) {
267 continue;
268 }
Christian König37615522014-02-18 15:58:31 +0100269 r = radeon_fence_wait_empty(rdev, i);
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500270 if (r) {
271 /* needs a GPU reset dont reset here */
272 mutex_unlock(&rdev->ring_lock);
273 up_write(&rdev->pm.mclk_lock);
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500274 return;
275 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400276 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400277
Matthew Garrett5876dd22010-04-26 15:52:20 -0400278 radeon_unmap_vram_bos(rdev);
279
Alex Deucherce8f5372010-05-07 15:10:16 -0400280 if (rdev->irq.installed) {
Gustavo Padovana782bca2016-06-06 11:41:44 -0300281 i = 0;
282 drm_for_each_crtc(crtc, rdev->ddev) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400283 if (rdev->pm.active_crtcs & (1 << i)) {
Mario Kleinere0b34e32016-02-12 20:30:31 +0100284 /* This can fail if a modeset is in progress */
Gustavo Padovana782bca2016-06-06 11:41:44 -0300285 if (drm_crtc_vblank_get(crtc) == 0)
Mario Kleinere0b34e32016-02-12 20:30:31 +0100286 rdev->pm.req_vblank |= (1 << i);
287 else
288 DRM_DEBUG_DRIVER("crtc %d no vblank, can glitch\n",
289 i);
Matthew Garrett2aba6312010-04-26 15:45:23 -0400290 }
Gustavo Padovana782bca2016-06-06 11:41:44 -0300291 i++;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400292 }
293 }
Alex Deucher539d2412010-04-29 00:22:43 -0400294
Alex Deucherce8f5372010-05-07 15:10:16 -0400295 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400296
Alex Deucherce8f5372010-05-07 15:10:16 -0400297 if (rdev->irq.installed) {
Gustavo Padovana782bca2016-06-06 11:41:44 -0300298 i = 0;
299 drm_for_each_crtc(crtc, rdev->ddev) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400300 if (rdev->pm.req_vblank & (1 << i)) {
301 rdev->pm.req_vblank &= ~(1 << i);
Gustavo Padovana782bca2016-06-06 11:41:44 -0300302 drm_crtc_vblank_put(crtc);
Matthew Garrett2aba6312010-04-26 15:45:23 -0400303 }
Gustavo Padovana782bca2016-06-06 11:41:44 -0300304 i++;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400305 }
306 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400307
Alex Deuchera4248162010-04-24 14:50:23 -0400308 /* update display watermarks based on new power state */
309 radeon_update_bandwidth_info(rdev);
310 if (rdev->pm.active_crtc_count)
311 radeon_bandwidth_update(rdev);
312
Alex Deucherce8f5372010-05-07 15:10:16 -0400313 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400314
Christian Königd6999bc2012-05-09 15:34:45 +0200315 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200316 up_write(&rdev->pm.mclk_lock);
Alex Deuchera4248162010-04-24 14:50:23 -0400317}
318
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400319static void radeon_pm_print_states(struct radeon_device *rdev)
320{
321 int i, j;
322 struct radeon_power_state *power_state;
323 struct radeon_pm_clock_info *clock_info;
324
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000325 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400326 for (i = 0; i < rdev->pm.num_power_states; i++) {
327 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000328 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400329 radeon_pm_state_type_name[power_state->type]);
330 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000331 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400332 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000333 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400334 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000335 DRM_DEBUG_DRIVER("\tSingle display only\n");
336 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400337 for (j = 0; j < power_state->num_clock_modes; j++) {
338 clock_info = &(power_state->clock_info[j]);
339 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400340 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
341 j,
342 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400343 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400344 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
345 j,
346 clock_info->sclk * 10,
347 clock_info->mclk * 10,
348 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400349 }
350 }
351}
352
Alex Deucherce8f5372010-05-07 15:10:16 -0400353static ssize_t radeon_get_pm_profile(struct device *dev,
354 struct device_attribute *attr,
355 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400356{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200357 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400358 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400359 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400360
Alex Deucherce8f5372010-05-07 15:10:16 -0400361 return snprintf(buf, PAGE_SIZE, "%s\n",
362 (cp == PM_PROFILE_AUTO) ? "auto" :
363 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100364 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400365 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400366}
367
Alex Deucherce8f5372010-05-07 15:10:16 -0400368static ssize_t radeon_set_pm_profile(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf,
371 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400372{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200373 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400374 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400375
Alex Deucher4f2f2032014-05-19 19:21:29 -0400376 /* Can't set profile when the card is off */
377 if ((rdev->flags & RADEON_IS_PX) &&
378 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
379 return -EINVAL;
380
Alex Deuchera4248162010-04-24 14:50:23 -0400381 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400382 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
383 if (strncmp("default", buf, strlen("default")) == 0)
384 rdev->pm.profile = PM_PROFILE_DEFAULT;
385 else if (strncmp("auto", buf, strlen("auto")) == 0)
386 rdev->pm.profile = PM_PROFILE_AUTO;
387 else if (strncmp("low", buf, strlen("low")) == 0)
388 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400389 else if (strncmp("mid", buf, strlen("mid")) == 0)
390 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400391 else if (strncmp("high", buf, strlen("high")) == 0)
392 rdev->pm.profile = PM_PROFILE_HIGH;
393 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000394 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400395 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400396 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400397 radeon_pm_update_profile(rdev);
398 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000399 } else
400 count = -EINVAL;
401
Alex Deucherce8f5372010-05-07 15:10:16 -0400402fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400403 mutex_unlock(&rdev->pm.mutex);
404
405 return count;
406}
407
Alex Deucherce8f5372010-05-07 15:10:16 -0400408static ssize_t radeon_get_pm_method(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400411{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200412 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400413 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400414 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400415
416 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherda321c82013-04-12 13:55:22 -0400417 (pm == PM_METHOD_DYNPM) ? "dynpm" :
418 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
Alex Deuchera4248162010-04-24 14:50:23 -0400419}
420
Alex Deucherce8f5372010-05-07 15:10:16 -0400421static ssize_t radeon_set_pm_method(struct device *dev,
422 struct device_attribute *attr,
423 const char *buf,
424 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400425{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200426 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deuchera4248162010-04-24 14:50:23 -0400427 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400428
Alex Deucher4f2f2032014-05-19 19:21:29 -0400429 /* Can't set method when the card is off */
430 if ((rdev->flags & RADEON_IS_PX) &&
431 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
432 count = -EINVAL;
433 goto fail;
434 }
435
Alex Deucherda321c82013-04-12 13:55:22 -0400436 /* we don't support the legacy modes with dpm */
437 if (rdev->pm.pm_method == PM_METHOD_DPM) {
438 count = -EINVAL;
439 goto fail;
440 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400441
442 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400443 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400444 rdev->pm.pm_method = PM_METHOD_DYNPM;
445 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
446 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400447 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400448 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
449 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400450 /* disable dynpm */
451 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
452 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000453 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400454 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100455 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400456 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000457 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400458 goto fail;
459 }
460 radeon_pm_compute_clocks(rdev);
461fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400462 return count;
463}
464
Alex Deucherda321c82013-04-12 13:55:22 -0400465static ssize_t radeon_get_dpm_state(struct device *dev,
466 struct device_attribute *attr,
467 char *buf)
468{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200469 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400470 struct radeon_device *rdev = ddev->dev_private;
471 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
472
473 return snprintf(buf, PAGE_SIZE, "%s\n",
474 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
475 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
476}
477
478static ssize_t radeon_set_dpm_state(struct device *dev,
479 struct device_attribute *attr,
480 const char *buf,
481 size_t count)
482{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200483 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucherda321c82013-04-12 13:55:22 -0400484 struct radeon_device *rdev = ddev->dev_private;
485
486 mutex_lock(&rdev->pm.mutex);
487 if (strncmp("battery", buf, strlen("battery")) == 0)
488 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
489 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
490 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
491 else if (strncmp("performance", buf, strlen("performance")) == 0)
492 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
493 else {
494 mutex_unlock(&rdev->pm.mutex);
495 count = -EINVAL;
496 goto fail;
497 }
498 mutex_unlock(&rdev->pm.mutex);
Pali Rohárb07a6572014-08-11 19:01:58 +0200499
500 /* Can't set dpm state when the card is off */
501 if (!(rdev->flags & RADEON_IS_PX) ||
502 (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
503 radeon_pm_compute_clocks(rdev);
504
Alex Deucherda321c82013-04-12 13:55:22 -0400505fail:
506 return count;
507}
508
Alex Deucher70d01a52013-07-02 18:38:02 -0400509static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
512{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200513 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400514 struct radeon_device *rdev = ddev->dev_private;
515 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
516
Alex Deucher4f2f2032014-05-19 19:21:29 -0400517 if ((rdev->flags & RADEON_IS_PX) &&
518 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
519 return snprintf(buf, PAGE_SIZE, "off\n");
520
Alex Deucher70d01a52013-07-02 18:38:02 -0400521 return snprintf(buf, PAGE_SIZE, "%s\n",
522 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
523 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
524}
525
526static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf,
529 size_t count)
530{
Jean Delvare3e4e2122013-09-10 10:30:44 +0200531 struct drm_device *ddev = dev_get_drvdata(dev);
Alex Deucher70d01a52013-07-02 18:38:02 -0400532 struct radeon_device *rdev = ddev->dev_private;
533 enum radeon_dpm_forced_level level;
534 int ret = 0;
535
Alex Deucher4f2f2032014-05-19 19:21:29 -0400536 /* Can't force performance level when the card is off */
537 if ((rdev->flags & RADEON_IS_PX) &&
538 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
539 return -EINVAL;
540
Alex Deucher70d01a52013-07-02 18:38:02 -0400541 mutex_lock(&rdev->pm.mutex);
542 if (strncmp("low", buf, strlen("low")) == 0) {
543 level = RADEON_DPM_FORCED_LEVEL_LOW;
544 } else if (strncmp("high", buf, strlen("high")) == 0) {
545 level = RADEON_DPM_FORCED_LEVEL_HIGH;
546 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
547 level = RADEON_DPM_FORCED_LEVEL_AUTO;
548 } else {
Alex Deucher70d01a52013-07-02 18:38:02 -0400549 count = -EINVAL;
550 goto fail;
551 }
552 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher0a17af372013-10-23 17:22:29 -0400553 if (rdev->pm.dpm.thermal_active) {
554 count = -EINVAL;
555 goto fail;
556 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400557 ret = radeon_dpm_force_performance_level(rdev, level);
558 if (ret)
559 count = -EINVAL;
560 }
Alex Deucher70d01a52013-07-02 18:38:02 -0400561fail:
Alex Deucher0a17af372013-10-23 17:22:29 -0400562 mutex_unlock(&rdev->pm.mutex);
563
Alex Deucher70d01a52013-07-02 18:38:02 -0400564 return count;
565}
566
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300567static ssize_t radeon_hwmon_get_pwm1_enable(struct device *dev,
568 struct device_attribute *attr,
569 char *buf)
570{
571 struct radeon_device *rdev = dev_get_drvdata(dev);
572 u32 pwm_mode = 0;
573
574 if (rdev->asic->dpm.fan_ctrl_get_mode)
575 pwm_mode = rdev->asic->dpm.fan_ctrl_get_mode(rdev);
576
577 /* never 0 (full-speed), fuse or smc-controlled always */
578 return sprintf(buf, "%i\n", pwm_mode == FDO_PWM_MODE_STATIC ? 1 : 2);
579}
580
581static ssize_t radeon_hwmon_set_pwm1_enable(struct device *dev,
582 struct device_attribute *attr,
583 const char *buf,
584 size_t count)
585{
586 struct radeon_device *rdev = dev_get_drvdata(dev);
587 int err;
588 int value;
589
590 if(!rdev->asic->dpm.fan_ctrl_set_mode)
591 return -EINVAL;
592
593 err = kstrtoint(buf, 10, &value);
594 if (err)
595 return err;
596
Alex Deucher082452e2015-02-04 17:18:55 -0500597 switch (value) {
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300598 case 1: /* manual, percent-based */
599 rdev->asic->dpm.fan_ctrl_set_mode(rdev, FDO_PWM_MODE_STATIC);
600 break;
601 default: /* disable */
602 rdev->asic->dpm.fan_ctrl_set_mode(rdev, 0);
603 break;
604 }
605
606 return count;
607}
608
609static ssize_t radeon_hwmon_get_pwm1_min(struct device *dev,
610 struct device_attribute *attr,
611 char *buf)
612{
613 return sprintf(buf, "%i\n", 0);
614}
615
616static ssize_t radeon_hwmon_get_pwm1_max(struct device *dev,
617 struct device_attribute *attr,
618 char *buf)
619{
Alex Deucher082452e2015-02-04 17:18:55 -0500620 return sprintf(buf, "%i\n", 255);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300621}
622
623static ssize_t radeon_hwmon_set_pwm1(struct device *dev,
624 struct device_attribute *attr,
625 const char *buf, size_t count)
626{
627 struct radeon_device *rdev = dev_get_drvdata(dev);
628 int err;
629 u32 value;
630
631 err = kstrtou32(buf, 10, &value);
632 if (err)
633 return err;
634
Alex Deucher082452e2015-02-04 17:18:55 -0500635 value = (value * 100) / 255;
636
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300637 err = rdev->asic->dpm.set_fan_speed_percent(rdev, value);
638 if (err)
639 return err;
640
641 return count;
642}
643
644static ssize_t radeon_hwmon_get_pwm1(struct device *dev,
645 struct device_attribute *attr,
646 char *buf)
647{
648 struct radeon_device *rdev = dev_get_drvdata(dev);
649 int err;
650 u32 speed;
651
652 err = rdev->asic->dpm.get_fan_speed_percent(rdev, &speed);
653 if (err)
654 return err;
655
Alex Deucher082452e2015-02-04 17:18:55 -0500656 speed = (speed * 255) / 100;
657
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300658 return sprintf(buf, "%i\n", speed);
659}
660
Alex Deucherce8f5372010-05-07 15:10:16 -0400661static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
662static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400663static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400664static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
665 radeon_get_dpm_forced_performance_level,
666 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400667
Alex Deucher21a81222010-07-02 12:58:16 -0400668static ssize_t radeon_hwmon_show_temp(struct device *dev,
669 struct device_attribute *attr,
670 char *buf)
671{
Guenter Roeckec39f642013-11-22 21:52:00 -0800672 struct radeon_device *rdev = dev_get_drvdata(dev);
Alex Deucher4f2f2032014-05-19 19:21:29 -0400673 struct drm_device *ddev = rdev->ddev;
Alex Deucher20d391d2011-02-01 16:12:34 -0500674 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400675
Alex Deucher4f2f2032014-05-19 19:21:29 -0400676 /* Can't get temperature when the card is off */
677 if ((rdev->flags & RADEON_IS_PX) &&
678 (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
679 return -EINVAL;
680
Alex Deucher6bd1c382013-06-21 14:38:03 -0400681 if (rdev->asic->pm.get_temperature)
682 temp = radeon_get_temperature(rdev);
683 else
Alex Deucher21a81222010-07-02 12:58:16 -0400684 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400685
686 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
687}
688
Jean Delvare6ea4e842013-09-10 10:32:41 +0200689static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
690 struct device_attribute *attr,
691 char *buf)
692{
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300693 struct radeon_device *rdev = dev_get_drvdata(dev);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200694 int hyst = to_sensor_dev_attr(attr)->index;
695 int temp;
696
697 if (hyst)
698 temp = rdev->pm.dpm.thermal.min_temp;
699 else
700 temp = rdev->pm.dpm.thermal.max_temp;
701
702 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
703}
704
Alex Deucher21a81222010-07-02 12:58:16 -0400705static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
Jean Delvare6ea4e842013-09-10 10:32:41 +0200706static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
707static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300708static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1, radeon_hwmon_set_pwm1, 0);
709static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, radeon_hwmon_get_pwm1_enable, radeon_hwmon_set_pwm1_enable, 0);
710static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO, radeon_hwmon_get_pwm1_min, NULL, 0);
711static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO, radeon_hwmon_get_pwm1_max, NULL, 0);
712
Alex Deucher21a81222010-07-02 12:58:16 -0400713
714static struct attribute *hwmon_attributes[] = {
715 &sensor_dev_attr_temp1_input.dev_attr.attr,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200716 &sensor_dev_attr_temp1_crit.dev_attr.attr,
717 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300718 &sensor_dev_attr_pwm1.dev_attr.attr,
719 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
720 &sensor_dev_attr_pwm1_min.dev_attr.attr,
721 &sensor_dev_attr_pwm1_max.dev_attr.attr,
Alex Deucher21a81222010-07-02 12:58:16 -0400722 NULL
723};
724
Jean Delvare6ea4e842013-09-10 10:32:41 +0200725static umode_t hwmon_attributes_visible(struct kobject *kobj,
726 struct attribute *attr, int index)
727{
Geliang Tange3837b02016-01-13 22:48:43 +0800728 struct device *dev = kobj_to_dev(kobj);
Sergey Senozhatskye4158f12013-12-13 02:25:57 +0300729 struct radeon_device *rdev = dev_get_drvdata(dev);
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300730 umode_t effective_mode = attr->mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200731
Alex Deucher2a7d44f2015-10-19 09:30:42 -0400732 /* Skip attributes if DPM is not enabled */
Jean Delvare6ea4e842013-09-10 10:32:41 +0200733 if (rdev->pm.pm_method != PM_METHOD_DPM &&
734 (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
Alex Deucher2a7d44f2015-10-19 09:30:42 -0400735 attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr ||
736 attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
737 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
738 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
739 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
Jean Delvare6ea4e842013-09-10 10:32:41 +0200740 return 0;
741
Oleg Chernovskiy99736702014-12-08 00:10:45 +0300742 /* Skip fan attributes if fan is not present */
743 if (rdev->pm.no_fan &&
744 (attr == &sensor_dev_attr_pwm1.dev_attr.attr ||
745 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr ||
746 attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
747 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
748 return 0;
749
750 /* mask fan attributes if we have no bindings for this asic to expose */
751 if ((!rdev->asic->dpm.get_fan_speed_percent &&
752 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't query fan */
753 (!rdev->asic->dpm.fan_ctrl_get_mode &&
754 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't query state */
755 effective_mode &= ~S_IRUGO;
756
757 if ((!rdev->asic->dpm.set_fan_speed_percent &&
758 attr == &sensor_dev_attr_pwm1.dev_attr.attr) || /* can't manage fan */
759 (!rdev->asic->dpm.fan_ctrl_set_mode &&
760 attr == &sensor_dev_attr_pwm1_enable.dev_attr.attr)) /* can't manage state */
761 effective_mode &= ~S_IWUSR;
762
763 /* hide max/min values if we can't both query and manage the fan */
764 if ((!rdev->asic->dpm.set_fan_speed_percent &&
765 !rdev->asic->dpm.get_fan_speed_percent) &&
766 (attr == &sensor_dev_attr_pwm1_max.dev_attr.attr ||
767 attr == &sensor_dev_attr_pwm1_min.dev_attr.attr))
768 return 0;
769
770 return effective_mode;
Jean Delvare6ea4e842013-09-10 10:32:41 +0200771}
772
Alex Deucher21a81222010-07-02 12:58:16 -0400773static const struct attribute_group hwmon_attrgroup = {
774 .attrs = hwmon_attributes,
Jean Delvare6ea4e842013-09-10 10:32:41 +0200775 .is_visible = hwmon_attributes_visible,
Alex Deucher21a81222010-07-02 12:58:16 -0400776};
777
Guenter Roeckec39f642013-11-22 21:52:00 -0800778static const struct attribute_group *hwmon_groups[] = {
779 &hwmon_attrgroup,
780 NULL
781};
782
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200783static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400784{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200785 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400786
787 switch (rdev->pm.int_thermal_type) {
788 case THERMAL_TYPE_RV6XX:
789 case THERMAL_TYPE_RV770:
790 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400791 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500792 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400793 case THERMAL_TYPE_SI:
Alex Deucher286d9cc2013-06-21 15:50:47 -0400794 case THERMAL_TYPE_CI:
795 case THERMAL_TYPE_KV:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400796 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400797 return err;
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400798 rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
799 "radeon", rdev,
800 hwmon_groups);
801 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
802 err = PTR_ERR(rdev->pm.int_hwmon_dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200803 dev_err(rdev->dev,
804 "Unable to register hwmon device: %d\n", err);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200805 }
Alex Deucher21a81222010-07-02 12:58:16 -0400806 break;
807 default:
808 break;
809 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200810
811 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400812}
813
Alex Deuchercb3e4e72014-04-15 12:44:32 -0400814static void radeon_hwmon_fini(struct radeon_device *rdev)
815{
816 if (rdev->pm.int_hwmon_dev)
817 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
818}
819
Alex Deucherda321c82013-04-12 13:55:22 -0400820static void radeon_dpm_thermal_work_handler(struct work_struct *work)
821{
822 struct radeon_device *rdev =
823 container_of(work, struct radeon_device,
824 pm.dpm.thermal.work);
825 /* switch to the thermal state */
826 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
827
828 if (!rdev->pm.dpm_enabled)
829 return;
830
831 if (rdev->asic->pm.get_temperature) {
832 int temp = radeon_get_temperature(rdev);
833
834 if (temp < rdev->pm.dpm.thermal.min_temp)
835 /* switch back the user state */
836 dpm_state = rdev->pm.dpm.user_state;
837 } else {
838 if (rdev->pm.dpm.thermal.high_to_low)
839 /* switch back the user state */
840 dpm_state = rdev->pm.dpm.user_state;
841 }
Alex Deucher60320342013-07-24 14:59:48 -0400842 mutex_lock(&rdev->pm.mutex);
843 if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
844 rdev->pm.dpm.thermal_active = true;
845 else
846 rdev->pm.dpm.thermal_active = false;
847 rdev->pm.dpm.state = dpm_state;
848 mutex_unlock(&rdev->pm.mutex);
849
850 radeon_pm_compute_clocks(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -0400851}
852
Alex Deucher3899ca82015-03-18 17:05:10 -0400853static bool radeon_dpm_single_display(struct radeon_device *rdev)
Alex Deucherda321c82013-04-12 13:55:22 -0400854{
Alex Deucher48783062013-07-08 11:35:06 -0400855 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
856 true : false;
857
858 /* check if the vblank period is too short to adjust the mclk */
859 if (single_display && rdev->asic->dpm.vblank_too_short) {
860 if (radeon_dpm_vblank_too_short(rdev))
861 single_display = false;
862 }
Alex Deucherda321c82013-04-12 13:55:22 -0400863
Alex Deucher951caa62015-02-18 00:59:45 -0500864 /* 120hz tends to be problematic even if they are under the
865 * vblank limit.
866 */
867 if (single_display && (r600_dpm_get_vrefresh(rdev) >= 120))
868 single_display = false;
869
Alex Deucher3899ca82015-03-18 17:05:10 -0400870 return single_display;
871}
872
873static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
874 enum radeon_pm_state_type dpm_state)
875{
876 int i;
877 struct radeon_ps *ps;
878 u32 ui_class;
879 bool single_display = radeon_dpm_single_display(rdev);
880
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400881 /* certain older asics have a separare 3D performance state,
882 * so try that first if the user selected performance
883 */
884 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
885 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400886 /* balanced states don't exist at the moment */
887 if (dpm_state == POWER_STATE_TYPE_BALANCED)
Lucas Stach1c331f72016-10-23 01:05:33 +0200888 dpm_state = rdev->pm.dpm.ac_power ?
889 POWER_STATE_TYPE_PERFORMANCE : POWER_STATE_TYPE_BATTERY;
Alex Deucherda321c82013-04-12 13:55:22 -0400890
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400891restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400892 /* Pick the best power state based on current conditions */
893 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
894 ps = &rdev->pm.dpm.ps[i];
895 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
896 switch (dpm_state) {
897 /* user states */
898 case POWER_STATE_TYPE_BATTERY:
899 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
900 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400901 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400902 return ps;
903 } else
904 return ps;
905 }
906 break;
907 case POWER_STATE_TYPE_BALANCED:
908 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
909 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400910 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400911 return ps;
912 } else
913 return ps;
914 }
915 break;
916 case POWER_STATE_TYPE_PERFORMANCE:
917 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
918 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400919 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400920 return ps;
921 } else
922 return ps;
923 }
924 break;
925 /* internal states */
926 case POWER_STATE_TYPE_INTERNAL_UVD:
Alex Deucherd4d32782013-06-11 17:55:39 -0400927 if (rdev->pm.dpm.uvd_ps)
928 return rdev->pm.dpm.uvd_ps;
929 else
930 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400931 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
932 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
933 return ps;
934 break;
935 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
936 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
937 return ps;
938 break;
939 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
940 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
941 return ps;
942 break;
943 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
944 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
945 return ps;
946 break;
947 case POWER_STATE_TYPE_INTERNAL_BOOT:
948 return rdev->pm.dpm.boot_ps;
949 case POWER_STATE_TYPE_INTERNAL_THERMAL:
950 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
951 return ps;
952 break;
953 case POWER_STATE_TYPE_INTERNAL_ACPI:
954 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
955 return ps;
956 break;
957 case POWER_STATE_TYPE_INTERNAL_ULV:
958 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
959 return ps;
960 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400961 case POWER_STATE_TYPE_INTERNAL_3DPERF:
962 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
963 return ps;
964 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400965 default:
966 break;
967 }
968 }
969 /* use a fallback state if we didn't match */
970 switch (dpm_state) {
971 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400972 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
973 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400974 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
975 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
976 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
Alex Deucherd4d32782013-06-11 17:55:39 -0400977 if (rdev->pm.dpm.uvd_ps) {
978 return rdev->pm.dpm.uvd_ps;
979 } else {
980 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
981 goto restart_search;
982 }
Alex Deucherda321c82013-04-12 13:55:22 -0400983 case POWER_STATE_TYPE_INTERNAL_THERMAL:
984 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
985 goto restart_search;
986 case POWER_STATE_TYPE_INTERNAL_ACPI:
987 dpm_state = POWER_STATE_TYPE_BATTERY;
988 goto restart_search;
989 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400990 case POWER_STATE_TYPE_BALANCED:
991 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400992 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
993 goto restart_search;
994 default:
995 break;
996 }
997
998 return NULL;
999}
1000
1001static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
1002{
1003 int i;
1004 struct radeon_ps *ps;
1005 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -05001006 int ret;
Alex Deucher3899ca82015-03-18 17:05:10 -04001007 bool single_display = radeon_dpm_single_display(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001008
1009 /* if dpm init failed */
1010 if (!rdev->pm.dpm_enabled)
1011 return;
1012
1013 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
1014 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -04001015 if ((!rdev->pm.dpm.thermal_active) &&
1016 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -04001017 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
1018 }
1019 dpm_state = rdev->pm.dpm.state;
1020
1021 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
1022 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -05001023 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -04001024 else
1025 return;
1026
Alex Deucherd22b7e42012-11-29 19:27:56 -05001027 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -04001028 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherb62d6282013-08-20 20:29:05 -04001029 /* vce just modifies an existing state so force a change */
1030 if (ps->vce_active != rdev->pm.dpm.vce_active)
1031 goto force;
Alex Deucher3899ca82015-03-18 17:05:10 -04001032 /* user has made a display change (such as timing) */
1033 if (rdev->pm.dpm.single_display != single_display)
1034 goto force;
Alex Deucherd22b7e42012-11-29 19:27:56 -05001035 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
1036 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
1037 * all we need to do is update the display configuration.
1038 */
1039 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
1040 /* update display watermarks based on new power state */
1041 radeon_bandwidth_update(rdev);
1042 /* update displays */
1043 radeon_dpm_display_configuration_changed(rdev);
1044 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1045 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1046 }
1047 return;
1048 } else {
1049 /* for BTC+ if the num crtcs hasn't changed and state is the same,
1050 * nothing to do, if the num crtcs is > 1 and state is the same,
1051 * update display configuration.
1052 */
1053 if (rdev->pm.dpm.new_active_crtcs ==
1054 rdev->pm.dpm.current_active_crtcs) {
1055 return;
1056 } else {
1057 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
1058 (rdev->pm.dpm.new_active_crtc_count > 1)) {
1059 /* update display watermarks based on new power state */
1060 radeon_bandwidth_update(rdev);
1061 /* update displays */
1062 radeon_dpm_display_configuration_changed(rdev);
1063 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1064 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1065 return;
1066 }
1067 }
Alex Deucherda321c82013-04-12 13:55:22 -04001068 }
Alex Deucherda321c82013-04-12 13:55:22 -04001069 }
1070
Alex Deucherb62d6282013-08-20 20:29:05 -04001071force:
Alex Deucher033a37d2013-10-23 18:35:43 -04001072 if (radeon_dpm == 1) {
1073 printk("switching from power state:\n");
1074 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
1075 printk("switching to power state:\n");
1076 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
1077 }
Alex Deucherb62d6282013-08-20 20:29:05 -04001078
Alex Deucherda321c82013-04-12 13:55:22 -04001079 down_write(&rdev->pm.mclk_lock);
1080 mutex_lock(&rdev->ring_lock);
1081
Alex Deucherb62d6282013-08-20 20:29:05 -04001082 /* update whether vce is active */
1083 ps->vce_active = rdev->pm.dpm.vce_active;
1084
Alex Deucher89c9bc52013-01-16 14:40:26 -05001085 ret = radeon_dpm_pre_set_power_state(rdev);
1086 if (ret)
1087 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -05001088
Alex Deucherda321c82013-04-12 13:55:22 -04001089 /* update display watermarks based on new power state */
1090 radeon_bandwidth_update(rdev);
Alex Deucherd74e7662016-03-08 11:31:00 -05001091 /* update displays */
1092 radeon_dpm_display_configuration_changed(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001093
Alex Deucherda321c82013-04-12 13:55:22 -04001094 /* wait for the rings to drain */
1095 for (i = 0; i < RADEON_NUM_RINGS; i++) {
1096 struct radeon_ring *ring = &rdev->ring[i];
1097 if (ring->ready)
Christian König37615522014-02-18 15:58:31 +01001098 radeon_fence_wait_empty(rdev, i);
Alex Deucherda321c82013-04-12 13:55:22 -04001099 }
1100
1101 /* program the new power state */
1102 radeon_dpm_set_power_state(rdev);
1103
1104 /* update current power state */
1105 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
1106
Alex Deucher89c9bc52013-01-16 14:40:26 -05001107 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -05001108
Alex Deucher5e031d92016-02-24 17:38:38 -05001109 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
1110 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
1111 rdev->pm.dpm.single_display = single_display;
1112
Alex Deucher1cd8b212013-09-13 14:07:03 -04001113 if (rdev->asic->dpm.force_performance_level) {
Alex Deucher14ac88a2013-10-23 17:31:42 -04001114 if (rdev->pm.dpm.thermal_active) {
1115 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001116 /* force low perf level for thermal */
1117 radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
Alex Deucher14ac88a2013-10-23 17:31:42 -04001118 /* save the user's level */
1119 rdev->pm.dpm.forced_level = level;
1120 } else {
1121 /* otherwise, user selected level */
1122 radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
1123 }
Alex Deucher60320342013-07-24 14:59:48 -04001124 }
1125
Alex Deucher84dd1922013-01-16 12:52:04 -05001126done:
Alex Deucherda321c82013-04-12 13:55:22 -04001127 mutex_unlock(&rdev->ring_lock);
1128 up_write(&rdev->pm.mclk_lock);
Alex Deucherda321c82013-04-12 13:55:22 -04001129}
1130
Alex Deucherce3537d2013-07-24 12:12:49 -04001131void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
1132{
1133 enum radeon_pm_state_type dpm_state;
1134
Alex Deucher9e9d9762013-07-31 18:13:23 -04001135 if (rdev->asic->dpm.powergate_uvd) {
Alex Deucherce3537d2013-07-24 12:12:49 -04001136 mutex_lock(&rdev->pm.mutex);
Christian König8158eb92014-01-10 16:05:05 +01001137 /* don't powergate anything if we
1138 have active but pause streams */
1139 enable |= rdev->pm.dpm.sd > 0;
1140 enable |= rdev->pm.dpm.hd > 0;
Alex Deucher9e9d9762013-07-31 18:13:23 -04001141 /* enable/disable UVD */
1142 radeon_dpm_powergate_uvd(rdev, !enable);
Alex Deucherce3537d2013-07-24 12:12:49 -04001143 mutex_unlock(&rdev->pm.mutex);
1144 } else {
Alex Deucher9e9d9762013-07-31 18:13:23 -04001145 if (enable) {
1146 mutex_lock(&rdev->pm.mutex);
1147 rdev->pm.dpm.uvd_active = true;
Alex Deucher0690a222014-06-07 11:31:25 -04001148 /* disable this for now */
1149#if 0
Alex Deucher9e9d9762013-07-31 18:13:23 -04001150 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
1151 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
1152 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
1153 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1154 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
1155 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
1156 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
1157 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
1158 else
Alex Deucher0690a222014-06-07 11:31:25 -04001159#endif
Alex Deucher9e9d9762013-07-31 18:13:23 -04001160 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
1161 rdev->pm.dpm.state = dpm_state;
1162 mutex_unlock(&rdev->pm.mutex);
1163 } else {
1164 mutex_lock(&rdev->pm.mutex);
1165 rdev->pm.dpm.uvd_active = false;
1166 mutex_unlock(&rdev->pm.mutex);
1167 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001168
Alex Deucher9e9d9762013-07-31 18:13:23 -04001169 radeon_pm_compute_clocks(rdev);
1170 }
Alex Deucherce3537d2013-07-24 12:12:49 -04001171}
1172
Alex Deucher03afe6f2013-08-23 11:56:26 -04001173void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
1174{
1175 if (enable) {
1176 mutex_lock(&rdev->pm.mutex);
1177 rdev->pm.dpm.vce_active = true;
1178 /* XXX select vce level based on ring/task */
1179 rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
1180 mutex_unlock(&rdev->pm.mutex);
1181 } else {
1182 mutex_lock(&rdev->pm.mutex);
1183 rdev->pm.dpm.vce_active = false;
1184 mutex_unlock(&rdev->pm.mutex);
1185 }
1186
1187 radeon_pm_compute_clocks(rdev);
1188}
1189
Alex Deucherda321c82013-04-12 13:55:22 -04001190static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -05001191{
Alex Deucherce8f5372010-05-07 15:10:16 -04001192 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001193 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001194 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
1195 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001196 }
Alex Deucherce8f5372010-05-07 15:10:16 -04001197 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001198
1199 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -05001200}
1201
Alex Deucherda321c82013-04-12 13:55:22 -04001202static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
1203{
1204 mutex_lock(&rdev->pm.mutex);
1205 /* disable dpm */
1206 radeon_dpm_disable(rdev);
1207 /* reset the power state */
1208 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1209 rdev->pm.dpm_enabled = false;
1210 mutex_unlock(&rdev->pm.mutex);
1211}
1212
1213void radeon_pm_suspend(struct radeon_device *rdev)
1214{
1215 if (rdev->pm.pm_method == PM_METHOD_DPM)
1216 radeon_pm_suspend_dpm(rdev);
1217 else
1218 radeon_pm_suspend_old(rdev);
1219}
1220
1221static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001222{
Alex Deuchered18a362011-01-06 21:19:32 -05001223 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001224 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001225 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001226 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001227 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001228 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1229 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -04001230 if (rdev->pm.default_vddci)
1231 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1232 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001233 if (rdev->pm.default_sclk)
1234 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1235 if (rdev->pm.default_mclk)
1236 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1237 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001238 /* asic init will reset the default power state */
1239 mutex_lock(&rdev->pm.mutex);
1240 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
1241 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001242 rdev->pm.current_sclk = rdev->pm.default_sclk;
1243 rdev->pm.current_mclk = rdev->pm.default_mclk;
Michel Dänzer37016952014-01-08 11:40:20 +09001244 if (rdev->pm.power_state) {
1245 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
1246 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
1247 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001248 if (rdev->pm.pm_method == PM_METHOD_DYNPM
1249 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
1250 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001251 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1252 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001253 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001254 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001255 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +01001256}
1257
Alex Deucherda321c82013-04-12 13:55:22 -04001258static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001259{
Dave Airlie26481fb2010-05-18 19:00:14 +10001260 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001261
Alex Deucherda321c82013-04-12 13:55:22 -04001262 /* asic init will reset to the boot state */
1263 mutex_lock(&rdev->pm.mutex);
1264 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1265 radeon_dpm_setup_asic(rdev);
1266 ret = radeon_dpm_enable(rdev);
1267 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001268 if (ret)
1269 goto dpm_resume_fail;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001270 rdev->pm.dpm_enabled = true;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001271 return;
1272
1273dpm_resume_fail:
1274 DRM_ERROR("radeon: dpm resume failed\n");
1275 if ((rdev->family >= CHIP_BARTS) &&
1276 (rdev->family <= CHIP_CAYMAN) &&
1277 rdev->mc_fw) {
1278 if (rdev->pm.default_vddc)
1279 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1280 SET_VOLTAGE_TYPE_ASIC_VDDC);
1281 if (rdev->pm.default_vddci)
1282 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1283 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1284 if (rdev->pm.default_sclk)
1285 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1286 if (rdev->pm.default_mclk)
1287 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
Alex Deucherda321c82013-04-12 13:55:22 -04001288 }
1289}
1290
1291void radeon_pm_resume(struct radeon_device *rdev)
1292{
1293 if (rdev->pm.pm_method == PM_METHOD_DPM)
1294 radeon_pm_resume_dpm(rdev);
1295 else
1296 radeon_pm_resume_old(rdev);
1297}
1298
1299static int radeon_pm_init_old(struct radeon_device *rdev)
1300{
1301 int ret;
1302
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001303 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001304 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1305 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1306 rdev->pm.dynpm_can_upclock = true;
1307 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001308 rdev->pm.default_sclk = rdev->clock.default_sclk;
1309 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001310 rdev->pm.current_sclk = rdev->clock.default_sclk;
1311 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001312 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001313
Alex Deucher56278a82009-12-28 13:58:44 -05001314 if (rdev->bios) {
1315 if (rdev->is_atom_bios)
1316 radeon_atombios_get_power_modes(rdev);
1317 else
1318 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001319 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001320 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001321 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001322 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucher36099182013-09-21 14:37:49 -04001323 (rdev->family <= CHIP_CAYMAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001324 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001325 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001326 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1327 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001328 if (rdev->pm.default_vddci)
1329 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1330 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001331 if (rdev->pm.default_sclk)
1332 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1333 if (rdev->pm.default_mclk)
1334 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1335 }
Alex Deucher56278a82009-12-28 13:58:44 -05001336 }
1337
Alex Deucher21a81222010-07-02 12:58:16 -04001338 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001339 ret = radeon_hwmon_init(rdev);
1340 if (ret)
1341 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001342
1343 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1344
Alex Deucherce8f5372010-05-07 15:10:16 -04001345 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001346 if (radeon_debugfs_pm_init(rdev)) {
1347 DRM_ERROR("Failed to register debugfs file for PM!\n");
1348 }
1349
1350 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001351 }
1352
1353 return 0;
1354}
1355
Alex Deucherda321c82013-04-12 13:55:22 -04001356static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1357{
1358 int i;
1359
1360 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1361 printk("== power state %d ==\n", i);
1362 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1363 }
1364}
1365
1366static int radeon_pm_init_dpm(struct radeon_device *rdev)
1367{
1368 int ret;
1369
Alex Deucher1cd8b212013-09-13 14:07:03 -04001370 /* default to balanced state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001371 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1372 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucher1cd8b212013-09-13 14:07:03 -04001373 rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
Alex Deucherda321c82013-04-12 13:55:22 -04001374 rdev->pm.default_sclk = rdev->clock.default_sclk;
1375 rdev->pm.default_mclk = rdev->clock.default_mclk;
1376 rdev->pm.current_sclk = rdev->clock.default_sclk;
1377 rdev->pm.current_mclk = rdev->clock.default_mclk;
1378 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1379
1380 if (rdev->bios && rdev->is_atom_bios)
1381 radeon_atombios_get_power_modes(rdev);
1382 else
1383 return -EINVAL;
1384
1385 /* set up the internal thermal sensor if applicable */
1386 ret = radeon_hwmon_init(rdev);
1387 if (ret)
1388 return ret;
1389
1390 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1391 mutex_lock(&rdev->pm.mutex);
1392 radeon_dpm_init(rdev);
1393 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
Alex Deucher033a37d2013-10-23 18:35:43 -04001394 if (radeon_dpm == 1)
1395 radeon_dpm_print_power_states(rdev);
Alex Deucherda321c82013-04-12 13:55:22 -04001396 radeon_dpm_setup_asic(rdev);
1397 ret = radeon_dpm_enable(rdev);
1398 mutex_unlock(&rdev->pm.mutex);
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001399 if (ret)
1400 goto dpm_failed;
Alex Deucherda321c82013-04-12 13:55:22 -04001401 rdev->pm.dpm_enabled = true;
Alex Deucherda321c82013-04-12 13:55:22 -04001402
Alex Deucherbb5abf92013-12-18 13:39:58 -05001403 if (radeon_debugfs_pm_init(rdev)) {
1404 DRM_ERROR("Failed to register debugfs file for dpm!\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001405 }
1406
Alex Deucherbb5abf92013-12-18 13:39:58 -05001407 DRM_INFO("radeon: dpm initialized\n");
1408
Alex Deucherda321c82013-04-12 13:55:22 -04001409 return 0;
Alex Deuchere14cd2b2013-12-19 16:17:47 -05001410
1411dpm_failed:
1412 rdev->pm.dpm_enabled = false;
1413 if ((rdev->family >= CHIP_BARTS) &&
1414 (rdev->family <= CHIP_CAYMAN) &&
1415 rdev->mc_fw) {
1416 if (rdev->pm.default_vddc)
1417 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1418 SET_VOLTAGE_TYPE_ASIC_VDDC);
1419 if (rdev->pm.default_vddci)
1420 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1421 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1422 if (rdev->pm.default_sclk)
1423 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1424 if (rdev->pm.default_mclk)
1425 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1426 }
1427 DRM_ERROR("radeon: dpm initialization failed\n");
1428 return ret;
Alex Deucherda321c82013-04-12 13:55:22 -04001429}
1430
Alex Deucher4369a692015-01-08 10:46:33 -05001431struct radeon_dpm_quirk {
1432 u32 chip_vendor;
1433 u32 chip_device;
1434 u32 subsys_vendor;
1435 u32 subsys_device;
1436};
1437
1438/* cards with dpm stability problems */
1439static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
1440 /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
1441 { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
1442 /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
1443 { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
1444 { 0, 0, 0, 0 },
1445};
1446
Alex Deucherda321c82013-04-12 13:55:22 -04001447int radeon_pm_init(struct radeon_device *rdev)
1448{
Alex Deucher4369a692015-01-08 10:46:33 -05001449 struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
1450 bool disable_dpm = false;
1451
1452 /* Apply dpm quirks */
1453 while (p && p->chip_device != 0) {
1454 if (rdev->pdev->vendor == p->chip_vendor &&
1455 rdev->pdev->device == p->chip_device &&
1456 rdev->pdev->subsystem_vendor == p->subsys_vendor &&
1457 rdev->pdev->subsystem_device == p->subsys_device) {
1458 disable_dpm = true;
1459 break;
1460 }
1461 ++p;
1462 }
1463
Alex Deucherda321c82013-04-12 13:55:22 -04001464 /* enable dpm on rv6xx+ */
1465 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001466 case CHIP_RV610:
1467 case CHIP_RV630:
1468 case CHIP_RV620:
1469 case CHIP_RV635:
1470 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001471 case CHIP_RS780:
1472 case CHIP_RS880:
Alex Deucher76e6dce2014-04-18 09:08:11 -04001473 case CHIP_RV770:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001474 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001475 if (!rdev->rlc_fw)
1476 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001477 else if ((rdev->family >= CHIP_RV770) &&
1478 (!(rdev->flags & RADEON_IS_IGP)) &&
1479 (!rdev->smc_fw))
1480 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001481 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001482 rdev->pm.pm_method = PM_METHOD_DPM;
1483 else
1484 rdev->pm.pm_method = PM_METHOD_PROFILE;
1485 break;
Alex Deucherab70b1d2013-11-01 15:16:02 -04001486 case CHIP_RV730:
1487 case CHIP_RV710:
1488 case CHIP_RV740:
Alex Deucher59f7a2f2013-11-01 15:11:34 -04001489 case CHIP_CEDAR:
1490 case CHIP_REDWOOD:
1491 case CHIP_JUNIPER:
1492 case CHIP_CYPRESS:
1493 case CHIP_HEMLOCK:
Alex Deucher5a16f762013-10-23 17:11:06 -04001494 case CHIP_PALM:
1495 case CHIP_SUMO:
1496 case CHIP_SUMO2:
Alex Deucherc08abf12014-07-14 12:01:40 -04001497 case CHIP_BARTS:
1498 case CHIP_TURKS:
1499 case CHIP_CAICOS:
Alex Deucher8f500af2014-07-07 17:13:37 -04001500 case CHIP_CAYMAN:
Alex Deucher3a118982013-11-14 10:21:29 -05001501 case CHIP_ARUBA:
Alex Deucher68bc7782013-10-23 17:14:06 -04001502 case CHIP_TAHITI:
1503 case CHIP_PITCAIRN:
1504 case CHIP_VERDE:
1505 case CHIP_OLAND:
1506 case CHIP_HAINAN:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001507 case CHIP_BONAIRE:
Alex Deuchere308b1d2013-12-19 17:39:17 -05001508 case CHIP_KABINI:
1509 case CHIP_KAVERI:
Alex Deucher4f22dde2013-12-19 17:37:33 -05001510 case CHIP_HAWAII:
Samuel Li7d032a42014-04-30 18:40:51 -04001511 case CHIP_MULLINS:
Alex Deucher5a16f762013-10-23 17:11:06 -04001512 /* DPM requires the RLC, RV770+ dGPU requires SMC */
1513 if (!rdev->rlc_fw)
1514 rdev->pm.pm_method = PM_METHOD_PROFILE;
1515 else if ((rdev->family >= CHIP_RV770) &&
1516 (!(rdev->flags & RADEON_IS_IGP)) &&
1517 (!rdev->smc_fw))
1518 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher4369a692015-01-08 10:46:33 -05001519 else if (disable_dpm && (radeon_dpm == -1))
1520 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher5a16f762013-10-23 17:11:06 -04001521 else if (radeon_dpm == 0)
1522 rdev->pm.pm_method = PM_METHOD_PROFILE;
1523 else
1524 rdev->pm.pm_method = PM_METHOD_DPM;
1525 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001526 default:
1527 /* default to profile method */
1528 rdev->pm.pm_method = PM_METHOD_PROFILE;
1529 break;
1530 }
1531
1532 if (rdev->pm.pm_method == PM_METHOD_DPM)
1533 return radeon_pm_init_dpm(rdev);
1534 else
1535 return radeon_pm_init_old(rdev);
1536}
1537
Alex Deucher914a8982013-12-19 11:37:22 -05001538int radeon_pm_late_init(struct radeon_device *rdev)
1539{
1540 int ret = 0;
1541
1542 if (rdev->pm.pm_method == PM_METHOD_DPM) {
Alex Deucher51a47262015-09-30 16:45:52 -04001543 if (rdev->pm.dpm_enabled) {
Alex Deucher49abb262015-10-23 10:38:52 -04001544 if (!rdev->pm.sysfs_initialized) {
1545 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1546 if (ret)
1547 DRM_ERROR("failed to create device file for dpm state\n");
1548 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1549 if (ret)
1550 DRM_ERROR("failed to create device file for dpm state\n");
1551 /* XXX: these are noops for dpm but are here for backwards compat */
1552 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1553 if (ret)
1554 DRM_ERROR("failed to create device file for power profile\n");
1555 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1556 if (ret)
1557 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher24dd2f62015-11-10 13:01:35 -05001558 rdev->pm.sysfs_initialized = true;
Alex Deucher49abb262015-10-23 10:38:52 -04001559 }
Alex Deucher51a47262015-09-30 16:45:52 -04001560
1561 mutex_lock(&rdev->pm.mutex);
1562 ret = radeon_dpm_late_enable(rdev);
1563 mutex_unlock(&rdev->pm.mutex);
1564 if (ret) {
1565 rdev->pm.dpm_enabled = false;
1566 DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
1567 } else {
1568 /* set the dpm state for PX since there won't be
1569 * a modeset to call this.
1570 */
1571 radeon_pm_compute_clocks(rdev);
1572 }
1573 }
1574 } else {
Alex Deucher49abb262015-10-23 10:38:52 -04001575 if ((rdev->pm.num_power_states > 1) &&
1576 (!rdev->pm.sysfs_initialized)) {
Alex Deucher51a47262015-09-30 16:45:52 -04001577 /* where's the best place to put these? */
1578 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1579 if (ret)
1580 DRM_ERROR("failed to create device file for power profile\n");
1581 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1582 if (ret)
1583 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher49abb262015-10-23 10:38:52 -04001584 if (!ret)
1585 rdev->pm.sysfs_initialized = true;
Alex Deucher51a47262015-09-30 16:45:52 -04001586 }
Alex Deucher914a8982013-12-19 11:37:22 -05001587 }
1588 return ret;
1589}
1590
Alex Deucherda321c82013-04-12 13:55:22 -04001591static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001592{
Alex Deucherce8f5372010-05-07 15:10:16 -04001593 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001594 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001595 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1596 rdev->pm.profile = PM_PROFILE_DEFAULT;
1597 radeon_pm_update_profile(rdev);
1598 radeon_pm_set_clocks(rdev);
1599 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001600 /* reset default clocks */
1601 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1602 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1603 radeon_pm_set_clocks(rdev);
1604 }
Alex Deuchera4248162010-04-24 14:50:23 -04001605 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001606
1607 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001608
Alex Deucherce8f5372010-05-07 15:10:16 -04001609 device_remove_file(rdev->dev, &dev_attr_power_profile);
1610 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001611 }
Alex Deuchera4248162010-04-24 14:50:23 -04001612
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001613 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001614 kfree(rdev->pm.power_state);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001615}
1616
Alex Deucherda321c82013-04-12 13:55:22 -04001617static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1618{
1619 if (rdev->pm.num_power_states > 1) {
1620 mutex_lock(&rdev->pm.mutex);
1621 radeon_dpm_disable(rdev);
1622 mutex_unlock(&rdev->pm.mutex);
1623
1624 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001625 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001626 /* XXX backwards compat */
1627 device_remove_file(rdev->dev, &dev_attr_power_profile);
1628 device_remove_file(rdev->dev, &dev_attr_power_method);
1629 }
1630 radeon_dpm_fini(rdev);
1631
Alex Deuchercb3e4e72014-04-15 12:44:32 -04001632 radeon_hwmon_fini(rdev);
Fabian Frederick9c244872014-07-04 21:37:09 +02001633 kfree(rdev->pm.power_state);
Alex Deucherda321c82013-04-12 13:55:22 -04001634}
1635
1636void radeon_pm_fini(struct radeon_device *rdev)
1637{
1638 if (rdev->pm.pm_method == PM_METHOD_DPM)
1639 radeon_pm_fini_dpm(rdev);
1640 else
1641 radeon_pm_fini_old(rdev);
1642}
1643
1644static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001645{
1646 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001647 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001648 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001649
Alex Deucherce8f5372010-05-07 15:10:16 -04001650 if (rdev->pm.num_power_states < 2)
1651 return;
1652
Rafał Miłeckic913e232009-12-22 23:02:16 +01001653 mutex_lock(&rdev->pm.mutex);
1654
1655 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001656 rdev->pm.active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001657 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1658 list_for_each_entry(crtc,
1659 &ddev->mode_config.crtc_list, head) {
1660 radeon_crtc = to_radeon_crtc(crtc);
1661 if (radeon_crtc->enabled) {
1662 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
1663 rdev->pm.active_crtc_count++;
1664 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001665 }
1666 }
1667
Alex Deucherce8f5372010-05-07 15:10:16 -04001668 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1669 radeon_pm_update_profile(rdev);
1670 radeon_pm_set_clocks(rdev);
1671 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1672 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1673 if (rdev->pm.active_crtc_count > 1) {
1674 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1675 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001676
Alex Deucherce8f5372010-05-07 15:10:16 -04001677 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1678 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1679 radeon_pm_get_dynpm_state(rdev);
1680 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001681
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001682 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001683 }
1684 } else if (rdev->pm.active_crtc_count == 1) {
1685 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001686
Alex Deucherce8f5372010-05-07 15:10:16 -04001687 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1688 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1689 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1690 radeon_pm_get_dynpm_state(rdev);
1691 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001692
Tejun Heo32c87fc2011-01-03 14:49:32 +01001693 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1694 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001695 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1696 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001697 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1698 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001699 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001700 }
1701 } else { /* count == 0 */
1702 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1703 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001704
Alex Deucherce8f5372010-05-07 15:10:16 -04001705 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1706 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1707 radeon_pm_get_dynpm_state(rdev);
1708 radeon_pm_set_clocks(rdev);
1709 }
1710 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001711 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001712 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001713
1714 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001715}
1716
Alex Deucherda321c82013-04-12 13:55:22 -04001717static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1718{
1719 struct drm_device *ddev = rdev->ddev;
1720 struct drm_crtc *crtc;
1721 struct radeon_crtc *radeon_crtc;
1722
Alex Deucher6c7bcce2013-12-18 14:07:14 -05001723 if (!rdev->pm.dpm_enabled)
1724 return;
1725
Alex Deucherda321c82013-04-12 13:55:22 -04001726 mutex_lock(&rdev->pm.mutex);
1727
Alex Deucher5ca302f2012-11-30 10:56:57 -05001728 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001729 rdev->pm.dpm.new_active_crtcs = 0;
1730 rdev->pm.dpm.new_active_crtc_count = 0;
Alex Deucher3ed9a332014-04-15 12:44:33 -04001731 if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
1732 list_for_each_entry(crtc,
1733 &ddev->mode_config.crtc_list, head) {
1734 radeon_crtc = to_radeon_crtc(crtc);
1735 if (crtc->enabled) {
1736 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1737 rdev->pm.dpm.new_active_crtc_count++;
1738 }
Alex Deucherda321c82013-04-12 13:55:22 -04001739 }
1740 }
1741
Alex Deucher5ca302f2012-11-30 10:56:57 -05001742 /* update battery/ac status */
1743 if (power_supply_is_system_supplied() > 0)
1744 rdev->pm.dpm.ac_power = true;
1745 else
1746 rdev->pm.dpm.ac_power = false;
1747
Alex Deucherda321c82013-04-12 13:55:22 -04001748 radeon_dpm_change_power_state_locked(rdev);
1749
1750 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001751
Alex Deucherda321c82013-04-12 13:55:22 -04001752}
1753
1754void radeon_pm_compute_clocks(struct radeon_device *rdev)
1755{
1756 if (rdev->pm.pm_method == PM_METHOD_DPM)
1757 radeon_pm_compute_clocks_dpm(rdev);
1758 else
1759 radeon_pm_compute_clocks_old(rdev);
1760}
1761
Alex Deucherce8f5372010-05-07 15:10:16 -04001762static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001763{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001764 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001765 bool in_vbl = true;
1766
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001767 /* Iterate over all active crtc's. All crtc's must be in vblank,
1768 * otherwise return in_vbl == false.
1769 */
1770 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1771 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleiner5b5561b2015-11-25 20:14:31 +01001772 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev,
1773 crtc,
1774 USE_REAL_VBLANKSTART,
Ville Syrjälä3bb403b2015-09-14 22:43:44 +03001775 &vpos, &hpos, NULL, NULL,
1776 &rdev->mode_info.crtcs[crtc]->base.hwmode);
Mario Kleinerf5a80202010-10-23 04:42:17 +02001777 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
Daniel Vetter3d3cbd82014-09-10 17:36:11 +02001778 !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
Dave Airlief7352612010-02-18 15:58:36 +10001779 in_vbl = false;
1780 }
1781 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001782
1783 return in_vbl;
1784}
1785
Alex Deucherce8f5372010-05-07 15:10:16 -04001786static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001787{
1788 u32 stat_crtc = 0;
1789 bool in_vbl = radeon_pm_in_vbl(rdev);
1790
Dave Airlief7352612010-02-18 15:58:36 +10001791 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001792 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001793 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001794 return in_vbl;
1795}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001796
Alex Deucherce8f5372010-05-07 15:10:16 -04001797static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001798{
1799 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001800 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001801 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001802 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001803
Matthew Garrettd9932a32010-04-26 16:02:26 -04001804 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001805 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001806 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001807 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001808 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001809
Alex Deucher74652802011-08-25 13:39:48 -04001810 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001811 struct radeon_ring *ring = &rdev->ring[i];
1812
1813 if (ring->ready) {
1814 not_processed += radeon_fence_count_emitted(rdev, i);
1815 if (not_processed >= 3)
1816 break;
1817 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001818 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001819
1820 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001821 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1822 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1823 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1824 rdev->pm.dynpm_can_upclock) {
1825 rdev->pm.dynpm_planned_action =
1826 DYNPM_ACTION_UPCLOCK;
1827 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001828 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1829 }
1830 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001831 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1832 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1833 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1834 rdev->pm.dynpm_can_downclock) {
1835 rdev->pm.dynpm_planned_action =
1836 DYNPM_ACTION_DOWNCLOCK;
1837 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001838 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1839 }
1840 }
1841
Alex Deucherd7311172010-05-03 01:13:14 -04001842 /* Note, radeon_pm_set_clocks is called with static_switch set
1843 * to false since we want to wait for vbl to avoid flicker.
1844 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001845 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1846 jiffies > rdev->pm.dynpm_action_timeout) {
1847 radeon_pm_get_dynpm_state(rdev);
1848 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001849 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001850
Tejun Heo32c87fc2011-01-03 14:49:32 +01001851 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1852 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001853 }
1854 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001855 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001856}
1857
Rafał Miłecki74338742009-11-03 00:53:02 +01001858/*
1859 * Debugfs info
1860 */
1861#if defined(CONFIG_DEBUG_FS)
1862
1863static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1864{
1865 struct drm_info_node *node = (struct drm_info_node *) m->private;
1866 struct drm_device *dev = node->minor->dev;
1867 struct radeon_device *rdev = dev->dev_private;
Alex Deucher4f2f2032014-05-19 19:21:29 -04001868 struct drm_device *ddev = rdev->ddev;
Rafał Miłecki74338742009-11-03 00:53:02 +01001869
Alex Deucher4f2f2032014-05-19 19:21:29 -04001870 if ((rdev->flags & RADEON_IS_PX) &&
1871 (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
1872 seq_printf(m, "PX asic powered off\n");
1873 } else if (rdev->pm.dpm_enabled) {
Alex Deucher1316b792013-06-28 09:28:39 -04001874 mutex_lock(&rdev->pm.mutex);
1875 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1876 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1877 else
Alex Deucher71375922013-07-02 09:11:39 -04001878 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001879 mutex_unlock(&rdev->pm.mutex);
1880 } else {
1881 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1882 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1883 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1884 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1885 else
1886 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1887 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1888 if (rdev->asic->pm.get_memory_clock)
1889 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1890 if (rdev->pm.current_vddc)
1891 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1892 if (rdev->asic->pm.get_pcie_lanes)
1893 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1894 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001895
1896 return 0;
1897}
1898
1899static struct drm_info_list radeon_pm_info_list[] = {
1900 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1901};
1902#endif
1903
Rafał Miłeckic913e232009-12-22 23:02:16 +01001904static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001905{
1906#if defined(CONFIG_DEBUG_FS)
1907 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1908#else
1909 return 0;
1910#endif
1911}