radeon_kms.c 27.8 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 57 58 59 60 61
int radeon_driver_unload_kms(struct drm_device *dev)
{
	struct radeon_device *rdev = dev->dev_private;

	if (rdev == NULL)
		return 0;
62

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

	pm_runtime_get_sync(dev->dev);

68 69
	radeon_kfd_device_fini(rdev);

70
	radeon_acpi_fini(rdev);
71
	
72 73
	radeon_modeset_fini(rdev);
	radeon_device_fini(rdev);
74 75

done_free:
76 77 78 79
	kfree(rdev);
	dev->dev_private = NULL;
	return 0;
}
80

A
Alex Deucher 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
/**
 * 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.
 */
94 95 96
int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
{
	struct radeon_device *rdev;
97
	int r, acpi_status;
98 99 100 101 102 103 104 105

	rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
	if (rdev == NULL) {
		return -ENOMEM;
	}
	dev->dev_private = (void *)rdev;

	/* update BUS flag */
106
	if (drm_pci_device_is_agp(dev)) {
107
		flags |= RADEON_IS_AGP;
J
Jon Mason 已提交
108
	} else if (pci_is_pcie(dev->pdev)) {
109 110 111 112 113
		flags |= RADEON_IS_PCIE;
	} else {
		flags |= RADEON_IS_PCI;
	}

114 115 116
	if ((radeon_runtime_pm != 0) &&
	    radeon_has_atpx() &&
	    ((flags & RADEON_IS_IGP) == 0))
117 118
		flags |= RADEON_IS_PX;

119 120 121 122 123 124
	/* 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
	 */
125 126
	r = radeon_device_init(rdev, dev, dev->pdev, flags);
	if (r) {
127 128
		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
		goto out;
129
	}
130

131 132 133 134 135
	/* Again modeset_init should fail only on fatal error
	 * otherwise it should provide enough functionalities
	 * for shadowfb to run
	 */
	r = radeon_modeset_init(rdev);
136 137
	if (r)
		dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
138 139 140 141 142 143 144 145 146 147 148

	/* 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");
	}

149 150 151
	radeon_kfd_device_probe(rdev);
	radeon_kfd_device_init(rdev);

152
	if (radeon_is_px(dev)) {
153 154 155 156 157 158 159 160
		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);
	}

161 162 163
out:
	if (r)
		radeon_driver_unload_kms(dev);
164 165


166
	return r;
167 168
}

A
Alex Deucher 已提交
169 170 171 172 173 174 175 176 177 178
/**
 * 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).
 */
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
static void radeon_set_filp_rights(struct drm_device *dev,
				   struct drm_file **owner,
				   struct drm_file *applier,
				   uint32_t *value)
{
	mutex_lock(&dev->struct_mutex);
	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;
	mutex_unlock(&dev->struct_mutex);
}
197 198

/*
199
 * Userspace get information ioctl
200
 */
A
Alex Deucher 已提交
201 202 203 204 205 206 207 208 209 210 211 212
/**
 * 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.
 */
213
static int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
214 215
{
	struct radeon_device *rdev = dev->dev_private;
216
	struct drm_radeon_info *info = data;
217
	struct radeon_mode_info *minfo = &rdev->mode_info;
218 219
	uint32_t *value, value_tmp, *value_ptr, value_size;
	uint64_t value64;
220 221
	struct drm_crtc *crtc;
	int i, found;
222 223

	value_ptr = (uint32_t *)((unsigned long)info->value);
224 225
	value = &value_tmp;
	value_size = sizeof(uint32_t);
226

227 228
	switch (info->request) {
	case RADEON_INFO_DEVICE_ID:
229
		*value = dev->pdev->device;
230 231
		break;
	case RADEON_INFO_NUM_GB_PIPES:
232
		*value = rdev->num_gb_pipes;
233
		break;
234
	case RADEON_INFO_NUM_Z_PIPES:
235
		*value = rdev->num_z_pipes;
236
		break;
237
	case RADEON_INFO_ACCEL_WORKING:
238 239
		/* xf86-video-ati 6.13.0 relies on this being false for evergreen */
		if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
240
			*value = false;
241
		else
242
			*value = rdev->accel_working;
243
		break;
244
	case RADEON_INFO_CRTC_FROM_ID:
D
Daniel Vetter 已提交
245
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
246 247 248
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
249 250
		for (i = 0, found = 0; i < rdev->num_crtc; i++) {
			crtc = (struct drm_crtc *)minfo->crtcs[i];
251
			if (crtc && crtc->base.id == *value) {
252
				struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
253
				*value = radeon_crtc->crtc_id;
254 255 256 257 258
				found = 1;
				break;
			}
		}
		if (!found) {
259
			DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
260 261 262
			return -EINVAL;
		}
		break;
263
	case RADEON_INFO_ACCEL_WORKING2:
264
		if (rdev->family == CHIP_HAWAII) {
265 266 267 268 269 270
			if (rdev->accel_working) {
				if (rdev->new_fw)
					*value = 3;
				else
					*value = 2;
			} else {
271
				*value = 0;
272
			}
273 274 275
		} else {
			*value = rdev->accel_working;
		}
276
		break;
277
	case RADEON_INFO_TILING_CONFIG:
278 279 280
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.tile_config;
		else if (rdev->family >= CHIP_TAHITI)
281
			*value = rdev->config.si.tile_config;
282
		else if (rdev->family >= CHIP_CAYMAN)
283
			*value = rdev->config.cayman.tile_config;
284
		else if (rdev->family >= CHIP_CEDAR)
285
			*value = rdev->config.evergreen.tile_config;
286
		else if (rdev->family >= CHIP_RV770)
287
			*value = rdev->config.rv770.tile_config;
288
		else if (rdev->family >= CHIP_R600)
289
			*value = rdev->config.r600.tile_config;
290
		else {
291
			DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
292 293
			return -EINVAL;
		}
294
		break;
295
	case RADEON_INFO_WANT_HYPERZ:
296 297 298 299 300 301
		/* 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 已提交
302
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
303 304 305 306 307
			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);
308 309
			return -EINVAL;
		}
310
		radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
311 312 313
		break;
	case RADEON_INFO_WANT_CMASK:
		/* The same logic as Hyper-Z. */
D
Daniel Vetter 已提交
314
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
315 316 317 318 319
			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);
320
			return -EINVAL;
321
		}
322
		radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
323
		break;
324 325
	case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
		/* return clock value in KHz */
326
		if (rdev->asic->get_xclk)
327
			*value = radeon_get_xclk(rdev) * 10;
328
		else
329
			*value = rdev->clock.spll.reference_freq * 10;
330
		break;
331
	case RADEON_INFO_NUM_BACKENDS:
332 333 334 335
		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)
