radeon_kms.c 27.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2008 Advanced Micro Devices, Inc.
 * Copyright 2008 Red Hat Inc.
 * Copyright 2009 Jerome Glisse.
 *
 * 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: Dave Airlie
 *          Alex Deucher
 *          Jerome Glisse
 */
28
#include <drm/drmP.h>
29
#include "radeon.h"
30
#include <drm/radeon_drm.h>
31
#include "radeon_asic.h"
32

33
#include <linux/vga_switcheroo.h>
34
#include <linux/slab.h>
35
#include <linux/pm_runtime.h>
36

37 38
#include "radeon_kfd.h"

39
#if defined(CONFIG_VGA_SWITCHEROO)
40
bool radeon_has_atpx(void);
41
#else
42
static inline bool radeon_has_atpx(void) { return false; }
43 44
#endif

A
Alex Deucher 已提交
45 46 47 48 49 50 51 52 53 54 55
/**
 * radeon_driver_unload_kms - Main unload function for KMS.
 *
 * @dev: drm dev pointer
 *
 * This is the main unload function for KMS (all asics).
 * It calls radeon_modeset_fini() to tear down the
 * displays, and radeon_device_fini() to tear down
 * the rest of the device (CP, writeback, etc.).
 * Returns 0 on success.
 */
56
void radeon_driver_unload_kms(struct drm_device *dev)
57 58 59 60
{
	struct radeon_device *rdev = dev->dev_private;

	if (rdev == NULL)
61
		return;
62

63 64
	if (rdev->rmmio == NULL)
		goto done_free;
65

66 67
	if (radeon_is_px(dev)) {
		pm_runtime_get_sync(dev->dev);
68
		pm_runtime_forbid(dev->dev);
69
	}
70

71 72
	radeon_kfd_device_fini(rdev);

73
	radeon_acpi_fini(rdev);
74
	
75 76
	radeon_modeset_fini(rdev);
	radeon_device_fini(rdev);
77 78

done_free:
79 80 81
	kfree(rdev);
	dev->dev_private = NULL;
}
82

A
Alex Deucher 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * radeon_driver_load_kms - Main load function for KMS.
 *
 * @dev: drm dev pointer
 * @flags: device flags
 *
 * This is the main load function for KMS (all asics).
 * It calls radeon_device_init() to set up the non-display
 * parts of the chip (asic init, CP, writeback, etc.), and
 * radeon_modeset_init() to set up the display parts
 * (crtcs, encoders, hotplug detect, etc.).
 * Returns 0 on success, error on failure.
 */
