blob: c703ae326bc39933444c8b04da9c68b99b8274be [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_set_clocks_locked(struct radeon_device *rdev);
33static void radeon_pm_set_clocks(struct radeon_device *rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +010034static void radeon_pm_idle_work_handler(struct work_struct *work);
35static int radeon_debugfs_pm_init(struct radeon_device *rdev);
36
Alex Deuchera4248162010-04-24 14:50:23 -040037static void radeon_pm_set_power_mode_static_locked(struct radeon_device *rdev)
38{
39 mutex_lock(&rdev->cp.mutex);
40
41 /* wait for GPU idle */
42 rdev->pm.gui_idle = false;
43 rdev->irq.gui_idle = true;
44 radeon_irq_set(rdev);
45 wait_event_interruptible_timeout(
46 rdev->irq.idle_queue, rdev->pm.gui_idle,
47 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
48 rdev->irq.gui_idle = false;
49 radeon_irq_set(rdev);
50
51 radeon_set_power_state(rdev, true);
52
53 /* update display watermarks based on new power state */
54 radeon_update_bandwidth_info(rdev);
55 if (rdev->pm.active_crtc_count)
56 radeon_bandwidth_update(rdev);
57
58 mutex_unlock(&rdev->cp.mutex);
59}
60
61static ssize_t radeon_get_power_state_static(struct device *dev,
62 struct device_attribute *attr,
63 char *buf)
64{
65 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
66 struct radeon_device *rdev = ddev->dev_private;
67
68 return snprintf(buf, PAGE_SIZE, "%d.%d\n", rdev->pm.current_power_state_index,
69 rdev->pm.current_clock_mode_index);
70}
71
72static ssize_t radeon_set_power_state_static(struct device *dev,
73 struct device_attribute *attr,
74 const char *buf,
75 size_t count)
76{
77 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
78 struct radeon_device *rdev = ddev->dev_private;
79 int ps, cm;
80
81 if (sscanf(buf, "%u.%u", &ps, &cm) != 2) {
82 DRM_ERROR("Invalid power state!\n");
83 return count;
84 }
85
86 mutex_lock(&rdev->pm.mutex);
87 if ((ps >= 0) && (ps < rdev->pm.num_power_states) &&
88 (cm >= 0) && (cm < rdev->pm.power_state[ps].num_clock_modes)) {
89 if ((rdev->pm.active_crtc_count > 1) &&
90 (rdev->pm.power_state[ps].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)) {
91 DRM_ERROR("Invalid power state for multi-head: %d.%d\n", ps, cm);
92 } else {
93 /* disable dynpm */
94 rdev->pm.state = PM_STATE_DISABLED;
95 rdev->pm.planned_action = PM_ACTION_NONE;
96 rdev->pm.requested_power_state_index = ps;
97 rdev->pm.requested_clock_mode_index = cm;
98 radeon_pm_set_power_mode_static_locked(rdev);
99 }
100 } else
101 DRM_ERROR("Invalid power state: %d.%d\n\n", ps, cm);
102 mutex_unlock(&rdev->pm.mutex);
103
104 return count;
105}
106
107static ssize_t radeon_get_dynpm(struct device *dev,
108 struct device_attribute *attr,
109 char *buf)
110{
111 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
112 struct radeon_device *rdev = ddev->dev_private;
113
114 return snprintf(buf, PAGE_SIZE, "%s\n",
115 (rdev->pm.state == PM_STATE_DISABLED) ? "disabled" : "enabled");
116}
117
118static ssize_t radeon_set_dynpm(struct device *dev,
119 struct device_attribute *attr,
120 const char *buf,
121 size_t count)
122{
123 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
124 struct radeon_device *rdev = ddev->dev_private;
125 int tmp = simple_strtoul(buf, NULL, 10);
126
127 if (tmp == 0) {
128 /* update power mode info */
129 radeon_pm_compute_clocks(rdev);
130 /* disable dynpm */
131 mutex_lock(&rdev->pm.mutex);
132 rdev->pm.state = PM_STATE_DISABLED;
133 rdev->pm.planned_action = PM_ACTION_NONE;
134 mutex_unlock(&rdev->pm.mutex);
135 DRM_INFO("radeon: dynamic power management disabled\n");
136 } else if (tmp == 1) {
137 if (rdev->pm.num_power_states > 1) {
138 /* enable dynpm */
139 mutex_lock(&rdev->pm.mutex);
140 rdev->pm.state = PM_STATE_PAUSED;
141 rdev->pm.planned_action = PM_ACTION_DEFAULT;
142 radeon_get_power_state(rdev, rdev->pm.planned_action);
143 mutex_unlock(&rdev->pm.mutex);
144 /* update power mode info */
145 radeon_pm_compute_clocks(rdev);
146 DRM_INFO("radeon: dynamic power management enabled\n");
147 } else
148 DRM_ERROR("dynpm not valid on this system\n");
149 } else
150 DRM_ERROR("Invalid setting: %d\n", tmp);
151
152 return count;
153}
154
155static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, radeon_get_power_state_static, radeon_set_power_state_static);
156static DEVICE_ATTR(dynpm, S_IRUGO | S_IWUSR, radeon_get_dynpm, radeon_set_dynpm);
157
158
Rafał Miłeckic913e232009-12-22 23:02:16 +0100159static const char *pm_state_names[4] = {
160 "PM_STATE_DISABLED",
161 "PM_STATE_MINIMUM",
162 "PM_STATE_PAUSED",
163 "PM_STATE_ACTIVE"
164};
Rafał Miłecki74338742009-11-03 00:53:02 +0100165
Alex Deucher0ec0e742009-12-23 13:21:58 -0500166static const char *pm_state_types[5] = {
Alex Deucherd91eeb72010-04-05 15:26:43 -0400167 "",
Alex Deucher0ec0e742009-12-23 13:21:58 -0500168 "Powersave",
169 "Battery",
170 "Balanced",
171 "Performance",
172};
173
Alex Deucher56278a82009-12-28 13:58:44 -0500174static void radeon_print_power_mode_info(struct radeon_device *rdev)
175{
176 int i, j;
177 bool is_default;
178
179 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
180 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deuchera48b9b42010-04-22 14:03:55 -0400181 if (rdev->pm.default_power_state_index == i)
Alex Deucher56278a82009-12-28 13:58:44 -0500182 is_default = true;
183 else
184 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -0500185 DRM_INFO("State %d %s %s\n", i,
186 pm_state_types[rdev->pm.power_state[i].type],
187 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -0500188 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Alex Deucher79daedc2010-04-22 14:25:19 -0400189 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].pcie_lanes);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400190 if (rdev->pm.power_state[i].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)
191 DRM_INFO("\tSingle display only\n");
Alex Deucher56278a82009-12-28 13:58:44 -0500192 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
193 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
194 if (rdev->flags & RADEON_IS_IGP)
195 DRM_INFO("\t\t%d engine: %d\n",
196 j,
197 rdev->pm.power_state[i].clock_info[j].sclk * 10);
198 else
199 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
200 j,
201 rdev->pm.power_state[i].clock_info[j].sclk * 10,
202 rdev->pm.power_state[i].clock_info[j].mclk * 10);
203 }
204 }
205}
206
Alex Deucherbae6b5622010-04-22 13:38:05 -0400207void radeon_sync_with_vblank(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100208{
209 if (rdev->pm.active_crtcs) {
210 rdev->pm.vblank_sync = false;
211 wait_event_timeout(
212 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
213 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
214 }
215}
216
Rafał Miłecki74338742009-11-03 00:53:02 +0100217int radeon_pm_init(struct radeon_device *rdev)
218{
Rafał Miłeckic913e232009-12-22 23:02:16 +0100219 rdev->pm.state = PM_STATE_DISABLED;
220 rdev->pm.planned_action = PM_ACTION_NONE;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400221 rdev->pm.can_upclock = true;
222 rdev->pm.can_downclock = true;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100223
Alex Deucher56278a82009-12-28 13:58:44 -0500224 if (rdev->bios) {
225 if (rdev->is_atom_bios)
226 radeon_atombios_get_power_modes(rdev);
227 else
228 radeon_combios_get_power_modes(rdev);
229 radeon_print_power_mode_info(rdev);
230 }
231
Rafał Miłecki74338742009-11-03 00:53:02 +0100232 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100233 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100234 }
235
Alex Deuchera4248162010-04-24 14:50:23 -0400236 /* where's the best place to put this? */
237 device_create_file(rdev->dev, &dev_attr_power_state);
238 device_create_file(rdev->dev, &dev_attr_dynpm);
239
Rafał Miłeckic913e232009-12-22 23:02:16 +0100240 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
241
Alex Deucher90c39052010-03-24 11:32:29 -0400242 if ((radeon_dynpm != -1 && radeon_dynpm) && (rdev->pm.num_power_states > 1)) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100243 rdev->pm.state = PM_STATE_PAUSED;
244 DRM_INFO("radeon: dynamic power management enabled\n");
245 }
246
247 DRM_INFO("radeon: power management initialized\n");
248
Rafał Miłecki74338742009-11-03 00:53:02 +0100249 return 0;
250}
251
Alex Deucher29fb52c2010-03-11 10:01:17 -0500252void radeon_pm_fini(struct radeon_device *rdev)
253{
Alex Deucher58e21df2010-03-22 13:31:08 -0400254 if (rdev->pm.state != PM_STATE_DISABLED) {
255 /* cancel work */
256 cancel_delayed_work_sync(&rdev->pm.idle_work);
257 /* reset default clocks */
258 rdev->pm.state = PM_STATE_DISABLED;
259 rdev->pm.planned_action = PM_ACTION_DEFAULT;
260 radeon_pm_set_clocks(rdev);
Alex Deuchera4248162010-04-24 14:50:23 -0400261 } else if ((rdev->pm.current_power_state_index !=
262 rdev->pm.default_power_state_index) ||
263 (rdev->pm.current_clock_mode_index != 0)) {
264 rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
265 rdev->pm.requested_clock_mode_index = 0;
266 mutex_lock(&rdev->pm.mutex);
267 radeon_pm_set_power_mode_static_locked(rdev);
268 mutex_unlock(&rdev->pm.mutex);
Alex Deucher58e21df2010-03-22 13:31:08 -0400269 }
270
Alex Deuchera4248162010-04-24 14:50:23 -0400271 device_remove_file(rdev->dev, &dev_attr_power_state);
272 device_remove_file(rdev->dev, &dev_attr_dynpm);
273
Alex Deucher29fb52c2010-03-11 10:01:17 -0500274 if (rdev->pm.i2c_bus)
275 radeon_i2c_destroy(rdev->pm.i2c_bus);
276}
277
Rafał Miłeckic913e232009-12-22 23:02:16 +0100278void radeon_pm_compute_clocks(struct radeon_device *rdev)
279{
280 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400281 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100282 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100283
284 if (rdev->pm.state == PM_STATE_DISABLED)
285 return;
286
287 mutex_lock(&rdev->pm.mutex);
288
289 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400290 rdev->pm.active_crtc_count = 0;
291 list_for_each_entry(crtc,
292 &ddev->mode_config.crtc_list, head) {
293 radeon_crtc = to_radeon_crtc(crtc);
294 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100295 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400296 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100297 }
298 }
299
Alex Deuchera48b9b42010-04-22 14:03:55 -0400300 if (rdev->pm.active_crtc_count > 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100301 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100302 cancel_delayed_work(&rdev->pm.idle_work);
303
304 rdev->pm.state = PM_STATE_PAUSED;
305 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400306 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100307
308 DRM_DEBUG("radeon: dynamic power management deactivated\n");
Rafał Miłeckic913e232009-12-22 23:02:16 +0100309 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400310 } else if (rdev->pm.active_crtc_count == 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100311 /* TODO: Increase clocks if needed for current mode */
312
313 if (rdev->pm.state == PM_STATE_MINIMUM) {
314 rdev->pm.state = PM_STATE_ACTIVE;
315 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100316 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100317
318 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
319 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deuchera48b9b42010-04-22 14:03:55 -0400320 } else if (rdev->pm.state == PM_STATE_PAUSED) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100321 rdev->pm.state = PM_STATE_ACTIVE;
322 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
323 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
324 DRM_DEBUG("radeon: dynamic power management activated\n");
325 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400326 } else { /* count == 0 */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100327 if (rdev->pm.state != PM_STATE_MINIMUM) {
328 cancel_delayed_work(&rdev->pm.idle_work);
329
330 rdev->pm.state = PM_STATE_MINIMUM;
331 rdev->pm.planned_action = PM_ACTION_MINIMUM;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100332 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100333 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100334 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100335
336 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100337}
338
Alex Deucherbae6b5622010-04-22 13:38:05 -0400339bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Dave Airlief7352612010-02-18 15:58:36 +1000340{
Alex Deucherbae6b5622010-04-22 13:38:05 -0400341 u32 stat_crtc = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000342 bool in_vbl = true;
343
Alex Deucherbae6b5622010-04-22 13:38:05 -0400344 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000345 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucherbae6b5622010-04-22 13:38:05 -0400346 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET);
347 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000348 in_vbl = false;
349 }
350 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucherbae6b5622010-04-22 13:38:05 -0400351 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET);
352 if (!(stat_crtc & 1))
353 in_vbl = false;
354 }
355 if (rdev->pm.active_crtcs & (1 << 2)) {
356 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET);
357 if (!(stat_crtc & 1))
358 in_vbl = false;
359 }
360 if (rdev->pm.active_crtcs & (1 << 3)) {
361 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET);
362 if (!(stat_crtc & 1))
363 in_vbl = false;
364 }
365 if (rdev->pm.active_crtcs & (1 << 4)) {
366 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET);
367 if (!(stat_crtc & 1))
368 in_vbl = false;
369 }
370 if (rdev->pm.active_crtcs & (1 << 5)) {
371 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET);
372 if (!(stat_crtc & 1))
373 in_vbl = false;
374 }
375 } else if (ASIC_IS_AVIVO(rdev)) {
376 if (rdev->pm.active_crtcs & (1 << 0)) {
377 stat_crtc = RREG32(D1CRTC_STATUS);
378 if (!(stat_crtc & 1))
379 in_vbl = false;
380 }
381 if (rdev->pm.active_crtcs & (1 << 1)) {
382 stat_crtc = RREG32(D2CRTC_STATUS);
383 if (!(stat_crtc & 1))
384 in_vbl = false;
385 }
386 } else {
387 if (rdev->pm.active_crtcs & (1 << 0)) {
388 stat_crtc = RREG32(RADEON_CRTC_STATUS);
389 if (!(stat_crtc & 1))
390 in_vbl = false;
391 }
392 if (rdev->pm.active_crtcs & (1 << 1)) {
393 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
394 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000395 in_vbl = false;
396 }
397 }
398 if (in_vbl == false)
Alex Deucherbae6b5622010-04-22 13:38:05 -0400399 DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc,
400 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000401 return in_vbl;
402}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100403static void radeon_pm_set_clocks_locked(struct radeon_device *rdev)
404{
405 /*radeon_fence_wait_last(rdev);*/
Dave Airlief7352612010-02-18 15:58:36 +1000406
Alex Deuchera4248162010-04-24 14:50:23 -0400407 radeon_set_power_state(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100408 rdev->pm.planned_action = PM_ACTION_NONE;
409}
410
411static void radeon_pm_set_clocks(struct radeon_device *rdev)
412{
Alex Deucher8a56df62010-03-15 17:09:05 -0400413 int i;
414
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100415 radeon_get_power_state(rdev, rdev->pm.planned_action);
416 mutex_lock(&rdev->cp.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100417
Alex Deucheref6e6cf2010-03-17 14:29:15 -0400418 /* wait for GPU idle */
419 rdev->pm.gui_idle = false;
420 rdev->irq.gui_idle = true;
421 radeon_irq_set(rdev);
422 wait_event_interruptible_timeout(
423 rdev->irq.idle_queue, rdev->pm.gui_idle,
424 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
425 rdev->irq.gui_idle = false;
426 radeon_irq_set(rdev);
427
Alex Deucher8a56df62010-03-15 17:09:05 -0400428 for (i = 0; i < rdev->num_crtc; i++) {
429 if (rdev->pm.active_crtcs & (1 << i)) {
430 rdev->pm.req_vblank |= (1 << i);
431 drm_vblank_get(rdev->ddev, i);
432 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100433 }
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100434 radeon_pm_set_clocks_locked(rdev);
Alex Deucher8a56df62010-03-15 17:09:05 -0400435 for (i = 0; i < rdev->num_crtc; i++) {
436 if (rdev->pm.req_vblank & (1 << i)) {
437 rdev->pm.req_vblank &= ~(1 << i);
438 drm_vblank_put(rdev->ddev, i);
439 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100440 }
441
Alex Deucherc00f53b2010-03-22 13:34:22 -0400442 /* update display watermarks based on new power state */
443 radeon_update_bandwidth_info(rdev);
444 if (rdev->pm.active_crtc_count)
445 radeon_bandwidth_update(rdev);
446
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100447 mutex_unlock(&rdev->cp.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100448}
449
450static void radeon_pm_idle_work_handler(struct work_struct *work)
451{
452 struct radeon_device *rdev;
453 rdev = container_of(work, struct radeon_device,
454 pm.idle_work.work);
455
456 mutex_lock(&rdev->pm.mutex);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100457 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100458 unsigned long irq_flags;
459 int not_processed = 0;
460
461 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
462 if (!list_empty(&rdev->fence_drv.emited)) {
463 struct list_head *ptr;
464 list_for_each(ptr, &rdev->fence_drv.emited) {
465 /* count up to 3, that's enought info */
466 if (++not_processed >= 3)
467 break;
468 }
469 }
470 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
471
472 if (not_processed >= 3) { /* should upclock */
473 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
474 rdev->pm.planned_action = PM_ACTION_NONE;
475 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400476 rdev->pm.can_upclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100477 rdev->pm.planned_action =
478 PM_ACTION_UPCLOCK;
479 rdev->pm.action_timeout = jiffies +
480 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
481 }
482 } else if (not_processed == 0) { /* should downclock */
483 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
484 rdev->pm.planned_action = PM_ACTION_NONE;
485 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400486 rdev->pm.can_downclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100487 rdev->pm.planned_action =
488 PM_ACTION_DOWNCLOCK;
489 rdev->pm.action_timeout = jiffies +
490 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
491 }
492 }
493
494 if (rdev->pm.planned_action != PM_ACTION_NONE &&
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100495 jiffies > rdev->pm.action_timeout) {
496 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100497 }
498 }
499 mutex_unlock(&rdev->pm.mutex);
500
501 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
502 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
503}
504
Rafał Miłecki74338742009-11-03 00:53:02 +0100505/*
506 * Debugfs info
507 */
508#if defined(CONFIG_DEBUG_FS)
509
510static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
511{
512 struct drm_info_node *node = (struct drm_info_node *) m->private;
513 struct drm_device *dev = node->minor->dev;
514 struct radeon_device *rdev = dev->dev_private;
515
Rafał Miłeckic913e232009-12-22 23:02:16 +0100516 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100517 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
518 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
519 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
520 if (rdev->asic->get_memory_clock)
521 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000522 if (rdev->asic->get_pcie_lanes)
523 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100524
525 return 0;
526}
527
528static struct drm_info_list radeon_pm_info_list[] = {
529 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
530};
531#endif
532
Rafał Miłeckic913e232009-12-22 23:02:16 +0100533static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100534{
535#if defined(CONFIG_DEBUG_FS)
536 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
537#else
538 return 0;
539#endif
540}