336
			*value = rdev->config.si.max_backends_per_se *
337 338
				rdev->config.si.max_shader_engines;
		else if (rdev->family >= CHIP_CAYMAN)
339
			*value = rdev->config.cayman.max_backends_per_se *
340 341
				rdev->config.cayman.max_shader_engines;
		else if (rdev->family >= CHIP_CEDAR)
342
			*value = rdev->config.evergreen.max_backends;
343
		else if (rdev->family >= CHIP_RV770)
344
			*value = rdev->config.rv770.max_backends;
345
		else if (rdev->family >= CHIP_R600)
346
			*value = rdev->config.r600.max_backends;
347 348 349 350
		else {
			return -EINVAL;
		}
		break;
351
	case RADEON_INFO_NUM_TILE_PIPES:
352 353 354
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_tile_pipes;
		else if (rdev->family >= CHIP_TAHITI)
355
			*value = rdev->config.si.max_tile_pipes;
356
		else if (rdev->family >= CHIP_CAYMAN)
357
			*value = rdev->config.cayman.max_tile_pipes;
358
		else if (rdev->family >= CHIP_CEDAR)
359
			*value = rdev->config.evergreen.max_tile_pipes;
360
		else if (rdev->family >= CHIP_RV770)
361
			*value = rdev->config.rv770.max_tile_pipes;
362
		else if (rdev->family >= CHIP_R600)
363
			*value = rdev->config.r600.max_tile_pipes;
364 365 366 367
		else {
			return -EINVAL;
		}
		break;
368
	case RADEON_INFO_FUSION_GART_WORKING:
369
		*value = 1;