96 97 98
int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
{
	struct radeon_device *rdev;
99
	int r, acpi_status;
100

101 102 103 104 105 106 107 108 109 110 111 112
	if (!radeon_si_support) {
		switch (flags & RADEON_FAMILY_MASK) {
		case CHIP_TAHITI:
		case CHIP_PITCAIRN:
		case CHIP_VERDE:
		case CHIP_OLAND:
		case CHIP_HAINAN:
			dev_info(dev->dev,
				 "SI support disabled by module param\n");
			return -ENODEV;
		}
	}
113 114 115 116 117 118 119 120
	if (!radeon_cik_support) {
		switch (flags & RADEON_FAMILY_MASK) {
		case CHIP_KAVERI:
		case CHIP_BONAIRE:
		case CHIP_HAWAII:
		case CHIP_KABINI:
		case CHIP_MULLINS:
			dev_info(dev->dev,
121
				 "CIK support disabled by module param\n");
122 123 124 125
			return -ENODEV;
		}
	}

126 127 128 129 130 131 132
	rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
	if (rdev == NULL) {
		return -ENOMEM;
	}
	dev->dev_private = (void *)rdev;

	/* update BUS flag */
133
	if (pci_find_capability(dev->pdev, PCI_CAP_ID_AGP)) {
134
		flags |= RADEON_IS_AGP;
J
Jon Mason 已提交
135
	} else if (pci_is_pcie(dev->pdev)) {
136 137 138 139 140
		flags |= RADEON_IS_PCIE;
	} else {
		flags |= RADEON_IS_PCI;
	}

141 142
	if ((radeon_runtime_pm != 0) &&
	    radeon_has_atpx() &&
143 144
	    ((flags & RADEON_IS_IGP) == 0) &&
	    !pci_is_thunderbolt_attached(rdev->pdev))
145 146
		flags |= RADEON_IS_PX;

147 148 149 150 151 152
	/* radeon_device_init should report only fatal error
	 * like memory allocation failure or iomapping failure,
	 * or memory manager initialization failure, it must
	 * properly initialize the GPU MC controller and permit
	 * VRAM allocation
	 */
153 154
	r = radeon_device_init(rdev, dev, dev->pdev, flags);
	if (r) {
155 156
		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
		goto out;
157
	}
158

159 160 161 162 163
	/* Again modeset_init should fail only on fatal error
	 * otherwise it should provide enough functionalities
	 * for shadowfb to run
	 */
	r = radeon_modeset_init(rdev);
164 165
	if (r)
		dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
166 167 168 169 170 171 172 173 174 175 176

	/* Call ACPI methods: require modeset init
	 * but failure is not fatal
	 */
	if (!r) {
		acpi_status = radeon_acpi_init(rdev);
		if (acpi_status)
		dev_dbg(&dev->pdev->dev,
				"Error during ACPI methods call\n");
	}

177 178 179
	radeon_kfd_device_probe(rdev);
	radeon_kfd_device_init(rdev);

180
	if (radeon_is_px(dev)) {
181 182 183 184 185 186 187 188
		pm_runtime_use_autosuspend(dev->dev);
		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
		pm_runtime_set_active(dev->dev);
		pm_runtime_allow(dev->dev);
		pm_runtime_mark_last_busy(dev->dev);
		pm_runtime_put_autosuspend(dev->dev);
	}

189 190 191
out:
	if (r)
		radeon_driver_unload_kms(dev);
192 193


194
	return r;
195 196
}

A
Alex Deucher 已提交
197 198 199 200 201 202 203 204 205 206
/**
 * radeon_set_filp_rights - Set filp right.
 *
 * @dev: drm dev pointer
 * @owner: drm file
 * @applier: drm file
 * @value: value
 *
 * Sets the filp rights for the device (all asics).
 */
207 208 209 210 211
static void radeon_set_filp_rights(struct drm_device *dev,
				   struct drm_file **owner,
				   struct drm_file *applier,
				   uint32_t *value)
{
212 213 214
	struct radeon_device *rdev = dev->dev_private;

	mutex_lock(&rdev->gem.mutex);
215 216 217 218 219 220 221 222 223 224
	if (*value == 1) {
		/* wants rights */
		if (!*owner)
			*owner = applier;
	} else if (*value == 0) {
		/* revokes rights */
		if (*owner == applier)
			*owner = NULL;
	}
	*value = *owner == applier ? 1 : 0;
225
	mutex_unlock(&rdev->gem.mutex);
226
}
227 228

/*
229
 * Userspace get information ioctl
230
 */
A
Alex Deucher 已提交
231 232 233 234 235 236 237 238 239 240 241 242
/**
 * radeon_info_ioctl - answer a device specific request.
 *
 * @rdev: radeon device pointer
 * @data: request object
 * @filp: drm filp
 *
 * This function is used to pass device specific parameters to the userspace
 * drivers.  Examples include: pci device id, pipeline parms, tiling params,
 * etc. (all asics).
 * Returns 0 on success, -EINVAL on failure.
 */
