radeon_pm.c 17.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Rafał Miłecki <zajec5@gmail.com>
21
 *          Alex Deucher <alexdeucher@gmail.com>
22 23 24
 */
#include "drmP.h"
#include "radeon.h"
25
#include "avivod.h"
26

27 28
#define RADEON_IDLE_LOOP_MS 100
#define RADEON_RECLOCK_DELAY_MS 200
29
#define RADEON_WAIT_VBLANK_TIMEOUT 200
30
#define RADEON_WAIT_IDLE_TIMEOUT 200
31 32 33 34

static void radeon_pm_idle_work_handler(struct work_struct *work);
static int radeon_debugfs_pm_init(struct radeon_device *rdev);

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
static void radeon_unmap_vram_bos(struct radeon_device *rdev)
{
	struct radeon_bo *bo, *n;

	if (list_empty(&rdev->gem.objects))
		return;

	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
			ttm_bo_unmap_virtual(&bo->tbo);
	}

	if (rdev->gart.table.vram.robj)
		ttm_bo_unmap_virtual(&rdev->gart.table.vram.robj->tbo);

	if (rdev->stollen_vga_memory)
		ttm_bo_unmap_virtual(&rdev->stollen_vga_memory->tbo);

	if (rdev->r600_blit.shader_obj)
		ttm_bo_unmap_virtual(&rdev->r600_blit.shader_obj->tbo);
}

