Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_kms.c revision 1.1.1.2
      1 /*	$NetBSD: amdgpu_kms.c,v 1.1.1.2 2021/12/18 20:11:09 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2008 Advanced Micro Devices, Inc.
      5  * Copyright 2008 Red Hat Inc.
      6  * Copyright 2009 Jerome Glisse.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the "Software"),
     10  * to deal in the Software without restriction, including without limitation
     11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  * and/or sell copies of the Software, and to permit persons to whom the
     13  * Software is furnished to do so, subject to the following conditions:
     14  *
     15  * The above copyright notice and this permission notice shall be included in
     16  * all copies or substantial portions of the Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  * OTHER DEALINGS IN THE SOFTWARE.
     25  *
     26  * Authors: Dave Airlie
     27  *          Alex Deucher
     28  *          Jerome Glisse
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: amdgpu_kms.c,v 1.1.1.2 2021/12/18 20:11:09 riastradh Exp $");
     33 
     34 #include "amdgpu.h"
     35 #include <drm/drm_debugfs.h>
     36 #include <drm/amdgpu_drm.h>
     37 #include "amdgpu_sched.h"
     38 #include "amdgpu_uvd.h"
     39 #include "amdgpu_vce.h"
     40 #include "atom.h"
     41 
     42 #include <linux/vga_switcheroo.h>
     43 #include <linux/slab.h>
     44 #include <linux/uaccess.h>
     45 #include <linux/pci.h>
     46 #include <linux/pm_runtime.h>
     47 #include "amdgpu_amdkfd.h"
     48 #include "amdgpu_gem.h"
     49 #include "amdgpu_display.h"
     50 #include "amdgpu_ras.h"
     51 
     52 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev)
     53 {
     54 	struct amdgpu_gpu_instance *gpu_instance;
     55 	int i;
     56 
     57 	mutex_lock(&mgpu_info.mutex);
     58 
     59 	for (i = 0; i < mgpu_info.num_gpu; i++) {
     60 		gpu_instance = &(mgpu_info.gpu_ins[i]);
     61 		if (gpu_instance->adev == adev) {
     62 			mgpu_info.gpu_ins[i] =
     63 				mgpu_info.gpu_ins[mgpu_info.num_gpu - 1];
     64 			mgpu_info.num_gpu--;
     65 			if (adev->flags & AMD_IS_APU)
     66 				mgpu_info.num_apu--;
     67 			else
     68 				mgpu_info.num_dgpu--;
     69 			break;
     70 		}
     71 	}
     72 
     73 	mutex_unlock(&mgpu_info.mutex);
     74 }
     75 
     76 /**
     77  * amdgpu_driver_unload_kms - Main unload function for KMS.
     78  *
     79  * @dev: drm dev pointer
     80  *
     81  * This is the main unload function for KMS (all asics).
     82  * Returns 0 on success.
     83  */
     84 void amdgpu_driver_unload_kms(struct drm_device *dev)
     85 {
     86 	struct amdgpu_device *adev = dev->dev_private;
     87 
     88 	if (adev == NULL)
     89 		return;
     90 
     91 	amdgpu_unregister_gpu_instance(adev);
     92 
     93 	if (adev->rmmio == NULL)
     94 		goto done_free;
     95 
     96 	if (amdgpu_sriov_vf(adev))
     97 		amdgpu_virt_request_full_gpu(adev, false);
     98 
     99 	if (adev->runpm) {
    100 		pm_runtime_get_sync(dev->dev);
    101 		pm_runtime_forbid(dev->dev);
    102 	}
    103 
    104 	amdgpu_acpi_fini(adev);
    105 
    106 	amdgpu_device_fini(adev);
    107 
    108 done_free:
    109 	kfree(adev);
    110 	dev->dev_private = NULL;
    111 }
    112 
    113 void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
    114 {
    115 	struct amdgpu_gpu_instance *gpu_instance;
    116 
    117 	mutex_lock(&mgpu_info.mutex);
    118 
    119 	if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) {
    120 		DRM_ERROR("Cannot register more gpu instance\n");
    121 		mutex_unlock(&mgpu_info.mutex);
    122 		return;
    123 	}
    124 
    125 	gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]);
    126 	gpu_instance->adev = adev;
    127 	gpu_instance->mgpu_fan_enabled = 0;
    128 
    129 	mgpu_info.num_gpu++;
    130 	if (adev->flags & AMD_IS_APU)
    131 		mgpu_info.num_apu++;
    132 	else
    133 		mgpu_info.num_dgpu++;
    134 
    135 	mutex_unlock(&mgpu_info.mutex);
    136 }
    137 
    138 /**
    139  * amdgpu_driver_load_kms - Main load function for KMS.
    140  *
    141  * @dev: drm dev pointer
    142  * @flags: device flags
    143  *
    144  * This is the main load function for KMS (all asics).
    145  * Returns 0 on success, error on failure.
    146  */
    147 int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
    148 {
    149 	struct amdgpu_device *adev;
    150 	int r, acpi_status;
    151 
    152 	adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
    153 	if (adev == NULL) {
    154 		return -ENOMEM;
    155 	}
    156 	dev->dev_private = (void *)adev;
    157 
    158 	if (amdgpu_has_atpx() &&
    159 	    (amdgpu_is_atpx_hybrid() ||
    160 	     amdgpu_has_atpx_dgpu_power_cntl()) &&
    161 	    ((flags & AMD_IS_APU) == 0) &&
    162 	    !pci_is_thunderbolt_attached(dev->pdev))
    163 		flags |= AMD_IS_PX;
    164 
    165 	/* amdgpu_device_init should report only fatal error
    166 	 * like memory allocation failure or iomapping failure,
    167 	 * or memory manager initialization failure, it must
    168 	 * properly initialize the GPU MC controller and permit
    169 	 * VRAM allocation
    170 	 */
    171 	r = amdgpu_device_init(adev, dev, dev->pdev, flags);
    172 	if (r) {
    173 		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
    174 		goto out;
    175 	}
    176 
    177 	if (amdgpu_device_supports_boco(dev) &&
    178 	    (amdgpu_runtime_pm != 0)) /* enable runpm by default */
    179 		adev->runpm = true;
    180 	else if (amdgpu_device_supports_baco(dev) &&
    181 		 (amdgpu_runtime_pm > 0)) /* enable runpm if runpm=1 */
    182 		adev->runpm = true;
    183 
    184 	/* Call ACPI methods: require modeset init
    185 	 * but failure is not fatal
    186 	 */
    187 	if (!r) {
    188 		acpi_status = amdgpu_acpi_init(adev);
    189 		if (acpi_status)
    190 			dev_dbg(&dev->pdev->dev,
    191 				"Error during ACPI methods call\n");
    192 	}
    193 
    194 	if (adev->runpm) {
    195 		dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP);
    196 		pm_runtime_use_autosuspend(dev->dev);
    197 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
    198 		pm_runtime_set_active(dev->dev);
    199 		pm_runtime_allow(dev->dev);
    200 		pm_runtime_mark_last_busy(dev->dev);
    201 		pm_runtime_put_autosuspend(dev->dev);
    202 	}
    203 
    204 out:
    205 	if (r) {
    206 		/* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
    207 		if (adev->rmmio && adev->runpm)
    208 			pm_runtime_put_noidle(dev->dev);
    209 		amdgpu_driver_unload_kms(dev);
    210 	}
    211 
    212 	return r;
    213 }
    214 
    215 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
    216 				struct drm_amdgpu_query_fw *query_fw,
    217 				struct amdgpu_device *adev)
    218 {
    219 	switch (query_fw->fw_type) {
    220 	case AMDGPU_INFO_FW_VCE:
    221 		fw_info->ver = adev->vce.fw_version;
    222 		fw_info->feature = adev->vce.fb_version;
    223 		break;
    224 	case AMDGPU_INFO_FW_UVD:
    225 		fw_info->ver = adev->uvd.fw_version;
    226 		fw_info->feature = 0;
    227 		break;
    228 	case AMDGPU_INFO_FW_VCN:
    229 		fw_info->ver = adev->vcn.fw_version;
    230 		fw_info->feature = 0;
    231 		break;
    232 	case AMDGPU_INFO_FW_GMC:
    233 		fw_info->ver = adev->gmc.fw_version;
    234 		fw_info->feature = 0;
    235 		break;
    236 	case AMDGPU_INFO_FW_GFX_ME:
    237 		fw_info->ver = adev->gfx.me_fw_version;
    238 		fw_info->feature = adev->gfx.me_feature_version;
    239 		break;
    240 	case AMDGPU_INFO_FW_GFX_PFP:
    241 		fw_info->ver = adev->gfx.pfp_fw_version;
    242 		fw_info->feature = adev->gfx.pfp_feature_version;
    243 		break;
    244 	case AMDGPU_INFO_FW_GFX_CE:
    245 		fw_info->ver = adev->gfx.ce_fw_version;
    246 		fw_info->feature = adev->gfx.ce_feature_version;
    247 		break;
    248 	case AMDGPU_INFO_FW_GFX_RLC:
    249 		fw_info->ver = adev->gfx.rlc_fw_version;
    250 		fw_info->feature = adev->gfx.rlc_feature_version;
    251 		break;
    252 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
    253 		fw_info->ver = adev->gfx.rlc_srlc_fw_version;
    254 		fw_info->feature = adev->gfx.rlc_srlc_feature_version;
    255 		break;
    256 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
    257 		fw_info->ver = adev->gfx.rlc_srlg_fw_version;
    258 		fw_info->feature = adev->gfx.rlc_srlg_feature_version;
    259 		break;
    260 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
    261 		fw_info->ver = adev->gfx.rlc_srls_fw_version;
    262 		fw_info->feature = adev->gfx.rlc_srls_feature_version;
    263 		break;
    264 	case AMDGPU_INFO_FW_GFX_MEC:
    265 		if (query_fw->index == 0) {
    266 			fw_info->ver = adev->gfx.mec_fw_version;
    267 			fw_info->feature = adev->gfx.mec_feature_version;
    268 		} else if (query_fw->index == 1) {
    269 			fw_info->ver = adev->gfx.mec2_fw_version;
    270 			fw_info->feature = adev->gfx.mec2_feature_version;
    271 		} else
    272 			return -EINVAL;
    273 		break;
    274 	case AMDGPU_INFO_FW_SMC:
    275 		fw_info->ver = adev->pm.fw_version;
    276 		fw_info->feature = 0;
    277 		break;
    278 	case AMDGPU_INFO_FW_TA:
    279 		if (query_fw->index > 1)
    280 			return -EINVAL;
    281 		if (query_fw->index == 0) {
    282 			fw_info->ver = adev->psp.ta_fw_version;
    283 			fw_info->feature = adev->psp.ta_xgmi_ucode_version;
    284 		} else {
    285 			fw_info->ver = adev->psp.ta_fw_version;
    286 			fw_info->feature = adev->psp.ta_ras_ucode_version;
    287 		}
    288 		break;
    289 	case AMDGPU_INFO_FW_SDMA:
    290 		if (query_fw->index >= adev->sdma.num_instances)
    291 			return -EINVAL;
    292 		fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
    293 		fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
    294 		break;
    295 	case AMDGPU_INFO_FW_SOS:
    296 		fw_info->ver = adev->psp.sos_fw_version;
    297 		fw_info->feature = adev->psp.sos_feature_version;
    298 		break;
    299 	case AMDGPU_INFO_FW_ASD:
    300 		fw_info->ver = adev->psp.asd_fw_version;
    301 		fw_info->feature = adev->psp.asd_feature_version;
    302 		break;
    303 	case AMDGPU_INFO_FW_DMCU:
    304 		fw_info->ver = adev->dm.dmcu_fw_version;
    305 		fw_info->feature = 0;
    306 		break;
    307 	case AMDGPU_INFO_FW_DMCUB:
    308 		fw_info->ver = adev->dm.dmcub_fw_version;
    309 		fw_info->feature = 0;
    310 		break;
    311 	default:
    312 		return -EINVAL;
    313 	}
    314 	return 0;
    315 }
    316 
    317 static int amdgpu_hw_ip_info(struct amdgpu_device *adev,
    318 			     struct drm_amdgpu_info *info,
    319 			     struct drm_amdgpu_info_hw_ip *result)
    320 {
    321 	uint32_t ib_start_alignment = 0;
    322 	uint32_t ib_size_alignment = 0;
    323 	enum amd_ip_block_type type;
    324 	unsigned int num_rings = 0;
    325 	unsigned int i, j;
    326 
    327 	if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
    328 		return -EINVAL;
    329 
    330 	switch (info->query_hw_ip.type) {
    331 	case AMDGPU_HW_IP_GFX:
    332 		type = AMD_IP_BLOCK_TYPE_GFX;
    333 		for (i = 0; i < adev->gfx.num_gfx_rings; i++)
    334 			if (adev->gfx.gfx_ring[i].sched.ready)
    335 				++num_rings;
    336 		ib_start_alignment = 32;
    337 		ib_size_alignment = 32;
    338 		break;
    339 	case AMDGPU_HW_IP_COMPUTE:
    340 		type = AMD_IP_BLOCK_TYPE_GFX;
    341 		for (i = 0; i < adev->gfx.num_compute_rings; i++)
    342 			if (adev->gfx.compute_ring[i].sched.ready)
    343 				++num_rings;
    344 		ib_start_alignment = 32;
    345 		ib_size_alignment = 32;
    346 		break;
    347 	case AMDGPU_HW_IP_DMA:
    348 		type = AMD_IP_BLOCK_TYPE_SDMA;
    349 		for (i = 0; i < adev->sdma.num_instances; i++)
    350 			if (adev->sdma.instance[i].ring.sched.ready)
    351 				++num_rings;
    352 		ib_start_alignment = 256;
    353 		ib_size_alignment = 4;
    354 		break;
    355 	case AMDGPU_HW_IP_UVD:
    356 		type = AMD_IP_BLOCK_TYPE_UVD;
    357 		for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
    358 			if (adev->uvd.harvest_config & (1 << i))
    359 				continue;
    360 
    361 			if (adev->uvd.inst[i].ring.sched.ready)
    362 				++num_rings;
    363 		}
    364 		ib_start_alignment = 64;
    365 		ib_size_alignment = 64;
    366 		break;
    367 	case AMDGPU_HW_IP_VCE:
    368 		type = AMD_IP_BLOCK_TYPE_VCE;
    369 		for (i = 0; i < adev->vce.num_rings; i++)
    370 			if (adev->vce.ring[i].sched.ready)
    371 				++num_rings;
    372 		ib_start_alignment = 4;
    373 		ib_size_alignment = 1;
    374 		break;
    375 	case AMDGPU_HW_IP_UVD_ENC:
    376 		type = AMD_IP_BLOCK_TYPE_UVD;
    377 		for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
    378 			if (adev->uvd.harvest_config & (1 << i))
    379 				continue;
    380 
    381 			for (j = 0; j < adev->uvd.num_enc_rings; j++)
    382 				if (adev->uvd.inst[i].ring_enc[j].sched.ready)
    383 					++num_rings;
    384 		}
    385 		ib_start_alignment = 64;
    386 		ib_size_alignment = 64;
    387 		break;
    388 	case AMDGPU_HW_IP_VCN_DEC:
    389 		type = AMD_IP_BLOCK_TYPE_VCN;
    390 		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
    391 			if (adev->uvd.harvest_config & (1 << i))
    392 				continue;
    393 
    394 			if (adev->vcn.inst[i].ring_dec.sched.ready)
    395 				++num_rings;
    396 		}
    397 		ib_start_alignment = 16;
    398 		ib_size_alignment = 16;
    399 		break;
    400 	case AMDGPU_HW_IP_VCN_ENC:
    401 		type = AMD_IP_BLOCK_TYPE_VCN;
    402 		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
    403 			if (adev->uvd.harvest_config & (1 << i))
    404 				continue;
    405 
    406 			for (j = 0; j < adev->vcn.num_enc_rings; j++)
    407 				if (adev->vcn.inst[i].ring_enc[j].sched.ready)
    408 					++num_rings;
    409 		}
    410 		ib_start_alignment = 64;
    411 		ib_size_alignment = 1;
    412 		break;
    413 	case AMDGPU_HW_IP_VCN_JPEG:
    414 		type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
    415 			AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
    416 
    417 		for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
    418 			if (adev->jpeg.harvest_config & (1 << i))
    419 				continue;
    420 
    421 			if (adev->jpeg.inst[i].ring_dec.sched.ready)
    422 				++num_rings;
    423 		}
    424 		ib_start_alignment = 16;
    425 		ib_size_alignment = 16;
    426 		break;
    427 	default:
    428 		return -EINVAL;
    429 	}
    430 
    431 	for (i = 0; i < adev->num_ip_blocks; i++)
    432 		if (adev->ip_blocks[i].version->type == type &&
    433 		    adev->ip_blocks[i].status.valid)
    434 			break;
    435 
    436 	if (i == adev->num_ip_blocks)
    437 		return 0;
    438 
    439 	num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type],
    440 			num_rings);
    441 
    442 	result->hw_ip_version_major = adev->ip_blocks[i].version->major;
    443 	result->hw_ip_version_minor = adev->ip_blocks[i].version->minor;
    444 	result->capabilities_flags = 0;
    445 	result->available_rings = (1 << num_rings) - 1;
    446 	result->ib_start_alignment = ib_start_alignment;
    447 	result->ib_size_alignment = ib_size_alignment;
    448 	return 0;
    449 }
    450 
    451 /*
    452  * Userspace get information ioctl
    453  */
    454 /**
    455  * amdgpu_info_ioctl - answer a device specific request.
    456  *
    457  * @adev: amdgpu device pointer
    458  * @data: request object
    459  * @filp: drm filp
    460  *
    461  * This function is used to pass device specific parameters to the userspace
    462  * drivers.  Examples include: pci device id, pipeline parms, tiling params,
    463  * etc. (all asics).
    464  * Returns 0 on success, -EINVAL on failure.
    465  */
    466 static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
    467 {
    468 	struct amdgpu_device *adev = dev->dev_private;
    469 	struct drm_amdgpu_info *info = data;
    470 	struct amdgpu_mode_info *minfo = &adev->mode_info;
    471 	void __user *out = (void __user *)(uintptr_t)info->return_pointer;
    472 	uint32_t size = info->return_size;
    473 	struct drm_crtc *crtc;
    474 	uint32_t ui32 = 0;
    475 	uint64_t ui64 = 0;
    476 	int i, found;
    477 	int ui32_size = sizeof(ui32);
    478 
    479 	if (!info->return_size || !info->return_pointer)
    480 		return -EINVAL;
    481 
    482 	switch (info->query) {
    483 	case AMDGPU_INFO_ACCEL_WORKING:
    484 		ui32 = adev->accel_working;
    485 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
    486 	case AMDGPU_INFO_CRTC_FROM_ID:
    487 		for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
    488 			crtc = (struct drm_crtc *)minfo->crtcs[i];
    489 			if (crtc && crtc->base.id == info->mode_crtc.id) {
    490 				struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
    491 				ui32 = amdgpu_crtc->crtc_id;
    492 				found = 1;
    493 				break;
    494 			}
    495 		}
    496 		if (!found) {
    497 			DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
    498 			return -EINVAL;
    499 		}
    500 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
    501 	case AMDGPU_INFO_HW_IP_INFO: {
    502 		struct drm_amdgpu_info_hw_ip ip = {};
    503 		int ret;
    504 
    505 		ret = amdgpu_hw_ip_info(adev, info, &ip);
    506 		if (ret)
    507 			return ret;
    508 
    509 		ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip)));
    510 		return ret ? -EFAULT : 0;
    511 	}
    512 	case AMDGPU_INFO_HW_IP_COUNT: {
    513 		enum amd_ip_block_type type;
    514 		uint32_t count = 0;
    515 
    516 		switch (info->query_hw_ip.type) {
    517 		case AMDGPU_HW_IP_GFX:
    518 			type = AMD_IP_BLOCK_TYPE_GFX;
    519 			break;
    520 		case AMDGPU_HW_IP_COMPUTE:
    521 			type = AMD_IP_BLOCK_TYPE_GFX;
    522 			break;
    523 		case AMDGPU_HW_IP_DMA:
    524 			type = AMD_IP_BLOCK_TYPE_SDMA;
    525 			break;
    526 		case AMDGPU_HW_IP_UVD:
    527 			type = AMD_IP_BLOCK_TYPE_UVD;
    528 			break;
    529 		case AMDGPU_HW_IP_VCE:
    530 			type = AMD_IP_BLOCK_TYPE_VCE;
    531 			break;
    532 		case AMDGPU_HW_IP_UVD_ENC:
    533 			type = AMD_IP_BLOCK_TYPE_UVD;
    534 			break;
    535 		case AMDGPU_HW_IP_VCN_DEC:
    536 		case AMDGPU_HW_IP_VCN_ENC:
    537 			type = AMD_IP_BLOCK_TYPE_VCN;
    538 			break;
    539 		case AMDGPU_HW_IP_VCN_JPEG:
    540 			type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
    541 				AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
    542 			break;
    543 		default:
    544 			return -EINVAL;
    545 		}
    546 
    547 		for (i = 0; i < adev->num_ip_blocks; i++)
    548 			if (adev->ip_blocks[i].version->type == type &&
    549 			    adev->ip_blocks[i].status.valid &&
    550 			    count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
    551 				count++;
    552 
    553 		return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
    554 	}
    555 	case AMDGPU_INFO_TIMESTAMP:
    556 		ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
    557 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    558 	case AMDGPU_INFO_FW_VERSION: {
    559 		struct drm_amdgpu_info_firmware fw_info;
    560 		int ret;
    561 
    562 		/* We only support one instance of each IP block right now. */
    563 		if (info->query_fw.ip_instance != 0)
    564 			return -EINVAL;
    565 
    566 		ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
    567 		if (ret)
    568 			return ret;
    569 
    570 		return copy_to_user(out, &fw_info,
    571 				    min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
    572 	}
    573 	case AMDGPU_INFO_NUM_BYTES_MOVED:
    574 		ui64 = atomic64_read(&adev->num_bytes_moved);
    575 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    576 	case AMDGPU_INFO_NUM_EVICTIONS:
    577 		ui64 = atomic64_read(&adev->num_evictions);
    578 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    579 	case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
    580 		ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
    581 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    582 	case AMDGPU_INFO_VRAM_USAGE:
    583 		ui64 = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
    584 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    585 	case AMDGPU_INFO_VIS_VRAM_USAGE:
    586 		ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
    587 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    588 	case AMDGPU_INFO_GTT_USAGE:
    589 		ui64 = amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
    590 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
    591 	case AMDGPU_INFO_GDS_CONFIG: {
    592 		struct drm_amdgpu_info_gds gds_info;
    593 
    594 		memset(&gds_info, 0, sizeof(gds_info));
    595 		gds_info.compute_partition_size = adev->gds.gds_size;
    596 		gds_info.gds_total_size = adev->gds.gds_size;
    597 		gds_info.gws_per_compute_partition = adev->gds.gws_size;
    598 		gds_info.oa_per_compute_partition = adev->gds.oa_size;
    599 		return copy_to_user(out, &gds_info,
    600 				    min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
    601 	}
    602 	case AMDGPU_INFO_VRAM_GTT: {
    603 		struct drm_amdgpu_info_vram_gtt vram_gtt;
    604 
    605 		vram_gtt.vram_size = adev->gmc.real_vram_size -
    606 			atomic64_read(&adev->vram_pin_size) -
    607 			AMDGPU_VM_RESERVED_VRAM;
    608 		vram_gtt.vram_cpu_accessible_size =
    609 			min(adev->gmc.visible_vram_size -
    610 			    atomic64_read(&adev->visible_pin_size),
    611 			    vram_gtt.vram_size);
    612 		vram_gtt.gtt_size = adev->mman.bdev.man[TTM_PL_TT].size;
    613 		vram_gtt.gtt_size *= PAGE_SIZE;
    614 		vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
    615 		return copy_to_user(out, &vram_gtt,
    616 				    min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
    617 	}
    618 	case AMDGPU_INFO_MEMORY: {
    619 		struct drm_amdgpu_memory_info mem;
    620 
    621 		memset(&mem, 0, sizeof(mem));
    622 		mem.vram.total_heap_size = adev->gmc.real_vram_size;
    623 		mem.vram.usable_heap_size = adev->gmc.real_vram_size -
    624 			atomic64_read(&adev->vram_pin_size) -
    625 			AMDGPU_VM_RESERVED_VRAM;
    626 		mem.vram.heap_usage =
    627 			amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
    628 		mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
    629 
    630 		mem.cpu_accessible_vram.total_heap_size =
    631 			adev->gmc.visible_vram_size;
    632 		mem.cpu_accessible_vram.usable_heap_size =
    633 			min(adev->gmc.visible_vram_size -
    634 			    atomic64_read(&adev->visible_pin_size),
    635 			    mem.vram.usable_heap_size);
    636 		mem.cpu_accessible_vram.heap_usage =
    637 			amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
    638 		mem.cpu_accessible_vram.max_allocation =
    639 			mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
    640 
    641 		mem.gtt.total_heap_size = adev->mman.bdev.man[TTM_PL_TT].size;
    642 		mem.gtt.total_heap_size *= PAGE_SIZE;
    643 		mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
    644 			atomic64_read(&adev->gart_pin_size);
    645 		mem.gtt.heap_usage =
    646 			amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
    647 		mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
    648 
    649 		return copy_to_user(out, &mem,
    650 				    min((size_t)size, sizeof(mem)))
    651 				    ? -EFAULT : 0;
    652 	}
    653 	case AMDGPU_INFO_READ_MMR_REG: {
    654 		unsigned n, alloc_size;
    655 		uint32_t *regs;
    656 		unsigned se_num = (info->read_mmr_reg.instance >>
    657 				   AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
    658 				  AMDGPU_INFO_MMR_SE_INDEX_MASK;
    659 		unsigned sh_num = (info->read_mmr_reg.instance >>
    660 				   AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
    661 				  AMDGPU_INFO_MMR_SH_INDEX_MASK;
    662 
    663 		/* set full masks if the userspace set all bits
    664 		 * in the bitfields */
    665 		if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
    666 			se_num = 0xffffffff;
    667 		if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
    668 			sh_num = 0xffffffff;
    669 
    670 		if (info->read_mmr_reg.count > 128)
    671 			return -EINVAL;
    672 
    673 		regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
    674 		if (!regs)
    675 			return -ENOMEM;
    676 		alloc_size = info->read_mmr_reg.count * sizeof(*regs);
    677 
    678 		amdgpu_gfx_off_ctrl(adev, false);
    679 		for (i = 0; i < info->read_mmr_reg.count; i++) {
    680 			if (amdgpu_asic_read_register(adev, se_num, sh_num,
    681 						      info->read_mmr_reg.dword_offset + i,
    682 						      &regs[i])) {
    683 				DRM_DEBUG_KMS("unallowed offset %#x\n",
    684 					      info->read_mmr_reg.dword_offset + i);
    685 				kfree(regs);
    686 				amdgpu_gfx_off_ctrl(adev, true);
    687 				return -EFAULT;
    688 			}
    689 		}
    690 		amdgpu_gfx_off_ctrl(adev, true);
    691 		n = copy_to_user(out, regs, min(size, alloc_size));
    692 		kfree(regs);
    693 		return n ? -EFAULT : 0;
    694 	}
    695 	case AMDGPU_INFO_DEV_INFO: {
    696 		struct drm_amdgpu_info_device dev_info = {};
    697 		uint64_t vm_size;
    698 
    699 		dev_info.device_id = dev->pdev->device;
    700 		dev_info.chip_rev = adev->rev_id;
    701 		dev_info.external_rev = adev->external_rev_id;
    702 		dev_info.pci_rev = dev->pdev->revision;
    703 		dev_info.family = adev->family;
    704 		dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
    705 		dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
    706 		/* return all clocks in KHz */
    707 		dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
    708 		if (adev->pm.dpm_enabled) {
    709 			dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
    710 			dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
    711 		} else {
    712 			dev_info.max_engine_clock = adev->clock.default_sclk * 10;
    713 			dev_info.max_memory_clock = adev->clock.default_mclk * 10;
    714 		}
    715 		dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
    716 		dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
    717 			adev->gfx.config.max_shader_engines;
    718 		dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
    719 		dev_info._pad = 0;
    720 		dev_info.ids_flags = 0;
    721 		if (adev->flags & AMD_IS_APU)
    722 			dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
    723 		if (amdgpu_mcbp || amdgpu_sriov_vf(adev))
    724 			dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
    725 
    726 		vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
    727 		vm_size -= AMDGPU_VA_RESERVED_SIZE;
    728 
    729 		/* Older VCE FW versions are buggy and can handle only 40bits */
    730 		if (adev->vce.fw_version &&
    731 		    adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
    732 			vm_size = min(vm_size, 1ULL << 40);
    733 
    734 		dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
    735 		dev_info.virtual_address_max =
    736 			min(vm_size, AMDGPU_GMC_HOLE_START);
    737 
    738 		if (vm_size > AMDGPU_GMC_HOLE_START) {
    739 			dev_info.high_va_offset = AMDGPU_GMC_HOLE_END;
    740 			dev_info.high_va_max = AMDGPU_GMC_HOLE_END | vm_size;
    741 		}
    742 		dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
    743 		dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
    744 		dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
    745 		dev_info.cu_active_number = adev->gfx.cu_info.number;
    746 		dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
    747 		dev_info.ce_ram_size = adev->gfx.ce_ram_size;
    748 		memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
    749 		       sizeof(adev->gfx.cu_info.ao_cu_bitmap));
    750 		memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
    751 		       sizeof(adev->gfx.cu_info.bitmap));
    752 		dev_info.vram_type = adev->gmc.vram_type;
    753 		dev_info.vram_bit_width = adev->gmc.vram_width;
    754 		dev_info.vce_harvest_config = adev->vce.harvest_config;
    755 		dev_info.gc_double_offchip_lds_buf =
    756 			adev->gfx.config.double_offchip_lds_buf;
    757 		dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size;
    758 		dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs;
    759 		dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
    760 		dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
    761 		dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
    762 		dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
    763 		dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
    764 
    765 		if (adev->family >= AMDGPU_FAMILY_NV)
    766 			dev_info.pa_sc_tile_steering_override =
    767 				adev->gfx.config.pa_sc_tile_steering_override;
    768 
    769 		dev_info.tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask;
    770 
    771 		return copy_to_user(out, &dev_info,
    772 				    min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
    773 	}
    774 	case AMDGPU_INFO_VCE_CLOCK_TABLE: {
    775 		unsigned i;
    776 		struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
    777 		struct amd_vce_state *vce_state;
    778 
    779 		for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
    780 			vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
    781 			if (vce_state) {
    782 				vce_clk_table.entries[i].sclk = vce_state->sclk;
    783 				vce_clk_table.entries[i].mclk = vce_state->mclk;
    784 				vce_clk_table.entries[i].eclk = vce_state->evclk;
    785 				vce_clk_table.num_valid_entries++;
    786 			}
    787 		}
    788 
    789 		return copy_to_user(out, &vce_clk_table,
    790 				    min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
    791 	}
    792 	case AMDGPU_INFO_VBIOS: {
    793 		uint32_t bios_size = adev->bios_size;
    794 
    795 		switch (info->vbios_info.type) {
    796 		case AMDGPU_INFO_VBIOS_SIZE:
    797 			return copy_to_user(out, &bios_size,
    798 					min((size_t)size, sizeof(bios_size)))
    799 					? -EFAULT : 0;
    800 		case AMDGPU_INFO_VBIOS_IMAGE: {
    801 			uint8_t *bios;
    802 			uint32_t bios_offset = info->vbios_info.offset;
    803 
    804 			if (bios_offset >= bios_size)
    805 				return -EINVAL;
    806 
    807 			bios = adev->bios + bios_offset;
    808 			return copy_to_user(out, bios,
    809 					    min((size_t)size, (size_t)(bios_size - bios_offset)))
    810 					? -EFAULT : 0;
    811 		}
    812 		default:
    813 			DRM_DEBUG_KMS("Invalid request %d\n",
    814 					info->vbios_info.type);
    815 			return -EINVAL;
    816 		}
    817 	}
    818 	case AMDGPU_INFO_NUM_HANDLES: {
    819 		struct drm_amdgpu_info_num_handles handle;
    820 
    821 		switch (info->query_hw_ip.type) {
    822 		case AMDGPU_HW_IP_UVD:
    823 			/* Starting Polaris, we support unlimited UVD handles */
    824 			if (adev->asic_type < CHIP_POLARIS10) {
    825 				handle.uvd_max_handles = adev->uvd.max_handles;
    826 				handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
    827 
    828 				return copy_to_user(out, &handle,
    829 					min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
    830 			} else {
    831 				return -ENODATA;
    832 			}
    833 
    834 			break;
    835 		default:
    836 			return -EINVAL;
    837 		}
    838 	}
    839 	case AMDGPU_INFO_SENSOR: {
    840 		if (!adev->pm.dpm_enabled)
    841 			return -ENOENT;
    842 
    843 		switch (info->sensor_info.type) {
    844 		case AMDGPU_INFO_SENSOR_GFX_SCLK:
    845 			/* get sclk in Mhz */
    846 			if (amdgpu_dpm_read_sensor(adev,
    847 						   AMDGPU_PP_SENSOR_GFX_SCLK,
    848 						   (void *)&ui32, &ui32_size)) {
    849 				return -EINVAL;
    850 			}
    851 			ui32 /= 100;
    852 			break;
    853 		case AMDGPU_INFO_SENSOR_GFX_MCLK:
    854 			/* get mclk in Mhz */
    855 			if (amdgpu_dpm_read_sensor(adev,
    856 						   AMDGPU_PP_SENSOR_GFX_MCLK,
    857 						   (void *)&ui32, &ui32_size)) {
    858 				return -EINVAL;
    859 			}
    860 			ui32 /= 100;
    861 			break;
    862 		case AMDGPU_INFO_SENSOR_GPU_TEMP:
    863 			/* get temperature in millidegrees C */
    864 			if (amdgpu_dpm_read_sensor(adev,
    865 						   AMDGPU_PP_SENSOR_GPU_TEMP,
    866 						   (void *)&ui32, &ui32_size)) {
    867 				return -EINVAL;
    868 			}
    869 			break;
    870 		case AMDGPU_INFO_SENSOR_GPU_LOAD:
    871 			/* get GPU load */
    872 			if (amdgpu_dpm_read_sensor(adev,
    873 						   AMDGPU_PP_SENSOR_GPU_LOAD,
    874 						   (void *)&ui32, &ui32_size)) {
    875 				return -EINVAL;
    876 			}
    877 			break;
    878 		case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
    879 			/* get average GPU power */
    880 			if (amdgpu_dpm_read_sensor(adev,
    881 						   AMDGPU_PP_SENSOR_GPU_POWER,
    882 						   (void *)&ui32, &ui32_size)) {
    883 				return -EINVAL;
    884 			}
    885 			ui32 >>= 8;
    886 			break;
    887 		case AMDGPU_INFO_SENSOR_VDDNB:
    888 			/* get VDDNB in millivolts */
    889 			if (amdgpu_dpm_read_sensor(adev,
    890 						   AMDGPU_PP_SENSOR_VDDNB,
    891 						   (void *)&ui32, &ui32_size)) {
    892 				return -EINVAL;
    893 			}
    894 			break;
    895 		case AMDGPU_INFO_SENSOR_VDDGFX:
    896 			/* get VDDGFX in millivolts */
    897 			if (amdgpu_dpm_read_sensor(adev,
    898 						   AMDGPU_PP_SENSOR_VDDGFX,
    899 						   (void *)&ui32, &ui32_size)) {
    900 				return -EINVAL;
    901 			}
    902 			break;
    903 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
    904 			/* get stable pstate sclk in Mhz */
    905 			if (amdgpu_dpm_read_sensor(adev,
    906 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
    907 						   (void *)&ui32, &ui32_size)) {
    908 				return -EINVAL;
    909 			}
    910 			ui32 /= 100;
    911 			break;
    912 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
    913 			/* get stable pstate mclk in Mhz */
    914 			if (amdgpu_dpm_read_sensor(adev,
    915 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
    916 						   (void *)&ui32, &ui32_size)) {
    917 				return -EINVAL;
    918 			}
    919 			ui32 /= 100;
    920 			break;
    921 		default:
    922 			DRM_DEBUG_KMS("Invalid request %d\n",
    923 				      info->sensor_info.type);
    924 			return -EINVAL;
    925 		}
    926 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
    927 	}
    928 	case AMDGPU_INFO_VRAM_LOST_COUNTER:
    929 		ui32 = atomic_read(&adev->vram_lost_counter);
    930 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
    931 	case AMDGPU_INFO_RAS_ENABLED_FEATURES: {
    932 		struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
    933 		uint64_t ras_mask;
    934 
    935 		if (!ras)
    936 			return -EINVAL;
    937 		ras_mask = (uint64_t)ras->supported << 32 | ras->features;
    938 
    939 		return copy_to_user(out, &ras_mask,
    940 				min_t(u64, size, sizeof(ras_mask))) ?
    941 			-EFAULT : 0;
    942 	}
    943 	default:
    944 		DRM_DEBUG_KMS("Invalid request %d\n", info->query);
    945 		return -EINVAL;
    946 	}
    947 	return 0;
    948 }
    949 
    950 
    951 /*
    952  * Outdated mess for old drm with Xorg being in charge (void function now).
    953  */
    954 /**
    955  * amdgpu_driver_lastclose_kms - drm callback for last close
    956  *
    957  * @dev: drm dev pointer
    958  *
    959  * Switch vga_switcheroo state after last close (all asics).
    960  */
    961 void amdgpu_driver_lastclose_kms(struct drm_device *dev)
    962 {
    963 	drm_fb_helper_lastclose(dev);
    964 	vga_switcheroo_process_delayed_switch();
    965 }
    966 
    967 /**
    968  * amdgpu_driver_open_kms - drm callback for open
    969  *
    970  * @dev: drm dev pointer
    971  * @file_priv: drm file
    972  *
    973  * On device open, init vm on cayman+ (all asics).
    974  * Returns 0 on success, error on failure.
    975  */
    976 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
    977 {
    978 	struct amdgpu_device *adev = dev->dev_private;
    979 	struct amdgpu_fpriv *fpriv;
    980 	int r, pasid;
    981 
    982 	/* Ensure IB tests are run on ring */
    983 	flush_delayed_work(&adev->delayed_init_work);
    984 
    985 
    986 	if (amdgpu_ras_intr_triggered()) {
    987 		DRM_ERROR("RAS Intr triggered, device disabled!!");
    988 		return -EHWPOISON;
    989 	}
    990 
    991 	file_priv->driver_priv = NULL;
    992 
    993 	r = pm_runtime_get_sync(dev->dev);
    994 	if (r < 0)
    995 		return r;
    996 
    997 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
    998 	if (unlikely(!fpriv)) {
    999 		r = -ENOMEM;
   1000 		goto out_suspend;
   1001 	}
   1002 
   1003 	pasid = amdgpu_pasid_alloc(16);
   1004 	if (pasid < 0) {
   1005 		dev_warn(adev->dev, "No more PASIDs available!");
   1006 		pasid = 0;
   1007 	}
   1008 	r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid);
   1009 	if (r)
   1010 		goto error_pasid;
   1011 
   1012 	fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
   1013 	if (!fpriv->prt_va) {
   1014 		r = -ENOMEM;
   1015 		goto error_vm;
   1016 	}
   1017 
   1018 	if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
   1019 		uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK;
   1020 
   1021 		r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj,
   1022 						&fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE);
   1023 		if (r)
   1024 			goto error_vm;
   1025 	}
   1026 
   1027 	mutex_init(&fpriv->bo_list_lock);
   1028 	idr_init(&fpriv->bo_list_handles);
   1029 
   1030 	amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
   1031 
   1032 	file_priv->driver_priv = fpriv;
   1033 	goto out_suspend;
   1034 
   1035 error_vm:
   1036 	amdgpu_vm_fini(adev, &fpriv->vm);
   1037 
   1038 error_pasid:
   1039 	if (pasid)
   1040 		amdgpu_pasid_free(pasid);
   1041 
   1042 	kfree(fpriv);
   1043 
   1044 out_suspend:
   1045 	pm_runtime_mark_last_busy(dev->dev);
   1046 	pm_runtime_put_autosuspend(dev->dev);
   1047 
   1048 	return r;
   1049 }
   1050 
   1051 /**
   1052  * amdgpu_driver_postclose_kms - drm callback for post close
   1053  *
   1054  * @dev: drm dev pointer
   1055  * @file_priv: drm file
   1056  *
   1057  * On device post close, tear down vm on cayman+ (all asics).
   1058  */
   1059 void amdgpu_driver_postclose_kms(struct drm_device *dev,
   1060 				 struct drm_file *file_priv)
   1061 {
   1062 	struct amdgpu_device *adev = dev->dev_private;
   1063 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
   1064 	struct amdgpu_bo_list *list;
   1065 	struct amdgpu_bo *pd;
   1066 	unsigned int pasid;
   1067 	int handle;
   1068 
   1069 	if (!fpriv)
   1070 		return;
   1071 
   1072 	pm_runtime_get_sync(dev->dev);
   1073 
   1074 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
   1075 		amdgpu_uvd_free_handles(adev, file_priv);
   1076 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
   1077 		amdgpu_vce_free_handles(adev, file_priv);
   1078 
   1079 	amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
   1080 
   1081 	if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
   1082 		/* TODO: how to handle reserve failure */
   1083 		BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
   1084 		amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
   1085 		fpriv->csa_va = NULL;
   1086 		amdgpu_bo_unreserve(adev->virt.csa_obj);
   1087 	}
   1088 
   1089 	pasid = fpriv->vm.pasid;
   1090 	pd = amdgpu_bo_ref(fpriv->vm.root.base.bo);
   1091 
   1092 	amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
   1093 	amdgpu_vm_fini(adev, &fpriv->vm);
   1094 
   1095 	if (pasid)
   1096 		amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
   1097 	amdgpu_bo_unref(&pd);
   1098 
   1099 	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
   1100 		amdgpu_bo_list_put(list);
   1101 
   1102 	idr_destroy(&fpriv->bo_list_handles);
   1103 	mutex_destroy(&fpriv->bo_list_lock);
   1104 
   1105 	kfree(fpriv);
   1106 	file_priv->driver_priv = NULL;
   1107 
   1108 	pm_runtime_mark_last_busy(dev->dev);
   1109 	pm_runtime_put_autosuspend(dev->dev);
   1110 }
   1111 
   1112 /*
   1113  * VBlank related functions.
   1114  */
   1115 /**
   1116  * amdgpu_get_vblank_counter_kms - get frame count
   1117  *
   1118  * @dev: drm dev pointer
   1119  * @pipe: crtc to get the frame count from
   1120  *
   1121  * Gets the frame count on the requested crtc (all asics).
   1122  * Returns frame count on success, -EINVAL on failure.
   1123  */
   1124 u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
   1125 {
   1126 	struct amdgpu_device *adev = dev->dev_private;
   1127 	int vpos, hpos, stat;
   1128 	u32 count;
   1129 
   1130 	if (pipe >= adev->mode_info.num_crtc) {
   1131 		DRM_ERROR("Invalid crtc %u\n", pipe);
   1132 		return -EINVAL;
   1133 	}
   1134 
   1135 	/* The hw increments its frame counter at start of vsync, not at start
   1136 	 * of vblank, as is required by DRM core vblank counter handling.
   1137 	 * Cook the hw count here to make it appear to the caller as if it
   1138 	 * incremented at start of vblank. We measure distance to start of
   1139 	 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
   1140 	 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
   1141 	 * result by 1 to give the proper appearance to caller.
   1142 	 */
   1143 	if (adev->mode_info.crtcs[pipe]) {
   1144 		/* Repeat readout if needed to provide stable result if
   1145 		 * we cross start of vsync during the queries.
   1146 		 */
   1147 		do {
   1148 			count = amdgpu_display_vblank_get_counter(adev, pipe);
   1149 			/* Ask amdgpu_display_get_crtc_scanoutpos to return
   1150 			 * vpos as distance to start of vblank, instead of
   1151 			 * regular vertical scanout pos.
   1152 			 */
   1153 			stat = amdgpu_display_get_crtc_scanoutpos(
   1154 				dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
   1155 				&vpos, &hpos, NULL, NULL,
   1156 				&adev->mode_info.crtcs[pipe]->base.hwmode);
   1157 		} while (count != amdgpu_display_vblank_get_counter(adev, pipe));
   1158 
   1159 		if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
   1160 		    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
   1161 			DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
   1162 		} else {
   1163 			DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
   1164 				      pipe, vpos);
   1165 
   1166 			/* Bump counter if we are at >= leading edge of vblank,
   1167 			 * but before vsync where vpos would turn negative and
   1168 			 * the hw counter really increments.
   1169 			 */
   1170 			if (vpos >= 0)
   1171 				count++;
   1172 		}
   1173 	} else {
   1174 		/* Fallback to use value as is. */
   1175 		count = amdgpu_display_vblank_get_counter(adev, pipe);
   1176 		DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
   1177 	}
   1178 
   1179 	return count;
   1180 }
   1181 
   1182 /**
   1183  * amdgpu_enable_vblank_kms - enable vblank interrupt
   1184  *
   1185  * @dev: drm dev pointer
   1186  * @pipe: crtc to enable vblank interrupt for
   1187  *
   1188  * Enable the interrupt on the requested crtc (all asics).
   1189  * Returns 0 on success, -EINVAL on failure.
   1190  */
   1191 int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
   1192 {
   1193 	struct amdgpu_device *adev = dev->dev_private;
   1194 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
   1195 
   1196 	return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
   1197 }
   1198 
   1199 /**
   1200  * amdgpu_disable_vblank_kms - disable vblank interrupt
   1201  *
   1202  * @dev: drm dev pointer
   1203  * @pipe: crtc to disable vblank interrupt for
   1204  *
   1205  * Disable the interrupt on the requested crtc (all asics).
   1206  */
   1207 void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
   1208 {
   1209 	struct amdgpu_device *adev = dev->dev_private;
   1210 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
   1211 
   1212 	amdgpu_irq_put(adev, &adev->crtc_irq, idx);
   1213 }
   1214 
   1215 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
   1216 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1217 	DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1218 	DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1219 	DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
   1220 	DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1221 	DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1222 	/* KMS */
   1223 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1224 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1225 	DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1226 	DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1227 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1228 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1229 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1230 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1231 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
   1232 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW)
   1233 };
   1234 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
   1235 
   1236 /*
   1237  * Debugfs info
   1238  */
   1239 #if defined(CONFIG_DEBUG_FS)
   1240 
   1241 static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data)
   1242 {
   1243 	struct drm_info_node *node = (struct drm_info_node *) m->private;
   1244 	struct drm_device *dev = node->minor->dev;
   1245 	struct amdgpu_device *adev = dev->dev_private;
   1246 	struct drm_amdgpu_info_firmware fw_info;
   1247 	struct drm_amdgpu_query_fw query_fw;
   1248 	struct atom_context *ctx = adev->mode_info.atom_context;
   1249 	int ret, i;
   1250 
   1251 	/* VCE */
   1252 	query_fw.fw_type = AMDGPU_INFO_FW_VCE;
   1253 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1254 	if (ret)
   1255 		return ret;
   1256 	seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
   1257 		   fw_info.feature, fw_info.ver);
   1258 
   1259 	/* UVD */
   1260 	query_fw.fw_type = AMDGPU_INFO_FW_UVD;
   1261 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1262 	if (ret)
   1263 		return ret;
   1264 	seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
   1265 		   fw_info.feature, fw_info.ver);
   1266 
   1267 	/* GMC */
   1268 	query_fw.fw_type = AMDGPU_INFO_FW_GMC;
   1269 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1270 	if (ret)
   1271 		return ret;
   1272 	seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
   1273 		   fw_info.feature, fw_info.ver);
   1274 
   1275 	/* ME */
   1276 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
   1277 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1278 	if (ret)
   1279 		return ret;
   1280 	seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
   1281 		   fw_info.feature, fw_info.ver);
   1282 
   1283 	/* PFP */
   1284 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
   1285 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1286 	if (ret)
   1287 		return ret;
   1288 	seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
   1289 		   fw_info.feature, fw_info.ver);
   1290 
   1291 	/* CE */
   1292 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
   1293 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1294 	if (ret)
   1295 		return ret;
   1296 	seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
   1297 		   fw_info.feature, fw_info.ver);
   1298 
   1299 	/* RLC */
   1300 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
   1301 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1302 	if (ret)
   1303 		return ret;
   1304 	seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
   1305 		   fw_info.feature, fw_info.ver);
   1306 
   1307 	/* RLC SAVE RESTORE LIST CNTL */
   1308 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
   1309 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1310 	if (ret)
   1311 		return ret;
   1312 	seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
   1313 		   fw_info.feature, fw_info.ver);
   1314 
   1315 	/* RLC SAVE RESTORE LIST GPM MEM */
   1316 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
   1317 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1318 	if (ret)
   1319 		return ret;
   1320 	seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
   1321 		   fw_info.feature, fw_info.ver);
   1322 
   1323 	/* RLC SAVE RESTORE LIST SRM MEM */
   1324 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
   1325 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1326 	if (ret)
   1327 		return ret;
   1328 	seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
   1329 		   fw_info.feature, fw_info.ver);
   1330 
   1331 	/* MEC */
   1332 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
   1333 	query_fw.index = 0;
   1334 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1335 	if (ret)
   1336 		return ret;
   1337 	seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
   1338 		   fw_info.feature, fw_info.ver);
   1339 
   1340 	/* MEC2 */
   1341 	if (adev->asic_type == CHIP_KAVERI ||
   1342 	    (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) {
   1343 		query_fw.index = 1;
   1344 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1345 		if (ret)
   1346 			return ret;
   1347 		seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
   1348 			   fw_info.feature, fw_info.ver);
   1349 	}
   1350 
   1351 	/* PSP SOS */
   1352 	query_fw.fw_type = AMDGPU_INFO_FW_SOS;
   1353 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1354 	if (ret)
   1355 		return ret;
   1356 	seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
   1357 		   fw_info.feature, fw_info.ver);
   1358 
   1359 
   1360 	/* PSP ASD */
   1361 	query_fw.fw_type = AMDGPU_INFO_FW_ASD;
   1362 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1363 	if (ret)
   1364 		return ret;
   1365 	seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
   1366 		   fw_info.feature, fw_info.ver);
   1367 
   1368 	query_fw.fw_type = AMDGPU_INFO_FW_TA;
   1369 	for (i = 0; i < 2; i++) {
   1370 		query_fw.index = i;
   1371 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1372 		if (ret)
   1373 			continue;
   1374 		seq_printf(m, "TA %s feature version: %u, firmware version: 0x%08x\n",
   1375 				i ? "RAS" : "XGMI", fw_info.feature, fw_info.ver);
   1376 	}
   1377 
   1378 	/* SMC */
   1379 	query_fw.fw_type = AMDGPU_INFO_FW_SMC;
   1380 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1381 	if (ret)
   1382 		return ret;
   1383 	seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
   1384 		   fw_info.feature, fw_info.ver);
   1385 
   1386 	/* SDMA */
   1387 	query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
   1388 	for (i = 0; i < adev->sdma.num_instances; i++) {
   1389 		query_fw.index = i;
   1390 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1391 		if (ret)
   1392 			return ret;
   1393 		seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
   1394 			   i, fw_info.feature, fw_info.ver);
   1395 	}
   1396 
   1397 	/* VCN */
   1398 	query_fw.fw_type = AMDGPU_INFO_FW_VCN;
   1399 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1400 	if (ret)
   1401 		return ret;
   1402 	seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
   1403 		   fw_info.feature, fw_info.ver);
   1404 
   1405 	/* DMCU */
   1406 	query_fw.fw_type = AMDGPU_INFO_FW_DMCU;
   1407 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1408 	if (ret)
   1409 		return ret;
   1410 	seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n",
   1411 		   fw_info.feature, fw_info.ver);
   1412 
   1413 	/* DMCUB */
   1414 	query_fw.fw_type = AMDGPU_INFO_FW_DMCUB;
   1415 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
   1416 	if (ret)
   1417 		return ret;
   1418 	seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n",
   1419 		   fw_info.feature, fw_info.ver);
   1420 
   1421 
   1422 	seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
   1423 
   1424 	return 0;
   1425 }
   1426 
   1427 static const struct drm_info_list amdgpu_firmware_info_list[] = {
   1428 	{"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL},
   1429 };
   1430 #endif
   1431 
   1432 int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
   1433 {
   1434 #if defined(CONFIG_DEBUG_FS)
   1435 	return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list,
   1436 					ARRAY_SIZE(amdgpu_firmware_info_list));
   1437 #else
   1438 	return 0;
   1439 #endif
   1440 }
   1441