243
static int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
244 245
{
	struct radeon_device *rdev = dev->dev_private;
246
	struct drm_radeon_info *info = data;
247
	struct radeon_mode_info *minfo = &rdev->mode_info;
248 249
	uint32_t *value, value_tmp, *value_ptr, value_size;
	uint64_t value64;
250 251
	struct drm_crtc *crtc;
	int i, found;
252 253

	value_ptr = (uint32_t *)((unsigned long)info->value);
254 255
	value = &value_tmp;
	value_size = sizeof(uint32_t);
256

257 258
	switch (info->request) {
	case RADEON_INFO_DEVICE_ID:
259
		*value = dev->pdev->device;
260 261
		break;
	case RADEON_INFO_NUM_GB_PIPES:
262
		*value = rdev->num_gb_pipes;
263
		break;
264
	case RADEON_INFO_NUM_Z_PIPES:
265
		*value = rdev->num_z_pipes;
266
		break;
267
	case RADEON_INFO_ACCEL_WORKING:
268 269
		/* xf86-video-ati 6.13.0 relies on this being false for evergreen */
		if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
270
			*value = false;
271
		else
272
			*value = rdev->accel_working;
273
		break;
274
	case RADEON_INFO_CRTC_FROM_ID:
D
Daniel Vetter 已提交
275
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
276 277 278
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
279 280
		for (i = 0, found = 0; i < rdev->num_crtc; i++) {
			crtc = (struct drm_crtc *)minfo->crtcs[i];
281
			if (crtc && crtc->base.id == *value) {
282
				struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
283
				*value = radeon_crtc->crtc_id;
284 285 286 287 288
				found = 1;
				break;
			}
		}
		if (!found) {
289
			DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
290 291 292
			return -EINVAL;
		}
		break;
293
	case RADEON_INFO_ACCEL_WORKING2:
294
		if (rdev->family == CHIP_HAWAII) {
295 296 297 298 299 300
			if (rdev->accel_working) {
				if (rdev->new_fw)
					*value = 3;
				else
					*value = 2;
			} else {
301
				*value = 0;
302
			}
303 304 305
		} else {
			*value = rdev->accel_working;
		}
306
		break;
307
	case RADEON_INFO_TILING_CONFIG:
308 309 310
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.tile_config;
		else if (rdev->family >= CHIP_TAHITI)
311
			*value = rdev->config.si.tile_config;
312
		else if (rdev->family >= CHIP_CAYMAN)
313
			*value = rdev->config.cayman.tile_config;
314
		else if (rdev->family >= CHIP_CEDAR)
315
			*value = rdev->config.evergreen.tile_config;
316
		else if (rdev->family >= CHIP_RV770)
317
			*value = rdev->config.rv770.tile_config;
318
		else if (rdev->family >= CHIP_R600)
319
			*value = rdev->config.r600.tile_config;
320
		else {
321
			DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
322 323
			return -EINVAL;
		}
324
		break;
325
	case RADEON_INFO_WANT_HYPERZ:
326 327 328 329 330 331
		/* The "value" here is both an input and output parameter.
		 * If the input value is 1, filp requests hyper-z access.
		 * If the input value is 0, filp revokes its hyper-z access.
		 *
		 * When returning, the value is 1 if filp owns hyper-z access,
		 * 0 otherwise. */
D
Daniel Vetter 已提交
332
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
333 334 335 336 337
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
		if (*value >= 2) {
			DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", *value);
338 339
			return -EINVAL;
		}
340
		radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
341 342 343
		break;
	case RADEON_INFO_WANT_CMASK:
		/* The same logic as Hyper-Z. */
D
Daniel Vetter 已提交
344
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
345 346 347 348 349
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
		if (*value >= 2) {
			DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", *value);
350
			return -EINVAL;
351
		}
352
		radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
353
		break;
354 355
	case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
		/* return clock value in KHz */
356
		if (rdev->asic->get_xclk)
357
			*value = radeon_get_xclk(rdev) * 10;
358
		else
359
			*value = rdev->clock.spll.reference_freq * 10;
360
		break;
361
	case RADEON_INFO_NUM_BACKENDS:
362 363 364 365
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_backends_per_se *
				rdev->config.cik.max_shader_engines;
		else if (rdev->family >= CHIP_TAHITI)
366
			*value = rdev->config.si.max_backends_per_se *
367 368
				rdev->config.si.max_shader_engines;
		else if (rdev->family >= CHIP_CAYMAN)
369
			*value = rdev->config.cayman.max_backends_per_se *
370 371
				rdev->config.cayman.max_shader_engines;
		else if (rdev->family >= CHIP_CEDAR)
372
			*value = rdev->config.evergreen.max_backends;
373
		else if (rdev->family >= CHIP_RV770)
374
			*value = rdev->config.rv770.max_backends;
375
		else if (rdev->family >= CHIP_R600)
376
			*value = rdev->config.r600.max_backends;
377 378 379 380
		else {
			return -EINVAL;
		}
		break;
381
	case RADEON_INFO_NUM_TILE_PIPES:
382 383 384
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_tile_pipes;
		else if (rdev->family >= CHIP_TAHITI)
385
			*value = rdev->config.si.max_tile_pipes;
386
		else if (rdev->family >= CHIP_CAYMAN)
387
			*value = rdev->config.cayman.max_tile_pipes;
388
		else if (rdev->family >= CHIP_CEDAR)
389
			*value = rdev->config.evergreen.max_tile_pipes;
390
		else if (rdev->family >= CHIP_RV770)
391
			*value = rdev->config.rv770.max_tile_pipes;
392
		else if (rdev->family >= CHIP_R600)
393
			*value = rdev->config.r600.max_tile_pipes;
394 395 396 397
		else {
			return -EINVAL;
		}
		break;
398
	case RADEON_INFO_FUSION_GART_WORKING:
399
		*value = 1;
400
		break;
401
	case RADEON_INFO_BACKEND_MAP:
402
		if (rdev->family >= CHIP_BONAIRE)
403
			*value = rdev->config.cik.backend_map;
404
		else if (rdev->family >= CHIP_TAHITI)
405
			*value = rdev->config.si.backend_map;
406
		else if (rdev->family >= CHIP_CAYMAN)
407
			*value = rdev->config.cayman.backend_map;
408
		else if (rdev->family >= CHIP_CEDAR)
409
			*value = rdev->config.evergreen.backend_map;
410
		else if (rdev->family >= CHIP_RV770)
411
			*value = rdev->config.rv770.backend_map;
412
		else if (rdev->family >= CHIP_R600)
413
			*value = rdev->config.r600.backend_map;
414 415 416 417
		else {
			return -EINVAL;
		}
		break;
418 419 420 421
	case RADEON_INFO_VA_START:
		/* this is where we report if vm is supported or not */
		if (rdev->family < CHIP_CAYMAN)
			return -EINVAL;
422
		*value = RADEON_VA_RESERVED_SIZE;
423 424 425 426 427
		break;
	case RADEON_INFO_IB_VM_MAX_SIZE:
		/* this is where we report if vm is supported or not */
		if (rdev->family < CHIP_CAYMAN)
			return -EINVAL;
428
		*value = RADEON_IB_VM_MAX_SIZE;
429
		break;
430
	case RADEON_INFO_MAX_PIPES:
431 432 433
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_cu_per_sh;
		else if (rdev->family >= CHIP_TAHITI)
434
			*value = rdev->config.si.max_cu_per_sh;
435
		else if (rdev->family >= CHIP_CAYMAN)
436
			*value = rdev->config.cayman.max_pipes_per_simd;
437
		else if (rdev->family >= CHIP_CEDAR)
438
			*value = rdev->config.evergreen.max_pipes;
439
		else if (rdev->family >= CHIP_RV770)
440
			*value = rdev->config.rv770.max_pipes;
441
		else if (rdev->family >= CHIP_R600)
442
			*value = rdev->config.r600.max_pipes;
443 444 445 446
		else {
			return -EINVAL;
		}
		break;
447 448 449 450 451 452 453 454 455
	case RADEON_INFO_TIMESTAMP:
		if (rdev->family < CHIP_R600) {
			DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
			return -EINVAL;
		}
		value = (uint32_t*)&value64;
		value_size = sizeof(uint64_t);
		value64 = radeon_get_gpu_clock_counter(rdev);
		break;
456
	case RADEON_INFO_MAX_SE:
457 458 459
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_shader_engines;
		else if (rdev->family >= CHIP_TAHITI)
460
			*value = rdev->config.si.max_shader_engines;
461
		else if (rdev->family >= CHIP_CAYMAN)
462
			*value = rdev->config.cayman.max_shader_engines;
463
		else if (rdev->family >= CHIP_CEDAR)
464
			*value = rdev->config.evergreen.num_ses;
465
		else
466
			*value = 1;
467 468
		break;
	case RADEON_INFO_MAX_SH_PER_SE:
469 470 471
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_sh_per_se;
		else if (rdev->family >= CHIP_TAHITI)
472
			*value = rdev->config.si.max_sh_per_se;
473 474 475
		else
			return -EINVAL;
		break;
476
	case RADEON_INFO_FASTFB_WORKING:
477
		*value = rdev->fastfb_working;
478
		break;
479
	case RADEON_INFO_RING_WORKING:
D
Daniel Vetter 已提交
480
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
481 482 483 484
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
		switch (*value) {
485 486
		case RADEON_CS_RING_GFX:
		case RADEON_CS_RING_COMPUTE:
487
			*value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
488 489
			break;
		case RADEON_CS_RING_DMA:
490 491
			*value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
			*value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
492 493
			break;
		case RADEON_CS_RING_UVD:
494
			*value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
495
			break;
496 497 498
		case RADEON_CS_RING_VCE:
			*value = rdev->ring[TN_RING_TYPE_VCE1_INDEX].ready;
			break;
499 500 501 502
		default:
			return -EINVAL;
		}
		break;
503
	case RADEON_INFO_SI_TILE_MODE_ARRAY:
504
		if (rdev->family >= CHIP_BONAIRE) {
505 506 507 508 509 510 511
			value = rdev->config.cik.tile_mode_array;
			value_size = sizeof(uint32_t)*32;
		} else if (rdev->family >= CHIP_TAHITI) {
			value = rdev->config.si.tile_mode_array;
			value_size = sizeof(uint32_t)*32;
		} else {
			DRM_DEBUG_KMS("tile mode array is si+ only!\n");
512 513
			return -EINVAL;
		}
514
		break;
515 516 517 518 519 520 521 522 523
	case RADEON_INFO_CIK_MACROTILE_MODE_ARRAY:
		if (rdev->family >= CHIP_BONAIRE) {
			value = rdev->config.cik.macrotile_mode_array;
			value_size = sizeof(uint32_t)*16;
		} else {
			DRM_DEBUG_KMS("macrotile mode array is cik+ only!\n");
			return -EINVAL;
		}
		break;
524 525 526
	case RADEON_INFO_SI_CP_DMA_COMPUTE:
		*value = 1;
		break;
527 528 529 530 531 532 533 534 535
	case RADEON_INFO_SI_BACKEND_ENABLED_MASK:
		if (rdev->family >= CHIP_BONAIRE) {
			*value = rdev->config.cik.backend_enable_mask;
		} else if (rdev->family >= CHIP_TAHITI) {
			*value = rdev->config.si.backend_enable_mask;
		} else {
			DRM_DEBUG_KMS("BACKEND_ENABLED_MASK is si+ only!\n");
		}
		break;
536 537 538 539 540 541 542
	case RADEON_INFO_MAX_SCLK:
		if ((rdev->pm.pm_method == PM_METHOD_DPM) &&
		    rdev->pm.dpm_enabled)
			*value = rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
		else
			*value = rdev->pm.default_sclk * 10;
		break;
543 544 545 546 547 548
	case RADEON_INFO_VCE_FW_VERSION:
		*value = rdev->vce.fw_version;
		break;
	case RADEON_INFO_VCE_FB_VERSION:
		*value = rdev->vce.fb_version;
		break;
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	case RADEON_INFO_NUM_BYTES_MOVED:
		value = (uint32_t*)&value64;
		value_size = sizeof(uint64_t);
		value64 = atomic64_read(&rdev->num_bytes_moved);
		break;
	case RADEON_INFO_VRAM_USAGE:
		value = (uint32_t*)&value64;
		value_size = sizeof(uint64_t);
		value64 = atomic64_read(&rdev->vram_usage);
		break;
	case RADEON_INFO_GTT_USAGE:
		value = (uint32_t*)&value64;
		value_size = sizeof(uint64_t);
		value64 = atomic64_read(&rdev->gtt_usage);
		break;
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
	case RADEON_INFO_ACTIVE_CU_COUNT:
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.active_cus;
		else if (rdev->family >= CHIP_TAHITI)
			*value = rdev->config.si.active_cus;
		else if (rdev->family >= CHIP_CAYMAN)
			*value = rdev->config.cayman.active_simds;
		else if (rdev->family >= CHIP_CEDAR)
			*value = rdev->config.evergreen.active_simds;
		else if (rdev->family >= CHIP_RV770)
			*value = rdev->config.rv770.active_simds;
		else if (rdev->family >= CHIP_R600)
			*value = rdev->config.r600.active_simds;
		else
			*value = 1;
		break;
580 581 582 583 584 585 586
	case RADEON_INFO_CURRENT_GPU_TEMP:
		/* get temperature in millidegrees C */
		if (rdev->asic->pm.get_temperature)
			*value = radeon_get_temperature(rdev);
		else
			*value = 0;
		break;
587 588 589 590 591 592 593 594 595 596 597 598 599 600
	case RADEON_INFO_CURRENT_GPU_SCLK:
		/* get sclk in Mhz */
		if (rdev->pm.dpm_enabled)
			*value = radeon_dpm_get_current_sclk(rdev) / 100;
		else
			*value = rdev->pm.current_sclk / 100;
		break;
	case RADEON_INFO_CURRENT_GPU_MCLK:
		/* get mclk in Mhz */
		if (rdev->pm.dpm_enabled)
			*value = radeon_dpm_get_current_mclk(rdev) / 100;
		else
			*value = rdev->pm.current_mclk / 100;
		break;
601 602 603 604 605 606 607 608
	case RADEON_INFO_READ_REG:
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
		if (radeon_get_allowed_info_register(rdev, *value, value))
			return -EINVAL;
		break;
609 610 611
	case RADEON_INFO_VA_UNMAP_WORKING:
		*value = true;
		break;
612 613 614
	case RADEON_INFO_GPU_RESET_COUNTER:
		*value = atomic_read(&rdev->gpu_reset_counter);
		break;
615
	default:
616
		DRM_DEBUG_KMS("Invalid request %d\n", info->request);
617 618
		return -EINVAL;
	}
D
Daniel Vetter 已提交
619
	if (copy_to_user(value_ptr, (char*)value, value_size)) {
620
		DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
621 622 623 624 625 626 627 628 629
		return -EFAULT;
	}
	return 0;
}


