blob: 10ef8924799d6a2c7f638efe5319b360d1485058 [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
37static const char *pm_state_names[4] = {
38 "PM_STATE_DISABLED",
39 "PM_STATE_MINIMUM",
40 "PM_STATE_PAUSED",
41 "PM_STATE_ACTIVE"
42};
Rafał Miłecki74338742009-11-03 00:53:02 +010043
Alex Deucher0ec0e742009-12-23 13:21:58 -050044static const char *pm_state_types[5] = {
45 "Default",
46 "Powersave",
47 "Battery",
48 "Balanced",
49 "Performance",
50};
51
Alex Deucher56278a82009-12-28 13:58:44 -050052static void radeon_print_power_mode_info(struct radeon_device *rdev)
53{
54 int i, j;
55 bool is_default;
56
57 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
58 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deuchera48b9b42010-04-22 14:03:55 -040059 if (rdev->pm.default_power_state_index == i)
Alex Deucher56278a82009-12-28 13:58:44 -050060 is_default = true;
61 else
62 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -050063 DRM_INFO("State %d %s %s\n", i,
64 pm_state_types[rdev->pm.power_state[i].type],
65 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -050066 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
67 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].non_clock_info.pcie_lanes);
Alex Deuchera48b9b42010-04-22 14:03:55 -040068 if (rdev->pm.power_state[i].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)
69 DRM_INFO("\tSingle display only\n");
Alex Deucher56278a82009-12-28 13:58:44 -050070 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
71 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
72 if (rdev->flags & RADEON_IS_IGP)
73 DRM_INFO("\t\t%d engine: %d\n",
74 j,
75 rdev->pm.power_state[i].clock_info[j].sclk * 10);
76 else
77 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
78 j,
79 rdev->pm.power_state[i].clock_info[j].sclk * 10,
80 rdev->pm.power_state[i].clock_info[j].mclk * 10);
81 }
82 }
83}
84
Alex Deucherbae6b562010-04-22 13:38:05 -040085void radeon_sync_with_vblank(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +010086{
87 if (rdev->pm.active_crtcs) {
88 rdev->pm.vblank_sync = false;
89 wait_event_timeout(
90 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
91 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
92 }
93}
94
Rafał Miłecki74338742009-11-03 00:53:02 +010095int radeon_pm_init(struct radeon_device *rdev)
96{
Rafał Miłeckic913e232009-12-22 23:02:16 +010097 rdev->pm.state = PM_STATE_DISABLED;
98 rdev->pm.planned_action = PM_ACTION_NONE;
Alex Deuchera48b9b42010-04-22 14:03:55 -040099 rdev->pm.can_upclock = true;
100 rdev->pm.can_downclock = true;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100101
Alex Deucher56278a82009-12-28 13:58:44 -0500102 if (rdev->bios) {
103 if (rdev->is_atom_bios)
104 radeon_atombios_get_power_modes(rdev);
105 else
106 radeon_combios_get_power_modes(rdev);
107 radeon_print_power_mode_info(rdev);
108 }
109
Rafał Miłecki74338742009-11-03 00:53:02 +0100110 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100111 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100112 }
113
Rafał Miłeckic913e232009-12-22 23:02:16 +0100114 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
115
116 if (radeon_dynpm != -1 && radeon_dynpm) {
117 rdev->pm.state = PM_STATE_PAUSED;
118 DRM_INFO("radeon: dynamic power management enabled\n");
119 }
120
121 DRM_INFO("radeon: power management initialized\n");
122
Rafał Miłecki74338742009-11-03 00:53:02 +0100123 return 0;
124}
125
Alex Deucher29fb52c2010-03-11 10:01:17 -0500126void radeon_pm_fini(struct radeon_device *rdev)
127{
128 if (rdev->pm.i2c_bus)
129 radeon_i2c_destroy(rdev->pm.i2c_bus);
130}
131
Rafał Miłeckic913e232009-12-22 23:02:16 +0100132void radeon_pm_compute_clocks(struct radeon_device *rdev)
133{
134 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400135 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100136 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100137
138 if (rdev->pm.state == PM_STATE_DISABLED)
139 return;
140
141 mutex_lock(&rdev->pm.mutex);
142
143 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400144 rdev->pm.active_crtc_count = 0;
145 list_for_each_entry(crtc,
146 &ddev->mode_config.crtc_list, head) {
147 radeon_crtc = to_radeon_crtc(crtc);
148 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100149 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400150 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100151 }
152 }
153
Alex Deuchera48b9b42010-04-22 14:03:55 -0400154 if (rdev->pm.active_crtc_count > 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100155 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100156 cancel_delayed_work(&rdev->pm.idle_work);
157
158 rdev->pm.state = PM_STATE_PAUSED;
159 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400160 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100161
162 DRM_DEBUG("radeon: dynamic power management deactivated\n");
Rafał Miłeckic913e232009-12-22 23:02:16 +0100163 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400164 } else if (rdev->pm.active_crtc_count == 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100165 /* TODO: Increase clocks if needed for current mode */
166
167 if (rdev->pm.state == PM_STATE_MINIMUM) {
168 rdev->pm.state = PM_STATE_ACTIVE;
169 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100170 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100171
172 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
173 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deuchera48b9b42010-04-22 14:03:55 -0400174 } else if (rdev->pm.state == PM_STATE_PAUSED) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100175 rdev->pm.state = PM_STATE_ACTIVE;
176 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
177 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
178 DRM_DEBUG("radeon: dynamic power management activated\n");
179 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400180 } else { /* count == 0 */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100181 if (rdev->pm.state != PM_STATE_MINIMUM) {
182 cancel_delayed_work(&rdev->pm.idle_work);
183
184 rdev->pm.state = PM_STATE_MINIMUM;
185 rdev->pm.planned_action = PM_ACTION_MINIMUM;
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100186 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100187 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100188 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100189
190 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100191}
192
Alex Deucherbae6b562010-04-22 13:38:05 -0400193bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
Dave Airlief7352612010-02-18 15:58:36 +1000194{
Alex Deucherbae6b562010-04-22 13:38:05 -0400195 u32 stat_crtc = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000196 bool in_vbl = true;
197
Alex Deucherbae6b562010-04-22 13:38:05 -0400198 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000199 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucherbae6b562010-04-22 13:38:05 -0400200 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC0_REGISTER_OFFSET);
201 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000202 in_vbl = false;
203 }
204 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucherbae6b562010-04-22 13:38:05 -0400205 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC1_REGISTER_OFFSET);
206 if (!(stat_crtc & 1))
207 in_vbl = false;
208 }
209 if (rdev->pm.active_crtcs & (1 << 2)) {
210 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC2_REGISTER_OFFSET);
211 if (!(stat_crtc & 1))
212 in_vbl = false;
213 }
214 if (rdev->pm.active_crtcs & (1 << 3)) {
215 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC3_REGISTER_OFFSET);
216 if (!(stat_crtc & 1))
217 in_vbl = false;
218 }
219 if (rdev->pm.active_crtcs & (1 << 4)) {
220 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC4_REGISTER_OFFSET);
221 if (!(stat_crtc & 1))
222 in_vbl = false;
223 }
224 if (rdev->pm.active_crtcs & (1 << 5)) {
225 stat_crtc = RREG32(EVERGREEN_CRTC_STATUS + EVERGREEN_CRTC5_REGISTER_OFFSET);
226 if (!(stat_crtc & 1))
227 in_vbl = false;
228 }
229 } else if (ASIC_IS_AVIVO(rdev)) {
230 if (rdev->pm.active_crtcs & (1 << 0)) {
231 stat_crtc = RREG32(D1CRTC_STATUS);
232 if (!(stat_crtc & 1))
233 in_vbl = false;
234 }
235 if (rdev->pm.active_crtcs & (1 << 1)) {
236 stat_crtc = RREG32(D2CRTC_STATUS);
237 if (!(stat_crtc & 1))
238 in_vbl = false;
239 }
240 } else {
241 if (rdev->pm.active_crtcs & (1 << 0)) {
242 stat_crtc = RREG32(RADEON_CRTC_STATUS);
243 if (!(stat_crtc & 1))
244 in_vbl = false;
245 }
246 if (rdev->pm.active_crtcs & (1 << 1)) {
247 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
248 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000249 in_vbl = false;
250 }
251 }
252 if (in_vbl == false)
Alex Deucherbae6b562010-04-22 13:38:05 -0400253 DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc,
254 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000255 return in_vbl;
256}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100257static void radeon_pm_set_clocks_locked(struct radeon_device *rdev)
258{
259 /*radeon_fence_wait_last(rdev);*/
Dave Airlief7352612010-02-18 15:58:36 +1000260
Alex Deucher530079a2009-12-23 14:39:36 -0500261 radeon_set_power_state(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100262 rdev->pm.planned_action = PM_ACTION_NONE;
263}
264
265static void radeon_pm_set_clocks(struct radeon_device *rdev)
266{
Alex Deucher8a56df62010-03-15 17:09:05 -0400267 int i;
268
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100269 radeon_get_power_state(rdev, rdev->pm.planned_action);
270 mutex_lock(&rdev->cp.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100271
Alex Deucheref6e6cf2010-03-17 14:29:15 -0400272 /* wait for GPU idle */
273 rdev->pm.gui_idle = false;
274 rdev->irq.gui_idle = true;
275 radeon_irq_set(rdev);
276 wait_event_interruptible_timeout(
277 rdev->irq.idle_queue, rdev->pm.gui_idle,
278 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
279 rdev->irq.gui_idle = false;
280 radeon_irq_set(rdev);
281
Alex Deucher8a56df62010-03-15 17:09:05 -0400282 for (i = 0; i < rdev->num_crtc; i++) {
283 if (rdev->pm.active_crtcs & (1 << i)) {
284 rdev->pm.req_vblank |= (1 << i);
285 drm_vblank_get(rdev->ddev, i);
286 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100287 }
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100288 radeon_pm_set_clocks_locked(rdev);
Alex Deucher8a56df62010-03-15 17:09:05 -0400289 for (i = 0; i < rdev->num_crtc; i++) {
290 if (rdev->pm.req_vblank & (1 << i)) {
291 rdev->pm.req_vblank &= ~(1 << i);
292 drm_vblank_put(rdev->ddev, i);
293 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100294 }
295
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100296 mutex_unlock(&rdev->cp.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100297}
298
299static void radeon_pm_idle_work_handler(struct work_struct *work)
300{
301 struct radeon_device *rdev;
302 rdev = container_of(work, struct radeon_device,
303 pm.idle_work.work);
304
305 mutex_lock(&rdev->pm.mutex);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100306 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100307 unsigned long irq_flags;
308 int not_processed = 0;
309
310 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
311 if (!list_empty(&rdev->fence_drv.emited)) {
312 struct list_head *ptr;
313 list_for_each(ptr, &rdev->fence_drv.emited) {
314 /* count up to 3, that's enought info */
315 if (++not_processed >= 3)
316 break;
317 }
318 }
319 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
320
321 if (not_processed >= 3) { /* should upclock */
322 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
323 rdev->pm.planned_action = PM_ACTION_NONE;
324 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400325 rdev->pm.can_upclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100326 rdev->pm.planned_action =
327 PM_ACTION_UPCLOCK;
328 rdev->pm.action_timeout = jiffies +
329 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
330 }
331 } else if (not_processed == 0) { /* should downclock */
332 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
333 rdev->pm.planned_action = PM_ACTION_NONE;
334 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400335 rdev->pm.can_downclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100336 rdev->pm.planned_action =
337 PM_ACTION_DOWNCLOCK;
338 rdev->pm.action_timeout = jiffies +
339 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
340 }
341 }
342
343 if (rdev->pm.planned_action != PM_ACTION_NONE &&
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100344 jiffies > rdev->pm.action_timeout) {
345 radeon_pm_set_clocks(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100346 }
347 }
348 mutex_unlock(&rdev->pm.mutex);
349
350 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
351 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
352}
353
Rafał Miłecki74338742009-11-03 00:53:02 +0100354/*
355 * Debugfs info
356 */
357#if defined(CONFIG_DEBUG_FS)
358
359static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
360{
361 struct drm_info_node *node = (struct drm_info_node *) m->private;
362 struct drm_device *dev = node->minor->dev;
363 struct radeon_device *rdev = dev->dev_private;
364
Rafał Miłeckic913e232009-12-22 23:02:16 +0100365 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100366 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
367 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
368 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
369 if (rdev->asic->get_memory_clock)
370 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000371 if (rdev->asic->get_pcie_lanes)
372 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100373
374 return 0;
375}
376
377static struct drm_info_list radeon_pm_info_list[] = {
378 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
379};
380#endif
381
Rafał Miłeckic913e232009-12-22 23:02:16 +0100382static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100383{
384#if defined(CONFIG_DEBUG_FS)
385 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
386#else
387 return 0;
388#endif
389}