blob: 50b632ac8237915646c40b16b0a39e28fa7275ae [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 */
23#include "drmP.h"
24#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
Alex Deucher2031f772010-04-22 12:52:11 -040037#define RADEON_WAIT_IDLE_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010038
Rafał Miłeckif712d0c2010-06-07 18:29:44 -040039static const char *radeon_pm_state_type_name[5] = {
40 "Default",
41 "Powersave",
42 "Battery",
43 "Balanced",
44 "Performance",
45};
46
Alex Deucherce8f5372010-05-07 15:10:16 -040047static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010048static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040049static bool radeon_pm_in_vbl(struct radeon_device *rdev);
50static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
51static void radeon_pm_update_profile(struct radeon_device *rdev);
52static void radeon_pm_set_clocks(struct radeon_device *rdev);
53
54#define ACPI_AC_CLASS "ac_adapter"
55
Alex Deuchera4c9e2e2011-11-04 10:09:41 -040056int radeon_pm_get_type_index(struct radeon_device *rdev,
57 enum radeon_pm_state_type ps_type,
58 int instance)
59{
60 int i;
61 int found_instance = -1;
62
63 for (i = 0; i < rdev->pm.num_power_states; i++) {
64 if (rdev->pm.power_state[i].type == ps_type) {
65 found_instance++;
66 if (found_instance == instance)
67 return i;
68 }
69 }
70 /* return default if no match */
71 return rdev->pm.default_power_state_index;
72}
73
Alex Deucherce8f5372010-05-07 15:10:16 -040074#ifdef CONFIG_ACPI
75static int radeon_acpi_event(struct notifier_block *nb,
76 unsigned long val,
77 void *data)
78{
79 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
80 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
81
82 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
83 if (power_supply_is_system_supplied() > 0)
Dave Airlied9fdaaf2010-08-02 10:42:55 +100084 DRM_DEBUG_DRIVER("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040085 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +100086 DRM_DEBUG_DRIVER("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040087
88 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
89 if (rdev->pm.profile == PM_PROFILE_AUTO) {
90 mutex_lock(&rdev->pm.mutex);
91 radeon_pm_update_profile(rdev);
92 radeon_pm_set_clocks(rdev);
93 mutex_unlock(&rdev->pm.mutex);
94 }
95 }
96 }
97
98 return NOTIFY_OK;
99}
100#endif
101
102static void radeon_pm_update_profile(struct radeon_device *rdev)
103{
104 switch (rdev->pm.profile) {
105 case PM_PROFILE_DEFAULT:
106 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
107 break;
108 case PM_PROFILE_AUTO:
109 if (power_supply_is_system_supplied() > 0) {
110 if (rdev->pm.active_crtc_count > 1)
111 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
112 else
113 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
114 } else {
115 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -0400116 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400117 else
Alex Deucherc9e75b22010-06-02 17:56:01 -0400118 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -0400119 }
120 break;
121 case PM_PROFILE_LOW:
122 if (rdev->pm.active_crtc_count > 1)
123 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
124 else
125 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
126 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400127 case PM_PROFILE_MID:
128 if (rdev->pm.active_crtc_count > 1)
129 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
130 else
131 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
132 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400133 case PM_PROFILE_HIGH:
134 if (rdev->pm.active_crtc_count > 1)
135 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
136 else
137 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
138 break;
139 }
140
141 if (rdev->pm.active_crtc_count == 0) {
142 rdev->pm.requested_power_state_index =
143 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
144 rdev->pm.requested_clock_mode_index =
145 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
146 } else {
147 rdev->pm.requested_power_state_index =
148 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
149 rdev->pm.requested_clock_mode_index =
150 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
151 }
152}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100153
Matthew Garrett5876dd22010-04-26 15:52:20 -0400154static void radeon_unmap_vram_bos(struct radeon_device *rdev)
155{
156 struct radeon_bo *bo, *n;
157
158 if (list_empty(&rdev->gem.objects))
159 return;
160
161 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
162 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
163 ttm_bo_unmap_virtual(&bo->tbo);
164 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400165}
166
Alex Deucherce8f5372010-05-07 15:10:16 -0400167static void radeon_sync_with_vblank(struct radeon_device *rdev)
168{
169 if (rdev->pm.active_crtcs) {
170 rdev->pm.vblank_sync = false;
171 wait_event_timeout(
172 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
173 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
174 }
175}
176
177static void radeon_set_power_state(struct radeon_device *rdev)
178{
179 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400180 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400181
182 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
183 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
184 return;
185
186 if (radeon_gui_idle(rdev)) {
187 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
188 clock_info[rdev->pm.requested_clock_mode_index].sclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500189 if (sclk > rdev->pm.default_sclk)
190 sclk = rdev->pm.default_sclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400191
192 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
193 clock_info[rdev->pm.requested_clock_mode_index].mclk;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500194 if (mclk > rdev->pm.default_mclk)
195 mclk = rdev->pm.default_mclk;
Alex Deucherce8f5372010-05-07 15:10:16 -0400196
Alex Deucher92645872010-05-27 17:01:41 -0400197 /* upvolt before raising clocks, downvolt after lowering clocks */
198 if (sclk < rdev->pm.current_sclk)
199 misc_after = true;
200
201 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400202
203 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400204 if (!radeon_pm_in_vbl(rdev))
205 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400206 }
207
Alex Deucher92645872010-05-27 17:01:41 -0400208 radeon_pm_prepare(rdev);
209
210 if (!misc_after)
211 /* voltage, pcie lanes, etc.*/
212 radeon_pm_misc(rdev);
213
214 /* set engine clock */
215 if (sclk != rdev->pm.current_sclk) {
216 radeon_pm_debug_check_in_vbl(rdev, false);
217 radeon_set_engine_clock(rdev, sclk);
218 radeon_pm_debug_check_in_vbl(rdev, true);
219 rdev->pm.current_sclk = sclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000220 DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
Alex Deucher92645872010-05-27 17:01:41 -0400221 }
222
223 /* set memory clock */
224 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
225 radeon_pm_debug_check_in_vbl(rdev, false);
226 radeon_set_memory_clock(rdev, mclk);
227 radeon_pm_debug_check_in_vbl(rdev, true);
228 rdev->pm.current_mclk = mclk;
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000229 DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
Alex Deucher92645872010-05-27 17:01:41 -0400230 }
231
232 if (misc_after)
233 /* voltage, pcie lanes, etc.*/
234 radeon_pm_misc(rdev);
235
236 radeon_pm_finish(rdev);
237
Alex Deucherce8f5372010-05-07 15:10:16 -0400238 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
239 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
240 } else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000241 DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400242}
243
244static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400245{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400246 int i;
247
Alex Deucher4e186b22010-08-13 10:53:35 -0400248 /* no need to take locks, etc. if nothing's going to change */
249 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
250 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
251 return;
252
Matthew Garrett612e06c2010-04-27 17:16:58 -0400253 mutex_lock(&rdev->ddev->struct_mutex);
254 mutex_lock(&rdev->vram_mutex);
Christian Königbf852792011-10-13 13:19:22 +0200255 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
256 if (rdev->cp[i].ring_obj)
257 mutex_lock(&rdev->cp[i].mutex);
258 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400259
260 /* gui idle int has issues on older chips it seems */
261 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400262 if (rdev->irq.installed) {
263 /* wait for GPU idle */
264 rdev->pm.gui_idle = false;
265 rdev->irq.gui_idle = true;
266 radeon_irq_set(rdev);
267 wait_event_interruptible_timeout(
268 rdev->irq.idle_queue, rdev->pm.gui_idle,
269 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
270 rdev->irq.gui_idle = false;
271 radeon_irq_set(rdev);
272 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400273 } else {
Christian Königbf852792011-10-13 13:19:22 +0200274 struct radeon_cp *cp = &rdev->cp[RADEON_RING_TYPE_GFX_INDEX];
Christian König7b1f2482011-09-23 15:11:23 +0200275 if (cp->ready) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400276 struct radeon_fence *fence;
Christian König7b1f2482011-09-23 15:11:23 +0200277 radeon_ring_alloc(rdev, cp, 64);
Christian Königbf852792011-10-13 13:19:22 +0200278 radeon_fence_create(rdev, &fence, radeon_ring_index(rdev, cp));
Alex Deucherce8f5372010-05-07 15:10:16 -0400279 radeon_fence_emit(rdev, fence);
Christian König7b1f2482011-09-23 15:11:23 +0200280 radeon_ring_commit(rdev, cp);
Alex Deucherce8f5372010-05-07 15:10:16 -0400281 radeon_fence_wait(fence, false);
282 radeon_fence_unref(&fence);
283 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400284 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400285 radeon_unmap_vram_bos(rdev);
286
Alex Deucherce8f5372010-05-07 15:10:16 -0400287 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400288 for (i = 0; i < rdev->num_crtc; i++) {
289 if (rdev->pm.active_crtcs & (1 << i)) {
290 rdev->pm.req_vblank |= (1 << i);
291 drm_vblank_get(rdev->ddev, i);
292 }
293 }
294 }
Alex Deucher539d2412010-04-29 00:22:43 -0400295
Alex Deucherce8f5372010-05-07 15:10:16 -0400296 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400297
Alex Deucherce8f5372010-05-07 15:10:16 -0400298 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400299 for (i = 0; i < rdev->num_crtc; i++) {
300 if (rdev->pm.req_vblank & (1 << i)) {
301 rdev->pm.req_vblank &= ~(1 << i);
302 drm_vblank_put(rdev->ddev, i);
303 }
304 }
305 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400306
Alex Deuchera4248162010-04-24 14:50:23 -0400307 /* update display watermarks based on new power state */
308 radeon_update_bandwidth_info(rdev);
309 if (rdev->pm.active_crtc_count)
310 radeon_bandwidth_update(rdev);
311
Alex Deucherce8f5372010-05-07 15:10:16 -0400312 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400313
Christian Königbf852792011-10-13 13:19:22 +0200314 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
315 if (rdev->cp[i].ring_obj)
316 mutex_unlock(&rdev->cp[i].mutex);
317 }
Matthew Garrett612e06c2010-04-27 17:16:58 -0400318 mutex_unlock(&rdev->vram_mutex);
319 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400320}
321
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400322static void radeon_pm_print_states(struct radeon_device *rdev)
323{
324 int i, j;
325 struct radeon_power_state *power_state;
326 struct radeon_pm_clock_info *clock_info;
327
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000328 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400329 for (i = 0; i < rdev->pm.num_power_states; i++) {
330 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000331 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400332 radeon_pm_state_type_name[power_state->type]);
333 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000334 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400335 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000336 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400337 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000338 DRM_DEBUG_DRIVER("\tSingle display only\n");
339 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400340 for (j = 0; j < power_state->num_clock_modes; j++) {
341 clock_info = &(power_state->clock_info[j]);
342 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000343 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400344 j,
345 clock_info->sclk * 10,
346 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
347 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000348 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400349 j,
350 clock_info->sclk * 10,
351 clock_info->mclk * 10,
352 clock_info->voltage.voltage,
353 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
354 }
355 }
356}
357
Alex Deucherce8f5372010-05-07 15:10:16 -0400358static ssize_t radeon_get_pm_profile(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400361{
362 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
363 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400364 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400365
Alex Deucherce8f5372010-05-07 15:10:16 -0400366 return snprintf(buf, PAGE_SIZE, "%s\n",
367 (cp == PM_PROFILE_AUTO) ? "auto" :
368 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100369 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400370 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400371}
372
Alex Deucherce8f5372010-05-07 15:10:16 -0400373static ssize_t radeon_set_pm_profile(struct device *dev,
374 struct device_attribute *attr,
375 const char *buf,
376 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400377{
378 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
379 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400380
381 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400382 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
383 if (strncmp("default", buf, strlen("default")) == 0)
384 rdev->pm.profile = PM_PROFILE_DEFAULT;
385 else if (strncmp("auto", buf, strlen("auto")) == 0)
386 rdev->pm.profile = PM_PROFILE_AUTO;
387 else if (strncmp("low", buf, strlen("low")) == 0)
388 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400389 else if (strncmp("mid", buf, strlen("mid")) == 0)
390 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400391 else if (strncmp("high", buf, strlen("high")) == 0)
392 rdev->pm.profile = PM_PROFILE_HIGH;
393 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000394 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400395 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400396 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400397 radeon_pm_update_profile(rdev);
398 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000399 } else
400 count = -EINVAL;
401
Alex Deucherce8f5372010-05-07 15:10:16 -0400402fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400403 mutex_unlock(&rdev->pm.mutex);
404
405 return count;
406}
407
Alex Deucherce8f5372010-05-07 15:10:16 -0400408static ssize_t radeon_get_pm_method(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400411{
412 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
413 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400414 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400415
416 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400417 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400418}
419
Alex Deucherce8f5372010-05-07 15:10:16 -0400420static ssize_t radeon_set_pm_method(struct device *dev,
421 struct device_attribute *attr,
422 const char *buf,
423 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400424{
425 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
426 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400427
Alex Deucherce8f5372010-05-07 15:10:16 -0400428
429 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400430 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400431 rdev->pm.pm_method = PM_METHOD_DYNPM;
432 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
433 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400434 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400435 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
436 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400437 /* disable dynpm */
438 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
439 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000440 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400441 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100442 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400443 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000444 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400445 goto fail;
446 }
447 radeon_pm_compute_clocks(rdev);
448fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400449 return count;
450}
451
Alex Deucherce8f5372010-05-07 15:10:16 -0400452static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
453static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400454
Alex Deucher21a81222010-07-02 12:58:16 -0400455static ssize_t radeon_hwmon_show_temp(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
458{
459 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
460 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500461 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400462
463 switch (rdev->pm.int_thermal_type) {
464 case THERMAL_TYPE_RV6XX:
465 temp = rv6xx_get_temp(rdev);
466 break;
467 case THERMAL_TYPE_RV770:
468 temp = rv770_get_temp(rdev);
469 break;
470 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500471 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400472 temp = evergreen_get_temp(rdev);
473 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500474 case THERMAL_TYPE_SUMO:
475 temp = sumo_get_temp(rdev);
476 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400477 default:
478 temp = 0;
479 break;
480 }
481
482 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
483}
484
485static ssize_t radeon_hwmon_show_name(struct device *dev,
486 struct device_attribute *attr,
487 char *buf)
488{
489 return sprintf(buf, "radeon\n");
490}
491
492static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
493static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
494
495static struct attribute *hwmon_attributes[] = {
496 &sensor_dev_attr_temp1_input.dev_attr.attr,
497 &sensor_dev_attr_name.dev_attr.attr,
498 NULL
499};
500
501static const struct attribute_group hwmon_attrgroup = {
502 .attrs = hwmon_attributes,
503};
504
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200505static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400506{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200507 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400508
509 rdev->pm.int_hwmon_dev = NULL;
510
511 switch (rdev->pm.int_thermal_type) {
512 case THERMAL_TYPE_RV6XX:
513 case THERMAL_TYPE_RV770:
514 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400515 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500516 case THERMAL_TYPE_SUMO:
Alex Deucher21a81222010-07-02 12:58:16 -0400517 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200518 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
519 err = PTR_ERR(rdev->pm.int_hwmon_dev);
520 dev_err(rdev->dev,
521 "Unable to register hwmon device: %d\n", err);
522 break;
523 }
Alex Deucher21a81222010-07-02 12:58:16 -0400524 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
525 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
526 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200527 if (err) {
528 dev_err(rdev->dev,
529 "Unable to create hwmon sysfs file: %d\n", err);
530 hwmon_device_unregister(rdev->dev);
531 }
Alex Deucher21a81222010-07-02 12:58:16 -0400532 break;
533 default:
534 break;
535 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200536
537 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400538}
539
540static void radeon_hwmon_fini(struct radeon_device *rdev)
541{
542 if (rdev->pm.int_hwmon_dev) {
543 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
544 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
545 }
546}
547
Alex Deucherce8f5372010-05-07 15:10:16 -0400548void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500549{
Alex Deucherce8f5372010-05-07 15:10:16 -0400550 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000551 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000552 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
553 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000554 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400555 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100556
557 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500558}
559
Alex Deucherce8f5372010-05-07 15:10:16 -0400560void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100561{
Alex Deuchered18a362011-01-06 21:19:32 -0500562 /* set up the default clocks if the MC ucode is loaded */
563 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
564 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400565 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
566 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400567 if (rdev->pm.default_vddci)
568 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
569 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500570 if (rdev->pm.default_sclk)
571 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
572 if (rdev->pm.default_mclk)
573 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
574 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400575 /* asic init will reset the default power state */
576 mutex_lock(&rdev->pm.mutex);
577 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
578 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500579 rdev->pm.current_sclk = rdev->pm.default_sclk;
580 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400581 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 -0400582 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 +0000583 if (rdev->pm.pm_method == PM_METHOD_DYNPM
584 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
585 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100586 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
587 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000588 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400589 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400590 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100591}
592
Rafał Miłecki74338742009-11-03 00:53:02 +0100593int radeon_pm_init(struct radeon_device *rdev)
594{
Dave Airlie26481fb2010-05-18 19:00:14 +1000595 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200596
Alex Deucherce8f5372010-05-07 15:10:16 -0400597 /* default to profile method */
598 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400599 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400600 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
601 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
602 rdev->pm.dynpm_can_upclock = true;
603 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500604 rdev->pm.default_sclk = rdev->clock.default_sclk;
605 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400606 rdev->pm.current_sclk = rdev->clock.default_sclk;
607 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400608 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100609
Alex Deucher56278a82009-12-28 13:58:44 -0500610 if (rdev->bios) {
611 if (rdev->is_atom_bios)
612 radeon_atombios_get_power_modes(rdev);
613 else
614 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400615 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400616 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500617 /* set up the default clocks if the MC ucode is loaded */
618 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
619 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400620 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
621 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -0400622 if (rdev->pm.default_vddci)
623 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
624 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500625 if (rdev->pm.default_sclk)
626 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
627 if (rdev->pm.default_mclk)
628 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
629 }
Alex Deucher56278a82009-12-28 13:58:44 -0500630 }
631
Alex Deucher21a81222010-07-02 12:58:16 -0400632 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200633 ret = radeon_hwmon_init(rdev);
634 if (ret)
635 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100636
637 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
638
Alex Deucherce8f5372010-05-07 15:10:16 -0400639 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400640 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000641 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
642 if (ret)
643 DRM_ERROR("failed to create device file for power profile\n");
644 ret = device_create_file(rdev->dev, &dev_attr_power_method);
645 if (ret)
646 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400647
648#ifdef CONFIG_ACPI
649 rdev->acpi_nb.notifier_call = radeon_acpi_event;
650 register_acpi_notifier(&rdev->acpi_nb);
651#endif
Alex Deucherce8f5372010-05-07 15:10:16 -0400652 if (radeon_debugfs_pm_init(rdev)) {
653 DRM_ERROR("Failed to register debugfs file for PM!\n");
654 }
655
656 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100657 }
658
659 return 0;
660}
661
Alex Deucher29fb52c2010-03-11 10:01:17 -0500662void radeon_pm_fini(struct radeon_device *rdev)
663{
Alex Deucherce8f5372010-05-07 15:10:16 -0400664 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400665 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400666 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
667 rdev->pm.profile = PM_PROFILE_DEFAULT;
668 radeon_pm_update_profile(rdev);
669 radeon_pm_set_clocks(rdev);
670 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400671 /* reset default clocks */
672 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
673 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
674 radeon_pm_set_clocks(rdev);
675 }
Alex Deuchera4248162010-04-24 14:50:23 -0400676 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100677
678 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400679
Alex Deucherce8f5372010-05-07 15:10:16 -0400680 device_remove_file(rdev->dev, &dev_attr_power_profile);
681 device_remove_file(rdev->dev, &dev_attr_power_method);
682#ifdef CONFIG_ACPI
683 unregister_acpi_notifier(&rdev->acpi_nb);
684#endif
685 }
Alex Deuchera4248162010-04-24 14:50:23 -0400686
Alex Deucher0975b162011-02-02 18:42:03 -0500687 if (rdev->pm.power_state)
688 kfree(rdev->pm.power_state);
689
Alex Deucher21a81222010-07-02 12:58:16 -0400690 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500691}
692
Rafał Miłeckic913e232009-12-22 23:02:16 +0100693void radeon_pm_compute_clocks(struct radeon_device *rdev)
694{
695 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400696 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100697 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100698
Alex Deucherce8f5372010-05-07 15:10:16 -0400699 if (rdev->pm.num_power_states < 2)
700 return;
701
Rafał Miłeckic913e232009-12-22 23:02:16 +0100702 mutex_lock(&rdev->pm.mutex);
703
704 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400705 rdev->pm.active_crtc_count = 0;
706 list_for_each_entry(crtc,
707 &ddev->mode_config.crtc_list, head) {
708 radeon_crtc = to_radeon_crtc(crtc);
709 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100710 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400711 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100712 }
713 }
714
Alex Deucherce8f5372010-05-07 15:10:16 -0400715 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
716 radeon_pm_update_profile(rdev);
717 radeon_pm_set_clocks(rdev);
718 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
719 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
720 if (rdev->pm.active_crtc_count > 1) {
721 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
722 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400723
Alex Deucherce8f5372010-05-07 15:10:16 -0400724 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
725 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
726 radeon_pm_get_dynpm_state(rdev);
727 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100728
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000729 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400730 }
731 } else if (rdev->pm.active_crtc_count == 1) {
732 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100733
Alex Deucherce8f5372010-05-07 15:10:16 -0400734 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
735 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
736 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
737 radeon_pm_get_dynpm_state(rdev);
738 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100739
Tejun Heo32c87fc2011-01-03 14:49:32 +0100740 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
741 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400742 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
743 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100744 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
745 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000746 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400747 }
748 } else { /* count == 0 */
749 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
750 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100751
Alex Deucherce8f5372010-05-07 15:10:16 -0400752 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
753 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
754 radeon_pm_get_dynpm_state(rdev);
755 radeon_pm_set_clocks(rdev);
756 }
757 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100758 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100759 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100760
761 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100762}
763
Alex Deucherce8f5372010-05-07 15:10:16 -0400764static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000765{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400766 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000767 bool in_vbl = true;
768
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400769 /* Iterate over all active crtc's. All crtc's must be in vblank,
770 * otherwise return in_vbl == false.
771 */
772 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
773 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200774 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
775 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
776 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000777 in_vbl = false;
778 }
779 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400780
781 return in_vbl;
782}
783
Alex Deucherce8f5372010-05-07 15:10:16 -0400784static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400785{
786 u32 stat_crtc = 0;
787 bool in_vbl = radeon_pm_in_vbl(rdev);
788
Dave Airlief7352612010-02-18 15:58:36 +1000789 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000790 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400791 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000792 return in_vbl;
793}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100794
Alex Deucherce8f5372010-05-07 15:10:16 -0400795static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100796{
797 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400798 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100799 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400800 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100801
Matthew Garrettd9932a32010-04-26 16:02:26 -0400802 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100803 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400804 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100805 unsigned long irq_flags;
806 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -0400807 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100808
Alex Deucher74652802011-08-25 13:39:48 -0400809 read_lock_irqsave(&rdev->fence_lock, irq_flags);
810 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
811 if (!rdev->fence_drv[i].initialized)
812 continue;
813
814 if (!list_empty(&rdev->fence_drv[i].emitted)) {
815 struct list_head *ptr;
816 list_for_each(ptr, &rdev->fence_drv[i].emitted) {
817 /* count up to 3, that's enought info */
818 if (++not_processed >= 3)
819 break;
820 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100821 }
Alex Deucher74652802011-08-25 13:39:48 -0400822 if (not_processed >= 3)
823 break;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100824 }
Alex Deucher74652802011-08-25 13:39:48 -0400825 read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100826
827 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400828 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
829 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
830 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
831 rdev->pm.dynpm_can_upclock) {
832 rdev->pm.dynpm_planned_action =
833 DYNPM_ACTION_UPCLOCK;
834 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100835 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
836 }
837 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400838 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
839 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
840 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
841 rdev->pm.dynpm_can_downclock) {
842 rdev->pm.dynpm_planned_action =
843 DYNPM_ACTION_DOWNCLOCK;
844 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100845 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
846 }
847 }
848
Alex Deucherd7311172010-05-03 01:13:14 -0400849 /* Note, radeon_pm_set_clocks is called with static_switch set
850 * to false since we want to wait for vbl to avoid flicker.
851 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400852 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
853 jiffies > rdev->pm.dynpm_action_timeout) {
854 radeon_pm_get_dynpm_state(rdev);
855 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100856 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000857
Tejun Heo32c87fc2011-01-03 14:49:32 +0100858 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
859 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100860 }
861 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400862 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100863}
864
Rafał Miłecki74338742009-11-03 00:53:02 +0100865/*
866 * Debugfs info
867 */
868#if defined(CONFIG_DEBUG_FS)
869
870static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
871{
872 struct drm_info_node *node = (struct drm_info_node *) m->private;
873 struct drm_device *dev = node->minor->dev;
874 struct radeon_device *rdev = dev->dev_private;
875
Alex Deucher9ace9f72011-01-06 21:19:26 -0500876 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100877 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500878 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100879 if (rdev->asic->get_memory_clock)
880 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400881 if (rdev->pm.current_vddc)
882 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000883 if (rdev->asic->get_pcie_lanes)
884 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100885
886 return 0;
887}
888
889static struct drm_info_list radeon_pm_info_list[] = {
890 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
891};
892#endif
893
Rafał Miłeckic913e232009-12-22 23:02:16 +0100894static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100895{
896#if defined(CONFIG_DEBUG_FS)
897 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
898#else
899 return 0;
900#endif
901}