blob: f500c8d200e7fa19866b67fd24762a8ca5d1b43c [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"
25
Rafał Miłeckic913e232009-12-22 23:02:16 +010026#define RADEON_IDLE_LOOP_MS 100
27#define RADEON_RECLOCK_DELAY_MS 200
28
Rafał Miłeckic913e232009-12-22 23:02:16 +010029static void radeon_pm_set_clocks_locked(struct radeon_device *rdev);
30static void radeon_pm_set_clocks(struct radeon_device *rdev);
31static void radeon_pm_reclock_work_handler(struct work_struct *work);
32static void radeon_pm_idle_work_handler(struct work_struct *work);
33static int radeon_debugfs_pm_init(struct radeon_device *rdev);
34
35static const char *pm_state_names[4] = {
36 "PM_STATE_DISABLED",
37 "PM_STATE_MINIMUM",
38 "PM_STATE_PAUSED",
39 "PM_STATE_ACTIVE"
40};
Rafał Miłecki74338742009-11-03 00:53:02 +010041
Alex Deucher0ec0e742009-12-23 13:21:58 -050042static const char *pm_state_types[5] = {
43 "Default",
44 "Powersave",
45 "Battery",
46 "Balanced",
47 "Performance",
48};
49
Alex Deucher56278a82009-12-28 13:58:44 -050050static void radeon_print_power_mode_info(struct radeon_device *rdev)
51{
52 int i, j;
53 bool is_default;
54
55 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
56 for (i = 0; i < rdev->pm.num_power_states; i++) {
57 if (rdev->pm.default_power_state == &rdev->pm.power_state[i])
58 is_default = true;
59 else
60 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -050061 DRM_INFO("State %d %s %s\n", i,
62 pm_state_types[rdev->pm.power_state[i].type],
63 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -050064 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
65 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].non_clock_info.pcie_lanes);
66 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
67 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
68 if (rdev->flags & RADEON_IS_IGP)
69 DRM_INFO("\t\t%d engine: %d\n",
70 j,
71 rdev->pm.power_state[i].clock_info[j].sclk * 10);
72 else
73 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
74 j,
75 rdev->pm.power_state[i].clock_info[j].sclk * 10,
76 rdev->pm.power_state[i].clock_info[j].mclk * 10);
77 }
78 }
79}
80
Alex Deucher516d0e42009-12-23 14:28:05 -050081static struct radeon_power_state * radeon_pick_power_state(struct radeon_device *rdev,
82 enum radeon_pm_state_type type)
83{
84 int i;
85 struct radeon_power_state *power_state = NULL;
86
87 switch (type) {
88 case POWER_STATE_TYPE_DEFAULT:
89 default:
90 return rdev->pm.default_power_state;
91 case POWER_STATE_TYPE_POWERSAVE:
92 for (i = 0; i < rdev->pm.num_power_states; i++) {
93 if (rdev->pm.power_state[i].type == POWER_STATE_TYPE_POWERSAVE) {
94 power_state = &rdev->pm.power_state[i];
95 break;
96 }
97 }
98 if (power_state == NULL) {
99 for (i = 0; i < rdev->pm.num_power_states; i++) {
100 if (rdev->pm.power_state[i].type == POWER_STATE_TYPE_BATTERY) {
101 power_state = &rdev->pm.power_state[i];
102 break;
103 }
104 }
105 }
106 break;
107 case POWER_STATE_TYPE_BATTERY:
108 for (i = 0; i < rdev->pm.num_power_states; i++) {
109 if (rdev->pm.power_state[i].type == POWER_STATE_TYPE_BATTERY) {
110 power_state = &rdev->pm.power_state[i];
111 break;
112 }
113 }
114 if (power_state == NULL) {
115 for (i = 0; i < rdev->pm.num_power_states; i++) {
116 if (rdev->pm.power_state[i].type == POWER_STATE_TYPE_POWERSAVE) {
117 power_state = &rdev->pm.power_state[i];
118 break;
119 }
120 }
121 }
122 break;
123 case POWER_STATE_TYPE_BALANCED:
124 case POWER_STATE_TYPE_PERFORMANCE:
125 for (i = 0; i < rdev->pm.num_power_states; i++) {
126 if (rdev->pm.power_state[i].type == type) {
127 power_state = &rdev->pm.power_state[i];
128 break;
129 }
130 }
131 break;
132 }
133
134 if (power_state == NULL)
135 return rdev->pm.default_power_state;
136
137 return power_state;
138}
139
140static struct radeon_pm_clock_info * radeon_pick_clock_mode(struct radeon_device *rdev,
141 struct radeon_power_state *power_state,
142 enum radeon_pm_clock_mode_type type)
143{
144 switch (type) {
145 case POWER_MODE_TYPE_DEFAULT:
146 default:
147 return power_state->default_clock_mode;
148 case POWER_MODE_TYPE_LOW:
149 return &power_state->clock_info[0];
150 case POWER_MODE_TYPE_MID:
151 if (power_state->num_clock_modes > 2)
152 return &power_state->clock_info[1];
153 else
154 return &power_state->clock_info[0];
155 break;
156 case POWER_MODE_TYPE_HIGH:
157 return &power_state->clock_info[power_state->num_clock_modes - 1];
158 }
159
160}
161
162static void radeon_get_power_state(struct radeon_device *rdev,
163 enum radeon_pm_action action)
164{
165 switch (action) {
166 case PM_ACTION_NONE:
167 default:
168 rdev->pm.requested_power_state = rdev->pm.current_power_state;
169 rdev->pm.requested_power_state->requested_clock_mode =
170 rdev->pm.requested_power_state->current_clock_mode;
171 break;
172 case PM_ACTION_MINIMUM:
173 rdev->pm.requested_power_state = radeon_pick_power_state(rdev, POWER_STATE_TYPE_BATTERY);
174 rdev->pm.requested_power_state->requested_clock_mode =
175 radeon_pick_clock_mode(rdev, rdev->pm.requested_power_state, POWER_MODE_TYPE_LOW);
176 break;
177 case PM_ACTION_DOWNCLOCK:
178 rdev->pm.requested_power_state = radeon_pick_power_state(rdev, POWER_STATE_TYPE_POWERSAVE);
179 rdev->pm.requested_power_state->requested_clock_mode =
180 radeon_pick_clock_mode(rdev, rdev->pm.requested_power_state, POWER_MODE_TYPE_MID);
181 break;
182 case PM_ACTION_UPCLOCK:
183 rdev->pm.requested_power_state = radeon_pick_power_state(rdev, POWER_STATE_TYPE_DEFAULT);
184 rdev->pm.requested_power_state->requested_clock_mode =
185 radeon_pick_clock_mode(rdev, rdev->pm.requested_power_state, POWER_MODE_TYPE_HIGH);
186 break;
187 }
Alex Deucher530079a2009-12-23 14:39:36 -0500188 DRM_INFO("Requested: e: %d m: %d p: %d\n",
189 rdev->pm.requested_power_state->requested_clock_mode->sclk,
190 rdev->pm.requested_power_state->requested_clock_mode->mclk,
191 rdev->pm.requested_power_state->non_clock_info.pcie_lanes);
Alex Deucher516d0e42009-12-23 14:28:05 -0500192}
193
194static void radeon_set_power_state(struct radeon_device *rdev)
195{
196 if (rdev->pm.requested_power_state == rdev->pm.current_power_state)
197 return;
Alex Deucher530079a2009-12-23 14:39:36 -0500198
199 DRM_INFO("Setting: e: %d m: %d p: %d\n",
200 rdev->pm.requested_power_state->requested_clock_mode->sclk,
201 rdev->pm.requested_power_state->requested_clock_mode->mclk,
202 rdev->pm.requested_power_state->non_clock_info.pcie_lanes);
Alex Deucher516d0e42009-12-23 14:28:05 -0500203 /* set pcie lanes */
204 /* set voltage */
205 /* set engine clock */
206 radeon_set_engine_clock(rdev, rdev->pm.requested_power_state->requested_clock_mode->sclk);
207 /* set memory clock */
208
209 rdev->pm.current_power_state = rdev->pm.requested_power_state;
210}
211
Rafał Miłecki74338742009-11-03 00:53:02 +0100212int radeon_pm_init(struct radeon_device *rdev)
213{
Rafał Miłeckic913e232009-12-22 23:02:16 +0100214 rdev->pm.state = PM_STATE_DISABLED;
215 rdev->pm.planned_action = PM_ACTION_NONE;
216 rdev->pm.downclocked = false;
217 rdev->pm.vblank_callback = false;
218
Alex Deucher56278a82009-12-28 13:58:44 -0500219 if (rdev->bios) {
220 if (rdev->is_atom_bios)
221 radeon_atombios_get_power_modes(rdev);
222 else
223 radeon_combios_get_power_modes(rdev);
224 radeon_print_power_mode_info(rdev);
225 }
226
Rafał Miłecki74338742009-11-03 00:53:02 +0100227 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100228 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100229 }
230
Rafał Miłeckic913e232009-12-22 23:02:16 +0100231 INIT_WORK(&rdev->pm.reclock_work, radeon_pm_reclock_work_handler);
232 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
233
234 if (radeon_dynpm != -1 && radeon_dynpm) {
235 rdev->pm.state = PM_STATE_PAUSED;
236 DRM_INFO("radeon: dynamic power management enabled\n");
237 }
238
239 DRM_INFO("radeon: power management initialized\n");
240
Rafał Miłecki74338742009-11-03 00:53:02 +0100241 return 0;
242}
243
Rafał Miłeckic913e232009-12-22 23:02:16 +0100244void radeon_pm_compute_clocks(struct radeon_device *rdev)
245{
246 struct drm_device *ddev = rdev->ddev;
247 struct drm_connector *connector;
248 struct radeon_crtc *radeon_crtc;
249 int count = 0;
250
251 if (rdev->pm.state == PM_STATE_DISABLED)
252 return;
253
254 mutex_lock(&rdev->pm.mutex);
255
256 rdev->pm.active_crtcs = 0;
257 list_for_each_entry(connector,
258 &ddev->mode_config.connector_list, head) {
259 if (connector->encoder &&
260 connector->dpms != DRM_MODE_DPMS_OFF) {
261 radeon_crtc = to_radeon_crtc(connector->encoder->crtc);
262 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
263 ++count;
264 }
265 }
266
267 if (count > 1) {
268 if (rdev->pm.state == PM_STATE_ACTIVE) {
269 wait_queue_head_t wait;
270 init_waitqueue_head(&wait);
271
272 cancel_delayed_work(&rdev->pm.idle_work);
273
274 rdev->pm.state = PM_STATE_PAUSED;
275 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
276 rdev->pm.vblank_callback = true;
277
278 mutex_unlock(&rdev->pm.mutex);
279
280 wait_event_timeout(wait, !rdev->pm.downclocked,
281 msecs_to_jiffies(300));
282 if (!rdev->pm.downclocked)
283 radeon_pm_set_clocks(rdev);
284
285 DRM_DEBUG("radeon: dynamic power management deactivated\n");
286 } else {
287 mutex_unlock(&rdev->pm.mutex);
288 }
289 } else if (count == 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100290 /* TODO: Increase clocks if needed for current mode */
291
292 if (rdev->pm.state == PM_STATE_MINIMUM) {
293 rdev->pm.state = PM_STATE_ACTIVE;
294 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
295 radeon_pm_set_clocks_locked(rdev);
296
297 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
298 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
299 }
300 else if (rdev->pm.state == PM_STATE_PAUSED) {
301 rdev->pm.state = PM_STATE_ACTIVE;
302 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
303 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
304 DRM_DEBUG("radeon: dynamic power management activated\n");
305 }
306
307 mutex_unlock(&rdev->pm.mutex);
308 }
309 else { /* count == 0 */
310 if (rdev->pm.state != PM_STATE_MINIMUM) {
311 cancel_delayed_work(&rdev->pm.idle_work);
312
313 rdev->pm.state = PM_STATE_MINIMUM;
314 rdev->pm.planned_action = PM_ACTION_MINIMUM;
315 radeon_pm_set_clocks_locked(rdev);
316 }
317
318 mutex_unlock(&rdev->pm.mutex);
319 }
320}
321
322static void radeon_pm_set_clocks_locked(struct radeon_device *rdev)
323{
324 /*radeon_fence_wait_last(rdev);*/
325 switch (rdev->pm.planned_action) {
326 case PM_ACTION_UPCLOCK:
Alex Deucher530079a2009-12-23 14:39:36 -0500327 radeon_get_power_state(rdev, PM_ACTION_UPCLOCK);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100328 rdev->pm.downclocked = false;
329 break;
330 case PM_ACTION_DOWNCLOCK:
Alex Deucher530079a2009-12-23 14:39:36 -0500331 radeon_get_power_state(rdev, PM_ACTION_DOWNCLOCK);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100332 rdev->pm.downclocked = true;
333 break;
334 case PM_ACTION_MINIMUM:
Alex Deucher530079a2009-12-23 14:39:36 -0500335 radeon_get_power_state(rdev, PM_ACTION_MINIMUM);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100336 break;
337 case PM_ACTION_NONE:
Alex Deucher530079a2009-12-23 14:39:36 -0500338 radeon_get_power_state(rdev, PM_ACTION_NONE);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100339 DRM_ERROR("%s: PM_ACTION_NONE\n", __func__);
340 break;
341 }
Alex Deucher530079a2009-12-23 14:39:36 -0500342 radeon_set_power_state(rdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100343 rdev->pm.planned_action = PM_ACTION_NONE;
344}
345
346static void radeon_pm_set_clocks(struct radeon_device *rdev)
347{
348 mutex_lock(&rdev->pm.mutex);
349 /* new VBLANK irq may come before handling previous one */
350 if (rdev->pm.vblank_callback) {
351 mutex_lock(&rdev->cp.mutex);
352 if (rdev->pm.req_vblank & (1 << 0)) {
353 rdev->pm.req_vblank &= ~(1 << 0);
354 drm_vblank_put(rdev->ddev, 0);
355 }
356 if (rdev->pm.req_vblank & (1 << 1)) {
357 rdev->pm.req_vblank &= ~(1 << 1);
358 drm_vblank_put(rdev->ddev, 1);
359 }
360 rdev->pm.vblank_callback = false;
361 radeon_pm_set_clocks_locked(rdev);
362 mutex_unlock(&rdev->cp.mutex);
363 }
364 mutex_unlock(&rdev->pm.mutex);
365}
366
367static void radeon_pm_reclock_work_handler(struct work_struct *work)
368{
369 struct radeon_device *rdev;
370 rdev = container_of(work, struct radeon_device,
371 pm.reclock_work);
372 radeon_pm_set_clocks(rdev);
373}
374
375static void radeon_pm_idle_work_handler(struct work_struct *work)
376{
377 struct radeon_device *rdev;
378 rdev = container_of(work, struct radeon_device,
379 pm.idle_work.work);
380
381 mutex_lock(&rdev->pm.mutex);
382 if (rdev->pm.state == PM_STATE_ACTIVE &&
383 !rdev->pm.vblank_callback) {
384 unsigned long irq_flags;
385 int not_processed = 0;
386
387 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
388 if (!list_empty(&rdev->fence_drv.emited)) {
389 struct list_head *ptr;
390 list_for_each(ptr, &rdev->fence_drv.emited) {
391 /* count up to 3, that's enought info */
392 if (++not_processed >= 3)
393 break;
394 }
395 }
396 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
397
398 if (not_processed >= 3) { /* should upclock */
399 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
400 rdev->pm.planned_action = PM_ACTION_NONE;
401 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
402 rdev->pm.downclocked) {
403 rdev->pm.planned_action =
404 PM_ACTION_UPCLOCK;
405 rdev->pm.action_timeout = jiffies +
406 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
407 }
408 } else if (not_processed == 0) { /* should downclock */
409 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
410 rdev->pm.planned_action = PM_ACTION_NONE;
411 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
412 !rdev->pm.downclocked) {
413 rdev->pm.planned_action =
414 PM_ACTION_DOWNCLOCK;
415 rdev->pm.action_timeout = jiffies +
416 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
417 }
418 }
419
420 if (rdev->pm.planned_action != PM_ACTION_NONE &&
421 jiffies > rdev->pm.action_timeout) {
422 if (rdev->pm.active_crtcs & (1 << 0)) {
423 rdev->pm.req_vblank |= (1 << 0);
424 drm_vblank_get(rdev->ddev, 0);
425 }
426 if (rdev->pm.active_crtcs & (1 << 1)) {
427 rdev->pm.req_vblank |= (1 << 1);
428 drm_vblank_get(rdev->ddev, 1);
429 }
430 rdev->pm.vblank_callback = true;
431 }
432 }
433 mutex_unlock(&rdev->pm.mutex);
434
435 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
436 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
437}
438
Rafał Miłecki74338742009-11-03 00:53:02 +0100439/*
440 * Debugfs info
441 */
442#if defined(CONFIG_DEBUG_FS)
443
444static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
445{
446 struct drm_info_node *node = (struct drm_info_node *) m->private;
447 struct drm_device *dev = node->minor->dev;
448 struct radeon_device *rdev = dev->dev_private;
449
Rafał Miłeckic913e232009-12-22 23:02:16 +0100450 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100451 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
452 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
453 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
454 if (rdev->asic->get_memory_clock)
455 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100456
457 return 0;
458}
459
460static struct drm_info_list radeon_pm_info_list[] = {
461 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
462};
463#endif
464
Rafał Miłeckic913e232009-12-22 23:02:16 +0100465static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100466{
467#if defined(CONFIG_DEBUG_FS)
468 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
469#else
470 return 0;
471#endif
472}