M
Matthew Garrett 已提交
57
static void radeon_pm_set_clocks(struct radeon_device *rdev, int static_switch)
58
{
M
Matthew Garrett 已提交
59 60
	int i;

61 62 63
	if (!static_switch)
		radeon_get_power_state(rdev, rdev->pm.planned_action);

64 65
	mutex_lock(&rdev->ddev->struct_mutex);
	mutex_lock(&rdev->vram_mutex);
66
	mutex_lock(&rdev->cp.mutex);
67 68 69 70 71 72 73 74 75 76 77 78

	/* gui idle int has issues on older chips it seems */
	if (rdev->family >= CHIP_R600) {
		/* wait for GPU idle */
		rdev->pm.gui_idle = false;
		rdev->irq.gui_idle = true;
		radeon_irq_set(rdev);
		wait_event_interruptible_timeout(
			rdev->irq.idle_queue, rdev->pm.gui_idle,
			msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
		rdev->irq.gui_idle = false;
		radeon_irq_set(rdev);
79 80 81 82 83 84 85 86
	} else {
		struct radeon_fence *fence;
		radeon_ring_alloc(rdev, 64);
		radeon_fence_create(rdev, &fence);
		radeon_fence_emit(rdev, fence);
		radeon_ring_commit(rdev);
		radeon_fence_wait(fence, false);
		radeon_fence_unref(&fence);
87
	}
88 89
	radeon_unmap_vram_bos(rdev);

M
Matthew Garrett 已提交
90 91 92 93 94 95 96 97
	if (!static_switch) {
		for (i = 0; i < rdev->num_crtc; i++) {
			if (rdev->pm.active_crtcs & (1 << i)) {
				rdev->pm.req_vblank |= (1 << i);
				drm_vblank_get(rdev->ddev, i);
			}
		}
	}
A
Alex Deucher 已提交
98

M
Matthew Garrett 已提交
99 100 101 102 103 104 105 106 107 108
	radeon_set_power_state(rdev, static_switch);

	if (!static_switch) {
		for (i = 0; i < rdev->num_crtc; i++) {
			if (rdev->pm.req_vblank & (1 << i)) {
				rdev->pm.req_vblank &= ~(1 << i);
				drm_vblank_put(rdev->ddev, i);
			}
		}
	}
109

110 111 112 113 114
	/* update display watermarks based on new power state */
	radeon_update_bandwidth_info(rdev);
	if (rdev->pm.active_crtc_count)
		radeon_bandwidth_update(rdev);

M
Matthew Garrett 已提交
115 116
	rdev->pm.planned_action = PM_ACTION_NONE;

117
	mutex_unlock(&rdev->cp.mutex);
118 119
	mutex_unlock(&rdev->vram_mutex);
	mutex_unlock(&rdev->ddev->struct_mutex);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

static ssize_t radeon_get_power_state_static(struct device *dev,
					     struct device_attribute *attr,
					     char *buf)
{
	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
	struct radeon_device *rdev = ddev->dev_private;

	return snprintf(buf, PAGE_SIZE, "%d.%d\n", rdev->pm.current_power_state_index,
			rdev->pm.current_clock_mode_index);
}

static ssize_t radeon_set_power_state_static(struct device *dev,
					     struct device_attribute *attr,
					     const char *buf,
					     size_t count)
{
	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
	struct radeon_device *rdev = ddev->dev_private;
	int ps, cm;

	if (sscanf(buf, "%u.%u", &ps, &cm) != 2) {
		DRM_ERROR("Invalid power state!\n");
		return count;
	}

	mutex_lock(&rdev->pm.mutex);
	if ((ps >= 0) && (ps < rdev->pm.num_power_states) &&
	    (cm >= 0) && (cm < rdev->pm.power_state[ps].num_clock_modes)) {
		if ((rdev->pm.active_crtc_count > 1) &&
		    (rdev->pm.power_state[ps].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)) {
			DRM_ERROR("Invalid power state for multi-head: %d.%d\n", ps, cm);
		} else {
			/* disable dynpm */
			rdev->pm.state = PM_STATE_DISABLED;
			rdev->pm.planned_action = PM_ACTION_NONE;
			rdev->pm.requested_power_state_index = ps;
			rdev->pm.requested_clock_mode_index = cm;
M
Matthew Garrett 已提交
159
			radeon_pm_set_clocks(rdev, true);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		}
	} else
		DRM_ERROR("Invalid power state: %d.%d\n\n", ps, cm);
	mutex_unlock(&rdev->pm.mutex);

	return count;
}

static ssize_t radeon_get_dynpm(struct device *dev,
				struct device_attribute *attr,
				char *buf)
{
	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
	struct radeon_device *rdev = ddev->dev_private;

	return snprintf(buf, PAGE_SIZE, "%s\n",
			(rdev->pm.state == PM_STATE_DISABLED) ? "disabled" : "enabled");
}

static ssize_t radeon_set_dynpm(struct device *dev,
				struct device_attribute *attr,
				const char *buf,
				size_t count)
{
	struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
	struct radeon_device *rdev = ddev->dev_private;
	int tmp = simple_strtoul(buf, NULL, 10);

	if (tmp == 0) {
		/* update power mode info */
		radeon_pm_compute_clocks(rdev);
		/* disable dynpm */
		mutex_lock(&rdev->pm.mutex);
		rdev->pm.state = PM_STATE_DISABLED;
		rdev->pm.planned_action = PM_ACTION_NONE;
		mutex_unlock(&rdev->pm.mutex);
		DRM_INFO("radeon: dynamic power management disabled\n");
	} else if (tmp == 1) {
		if (rdev->pm.num_power_states > 1) {
			/* enable dynpm */
			mutex_lock(&rdev->pm.mutex);
			rdev->pm.state = PM_STATE_PAUSED;
			rdev->pm.planned_action = PM_ACTION_DEFAULT;
			radeon_get_power_state(rdev, rdev->pm.planned_action);
			mutex_unlock(&rdev->pm.mutex);
			/* update power mode info */
			radeon_pm_compute_clocks(rdev);
			DRM_INFO("radeon: dynamic power management enabled\n");
		} else
			DRM_ERROR("dynpm not valid on this system\n");
	} else
		DRM_ERROR("Invalid setting: %d\n", tmp);

	return count;
}

static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, radeon_get_power_state_static, radeon_set_power_state_static);
static DEVICE_ATTR(dynpm, S_IRUGO | S_IWUSR, radeon_get_dynpm, radeon_set_dynpm);


220 221 222 223 224 225
static const char *pm_state_names[4] = {
	"PM_STATE_DISABLED",
	"PM_STATE_MINIMUM",
	"PM_STATE_PAUSED",
	"PM_STATE_ACTIVE"
};
226

227
static const char *pm_state_types[5] = {
228
	"",
229 230 231 232 233 234
	"Powersave",
	"Battery",
	"Balanced",
	"Performance",
};

235 236 237 238 239 240 241
static void radeon_print_power_mode_info(struct radeon_device *rdev)
{
	int i, j;
	bool is_default;

	DRM_INFO("%d Power State(s)\n", rdev->pm.num_power_states);
	for (i = 0; i < rdev->pm.num_power_states; i++) {
242
		if (rdev->pm.default_power_state_index == i)
243 244 245
			is_default = true;
		else
			is_default = false;
246 247 248
		DRM_INFO("State %d %s %s\n", i,
			 pm_state_types[rdev->pm.power_state[i].type],
			 is_default ? "(default)" : "");
249
		if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
A
Alex Deucher 已提交
250
			DRM_INFO("\t%d PCIE Lanes\n", rdev->pm.power_state[i].pcie_lanes);
251 252
		if (rdev->pm.power_state[i].flags & RADEON_PM_SINGLE_DISPLAY_ONLY)
			DRM_INFO("\tSingle display only\n");
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
		DRM_INFO("\t%d Clock Mode(s)\n", rdev->pm.power_state[i].num_clock_modes);
		for (j = 0; j < rdev->pm.power_state[i].num_clock_modes; j++) {
			if (rdev->flags & RADEON_IS_IGP)
				DRM_INFO("\t\t%d engine: %d\n",
					 j,
					 rdev->pm.power_state[i].clock_info[j].sclk * 10);
			else
				DRM_INFO("\t\t%d engine/memory: %d/%d\n",
					 j,
					 rdev->pm.power_state[i].clock_info[j].sclk * 10,
					 rdev->pm.power_state[i].clock_info[j].mclk * 10);
		}
	}
}

268
void radeon_sync_with_vblank(struct radeon_device *rdev)
269 270 271 272 273 274 275 276 277
{
	if (rdev->pm.active_crtcs) {
		rdev->pm.vblank_sync = false;
		wait_event_timeout(
			rdev->irq.vblank_queue, rdev->pm.vblank_sync,
			msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
	}
}

278 279
int radeon_pm_init(struct radeon_device *rdev)
{
280 281
	rdev->pm.state = PM_STATE_DISABLED;
	rdev->pm.planned_action = PM_ACTION_NONE;
282 283
	rdev->pm.can_upclock = true;
	rdev->pm.can_downclock = true;
284

285 286 287 288 289 290 291 292
	if (rdev->bios) {
		if (rdev->is_atom_bios)
			radeon_atombios_get_power_modes(rdev);
		else
			radeon_combios_get_power_modes(rdev);
		radeon_print_power_mode_info(rdev);
	}

293
	if (radeon_debugfs_pm_init(rdev)) {
R
Rafał Miłecki 已提交
294
		DRM_ERROR("Failed to register debugfs file for PM!\n");
295 296
	}

297 298 299 300
	/* where's the best place to put this? */
	device_create_file(rdev->dev, &dev_attr_power_state);
	device_create_file(rdev->dev, &dev_attr_dynpm);

301 302
	INIT_DELAYED_WORK(&rdev->pm.idle_work, radeon_pm_idle_work_handler);

303
	if ((radeon_dynpm != -1 && radeon_dynpm) && (rdev->pm.num_power_states > 1)) {
304 305 306 307 308 309
		rdev->pm.state = PM_STATE_PAUSED;
		DRM_INFO("radeon: dynamic power management enabled\n");
	}

	DRM_INFO("radeon: power management initialized\n");

310 311 312
	return 0;
}

313 314
void radeon_pm_fini(struct radeon_device *rdev)
{
315 316 317 318 319 320
	if (rdev->pm.state != PM_STATE_DISABLED) {
		/* cancel work */
		cancel_delayed_work_sync(&rdev->pm.idle_work);
		/* reset default clocks */
		rdev->pm.state = PM_STATE_DISABLED;
		rdev->pm.planned_action = PM_ACTION_DEFAULT;
M
Matthew Garrett 已提交
321
		radeon_pm_set_clocks(rdev, false);
322 323 324 325 326 327
	} else if ((rdev->pm.current_power_state_index !=
		    rdev->pm.default_power_state_index) ||
		   (rdev->pm.current_clock_mode_index != 0)) {
		rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
		rdev->pm.requested_clock_mode_index = 0;
		mutex_lock(&rdev->pm.mutex);
M
Matthew Garrett 已提交
328
		radeon_pm_set_clocks(rdev, true);
329
		mutex_unlock(&rdev->pm.mutex);
330 331
	}

332 333 334
	device_remove_file(rdev->dev, &dev_attr_power_state);
	device_remove_file(rdev->dev, &dev_attr_dynpm);

335 336 337 338
	if (rdev->pm.i2c_bus)
		radeon_i2c_destroy(rdev->pm.i2c_bus);
}

339 340 341
void radeon_pm_compute_clocks(struct radeon_device *rdev)
{
	struct drm_device *ddev = rdev->ddev;
342
	struct drm_crtc *crtc;
343 344 345 346 347 348 349 350
	struct radeon_crtc *radeon_crtc;

	if (rdev->pm.state == PM_STATE_DISABLED)
		return;

	mutex_lock(&rdev->pm.mutex);

	rdev->pm.active_crtcs = 0;
351 352 353 354 355
	rdev->pm.active_crtc_count = 0;
	list_for_each_entry(crtc,
		&ddev->mode_config.crtc_list, head) {
		radeon_crtc = to_radeon_crtc(crtc);
		if (radeon_crtc->enabled) {
356
			rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
357
			rdev->pm.active_crtc_count++;
358 359 360
		}
	}

361
	if (rdev->pm.active_crtc_count > 1) {
362 363 364 365 366
		if (rdev->pm.state == PM_STATE_ACTIVE) {
			cancel_delayed_work(&rdev->pm.idle_work);

			rdev->pm.state = PM_STATE_PAUSED;
			rdev->pm.planned_action = PM_ACTION_UPCLOCK;
M
Matthew Garrett 已提交
367
			radeon_pm_set_clocks(rdev, false);
368 369 370

			DRM_DEBUG("radeon: dynamic power management deactivated\n");
		}
371
	} else if (rdev->pm.active_crtc_count == 1) {
372 373 374 375 376
		/* TODO: Increase clocks if needed for current mode */

		if (rdev->pm.state == PM_STATE_MINIMUM) {
			rdev->pm.state = PM_STATE_ACTIVE;
			rdev->pm.planned_action = PM_ACTION_UPCLOCK;
M
Matthew Garrett 已提交
377
			radeon_pm_set_clocks(rdev, false);
378 379 380

			queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
				msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
381
		} else if (rdev->pm.state == PM_STATE_PAUSED) {
382 383 384 385 386
			rdev->pm.state = PM_STATE_ACTIVE;
			queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
				msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
			DRM_DEBUG("radeon: dynamic power management activated\n");
		}
387
	} else { /* count == 0 */
388 389 390 391 392
		if (rdev->pm.state != PM_STATE_MINIMUM) {
			cancel_delayed_work(&rdev->pm.idle_work);

			rdev->pm.state = PM_STATE_MINIMUM;
			rdev->pm.planned_action = PM_ACTION_MINIMUM;
M
Matthew Garrett 已提交
393
			radeon_pm_set_clocks(rdev, false);
394 395
		}
	}
396 397

	mutex_unlock(&rdev->pm.mutex);
398 399
}

400
bool radeon_pm_in_vbl(struct radeon_device *rdev)
401
{
A
Alex Deucher 已提交
402
	u32 stat_crtc = 0, vbl = 0, position = 0;
403 404
	bool in_vbl = true;

405 406
	if (ASIC_IS_DCE4(rdev)) {
		if (rdev->pm.active_crtcs & (1 << 0)) {
A
Alex Deucher 已提交
407 408 409 410
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC0_REGISTER_OFFSET) & 0xfff;
411 412
		}
		if (rdev->pm.active_crtcs & (1 << 1)) {
A
Alex Deucher 已提交
413 414 415 416
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC1_REGISTER_OFFSET) & 0xfff;
417 418
		}
		if (rdev->pm.active_crtcs & (1 << 2)) {
A
Alex Deucher 已提交
419 420 421 422
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC2_REGISTER_OFFSET) & 0xfff;
423 424
		}
		if (rdev->pm.active_crtcs & (1 << 3)) {
A
Alex Deucher 已提交
425 426 427 428
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC3_REGISTER_OFFSET) & 0xfff;
429 430
		}
		if (rdev->pm.active_crtcs & (1 << 4)) {
A
Alex Deucher 已提交
431 432 433 434
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC4_REGISTER_OFFSET) & 0xfff;
435 436
		}
		if (rdev->pm.active_crtcs & (1 << 5)) {
A
Alex Deucher 已提交
437 438 439 440
			vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
				     EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
			position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
					  EVERGREEN_CRTC5_REGISTER_OFFSET) & 0xfff;
