blob: 9dc0b54fe3e377f480492e2a7ceee55ed249921d [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 */
Alex Deucher798bcf72012-02-23 17:53:48 -0500224 if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
Alex Deucher92645872010-05-27 17:01:41 -0400225 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);
Christian Königdb7fce32012-05-11 14:57:18 +0200254 down_write(&rdev->pm.mclk_lock);
Christian Königd6999bc2012-05-09 15:34:45 +0200255 mutex_lock(&rdev->ring_lock);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400256
257 /* gui idle int has issues on older chips it seems */
258 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400259 if (rdev->irq.installed) {
260 /* wait for GPU idle */
261 rdev->pm.gui_idle = false;
262 rdev->irq.gui_idle = true;
263 radeon_irq_set(rdev);
264 wait_event_interruptible_timeout(
265 rdev->irq.idle_queue, rdev->pm.gui_idle,
266 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
267 rdev->irq.gui_idle = false;
268 radeon_irq_set(rdev);
269 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400270 } else {
Christian Könige32eb502011-10-23 12:56:27 +0200271 struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
272 if (ring->ready) {
Christian König8a47cc92012-05-09 15:34:48 +0200273 radeon_fence_wait_empty_locked(rdev, RADEON_RING_TYPE_GFX_INDEX);
Alex Deucherce8f5372010-05-07 15:10:16 -0400274 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400275 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400276 radeon_unmap_vram_bos(rdev);
277
Alex Deucherce8f5372010-05-07 15:10:16 -0400278 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400279 for (i = 0; i < rdev->num_crtc; i++) {
280 if (rdev->pm.active_crtcs & (1 << i)) {
281 rdev->pm.req_vblank |= (1 << i);
282 drm_vblank_get(rdev->ddev, i);
283 }
284 }
285 }
Alex Deucher539d2412010-04-29 00:22:43 -0400286
Alex Deucherce8f5372010-05-07 15:10:16 -0400287 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400288
Alex Deucherce8f5372010-05-07 15:10:16 -0400289 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400290 for (i = 0; i < rdev->num_crtc; i++) {
291 if (rdev->pm.req_vblank & (1 << i)) {
292 rdev->pm.req_vblank &= ~(1 << i);
293 drm_vblank_put(rdev->ddev, i);
294 }
295 }
296 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400297
Alex Deuchera4248162010-04-24 14:50:23 -0400298 /* update display watermarks based on new power state */
299 radeon_update_bandwidth_info(rdev);
300 if (rdev->pm.active_crtc_count)
301 radeon_bandwidth_update(rdev);
302
Alex Deucherce8f5372010-05-07 15:10:16 -0400303 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400304
Christian Königd6999bc2012-05-09 15:34:45 +0200305 mutex_unlock(&rdev->ring_lock);
Christian Königdb7fce32012-05-11 14:57:18 +0200306 up_write(&rdev->pm.mclk_lock);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400307 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400308}
309
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400310static void radeon_pm_print_states(struct radeon_device *rdev)
311{
312 int i, j;
313 struct radeon_power_state *power_state;
314 struct radeon_pm_clock_info *clock_info;
315
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000316 DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400317 for (i = 0; i < rdev->pm.num_power_states; i++) {
318 power_state = &rdev->pm.power_state[i];
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000319 DRM_DEBUG_DRIVER("State %d: %s\n", i,
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400320 radeon_pm_state_type_name[power_state->type]);
321 if (i == rdev->pm.default_power_state_index)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000322 DRM_DEBUG_DRIVER("\tDefault");
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400323 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000324 DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400325 if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000326 DRM_DEBUG_DRIVER("\tSingle display only\n");
327 DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400328 for (j = 0; j < power_state->num_clock_modes; j++) {
329 clock_info = &(power_state->clock_info[j]);
330 if (rdev->flags & RADEON_IS_IGP)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000331 DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400332 j,
333 clock_info->sclk * 10,
334 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
335 else
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000336 DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400337 j,
338 clock_info->sclk * 10,
339 clock_info->mclk * 10,
340 clock_info->voltage.voltage,
341 clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
342 }
343 }
344}
345
Alex Deucherce8f5372010-05-07 15:10:16 -0400346static ssize_t radeon_get_pm_profile(struct device *dev,
347 struct device_attribute *attr,
348 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400349{
350 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
351 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400352 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400353
Alex Deucherce8f5372010-05-07 15:10:16 -0400354 return snprintf(buf, PAGE_SIZE, "%s\n",
355 (cp == PM_PROFILE_AUTO) ? "auto" :
356 (cp == PM_PROFILE_LOW) ? "low" :
Daniel J Blueman12e27be2010-07-28 12:25:58 +0100357 (cp == PM_PROFILE_MID) ? "mid" :
Alex Deucherce8f5372010-05-07 15:10:16 -0400358 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400359}
360
Alex Deucherce8f5372010-05-07 15:10:16 -0400361static ssize_t radeon_set_pm_profile(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf,
364 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400365{
366 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
367 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400368
369 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400370 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
371 if (strncmp("default", buf, strlen("default")) == 0)
372 rdev->pm.profile = PM_PROFILE_DEFAULT;
373 else if (strncmp("auto", buf, strlen("auto")) == 0)
374 rdev->pm.profile = PM_PROFILE_AUTO;
375 else if (strncmp("low", buf, strlen("low")) == 0)
376 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400377 else if (strncmp("mid", buf, strlen("mid")) == 0)
378 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400379 else if (strncmp("high", buf, strlen("high")) == 0)
380 rdev->pm.profile = PM_PROFILE_HIGH;
381 else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000382 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400383 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400384 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400385 radeon_pm_update_profile(rdev);
386 radeon_pm_set_clocks(rdev);
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000387 } else
388 count = -EINVAL;
389
Alex Deucherce8f5372010-05-07 15:10:16 -0400390fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400391 mutex_unlock(&rdev->pm.mutex);
392
393 return count;
394}
395
Alex Deucherce8f5372010-05-07 15:10:16 -0400396static ssize_t radeon_get_pm_method(struct device *dev,
397 struct device_attribute *attr,
398 char *buf)
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 Deucherce8f5372010-05-07 15:10:16 -0400402 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400403
404 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400405 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400406}
407
Alex Deucherce8f5372010-05-07 15:10:16 -0400408static ssize_t radeon_set_pm_method(struct device *dev,
409 struct device_attribute *attr,
410 const char *buf,
411 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400412{
413 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
414 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400415
Alex Deucherce8f5372010-05-07 15:10:16 -0400416
417 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400418 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400419 rdev->pm.pm_method = PM_METHOD_DYNPM;
420 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
421 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400422 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400423 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
424 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400425 /* disable dynpm */
426 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
427 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000428 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherce8f5372010-05-07 15:10:16 -0400429 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100430 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400431 } else {
Thomas Renninger1783e4b2011-03-23 15:14:09 +0000432 count = -EINVAL;
Alex Deucherce8f5372010-05-07 15:10:16 -0400433 goto fail;
434 }
435 radeon_pm_compute_clocks(rdev);
436fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400437 return count;
438}
439
Alex Deucherce8f5372010-05-07 15:10:16 -0400440static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
441static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400442
Alex Deucher21a81222010-07-02 12:58:16 -0400443static ssize_t radeon_hwmon_show_temp(struct device *dev,
444 struct device_attribute *attr,
445 char *buf)
446{
447 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
448 struct radeon_device *rdev = ddev->dev_private;
Alex Deucher20d391d2011-02-01 16:12:34 -0500449 int temp;
Alex Deucher21a81222010-07-02 12:58:16 -0400450
451 switch (rdev->pm.int_thermal_type) {
452 case THERMAL_TYPE_RV6XX:
453 temp = rv6xx_get_temp(rdev);
454 break;
455 case THERMAL_TYPE_RV770:
456 temp = rv770_get_temp(rdev);
457 break;
458 case THERMAL_TYPE_EVERGREEN:
Alex Deucher4fddba12011-01-06 21:19:22 -0500459 case THERMAL_TYPE_NI:
Alex Deucher21a81222010-07-02 12:58:16 -0400460 temp = evergreen_get_temp(rdev);
461 break;
Alex Deuchere33df252010-11-22 17:56:32 -0500462 case THERMAL_TYPE_SUMO:
463 temp = sumo_get_temp(rdev);
464 break;
Alex Deucher1bd47d22012-03-20 17:18:10 -0400465 case THERMAL_TYPE_SI:
466 temp = si_get_temp(rdev);
467 break;
Alex Deucher21a81222010-07-02 12:58:16 -0400468 default:
469 temp = 0;
470 break;
471 }
472
473 return snprintf(buf, PAGE_SIZE, "%d\n", temp);
474}
475
476static ssize_t radeon_hwmon_show_name(struct device *dev,
477 struct device_attribute *attr,
478 char *buf)
479{
480 return sprintf(buf, "radeon\n");
481}
482
483static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
484static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
485
486static struct attribute *hwmon_attributes[] = {
487 &sensor_dev_attr_temp1_input.dev_attr.attr,
488 &sensor_dev_attr_name.dev_attr.attr,
489 NULL
490};
491
492static const struct attribute_group hwmon_attrgroup = {
493 .attrs = hwmon_attributes,
494};
495
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200496static int radeon_hwmon_init(struct radeon_device *rdev)
Alex Deucher21a81222010-07-02 12:58:16 -0400497{
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200498 int err = 0;
Alex Deucher21a81222010-07-02 12:58:16 -0400499
500 rdev->pm.int_hwmon_dev = NULL;
501
502 switch (rdev->pm.int_thermal_type) {
503 case THERMAL_TYPE_RV6XX:
504 case THERMAL_TYPE_RV770:
505 case THERMAL_TYPE_EVERGREEN:
Alex Deucher457558e2011-05-25 17:49:54 -0400506 case THERMAL_TYPE_NI:
Alex Deuchere33df252010-11-22 17:56:32 -0500507 case THERMAL_TYPE_SUMO:
Alex Deucher1bd47d22012-03-20 17:18:10 -0400508 case THERMAL_TYPE_SI:
Alex Deucher5d7486c2012-03-20 17:18:29 -0400509 /* No support for TN yet */
510 if (rdev->family == CHIP_ARUBA)
511 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400512 rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200513 if (IS_ERR(rdev->pm.int_hwmon_dev)) {
514 err = PTR_ERR(rdev->pm.int_hwmon_dev);
515 dev_err(rdev->dev,
516 "Unable to register hwmon device: %d\n", err);
517 break;
518 }
Alex Deucher21a81222010-07-02 12:58:16 -0400519 dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
520 err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
521 &hwmon_attrgroup);
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200522 if (err) {
523 dev_err(rdev->dev,
524 "Unable to create hwmon sysfs file: %d\n", err);
525 hwmon_device_unregister(rdev->dev);
526 }
Alex Deucher21a81222010-07-02 12:58:16 -0400527 break;
528 default:
529 break;
530 }
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200531
532 return err;
Alex Deucher21a81222010-07-02 12:58:16 -0400533}
534
535static void radeon_hwmon_fini(struct radeon_device *rdev)
536{
537 if (rdev->pm.int_hwmon_dev) {
538 sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
539 hwmon_device_unregister(rdev->pm.int_hwmon_dev);
540 }
541}
542
Alex Deucherce8f5372010-05-07 15:10:16 -0400543void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500544{
Alex Deucherce8f5372010-05-07 15:10:16 -0400545 mutex_lock(&rdev->pm.mutex);
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000546 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000547 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
548 rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000549 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400550 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100551
552 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher56278a82009-12-28 13:58:44 -0500553}
554
Alex Deucherce8f5372010-05-07 15:10:16 -0400555void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100556{
Alex Deuchered18a362011-01-06 21:19:32 -0500557 /* set up the default clocks if the MC ucode is loaded */
558 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
559 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400560 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
561 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher2feea492011-04-12 14:49:24 -0400562 if (rdev->pm.default_vddci)
563 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
564 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500565 if (rdev->pm.default_sclk)
566 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
567 if (rdev->pm.default_mclk)
568 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
569 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400570 /* asic init will reset the default power state */
571 mutex_lock(&rdev->pm.mutex);
572 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
573 rdev->pm.current_clock_mode_index = 0;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500574 rdev->pm.current_sclk = rdev->pm.default_sclk;
575 rdev->pm.current_mclk = rdev->pm.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400576 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 -0400577 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 +0000578 if (rdev->pm.pm_method == PM_METHOD_DYNPM
579 && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
580 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100581 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
582 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000583 }
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400584 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400585 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100586}
587
Rafał Miłecki74338742009-11-03 00:53:02 +0100588int radeon_pm_init(struct radeon_device *rdev)
589{
Dave Airlie26481fb2010-05-18 19:00:14 +1000590 int ret;
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200591
Alex Deucherce8f5372010-05-07 15:10:16 -0400592 /* default to profile method */
593 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400594 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400595 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
596 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
597 rdev->pm.dynpm_can_upclock = true;
598 rdev->pm.dynpm_can_downclock = true;
Alex Deucher9ace9f72011-01-06 21:19:26 -0500599 rdev->pm.default_sclk = rdev->clock.default_sclk;
600 rdev->pm.default_mclk = rdev->clock.default_mclk;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400601 rdev->pm.current_sclk = rdev->clock.default_sclk;
602 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher21a81222010-07-02 12:58:16 -0400603 rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100604
Alex Deucher56278a82009-12-28 13:58:44 -0500605 if (rdev->bios) {
606 if (rdev->is_atom_bios)
607 radeon_atombios_get_power_modes(rdev);
608 else
609 radeon_combios_get_power_modes(rdev);
Rafał Miłeckif712d0c2010-06-07 18:29:44 -0400610 radeon_pm_print_states(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400611 radeon_pm_init_profile(rdev);
Alex Deuchered18a362011-01-06 21:19:32 -0500612 /* set up the default clocks if the MC ucode is loaded */
613 if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
614 if (rdev->pm.default_vddc)
Alex Deucher8a83ec52011-04-12 14:49:23 -0400615 radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
616 SET_VOLTAGE_TYPE_ASIC_VDDC);
Alex Deucher4639dd22011-07-25 18:50:08 -0400617 if (rdev->pm.default_vddci)
618 radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
619 SET_VOLTAGE_TYPE_ASIC_VDDCI);
Alex Deuchered18a362011-01-06 21:19:32 -0500620 if (rdev->pm.default_sclk)
621 radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
622 if (rdev->pm.default_mclk)
623 radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
624 }
Alex Deucher56278a82009-12-28 13:58:44 -0500625 }
626
Alex Deucher21a81222010-07-02 12:58:16 -0400627 /* set up the internal thermal sensor if applicable */
Dan Carpenter0d18abe2010-08-09 21:59:42 +0200628 ret = radeon_hwmon_init(rdev);
629 if (ret)
630 return ret;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100631
632 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
633
Alex Deucherce8f5372010-05-07 15:10:16 -0400634 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400635 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000636 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
637 if (ret)
638 DRM_ERROR("failed to create device file for power profile\n");
639 ret = device_create_file(rdev->dev, &dev_attr_power_method);
640 if (ret)
641 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400642
643#ifdef CONFIG_ACPI
644 rdev->acpi_nb.notifier_call = radeon_acpi_event;
645 register_acpi_notifier(&rdev->acpi_nb);
646#endif
Alex Deucherce8f5372010-05-07 15:10:16 -0400647 if (radeon_debugfs_pm_init(rdev)) {
648 DRM_ERROR("Failed to register debugfs file for PM!\n");
649 }
650
651 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100652 }
653
654 return 0;
655}
656
Alex Deucher29fb52c2010-03-11 10:01:17 -0500657void radeon_pm_fini(struct radeon_device *rdev)
658{
Alex Deucherce8f5372010-05-07 15:10:16 -0400659 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400660 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400661 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
662 rdev->pm.profile = PM_PROFILE_DEFAULT;
663 radeon_pm_update_profile(rdev);
664 radeon_pm_set_clocks(rdev);
665 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400666 /* reset default clocks */
667 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
668 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
669 radeon_pm_set_clocks(rdev);
670 }
Alex Deuchera4248162010-04-24 14:50:23 -0400671 mutex_unlock(&rdev->pm.mutex);
Tejun Heo32c87fc2011-01-03 14:49:32 +0100672
673 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
Alex Deucher58e21df2010-03-22 13:31:08 -0400674
Alex Deucherce8f5372010-05-07 15:10:16 -0400675 device_remove_file(rdev->dev, &dev_attr_power_profile);
676 device_remove_file(rdev->dev, &dev_attr_power_method);
677#ifdef CONFIG_ACPI
678 unregister_acpi_notifier(&rdev->acpi_nb);
679#endif
680 }
Alex Deuchera4248162010-04-24 14:50:23 -0400681
Alex Deucher0975b162011-02-02 18:42:03 -0500682 if (rdev->pm.power_state)
683 kfree(rdev->pm.power_state);
684
Alex Deucher21a81222010-07-02 12:58:16 -0400685 radeon_hwmon_fini(rdev);
Alex Deucher29fb52c2010-03-11 10:01:17 -0500686}
687
Rafał Miłeckic913e232009-12-22 23:02:16 +0100688void radeon_pm_compute_clocks(struct radeon_device *rdev)
689{
690 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400691 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100692 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100693
Alex Deucherce8f5372010-05-07 15:10:16 -0400694 if (rdev->pm.num_power_states < 2)
695 return;
696
Rafał Miłeckic913e232009-12-22 23:02:16 +0100697 mutex_lock(&rdev->pm.mutex);
698
699 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400700 rdev->pm.active_crtc_count = 0;
701 list_for_each_entry(crtc,
702 &ddev->mode_config.crtc_list, head) {
703 radeon_crtc = to_radeon_crtc(crtc);
704 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100705 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400706 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100707 }
708 }
709
Alex Deucherce8f5372010-05-07 15:10:16 -0400710 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
711 radeon_pm_update_profile(rdev);
712 radeon_pm_set_clocks(rdev);
713 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
714 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
715 if (rdev->pm.active_crtc_count > 1) {
716 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
717 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400718
Alex Deucherce8f5372010-05-07 15:10:16 -0400719 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
720 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
721 radeon_pm_get_dynpm_state(rdev);
722 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100723
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000724 DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400725 }
726 } else if (rdev->pm.active_crtc_count == 1) {
727 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100728
Alex Deucherce8f5372010-05-07 15:10:16 -0400729 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
730 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
731 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
732 radeon_pm_get_dynpm_state(rdev);
733 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100734
Tejun Heo32c87fc2011-01-03 14:49:32 +0100735 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
736 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deucherce8f5372010-05-07 15:10:16 -0400737 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
738 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
Tejun Heo32c87fc2011-01-03 14:49:32 +0100739 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
740 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000741 DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400742 }
743 } else { /* count == 0 */
744 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
745 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100746
Alex Deucherce8f5372010-05-07 15:10:16 -0400747 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
748 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
749 radeon_pm_get_dynpm_state(rdev);
750 radeon_pm_set_clocks(rdev);
751 }
752 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100753 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100754 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100755
756 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100757}
758
Alex Deucherce8f5372010-05-07 15:10:16 -0400759static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000760{
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400761 int crtc, vpos, hpos, vbl_status;
Dave Airlief7352612010-02-18 15:58:36 +1000762 bool in_vbl = true;
763
Mario Kleiner75fa0b02010-10-05 19:57:37 -0400764 /* Iterate over all active crtc's. All crtc's must be in vblank,
765 * otherwise return in_vbl == false.
766 */
767 for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
768 if (rdev->pm.active_crtcs & (1 << crtc)) {
Mario Kleinerf5a80202010-10-23 04:42:17 +0200769 vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
770 if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
771 !(vbl_status & DRM_SCANOUTPOS_INVBL))
Dave Airlief7352612010-02-18 15:58:36 +1000772 in_vbl = false;
773 }
774 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400775
776 return in_vbl;
777}
778
Alex Deucherce8f5372010-05-07 15:10:16 -0400779static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400780{
781 u32 stat_crtc = 0;
782 bool in_vbl = radeon_pm_in_vbl(rdev);
783
Dave Airlief7352612010-02-18 15:58:36 +1000784 if (in_vbl == false)
Dave Airlied9fdaaf2010-08-02 10:42:55 +1000785 DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400786 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000787 return in_vbl;
788}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100789
Alex Deucherce8f5372010-05-07 15:10:16 -0400790static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100791{
792 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400793 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100794 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400795 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100796
Matthew Garrettd9932a32010-04-26 16:02:26 -0400797 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100798 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400799 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100800 int not_processed = 0;
Alex Deucher74652802011-08-25 13:39:48 -0400801 int i;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100802
Alex Deucher74652802011-08-25 13:39:48 -0400803 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
Alex Deucher0ec06122012-06-14 15:54:57 -0400804 struct radeon_ring *ring = &rdev->ring[i];
805
806 if (ring->ready) {
807 not_processed += radeon_fence_count_emitted(rdev, i);
808 if (not_processed >= 3)
809 break;
810 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100811 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100812
813 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400814 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
815 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
816 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
817 rdev->pm.dynpm_can_upclock) {
818 rdev->pm.dynpm_planned_action =
819 DYNPM_ACTION_UPCLOCK;
820 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100821 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
822 }
823 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400824 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
825 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
826 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
827 rdev->pm.dynpm_can_downclock) {
828 rdev->pm.dynpm_planned_action =
829 DYNPM_ACTION_DOWNCLOCK;
830 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100831 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
832 }
833 }
834
Alex Deucherd7311172010-05-03 01:13:14 -0400835 /* Note, radeon_pm_set_clocks is called with static_switch set
836 * to false since we want to wait for vbl to avoid flicker.
837 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400838 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
839 jiffies > rdev->pm.dynpm_action_timeout) {
840 radeon_pm_get_dynpm_state(rdev);
841 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100842 }
Rafael J. Wysocki3f53eb62010-06-17 23:02:27 +0000843
Tejun Heo32c87fc2011-01-03 14:49:32 +0100844 schedule_delayed_work(&rdev->pm.dynpm_idle_work,
845 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Rafał Miłeckic913e232009-12-22 23:02:16 +0100846 }
847 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400848 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100849}
850
Rafał Miłecki74338742009-11-03 00:53:02 +0100851/*
852 * Debugfs info
853 */
854#if defined(CONFIG_DEBUG_FS)
855
856static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
857{
858 struct drm_info_node *node = (struct drm_info_node *) m->private;
859 struct drm_device *dev = node->minor->dev;
860 struct radeon_device *rdev = dev->dev_private;
861
Alex Deucher9ace9f72011-01-06 21:19:26 -0500862 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
Rafał Miłecki62340772009-12-15 21:46:58 +0100863 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
Alex Deucher9ace9f72011-01-06 21:19:26 -0500864 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
Alex Deucher798bcf72012-02-23 17:53:48 -0500865 if (rdev->asic->pm.get_memory_clock)
Rafał Miłecki62340772009-12-15 21:46:58 +0100866 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki0fcbe942010-06-07 18:25:21 -0400867 if (rdev->pm.current_vddc)
868 seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
Alex Deucher798bcf72012-02-23 17:53:48 -0500869 if (rdev->asic->pm.get_pcie_lanes)
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000870 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100871
872 return 0;
873}
874
875static struct drm_info_list radeon_pm_info_list[] = {
876 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
877};
878#endif
879
Rafał Miłeckic913e232009-12-22 23:02:16 +0100880static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100881{
882#if defined(CONFIG_DEBUG_FS)
883 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
884#else
885 return 0;
886#endif
887}