/*
 * Outdated mess for old drm with Xorg being in charge (void function now).
 */
A
Alex Deucher 已提交
630
/**
631
 * radeon_driver_lastclose_kms - drm callback for last close
A
Alex Deucher 已提交
632 633 634
 *
 * @dev: drm dev pointer
 *
635
 * Switch vga_switcheroo state after last close (all asics).
A
Alex Deucher 已提交
636
 */
637 638
void radeon_driver_lastclose_kms(struct drm_device *dev)
{
639 640 641
	struct radeon_device *rdev = dev->dev_private;

	radeon_fbdev_restore_mode(rdev);
642
	vga_switcheroo_process_delayed_switch();
643 644
}

A
Alex Deucher 已提交
645 646 647 648 649 650 651 652 653
/**
 * radeon_driver_open_kms - drm callback for open
 *
 * @dev: drm dev pointer
 * @file_priv: drm file
 *
 * On device open, init vm on cayman+ (all asics).
 * Returns 0 on success, error on failure.
 */
654 655
int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
{
656
	struct radeon_device *rdev = dev->dev_private;
657
	int r;
658 659 660

	file_priv->driver_priv = NULL;

661 662 663 664
	r = pm_runtime_get_sync(dev->dev);
	if (r < 0)
		return r;

665 666 667
	/* new gpu have virtual address space support */
	if (rdev->family >= CHIP_CAYMAN) {
		struct radeon_fpriv *fpriv;
668
		struct radeon_vm *vm;
669 670 671

		fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
		if (unlikely(!fpriv)) {
672 673
			r = -ENOMEM;
			goto out_suspend;
674 675
		}

676
		if (rdev->accel_working) {
677 678 679 680
			vm = &fpriv->vm;
			r = radeon_vm_init(rdev, vm);
			if (r) {
				kfree(fpriv);
681
				goto out_suspend;
682 683
			}

684 685
			r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
			if (r) {
686
				radeon_vm_fini(rdev, vm);
687
				kfree(fpriv);
688
				goto out_suspend;
689
			}
690

691 692
			/* map the ib pool buffer read only into
			 * virtual address space */
693 694 695 696
			vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,
							rdev->ring_tmp_bo.bo);
			r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,
						  RADEON_VA_IB_OFFSET,
697 698 699
						  RADEON_VM_PAGE_READABLE |
						  RADEON_VM_PAGE_SNOOPED);
			if (r) {
700
				radeon_vm_fini(rdev, vm);
701
				kfree(fpriv);
702
				goto out_suspend;
703
			}
704 705 706
		}
		file_priv->driver_priv = fpriv;
	}
