blob: f2e1c60afda5cbdf9683a6aa8b5a5d23449850e5 [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 Deucherce8f5372010-05-07 15:10:16 -040026#ifdef CONFIG_ACPI
27#include <linux/acpi.h>
28#endif
29#include <linux/power_supply.h>
Rafał Miłecki74338742009-11-03 00:53:02 +010030
Rafał Miłeckic913e232009-12-22 23:02:16 +010031#define RADEON_IDLE_LOOP_MS 100
32#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010033#define RADEON_WAIT_VBLANK_TIMEOUT 200
Alex Deucher2031f772010-04-22 12:52:11 -040034#define RADEON_WAIT_IDLE_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010035
Alex Deucherce8f5372010-05-07 15:10:16 -040036static void radeon_dynpm_idle_work_handler(struct work_struct *work);
Rafał Miłeckic913e232009-12-22 23:02:16 +010037static int radeon_debugfs_pm_init(struct radeon_device *rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -040038static bool radeon_pm_in_vbl(struct radeon_device *rdev);
39static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
40static void radeon_pm_update_profile(struct radeon_device *rdev);
41static void radeon_pm_set_clocks(struct radeon_device *rdev);
42
43#define ACPI_AC_CLASS "ac_adapter"
44
45#ifdef CONFIG_ACPI
46static int radeon_acpi_event(struct notifier_block *nb,
47 unsigned long val,
48 void *data)
49{
50 struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
51 struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
52
53 if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
54 if (power_supply_is_system_supplied() > 0)
Alex Deucherce8a3eb2010-05-07 16:58:27 -040055 DRM_DEBUG("pm: AC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040056 else
Alex Deucherce8a3eb2010-05-07 16:58:27 -040057 DRM_DEBUG("pm: DC\n");
Alex Deucherce8f5372010-05-07 15:10:16 -040058
59 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
60 if (rdev->pm.profile == PM_PROFILE_AUTO) {
61 mutex_lock(&rdev->pm.mutex);
62 radeon_pm_update_profile(rdev);
63 radeon_pm_set_clocks(rdev);
64 mutex_unlock(&rdev->pm.mutex);
65 }
66 }
67 }
68
69 return NOTIFY_OK;
70}
71#endif
72
73static void radeon_pm_update_profile(struct radeon_device *rdev)
74{
75 switch (rdev->pm.profile) {
76 case PM_PROFILE_DEFAULT:
77 rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
78 break;
79 case PM_PROFILE_AUTO:
80 if (power_supply_is_system_supplied() > 0) {
81 if (rdev->pm.active_crtc_count > 1)
82 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
83 else
84 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
85 } else {
86 if (rdev->pm.active_crtc_count > 1)
Alex Deucherc9e75b22010-06-02 17:56:01 -040087 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040088 else
Alex Deucherc9e75b22010-06-02 17:56:01 -040089 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
Alex Deucherce8f5372010-05-07 15:10:16 -040090 }
91 break;
92 case PM_PROFILE_LOW:
93 if (rdev->pm.active_crtc_count > 1)
94 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
95 else
96 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
97 break;
Alex Deucherc9e75b22010-06-02 17:56:01 -040098 case PM_PROFILE_MID:
99 if (rdev->pm.active_crtc_count > 1)
100 rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
101 else
102 rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
103 break;
Alex Deucherce8f5372010-05-07 15:10:16 -0400104 case PM_PROFILE_HIGH:
105 if (rdev->pm.active_crtc_count > 1)
106 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
107 else
108 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
109 break;
110 }
111
112 if (rdev->pm.active_crtc_count == 0) {
113 rdev->pm.requested_power_state_index =
114 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
115 rdev->pm.requested_clock_mode_index =
116 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
117 } else {
118 rdev->pm.requested_power_state_index =
119 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
120 rdev->pm.requested_clock_mode_index =
121 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
122 }
123}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100124
Matthew Garrett5876dd22010-04-26 15:52:20 -0400125static void radeon_unmap_vram_bos(struct radeon_device *rdev)
126{
127 struct radeon_bo *bo, *n;
128
129 if (list_empty(&rdev->gem.objects))
130 return;
131
132 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
133 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
134 ttm_bo_unmap_virtual(&bo->tbo);
135 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400136}
137
Alex Deucherce8f5372010-05-07 15:10:16 -0400138static void radeon_sync_with_vblank(struct radeon_device *rdev)
139{
140 if (rdev->pm.active_crtcs) {
141 rdev->pm.vblank_sync = false;
142 wait_event_timeout(
143 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
144 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
145 }
146}
147
148static void radeon_set_power_state(struct radeon_device *rdev)
149{
150 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400151 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400152
153 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
154 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
155 return;
156
157 if (radeon_gui_idle(rdev)) {
158 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
159 clock_info[rdev->pm.requested_clock_mode_index].sclk;
160 if (sclk > rdev->clock.default_sclk)
161 sclk = rdev->clock.default_sclk;
162
163 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
164 clock_info[rdev->pm.requested_clock_mode_index].mclk;
165 if (mclk > rdev->clock.default_mclk)
166 mclk = rdev->clock.default_mclk;
167
Alex Deucher92645872010-05-27 17:01:41 -0400168 /* upvolt before raising clocks, downvolt after lowering clocks */
169 if (sclk < rdev->pm.current_sclk)
170 misc_after = true;
171
172 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400173
174 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400175 if (!radeon_pm_in_vbl(rdev))
176 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400177 }
178
Alex Deucher92645872010-05-27 17:01:41 -0400179 radeon_pm_prepare(rdev);
180
181 if (!misc_after)
182 /* voltage, pcie lanes, etc.*/
183 radeon_pm_misc(rdev);
184
185 /* set engine clock */
186 if (sclk != rdev->pm.current_sclk) {
187 radeon_pm_debug_check_in_vbl(rdev, false);
188 radeon_set_engine_clock(rdev, sclk);
189 radeon_pm_debug_check_in_vbl(rdev, true);
190 rdev->pm.current_sclk = sclk;
191 DRM_DEBUG("Setting: e: %d\n", sclk);
192 }
193
194 /* set memory clock */
195 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
196 radeon_pm_debug_check_in_vbl(rdev, false);
197 radeon_set_memory_clock(rdev, mclk);
198 radeon_pm_debug_check_in_vbl(rdev, true);
199 rdev->pm.current_mclk = mclk;
200 DRM_DEBUG("Setting: m: %d\n", mclk);
201 }
202
203 if (misc_after)
204 /* voltage, pcie lanes, etc.*/
205 radeon_pm_misc(rdev);
206
207 radeon_pm_finish(rdev);
208
Alex Deucherce8f5372010-05-07 15:10:16 -0400209 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
210 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
211 } else
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400212 DRM_DEBUG("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400213}
214
215static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400216{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400217 int i;
218
Matthew Garrett612e06c2010-04-27 17:16:58 -0400219 mutex_lock(&rdev->ddev->struct_mutex);
220 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400221 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400222
223 /* gui idle int has issues on older chips it seems */
224 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400225 if (rdev->irq.installed) {
226 /* wait for GPU idle */
227 rdev->pm.gui_idle = false;
228 rdev->irq.gui_idle = true;
229 radeon_irq_set(rdev);
230 wait_event_interruptible_timeout(
231 rdev->irq.idle_queue, rdev->pm.gui_idle,
232 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
233 rdev->irq.gui_idle = false;
234 radeon_irq_set(rdev);
235 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400236 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400237 if (rdev->cp.ready) {
238 struct radeon_fence *fence;
239 radeon_ring_alloc(rdev, 64);
240 radeon_fence_create(rdev, &fence);
241 radeon_fence_emit(rdev, fence);
242 radeon_ring_commit(rdev);
243 radeon_fence_wait(fence, false);
244 radeon_fence_unref(&fence);
245 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400246 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400247 radeon_unmap_vram_bos(rdev);
248
Alex Deucherce8f5372010-05-07 15:10:16 -0400249 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400250 for (i = 0; i < rdev->num_crtc; i++) {
251 if (rdev->pm.active_crtcs & (1 << i)) {
252 rdev->pm.req_vblank |= (1 << i);
253 drm_vblank_get(rdev->ddev, i);
254 }
255 }
256 }
Alex Deucher539d2412010-04-29 00:22:43 -0400257
Alex Deucherce8f5372010-05-07 15:10:16 -0400258 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400259
Alex Deucherce8f5372010-05-07 15:10:16 -0400260 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400261 for (i = 0; i < rdev->num_crtc; i++) {
262 if (rdev->pm.req_vblank & (1 << i)) {
263 rdev->pm.req_vblank &= ~(1 << i);
264 drm_vblank_put(rdev->ddev, i);
265 }
266 }
267 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400268
Alex Deuchera4248162010-04-24 14:50:23 -0400269 /* update display watermarks based on new power state */
270 radeon_update_bandwidth_info(rdev);
271 if (rdev->pm.active_crtc_count)
272 radeon_bandwidth_update(rdev);
273
Alex Deucherce8f5372010-05-07 15:10:16 -0400274 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400275
Alex Deuchera4248162010-04-24 14:50:23 -0400276 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400277 mutex_unlock(&rdev->vram_mutex);
278 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400279}
280
Alex Deucherce8f5372010-05-07 15:10:16 -0400281static ssize_t radeon_get_pm_profile(struct device *dev,
282 struct device_attribute *attr,
283 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400284{
285 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
286 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400287 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400288
Alex Deucherce8f5372010-05-07 15:10:16 -0400289 return snprintf(buf, PAGE_SIZE, "%s\n",
290 (cp == PM_PROFILE_AUTO) ? "auto" :
291 (cp == PM_PROFILE_LOW) ? "low" :
292 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400293}
294
Alex Deucherce8f5372010-05-07 15:10:16 -0400295static ssize_t radeon_set_pm_profile(struct device *dev,
296 struct device_attribute *attr,
297 const char *buf,
298 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400299{
300 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
301 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400302
303 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400304 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
305 if (strncmp("default", buf, strlen("default")) == 0)
306 rdev->pm.profile = PM_PROFILE_DEFAULT;
307 else if (strncmp("auto", buf, strlen("auto")) == 0)
308 rdev->pm.profile = PM_PROFILE_AUTO;
309 else if (strncmp("low", buf, strlen("low")) == 0)
310 rdev->pm.profile = PM_PROFILE_LOW;
Alex Deucherc9e75b22010-06-02 17:56:01 -0400311 else if (strncmp("mid", buf, strlen("mid")) == 0)
312 rdev->pm.profile = PM_PROFILE_MID;
Alex Deucherce8f5372010-05-07 15:10:16 -0400313 else if (strncmp("high", buf, strlen("high")) == 0)
314 rdev->pm.profile = PM_PROFILE_HIGH;
315 else {
316 DRM_ERROR("invalid power profile!\n");
317 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400318 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400319 radeon_pm_update_profile(rdev);
320 radeon_pm_set_clocks(rdev);
321 }
322fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400323 mutex_unlock(&rdev->pm.mutex);
324
325 return count;
326}
327
Alex Deucherce8f5372010-05-07 15:10:16 -0400328static ssize_t radeon_get_pm_method(struct device *dev,
329 struct device_attribute *attr,
330 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400331{
332 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
333 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400334 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400335
336 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400337 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400338}
339
Alex Deucherce8f5372010-05-07 15:10:16 -0400340static ssize_t radeon_set_pm_method(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf,
343 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400344{
345 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
346 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400347
Alex Deucherce8f5372010-05-07 15:10:16 -0400348
349 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400350 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400351 rdev->pm.pm_method = PM_METHOD_DYNPM;
352 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
353 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400354 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400355 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
356 mutex_lock(&rdev->pm.mutex);
357 rdev->pm.pm_method = PM_METHOD_PROFILE;
358 /* disable dynpm */
359 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
360 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
361 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
362 mutex_unlock(&rdev->pm.mutex);
363 } else {
364 DRM_ERROR("invalid power method!\n");
365 goto fail;
366 }
367 radeon_pm_compute_clocks(rdev);
368fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400369 return count;
370}
371
Alex Deucherce8f5372010-05-07 15:10:16 -0400372static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
373static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400374
Alex Deucherce8f5372010-05-07 15:10:16 -0400375void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500376{
Alex Deucherce8f5372010-05-07 15:10:16 -0400377 mutex_lock(&rdev->pm.mutex);
378 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherce8f5372010-05-07 15:10:16 -0400379 mutex_unlock(&rdev->pm.mutex);
Alex Deucher56278a82009-12-28 13:58:44 -0500380}
381
Alex Deucherce8f5372010-05-07 15:10:16 -0400382void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100383{
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400384 /* asic init will reset the default power state */
385 mutex_lock(&rdev->pm.mutex);
386 rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
387 rdev->pm.current_clock_mode_index = 0;
388 rdev->pm.current_sclk = rdev->clock.default_sclk;
389 rdev->pm.current_mclk = rdev->clock.default_mclk;
Alex Deucher4d601732010-06-07 18:15:18 -0400390 rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400391 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400392 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100393}
394
Rafał Miłecki74338742009-11-03 00:53:02 +0100395int radeon_pm_init(struct radeon_device *rdev)
396{
Dave Airlie26481fb2010-05-18 19:00:14 +1000397 int ret;
Alex Deucherce8f5372010-05-07 15:10:16 -0400398 /* default to profile method */
399 rdev->pm.pm_method = PM_METHOD_PROFILE;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400400 rdev->pm.profile = PM_PROFILE_DEFAULT;
Alex Deucherce8f5372010-05-07 15:10:16 -0400401 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
402 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
403 rdev->pm.dynpm_can_upclock = true;
404 rdev->pm.dynpm_can_downclock = true;
Alex Deucherf8ed8b42010-06-07 17:49:51 -0400405 rdev->pm.current_sclk = rdev->clock.default_sclk;
406 rdev->pm.current_mclk = rdev->clock.default_mclk;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100407
Alex Deucher56278a82009-12-28 13:58:44 -0500408 if (rdev->bios) {
409 if (rdev->is_atom_bios)
410 radeon_atombios_get_power_modes(rdev);
411 else
412 radeon_combios_get_power_modes(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400413 radeon_pm_init_profile(rdev);
Alex Deucher56278a82009-12-28 13:58:44 -0500414 }
415
Alex Deucherce8f5372010-05-07 15:10:16 -0400416 if (rdev->pm.num_power_states > 1) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400417 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000418 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
419 if (ret)
420 DRM_ERROR("failed to create device file for power profile\n");
421 ret = device_create_file(rdev->dev, &dev_attr_power_method);
422 if (ret)
423 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400424
425#ifdef CONFIG_ACPI
426 rdev->acpi_nb.notifier_call = radeon_acpi_event;
427 register_acpi_notifier(&rdev->acpi_nb);
428#endif
429 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
430
431 if (radeon_debugfs_pm_init(rdev)) {
432 DRM_ERROR("Failed to register debugfs file for PM!\n");
433 }
434
435 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100436 }
437
438 return 0;
439}
440
Alex Deucher29fb52c2010-03-11 10:01:17 -0500441void radeon_pm_fini(struct radeon_device *rdev)
442{
Alex Deucherce8f5372010-05-07 15:10:16 -0400443 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400444 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400445 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
446 rdev->pm.profile = PM_PROFILE_DEFAULT;
447 radeon_pm_update_profile(rdev);
448 radeon_pm_set_clocks(rdev);
449 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
450 /* cancel work */
451 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
452 /* reset default clocks */
453 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
454 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
455 radeon_pm_set_clocks(rdev);
456 }
Alex Deuchera4248162010-04-24 14:50:23 -0400457 mutex_unlock(&rdev->pm.mutex);
Alex Deucher58e21df2010-03-22 13:31:08 -0400458
Alex Deucherce8f5372010-05-07 15:10:16 -0400459 device_remove_file(rdev->dev, &dev_attr_power_profile);
460 device_remove_file(rdev->dev, &dev_attr_power_method);
461#ifdef CONFIG_ACPI
462 unregister_acpi_notifier(&rdev->acpi_nb);
463#endif
464 }
Alex Deuchera4248162010-04-24 14:50:23 -0400465
Alex Deucher29fb52c2010-03-11 10:01:17 -0500466 if (rdev->pm.i2c_bus)
467 radeon_i2c_destroy(rdev->pm.i2c_bus);
468}
469
Rafał Miłeckic913e232009-12-22 23:02:16 +0100470void radeon_pm_compute_clocks(struct radeon_device *rdev)
471{
472 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400473 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100474 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100475
Alex Deucherce8f5372010-05-07 15:10:16 -0400476 if (rdev->pm.num_power_states < 2)
477 return;
478
Rafał Miłeckic913e232009-12-22 23:02:16 +0100479 mutex_lock(&rdev->pm.mutex);
480
481 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400482 rdev->pm.active_crtc_count = 0;
483 list_for_each_entry(crtc,
484 &ddev->mode_config.crtc_list, head) {
485 radeon_crtc = to_radeon_crtc(crtc);
486 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100487 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400488 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100489 }
490 }
491
Alex Deucherce8f5372010-05-07 15:10:16 -0400492 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
493 radeon_pm_update_profile(rdev);
494 radeon_pm_set_clocks(rdev);
495 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
496 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
497 if (rdev->pm.active_crtc_count > 1) {
498 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
499 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400500
Alex Deucherce8f5372010-05-07 15:10:16 -0400501 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
502 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
503 radeon_pm_get_dynpm_state(rdev);
504 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100505
Alex Deucherce8f5372010-05-07 15:10:16 -0400506 DRM_DEBUG("radeon: dynamic power management deactivated\n");
507 }
508 } else if (rdev->pm.active_crtc_count == 1) {
509 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100510
Alex Deucherce8f5372010-05-07 15:10:16 -0400511 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
512 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
513 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
514 radeon_pm_get_dynpm_state(rdev);
515 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100516
Alex Deucherce8f5372010-05-07 15:10:16 -0400517 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
518 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
519 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
520 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
521 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
522 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
523 DRM_DEBUG("radeon: dynamic power management activated\n");
524 }
525 } else { /* count == 0 */
526 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
527 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100528
Alex Deucherce8f5372010-05-07 15:10:16 -0400529 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
530 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
531 radeon_pm_get_dynpm_state(rdev);
532 radeon_pm_set_clocks(rdev);
533 }
534 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100535 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100536 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100537
538 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100539}
540
Alex Deucherce8f5372010-05-07 15:10:16 -0400541static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000542{
Alex Deucher539d2412010-04-29 00:22:43 -0400543 u32 stat_crtc = 0, vbl = 0, position = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000544 bool in_vbl = true;
545
Alex Deucherbae6b5622010-04-22 13:38:05 -0400546 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000547 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400548 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
549 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
550 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
551 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
Dave Airlief7352612010-02-18 15:58:36 +1000552 }
553 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400554 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
555 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
556 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
557 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400558 }
559 if (rdev->pm.active_crtcs & (1 << 2)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400560 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
561 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
562 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
563 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400564 }
565 if (rdev->pm.active_crtcs & (1 << 3)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400566 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
567 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
568 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
569 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400570 }
571 if (rdev->pm.active_crtcs & (1 << 4)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400572 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
573 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
574 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
575 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400576 }
577 if (rdev->pm.active_crtcs & (1 << 5)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400578 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
579 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
580 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
581 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400582 }
583 } else if (ASIC_IS_AVIVO(rdev)) {
584 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400585 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
586 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400587 }
588 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400589 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
590 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400591 }
Alex Deucher539d2412010-04-29 00:22:43 -0400592 if (position < vbl && position > 1)
593 in_vbl = false;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400594 } else {
595 if (rdev->pm.active_crtcs & (1 << 0)) {
596 stat_crtc = RREG32(RADEON_CRTC_STATUS);
597 if (!(stat_crtc & 1))
598 in_vbl = false;
599 }
600 if (rdev->pm.active_crtcs & (1 << 1)) {
601 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
602 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000603 in_vbl = false;
604 }
605 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400606
Alex Deucher539d2412010-04-29 00:22:43 -0400607 if (position < vbl && position > 1)
608 in_vbl = false;
609
Matthew Garrettf81f2022010-04-28 12:13:06 -0400610 return in_vbl;
611}
612
Alex Deucherce8f5372010-05-07 15:10:16 -0400613static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400614{
615 u32 stat_crtc = 0;
616 bool in_vbl = radeon_pm_in_vbl(rdev);
617
Dave Airlief7352612010-02-18 15:58:36 +1000618 if (in_vbl == false)
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400619 DRM_DEBUG("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400620 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000621 return in_vbl;
622}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100623
Alex Deucherce8f5372010-05-07 15:10:16 -0400624static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100625{
626 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400627 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100628 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400629 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100630
Matthew Garrettd9932a32010-04-26 16:02:26 -0400631 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100632 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400633 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100634 unsigned long irq_flags;
635 int not_processed = 0;
636
637 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
638 if (!list_empty(&rdev->fence_drv.emited)) {
639 struct list_head *ptr;
640 list_for_each(ptr, &rdev->fence_drv.emited) {
641 /* count up to 3, that's enought info */
642 if (++not_processed >= 3)
643 break;
644 }
645 }
646 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
647
648 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400649 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
650 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
651 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
652 rdev->pm.dynpm_can_upclock) {
653 rdev->pm.dynpm_planned_action =
654 DYNPM_ACTION_UPCLOCK;
655 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100656 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
657 }
658 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400659 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
660 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
661 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
662 rdev->pm.dynpm_can_downclock) {
663 rdev->pm.dynpm_planned_action =
664 DYNPM_ACTION_DOWNCLOCK;
665 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100666 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
667 }
668 }
669
Alex Deucherd7311172010-05-03 01:13:14 -0400670 /* Note, radeon_pm_set_clocks is called with static_switch set
671 * to false since we want to wait for vbl to avoid flicker.
672 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400673 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
674 jiffies > rdev->pm.dynpm_action_timeout) {
675 radeon_pm_get_dynpm_state(rdev);
676 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100677 }
678 }
679 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400680 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100681
Alex Deucherce8f5372010-05-07 15:10:16 -0400682 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
Rafał Miłeckic913e232009-12-22 23:02:16 +0100683 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
684}
685
Rafał Miłecki74338742009-11-03 00:53:02 +0100686/*
687 * Debugfs info
688 */
689#if defined(CONFIG_DEBUG_FS)
690
691static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
692{
693 struct drm_info_node *node = (struct drm_info_node *) m->private;
694 struct drm_device *dev = node->minor->dev;
695 struct radeon_device *rdev = dev->dev_private;
696
Rafał Miłecki62340772009-12-15 21:46:58 +0100697 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
698 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
699 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
700 if (rdev->asic->get_memory_clock)
701 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000702 if (rdev->asic->get_pcie_lanes)
703 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100704
705 return 0;
706}
707
708static struct drm_info_list radeon_pm_info_list[] = {
709 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
710};
711#endif
712
Rafał Miłeckic913e232009-12-22 23:02:16 +0100713static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100714{
715#if defined(CONFIG_DEBUG_FS)
716 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
717#else
718 return 0;
719#endif
720}