370
		break;
371
	case RADEON_INFO_BACKEND_MAP:
372
		if (rdev->family >= CHIP_BONAIRE)
373
			*value = rdev->config.cik.backend_map;
374
		else if (rdev->family >= CHIP_TAHITI)
375
			*value = rdev->config.si.backend_map;
376
		else if (rdev->family >= CHIP_CAYMAN)
377
			*value = rdev->config.cayman.backend_map;
378
		else if (rdev->family >= CHIP_CEDAR)
379
			*value = rdev->config.evergreen.backend_map;
380
		else if (rdev->family >= CHIP_RV770)
381
			*value = rdev->config.rv770.backend_map;
382
		else if (rdev->family >= CHIP_R600)
383
			*value = rdev->config.r600.backend_map;
384 385 386 387
		else {
			return -EINVAL;
		}
		break;
388 389 390 391
	case RADEON_INFO_VA_START:
		/* this is where we report if vm is supported or not */
		if (rdev->family < CHIP_CAYMAN)
			return -EINVAL;
392
		*value = RADEON_VA_RESERVED_SIZE;
393 394 395 396 397
		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;
398
		*value = RADEON_IB_VM_MAX_SIZE;
399
		break;
400
	case RADEON_INFO_MAX_PIPES:
401 402 403
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_cu_per_sh;
		else if (rdev->family >= CHIP_TAHITI)
404
			*value = rdev->config.si.max_cu_per_sh;
405
		else if (rdev->family >= CHIP_CAYMAN)
406
			*value = rdev->config.cayman.max_pipes_per_simd;
407
		else if (rdev->family >= CHIP_CEDAR)
408
			*value = rdev->config.evergreen.max_pipes;
409
		else if (rdev->family >= CHIP_RV770)
410
			*value = rdev->config.rv770.max_pipes;
411
		else if (rdev->family >= CHIP_R600)
412
			*value = rdev->config.r600.max_pipes;
413 414 415 416
		else {
			return -EINVAL;
		}
		break;
417 418 419 420 421 422 423 424 425
	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;
426
	case RADEON_INFO_MAX_SE:
427 428 429
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_shader_engines;
		else if (rdev->family >= CHIP_TAHITI)
430
			*value = rdev->config.si.max_shader_engines;
431
		else if (rdev->family >= CHIP_CAYMAN)
432
			*value = rdev->config.cayman.max_shader_engines;
433
		else if (rdev->family >= CHIP_CEDAR)
434
			*value = rdev->config.evergreen.num_ses;
435
		else
436
			*value = 1;
437 438
		break;
	case RADEON_INFO_MAX_SH_PER_SE:
439 440 441
		if (rdev->family >= CHIP_BONAIRE)
			*value = rdev->config.cik.max_sh_per_se;
		else if (rdev->family >= CHIP_TAHITI)
442
			*value = rdev->config.si.max_sh_per_se;
443 444 445
		else
			return -EINVAL;
		break;
446
	case RADEON_INFO_FASTFB_WORKING:
447
		*value = rdev->fastfb_working;
448
		break;
449
	case RADEON_INFO_RING_WORKING:
D
Daniel Vetter 已提交
450
		if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
451 452 453 454
			DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
			return -EFAULT;
		}
		switch (*value) {
455 456
		case RADEON_CS_RING_GFX:
		case RADEON_CS_RING_COMPUTE:
457
			*value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
458 459
			break;
		case RADEON_CS_RING_DMA:
460 461
			*value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
			*value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
462 463
			break;
		case RADEON_CS_RING_UVD:
464
			*value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
465
			break;
466 467 468
		case RADEON_CS_RING_VCE:
			*value = rdev->ring[TN_RING_TYPE_VCE1_INDEX].ready;
			break;
469 470 471 472
		default:
			return -EINVAL;
		}
		break;
473
	case RADEON_INFO_SI_TILE_MODE_ARRAY:
474
		if (rdev->family >= CHIP_BONAIRE) {
475 476 477 478 479 480 481
			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");
482 483
			return -EINVAL;
		}
484
		break;
485 486 487 488 489 490 491 492 493
	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;
494 495 496
	case RADEON_INFO_SI_CP_DMA_COMPUTE:
		*value = 1;
		break;