707

708
out_suspend:
709 710
	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);
711
	return r;
712 713
}

A
Alex Deucher 已提交
714 715 716 717 718 719
/**
 * radeon_driver_postclose_kms - drm callback for post close
 *
 * @dev: drm dev pointer
 * @file_priv: drm file
 *
720 721
 * On device close, tear down hyperz and cmask filps on r1xx-r5xx
 * (all asics).  And tear down vm on cayman+ (all asics).
A
Alex Deucher 已提交
722
 */
723 724 725
void radeon_driver_postclose_kms(struct drm_device *dev,
				 struct drm_file *file_priv)
{
726 727
	struct radeon_device *rdev = dev->dev_private;

728 729 730 731 732 733 734 735 736 737 738 739
	pm_runtime_get_sync(dev->dev);

	mutex_lock(&rdev->gem.mutex);
	if (rdev->hyperz_filp == file_priv)
		rdev->hyperz_filp = NULL;
	if (rdev->cmask_filp == file_priv)
		rdev->cmask_filp = NULL;
	mutex_unlock(&rdev->gem.mutex);

	radeon_uvd_free_handles(rdev, file_priv);
	radeon_vce_free_handles(rdev, file_priv);

740 741 742
	/* new gpu have virtual address space support */
	if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
		struct radeon_fpriv *fpriv = file_priv->driver_priv;
743
		struct radeon_vm *vm = &fpriv->vm;
744 745
		int r;

746 747 748
		if (rdev->accel_working) {
			r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
			if (!r) {
749 750
				if (vm->ib_bo_va)
					radeon_vm_bo_rmv(rdev, vm->ib_bo_va);
751 752
				radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
			}
753
			radeon_vm_fini(rdev, vm);
754
		}
755 756 757 758

