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