497 498 499 500 501 502 503 504 505
	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;
506 507 508 509 510 511 512
	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;
513 514 515 516 517 518
	case RADEON_INFO_VCE_FW_VERSION:
		*value = rdev->vce.fw_version;
		break;
	case RADEON_INFO_VCE_FB_VERSION:
		*value = rdev->vce.fb_version;
		break;
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
	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;
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	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;
550 551 552 553 554 555 556
	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;
557 558 559 560 561 562 563 564 565 566 567 568 569 570
	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;
571 572 573 574 575 576 577 578
	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;
579 580 581
	case RADEON_INFO_VA_UNMAP_WORKING:
		*value = true;
		break;
582 583 584
	case RADEON_INFO_GPU_RESET_COUNTER:
		*value = atomic_read(&rdev->gpu_reset_counter);
		break;
585
	default:
586
		DRM_DEBUG_KMS("Invalid request %d\n", info->request);
587 588
		return -EINVAL;
	}
D
Daniel Vetter 已提交
589
	if (copy_to_user(value_ptr, (char*)value, value_size)) {
590
		DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
591 592 593 594 595 596 597 598 599
		return -EFAULT;
	}
	return 0;
}


/*
 * Outdated mess for old drm with Xorg being in charge (void function now).
 */
A
Alex Deucher 已提交
600 601 602 603 604 605 606
/**
 * radeon_driver_firstopen_kms - drm callback for last close
 *
 * @dev: drm dev pointer
 *
 * Switch vga switcheroo state after last close (all asics).
 */
607 608
void radeon_driver_lastclose_kms(struct drm_device *dev)
{
609
	vga_switcheroo_process_delayed_switch();
610 611
}

A
Alex Deucher 已提交
612 613 614 615 616 617 618 619 620
/**
 * 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.
 */
621 622
int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
{
623
	struct radeon_device *rdev = dev->dev_private;
624
	int r;
625 626 627

	file_priv->driver_priv = NULL;

628 629 630 631
	r = pm_runtime_get_sync(dev->dev);
	if (r < 0)
		return r;

632 633 634
	/* new gpu have virtual address space support */
	if (rdev->family >= CHIP_CAYMAN) {
		struct radeon_fpriv *fpriv;
635
		struct radeon_vm *vm;
636 637 638 639 640 641 642
		int r;

		fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
		if (unlikely(!fpriv)) {
			return -ENOMEM;
		}

643
		if (rdev->accel_working) {
644 645 646 647 648 649 650
			vm = &fpriv->vm;
			r = radeon_vm_init(rdev, vm);
			if (r) {
				kfree(fpriv);
				return r;
			}

651 652
			r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
			if (r) {
653
				radeon_vm_fini(rdev, vm);
654 655 656
				kfree(fpriv);
				return r;
			}
657

658 659
			/* map the ib pool buffer read only into
			 * virtual address space */
660 661 662 663
			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,
664 665 666
						  RADEON_VM_PAGE_READABLE |
						  RADEON_VM_PAGE_SNOOPED);
			if (r) {
667
				radeon_vm_fini(rdev, vm);
668 669 670
				kfree(fpriv);
				return r;
			}
671 672 673
		}
		file_priv->driver_priv = fpriv;
	}
674 675 676

	pm_runtime_mark_last_busy(dev->dev);
	pm_runtime_put_autosuspend(dev->dev);
677 678 679
	return 0;
}

A
Alex Deucher 已提交
680 681 682 683 684 685 686 687
/**
 * radeon_driver_postclose_kms - drm callback for post close
 *
 * @dev: drm dev pointer
 * @file_priv: drm file
 *
 * On device post close, tear down vm on cayman+ (all asics).
 */
688 689 690
void radeon_driver_postclose_kms(struct drm_device *dev,
				 struct drm_file *file_priv)
{
691 692 693 694 695
	struct radeon_device *rdev = dev->dev_private;

	/* new gpu have virtual address space support */
	if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
		struct radeon_fpriv *fpriv = file_priv->driver_priv;
696
		struct radeon_vm *vm = &fpriv->vm;
697 698
		int r;

699 700 701
		if (rdev->accel_working) {
			r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
			if (!r) {
702 703
				if (vm->ib_bo_va)
					radeon_vm_bo_rmv(rdev, vm->ib_bo_va);
704 705
				radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
			}
706
			radeon_vm_fini(rdev, vm);
707
		}
708 709 710 711