		kfree(fpriv);
		file_priv->driver_priv = NULL;
	}
759 760
	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);
761 762 763 764 765
}

/*
 * VBlank related functions.
 */
A
Alex Deucher 已提交
766 767 768 769
/**
 * radeon_get_vblank_counter_kms - get frame count
 *
 * @dev: drm dev pointer
770
 * @pipe: crtc to get the frame count from
A
Alex Deucher 已提交
771 772 773 774
 *
 * Gets the frame count on the requested crtc (all asics).
 * Returns frame count on success, -EINVAL on failure.
 */
775
u32 radeon_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
776
{
777 778
	int vpos, hpos, stat;
	u32 count;
779 780
	struct radeon_device *rdev = dev->dev_private;

781
	if (pipe >= rdev->num_crtc) {
782
		DRM_ERROR("Invalid crtc %u\n", pipe);
783 784 785
		return -EINVAL;
	}

786 787 788 789 790 791 792 793
	/* The hw increments its frame counter at start of vsync, not at start
	 * of vblank, as is required by DRM core vblank counter handling.
	 * Cook the hw count here to make it appear to the caller as if it
	 * incremented at start of vblank. We measure distance to start of
	 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
	 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
	 * result by 1 to give the proper appearance to caller.
	 */
794
	if (rdev->mode_info.crtcs[pipe]) {
795 796 797 798
		/* Repeat readout if needed to provide stable result if
		 * we cross start of vsync during the queries.
		 */
		do {
799
			count = radeon_get_vblank_counter(rdev, pipe);
800 801 802 803 804
			/* Ask radeon_get_crtc_scanoutpos to return vpos as
			 * distance to start of vblank, instead of regular
			 * vertical scanout pos.
			 */
			stat = radeon_get_crtc_scanoutpos(
805
				dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
806
				&vpos, &hpos, NULL, NULL,
807 808
				&rdev->mode_info.crtcs[pipe]->base.hwmode);
		} while (count != radeon_get_vblank_counter(rdev, pipe));
809 810 811 812 813 814

		if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
		    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
			DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
		}
		else {
815 816
			DRM_DEBUG_VBL("crtc %u: dist from vblank start %d\n",
				      pipe, vpos);
817 818 819 820 821 822 823 824 825 826 827

			/* Bump counter if we are at >= leading edge of vblank,
			 * but before vsync where vpos would turn negative and
			 * the hw counter really increments.
			 */
			if (vpos >= 0)
				count++;
		}
	}
	else {
	    /* Fallback to use value as is. */
828
	    count = radeon_get_vblank_counter(rdev, pipe);
829 830 831 832
	    DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
	}

	return count;
