blob: 02281269a88153d4d04b9054233bf7c8320b8e51 [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)
87 rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
88 else
89 rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
90 }
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;
98 case PM_PROFILE_HIGH:
99 if (rdev->pm.active_crtc_count > 1)
100 rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
101 else
102 rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
103 break;
104 }
105
106 if (rdev->pm.active_crtc_count == 0) {
107 rdev->pm.requested_power_state_index =
108 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
109 rdev->pm.requested_clock_mode_index =
110 rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
111 } else {
112 rdev->pm.requested_power_state_index =
113 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
114 rdev->pm.requested_clock_mode_index =
115 rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
116 }
117}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100118
Matthew Garrett5876dd22010-04-26 15:52:20 -0400119static void radeon_unmap_vram_bos(struct radeon_device *rdev)
120{
121 struct radeon_bo *bo, *n;
122
123 if (list_empty(&rdev->gem.objects))
124 return;
125
126 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
127 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
128 ttm_bo_unmap_virtual(&bo->tbo);
129 }
130
131 if (rdev->gart.table.vram.robj)
132 ttm_bo_unmap_virtual(&rdev->gart.table.vram.robj->tbo);
133
134 if (rdev->stollen_vga_memory)
135 ttm_bo_unmap_virtual(&rdev->stollen_vga_memory->tbo);
136
137 if (rdev->r600_blit.shader_obj)
138 ttm_bo_unmap_virtual(&rdev->r600_blit.shader_obj->tbo);
139}
140
Alex Deucherce8f5372010-05-07 15:10:16 -0400141static void radeon_sync_with_vblank(struct radeon_device *rdev)
142{
143 if (rdev->pm.active_crtcs) {
144 rdev->pm.vblank_sync = false;
145 wait_event_timeout(
146 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
147 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
148 }
149}
150
151static void radeon_set_power_state(struct radeon_device *rdev)
152{
153 u32 sclk, mclk;
Alex Deucher92645872010-05-27 17:01:41 -0400154 bool misc_after = false;
Alex Deucherce8f5372010-05-07 15:10:16 -0400155
156 if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
157 (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
158 return;
159
160 if (radeon_gui_idle(rdev)) {
161 sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
162 clock_info[rdev->pm.requested_clock_mode_index].sclk;
163 if (sclk > rdev->clock.default_sclk)
164 sclk = rdev->clock.default_sclk;
165
166 mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
167 clock_info[rdev->pm.requested_clock_mode_index].mclk;
168 if (mclk > rdev->clock.default_mclk)
169 mclk = rdev->clock.default_mclk;
170
Alex Deucher92645872010-05-27 17:01:41 -0400171 /* upvolt before raising clocks, downvolt after lowering clocks */
172 if (sclk < rdev->pm.current_sclk)
173 misc_after = true;
174
175 radeon_sync_with_vblank(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400176
177 if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400178 if (!radeon_pm_in_vbl(rdev))
179 return;
Alex Deucherce8f5372010-05-07 15:10:16 -0400180 }
181
Alex Deucher92645872010-05-27 17:01:41 -0400182 radeon_pm_prepare(rdev);
183
184 if (!misc_after)
185 /* voltage, pcie lanes, etc.*/
186 radeon_pm_misc(rdev);
187
188 /* set engine clock */
189 if (sclk != rdev->pm.current_sclk) {
190 radeon_pm_debug_check_in_vbl(rdev, false);
191 radeon_set_engine_clock(rdev, sclk);
192 radeon_pm_debug_check_in_vbl(rdev, true);
193 rdev->pm.current_sclk = sclk;
194 DRM_DEBUG("Setting: e: %d\n", sclk);
195 }
196
197 /* set memory clock */
198 if (rdev->asic->set_memory_clock && (mclk != rdev->pm.current_mclk)) {
199 radeon_pm_debug_check_in_vbl(rdev, false);
200 radeon_set_memory_clock(rdev, mclk);
201 radeon_pm_debug_check_in_vbl(rdev, true);
202 rdev->pm.current_mclk = mclk;
203 DRM_DEBUG("Setting: m: %d\n", mclk);
204 }
205
206 if (misc_after)
207 /* voltage, pcie lanes, etc.*/
208 radeon_pm_misc(rdev);
209
210 radeon_pm_finish(rdev);
211
Alex Deucherce8f5372010-05-07 15:10:16 -0400212 rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
213 rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
214 } else
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400215 DRM_DEBUG("pm: GUI not idle!!!\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400216}
217
218static void radeon_pm_set_clocks(struct radeon_device *rdev)
Alex Deuchera4248162010-04-24 14:50:23 -0400219{
Matthew Garrett2aba6312010-04-26 15:45:23 -0400220 int i;
221
Matthew Garrett612e06c2010-04-27 17:16:58 -0400222 mutex_lock(&rdev->ddev->struct_mutex);
223 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400224 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -0400225
226 /* gui idle int has issues on older chips it seems */
227 if (rdev->family >= CHIP_R600) {
Alex Deucherce8f5372010-05-07 15:10:16 -0400228 if (rdev->irq.installed) {
229 /* wait for GPU idle */
230 rdev->pm.gui_idle = false;
231 rdev->irq.gui_idle = true;
232 radeon_irq_set(rdev);
233 wait_event_interruptible_timeout(
234 rdev->irq.idle_queue, rdev->pm.gui_idle,
235 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
236 rdev->irq.gui_idle = false;
237 radeon_irq_set(rdev);
238 }
Matthew Garrett01434b42010-04-30 15:48:23 -0400239 } else {
Alex Deucherce8f5372010-05-07 15:10:16 -0400240 if (rdev->cp.ready) {
241 struct radeon_fence *fence;
242 radeon_ring_alloc(rdev, 64);
243 radeon_fence_create(rdev, &fence);
244 radeon_fence_emit(rdev, fence);
245 radeon_ring_commit(rdev);
246 radeon_fence_wait(fence, false);
247 radeon_fence_unref(&fence);
248 }
Alex Deucher4f3218c2010-04-29 16:14:02 -0400249 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400250 radeon_unmap_vram_bos(rdev);
251
Alex Deucherce8f5372010-05-07 15:10:16 -0400252 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400253 for (i = 0; i < rdev->num_crtc; i++) {
254 if (rdev->pm.active_crtcs & (1 << i)) {
255 rdev->pm.req_vblank |= (1 << i);
256 drm_vblank_get(rdev->ddev, i);
257 }
258 }
259 }
Alex Deucher539d2412010-04-29 00:22:43 -0400260
Alex Deucherce8f5372010-05-07 15:10:16 -0400261 radeon_set_power_state(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400262
Alex Deucherce8f5372010-05-07 15:10:16 -0400263 if (rdev->irq.installed) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400264 for (i = 0; i < rdev->num_crtc; i++) {
265 if (rdev->pm.req_vblank & (1 << i)) {
266 rdev->pm.req_vblank &= ~(1 << i);
267 drm_vblank_put(rdev->ddev, i);
268 }
269 }
270 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400271
Alex Deuchera4248162010-04-24 14:50:23 -0400272 /* update display watermarks based on new power state */
273 radeon_update_bandwidth_info(rdev);
274 if (rdev->pm.active_crtc_count)
275 radeon_bandwidth_update(rdev);
276
Alex Deucherce8f5372010-05-07 15:10:16 -0400277 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400278
Alex Deuchera4248162010-04-24 14:50:23 -0400279 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400280 mutex_unlock(&rdev->vram_mutex);
281 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400282}
283
Alex Deucherce8f5372010-05-07 15:10:16 -0400284static ssize_t radeon_get_pm_profile(struct device *dev,
285 struct device_attribute *attr,
286 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400287{
288 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
289 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400290 int cp = rdev->pm.profile;
Alex Deuchera4248162010-04-24 14:50:23 -0400291
Alex Deucherce8f5372010-05-07 15:10:16 -0400292 return snprintf(buf, PAGE_SIZE, "%s\n",
293 (cp == PM_PROFILE_AUTO) ? "auto" :
294 (cp == PM_PROFILE_LOW) ? "low" :
295 (cp == PM_PROFILE_HIGH) ? "high" : "default");
Alex Deuchera4248162010-04-24 14:50:23 -0400296}
297
Alex Deucherce8f5372010-05-07 15:10:16 -0400298static ssize_t radeon_set_pm_profile(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf,
301 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400302{
303 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
304 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400305
306 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400307 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
308 if (strncmp("default", buf, strlen("default")) == 0)
309 rdev->pm.profile = PM_PROFILE_DEFAULT;
310 else if (strncmp("auto", buf, strlen("auto")) == 0)
311 rdev->pm.profile = PM_PROFILE_AUTO;
312 else if (strncmp("low", buf, strlen("low")) == 0)
313 rdev->pm.profile = PM_PROFILE_LOW;
314 else if (strncmp("high", buf, strlen("high")) == 0)
315 rdev->pm.profile = PM_PROFILE_HIGH;
316 else {
317 DRM_ERROR("invalid power profile!\n");
318 goto fail;
Alex Deuchera4248162010-04-24 14:50:23 -0400319 }
Alex Deucherce8f5372010-05-07 15:10:16 -0400320 radeon_pm_update_profile(rdev);
321 radeon_pm_set_clocks(rdev);
322 }
323fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400324 mutex_unlock(&rdev->pm.mutex);
325
326 return count;
327}
328
Alex Deucherce8f5372010-05-07 15:10:16 -0400329static ssize_t radeon_get_pm_method(struct device *dev,
330 struct device_attribute *attr,
331 char *buf)
Alex Deuchera4248162010-04-24 14:50:23 -0400332{
333 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
334 struct radeon_device *rdev = ddev->dev_private;
Alex Deucherce8f5372010-05-07 15:10:16 -0400335 int pm = rdev->pm.pm_method;
Alex Deuchera4248162010-04-24 14:50:23 -0400336
337 return snprintf(buf, PAGE_SIZE, "%s\n",
Alex Deucherce8f5372010-05-07 15:10:16 -0400338 (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
Alex Deuchera4248162010-04-24 14:50:23 -0400339}
340
Alex Deucherce8f5372010-05-07 15:10:16 -0400341static ssize_t radeon_set_pm_method(struct device *dev,
342 struct device_attribute *attr,
343 const char *buf,
344 size_t count)
Alex Deuchera4248162010-04-24 14:50:23 -0400345{
346 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
347 struct radeon_device *rdev = ddev->dev_private;
Alex Deuchera4248162010-04-24 14:50:23 -0400348
Alex Deucherce8f5372010-05-07 15:10:16 -0400349
350 if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
Alex Deuchera4248162010-04-24 14:50:23 -0400351 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400352 rdev->pm.pm_method = PM_METHOD_DYNPM;
353 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
354 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
Alex Deuchera4248162010-04-24 14:50:23 -0400355 mutex_unlock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400356 } else if (strncmp("profile", buf, strlen("profile")) == 0) {
357 mutex_lock(&rdev->pm.mutex);
358 rdev->pm.pm_method = PM_METHOD_PROFILE;
359 /* disable dynpm */
360 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
361 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
362 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
363 mutex_unlock(&rdev->pm.mutex);
364 } else {
365 DRM_ERROR("invalid power method!\n");
366 goto fail;
367 }
368 radeon_pm_compute_clocks(rdev);
369fail:
Alex Deuchera4248162010-04-24 14:50:23 -0400370 return count;
371}
372
Alex Deucherce8f5372010-05-07 15:10:16 -0400373static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
374static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
Alex Deuchera4248162010-04-24 14:50:23 -0400375
Alex Deucherce8f5372010-05-07 15:10:16 -0400376void radeon_pm_suspend(struct radeon_device *rdev)
Alex Deucher56278a82009-12-28 13:58:44 -0500377{
Alex Deucherce8f5372010-05-07 15:10:16 -0400378 mutex_lock(&rdev->pm.mutex);
379 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
380 rdev->pm.current_power_state_index = -1;
381 rdev->pm.current_clock_mode_index = -1;
382 rdev->pm.current_sclk = 0;
383 rdev->pm.current_mclk = 0;
384 mutex_unlock(&rdev->pm.mutex);
Alex Deucher56278a82009-12-28 13:58:44 -0500385}
386
Alex Deucherce8f5372010-05-07 15:10:16 -0400387void radeon_pm_resume(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100388{
Alex Deucherce8f5372010-05-07 15:10:16 -0400389 radeon_pm_compute_clocks(rdev);
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100390}
391
Rafał Miłecki74338742009-11-03 00:53:02 +0100392int radeon_pm_init(struct radeon_device *rdev)
393{
Dave Airlie26481fb2010-05-18 19:00:14 +1000394 int ret;
Alex Deucherce8f5372010-05-07 15:10:16 -0400395 /* default to profile method */
396 rdev->pm.pm_method = PM_METHOD_PROFILE;
397 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
398 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
399 rdev->pm.dynpm_can_upclock = true;
400 rdev->pm.dynpm_can_downclock = true;
401 rdev->pm.current_sclk = 0;
402 rdev->pm.current_mclk = 0;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100403
Alex Deucher56278a82009-12-28 13:58:44 -0500404 if (rdev->bios) {
405 if (rdev->is_atom_bios)
406 radeon_atombios_get_power_modes(rdev);
407 else
408 radeon_combios_get_power_modes(rdev);
Alex Deucherce8f5372010-05-07 15:10:16 -0400409 radeon_pm_init_profile(rdev);
410 rdev->pm.current_power_state_index = -1;
411 rdev->pm.current_clock_mode_index = -1;
Alex Deucher56278a82009-12-28 13:58:44 -0500412 }
413
Alex Deucherce8f5372010-05-07 15:10:16 -0400414 if (rdev->pm.num_power_states > 1) {
415 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
416 mutex_lock(&rdev->pm.mutex);
417 rdev->pm.profile = PM_PROFILE_DEFAULT;
418 radeon_pm_update_profile(rdev);
419 radeon_pm_set_clocks(rdev);
420 mutex_unlock(&rdev->pm.mutex);
421 }
422
423 /* where's the best place to put these? */
Dave Airlie26481fb2010-05-18 19:00:14 +1000424 ret = device_create_file(rdev->dev, &dev_attr_power_profile);
425 if (ret)
426 DRM_ERROR("failed to create device file for power profile\n");
427 ret = device_create_file(rdev->dev, &dev_attr_power_method);
428 if (ret)
429 DRM_ERROR("failed to create device file for power method\n");
Alex Deucherce8f5372010-05-07 15:10:16 -0400430
431#ifdef CONFIG_ACPI
432 rdev->acpi_nb.notifier_call = radeon_acpi_event;
433 register_acpi_notifier(&rdev->acpi_nb);
434#endif
435 INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
436
437 if (radeon_debugfs_pm_init(rdev)) {
438 DRM_ERROR("Failed to register debugfs file for PM!\n");
439 }
440
441 DRM_INFO("radeon: power management initialized\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100442 }
443
444 return 0;
445}
446
Alex Deucher29fb52c2010-03-11 10:01:17 -0500447void radeon_pm_fini(struct radeon_device *rdev)
448{
Alex Deucherce8f5372010-05-07 15:10:16 -0400449 if (rdev->pm.num_power_states > 1) {
Alex Deuchera4248162010-04-24 14:50:23 -0400450 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400451 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
452 rdev->pm.profile = PM_PROFILE_DEFAULT;
453 radeon_pm_update_profile(rdev);
454 radeon_pm_set_clocks(rdev);
455 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
456 /* cancel work */
457 cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
458 /* reset default clocks */
459 rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
460 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
461 radeon_pm_set_clocks(rdev);
462 }
Alex Deuchera4248162010-04-24 14:50:23 -0400463 mutex_unlock(&rdev->pm.mutex);
Alex Deucher58e21df2010-03-22 13:31:08 -0400464
Alex Deucherce8f5372010-05-07 15:10:16 -0400465 device_remove_file(rdev->dev, &dev_attr_power_profile);
466 device_remove_file(rdev->dev, &dev_attr_power_method);
467#ifdef CONFIG_ACPI
468 unregister_acpi_notifier(&rdev->acpi_nb);
469#endif
470 }
Alex Deuchera4248162010-04-24 14:50:23 -0400471
Alex Deucher29fb52c2010-03-11 10:01:17 -0500472 if (rdev->pm.i2c_bus)
473 radeon_i2c_destroy(rdev->pm.i2c_bus);
474}
475
Rafał Miłeckic913e232009-12-22 23:02:16 +0100476void radeon_pm_compute_clocks(struct radeon_device *rdev)
477{
478 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400479 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100480 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100481
Alex Deucherce8f5372010-05-07 15:10:16 -0400482 if (rdev->pm.num_power_states < 2)
483 return;
484
Rafał Miłeckic913e232009-12-22 23:02:16 +0100485 mutex_lock(&rdev->pm.mutex);
486
487 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400488 rdev->pm.active_crtc_count = 0;
489 list_for_each_entry(crtc,
490 &ddev->mode_config.crtc_list, head) {
491 radeon_crtc = to_radeon_crtc(crtc);
492 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100493 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400494 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100495 }
496 }
497
Alex Deucherce8f5372010-05-07 15:10:16 -0400498 if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
499 radeon_pm_update_profile(rdev);
500 radeon_pm_set_clocks(rdev);
501 } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
502 if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
503 if (rdev->pm.active_crtc_count > 1) {
504 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
505 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Alex Deucherd7311172010-05-03 01:13:14 -0400506
Alex Deucherce8f5372010-05-07 15:10:16 -0400507 rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
508 rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
509 radeon_pm_get_dynpm_state(rdev);
510 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100511
Alex Deucherce8f5372010-05-07 15:10:16 -0400512 DRM_DEBUG("radeon: dynamic power management deactivated\n");
513 }
514 } else if (rdev->pm.active_crtc_count == 1) {
515 /* TODO: Increase clocks if needed for current mode */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100516
Alex Deucherce8f5372010-05-07 15:10:16 -0400517 if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
518 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
519 rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
520 radeon_pm_get_dynpm_state(rdev);
521 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100522
Alex Deucherce8f5372010-05-07 15:10:16 -0400523 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
524 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
525 } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
526 rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
527 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
528 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
529 DRM_DEBUG("radeon: dynamic power management activated\n");
530 }
531 } else { /* count == 0 */
532 if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
533 cancel_delayed_work(&rdev->pm.dynpm_idle_work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100534
Alex Deucherce8f5372010-05-07 15:10:16 -0400535 rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
536 rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
537 radeon_pm_get_dynpm_state(rdev);
538 radeon_pm_set_clocks(rdev);
539 }
540 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100541 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100542 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100543
544 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100545}
546
Alex Deucherce8f5372010-05-07 15:10:16 -0400547static bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000548{
Alex Deucher539d2412010-04-29 00:22:43 -0400549 u32 stat_crtc = 0, vbl = 0, position = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000550 bool in_vbl = true;
551
Alex Deucherbae6b5622010-04-22 13:38:05 -0400552 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000553 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400554 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
555 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
556 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
557 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
Dave Airlief7352612010-02-18 15:58:36 +1000558 }
559 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400560 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
561 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
562 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
563 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400564 }
565 if (rdev->pm.active_crtcs & (1 << 2)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400566 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
567 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
568 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
569 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400570 }
571 if (rdev->pm.active_crtcs & (1 << 3)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400572 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
573 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
574 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
575 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400576 }
577 if (rdev->pm.active_crtcs & (1 << 4)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400578 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
579 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
580 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
581 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400582 }
583 if (rdev->pm.active_crtcs & (1 << 5)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400584 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
585 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
586 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
587 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400588 }
589 } else if (ASIC_IS_AVIVO(rdev)) {
590 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400591 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
592 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400593 }
594 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400595 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
596 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400597 }
Alex Deucher539d2412010-04-29 00:22:43 -0400598 if (position < vbl && position > 1)
599 in_vbl = false;
Alex Deucherbae6b5622010-04-22 13:38:05 -0400600 } else {
601 if (rdev->pm.active_crtcs & (1 << 0)) {
602 stat_crtc = RREG32(RADEON_CRTC_STATUS);
603 if (!(stat_crtc & 1))
604 in_vbl = false;
605 }
606 if (rdev->pm.active_crtcs & (1 << 1)) {
607 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
608 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000609 in_vbl = false;
610 }
611 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400612
Alex Deucher539d2412010-04-29 00:22:43 -0400613 if (position < vbl && position > 1)
614 in_vbl = false;
615
Matthew Garrettf81f2022010-04-28 12:13:06 -0400616 return in_vbl;
617}
618
Alex Deucherce8f5372010-05-07 15:10:16 -0400619static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Matthew Garrettf81f2022010-04-28 12:13:06 -0400620{
621 u32 stat_crtc = 0;
622 bool in_vbl = radeon_pm_in_vbl(rdev);
623
Dave Airlief7352612010-02-18 15:58:36 +1000624 if (in_vbl == false)
Alex Deucherce8a3eb2010-05-07 16:58:27 -0400625 DRM_DEBUG("not in vbl for pm change %08x at %s\n", stat_crtc,
Alex Deucherbae6b5622010-04-22 13:38:05 -0400626 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000627 return in_vbl;
628}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100629
Alex Deucherce8f5372010-05-07 15:10:16 -0400630static void radeon_dynpm_idle_work_handler(struct work_struct *work)
Rafał Miłeckic913e232009-12-22 23:02:16 +0100631{
632 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400633 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100634 rdev = container_of(work, struct radeon_device,
Alex Deucherce8f5372010-05-07 15:10:16 -0400635 pm.dynpm_idle_work.work);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100636
Matthew Garrettd9932a32010-04-26 16:02:26 -0400637 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100638 mutex_lock(&rdev->pm.mutex);
Alex Deucherce8f5372010-05-07 15:10:16 -0400639 if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100640 unsigned long irq_flags;
641 int not_processed = 0;
642
643 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
644 if (!list_empty(&rdev->fence_drv.emited)) {
645 struct list_head *ptr;
646 list_for_each(ptr, &rdev->fence_drv.emited) {
647 /* count up to 3, that's enought info */
648 if (++not_processed >= 3)
649 break;
650 }
651 }
652 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
653
654 if (not_processed >= 3) { /* should upclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400655 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
656 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
657 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
658 rdev->pm.dynpm_can_upclock) {
659 rdev->pm.dynpm_planned_action =
660 DYNPM_ACTION_UPCLOCK;
661 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100662 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
663 }
664 } else if (not_processed == 0) { /* should downclock */
Alex Deucherce8f5372010-05-07 15:10:16 -0400665 if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
666 rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
667 } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
668 rdev->pm.dynpm_can_downclock) {
669 rdev->pm.dynpm_planned_action =
670 DYNPM_ACTION_DOWNCLOCK;
671 rdev->pm.dynpm_action_timeout = jiffies +
Rafał Miłeckic913e232009-12-22 23:02:16 +0100672 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
673 }
674 }
675
Alex Deucherd7311172010-05-03 01:13:14 -0400676 /* Note, radeon_pm_set_clocks is called with static_switch set
677 * to false since we want to wait for vbl to avoid flicker.
678 */
Alex Deucherce8f5372010-05-07 15:10:16 -0400679 if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
680 jiffies > rdev->pm.dynpm_action_timeout) {
681 radeon_pm_get_dynpm_state(rdev);
682 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100683 }
684 }
685 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400686 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100687
Alex Deucherce8f5372010-05-07 15:10:16 -0400688 queue_delayed_work(rdev->wq, &rdev->pm.dynpm_idle_work,
Rafał Miłeckic913e232009-12-22 23:02:16 +0100689 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
690}
691
Rafał Miłecki74338742009-11-03 00:53:02 +0100692/*
693 * Debugfs info
694 */
695#if defined(CONFIG_DEBUG_FS)
696
697static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
698{
699 struct drm_info_node *node = (struct drm_info_node *) m->private;
700 struct drm_device *dev = node->minor->dev;
701 struct radeon_device *rdev = dev->dev_private;
702
Rafał Miłecki62340772009-12-15 21:46:58 +0100703 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
704 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
705 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
706 if (rdev->asic->get_memory_clock)
707 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000708 if (rdev->asic->get_pcie_lanes)
709 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100710
711 return 0;
712}
713
714static struct drm_info_list radeon_pm_info_list[] = {
715 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
716};
717#endif
718
Rafał Miłeckic913e232009-12-22 23:02:16 +0100719static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100720{
721#if defined(CONFIG_DEBUG_FS)
722 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
723#else
724 return 0;
725#endif
726}