blob: 0dfa508fe5f26fab31b330dbc0c11ac9f0d02afc [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
Alex Deucherd7311172010-05-03 01:13:14 -040061 if (rdev->pm.state != PM_STATE_DISABLED)
Matthew Garrettc37d2302010-04-27 13:58:46 -040062 radeon_get_power_state(rdev, rdev->pm.planned_action);
63
Matthew Garrett612e06c2010-04-27 17:16:58 -040064 mutex_lock(&rdev->ddev->struct_mutex);
65 mutex_lock(&rdev->vram_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -040066 mutex_lock(&rdev->cp.mutex);
Alex Deucher4f3218c2010-04-29 16:14:02 -040067
68 /* gui idle int has issues on older chips it seems */
69 if (rdev->family >= CHIP_R600) {
70 /* wait for GPU idle */
71 rdev->pm.gui_idle = false;
72 rdev->irq.gui_idle = true;
73 radeon_irq_set(rdev);
74 wait_event_interruptible_timeout(
75 rdev->irq.idle_queue, rdev->pm.gui_idle,
76 msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
77 rdev->irq.gui_idle = false;
78 radeon_irq_set(rdev);
Matthew Garrett01434b42010-04-30 15:48:23 -040079 } else {
80 struct radeon_fence *fence;
81 radeon_ring_alloc(rdev, 64);
82 radeon_fence_create(rdev, &fence);
83 radeon_fence_emit(rdev, fence);
84 radeon_ring_commit(rdev);
85 radeon_fence_wait(fence, false);
86 radeon_fence_unref(&fence);
Alex Deucher4f3218c2010-04-29 16:14:02 -040087 }
Matthew Garrett5876dd22010-04-26 15:52:20 -040088 radeon_unmap_vram_bos(rdev);
89
Matthew Garrett2aba6312010-04-26 15:45:23 -040090 if (!static_switch) {
91 for (i = 0; i < rdev->num_crtc; i++) {
92 if (rdev->pm.active_crtcs & (1 << i)) {
93 rdev->pm.req_vblank |= (1 << i);
94 drm_vblank_get(rdev->ddev, i);
95 }
96 }
97 }
Alex Deucher539d2412010-04-29 00:22:43 -040098
Matthew Garrett2aba6312010-04-26 15:45:23 -040099 radeon_set_power_state(rdev, static_switch);
Alex Deuchera4248162010-04-24 14:50:23 -0400100
Matthew Garrett2aba6312010-04-26 15:45:23 -0400101 if (!static_switch) {
102 for (i = 0; i < rdev->num_crtc; i++) {
103 if (rdev->pm.req_vblank & (1 << i)) {
104 rdev->pm.req_vblank &= ~(1 << i);
105 drm_vblank_put(rdev->ddev, i);
106 }
107 }
108 }
Matthew Garrett5876dd22010-04-26 15:52:20 -0400109
Alex Deuchera4248162010-04-24 14:50:23 -0400110 /* update display watermarks based on new power state */
111 radeon_update_bandwidth_info(rdev);
112 if (rdev->pm.active_crtc_count)
113 radeon_bandwidth_update(rdev);
114
Matthew Garrett2aba6312010-04-26 15:45:23 -0400115 rdev->pm.planned_action = PM_ACTION_NONE;
116
Alex Deuchera4248162010-04-24 14:50:23 -0400117 mutex_unlock(&rdev->cp.mutex);
Matthew Garrett612e06c2010-04-27 17:16:58 -0400118 mutex_unlock(&rdev->vram_mutex);
119 mutex_unlock(&rdev->ddev->struct_mutex);
Alex Deuchera4248162010-04-24 14:50:23 -0400120}
121
122static ssize_t radeon_get_power_state_static(struct device *dev,
123 struct device_attribute *attr,
124 char *buf)
125{
126 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
127 struct radeon_device *rdev = ddev->dev_private;
128
129 return snprintf(buf, PAGE_SIZE, "%d.%d\n", rdev->pm.current_power_state_index,
130 rdev->pm.current_clock_mode_index);
131}
132
133static ssize_t radeon_set_power_state_static(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf,
136 size_t count)
137{
138 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
139 struct radeon_device *rdev = ddev->dev_private;
140 int ps, cm;
141
142 if (sscanf(buf, "%u.%u", &ps, &cm) != 2) {
143 DRM_ERROR("Invalid power state!\n");
144 return count;
145 }
146
147 mutex_lock(&rdev->pm.mutex);
148 if ((ps >= 0) && (ps < rdev->pm.num_power_states) &&
149 (cm >= 0) && (cm < rdev->pm.power_state[ps].num_clock_modes)) {
Alex Deucherd7311172010-05-03 01:13:14 -0400150 if ((rdev->pm.active_crtc_count > 0) &&
151 (rdev->pm.power_state[ps].clock_info[cm].flags & RADEON_PM_MODE_NO_DISPLAY)) {
152 DRM_ERROR("Invalid power state for display: %d.%d\n", ps, cm);
153 } else if ((rdev->pm.active_crtc_count > 1) &&
154 (rdev->pm.power_state[ps].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)) {
Alex Deuchera4248162010-04-24 14:50:23 -0400155 DRM_ERROR("Invalid power state for multi-head: %d.%d\n", ps, cm);
156 } else {
157 /* disable dynpm */
158 rdev->pm.state = PM_STATE_DISABLED;
159 rdev->pm.planned_action = PM_ACTION_NONE;
160 rdev->pm.requested_power_state_index = ps;
161 rdev->pm.requested_clock_mode_index = cm;
Matthew Garrett2aba6312010-04-26 15:45:23 -0400162 radeon_pm_set_clocks(rdev, true);
Alex Deuchera4248162010-04-24 14:50:23 -0400163 }
164 } else
165 DRM_ERROR("Invalid power state: %d.%d\n\n", ps, cm);
166 mutex_unlock(&rdev->pm.mutex);
167
168 return count;
169}
170
171static ssize_t radeon_get_dynpm(struct device *dev,
172 struct device_attribute *attr,
173 char *buf)
174{
175 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
176 struct radeon_device *rdev = ddev->dev_private;
177
178 return snprintf(buf, PAGE_SIZE, "%s\n",
179 (rdev->pm.state == PM_STATE_DISABLED) ? "disabled" : "enabled");
180}
181
182static ssize_t radeon_set_dynpm(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf,
185 size_t count)
186{
187 struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
188 struct radeon_device *rdev = ddev->dev_private;
189 int tmp = simple_strtoul(buf, NULL, 10);
190
191 if (tmp == 0) {
192 /* update power mode info */
193 radeon_pm_compute_clocks(rdev);
194 /* disable dynpm */
195 mutex_lock(&rdev->pm.mutex);
196 rdev->pm.state = PM_STATE_DISABLED;
197 rdev->pm.planned_action = PM_ACTION_NONE;
198 mutex_unlock(&rdev->pm.mutex);
199 DRM_INFO("radeon: dynamic power management disabled\n");
200 } else if (tmp == 1) {
201 if (rdev->pm.num_power_states > 1) {
202 /* enable dynpm */
203 mutex_lock(&rdev->pm.mutex);
204 rdev->pm.state = PM_STATE_PAUSED;
205 rdev->pm.planned_action = PM_ACTION_DEFAULT;
206 radeon_get_power_state(rdev, rdev->pm.planned_action);
207 mutex_unlock(&rdev->pm.mutex);
208 /* update power mode info */
209 radeon_pm_compute_clocks(rdev);
210 DRM_INFO("radeon: dynamic power management enabled\n");
211 } else
212 DRM_ERROR("dynpm not valid on this system\n");
213 } else
214 DRM_ERROR("Invalid setting: %d\n", tmp);
215
216 return count;
217}
218
219static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, radeon_get_power_state_static, radeon_set_power_state_static);
220static DEVICE_ATTR(dynpm, S_IRUGO | S_IWUSR, radeon_get_dynpm, radeon_set_dynpm);
221
222
Rafał Miłeckic913e232009-12-22 23:02:16 +0100223static const char *pm_state_names[4] = {
224 "PM_STATE_DISABLED",
225 "PM_STATE_MINIMUM",
226 "PM_STATE_PAUSED",
227 "PM_STATE_ACTIVE"
228};
Rafał Miłecki74338742009-11-03 00:53:02 +0100229
Alex Deucher0ec0e742009-12-23 13:21:58 -0500230static const char *pm_state_types[5] = {
Alex Deucherd91eeb72010-04-05 15:26:43 -0400231 "",
Alex Deucher0ec0e742009-12-23 13:21:58 -0500232 "Powersave",
233 "Battery",
234 "Balanced",
235 "Performance",
236};
237
Alex Deucher56278a82009-12-28 13:58:44 -0500238static void radeon_print_power_mode_info(struct radeon_device *rdev)
239{
240 int i, j;
241 bool is_default;
242
243 DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
244 for (i = 0; i < rdev->pm.num_power_states; i++) {
Alex Deuchera48b9b42010-04-22 14:03:55 -0400245 if (rdev->pm.default_power_state_index == i)
Alex Deucher56278a82009-12-28 13:58:44 -0500246 is_default = true;
247 else
248 is_default = false;
Alex Deucher0ec0e742009-12-23 13:21:58 -0500249 DRM_INFO("State %d %s %s\n", i,
250 pm_state_types[rdev->pm.power_state[i].type],
251 is_default ? "(default)" : "");
Alex Deucher56278a82009-12-28 13:58:44 -0500252 if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
Alex Deucher79daedc2010-04-22 14:25:19 -0400253 DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].pcie_lanes);
Alex Deucherd7311172010-05-03 01:13:14 -0400254 if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
Alex Deuchera48b9b42010-04-22 14:03:55 -0400255 DRM_INFO("\tSingle display only\n");
Alex Deucher56278a82009-12-28 13:58:44 -0500256 DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
257 for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
258 if (rdev->flags & RADEON_IS_IGP)
259 DRM_INFO("\t\t%d engine: %d\n",
260 j,
261 rdev->pm.power_state[i].clock_info[j].sclk * 10);
262 else
263 DRM_INFO("\t\t%d engine/memory: %d/%d\n",
264 j,
265 rdev->pm.power_state[i].clock_info[j].sclk * 10,
266 rdev->pm.power_state[i].clock_info[j].mclk * 10);
Alex Deucherd7311172010-05-03 01:13:14 -0400267 if (rdev->pm.power_state[i].clock_info[j].flags & RADEON_PM_MODE_NO_DISPLAY)
268 DRM_INFO("\t\tNo display only\n");
Alex Deucher56278a82009-12-28 13:58:44 -0500269 }
270 }
271}
272
Alex Deucherbae6b562010-04-22 13:38:05 -0400273void radeon_sync_with_vblank(struct radeon_device *rdev)
Rafał Miłeckid0d6cb82010-03-02 22:06:52 +0100274{
275 if (rdev->pm.active_crtcs) {
276 rdev->pm.vblank_sync = false;
277 wait_event_timeout(
278 rdev->irq.vblank_queue, rdev->pm.vblank_sync,
279 msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
280 }
281}
282
Rafał Miłecki74338742009-11-03 00:53:02 +0100283int radeon_pm_init(struct radeon_device *rdev)
284{
Rafał Miłeckic913e232009-12-22 23:02:16 +0100285 rdev->pm.state = PM_STATE_DISABLED;
286 rdev->pm.planned_action = PM_ACTION_NONE;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400287 rdev->pm.can_upclock = true;
288 rdev->pm.can_downclock = true;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100289
Alex Deucher56278a82009-12-28 13:58:44 -0500290 if (rdev->bios) {
291 if (rdev->is_atom_bios)
292 radeon_atombios_get_power_modes(rdev);
293 else
294 radeon_combios_get_power_modes(rdev);
295 radeon_print_power_mode_info(rdev);
296 }
297
Rafał Miłecki74338742009-11-03 00:53:02 +0100298 if (radeon_debugfs_pm_init(rdev)) {
Rafał Miłeckic142c3e2009-11-06 11:38:34 +0100299 DRM_ERROR("Failed to register debugfs file for PM!\n");
Rafał Miłecki74338742009-11-03 00:53:02 +0100300 }
301
Alex Deuchera4248162010-04-24 14:50:23 -0400302 /* where's the best place to put this? */
303 device_create_file(rdev->dev, &dev_attr_power_state);
304 device_create_file(rdev->dev, &dev_attr_dynpm);
305
Rafał Miłeckic913e232009-12-22 23:02:16 +0100306 INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);
307
Alex Deucher90c39052010-03-24 11:32:29 -0400308 if ((radeon_dynpm != -1 && radeon_dynpm) && (rdev->pm.num_power_states > 1)) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100309 rdev->pm.state = PM_STATE_PAUSED;
310 DRM_INFO("radeon: dynamic power management enabled\n");
311 }
312
313 DRM_INFO("radeon: power management initialized\n");
314
Rafał Miłecki74338742009-11-03 00:53:02 +0100315 return 0;
316}
317
Alex Deucher29fb52c2010-03-11 10:01:17 -0500318void radeon_pm_fini(struct radeon_device *rdev)
319{
Alex Deucher58e21df2010-03-22 13:31:08 -0400320 if (rdev->pm.state != PM_STATE_DISABLED) {
321 /* cancel work */
322 cancel_delayed_work_sync(&rdev->pm.idle_work);
323 /* reset default clocks */
324 rdev->pm.state = PM_STATE_DISABLED;
325 rdev->pm.planned_action = PM_ACTION_DEFAULT;
Alex Deucherd7311172010-05-03 01:13:14 -0400326 radeon_pm_set_clocks(rdev, true);
Alex Deuchera4248162010-04-24 14:50:23 -0400327 } else if ((rdev->pm.current_power_state_index !=
328 rdev->pm.default_power_state_index) ||
329 (rdev->pm.current_clock_mode_index != 0)) {
330 rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
331 rdev->pm.requested_clock_mode_index = 0;
332 mutex_lock(&rdev->pm.mutex);
Matthew Garrett2aba6312010-04-26 15:45:23 -0400333 radeon_pm_set_clocks(rdev, true);
Alex Deuchera4248162010-04-24 14:50:23 -0400334 mutex_unlock(&rdev->pm.mutex);
Alex Deucher58e21df2010-03-22 13:31:08 -0400335 }
336
Alex Deuchera4248162010-04-24 14:50:23 -0400337 device_remove_file(rdev->dev, &dev_attr_power_state);
338 device_remove_file(rdev->dev, &dev_attr_dynpm);
339
Alex Deucher29fb52c2010-03-11 10:01:17 -0500340 if (rdev->pm.i2c_bus)
341 radeon_i2c_destroy(rdev->pm.i2c_bus);
342}
343
Rafał Miłeckic913e232009-12-22 23:02:16 +0100344void radeon_pm_compute_clocks(struct radeon_device *rdev)
345{
346 struct drm_device *ddev = rdev->ddev;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400347 struct drm_crtc *crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100348 struct radeon_crtc *radeon_crtc;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100349
Rafał Miłeckic913e232009-12-22 23:02:16 +0100350 mutex_lock(&rdev->pm.mutex);
351
352 rdev->pm.active_crtcs = 0;
Alex Deuchera48b9b42010-04-22 14:03:55 -0400353 rdev->pm.active_crtc_count = 0;
354 list_for_each_entry(crtc,
355 &ddev->mode_config.crtc_list, head) {
356 radeon_crtc = to_radeon_crtc(crtc);
357 if (radeon_crtc->enabled) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100358 rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
Alex Deuchera48b9b42010-04-22 14:03:55 -0400359 rdev->pm.active_crtc_count++;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100360 }
361 }
362
Alex Deucherd7311172010-05-03 01:13:14 -0400363 if (rdev->pm.state == PM_STATE_DISABLED) {
364 mutex_unlock(&rdev->pm.mutex);
365 return;
366 }
367
368 /* Note, radeon_pm_set_clocks is called with static_switch set
369 * to true since we always want to statically set the clocks,
370 * not wait for vbl.
371 */
Alex Deuchera48b9b42010-04-22 14:03:55 -0400372 if (rdev->pm.active_crtc_count > 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100373 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100374 cancel_delayed_work(&rdev->pm.idle_work);
375
376 rdev->pm.state = PM_STATE_PAUSED;
Alex Deucherd7311172010-05-03 01:13:14 -0400377 rdev->pm.planned_action = PM_ACTION_DEFAULT;
378 radeon_pm_set_clocks(rdev, true);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100379
380 DRM_DEBUG("radeon: dynamic power management deactivated\n");
Rafał Miłeckic913e232009-12-22 23:02:16 +0100381 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400382 } else if (rdev->pm.active_crtc_count == 1) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100383 /* TODO: Increase clocks if needed for current mode */
384
385 if (rdev->pm.state == PM_STATE_MINIMUM) {
386 rdev->pm.state = PM_STATE_ACTIVE;
387 rdev->pm.planned_action = PM_ACTION_UPCLOCK;
Alex Deucherd7311172010-05-03 01:13:14 -0400388 radeon_pm_set_clocks(rdev, true);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100389
390 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
391 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
Alex Deuchera48b9b42010-04-22 14:03:55 -0400392 } else if (rdev->pm.state == PM_STATE_PAUSED) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100393 rdev->pm.state = PM_STATE_ACTIVE;
394 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
395 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
396 DRM_DEBUG("radeon: dynamic power management activated\n");
397 }
Alex Deuchera48b9b42010-04-22 14:03:55 -0400398 } else { /* count == 0 */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100399 if (rdev->pm.state != PM_STATE_MINIMUM) {
400 cancel_delayed_work(&rdev->pm.idle_work);
401
402 rdev->pm.state = PM_STATE_MINIMUM;
403 rdev->pm.planned_action = PM_ACTION_MINIMUM;
Alex Deucherd7311172010-05-03 01:13:14 -0400404 radeon_pm_set_clocks(rdev, true);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100405 }
Rafał Miłeckic913e232009-12-22 23:02:16 +0100406 }
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100407
408 mutex_unlock(&rdev->pm.mutex);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100409}
410
Matthew Garrettf81f2022010-04-28 12:13:06 -0400411bool radeon_pm_in_vbl(struct radeon_device *rdev)
Dave Airlief7352612010-02-18 15:58:36 +1000412{
Alex Deucher539d2412010-04-29 00:22:43 -0400413 u32 stat_crtc = 0, vbl = 0, position = 0;
Dave Airlief7352612010-02-18 15:58:36 +1000414 bool in_vbl = true;
415
Alex Deucherbae6b562010-04-22 13:38:05 -0400416 if (ASIC_IS_DCE4(rdev)) {
Dave Airlief7352612010-02-18 15:58:36 +1000417 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400418 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
419 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
420 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
421 EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
Dave Airlief7352612010-02-18 15:58:36 +1000422 }
423 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400424 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
425 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
426 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
427 EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400428 }
429 if (rdev->pm.active_crtcs & (1 << 2)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400430 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
431 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
432 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
433 EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400434 }
435 if (rdev->pm.active_crtcs & (1 << 3)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400436 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
437 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
438 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
439 EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400440 }
441 if (rdev->pm.active_crtcs & (1 << 4)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400442 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
443 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
444 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
445 EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400446 }
447 if (rdev->pm.active_crtcs & (1 << 5)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400448 vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
449 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
450 position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
451 EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400452 }
453 } else if (ASIC_IS_AVIVO(rdev)) {
454 if (rdev->pm.active_crtcs & (1 << 0)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400455 vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
456 position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400457 }
458 if (rdev->pm.active_crtcs & (1 << 1)) {
Alex Deucher539d2412010-04-29 00:22:43 -0400459 vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
460 position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
Alex Deucherbae6b562010-04-22 13:38:05 -0400461 }
Alex Deucher539d2412010-04-29 00:22:43 -0400462 if (position < vbl && position > 1)
463 in_vbl = false;
Alex Deucherbae6b562010-04-22 13:38:05 -0400464 } else {
465 if (rdev->pm.active_crtcs & (1 << 0)) {
466 stat_crtc = RREG32(RADEON_CRTC_STATUS);
467 if (!(stat_crtc & 1))
468 in_vbl = false;
469 }
470 if (rdev->pm.active_crtcs & (1 << 1)) {
471 stat_crtc = RREG32(RADEON_CRTC2_STATUS);
472 if (!(stat_crtc & 1))
Dave Airlief7352612010-02-18 15:58:36 +1000473 in_vbl = false;
474 }
475 }
Matthew Garrettf81f2022010-04-28 12:13:06 -0400476
Alex Deucher539d2412010-04-29 00:22:43 -0400477 if (position < vbl && position > 1)
478 in_vbl = false;
479
Matthew Garrettf81f2022010-04-28 12:13:06 -0400480 return in_vbl;
481}
482
483bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
484{
485 u32 stat_crtc = 0;
486 bool in_vbl = radeon_pm_in_vbl(rdev);
487
Dave Airlief7352612010-02-18 15:58:36 +1000488 if (in_vbl == false)
Alex Deucherbae6b562010-04-22 13:38:05 -0400489 DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc,
490 finish ? "exit" : "entry");
Dave Airlief7352612010-02-18 15:58:36 +1000491 return in_vbl;
492}
Rafał Miłeckic913e232009-12-22 23:02:16 +0100493
494static void radeon_pm_idle_work_handler(struct work_struct *work)
495{
496 struct radeon_device *rdev;
Matthew Garrettd9932a32010-04-26 16:02:26 -0400497 int resched;
Rafał Miłeckic913e232009-12-22 23:02:16 +0100498 rdev = container_of(work, struct radeon_device,
499 pm.idle_work.work);
500
Matthew Garrettd9932a32010-04-26 16:02:26 -0400501 resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100502 mutex_lock(&rdev->pm.mutex);
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100503 if (rdev->pm.state == PM_STATE_ACTIVE) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100504 unsigned long irq_flags;
505 int not_processed = 0;
506
507 read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
508 if (!list_empty(&rdev->fence_drv.emited)) {
509 struct list_head *ptr;
510 list_for_each(ptr, &rdev->fence_drv.emited) {
511 /* count up to 3, that's enought info */
512 if (++not_processed >= 3)
513 break;
514 }
515 }
516 read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
517
518 if (not_processed >= 3) { /* should upclock */
519 if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
520 rdev->pm.planned_action = PM_ACTION_NONE;
521 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400522 rdev->pm.can_upclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100523 rdev->pm.planned_action =
524 PM_ACTION_UPCLOCK;
525 rdev->pm.action_timeout = jiffies +
526 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
527 }
528 } else if (not_processed == 0) { /* should downclock */
529 if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
530 rdev->pm.planned_action = PM_ACTION_NONE;
531 } else if (rdev->pm.planned_action == PM_ACTION_NONE &&
Alex Deuchera48b9b42010-04-22 14:03:55 -0400532 rdev->pm.can_downclock) {
Rafał Miłeckic913e232009-12-22 23:02:16 +0100533 rdev->pm.planned_action =
534 PM_ACTION_DOWNCLOCK;
535 rdev->pm.action_timeout = jiffies +
536 msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
537 }
538 }
539
Alex Deucherd7311172010-05-03 01:13:14 -0400540 /* Note, radeon_pm_set_clocks is called with static_switch set
541 * to false since we want to wait for vbl to avoid flicker.
542 */
Rafał Miłeckic913e232009-12-22 23:02:16 +0100543 if (rdev->pm.planned_action != PM_ACTION_NONE &&
Rafał Miłecki73a6d3f2010-01-08 00:22:47 +0100544 jiffies > rdev->pm.action_timeout) {
Matthew Garrett2aba6312010-04-26 15:45:23 -0400545 radeon_pm_set_clocks(rdev, false);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100546 }
547 }
548 mutex_unlock(&rdev->pm.mutex);
Matthew Garrettd9932a32010-04-26 16:02:26 -0400549 ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
Rafał Miłeckic913e232009-12-22 23:02:16 +0100550
551 queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
552 msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
553}
554
Rafał Miłecki74338742009-11-03 00:53:02 +0100555/*
556 * Debugfs info
557 */
558#if defined(CONFIG_DEBUG_FS)
559
560static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
561{
562 struct drm_info_node *node = (struct drm_info_node *) m->private;
563 struct drm_device *dev = node->minor->dev;
564 struct radeon_device *rdev = dev->dev_private;
565
Rafał Miłeckic913e232009-12-22 23:02:16 +0100566 seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
Rafał Miłecki62340772009-12-15 21:46:58 +0100567 seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
568 seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
569 seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
570 if (rdev->asic->get_memory_clock)
571 seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
Rafał Miłeckiaa5120d2010-02-18 20:24:28 +0000572 if (rdev->asic->get_pcie_lanes)
573 seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
Rafał Miłecki74338742009-11-03 00:53:02 +0100574
575 return 0;
576}
577
578static struct drm_info_list radeon_pm_info_list[] = {
579 {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
580};
581#endif
582
Rafał Miłeckic913e232009-12-22 23:02:16 +0100583static int radeon_debugfs_pm_init(struct radeon_device *rdev)
Rafał Miłecki74338742009-11-03 00:53:02 +0100584{
585#if defined(CONFIG_DEBUG_FS)
586 return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
587#else
588 return 0;
589#endif
590}