441 442 443
		}
	} else if (ASIC_IS_AVIVO(rdev)) {
		if (rdev->pm.active_crtcs & (1 << 0)) {
A
Alex Deucher 已提交
444 445
			vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END) & 0xfff;
			position = RREG32(AVIVO_D1CRTC_STATUS_POSITION) & 0xfff;
446 447
		}
		if (rdev->pm.active_crtcs & (1 << 1)) {
A
Alex Deucher 已提交
448 449
			vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END) & 0xfff;
			position = RREG32(AVIVO_D2CRTC_STATUS_POSITION) & 0xfff;
450
		}
A
Alex Deucher 已提交
451 452
		if (position < vbl && position > 1)
			in_vbl = false;
453
	} else {
454
		if (rdev->pm.active_crtcs & (1 << 0)) {
455 456
			stat_crtc = RREG32(RADEON_CRTC_STATUS);
			if (!(stat_crtc & 1))
457 458 459
				in_vbl = false;
		}
		if (rdev->pm.active_crtcs & (1 << 1)) {
460 461
			stat_crtc = RREG32(RADEON_CRTC2_STATUS);
			if (!(stat_crtc & 1))
462 463 464
				in_vbl = false;
		}
	}
465

A
Alex Deucher 已提交
466 467 468
	if (position < vbl && position > 1)
		in_vbl = false;

