blob: 59d7a0c865892c7523f81b7660b51a1695424b6a [file] [log] [blame]
Rafał Miłecki74338742009-11-03 00:53:02 +01001/*
2 * Permission is hereby granted, free of charge, to any person obtaining a
3 * copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation
5 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
16 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
17 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
18 * OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Authors: Rafał Miłecki <zajec5@gmail.com>
Alex Deucher56278a82009-12-28 13:58:44 -050021 * Alex Deucher <alexdeucher@gmail.com>
Rafał Miłecki74338742009-11-03 00:53:02 +010022 */
David Howells760285e2012-10-02 18:01:07 +010023#include <drm/drmP.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010024#include "radeon.h"
Dave Airlief7352612010-02-18 15:58:36 +100025#include "avivod.h"
Alex Deucher8a83ec52011-04-12 14:49:23 -040026#include "atom.h"
Alex Deucherce8f5372010-05-07 15:10:16 -040027#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040028#include <linux/hwmon.h>
29#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010030
Rafał Miłeckic913e232009-12-22 23:02:16 +010031#define RADEON_IDLE_LOOP_MS 100
32#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010033#define RADEON_WAIT_VBLANK_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010034
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040035static const char *radeon_pm_state_type_name[5] = {
Alex Deuchereb2c27a2012-10-01 18:28:09 -040036 "",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040037 "Powersave",
38 "Battery",
39 "Balanced",
40 "Performance",
41};
42
Alex Deucherce8f5372010-05-07 15:10:16 -040043static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010044static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040045static bool radeon_pm_in_vbl(struct radeon_device *rdev);
46static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
47static void radeon_pm_update_profile(struct radeon_device *rdev);
48static void radeon_pm_set_clocks(struct radeon_device *rdev);
49
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040050int radeon_pm_get_type_index(struct radeon_device *rdev,
51 enum radeon_pm_state_type ps_type,
52 int instance)
53{
54 int i;
55 int found_instance = -1;
56
57 for (i = 0; i < rdev->pm.num_power_states; i++) {
58 if (rdev->pm.power_state[i].type == ps_type) {
59 found_instance++;
60 if (found_instance == instance)
61 return i;
62 }
63 }
64 /* return default if no match */
65 return rdev->pm.default_power_state_index;
66}
67
Alex Deucherc4917072012-07-31 17:14:35 -040068void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
Alex Deucherce8f5372010-05-07 15:10:16 -040069{
Alex Deucherc4917072012-07-31 17:14:35 -040070 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
71 if (rdev->pm.profile == PM_PROFILE_AUTO) {
72 mutex_lock(&rdev->pm.mutex);
73 radeon_pm_update_profile(rdev);
74 radeon_pm_set_clocks(rdev);
75 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -040076 }
77 }
Alex Deucherce8f5372010-05-07 15:10:16 -040078}
Alex Deucherce8f5372010-05-07 15:10:16 -040079
80static void radeon_pm_update_profile(struct radeon_device *rdev)
81{
82 switch (rdev->pm.profile) {
83 case PM_PROFILE_DEFAULT:
84 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
85 break;
86 case PM_PROFILE_AUTO:
87 if (power_supply_is_system_supplied() > 0) {
88 if (rdev->pm.active_crtc_count > 1)
89 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
90 else
91 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
92 } else {
93 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -040094 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040095 else
Alex Deucherc9e75b22010-06-02 17:56:01 -040096 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040097 }
98 break;
99 case PM_PROFILE_LOW:
100 if (rdev->pm.active_crtc_count > 1)
101 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
102 else
103 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
104 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400105 case PM_PROFILE_MID:
106 if (rdev->pm.active_crtc_count > 1)
107 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
108 else
109 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
110 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400111 case PM_PROFILE_HIGH:
112 if (rdev->pm.active_crtc_count > 1)
113 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
114 else
115 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
116 break;
117 }
118
119 if (rdev->pm.active_crtc_count == 0) {
120 rdev->pm.requested_power_state_index =
121 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
122 rdev->pm.requested_clock_mode_index =
123 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
124 } else {
125 rdev->pm.requested_power_state_index =
126 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
127 rdev->pm.requested_clock_mode_index =
128 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
129 }
130}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100131
Matthew Garrett5876dd22010-04-26 15:52:20 -0400132static void radeon_unmap_vram_bos(struct radeon_device *rdev)
133{
134 struct radeon_bo *bo, *n;
135
136 if (list_empty(&rdev->gem.objects))
137 return;
138
139 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
140 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
141 ttm_bo_unmap_virtual(&bo->tbo);
142 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400143}
144
Alex Deucherce8f5372010-05-07 15:10:16 -0400145static void radeon_sync_with_vblank(struct radeon_device *rdev)
146{
147 if (rdev->pm.active_crtcs) {
148 rdev->pm.vblank_sync = false;
149 wait_event_timeout(
150 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
151 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
152 }
153}
154
155static void radeon_set_power_state(struct radeon_device *rdev)
156{
157 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400158 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400159
160 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
161 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
162 return;
163
164 if (radeon_gui_idle(rdev)) {
165 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
166 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500167 if (sclk > rdev->pm.default_sclk)
168 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400169
Alex Deucher27810fb2012-10-01 19:25:11 -0400170 /* starting with BTC, there is one state that is used for both
171 * MH and SH. Difference is that we always use the high clock index for
Alex Deucher7ae764b2013-02-11 08:44:48 -0500172 * mclk and vddci.
Alex Deucher27810fb2012-10-01 19:25:11 -0400173 */
174 if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
175 (rdev->family >= CHIP_BARTS) &&
176 rdev->pm.active_crtc_count &&
177 ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
178 (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
179 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
180 clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
181 else
182 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
183 clock_info[rdev->pm.requested_clock_mode_index].mclk;
184
Alex Deucher9ace9f72011-01-06 21:19:26 -0500185 if (mclk > rdev->pm.default_mclk)
186 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400187
Alex Deucher92645872010-05-27 17:01:41 -0400188 /* upvolt before raising clocks, downvolt after lowering clocks */
189 if (sclk < rdev->pm.current_sclk)
190 misc_after = true;
191
192 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400193
194 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400195 if (!radeon_pm_in_vbl(rdev))
196 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400197 }
198
Alex Deucher92645872010-05-27 17:01:41 -0400199 radeon_pm_prepare(rdev);
200
201 if (!misc_after)
202 /* voltage, pcie lanes, etc.*/
203 radeon_pm_misc(rdev);
204
205 /* set engine clock */
206 if (sclk != rdev->pm.current_sclk) {
207 radeon_pm_debug_check_in_vbl(rdev, false);
208 radeon_set_engine_clock(rdev, sclk);
209 radeon_pm_debug_check_in_vbl(rdev, true);
210 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000211 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400212 }
213
214 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500215 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400216 radeon_pm_debug_check_in_vbl(rdev, false);
217 radeon_set_memory_clock(rdev, mclk);
218 radeon_pm_debug_check_in_vbl(rdev, true);
219 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000220 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400221 }
222
223 if (misc_after)
224 /* voltage, pcie lanes, etc.*/
225 radeon_pm_misc(rdev);
226
227 radeon_pm_finish(rdev);
228
Alex Deucherce8f5372010-05-07 15:10:16 -0400229 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
230 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
231 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000232 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400233}
234
235static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400236{
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500237 int i, r;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400238
Alex Deucher4e186b22010-08-13 10:53:35 -0400239 /* no need to take locks, etc. if nothing's going to change */
240 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
241 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
242 return;
243
Matthew Garrett612e06c2010-04-27 17:16:58 -0400244 mutex_lock(&rdev->ddev->struct_mutex);
Christian Königdb7fce32012-05-11 14:57:18 +0200245 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200246 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400247
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400248 /* wait for the rings to drain */
249 for (i = 0; i < RADEON_NUM_RINGS; i++) {
250 struct radeon_ring *ring = &rdev->ring[i];
Jerome Glisse5f8f6352012-12-17 11:04:32 -0500251 if (!ring->ready) {
252 continue;
253 }
254 r = radeon_fence_wait_empty_locked(rdev, i);
255 if (r) {
256 /* needs a GPU reset dont reset here */
257 mutex_unlock(&rdev->ring_lock);
258 up_write(&rdev->pm.mclk_lock);
259 mutex_unlock(&rdev->ddev->struct_mutex);
260 return;
261 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400262 }
Alex Deucher95f5a3a2012-08-10 13:12:08 -0400263
Matthew Garrett5876dd22010-04-26 15:52:20 -0400264 radeon_unmap_vram_bos(rdev);
265
Alex Deucherce8f5372010-05-07 15:10:16 -0400266 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400267 for (i = 0; i < rdev->num_crtc; i++) {
268 if (rdev->pm.active_crtcs & (1 << i)) {
269 rdev->pm.req_vblank |= (1 << i);
270 drm_vblank_get(rdev->ddev, i);
271 }
272 }
273 }
Alex Deucher539d2412010-04-29 00:22:43 -0400274
Alex Deucherce8f5372010-05-07 15:10:16 -0400275 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400276
Alex Deucherce8f5372010-05-07 15:10:16 -0400277 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400278 for (i = 0; i < rdev->num_crtc; i++) {
279 if (rdev->pm.req_vblank & (1 << i)) {
280 rdev->pm.req_vblank &= ~(1 << i);
281 drm_vblank_put(rdev->ddev, i);
282 }
283 }
284 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400285
Alex Deuchera4248162010-04-24 14:50:23 -0400286 /* update display watermarks based on new power state */
287 radeon_update_bandwidth_info(rdev);
288 if (rdev->pm.active_crtc_count)
289 radeon_bandwidth_update(rdev);
290
Alex Deucherce8f5372010-05-07 15:10:16 -0400291 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400292
Christian Königd6999bc2012-05-09 15:34:45 +0200293 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200294 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400295 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400296}
297
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400298static void radeon_pm_print_states(struct radeon_device *rdev)
299{
300 int i, j;
301 struct radeon_power_state *power_state;
302 struct radeon_pm_clock_info *clock_info;
303
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000304 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400305 for (i = 0; i < rdev->pm.num_power_states; i++) {
306 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000307 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400308 radeon_pm_state_type_name[power_state->type]);
309 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000310 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400311 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000312 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400313 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000314 DRM_DEBUG_DRIVER("\tSingle display only\n");
315 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400316 for (j = 0; j < power_state->num_clock_modes; j++) {
317 clock_info = &(power_state->clock_info[j]);
318 if (rdev->flags & RADEON_IS_IGP)
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400319 DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
320 j,
321 clock_info->sclk * 10);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400322 else
Alex Deuchereb2c27a2012-10-01 18:28:09 -0400323 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
324 j,
325 clock_info->sclk * 10,
326 clock_info->mclk * 10,
327 clock_info->voltage.voltage);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400328 }
329 }
330}
331
Alex Deucherce8f5372010-05-07 15:10:16 -0400332static ssize_t radeon_get_pm_profile(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400335{
336 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
337 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400338 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400339
Alex Deucherce8f5372010-05-07 15:10:16 -0400340 return snprintf(buf, PAGE_SIZE, "%s\n",
341 (cp == PM_PROFILE_AUTO) ? "auto" :
342 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100343 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400344 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400345}
346
Alex Deucherce8f5372010-05-07 15:10:16 -0400347static ssize_t radeon_set_pm_profile(struct device *dev,
348 struct device_attribute *attr,
349 const char *buf,
350 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400351{
352 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
353 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400354
355 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400356 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
357 if (strncmp("default", buf, strlen("default")) == 0)
358 rdev->pm.profile = PM_PROFILE_DEFAULT;
359 else if (strncmp("auto", buf, strlen("auto")) == 0)
360 rdev->pm.profile = PM_PROFILE_AUTO;
361 else if (strncmp("low", buf, strlen("low")) == 0)
362 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400363 else if (strncmp("mid", buf, strlen("mid")) == 0)
364 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400365 else if (strncmp("high", buf, strlen("high")) == 0)
366 rdev->pm.profile = PM_PROFILE_HIGH;
367 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000368 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400369 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400370 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400371 radeon_pm_update_profile(rdev);
372 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000373 } else
374 count = -EINVAL;
375
Alex Deucherce8f5372010-05-07 15:10:16 -0400376fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400377 mutex_unlock(&rdev->pm.mutex);
378
379 return count;
380}
381
Alex Deucherce8f5372010-05-07 15:10:16 -0400382static ssize_t radeon_get_pm_method(struct device *dev,
383 struct device_attribute *attr,
384 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400385{
386 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
387 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400388 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400389
390 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherda321c82013-04-12 13:55:22 -0400391 (pm == PM_METHOD_DYNPM) ? "dynpm" :
392 (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
Alex Deuchera4248162010-04-24 14:50:23 -0400393}
394
Alex Deucherce8f5372010-05-07 15:10:16 -0400395static ssize_t radeon_set_pm_method(struct device *dev,
396 struct device_attribute *attr,
397 const char *buf,
398 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400399{
400 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
401 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400402
Alex Deucherda321c82013-04-12 13:55:22 -0400403 /* we don't support the legacy modes with dpm */
404 if (rdev->pm.pm_method == PM_METHOD_DPM) {
405 count = -EINVAL;
406 goto fail;
407 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400408
409 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400410 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400411 rdev->pm.pm_method = PM_METHOD_DYNPM;
412 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
413 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400414 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400415 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
416 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400417 /* disable dynpm */
418 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
419 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000420 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400421 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100422 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400423 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000424 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400425 goto fail;
426 }
427 radeon_pm_compute_clocks(rdev);
428fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400429 return count;
430}
431
Alex Deucherda321c82013-04-12 13:55:22 -0400432static ssize_t radeon_get_dpm_state(struct device *dev,
433 struct device_attribute *attr,
434 char *buf)
435{
436 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
437 struct radeon_device *rdev = ddev->dev_private;
438 enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
439
440 return snprintf(buf, PAGE_SIZE, "%s\n",
441 (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
442 (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
443}
444
445static ssize_t radeon_set_dpm_state(struct device *dev,
446 struct device_attribute *attr,
447 const char *buf,
448 size_t count)
449{
450 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
451 struct radeon_device *rdev = ddev->dev_private;
452
453 mutex_lock(&rdev->pm.mutex);
454 if (strncmp("battery", buf, strlen("battery")) == 0)
455 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
456 else if (strncmp("balanced", buf, strlen("balanced")) == 0)
457 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
458 else if (strncmp("performance", buf, strlen("performance")) == 0)
459 rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
460 else {
461 mutex_unlock(&rdev->pm.mutex);
462 count = -EINVAL;
463 goto fail;
464 }
465 mutex_unlock(&rdev->pm.mutex);
466 radeon_pm_compute_clocks(rdev);
467fail:
468 return count;
469}
470
Alex Deucher70d01a52013-07-02 18:38:02 -0400471static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
472 struct device_attribute *attr,
473 char *buf)
474{
475 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
476 struct radeon_device *rdev = ddev->dev_private;
477 enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
478
479 return snprintf(buf, PAGE_SIZE, "%s\n",
480 (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
481 (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
482}
483
484static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
485 struct device_attribute *attr,
486 const char *buf,
487 size_t count)
488{
489 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
490 struct radeon_device *rdev = ddev->dev_private;
491 enum radeon_dpm_forced_level level;
492 int ret = 0;
493
494 mutex_lock(&rdev->pm.mutex);
495 if (strncmp("low", buf, strlen("low")) == 0) {
496 level = RADEON_DPM_FORCED_LEVEL_LOW;
497 } else if (strncmp("high", buf, strlen("high")) == 0) {
498 level = RADEON_DPM_FORCED_LEVEL_HIGH;
499 } else if (strncmp("auto", buf, strlen("auto")) == 0) {
500 level = RADEON_DPM_FORCED_LEVEL_AUTO;
501 } else {
502 mutex_unlock(&rdev->pm.mutex);
503 count = -EINVAL;
504 goto fail;
505 }
506 if (rdev->asic->dpm.force_performance_level) {
507 ret = radeon_dpm_force_performance_level(rdev, level);
508 if (ret)
509 count = -EINVAL;
510 }
511 mutex_unlock(&rdev->pm.mutex);
512fail:
513 return count;
514}
515
Alex Deucherce8f5372010-05-07 15:10:16 -0400516static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
517static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deucherda321c82013-04-12 13:55:22 -0400518static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -0400519static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
520 radeon_get_dpm_forced_performance_level,
521 radeon_set_dpm_forced_performance_level);
Alex Deuchera4248162010-04-24 14:50:23 -0400522
Alex Deucher21a81222010-07-02 12:58:16 -0400523static ssize_t radeon_hwmon_show_temp(struct device *dev,
524 struct device_attribute *attr,
525 char *buf)
526{
527 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
528 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500529 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400530
Alex Deucher6bd1c382013-06-21 14:38:03 -0400531 if (rdev->asic->pm.get_temperature)
532 temp = radeon_get_temperature(rdev);
533 else
Alex Deucher21a81222010-07-02 12:58:16 -0400534 temp = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400535
536 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
537}
538
539static ssize_t radeon_hwmon_show_name(struct device *dev,
540 struct device_attribute *attr,
541 char *buf)
542{
543 return sprintf(buf, "radeon\n");
544}
545
546static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
547static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
548
549static struct attribute *hwmon_attributes[] = {
550 &sensor_dev_attr_temp1_input.dev_attr.attr,
551 &sensor_dev_attr_name.dev_attr.attr,
552 NULL
553};
554
555static const struct attribute_group hwmon_attrgroup = {
556 .attrs = hwmon_attributes,
557};
558
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200559static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400560{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200561 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400562
563 rdev->pm.int_hwmon_dev = NULL;
564
565 switch (rdev->pm.int_thermal_type) {
566 case THERMAL_TYPE_RV6XX:
567 case THERMAL_TYPE_RV770:
568 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400569 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500570 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400571 case THERMAL_TYPE_SI:
Alex Deucher6bd1c382013-06-21 14:38:03 -0400572 if (rdev->asic->pm.get_temperature == NULL)
Alex Deucher5d7486c2012-03-20 17:18:29 -0400573 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400574 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200575 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
576 err = PTR_ERR(rdev->pm.int_hwmon_dev);
577 dev_err(rdev->dev,
578 "Unable to register hwmon device: %d\n", err);
579 break;
580 }
Alex Deucher21a81222010-07-02 12:58:16 -0400581 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
582 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
583 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200584 if (err) {
585 dev_err(rdev->dev,
586 "Unable to create hwmon sysfs file: %d\n", err);
587 hwmon_device_unregister(rdev->dev);
588 }
Alex Deucher21a81222010-07-02 12:58:16 -0400589 break;
590 default:
591 break;
592 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200593
594 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400595}
596
597static void radeon_hwmon_fini(struct radeon_device *rdev)
598{
599 if (rdev->pm.int_hwmon_dev) {
600 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
601 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
602 }
603}
604
Alex Deucherda321c82013-04-12 13:55:22 -0400605static void radeon_dpm_thermal_work_handler(struct work_struct *work)
606{
607 struct radeon_device *rdev =
608 container_of(work, struct radeon_device,
609 pm.dpm.thermal.work);
610 /* switch to the thermal state */
611 enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
612
613 if (!rdev->pm.dpm_enabled)
614 return;
615
616 if (rdev->asic->pm.get_temperature) {
617 int temp = radeon_get_temperature(rdev);
618
619 if (temp < rdev->pm.dpm.thermal.min_temp)
620 /* switch back the user state */
621 dpm_state = rdev->pm.dpm.user_state;
622 } else {
623 if (rdev->pm.dpm.thermal.high_to_low)
624 /* switch back the user state */
625 dpm_state = rdev->pm.dpm.user_state;
626 }
627 radeon_dpm_enable_power_state(rdev, dpm_state);
628}
629
630static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
631 enum radeon_pm_state_type dpm_state)
632{
633 int i;
634 struct radeon_ps *ps;
635 u32 ui_class;
Alex Deucher48783062013-07-08 11:35:06 -0400636 bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
637 true : false;
638
639 /* check if the vblank period is too short to adjust the mclk */
640 if (single_display && rdev->asic->dpm.vblank_too_short) {
641 if (radeon_dpm_vblank_too_short(rdev))
642 single_display = false;
643 }
Alex Deucherda321c82013-04-12 13:55:22 -0400644
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400645 /* certain older asics have a separare 3D performance state,
646 * so try that first if the user selected performance
647 */
648 if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
649 dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
Alex Deucherda321c82013-04-12 13:55:22 -0400650 /* balanced states don't exist at the moment */
651 if (dpm_state == POWER_STATE_TYPE_BALANCED)
652 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
653
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400654restart_search:
Alex Deucherda321c82013-04-12 13:55:22 -0400655 /* Pick the best power state based on current conditions */
656 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
657 ps = &rdev->pm.dpm.ps[i];
658 ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
659 switch (dpm_state) {
660 /* user states */
661 case POWER_STATE_TYPE_BATTERY:
662 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
663 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400664 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400665 return ps;
666 } else
667 return ps;
668 }
669 break;
670 case POWER_STATE_TYPE_BALANCED:
671 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
672 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400673 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400674 return ps;
675 } else
676 return ps;
677 }
678 break;
679 case POWER_STATE_TYPE_PERFORMANCE:
680 if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
681 if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
Alex Deucher48783062013-07-08 11:35:06 -0400682 if (single_display)
Alex Deucherda321c82013-04-12 13:55:22 -0400683 return ps;
684 } else
685 return ps;
686 }
687 break;
688 /* internal states */
689 case POWER_STATE_TYPE_INTERNAL_UVD:
690 return rdev->pm.dpm.uvd_ps;
691 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
692 if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
693 return ps;
694 break;
695 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
696 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
697 return ps;
698 break;
699 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
700 if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
701 return ps;
702 break;
703 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
704 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
705 return ps;
706 break;
707 case POWER_STATE_TYPE_INTERNAL_BOOT:
708 return rdev->pm.dpm.boot_ps;
709 case POWER_STATE_TYPE_INTERNAL_THERMAL:
710 if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
711 return ps;
712 break;
713 case POWER_STATE_TYPE_INTERNAL_ACPI:
714 if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
715 return ps;
716 break;
717 case POWER_STATE_TYPE_INTERNAL_ULV:
718 if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
719 return ps;
720 break;
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400721 case POWER_STATE_TYPE_INTERNAL_3DPERF:
722 if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
723 return ps;
724 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400725 default:
726 break;
727 }
728 }
729 /* use a fallback state if we didn't match */
730 switch (dpm_state) {
731 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
Alex Deucherce3537d2013-07-24 12:12:49 -0400732 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
733 goto restart_search;
Alex Deucherda321c82013-04-12 13:55:22 -0400734 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
735 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
736 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
737 return rdev->pm.dpm.uvd_ps;
738 case POWER_STATE_TYPE_INTERNAL_THERMAL:
739 dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
740 goto restart_search;
741 case POWER_STATE_TYPE_INTERNAL_ACPI:
742 dpm_state = POWER_STATE_TYPE_BATTERY;
743 goto restart_search;
744 case POWER_STATE_TYPE_BATTERY:
Alex Deucheredcaa5b2013-07-05 11:48:31 -0400745 case POWER_STATE_TYPE_BALANCED:
746 case POWER_STATE_TYPE_INTERNAL_3DPERF:
Alex Deucherda321c82013-04-12 13:55:22 -0400747 dpm_state = POWER_STATE_TYPE_PERFORMANCE;
748 goto restart_search;
749 default:
750 break;
751 }
752
753 return NULL;
754}
755
756static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
757{
758 int i;
759 struct radeon_ps *ps;
760 enum radeon_pm_state_type dpm_state;
Alex Deucher84dd1922013-01-16 12:52:04 -0500761 int ret;
Alex Deucherda321c82013-04-12 13:55:22 -0400762
763 /* if dpm init failed */
764 if (!rdev->pm.dpm_enabled)
765 return;
766
767 if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
768 /* add other state override checks here */
Alex Deucher8a227552013-06-21 15:12:57 -0400769 if ((!rdev->pm.dpm.thermal_active) &&
770 (!rdev->pm.dpm.uvd_active))
Alex Deucherda321c82013-04-12 13:55:22 -0400771 rdev->pm.dpm.state = rdev->pm.dpm.user_state;
772 }
773 dpm_state = rdev->pm.dpm.state;
774
775 ps = radeon_dpm_pick_power_state(rdev, dpm_state);
776 if (ps)
Alex Deucher89c9bc52013-01-16 14:40:26 -0500777 rdev->pm.dpm.requested_ps = ps;
Alex Deucherda321c82013-04-12 13:55:22 -0400778 else
779 return;
780
Alex Deucherd22b7e42012-11-29 19:27:56 -0500781 /* no need to reprogram if nothing changed unless we are on BTC+ */
Alex Deucherda321c82013-04-12 13:55:22 -0400782 if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
Alex Deucherd22b7e42012-11-29 19:27:56 -0500783 if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
784 /* for pre-BTC and APUs if the num crtcs changed but state is the same,
785 * all we need to do is update the display configuration.
786 */
787 if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
788 /* update display watermarks based on new power state */
789 radeon_bandwidth_update(rdev);
790 /* update displays */
791 radeon_dpm_display_configuration_changed(rdev);
792 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
793 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
794 }
795 return;
796 } else {
797 /* for BTC+ if the num crtcs hasn't changed and state is the same,
798 * nothing to do, if the num crtcs is > 1 and state is the same,
799 * update display configuration.
800 */
801 if (rdev->pm.dpm.new_active_crtcs ==
802 rdev->pm.dpm.current_active_crtcs) {
803 return;
804 } else {
805 if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
806 (rdev->pm.dpm.new_active_crtc_count > 1)) {
807 /* update display watermarks based on new power state */
808 radeon_bandwidth_update(rdev);
809 /* update displays */
810 radeon_dpm_display_configuration_changed(rdev);
811 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
812 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
813 return;
814 }
815 }
Alex Deucherda321c82013-04-12 13:55:22 -0400816 }
Alex Deucherda321c82013-04-12 13:55:22 -0400817 }
818
819 printk("switching from power state:\n");
820 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
821 printk("switching to power state:\n");
822 radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
823
824 mutex_lock(&rdev->ddev->struct_mutex);
825 down_write(&rdev->pm.mclk_lock);
826 mutex_lock(&rdev->ring_lock);
827
Alex Deucher89c9bc52013-01-16 14:40:26 -0500828 ret = radeon_dpm_pre_set_power_state(rdev);
829 if (ret)
830 goto done;
Alex Deucher84dd1922013-01-16 12:52:04 -0500831
Alex Deucherda321c82013-04-12 13:55:22 -0400832 /* update display watermarks based on new power state */
833 radeon_bandwidth_update(rdev);
834 /* update displays */
835 radeon_dpm_display_configuration_changed(rdev);
836
837 rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
838 rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
839
840 /* wait for the rings to drain */
841 for (i = 0; i < RADEON_NUM_RINGS; i++) {
842 struct radeon_ring *ring = &rdev->ring[i];
843 if (ring->ready)
844 radeon_fence_wait_empty_locked(rdev, i);
845 }
846
847 /* program the new power state */
848 radeon_dpm_set_power_state(rdev);
849
850 /* update current power state */
851 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
852
Alex Deucher89c9bc52013-01-16 14:40:26 -0500853 radeon_dpm_post_set_power_state(rdev);
Alex Deucher84dd1922013-01-16 12:52:04 -0500854
855done:
Alex Deucherda321c82013-04-12 13:55:22 -0400856 mutex_unlock(&rdev->ring_lock);
857 up_write(&rdev->pm.mclk_lock);
858 mutex_unlock(&rdev->ddev->struct_mutex);
859}
860
861void radeon_dpm_enable_power_state(struct radeon_device *rdev,
862 enum radeon_pm_state_type dpm_state)
863{
864 if (!rdev->pm.dpm_enabled)
865 return;
866
867 mutex_lock(&rdev->pm.mutex);
868 switch (dpm_state) {
869 case POWER_STATE_TYPE_INTERNAL_THERMAL:
870 rdev->pm.dpm.thermal_active = true;
871 break;
Alex Deucher8a227552013-06-21 15:12:57 -0400872 case POWER_STATE_TYPE_INTERNAL_UVD:
873 case POWER_STATE_TYPE_INTERNAL_UVD_SD:
874 case POWER_STATE_TYPE_INTERNAL_UVD_HD:
875 case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
876 case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
877 rdev->pm.dpm.uvd_active = true;
878 break;
Alex Deucherda321c82013-04-12 13:55:22 -0400879 default:
880 rdev->pm.dpm.thermal_active = false;
Alex Deucher8a227552013-06-21 15:12:57 -0400881 rdev->pm.dpm.uvd_active = false;
Alex Deucherda321c82013-04-12 13:55:22 -0400882 break;
883 }
884 rdev->pm.dpm.state = dpm_state;
885 mutex_unlock(&rdev->pm.mutex);
886 radeon_pm_compute_clocks(rdev);
887}
888
Alex Deucherce3537d2013-07-24 12:12:49 -0400889void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
890{
891 enum radeon_pm_state_type dpm_state;
892
893 if (enable) {
894 mutex_lock(&rdev->pm.mutex);
895 rdev->pm.dpm.uvd_active = true;
896 if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
897 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
898 else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
899 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
900 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
901 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
902 else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
903 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
904 else
905 dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
906 rdev->pm.dpm.state = dpm_state;
907 mutex_unlock(&rdev->pm.mutex);
908 } else {
909 mutex_lock(&rdev->pm.mutex);
910 rdev->pm.dpm.uvd_active = false;
911 mutex_unlock(&rdev->pm.mutex);
912 }
913
914 radeon_pm_compute_clocks(rdev);
915}
916
Alex Deucherda321c82013-04-12 13:55:22 -0400917static void radeon_pm_suspend_old(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500918{
Alex Deucherce8f5372010-05-07 15:10:16 -0400919 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000920 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000921 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
922 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000923 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400924 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100925
926 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500927}
928
Alex Deucherda321c82013-04-12 13:55:22 -0400929static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
930{
931 mutex_lock(&rdev->pm.mutex);
932 /* disable dpm */
933 radeon_dpm_disable(rdev);
934 /* reset the power state */
935 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
936 rdev->pm.dpm_enabled = false;
937 mutex_unlock(&rdev->pm.mutex);
938}
939
940void radeon_pm_suspend(struct radeon_device *rdev)
941{
942 if (rdev->pm.pm_method == PM_METHOD_DPM)
943 radeon_pm_suspend_dpm(rdev);
944 else
945 radeon_pm_suspend_old(rdev);
946}
947
948static void radeon_pm_resume_old(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100949{
Alex Deuchered18a362011-01-06 21:19:32 -0500950 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -0400951 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucherc6cf7772013-07-05 13:14:30 -0400952 (rdev->family <= CHIP_HAINAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -0400953 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -0500954 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400955 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
956 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400957 if (rdev->pm.default_vddci)
958 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
959 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500960 if (rdev->pm.default_sclk)
961 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
962 if (rdev->pm.default_mclk)
963 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
964 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400965 /* asic init will reset the default power state */
966 mutex_lock(&rdev->pm.mutex);
967 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
968 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500969 rdev->pm.current_sclk = rdev->pm.default_sclk;
970 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400971 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
Alex Deucher2feea492011-04-12 14:49:24 -0400972 rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000973 if (rdev->pm.pm_method == PM_METHOD_DYNPM
974 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
975 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100976 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
977 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000978 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400979 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400980 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100981}
982
Alex Deucherda321c82013-04-12 13:55:22 -0400983static void radeon_pm_resume_dpm(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100984{
Dave Airlie26481fb2010-05-18 19:00:14 +1000985 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200986
Alex Deucherda321c82013-04-12 13:55:22 -0400987 /* asic init will reset to the boot state */
988 mutex_lock(&rdev->pm.mutex);
989 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
990 radeon_dpm_setup_asic(rdev);
991 ret = radeon_dpm_enable(rdev);
992 mutex_unlock(&rdev->pm.mutex);
993 if (ret) {
994 DRM_ERROR("radeon: dpm resume failed\n");
995 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucherc6cf7772013-07-05 13:14:30 -0400996 (rdev->family <= CHIP_HAINAN) &&
Alex Deucherda321c82013-04-12 13:55:22 -0400997 rdev->mc_fw) {
998 if (rdev->pm.default_vddc)
999 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1000 SET_VOLTAGE_TYPE_ASIC_VDDC);
1001 if (rdev->pm.default_vddci)
1002 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1003 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1004 if (rdev->pm.default_sclk)
1005 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1006 if (rdev->pm.default_mclk)
1007 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1008 }
1009 } else {
1010 rdev->pm.dpm_enabled = true;
1011 radeon_pm_compute_clocks(rdev);
1012 }
1013}
1014
1015void radeon_pm_resume(struct radeon_device *rdev)
1016{
1017 if (rdev->pm.pm_method == PM_METHOD_DPM)
1018 radeon_pm_resume_dpm(rdev);
1019 else
1020 radeon_pm_resume_old(rdev);
1021}
1022
1023static int radeon_pm_init_old(struct radeon_device *rdev)
1024{
1025 int ret;
1026
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001027 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -04001028 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1029 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1030 rdev->pm.dynpm_can_upclock = true;
1031 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -05001032 rdev->pm.default_sclk = rdev->clock.default_sclk;
1033 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -04001034 rdev->pm.current_sclk = rdev->clock.default_sclk;
1035 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -04001036 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001037
Alex Deucher56278a82009-12-28 13:58:44 -05001038 if (rdev->bios) {
1039 if (rdev->is_atom_bios)
1040 radeon_atombios_get_power_modes(rdev);
1041 else
1042 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -04001043 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -04001044 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -05001045 /* set up the default clocks if the MC ucode is loaded */
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001046 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucherc6cf7772013-07-05 13:14:30 -04001047 (rdev->family <= CHIP_HAINAN) &&
Alex Deucher2e3b3b12012-09-14 10:59:26 -04001048 rdev->mc_fw) {
Alex Deuchered18a362011-01-06 21:19:32 -05001049 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -04001050 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1051 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -04001052 if (rdev->pm.default_vddci)
1053 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1054 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -05001055 if (rdev->pm.default_sclk)
1056 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1057 if (rdev->pm.default_mclk)
1058 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1059 }
Alex Deucher56278a82009-12-28 13:58:44 -05001060 }
1061
Alex Deucher21a81222010-07-02 12:58:16 -04001062 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +02001063 ret = radeon_hwmon_init(rdev);
1064 if (ret)
1065 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001066
1067 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
1068
Alex Deucherce8f5372010-05-07 15:10:16 -04001069 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001070 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +10001071 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1072 if (ret)
1073 DRM_ERROR("failed to create device file for power profile\n");
1074 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1075 if (ret)
1076 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001077
Alex Deucherce8f5372010-05-07 15:10:16 -04001078 if (radeon_debugfs_pm_init(rdev)) {
1079 DRM_ERROR("Failed to register debugfs file for PM!\n");
1080 }
1081
1082 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +01001083 }
1084
1085 return 0;
1086}
1087
Alex Deucherda321c82013-04-12 13:55:22 -04001088static void radeon_dpm_print_power_states(struct radeon_device *rdev)
1089{
1090 int i;
1091
1092 for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
1093 printk("== power state %d ==\n", i);
1094 radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
1095 }
1096}
1097
1098static int radeon_pm_init_dpm(struct radeon_device *rdev)
1099{
1100 int ret;
1101
1102 /* default to performance state */
Alex Deucheredcaa5b2013-07-05 11:48:31 -04001103 rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
1104 rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
Alex Deucherda321c82013-04-12 13:55:22 -04001105 rdev->pm.default_sclk = rdev->clock.default_sclk;
1106 rdev->pm.default_mclk = rdev->clock.default_mclk;
1107 rdev->pm.current_sclk = rdev->clock.default_sclk;
1108 rdev->pm.current_mclk = rdev->clock.default_mclk;
1109 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
1110
1111 if (rdev->bios && rdev->is_atom_bios)
1112 radeon_atombios_get_power_modes(rdev);
1113 else
1114 return -EINVAL;
1115
1116 /* set up the internal thermal sensor if applicable */
1117 ret = radeon_hwmon_init(rdev);
1118 if (ret)
1119 return ret;
1120
1121 INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
1122 mutex_lock(&rdev->pm.mutex);
1123 radeon_dpm_init(rdev);
1124 rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
1125 radeon_dpm_print_power_states(rdev);
1126 radeon_dpm_setup_asic(rdev);
1127 ret = radeon_dpm_enable(rdev);
1128 mutex_unlock(&rdev->pm.mutex);
1129 if (ret) {
1130 rdev->pm.dpm_enabled = false;
1131 if ((rdev->family >= CHIP_BARTS) &&
Alex Deucherc6cf7772013-07-05 13:14:30 -04001132 (rdev->family <= CHIP_HAINAN) &&
Alex Deucherda321c82013-04-12 13:55:22 -04001133 rdev->mc_fw) {
1134 if (rdev->pm.default_vddc)
1135 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
1136 SET_VOLTAGE_TYPE_ASIC_VDDC);
1137 if (rdev->pm.default_vddci)
1138 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
1139 SET_VOLTAGE_TYPE_ASIC_VDDCI);
1140 if (rdev->pm.default_sclk)
1141 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
1142 if (rdev->pm.default_mclk)
1143 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
1144 }
1145 DRM_ERROR("radeon: dpm initialization failed\n");
1146 return ret;
1147 }
1148 rdev->pm.dpm_enabled = true;
1149 radeon_pm_compute_clocks(rdev);
1150
1151 if (rdev->pm.num_power_states > 1) {
1152 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
1153 if (ret)
1154 DRM_ERROR("failed to create device file for dpm state\n");
Alex Deucher70d01a52013-07-02 18:38:02 -04001155 ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
1156 if (ret)
1157 DRM_ERROR("failed to create device file for dpm state\n");
Alex Deucherda321c82013-04-12 13:55:22 -04001158 /* XXX: these are noops for dpm but are here for backwards compat */
1159 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
1160 if (ret)
1161 DRM_ERROR("failed to create device file for power profile\n");
1162 ret = device_create_file(rdev->dev, &dev_attr_power_method);
1163 if (ret)
1164 DRM_ERROR("failed to create device file for power method\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001165
1166 if (radeon_debugfs_pm_init(rdev)) {
1167 DRM_ERROR("Failed to register debugfs file for dpm!\n");
1168 }
1169
Alex Deucherda321c82013-04-12 13:55:22 -04001170 DRM_INFO("radeon: dpm initialized\n");
1171 }
1172
1173 return 0;
1174}
1175
1176int radeon_pm_init(struct radeon_device *rdev)
1177{
1178 /* enable dpm on rv6xx+ */
1179 switch (rdev->family) {
Alex Deucher4a6369e2013-04-12 14:04:10 -04001180 case CHIP_RV610:
1181 case CHIP_RV630:
1182 case CHIP_RV620:
1183 case CHIP_RV635:
1184 case CHIP_RV670:
Alex Deucher9d670062013-04-12 13:59:22 -04001185 case CHIP_RS780:
1186 case CHIP_RS880:
Alex Deucher66229b22013-06-26 00:11:19 -04001187 case CHIP_RV770:
1188 case CHIP_RV730:
1189 case CHIP_RV710:
1190 case CHIP_RV740:
Alex Deucherdc50ba72013-06-26 00:33:35 -04001191 case CHIP_CEDAR:
1192 case CHIP_REDWOOD:
1193 case CHIP_JUNIPER:
1194 case CHIP_CYPRESS:
1195 case CHIP_HEMLOCK:
Alex Deucher80ea2c12013-04-12 14:56:21 -04001196 case CHIP_PALM:
1197 case CHIP_SUMO:
1198 case CHIP_SUMO2:
Alex Deucher6596afd2013-06-26 00:15:24 -04001199 case CHIP_BARTS:
1200 case CHIP_TURKS:
1201 case CHIP_CAICOS:
Alex Deucher69e0b572013-04-12 16:42:42 -04001202 case CHIP_CAYMAN:
Alex Deucherd70229f2013-04-12 16:40:41 -04001203 case CHIP_ARUBA:
Alex Deuchera9e61412013-06-25 17:56:16 -04001204 case CHIP_TAHITI:
1205 case CHIP_PITCAIRN:
1206 case CHIP_VERDE:
1207 case CHIP_OLAND:
1208 case CHIP_HAINAN:
Alex Deucher8a53fa22013-08-07 16:09:08 -04001209 /* DPM requires the RLC, RV770+ dGPU requires SMC */
Alex Deucher761bfb92013-08-06 13:34:00 -04001210 if (!rdev->rlc_fw)
1211 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher8a53fa22013-08-07 16:09:08 -04001212 else if ((rdev->family >= CHIP_RV770) &&
1213 (!(rdev->flags & RADEON_IS_IGP)) &&
1214 (!rdev->smc_fw))
1215 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucher761bfb92013-08-06 13:34:00 -04001216 else if (radeon_dpm == 1)
Alex Deucher9d670062013-04-12 13:59:22 -04001217 rdev->pm.pm_method = PM_METHOD_DPM;
1218 else
1219 rdev->pm.pm_method = PM_METHOD_PROFILE;
1220 break;
Alex Deucherda321c82013-04-12 13:55:22 -04001221 default:
1222 /* default to profile method */
1223 rdev->pm.pm_method = PM_METHOD_PROFILE;
1224 break;
1225 }
1226
1227 if (rdev->pm.pm_method == PM_METHOD_DPM)
1228 return radeon_pm_init_dpm(rdev);
1229 else
1230 return radeon_pm_init_old(rdev);
1231}
1232
1233static void radeon_pm_fini_old(struct radeon_device *rdev)
Alex Deucher29fb52c2010-03-11 10:01:17 -05001234{
Alex Deucherce8f5372010-05-07 15:10:16 -04001235 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -04001236 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001237 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1238 rdev->pm.profile = PM_PROFILE_DEFAULT;
1239 radeon_pm_update_profile(rdev);
1240 radeon_pm_set_clocks(rdev);
1241 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -04001242 /* reset default clocks */
1243 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
1244 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1245 radeon_pm_set_clocks(rdev);
1246 }
Alex Deuchera4248162010-04-24 14:50:23 -04001247 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +01001248
1249 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -04001250
Alex Deucherce8f5372010-05-07 15:10:16 -04001251 device_remove_file(rdev->dev, &dev_attr_power_profile);
1252 device_remove_file(rdev->dev, &dev_attr_power_method);
Alex Deucherce8f5372010-05-07 15:10:16 -04001253 }
Alex Deuchera4248162010-04-24 14:50:23 -04001254
Alex Deucher0975b162011-02-02 18:42:03 -05001255 if (rdev->pm.power_state)
1256 kfree(rdev->pm.power_state);
1257
Alex Deucher21a81222010-07-02 12:58:16 -04001258 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -05001259}
1260
Alex Deucherda321c82013-04-12 13:55:22 -04001261static void radeon_pm_fini_dpm(struct radeon_device *rdev)
1262{
1263 if (rdev->pm.num_power_states > 1) {
1264 mutex_lock(&rdev->pm.mutex);
1265 radeon_dpm_disable(rdev);
1266 mutex_unlock(&rdev->pm.mutex);
1267
1268 device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
Alex Deucher70d01a52013-07-02 18:38:02 -04001269 device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
Alex Deucherda321c82013-04-12 13:55:22 -04001270 /* XXX backwards compat */
1271 device_remove_file(rdev->dev, &dev_attr_power_profile);
1272 device_remove_file(rdev->dev, &dev_attr_power_method);
1273 }
1274 radeon_dpm_fini(rdev);
1275
1276 if (rdev->pm.power_state)
1277 kfree(rdev->pm.power_state);
1278
1279 radeon_hwmon_fini(rdev);
1280}
1281
1282void radeon_pm_fini(struct radeon_device *rdev)
1283{
1284 if (rdev->pm.pm_method == PM_METHOD_DPM)
1285 radeon_pm_fini_dpm(rdev);
1286 else
1287 radeon_pm_fini_old(rdev);
1288}
1289
1290static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001291{
1292 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001293 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001294 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001295
Alex Deucherce8f5372010-05-07 15:10:16 -04001296 if (rdev->pm.num_power_states < 2)
1297 return;
1298
Rafał Miłeckic913e232009-12-22 23:02:16 +01001299 mutex_lock(&rdev->pm.mutex);
1300
1301 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -04001302 rdev->pm.active_crtc_count = 0;
1303 list_for_each_entry(crtc,
1304 &ddev->mode_config.crtc_list, head) {
1305 radeon_crtc = to_radeon_crtc(crtc);
1306 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001307 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -04001308 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001309 }
1310 }
1311
Alex Deucherce8f5372010-05-07 15:10:16 -04001312 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
1313 radeon_pm_update_profile(rdev);
1314 radeon_pm_set_clocks(rdev);
1315 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
1316 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
1317 if (rdev->pm.active_crtc_count > 1) {
1318 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
1319 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -04001320
Alex Deucherce8f5372010-05-07 15:10:16 -04001321 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
1322 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
1323 radeon_pm_get_dynpm_state(rdev);
1324 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001325
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001326 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001327 }
1328 } else if (rdev->pm.active_crtc_count == 1) {
1329 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +01001330
Alex Deucherce8f5372010-05-07 15:10:16 -04001331 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
1332 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
1333 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
1334 radeon_pm_get_dynpm_state(rdev);
1335 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001336
Tejun Heo32c87fc2011-01-03 14:49:32 +01001337 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1338 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -04001339 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
1340 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +01001341 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1342 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001343 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -04001344 }
1345 } else { /* count == 0 */
1346 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
1347 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001348
Alex Deucherce8f5372010-05-07 15:10:16 -04001349 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
1350 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
1351 radeon_pm_get_dynpm_state(rdev);
1352 radeon_pm_set_clocks(rdev);
1353 }
1354 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001355 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001356 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +01001357
1358 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001359}
1360
Alex Deucherda321c82013-04-12 13:55:22 -04001361static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
1362{
1363 struct drm_device *ddev = rdev->ddev;
1364 struct drm_crtc *crtc;
1365 struct radeon_crtc *radeon_crtc;
1366
1367 mutex_lock(&rdev->pm.mutex);
1368
Alex Deucher5ca302f2012-11-30 10:56:57 -05001369 /* update active crtc counts */
Alex Deucherda321c82013-04-12 13:55:22 -04001370 rdev->pm.dpm.new_active_crtcs = 0;
1371 rdev->pm.dpm.new_active_crtc_count = 0;
1372 list_for_each_entry(crtc,
1373 &ddev->mode_config.crtc_list, head) {
1374 radeon_crtc = to_radeon_crtc(crtc);
1375 if (crtc->enabled) {
1376 rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
1377 rdev->pm.dpm.new_active_crtc_count++;
1378 }
1379 }
1380
Alex Deucher5ca302f2012-11-30 10:56:57 -05001381 /* update battery/ac status */
1382 if (power_supply_is_system_supplied() > 0)
1383 rdev->pm.dpm.ac_power = true;
1384 else
1385 rdev->pm.dpm.ac_power = false;
1386
Alex Deucherda321c82013-04-12 13:55:22 -04001387 radeon_dpm_change_power_state_locked(rdev);
1388
1389 mutex_unlock(&rdev->pm.mutex);
Alex Deucher8a227552013-06-21 15:12:57 -04001390
Alex Deucherda321c82013-04-12 13:55:22 -04001391}
1392
1393void radeon_pm_compute_clocks(struct radeon_device *rdev)
1394{
1395 if (rdev->pm.pm_method == PM_METHOD_DPM)
1396 radeon_pm_compute_clocks_dpm(rdev);
1397 else
1398 radeon_pm_compute_clocks_old(rdev);
1399}
1400
Alex Deucherce8f5372010-05-07 15:10:16 -04001401static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +10001402{
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001403 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +10001404 bool in_vbl = true;
1405
Mario Kleiner75fa0b02010-10-05 19:57:37 -04001406 /* Iterate over all active crtc's. All crtc's must be in vblank,
1407 * otherwise return in_vbl == false.
1408 */
1409 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
1410 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +02001411 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
1412 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
1413 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +10001414 in_vbl = false;
1415 }
1416 }
Matthew Garrettf81f2022010-04-28 12:13:06 -04001417
1418 return in_vbl;
1419}
1420
Alex Deucherce8f5372010-05-07 15:10:16 -04001421static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -04001422{
1423 u32 stat_crtc = 0;
1424 bool in_vbl = radeon_pm_in_vbl(rdev);
1425
Dave Airlief7352612010-02-18 15:58:36 +10001426 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +10001427 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -04001428 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +10001429 return in_vbl;
1430}
Rafał Miłeckic913e232009-12-22 23:02:16 +01001431
Alex Deucherce8f5372010-05-07 15:10:16 -04001432static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +01001433{
1434 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -04001435 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001436 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -04001437 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001438
Matthew Garrettd9932a32010-04-26 16:02:26 -04001439 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001440 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -04001441 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +01001442 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -04001443 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +01001444
Alex Deucher74652802011-08-25 13:39:48 -04001445 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -04001446 struct radeon_ring *ring = &rdev->ring[i];
1447
1448 if (ring->ready) {
1449 not_processed += radeon_fence_count_emitted(rdev, i);
1450 if (not_processed >= 3)
1451 break;
1452 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001453 }
Rafał Miłeckic913e232009-12-22 23:02:16 +01001454
1455 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001456 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
1457 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1458 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1459 rdev->pm.dynpm_can_upclock) {
1460 rdev->pm.dynpm_planned_action =
1461 DYNPM_ACTION_UPCLOCK;
1462 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001463 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1464 }
1465 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -04001466 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
1467 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
1468 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
1469 rdev->pm.dynpm_can_downclock) {
1470 rdev->pm.dynpm_planned_action =
1471 DYNPM_ACTION_DOWNCLOCK;
1472 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +01001473 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
1474 }
1475 }
1476
Alex Deucherd7311172010-05-03 01:13:14 -04001477 /* Note, radeon_pm_set_clocks is called with static_switch set
1478 * to false since we want to wait for vbl to avoid flicker.
1479 */
Alex Deucherce8f5372010-05-07 15:10:16 -04001480 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
1481 jiffies > rdev->pm.dynpm_action_timeout) {
1482 radeon_pm_get_dynpm_state(rdev);
1483 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001484 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +00001485
Tejun Heo32c87fc2011-01-03 14:49:32 +01001486 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
1487 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +01001488 }
1489 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -04001490 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +01001491}
1492
Rafał Miłecki74338742009-11-03 00:53:02 +01001493/*
1494 * Debugfs info
1495 */
1496#if defined(CONFIG_DEBUG_FS)
1497
1498static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
1499{
1500 struct drm_info_node *node = (struct drm_info_node *) m->private;
1501 struct drm_device *dev = node->minor->dev;
1502 struct radeon_device *rdev = dev->dev_private;
1503
Alex Deucher1316b792013-06-28 09:28:39 -04001504 if (rdev->pm.dpm_enabled) {
1505 mutex_lock(&rdev->pm.mutex);
1506 if (rdev->asic->dpm.debugfs_print_current_performance_level)
1507 radeon_dpm_debugfs_print_current_performance_level(rdev, m);
1508 else
Alex Deucher71375922013-07-02 09:11:39 -04001509 seq_printf(m, "Debugfs support not implemented for this asic\n");
Alex Deucher1316b792013-06-28 09:28:39 -04001510 mutex_unlock(&rdev->pm.mutex);
1511 } else {
1512 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
1513 /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
1514 if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
1515 seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
1516 else
1517 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
1518 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
1519 if (rdev->asic->pm.get_memory_clock)
1520 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
1521 if (rdev->pm.current_vddc)
1522 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
1523 if (rdev->asic->pm.get_pcie_lanes)
1524 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
1525 }
Rafał Miłecki74338742009-11-03 00:53:02 +01001526
1527 return 0;
1528}
1529
1530static struct drm_info_list radeon_pm_info_list[] = {
1531 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
1532};
1533#endif
1534
Rafał Miłeckic913e232009-12-22 23:02:16 +01001535static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +01001536{
1537#if defined(CONFIG_DEBUG_FS)
1538 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
1539#else
1540 return 0;
1541#endif
1542}