blob: 7cc54c804cb04d21037f55ffd04f30d3206b82b3 [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"
Rafał Miłecki74338742009-11-03 00:53:02 +010026
Rafał Miłeckic913e232009-12-22 23:02:16 +010027#define RADEON_IDLE_LOOP_MS 100
28#define RADEON_RECLOCK_DELAY_MS 200
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +010029#define RADEON_WAIT_VBLANK_TIMEOUT 200
Alex Deucher2031f772010-04-22 12:52:11 -040030#define RADEON_WAIT_IDLE_TIMEOUT 200
Rafał Miłeckic913e232009-12-22 23:02:16 +010031
Rafał Miłeckic913e232009-12-22 23:02:16 +010032static void radeon_pm_idle_work_handler(struct work_struct *work);
33static int radeon_debugfs_pm_init(struct radeon_device *rdev);
34
Matthew Garrett5876dd22010-04-26 15:52:20 -040035static void radeon_unmap_vram_bos(struct radeon_device *rdev)
36{
37 struct radeon_bo *bo, *n;
38
39 if (list_empty(&rdev->gem.objects))
40 return;
41
42 list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
43 if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
44 ttm_bo_unmap_virtual(&bo->tbo);
45 }
46
47 if (rdev->gart.table.vram.robj)
48 ttm_bo_unmap_virtual(&rdev->gart.table.vram.robj->tbo);
49
50 if (rdev->stollen_vga_memory)
51 ttm_bo_unmap_virtual(&rdev->stollen_vga_memory->tbo);
52
53 if (rdev->r600_blit.shader_obj)
54 ttm_bo_unmap_virtual(&rdev->r600_blit.shader_obj->tbo);
55}
56
Matthew Garrett2aba6312010-04-26 15:45:23 -040057static void radeon_pm_set_clocks(struct radeon_device *rdev, int static_switch)
Alex Deuchera4248162010-04-24 14:50:23 -040058{
Matthew Garrett2aba6312010-04-26 15:45:23 -040059 int i;
60
Matthew Garrettc37d2302010-04-27 13:58:46 -040061 if (!static_switch)
62 radeon_get_power_state(rdev, rdev->pm.planned_action);
63
Alex Deuchera4248162010-04-24 14:50:23 -040064 mutex_lock(&rdev->cp.mutex);
65
66 /* wait for GPU idle */
67 rdev->pm.gui_idle = false;
68 rdev->irq.gui_idle = true;
69 radeon_irq_set(rdev);
70 wait_event_interruptible_timeout(
71 rdev->irq.idle_queue, rdev->pm.gui_idle,
72 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
73 rdev->irq.gui_idle = false;
74 radeon_irq_set(rdev);
75
Matthew Garrett5876dd22010-04-26 15:52:20 -040076 mutex_lock(&rdev->vram_mutex);
77
78 radeon_unmap_vram_bos(rdev);
79
Matthew Garrett2aba6312010-04-26 15:45:23 -040080 if (!static_switch) {
81 for (i = 0; i < rdev->num_crtc; i++) {
82 if (rdev->pm.active_crtcs & (1 << i)) {
83 rdev->pm.req_vblank |= (1 << i);
84 drm_vblank_get(rdev->ddev, i);
85 }
86 }
87 }
88
89 radeon_set_power_state(rdev, static_switch);
Alex Deuchera4248162010-04-24 14:50:23 -040090
Matthew Garrett2aba6312010-04-26 15:45:23 -040091 if (!static_switch) {
92 for (i = 0; i < rdev->num_crtc; i++) {
93 if (rdev->pm.req_vblank & (1 << i)) {
94 rdev->pm.req_vblank &= ~(1 << i);
95 drm_vblank_put(rdev->ddev, i);
96 }
97 }
98 }
Matthew Garrett5876dd22010-04-26 15:52:20 -040099
100 mutex_unlock(&rdev->vram_mutex);
Matthew Garrett2aba6312010-04-26 15:45:23 -0400101
Alex Deuchera4248162010-04-24 14:50:23 -0400102 /* update display watermarks based on new power state */
103 radeon_update_bandwidth_info(rdev);
104 if (rdev->pm.active_crtc_count)
105 radeon_bandwidth_update(rdev);
106
Matthew Garrett2aba6312010-04-26 15:45:23 -0400107 rdev->pm.planned_action = PM_ACTION_NONE;
108
Alex Deuchera4248162010-04-24 14:50:23 -0400109 mutex_unlock(&rdev->cp.mutex);
110}
111
112static ssize_t radeon_get_power_state_static(struct device *dev,
113 struct device_attribute *attr,
114 char *buf)
115{
116 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
117 struct radeon_device *rdev = ddev->dev_private;
118
119 return snprintf(buf, PAGE_SIZE, "%d.%d\n", rdev->pm.current_power_state_index,
120 rdev->pm.current_clock_mode_index);
121}
122
123static ssize_t radeon_set_power_state_static(struct device *dev,
124 struct device_attribute *attr,
125 const char *buf,
126 size_t count)
127{
128 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
129 struct radeon_device *rdev = ddev->dev_private;
130 int ps, cm;
131
132 if (sscanf(buf, "%u.%u", &ps, &cm) != 2) {
133 DRM_ERROR("Invalid power state!\n");
134 return count;
135 }
136
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400137 mutex_lock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400138 mutex_lock(&rdev->pm.mutex);
139 if ((ps >= 0) && (ps < rdev->pm.num_power_states) &&
140 (cm >= 0) && (cm < rdev->pm.power_state[ps].num_clock_modes)) {
141 if ((rdev->pm.active_crtc_count > 1) &&
142 (rdev->pm.power_state[ps].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)) {
143 DRM_ERROR("Invalid power state for multi-head: %d.%d\n", ps, cm);
144 } else {
145 /* disable dynpm */
146 rdev->pm.state = PM_STATE_DISABLED;
147 rdev->pm.planned_action = PM_ACTION_NONE;
148 rdev->pm.requested_power_state_index = ps;
149 rdev->pm.requested_clock_mode_index = cm;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400150 radeon_pm_set_clocks(rdev, true);
Alex Deuchera4248162010-04-24 14:50:23 -0400151 }
152 } else
153 DRM_ERROR("Invalid power state: %d.%d\n\n", ps, cm);
154 mutex_unlock(&rdev->pm.mutex);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400155 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400156
157 return count;
158}
159
160static ssize_t radeon_get_dynpm(struct device *dev,
161 struct device_attribute *attr,
162 char *buf)
163{
164 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
165 struct radeon_device *rdev = ddev->dev_private;
166
167 return snprintf(buf, PAGE_SIZE, "%s\n",
168 (rdev->pm.state == PM_STATE_DISABLED) ? "disabled" : "enabled");
169}
170
171static ssize_t radeon_set_dynpm(struct device *dev,
172 struct device_attribute *attr,
173 const char *buf,
174 size_t count)
175{
176 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
177 struct radeon_device *rdev = ddev->dev_private;
178 int tmp = simple_strtoul(buf, NULL, 10);
179
180 if (tmp == 0) {
181 /* update power mode info */
182 radeon_pm_compute_clocks(rdev);
183 /* disable dynpm */
184 mutex_lock(&rdev->pm.mutex);
185 rdev->pm.state = PM_STATE_DISABLED;
186 rdev->pm.planned_action = PM_ACTION_NONE;
187 mutex_unlock(&rdev->pm.mutex);
188 DRM_INFO("radeon: dynamic power management disabled\n");
189 } else if (tmp == 1) {
190 if (rdev->pm.num_power_states > 1) {
191 /* enable dynpm */
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400192 mutex_lock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400193 mutex_lock(&rdev->pm.mutex);
194 rdev->pm.state = PM_STATE_PAUSED;
195 rdev->pm.planned_action = PM_ACTION_DEFAULT;
196 radeon_get_power_state(rdev, rdev->pm.planned_action);
197 mutex_unlock(&rdev->pm.mutex);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400198 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400199 /* update power mode info */
200 radeon_pm_compute_clocks(rdev);
201 DRM_INFO("radeon: dynamic power management enabled\n");
202 } else
203 DRM_ERROR("dynpm not valid on this system\n");
204 } else
205 DRM_ERROR("Invalid setting: %d\n", tmp);
206
207 return count;
208}
209
210static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, radeon_get_power_state_static, radeon_set_power_state_static);
211static DEVICE_ATTR(dynpm, S_IRUGO | S_IWUSR, radeon_get_dynpm, radeon_set_dynpm);
212
213
Rafał Miłeckic913e232009-12-22 23:02:16 +0100214static const char *pm_state_names[4] = {
215 "PM_STATE_DISABLED",
216 "PM_STATE_MINIMUM",
217 "PM_STATE_PAUSED",
218 "PM_STATE_ACTIVE"
219};
Rafał Miłecki74338742009-11-03 00:53:02 +0100220
Alex Deucher0ec0e742009-12-23 13:21:58 -0500221static const char *pm_state_types[5] = {
Alex Deucherd91eeb72010-04-05 15:26:43 -0400222 "",
Alex Deucher0ec0e742009-12-23 13:21:58 -0500223 "Powersave",
224 "Battery",
225 "Balanced",
226 "Performance",
227};
228
Alex Deucher56278a82009-12-28 13:58:44 -0500229static void radeon_print_power_mode_info(struct radeon_device *rdev)
230{
231 int i, j;
232 bool is_default;
233
234 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
235 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deuchera48b9b42010-04-22 14:03:55 -0400236 if (rdev->pm.default_power_state_index == i)
Alex Deucher56278a82009-12-28 13:58:44 -0500237 is_default = true;
238 else
239 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -0500240 DRM_INFO("State %d %s %s\n", i,
241 pm_state_types[rdev->pm.power_state[i].type],
242 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -0500243 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Alex Deucher79daedc2010-04-22 14:25:19 -0400244 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].pcie_lanes);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400245 if (rdev->pm.power_state[i].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)
246 DRM_INFO("\tSingle display only\n");
Alex Deucher56278a82009-12-28 13:58:44 -0500247 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
248 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
249 if (rdev->flags & RADEON_IS_IGP)
250 DRM_INFO("\t\t%d engine: %d\n",
251 j,
252 rdev->pm.power_state[i].clock_info[j].sclk * 10);
253 else
254 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
255 j,
256 rdev->pm.power_state[i].clock_info[j].sclk * 10,
257 rdev->pm.power_state[i].clock_info[j].mclk * 10);
258 }
259 }
260}
261
Alex Deucherbae6b5622010-04-22 13:38:05 -0400262void radeon_sync_with_vblank(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100263{
264 if (rdev->pm.active_crtcs) {
265 rdev->pm.vblank_sync = false;
266 wait_event_timeout(
267 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
268 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
269 }
270}
271
Rafał Miłecki74338742009-11-03 00:53:02 +0100272int radeon_pm_init(struct radeon_device *rdev)
273{
Rafał Miłeckic913e232009-12-22 23:02:16 +0100274 rdev->pm.state = PM_STATE_DISABLED;
275 rdev->pm.planned_action = PM_ACTION_NONE;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400276 rdev->pm.can_upclock = true;
277 rdev->pm.can_downclock = true;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100278
Alex Deucher56278a82009-12-28 13:58:44 -0500279 if (rdev->bios) {
280 if (rdev->is_atom_bios)
281 radeon_atombios_get_power_modes(rdev);
282 else
283 radeon_combios_get_power_modes(rdev);
284 radeon_print_power_mode_info(rdev);
285 }
286
Rafał Miłecki74338742009-11-03 00:53:02 +0100287 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100288 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100289 }
290
Alex Deuchera4248162010-04-24 14:50:23 -0400291 /* where's the best place to put this? */
292 device_create_file(rdev->dev, &dev_attr_power_state);
293 device_create_file(rdev->dev, &dev_attr_dynpm);
294
Rafał Miłeckic913e232009-12-22 23:02:16 +0100295 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
296
Alex Deucher90c39052010-03-24 11:32:29 -0400297 if ((radeon_dynpm != -1 && radeon_dynpm) && (rdev->pm.num_power_states > 1)) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100298 rdev->pm.state = PM_STATE_PAUSED;
299 DRM_INFO("radeon: dynamic power management enabled\n");
300 }
301
302 DRM_INFO("radeon: power management initialized\n");
303
Rafał Miłecki74338742009-11-03 00:53:02 +0100304 return 0;
305}
306
Alex Deucher29fb52c2010-03-11 10:01:17 -0500307void radeon_pm_fini(struct radeon_device *rdev)
308{
Alex Deucher58e21df2010-03-22 13:31:08 -0400309 if (rdev->pm.state != PM_STATE_DISABLED) {
310 /* cancel work */
311 cancel_delayed_work_sync(&rdev->pm.idle_work);
312 /* reset default clocks */
313 rdev->pm.state = PM_STATE_DISABLED;
314 rdev->pm.planned_action = PM_ACTION_DEFAULT;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400315 radeon_pm_set_clocks(rdev, false);
Alex Deuchera4248162010-04-24 14:50:23 -0400316 } else if ((rdev->pm.current_power_state_index !=
317 rdev->pm.default_power_state_index) ||
318 (rdev->pm.current_clock_mode_index != 0)) {
319 rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
320 rdev->pm.requested_clock_mode_index = 0;
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400321 mutex_lock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400322 mutex_lock(&rdev->pm.mutex);
Matthew Garrett2aba6312010-04-26 15:45:23 -0400323 radeon_pm_set_clocks(rdev, true);
Alex Deuchera4248162010-04-24 14:50:23 -0400324 mutex_unlock(&rdev->pm.mutex);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400325 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deucher58e21df2010-03-22 13:31:08 -0400326 }
327
Alex Deuchera4248162010-04-24 14:50:23 -0400328 device_remove_file(rdev->dev, &dev_attr_power_state);
329 device_remove_file(rdev->dev, &dev_attr_dynpm);
330
Alex Deucher29fb52c2010-03-11 10:01:17 -0500331 if (rdev->pm.i2c_bus)
332 radeon_i2c_destroy(rdev->pm.i2c_bus);
333}
334
Rafał Miłeckic913e232009-12-22 23:02:16 +0100335void radeon_pm_compute_clocks(struct radeon_device *rdev)
336{
337 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400338 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100339 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100340
341 if (rdev->pm.state == PM_STATE_DISABLED)
342 return;
343
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400344 mutex_lock(&rdev->ddev->struct_mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100345 mutex_lock(&rdev->pm.mutex);
346
347 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400348 rdev->pm.active_crtc_count = 0;
349 list_for_each_entry(crtc,
350 &ddev->mode_config.crtc_list, head) {
351 radeon_crtc = to_radeon_crtc(crtc);
352 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100353 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400354 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100355 }
356 }
357
Alex Deuchera48b9b42010-04-22 14:03:55 -0400358 if (rdev->pm.active_crtc_count > 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100359 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100360 cancel_delayed_work(&rdev->pm.idle_work);
361
362 rdev->pm.state = PM_STATE_PAUSED;
363 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400364 radeon_pm_set_clocks(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100365
366 DRM_DEBUG("radeon: dynamic power management deactivated\n");
Rafał Miłeckic913e232009-12-22 23:02:16 +0100367 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400368 } else if (rdev->pm.active_crtc_count == 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100369 /* TODO: Increase clocks if needed for current mode */
370
371 if (rdev->pm.state == PM_STATE_MINIMUM) {
372 rdev->pm.state = PM_STATE_ACTIVE;
373 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400374 radeon_pm_set_clocks(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100375
376 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
377 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deuchera48b9b42010-04-22 14:03:55 -0400378 } else if (rdev->pm.state == PM_STATE_PAUSED) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100379 rdev->pm.state = PM_STATE_ACTIVE;
380 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
381 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
382 DRM_DEBUG("radeon: dynamic power management activated\n");
383 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400384 } else { /* count == 0 */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100385 if (rdev->pm.state != PM_STATE_MINIMUM) {
386 cancel_delayed_work(&rdev->pm.idle_work);
387
388 rdev->pm.state = PM_STATE_MINIMUM;
389 rdev->pm.planned_action = PM_ACTION_MINIMUM;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400390 radeon_pm_set_clocks(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100391 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100392 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100393
394 mutex_unlock(&rdev->pm.mutex);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400395 mutex_unlock(&rdev->ddev->struct_mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100396}
397
Alex Deucherbae6b5622010-04-22 13:38:05 -0400398bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Dave Airlief7352612010-02-18 15:58:36 +1000399{
Alex Deucherbae6b5622010-04-22 13:38:05 -0400400 u32 stat_crtc = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000401 bool in_vbl = true;
402
Alex Deucherbae6b5622010-04-22 13:38:05 -0400403 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000404 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucherbae6b5622010-04-22 13:38:05 -0400405 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET);
406 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000407 in_vbl = false;
408 }
409 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucherbae6b5622010-04-22 13:38:05 -0400410 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET);
411 if (!(stat_crtc & 1))
412 in_vbl = false;
413 }
414 if (rdev->pm.active_crtcs & (1 << 2)) {
415 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET);
416 if (!(stat_crtc & 1))
417 in_vbl = false;
418 }
419 if (rdev->pm.active_crtcs & (1 << 3)) {
420 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET);
421 if (!(stat_crtc & 1))
422 in_vbl = false;
423 }
424 if (rdev->pm.active_crtcs & (1 << 4)) {
425 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET);
426 if (!(stat_crtc & 1))
427 in_vbl = false;
428 }
429 if (rdev->pm.active_crtcs & (1 << 5)) {
430 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET);
431 if (!(stat_crtc & 1))
432 in_vbl = false;
433 }
434 } else if (ASIC_IS_AVIVO(rdev)) {
435 if (rdev->pm.active_crtcs & (1 << 0)) {
436 stat_crtc = RREG32(D1CRTC_STATUS);
437 if (!(stat_crtc & 1))
438 in_vbl = false;
439 }
440 if (rdev->pm.active_crtcs & (1 << 1)) {
441 stat_crtc = RREG32(D2CRTC_STATUS);
442 if (!(stat_crtc & 1))
443 in_vbl = false;
444 }
445 } else {
446 if (rdev->pm.active_crtcs & (1 << 0)) {
447 stat_crtc = RREG32(RADEON_CRTC_STATUS);
448 if (!(stat_crtc & 1))
449 in_vbl = false;
450 }
451 if (rdev->pm.active_crtcs & (1 << 1)) {
452 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
453 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000454 in_vbl = false;
455 }
456 }
457 if (in_vbl == false)
Alex Deucherbae6b5622010-04-22 13:38:05 -0400458 DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc,
459 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000460 return in_vbl;
461}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100462
463static void radeon_pm_idle_work_handler(struct work_struct *work)
464{
465 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400466 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100467 rdev = container_of(work, struct radeon_device,
468 pm.idle_work.work);
469
Matthew Garrettd9932a32010-04-26 16:02:26 -0400470 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400471 mutex_lock(&rdev->ddev->struct_mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100472 mutex_lock(&rdev->pm.mutex);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100473 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100474 unsigned long irq_flags;
475 int not_processed = 0;
476
477 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
478 if (!list_empty(&rdev->fence_drv.emited)) {
479 struct list_head *ptr;
480 list_for_each(ptr, &rdev->fence_drv.emited) {
481 /* count up to 3, that's enought info */
482 if (++not_processed >= 3)
483 break;
484 }
485 }
486 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
487
488 if (not_processed >= 3) { /* should upclock */
489 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
490 rdev->pm.planned_action = PM_ACTION_NONE;
491 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400492 rdev->pm.can_upclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100493 rdev->pm.planned_action =
494 PM_ACTION_UPCLOCK;
495 rdev->pm.action_timeout = jiffies +
496 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
497 }
498 } else if (not_processed == 0) { /* should downclock */
499 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
500 rdev->pm.planned_action = PM_ACTION_NONE;
501 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400502 rdev->pm.can_downclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100503 rdev->pm.planned_action =
504 PM_ACTION_DOWNCLOCK;
505 rdev->pm.action_timeout = jiffies +
506 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
507 }
508 }
509
510 if (rdev->pm.planned_action != PM_ACTION_NONE &&
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100511 jiffies > rdev->pm.action_timeout) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400512 radeon_pm_set_clocks(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100513 }
514 }
515 mutex_unlock(&rdev->pm.mutex);
Matthew Garrett8f5b5e62010-04-26 15:57:01 -0400516 mutex_unlock(&rdev->ddev->struct_mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400517 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100518
519 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
520 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
521}
522
Rafał Miłecki74338742009-11-03 00:53:02 +0100523/*
524 * Debugfs info
525 */
526#if defined(CONFIG_DEBUG_FS)
527
528static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
529{
530 struct drm_info_node *node = (struct drm_info_node *) m->private;
531 struct drm_device *dev = node->minor->dev;
532 struct radeon_device *rdev = dev->dev_private;
533
Rafał Miłeckic913e232009-12-22 23:02:16 +0100534 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100535 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
536 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
537 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
538 if (rdev->asic->get_memory_clock)
539 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000540 if (rdev->asic->get_pcie_lanes)
541 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100542
543 return 0;
544}
545
546static struct drm_info_list radeon_pm_info_list[] = {
547 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
548};
549#endif
550
Rafał Miłeckic913e232009-12-22 23:02:16 +0100551static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100552{
553#if defined(CONFIG_DEBUG_FS)
554 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
555#else
556 return 0;
557#endif
558}