		kfree(fpriv);
		file_priv->driver_priv = NULL;
	}
712 713
}

A
Alex Deucher 已提交
714 715 716 717 718 719 720 721 722
/**
 * radeon_driver_preclose_kms - drm callback for pre close
 *
 * @dev: drm dev pointer
 * @file_priv: drm file
 *
 * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
 * (all asics).
 */
723 724 725
void radeon_driver_preclose_kms(struct drm_device *dev,
				struct drm_file *file_priv)
{
726 727 728
	struct radeon_device *rdev = dev->dev_private;
	if (rdev->hyperz_filp == file_priv)
		rdev->hyperz_filp = NULL;
729 730
	if (rdev->cmask_filp == file_priv)
		rdev->cmask_filp = NULL;
C
Christian König 已提交
731
	radeon_uvd_free_handles(rdev, file_priv);
732
	radeon_vce_free_handles(rdev, file_priv);
733 734 735 736 737
}

/*
 * VBlank related functions.
 */
A
Alex Deucher 已提交
738 739 740 741 742 743 744 745 746
/**
 * radeon_get_vblank_counter_kms - get frame count
 *
 * @dev: drm dev pointer
 * @crtc: crtc to get the frame count from
 *
 * Gets the frame count on the requested crtc (all asics).
 * Returns frame count on success, -EINVAL on failure.
 */
747 748
u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc)
{
749 750
	struct radeon_device *rdev = dev->dev_private;

751
	if (crtc < 0 || crtc >= rdev->num_crtc) {
752 753 754 755 756
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return -EINVAL;
	}

	return radeon_get_vblank_counter(rdev, crtc);
757 758
}

A
Alex Deucher 已提交
759 760 761 762 763 764 765 766 767
/**
 * 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.
 */
768 769
int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
{
770
	struct radeon_device *rdev = dev->dev_private;
771 772
	unsigned long irqflags;
	int r;
773

774
	if (crtc < 0 || crtc >= rdev->num_crtc) {
775 776 777 778
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return -EINVAL;
	}

779
	spin_lock_irqsave(&rdev->irq.lock, irqflags);
780
	rdev->irq.crtc_vblank_int[crtc] = true;
781 782 783
	r = radeon_irq_set(rdev);
	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
	return r;
784 785
}

A
Alex Deucher 已提交
786 787 788 789 790 791 792 793
/**
 * 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).
 */
794 795
void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
{
796
	struct radeon_device *rdev = dev->dev_private;
797
	unsigned long irqflags;
798

799
	if (crtc < 0 || crtc >= rdev->num_crtc) {
800 801 802 803
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return;
	}

804
	spin_lock_irqsave(&rdev->irq.lock, irqflags);
805 806
	rdev->irq.crtc_vblank_int[crtc] = false;
	radeon_irq_set(rdev);
807
	spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
808 809
}

A
Alex Deucher 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822
/**
 * radeon_get_vblank_timestamp_kms - get vblank timestamp
 *
 * @dev: drm dev pointer
 * @crtc: crtc to get the timestamp for
 * @max_error: max error
 * @vblank_time: time value
 * @flags: flags passed to the driver
 *
 * Gets the timestamp on the requested crtc based on the
 * scanout position.  (all asics).
 * Returns postive status flags on success, negative error on failure.
 */
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
				    int *max_error,
				    struct timeval *vblank_time,
				    unsigned flags)
{
	struct drm_crtc *drmcrtc;
	struct radeon_device *rdev = dev->dev_private;

	if (crtc < 0 || crtc >= dev->num_crtcs) {
		DRM_ERROR("Invalid crtc %d\n", crtc);
		return -EINVAL;
	}

	/* Get associated drm_crtc: */
	drmcrtc = &rdev->mode_info.crtcs[crtc]->base;
838 839
	if (!drmcrtc)
		return -EINVAL;
840 841 842 843

	/* Helper routine in DRM core does all the work: */
	return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
						     vblank_time, flags,
844
						     &drmcrtc->hwmode);