833 834
}

A
Alex Deucher 已提交
835 836 837 838 839 840 841 842 843
/**
 * radeon_enable_vblank_kms - enable vblank interrupt
 *
 * @dev: drm dev pointer
 * @crtc: crtc to enable vblank interrupt for
 *
 * Enable the interrupt on the requested crtc (all asics).
 * Returns 0 on success, -EINVAL on failure.
 */
844 845
int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
{
846
	struct radeon_device *rdev = dev->dev_private;
847 848
	unsigned long irqflags;
	int r;
849

850
	if (crtc < 0 || crtc >= rdev->num_crtc) {
851 852 853 854
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return -EINVAL;
	}

855
	spin_lock_irqsave(&rdev->irq.lock, irqflags);
856
	rdev->irq.crtc_vblank_int[crtc] = true;
857 858 859
	r = radeon_irq_set(rdev);
	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
	return r;
860 861
}

A
Alex Deucher 已提交
862 863 864 865 866 867 868 869
/**
 * radeon_disable_vblank_kms - disable vblank interrupt
 *
 * @dev: drm dev pointer
 * @crtc: crtc to disable vblank interrupt for
 *
 * Disable the interrupt on the requested crtc (all asics).
 */
870 871
void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
{
872
	struct radeon_device *rdev = dev->dev_private;
873
	unsigned long irqflags;
874

875
	if (crtc < 0 || crtc >= rdev->num_crtc) {
876 877 878 879
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return;
	}

880
	spin_lock_irqsave(&rdev->irq.lock, irqflags);
881 882
	rdev->irq.crtc_vblank_int[crtc] = false;
	radeon_irq_set(rdev);
883
	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
884 885
}

R
Rob Clark 已提交
886
const struct drm_ioctl_desc radeon_ioctls_kms[] = {
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
	DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_START, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_RESET, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SWAP, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CLEAR, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_VERTEX, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INDICES, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FLIP, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_ALLOC, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FREE, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, drm_invalid_op, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, drm_invalid_op, DRM_AUTH),
914
	/* KMS */
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_OP, radeon_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_USERPTR, radeon_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
930
};
931
int radeon_max_kms_ioctl = ARRAY_SIZE(radeon_ioctls_kms);