469 470 471 472 473 474 475 476
	return in_vbl;
}

bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
{
	u32 stat_crtc = 0;
	bool in_vbl = radeon_pm_in_vbl(rdev);

477
	if (in_vbl == false)
478 479
		DRM_INFO("not in vbl for pm change %08x at %s\n", stat_crtc,
			 finish ? "exit" : "entry");
480 481
	return in_vbl;
}
482 483 484 485

static void radeon_pm_idle_work_handler(struct work_struct *work)
{
	struct radeon_device *rdev;
486
	int resched;
487 488 489
	rdev = container_of(work, struct radeon_device,
				pm.idle_work.work);

490
	resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
491
	mutex_lock(&rdev->pm.mutex);
492
	if (rdev->pm.state == PM_STATE_ACTIVE) {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
		unsigned long irq_flags;
		int not_processed = 0;

		read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
		if (!list_empty(&rdev->fence_drv.emited)) {
			struct list_head *ptr;
			list_for_each(ptr, &rdev->fence_drv.emited) {
				/* count up to 3, that's enought info */
				if (++not_processed >= 3)
					break;
			}
		}
		read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);

		if (not_processed >= 3) { /* should upclock */
			if (rdev->pm.planned_action == PM_ACTION_DOWNCLOCK) {
				rdev->pm.planned_action = PM_ACTION_NONE;
			} else if (rdev->pm.planned_action == PM_ACTION_NONE &&
511
				   rdev->pm.can_upclock) {
512 513 514 515 516 517 518 519 520
				rdev->pm.planned_action =
					PM_ACTION_UPCLOCK;
				rdev->pm.action_timeout = jiffies +
				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
			}
		} else if (not_processed == 0) { /* should downclock */
			if (rdev->pm.planned_action == PM_ACTION_UPCLOCK) {
				rdev->pm.planned_action = PM_ACTION_NONE;
			} else if (rdev->pm.planned_action == PM_ACTION_NONE &&
521
				   rdev->pm.can_downclock) {
522 523 524 525 526 527 528 529
				rdev->pm.planned_action =
					PM_ACTION_DOWNCLOCK;
				rdev->pm.action_timeout = jiffies +
				msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
			}
		}

		if (rdev->pm.planned_action != PM_ACTION_NONE &&
530
		    jiffies > rdev->pm.action_timeout) {
M
Matthew Garrett 已提交
531
			radeon_pm_set_clocks(rdev, false);
532 533 534
		}
	}
	mutex_unlock(&rdev->pm.mutex);
