blob: 3ef0319981d3329d7a52619dd8d2243ca58612ae [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#ifdef CONFIG_ACPI
28#include <linux/acpi.h>
29#endif
30#include <linux/power_supply.h>
Alex Deucher21a81222010-07-02 12:58:16 -040031#include <linux/hwmon.h>
32#include <linux/hwmon-sysfs.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010033
Rafał Miłeckic913e232009-12-22 23:02:16 +010034#define RADEON_IDLE_LOOP_MS 100
35#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010036#define RADEON_WAIT_VBLANK_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010037
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040038static const char *radeon_pm_state_type_name[5] = {
39 "Default",
40 "Powersave",
41 "Battery",
42 "Balanced",
43 "Performance",
44};
45
Alex Deucherce8f5372010-05-07 15:10:16 -040046static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010047static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040048static bool radeon_pm_in_vbl(struct radeon_device *rdev);
49static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
50static void radeon_pm_update_profile(struct radeon_device *rdev);
51static void radeon_pm_set_clocks(struct radeon_device *rdev);
52
53#define ACPI_AC_CLASS "ac_adapter"
54
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040055int radeon_pm_get_type_index(struct radeon_device *rdev,
56 enum radeon_pm_state_type ps_type,
57 int instance)
58{
59 int i;
60 int found_instance = -1;
61
62 for (i = 0; i < rdev->pm.num_power_states; i++) {
63 if (rdev->pm.power_state[i].type == ps_type) {
64 found_instance++;
65 if (found_instance == instance)
66 return i;
67 }
68 }
69 /* return default if no match */
70 return rdev->pm.default_power_state_index;
71}
72
Alex Deucherce8f5372010-05-07 15:10:16 -040073#ifdef CONFIG_ACPI
74static int radeon_acpi_event(struct notifier_block *nb,
75 unsigned long val,
76 void *data)
77{
78 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
79 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
80
81 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
82 if (power_supply_is_system_supplied() > 0)
Dave Airlied9fdaaf2010-08-02 10:42:55 +100083 DRM_DEBUG_DRIVER("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040084 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +100085 DRM_DEBUG_DRIVER("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040086
87 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
88 if (rdev->pm.profile == PM_PROFILE_AUTO) {
89 mutex_lock(&rdev->pm.mutex);
90 radeon_pm_update_profile(rdev);
91 radeon_pm_set_clocks(rdev);
92 mutex_unlock(&rdev->pm.mutex);
93 }
94 }
95 }
96
97 return NOTIFY_OK;
98}
99#endif
100
101static void radeon_pm_update_profile(struct radeon_device *rdev)
102{
103 switch (rdev->pm.profile) {
104 case PM_PROFILE_DEFAULT:
105 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
106 break;
107 case PM_PROFILE_AUTO:
108 if (power_supply_is_system_supplied() > 0) {
109 if (rdev->pm.active_crtc_count > 1)
110 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
111 else
112 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
113 } else {
114 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400115 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400116 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400117 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400118 }
119 break;
120 case PM_PROFILE_LOW:
121 if (rdev->pm.active_crtc_count > 1)
122 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
123 else
124 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
125 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400126 case PM_PROFILE_MID:
127 if (rdev->pm.active_crtc_count > 1)
128 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
129 else
130 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
131 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400132 case PM_PROFILE_HIGH:
133 if (rdev->pm.active_crtc_count > 1)
134 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
135 else
136 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
137 break;
138 }
139
140 if (rdev->pm.active_crtc_count == 0) {
141 rdev->pm.requested_power_state_index =
142 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
143 rdev->pm.requested_clock_mode_index =
144 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
145 } else {
146 rdev->pm.requested_power_state_index =
147 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
148 rdev->pm.requested_clock_mode_index =
149 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
150 }
151}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100152
Matthew Garrett5876dd22010-04-26 15:52:20 -0400153static void radeon_unmap_vram_bos(struct radeon_device *rdev)
154{
155 struct radeon_bo *bo, *n;
156
157 if (list_empty(&rdev->gem.objects))
158 return;
159
160 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
161 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
162 ttm_bo_unmap_virtual(&bo->tbo);
163 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400164}
165
Alex Deucherce8f5372010-05-07 15:10:16 -0400166static void radeon_sync_with_vblank(struct radeon_device *rdev)
167{
168 if (rdev->pm.active_crtcs) {
169 rdev->pm.vblank_sync = false;
170 wait_event_timeout(
171 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
172 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
173 }
174}
175
176static void radeon_set_power_state(struct radeon_device *rdev)
177{
178 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400179 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400180
181 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
182 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
183 return;
184
185 if (radeon_gui_idle(rdev)) {
186 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
187 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500188 if (sclk > rdev->pm.default_sclk)
189 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400190
191 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
192 clock_info[rdev->pm.requested_clock_mode_index].mclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500193 if (mclk > rdev->pm.default_mclk)
194 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400195
Alex Deucher92645872010-05-27 17:01:41 -0400196 /* upvolt before raising clocks, downvolt after lowering clocks */
197 if (sclk < rdev->pm.current_sclk)
198 misc_after = true;
199
200 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400201
202 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400203 if (!radeon_pm_in_vbl(rdev))
204 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400205 }
206
Alex Deucher92645872010-05-27 17:01:41 -0400207 radeon_pm_prepare(rdev);
208
209 if (!misc_after)
210 /* voltage, pcie lanes, etc.*/
211 radeon_pm_misc(rdev);
212
213 /* set engine clock */
214 if (sclk != rdev->pm.current_sclk) {
215 radeon_pm_debug_check_in_vbl(rdev, false);
216 radeon_set_engine_clock(rdev, sclk);
217 radeon_pm_debug_check_in_vbl(rdev, true);
218 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000219 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400220 }
221
222 /* set memory clock */
Alex Deucher798bcf72012-02-23 17:53:48 -0500223 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400224 radeon_pm_debug_check_in_vbl(rdev, false);
225 radeon_set_memory_clock(rdev, mclk);
226 radeon_pm_debug_check_in_vbl(rdev, true);
227 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000228 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400229 }
230
231 if (misc_after)
232 /* voltage, pcie lanes, etc.*/
233 radeon_pm_misc(rdev);
234
235 radeon_pm_finish(rdev);
236
Alex Deucherce8f5372010-05-07 15:10:16 -0400237 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
238 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
239 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000240 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400241}
242
243static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400244{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400245 int i;
246
Alex Deucher4e186b22010-08-13 10:53:35 -0400247 /* no need to take locks, etc. if nothing's going to change */
248 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
249 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
250 return;
251
Matthew Garrett612e06c2010-04-27 17:16:58 -0400252 mutex_lock(&rdev->ddev->struct_mutex);
Christian Königdb7fce32012-05-11 14:57:18 +0200253 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200254 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400255
256 /* gui idle int has issues on older chips it seems */
257 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400258 if (rdev->irq.installed) {
Christian Koenigfb982572012-05-17 01:33:30 +0200259 /* wait for GPU to become idle */
260 radeon_irq_kms_wait_gui_idle(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400261 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400262 } else {
Christian Könige32eb502011-10-23 12:56:27 +0200263 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
264 if (ring->ready) {
Christian König8a47cc92012-05-09 15:34:48 +0200265 radeon_fence_wait_empty_locked(rdev, RADEON_RING_TYPE_GFX_INDEX);
Alex Deucherce8f5372010-05-07 15:10:16 -0400266 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400267 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400268 radeon_unmap_vram_bos(rdev);
269
Alex Deucherce8f5372010-05-07 15:10:16 -0400270 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400271 for (i = 0; i < rdev->num_crtc; i++) {
272 if (rdev->pm.active_crtcs & (1 << i)) {
273 rdev->pm.req_vblank |= (1 << i);
274 drm_vblank_get(rdev->ddev, i);
275 }
276 }
277 }
Alex Deucher539d2412010-04-29 00:22:43 -0400278
Alex Deucherce8f5372010-05-07 15:10:16 -0400279 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400280
Alex Deucherce8f5372010-05-07 15:10:16 -0400281 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400282 for (i = 0; i < rdev->num_crtc; i++) {
283 if (rdev->pm.req_vblank & (1 << i)) {
284 rdev->pm.req_vblank &= ~(1 << i);
285 drm_vblank_put(rdev->ddev, i);
286 }
287 }
288 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400289
Alex Deuchera4248162010-04-24 14:50:23 -0400290 /* update display watermarks based on new power state */
291 radeon_update_bandwidth_info(rdev);
292 if (rdev->pm.active_crtc_count)
293 radeon_bandwidth_update(rdev);
294
Alex Deucherce8f5372010-05-07 15:10:16 -0400295 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400296
Christian Königd6999bc2012-05-09 15:34:45 +0200297 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200298 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400299 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400300}
301
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400302static void radeon_pm_print_states(struct radeon_device *rdev)
303{
304 int i, j;
305 struct radeon_power_state *power_state;
306 struct radeon_pm_clock_info *clock_info;
307
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000308 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400309 for (i = 0; i < rdev->pm.num_power_states; i++) {
310 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000311 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400312 radeon_pm_state_type_name[power_state->type]);
313 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000314 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400315 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000316 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400317 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000318 DRM_DEBUG_DRIVER("\tSingle display only\n");
319 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400320 for (j = 0; j < power_state->num_clock_modes; j++) {
321 clock_info = &(power_state->clock_info[j]);
322 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000323 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400324 j,
325 clock_info->sclk * 10,
326 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
327 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000328 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400329 j,
330 clock_info->sclk * 10,
331 clock_info->mclk * 10,
332 clock_info->voltage.voltage,
333 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
334 }
335 }
336}
337
Alex Deucherce8f5372010-05-07 15:10:16 -0400338static ssize_t radeon_get_pm_profile(struct device *dev,
339 struct device_attribute *attr,
340 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400341{
342 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
343 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400344 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400345
Alex Deucherce8f5372010-05-07 15:10:16 -0400346 return snprintf(buf, PAGE_SIZE, "%s\n",
347 (cp == PM_PROFILE_AUTO) ? "auto" :
348 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100349 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400350 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400351}
352
Alex Deucherce8f5372010-05-07 15:10:16 -0400353static ssize_t radeon_set_pm_profile(struct device *dev,
354 struct device_attribute *attr,
355 const char *buf,
356 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400357{
358 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
359 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400360
361 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400362 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
363 if (strncmp("default", buf, strlen("default")) == 0)
364 rdev->pm.profile = PM_PROFILE_DEFAULT;
365 else if (strncmp("auto", buf, strlen("auto")) == 0)
366 rdev->pm.profile = PM_PROFILE_AUTO;
367 else if (strncmp("low", buf, strlen("low")) == 0)
368 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400369 else if (strncmp("mid", buf, strlen("mid")) == 0)
370 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400371 else if (strncmp("high", buf, strlen("high")) == 0)
372 rdev->pm.profile = PM_PROFILE_HIGH;
373 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000374 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400375 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400376 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400377 radeon_pm_update_profile(rdev);
378 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000379 } else
380 count = -EINVAL;
381
Alex Deucherce8f5372010-05-07 15:10:16 -0400382fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400383 mutex_unlock(&rdev->pm.mutex);
384
385 return count;
386}
387
Alex Deucherce8f5372010-05-07 15:10:16 -0400388static ssize_t radeon_get_pm_method(struct device *dev,
389 struct device_attribute *attr,
390 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400391{
392 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
393 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400394 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400395
396 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400397 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400398}
399
Alex Deucherce8f5372010-05-07 15:10:16 -0400400static ssize_t radeon_set_pm_method(struct device *dev,
401 struct device_attribute *attr,
402 const char *buf,
403 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400404{
405 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
406 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400407
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 Deucherce8f5372010-05-07 15:10:16 -0400432static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
433static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400434
Alex Deucher21a81222010-07-02 12:58:16 -0400435static ssize_t radeon_hwmon_show_temp(struct device *dev,
436 struct device_attribute *attr,
437 char *buf)
438{
439 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
440 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500441 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400442
443 switch (rdev->pm.int_thermal_type) {
444 case THERMAL_TYPE_RV6XX:
445 temp = rv6xx_get_temp(rdev);
446 break;
447 case THERMAL_TYPE_RV770:
448 temp = rv770_get_temp(rdev);
449 break;
450 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500451 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400452 temp = evergreen_get_temp(rdev);
453 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500454 case THERMAL_TYPE_SUMO:
455 temp = sumo_get_temp(rdev);
456 break;
Alex Deucher1bd47d22012-03-20 17:18:10 -0400457 case THERMAL_TYPE_SI:
458 temp = si_get_temp(rdev);
459 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400460 default:
461 temp = 0;
462 break;
463 }
464
465 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
466}
467
468static ssize_t radeon_hwmon_show_name(struct device *dev,
469 struct device_attribute *attr,
470 char *buf)
471{
472 return sprintf(buf, "radeon\n");
473}
474
475static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
476static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
477
478static struct attribute *hwmon_attributes[] = {
479 &sensor_dev_attr_temp1_input.dev_attr.attr,
480 &sensor_dev_attr_name.dev_attr.attr,
481 NULL
482};
483
484static const struct attribute_group hwmon_attrgroup = {
485 .attrs = hwmon_attributes,
486};
487
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200488static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400489{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200490 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400491
492 rdev->pm.int_hwmon_dev = NULL;
493
494 switch (rdev->pm.int_thermal_type) {
495 case THERMAL_TYPE_RV6XX:
496 case THERMAL_TYPE_RV770:
497 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400498 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500499 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400500 case THERMAL_TYPE_SI:
Alex Deucher5d7486c2012-03-20 17:18:29 -0400501 /* No support for TN yet */
502 if (rdev->family == CHIP_ARUBA)
503 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400504 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200505 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
506 err = PTR_ERR(rdev->pm.int_hwmon_dev);
507 dev_err(rdev->dev,
508 "Unable to register hwmon device: %d\n", err);
509 break;
510 }
Alex Deucher21a81222010-07-02 12:58:16 -0400511 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
512 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
513 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200514 if (err) {
515 dev_err(rdev->dev,
516 "Unable to create hwmon sysfs file: %d\n", err);
517 hwmon_device_unregister(rdev->dev);
518 }
Alex Deucher21a81222010-07-02 12:58:16 -0400519 break;
520 default:
521 break;
522 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200523
524 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400525}
526
527static void radeon_hwmon_fini(struct radeon_device *rdev)
528{
529 if (rdev->pm.int_hwmon_dev) {
530 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
531 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
532 }
533}
534
Alex Deucherce8f5372010-05-07 15:10:16 -0400535void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500536{
Alex Deucherce8f5372010-05-07 15:10:16 -0400537 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000538 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000539 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
540 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000541 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400542 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100543
544 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500545}
546
Alex Deucherce8f5372010-05-07 15:10:16 -0400547void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100548{
Alex Deuchered18a362011-01-06 21:19:32 -0500549 /* set up the default clocks if the MC ucode is loaded */
550 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
551 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400552 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
553 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400554 if (rdev->pm.default_vddci)
555 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
556 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500557 if (rdev->pm.default_sclk)
558 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
559 if (rdev->pm.default_mclk)
560 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
561 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400562 /* asic init will reset the default power state */
563 mutex_lock(&rdev->pm.mutex);
564 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
565 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500566 rdev->pm.current_sclk = rdev->pm.default_sclk;
567 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400568 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 -0400569 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 +0000570 if (rdev->pm.pm_method == PM_METHOD_DYNPM
571 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
572 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100573 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
574 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000575 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400576 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400577 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100578}
579
Rafał Miłecki74338742009-11-03 00:53:02 +0100580int radeon_pm_init(struct radeon_device *rdev)
581{
Dave Airlie26481fb2010-05-18 19:00:14 +1000582 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200583
Alex Deucherce8f5372010-05-07 15:10:16 -0400584 /* default to profile method */
585 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400586 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400587 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
588 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
589 rdev->pm.dynpm_can_upclock = true;
590 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500591 rdev->pm.default_sclk = rdev->clock.default_sclk;
592 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400593 rdev->pm.current_sclk = rdev->clock.default_sclk;
594 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400595 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100596
Alex Deucher56278a82009-12-28 13:58:44 -0500597 if (rdev->bios) {
598 if (rdev->is_atom_bios)
599 radeon_atombios_get_power_modes(rdev);
600 else
601 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400602 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400603 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500604 /* set up the default clocks if the MC ucode is loaded */
605 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
606 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400607 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
608 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -0400609 if (rdev->pm.default_vddci)
610 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
611 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500612 if (rdev->pm.default_sclk)
613 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
614 if (rdev->pm.default_mclk)
615 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
616 }
Alex Deucher56278a82009-12-28 13:58:44 -0500617 }
618
Alex Deucher21a81222010-07-02 12:58:16 -0400619 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200620 ret = radeon_hwmon_init(rdev);
621 if (ret)
622 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100623
624 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
625
Alex Deucherce8f5372010-05-07 15:10:16 -0400626 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400627 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000628 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
629 if (ret)
630 DRM_ERROR("failed to create device file for power profile\n");
631 ret = device_create_file(rdev->dev, &dev_attr_power_method);
632 if (ret)
633 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400634
635#ifdef CONFIG_ACPI
636 rdev->acpi_nb.notifier_call = radeon_acpi_event;
637 register_acpi_notifier(&rdev->acpi_nb);
638#endif
Alex Deucherce8f5372010-05-07 15:10:16 -0400639 if (radeon_debugfs_pm_init(rdev)) {
640 DRM_ERROR("Failed to register debugfs file for PM!\n");
641 }
642
643 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100644 }
645
646 return 0;
647}
648
Alex Deucher29fb52c2010-03-11 10:01:17 -0500649void radeon_pm_fini(struct radeon_device *rdev)
650{
Alex Deucherce8f5372010-05-07 15:10:16 -0400651 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400652 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400653 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
654 rdev->pm.profile = PM_PROFILE_DEFAULT;
655 radeon_pm_update_profile(rdev);
656 radeon_pm_set_clocks(rdev);
657 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400658 /* reset default clocks */
659 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
660 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
661 radeon_pm_set_clocks(rdev);
662 }
Alex Deuchera4248162010-04-24 14:50:23 -0400663 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100664
665 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400666
Alex Deucherce8f5372010-05-07 15:10:16 -0400667 device_remove_file(rdev->dev, &dev_attr_power_profile);
668 device_remove_file(rdev->dev, &dev_attr_power_method);
669#ifdef CONFIG_ACPI
670 unregister_acpi_notifier(&rdev->acpi_nb);
671#endif
672 }
Alex Deuchera4248162010-04-24 14:50:23 -0400673
Alex Deucher0975b162011-02-02 18:42:03 -0500674 if (rdev->pm.power_state)
675 kfree(rdev->pm.power_state);
676
Alex Deucher21a81222010-07-02 12:58:16 -0400677 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500678}
679
Rafał Miłeckic913e232009-12-22 23:02:16 +0100680void radeon_pm_compute_clocks(struct radeon_device *rdev)
681{
682 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400683 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100684 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100685
Alex Deucherce8f5372010-05-07 15:10:16 -0400686 if (rdev->pm.num_power_states < 2)
687 return;
688
Rafał Miłeckic913e232009-12-22 23:02:16 +0100689 mutex_lock(&rdev->pm.mutex);
690
691 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400692 rdev->pm.active_crtc_count = 0;
693 list_for_each_entry(crtc,
694 &ddev->mode_config.crtc_list, head) {
695 radeon_crtc = to_radeon_crtc(crtc);
696 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100697 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400698 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100699 }
700 }
701
Alex Deucherce8f5372010-05-07 15:10:16 -0400702 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
703 radeon_pm_update_profile(rdev);
704 radeon_pm_set_clocks(rdev);
705 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
706 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
707 if (rdev->pm.active_crtc_count > 1) {
708 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
709 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400710
Alex Deucherce8f5372010-05-07 15:10:16 -0400711 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
712 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
713 radeon_pm_get_dynpm_state(rdev);
714 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100715
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000716 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400717 }
718 } else if (rdev->pm.active_crtc_count == 1) {
719 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100720
Alex Deucherce8f5372010-05-07 15:10:16 -0400721 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
722 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
723 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
724 radeon_pm_get_dynpm_state(rdev);
725 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100726
Tejun Heo32c87fc2011-01-03 14:49:32 +0100727 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
728 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400729 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
730 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100731 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
732 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000733 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400734 }
735 } else { /* count == 0 */
736 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
737 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100738
Alex Deucherce8f5372010-05-07 15:10:16 -0400739 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
740 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
741 radeon_pm_get_dynpm_state(rdev);
742 radeon_pm_set_clocks(rdev);
743 }
744 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100745 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100746 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100747
748 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100749}
750
Alex Deucherce8f5372010-05-07 15:10:16 -0400751static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000752{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400753 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000754 bool in_vbl = true;
755
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400756 /* Iterate over all active crtc's. All crtc's must be in vblank,
757 * otherwise return in_vbl == false.
758 */
759 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
760 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200761 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
762 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
763 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000764 in_vbl = false;
765 }
766 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400767
768 return in_vbl;
769}
770
Alex Deucherce8f5372010-05-07 15:10:16 -0400771static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400772{
773 u32 stat_crtc = 0;
774 bool in_vbl = radeon_pm_in_vbl(rdev);
775
Dave Airlief7352612010-02-18 15:58:36 +1000776 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000777 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400778 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000779 return in_vbl;
780}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100781
Alex Deucherce8f5372010-05-07 15:10:16 -0400782static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100783{
784 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400785 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100786 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400787 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100788
Matthew Garrettd9932a32010-04-26 16:02:26 -0400789 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100790 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400791 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100792 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -0400793 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100794
Alex Deucher74652802011-08-25 13:39:48 -0400795 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -0400796 struct radeon_ring *ring = &rdev->ring[i];
797
798 if (ring->ready) {
799 not_processed += radeon_fence_count_emitted(rdev, i);
800 if (not_processed >= 3)
801 break;
802 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100803 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100804
805 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400806 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
807 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
808 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
809 rdev->pm.dynpm_can_upclock) {
810 rdev->pm.dynpm_planned_action =
811 DYNPM_ACTION_UPCLOCK;
812 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100813 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
814 }
815 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400816 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
817 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
818 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
819 rdev->pm.dynpm_can_downclock) {
820 rdev->pm.dynpm_planned_action =
821 DYNPM_ACTION_DOWNCLOCK;
822 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100823 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
824 }
825 }
826
Alex Deucherd7311172010-05-03 01:13:14 -0400827 /* Note, radeon_pm_set_clocks is called with static_switch set
828 * to false since we want to wait for vbl to avoid flicker.
829 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400830 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
831 jiffies > rdev->pm.dynpm_action_timeout) {
832 radeon_pm_get_dynpm_state(rdev);
833 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100834 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000835
Tejun Heo32c87fc2011-01-03 14:49:32 +0100836 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
837 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100838 }
839 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400840 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100841}
842
Rafał Miłecki74338742009-11-03 00:53:02 +0100843/*
844 * Debugfs info
845 */
846#if defined(CONFIG_DEBUG_FS)
847
848static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
849{
850 struct drm_info_node *node = (struct drm_info_node *) m->private;
851 struct drm_device *dev = node->minor->dev;
852 struct radeon_device *rdev = dev->dev_private;
853
Alex Deucher9ace9f72011-01-06 21:19:26 -0500854 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100855 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500856 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Alex Deucher798bcf72012-02-23 17:53:48 -0500857 if (rdev->asic->pm.get_memory_clock)
Rafał Miłecki62340772009-12-15 21:46:58 +0100858 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400859 if (rdev->pm.current_vddc)
860 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Alex Deucher798bcf72012-02-23 17:53:48 -0500861 if (rdev->asic->pm.get_pcie_lanes)
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000862 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100863
864 return 0;
865}
866
867static struct drm_info_list radeon_pm_info_list[] = {
868 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
869};
870#endif
871
Rafał Miłeckic913e232009-12-22 23:02:16 +0100872static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100873{
874#if defined(CONFIG_DEBUG_FS)
875 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
876#else
877 return 0;
878#endif
879}