blob: 93ba0fb27e9d4e6f939c0369dc6a793c3630d9f2 [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
29static void radeon_pm_check_limits(struct radeon_device *rdev);
30static void radeon_pm_set_clocks_locked(struct radeon_device *rdev);
31static void radeon_pm_set_clocks(struct radeon_device *rdev);
32static void radeon_pm_reclock_work_handler(struct work_struct *work);
33static void radeon_pm_idle_work_handler(struct work_struct *work);
34static int radeon_debugfs_pm_init(struct radeon_device *rdev);
35
36static const char *pm_state_names[4] = {
37 "PM_STATE_DISABLED",
38 "PM_STATE_MINIMUM",
39 "PM_STATE_PAUSED",
40 "PM_STATE_ACTIVE"
41};
Rafał Miłecki74338742009-11-03 00:53:02 +010042
Alex Deucher0ec0e742009-12-23 13:21:58 -050043static const char *pm_state_types[5] = {
44 "Default",
45 "Powersave",
46 "Battery",
47 "Balanced",
48 "Performance",
49};
50
Alex Deucher56278a82009-12-28 13:58:44 -050051static void radeon_print_power_mode_info(struct radeon_device *rdev)
52{
53 int i, j;
54 bool is_default;
55
56 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
57 for (i = 0; i < rdev->pm.num_power_states; i++) {
58 if (rdev->pm.default_power_state == &rdev->pm.power_state[i])
59 is_default = true;
60 else
61 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -050062 DRM_INFO("State %d %s %s\n", i,
63 pm_state_types[rdev->pm.power_state[i].type],
64 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -050065 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
66 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].non_clock_info.pcie_lanes);
67 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
68 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
69 if (rdev->flags & RADEON_IS_IGP)
70 DRM_INFO("\t\t%d engine: %d\n",
71 j,
72 rdev->pm.power_state[i].clock_info[j].sclk * 10);
73 else
74 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
75 j,
76 rdev->pm.power_state[i].clock_info[j].sclk * 10,
77 rdev->pm.power_state[i].clock_info[j].mclk * 10);
78 }
79 }
80}
81
Rafał Miłecki74338742009-11-03 00:53:02 +010082int radeon_pm_init(struct radeon_device *rdev)
83{
Rafał Miłeckic913e232009-12-22 23:02:16 +010084 rdev->pm.state = PM_STATE_DISABLED;
85 rdev->pm.planned_action = PM_ACTION_NONE;
86 rdev->pm.downclocked = false;
87 rdev->pm.vblank_callback = false;
88
Alex Deucher56278a82009-12-28 13:58:44 -050089 if (rdev->bios) {
90 if (rdev->is_atom_bios)
91 radeon_atombios_get_power_modes(rdev);
92 else
93 radeon_combios_get_power_modes(rdev);
94 radeon_print_power_mode_info(rdev);
95 }
96
Rafał Miłeckic913e232009-12-22 23:02:16 +010097 radeon_pm_check_limits(rdev);
98
Rafał Miłecki74338742009-11-03 00:53:02 +010099 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100100 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100101 }
102
Rafał Miłeckic913e232009-12-22 23:02:16 +0100103 INIT_WORK(&rdev->pm.reclock_work, radeon_pm_reclock_work_handler);
104 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
105
106 if (radeon_dynpm != -1 && radeon_dynpm) {
107 rdev->pm.state = PM_STATE_PAUSED;
108 DRM_INFO("radeon: dynamic power management enabled\n");
109 }
110
111 DRM_INFO("radeon: power management initialized\n");
112
Rafał Miłecki74338742009-11-03 00:53:02 +0100113 return 0;
114}
115
Rafał Miłeckic913e232009-12-22 23:02:16 +0100116static void radeon_pm_check_limits(struct radeon_device *rdev)
117{
118 rdev->pm.min_gpu_engine_clock = rdev->clock.default_sclk - 5000;
119 rdev->pm.min_gpu_memory_clock = rdev->clock.default_mclk - 5000;
120}
121
122void radeon_pm_compute_clocks(struct radeon_device *rdev)
123{
124 struct drm_device *ddev = rdev->ddev;
125 struct drm_connector *connector;
126 struct radeon_crtc *radeon_crtc;
127 int count = 0;
128
129 if (rdev->pm.state == PM_STATE_DISABLED)
130 return;
131
132 mutex_lock(&rdev->pm.mutex);
133
134 rdev->pm.active_crtcs = 0;
135 list_for_each_entry(connector,
136 &ddev->mode_config.connector_list, head) {
137 if (connector->encoder &&
138 connector->dpms != DRM_MODE_DPMS_OFF) {
139 radeon_crtc = to_radeon_crtc(connector->encoder->crtc);
140 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
141 ++count;
142 }
143 }
144
145 if (count > 1) {
146 if (rdev->pm.state == PM_STATE_ACTIVE) {
147 wait_queue_head_t wait;
148 init_waitqueue_head(&wait);
149
150 cancel_delayed_work(&rdev->pm.idle_work);
151
152 rdev->pm.state = PM_STATE_PAUSED;
153 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
154 rdev->pm.vblank_callback = true;
155
156 mutex_unlock(&rdev->pm.mutex);
157
158 wait_event_timeout(wait, !rdev->pm.downclocked,
159 msecs_to_jiffies(300));
160 if (!rdev->pm.downclocked)
161 radeon_pm_set_clocks(rdev);
162
163 DRM_DEBUG("radeon: dynamic power management deactivated\n");
164 } else {
165 mutex_unlock(&rdev->pm.mutex);
166 }
167 } else if (count == 1) {
168 rdev->pm.min_mode_engine_clock = rdev->pm.min_gpu_engine_clock;
169 rdev->pm.min_mode_memory_clock = rdev->pm.min_gpu_memory_clock;
170 /* TODO: Increase clocks if needed for current mode */
171
172 if (rdev->pm.state == PM_STATE_MINIMUM) {
173 rdev->pm.state = PM_STATE_ACTIVE;
174 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
175 radeon_pm_set_clocks_locked(rdev);
176
177 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
178 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
179 }
180 else if (rdev->pm.state == PM_STATE_PAUSED) {
181 rdev->pm.state = PM_STATE_ACTIVE;
182 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
183 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
184 DRM_DEBUG("radeon: dynamic power management activated\n");
185 }
186
187 mutex_unlock(&rdev->pm.mutex);
188 }
189 else { /* count == 0 */
190 if (rdev->pm.state != PM_STATE_MINIMUM) {
191 cancel_delayed_work(&rdev->pm.idle_work);
192
193 rdev->pm.state = PM_STATE_MINIMUM;
194 rdev->pm.planned_action = PM_ACTION_MINIMUM;
195 radeon_pm_set_clocks_locked(rdev);
196 }
197
198 mutex_unlock(&rdev->pm.mutex);
199 }
200}
201
202static void radeon_pm_set_clocks_locked(struct radeon_device *rdev)
203{
204 /*radeon_fence_wait_last(rdev);*/
205 switch (rdev->pm.planned_action) {
206 case PM_ACTION_UPCLOCK:
207 radeon_set_engine_clock(rdev, rdev->clock.default_sclk);
208 rdev->pm.downclocked = false;
209 break;
210 case PM_ACTION_DOWNCLOCK:
211 radeon_set_engine_clock(rdev,
212 rdev->pm.min_mode_engine_clock);
213 rdev->pm.downclocked = true;
214 break;
215 case PM_ACTION_MINIMUM:
216 radeon_set_engine_clock(rdev,
217 rdev->pm.min_gpu_engine_clock);
218 break;
219 case PM_ACTION_NONE:
220 DRM_ERROR("%s: PM_ACTION_NONE\n", __func__);
221 break;
222 }
223
224 rdev->pm.planned_action = PM_ACTION_NONE;
225}
226
227static void radeon_pm_set_clocks(struct radeon_device *rdev)
228{
229 mutex_lock(&rdev->pm.mutex);
230 /* new VBLANK irq may come before handling previous one */
231 if (rdev->pm.vblank_callback) {
232 mutex_lock(&rdev->cp.mutex);
233 if (rdev->pm.req_vblank & (1 << 0)) {
234 rdev->pm.req_vblank &= ~(1 << 0);
235 drm_vblank_put(rdev->ddev, 0);
236 }
237 if (rdev->pm.req_vblank & (1 << 1)) {
238 rdev->pm.req_vblank &= ~(1 << 1);
239 drm_vblank_put(rdev->ddev, 1);
240 }
241 rdev->pm.vblank_callback = false;
242 radeon_pm_set_clocks_locked(rdev);
243 mutex_unlock(&rdev->cp.mutex);
244 }
245 mutex_unlock(&rdev->pm.mutex);
246}
247
248static void radeon_pm_reclock_work_handler(struct work_struct *work)
249{
250 struct radeon_device *rdev;
251 rdev = container_of(work, struct radeon_device,
252 pm.reclock_work);
253 radeon_pm_set_clocks(rdev);
254}
255
256static void radeon_pm_idle_work_handler(struct work_struct *work)
257{
258 struct radeon_device *rdev;
259 rdev = container_of(work, struct radeon_device,
260 pm.idle_work.work);
261
262 mutex_lock(&rdev->pm.mutex);
263 if (rdev->pm.state == PM_STATE_ACTIVE &&
264 !rdev->pm.vblank_callback) {
265 unsigned long irq_flags;
266 int not_processed = 0;
267
268 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
269 if (!list_empty(&rdev->fence_drv.emited)) {
270 struct list_head *ptr;
271 list_for_each(ptr, &rdev->fence_drv.emited) {
272 /* count up to 3, that's enought info */
273 if (++not_processed >= 3)
274 break;
275 }
276 }
277 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
278
279 if (not_processed >= 3) { /* should upclock */
280 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
281 rdev->pm.planned_action = PM_ACTION_NONE;
282 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
283 rdev->pm.downclocked) {
284 rdev->pm.planned_action =
285 PM_ACTION_UPCLOCK;
286 rdev->pm.action_timeout = jiffies +
287 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
288 }
289 } else if (not_processed == 0) { /* should downclock */
290 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
291 rdev->pm.planned_action = PM_ACTION_NONE;
292 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
293 !rdev->pm.downclocked) {
294 rdev->pm.planned_action =
295 PM_ACTION_DOWNCLOCK;
296 rdev->pm.action_timeout = jiffies +
297 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
298 }
299 }
300
301 if (rdev->pm.planned_action != PM_ACTION_NONE &&
302 jiffies > rdev->pm.action_timeout) {
303 if (rdev->pm.active_crtcs & (1 << 0)) {
304 rdev->pm.req_vblank |= (1 << 0);
305 drm_vblank_get(rdev->ddev, 0);
306 }
307 if (rdev->pm.active_crtcs & (1 << 1)) {
308 rdev->pm.req_vblank |= (1 << 1);
309 drm_vblank_get(rdev->ddev, 1);
310 }
311 rdev->pm.vblank_callback = true;
312 }
313 }
314 mutex_unlock(&rdev->pm.mutex);
315
316 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
317 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
318}
319
Rafał Miłecki74338742009-11-03 00:53:02 +0100320/*
321 * Debugfs info
322 */
323#if defined(CONFIG_DEBUG_FS)
324
325static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
326{
327 struct drm_info_node *node = (struct drm_info_node *) m->private;
328 struct drm_device *dev = node->minor->dev;
329 struct radeon_device *rdev = dev->dev_private;
330
Rafał Miłeckic913e232009-12-22 23:02:16 +0100331 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100332 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
333 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
334 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
335 if (rdev->asic->get_memory_clock)
336 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100337
338 return 0;
339}
340
341static struct drm_info_list radeon_pm_info_list[] = {
342 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
343};
344#endif
345
Rafał Miłeckic913e232009-12-22 23:02:16 +0100346static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100347{
348#if defined(CONFIG_DEBUG_FS)
349 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
350#else
351 return 0;
352#endif
353}