845
}
846 847

#define KMS_INVALID_IOCTL(name)						\
848 849
static int name(struct drm_device *dev, void *data, struct drm_file	\
		*file_priv)						\
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
{									\
	DRM_ERROR("invalid ioctl with kms %s\n", __func__);		\
	return -EINVAL;							\
}

/*
 * All these ioctls are invalid in kms world.
 */
KMS_INVALID_IOCTL(radeon_cp_init_kms)
KMS_INVALID_IOCTL(radeon_cp_start_kms)
KMS_INVALID_IOCTL(radeon_cp_stop_kms)
KMS_INVALID_IOCTL(radeon_cp_reset_kms)
KMS_INVALID_IOCTL(radeon_cp_idle_kms)
KMS_INVALID_IOCTL(radeon_cp_resume_kms)
KMS_INVALID_IOCTL(radeon_engine_reset_kms)
KMS_INVALID_IOCTL(radeon_fullscreen_kms)
KMS_INVALID_IOCTL(radeon_cp_swap_kms)
KMS_INVALID_IOCTL(radeon_cp_clear_kms)
KMS_INVALID_IOCTL(radeon_cp_vertex_kms)
KMS_INVALID_IOCTL(radeon_cp_indices_kms)
KMS_INVALID_IOCTL(radeon_cp_texture_kms)
KMS_INVALID_IOCTL(radeon_cp_stipple_kms)
KMS_INVALID_IOCTL(radeon_cp_indirect_kms)
KMS_INVALID_IOCTL(radeon_cp_vertex2_kms)
KMS_INVALID_IOCTL(radeon_cp_cmdbuf_kms)
KMS_INVALID_IOCTL(radeon_cp_getparam_kms)
KMS_INVALID_IOCTL(radeon_cp_flip_kms)
KMS_INVALID_IOCTL(radeon_mem_alloc_kms)
KMS_INVALID_IOCTL(radeon_mem_free_kms)
KMS_INVALID_IOCTL(radeon_mem_init_heap_kms)
KMS_INVALID_IOCTL(radeon_irq_emit_kms)
KMS_INVALID_IOCTL(radeon_irq_wait_kms)
KMS_INVALID_IOCTL(radeon_cp_setparam_kms)
KMS_INVALID_IOCTL(radeon_surface_alloc_kms)
KMS_INVALID_IOCTL(radeon_surface_free_kms)


R
Rob Clark 已提交
887
const struct drm_ioctl_desc radeon_ioctls_kms[] = {
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 914
	DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, radeon_cp_init_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_START, radeon_cp_start_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, radeon_cp_stop_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, radeon_cp_reset_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, radeon_cp_idle_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, radeon_cp_resume_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_RESET, radeon_engine_reset_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, radeon_fullscreen_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SWAP, radeon_cp_swap_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CLEAR, radeon_cp_clear_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_VERTEX, radeon_cp_vertex_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INDICES, radeon_cp_indices_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, radeon_cp_texture_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, radeon_cp_stipple_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, radeon_cp_indirect_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, radeon_cp_vertex2_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, radeon_cp_cmdbuf_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, radeon_cp_getparam_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FLIP, radeon_cp_flip_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_ALLOC, radeon_mem_alloc_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_FREE, radeon_mem_free_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, radeon_mem_init_heap_kms, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
	DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, radeon_irq_emit_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, radeon_irq_wait_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, radeon_cp_setparam_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, radeon_surface_alloc_kms, DRM_AUTH),
	DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, radeon_surface_free_kms, DRM_AUTH),
915
	/* KMS */
916 917 918 919
	DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
920 921
	DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH|DRM_UNLOCKED),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH|DRM_UNLOCKED),
922 923 924 925 926 927 928
	DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
	DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
929
	DRM_IOCTL_DEF_DRV(RADEON_GEM_OP, radeon_gem_op_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
930
	DRM_IOCTL_DEF_DRV(RADEON_GEM_USERPTR, radeon_gem_userptr_ioctl, DRM_AUTH|DRM_UNLOCKED|DRM_RENDER_ALLOW),
931
};
932
int radeon_max_kms_ioctl = ARRAY_SIZE(radeon_ioctls_kms);