535
	ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
536 537 538 539 540

	queue_delayed_work(rdev->wq, &rdev->pm.idle_work,
					msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
}

541 542 543 544 545 546 547 548 549 550 551
/*
 * Debugfs info
 */
#if defined(CONFIG_DEBUG_FS)

static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
{
	struct drm_info_node *node = (struct drm_info_node *) m->private;
	struct drm_device *dev = node->minor->dev;
	struct radeon_device *rdev = dev->dev_private;

552
	seq_printf(m, "state: %s\n", pm_state_names[rdev->pm.state]);
553 554 555 556 557
	seq_printf(m, "default engine clock: %u0 kHz\n", rdev->clock.default_sclk);
	seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
	seq_printf(m, "default memory clock: %u0 kHz\n", rdev->clock.default_mclk);
	if (rdev->asic->get_memory_clock)
		seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
558 559
	if (rdev->asic->get_pcie_lanes)
		seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
560 561 562 563 564 565 566 567 568

	return 0;
}

static struct drm_info_list radeon_pm_info_list[] = {
	{"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
};
#endif

569
static int radeon_debugfs_pm_init(struct radeon_device *rdev)
570 571 572 573 574 575 576
{
#if defined(CONFIG_DEBUG_FS)
	return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
#else
	return 0;
#endif
}